Skip to content

Instantly share code, notes, and snippets.

@mdobson
Created June 6, 2014 16:51
Show Gist options
  • Save mdobson/5268d0a73b065b33b4f8 to your computer and use it in GitHub Desktop.
Save mdobson/5268d0a73b065b33b4f8 to your computer and use it in GitHub Desktop.
Driver example
var Driver = require('zetta').Driver;
var util = require('util');
// Name your driver the device it's modeling
var Microphone = module.exports = function(port){
Driver.call(this);
// not saved and not exposed in api
this._port = port;
// saved and exposed in api
this.amplitude = 123;
};
util.inherits(Microphone, Driver);
// must include init function
Microphone.prototype.init = function(config) {
config
.state('ready')
.name('Matt\'s Sound Sensor') // name is optional but our examples should show it.
.type('sound')
.stream('amplitude', this.streamAmplitude); // .stream{Name} as function
}
Microphone.prototype.streamAmplitude = function(stream) {
var self = this;
this._port.on('data', function(d) {
var data = Number(d.toString());
self.amplitude = data;
stream.write(data);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment