index.js
function Button (hardware, callback) {
  var self = this;

  // Set hardware connection of the object
  self.hardware = hardware;

  // Object properties
  self.delay = 100;
  self.pressed = false;

  // Begin listening for events
  self.hardware.on('fall', function () {
    self._press();
  });

  self.hardware.on('rise', function () {
    self._release();
  });

  // Make sure the events get emitted, even if late
  setInterval(function () {
    if(!self.hardware.read()) {
      self._press();
    } else {
      self._release();
    }
  }, self.delay);

  // Emit the ready event when everything is set up
  setImmediate(function emitReady() {
    self.emit('ready');
  });
  // Call the callback with object
  if (callback) {
    callback(null, self);
  }
}
function Button (hardware, callback) {
  var self = this;

  // Set hardware connection of the object
  self.hardware = hardware;

  // Object properties
  self.delay = 100;
  self.pressed = false;

  // Begin listening for events
  self.hardware.on('fall', function () {
    self._press();
  });

  self.hardware.on('rise', function () {
    self._release();
  });

  // Make sure the events get emitted, even if late
  setInterval(function () {
    if(!self.hardware.read()) {
      self._press();
    } else {
      self._release();
    }
  }, self.delay);

  // Emit the ready event when everything is set up
  setImmediate(function emitReady() {
    self.emit('ready');
  });
  // Call the callback with object
  if (callback) {
    callback(null, self);
  }
}
function Button ( hardware , callback ) { var self = this ; // Set hardware connection of the object self . hardware = hardware ; // Object properties self . delay = 100 ; self . pressed = false ; // Begin listening for events self . hardware . on ( 'fall' , function ( ) { self . _press ( ) ; } ) ; self . hardware . on ( 'rise' , function ( ) { self . _release ( ) ; } ) ; // Make sure the events get emitted, even if late setInterval ( function ( ) { if ( ! self . hardware . read ( ) ) { self . _press ( ) ; } else { self . _release ( ) ; } } , self . delay ) ; // Emit the ready event when everything is set up setImmediate ( function emitReady ( ) { self . emit ( 'ready' ) ; } ) ; // Call the callback with object if ( callback ) { callback ( null , self ) ; } }
Constructor function to instantiate the hardware object
Constructor function to instantiate the hardware object