Created
June 3, 2014 17:52
-
-
Save AdamMagaluk/dd69c5d766a12750560d to your computer and use it in GitHub Desktop.
Latest Zetta
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 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); |
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 util = require('util'); | |
var Scout = require('zetta').Scout; | |
var SerialPort = require('serialport').SerialPort; | |
var Sound = require('./sound'); | |
var ArduinoScout = module.exports = function(runtime) { | |
Scout.call(this); | |
this.runtime = runtime; | |
this.portName = '/dev/tty.usbserial'; | |
}; | |
util.inherits(ArduinoScout, Scout); | |
ArduinoScout.prototype.init = function(cb){ | |
var self = this; | |
this.runtime.implode(); | |
var port = new SerialPort(portName, function(err) { | |
if(err) { | |
return cb(err); | |
} | |
self.discover(Sound, port); | |
}); | |
}; |
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, { binary: true }); | |
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.write(Number(d.toString())); | |
}); | |
// or | |
this._port.pipe(stream); | |
}; |
var Driver = require('zetta').Driver;
var util = require('util');
var Sound = module.exports = function(port){
Driver.call(this);
this._port = port;
};
util.inherits(Sound, Driver);
Sound.prototype.init = function(config) {
config
.state('ready')
.name('Matt\'s Sound Sensor')
.type('sound')
.stream('amplitude', this.streamAmplitude);
}
Sound.prototype.streamAmplitude = function(stream) {
var self = this;
this._port.on('data', function(d) {
var data = Number(d.toString());
self.set('amplitude', data);
stream.write(data);
});
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Newer app.js