Skip to content

Instantly share code, notes, and snippets.

@AdamMagaluk
Created June 2, 2014 20:18
Show Gist options
  • Save AdamMagaluk/2d32976d1fff1d6280b4 to your computer and use it in GitHub Desktop.
Save AdamMagaluk/2d32976d1fff1d6280b4 to your computer and use it in GitHub Desktop.
Zetta Driver Options
//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);
};
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);
});
};
@kevinswiber
Copy link

Latest.

//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);

  // or

};

// app.js

var stream = device.stream('sound')

device.stream('sound').read(function(data) {
  doSomething(data);
});

stream.read(function(data) {
  doSomething(data); // returns data chunk OR object
});

// alias for:

stream.on('readable', function() {
  var data;
  while (data = stream.read()) {
    doSomething(data);
  }
});

@kevinswiber
Copy link

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