Skip to content

Instantly share code, notes, and snippets.

@AdamMagaluk
Created June 3, 2014 17:52
Show Gist options
  • Save AdamMagaluk/dd69c5d766a12750560d to your computer and use it in GitHub Desktop.
Save AdamMagaluk/dd69c5d766a12750560d to your computer and use it in GitHub Desktop.
Latest Zetta
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);
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);
});
};
//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);
};
@kevinswiber
Copy link

Newer app.js

var zetta = require('zetta');
var Arduino = require('./arduino');

var app = zetta();

app.name('i-heard-that');
app.expose('*');
app.load(Arduino);

app.observe(['sound', 'lcd'], function(sound, lcd) {
  var stream = sound.getStream('amplitude');

  stream.on('data', function(value) {
    if (value > 160) {
      lcd.call('update', 'I heard that!');
    }
  });
});

app.listen(process.env.PORT || 3000);

@mdobson
Copy link

mdobson commented Jun 4, 2014

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