Created
April 16, 2017 09:11
-
-
Save vaclavbohac/f368a27d3e57730dd190198b4beaf3ae to your computer and use it in GitHub Desktop.
Double Clap Lights
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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