Skip to content

Instantly share code, notes, and snippets.

@vaclavbohac
Created April 16, 2017 09:11
Show Gist options
  • Save vaclavbohac/f368a27d3e57730dd190198b4beaf3ae to your computer and use it in GitHub Desktop.
Save vaclavbohac/f368a27d3e57730dd190198b4beaf3ae to your computer and use it in GitHub Desktop.
Double Clap Lights
import { Board, Sensor } from 'johnny-five';
import { Observable, Observer } from 'rxjs/Rx';
// The pin that's microphone connected to
const SENSOR_PORT: number = 3;
// Delay between the two claps
const DCLAP_DELAY: number = 500;
const board = new Board();
function fromSensor<T>(sensor: Sensor): Observable<T> {
return Observable.create((observer: Observer<T>) => {
sensor.on('change', () => {
observer.next((this.value as T));
});
});
}
board.on('ready', () => {
const mic$ = fromSensor<number>(new Sensor(SENSOR_PORT));
const doubleClap$ = mic$
.bufferTime<number>(DCLAP_DELAY)
.map((list: number[]) => list.length)
.filter((x: number) => x === 2);
doubleClap$.subscribe(console.log);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment