Created
June 2, 2014 20:18
-
-
Save AdamMagaluk/2d32976d1fff1d6280b4 to your computer and use it in GitHub Desktop.
Zetta Driver Options
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
//Driver is an event emitter | |
var Driver = require('zetta').Driver; | |
var util = require('util'); | |
var Sound = module.exports = function(port){ | |
Driver.call(this); | |
this._port = port; | |
}; | |
//UUID is given to us by the Driver constructor | |
util.inherits(Sound, Driver); | |
//We added state, name, and type as functions to be called by config | |
//Setting up the state machine in two different places is strange | |
Sound.prototype.init = function(config) { | |
config | |
.state('ready') | |
.name('Matt\'s Sound Sensor') | |
.type('sound') | |
//We use set to save user based stuff in the registry | |
.set('Key', 'Value') | |
.stream('sound', this.streamSound); | |
var Value = this.get('Key'); | |
//Update the registry | |
this.save(); | |
} | |
Sound.prototype.streamSound = function(stream) { | |
//stream passed into this function is a writable stream | |
this._port.on('data', function(d) { | |
stream.emit('data', Number(d.toString())); | |
}); | |
// or | |
this._port.pipe(stream); | |
}; |
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
var iphash = require('../lib/iphash'); | |
var SoundDriver = module.exports = function(id, emitter, ip) { | |
this.type = 'sound'; | |
this.name = 'sound-'+iphash(ip); | |
this._emitter = emitter; | |
this.data = {}; | |
this.id = id; | |
this.state = 'on'; | |
}; | |
SoundDriver.prototype.init = function(config) { | |
config | |
.stream('sound', this.streamSound); | |
}; | |
SoundDriver.prototype.streamSound = function(emitter) { | |
var self = this; | |
this._emitter.on('sound', function(data) { | |
emitter.emit('data', data); | |
self.emit('update', data); | |
}); | |
}; | |
Latest app.js
var zetta = require('zetta');
var Arduino = require('./arduino');
var app = zetta();
app.name('i-heard-that'); // give the empress a name
app.expose('*'); // explore other arguments
app.load(Arduino); // load that scout in
app.observe('sound', function(microphone) {
app.observe('lcd', function(lcd) {
var soundStream = microphone.getStream('sound'); // .stream or .getStream?
soundStream.on('data', function(value) {
if (value > 160) {
lcd.call('update', 'I heard that!');
}
});
// ^^ :D :D
});
});
app.listen(process.env.PORT || 3000);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Latest.