Last active
August 29, 2015 14:23
-
-
Save rc1/947336a29073b6e31300 to your computer and use it in GitHub Desktop.
Touch & Speak: Bare Conductive's TouchBoard with Bacon.js
This file contains hidden or 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
| // Touch Board Example | |
| // =================== | |
| // Connects to a touch board and 'says' a word when a touch is detecte | |
| // Modules | |
| // ======= | |
| var serialPort = require( 'serialport' ); | |
| var Bacon = require( 'baconjs' ); | |
| var W = require( 'w-js' ); | |
| var say = require( 'say' ); | |
| // Touchboard Module | |
| // ================= | |
| // | |
| // Example usage: | |
| // | |
| // touchBoard() | |
| // .error( doThrowError ) | |
| // .success( onReady ); | |
| // | |
| // function onReady ( touchBoard ) { | |
| // | |
| // } | |
| function TouchBoard ( comPort ) { | |
| return W.promise( function ( resolve, reject ) { | |
| // Open the device | |
| var serial = new serialPort.SerialPort( comPort, { | |
| parser: serialPort.parsers.readline( '\r\n' ) | |
| }); | |
| serial.open( function ( err ) { | |
| if ( err ) { return reject( err ); } | |
| // Set it up | |
| var dataStream = Bacon.fromEvent( serial, 'data' ) | |
| .map( buffer => buffer.toString() ) | |
| .map( d => d.split( ' ' ) ) | |
| .filter( arr => arr.length > 0 ) | |
| .filter( arr => arr[ 0 ].indexOf( ':' ) > -1 ); | |
| function filterByType ( stream, eventType ) { | |
| return stream | |
| .filter( arr => arr[ 0 ] === eventType + ':' ) | |
| .map( W.rest ) | |
| .skipDuplicates( function ( oldArr, newArr ) { | |
| return !oldArr.reduce( ( acc, v, idx ) => { if ( v !== newArr[ idx ] ) { acc = true; } return acc; }, false ); | |
| }); | |
| } | |
| // Retuen the different data streams | |
| resolve( { | |
| dataStream: dataStream, | |
| touchStream: filterByType( dataStream, 'TOUCH' ), | |
| thresholdStream: filterByType( dataStream, 'TTHS' ), | |
| releaseStream: filterByType( dataStream,'RTHS' ), | |
| filteredStream: filterByType( dataStream, 'FDAT' ), | |
| baselineStream: filterByType( dataStream, 'BVAL' ), | |
| differenceStream: filterByType( dataStream, 'DIFF' ) | |
| }); | |
| }); | |
| }); | |
| } | |
| // Speaking | |
| // ======== | |
| // The speachBus is a bacon stream which we can push *values* on to. I.e. we can push things we want to say on to this stream | |
| var speachBus = new Bacon.Bus(); | |
| // Make the speachBus consumer that when recieveing data it will *say* the data | |
| speachBus | |
| .throttle( 100 ) | |
| .onValue( text => say.speak( 'Alex', text ) ); | |
| // Implement the TouchBoard | |
| // ======================== | |
| TouchBoard( '/dev/cu.usbmodem1411' ) | |
| .error( doThrowError ) | |
| .success( function ( touchBoard ) { | |
| sayOnPin( 0, 'happy' ); | |
| sayOnPin( 1, 'birthday' ); | |
| sayOnPin( 2, 'to' ); | |
| sayOnPin( 3, 'you' ); | |
| sayOnPin( 4, 'Jemma' ); | |
| function sayOnPin( pin, text ) { | |
| touchBoard.touchStream | |
| .map( arr => arr[ pin ] ) | |
| .skipDuplicates() | |
| .filter( v => v == 1 ) | |
| .onValue( function () { | |
| console.log( '------------------' ); | |
| console.log( text ); | |
| speachBus.push( text ); | |
| }); | |
| } | |
| }); | |
| // Utils | |
| // ===== | |
| function doThrowError( err ) { throw err; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment