
/**
 * Module dependencies
 */

var co        = require('co');
var gpio      = require('rpi-gpio');
var log       = require('logule').init(module, 'cascade');
var model     = require('./model.js');
var thunkify  = require('thunkify');
var usb       = require('usb-detection');
var wait      = require('co-wait');

/**
 * Initialize Cascade
 *
 * @constructor
 */

function Cascade() {

  /**
   * usb detection handlers
   * @see https://github.com/KABA-CCEAC/node-usb-detection
   */

  usb.on('add', this._handleAddDevice);
  usb.on('remove', this._handleRemoveDevice);

  gpio.setup(7, gpio.DIR_OUT);

  // ..

  log.info('running!');
}

// ------------------------------------------------------------------------------------
// USB handlers
// ------------------------------------------------------------------------------------

/**
 * Handle usb-detection `add` device event
 *
 * @param err
 * @param devices
 * @private
 */

Cascade.prototype._handleAddDevice = function(device) {
  dumpCoins();
/*  co(function * () {
    var error = yield write(7, true);
    if (error) throw error;
  })();*/
};

/**
 * Handle usb-detection `remove` device event
 *
 * @param err
 * @param devices
 * @private
 */

Cascade.prototype._handleRemoveDevice = function(device) {
/*  co(function * () {
    var error = yield write(7, false);
    if (error) throw error;
  })();*/
};

// ------------------------------------------------------------------------------------
// RPI handlers
// ------------------------------------------------------------------------------------

function write(channel, value) {
  return function(fn) {
    gpio.write(channel, value, fn);
  }
}

function dumpCoins() {
  co(function * () {
    var error = yield write(7, true);
    if (!error) {
      yield wait(2500);
      error = yield write(7, false);
      if (error) throw error;
    } else {
      throw error;
    }
  })();
}

/**
 * Expose `Cascade`
 * @type {Cascade}
 */

module.exports = Cascade;

@@ -308,16 +308,55 @@
 ait');%0A%0A
+var write     = thunkify(gpio.write);%0A%0A
 /**%0A * I
@@ -606,16 +606,167 @@
 vice);%0A%0A
+%0A  /**%0A   * setup rpi pins%0A   *%0A   * GPIO 7 = change slot%0A   * GPIO X = status light%0A   *%0A   * @see https://github.com/JamesBarwell/rpi-gpio.js%0A   */%0A%0A
   gpio.s
@@ -1179,119 +1179,20 @@
 %7B%0A  
-dumpCoins();%0A/*  co(function * () %7B%0A    var error = yield write(7, true);%0A    if (error) throw error;%0A  %7D)
+cycleSlot
 ();
-*/
 %0A%7D;%0A
@@ -1357,107 +1357,8 @@
 ) %7B%0A
-/*  co(function * () %7B%0A    var error = yield write(7, false);%0A    if (error) throw error;%0A  %7D)();*/
 %0A%7D;%0A
@@ -1454,20 +1454,19 @@
 // RPI h
-andl
+elp
 ers%0A// -
@@ -1554,130 +1554,145 @@
 --%0A%0A
-function write(channel, value) %7B%0A  return function(fn) %7B%0A    gpio.write(channel, value, fn);%0A  %7D%0A%7D%0A%0Afunction dumpCoins() %7B
+/**%0A * Set pin high and after %60delay%60 set pin low%0A * @param delay in milliseconds%0A */%0A%0Afunction cycleSlot(delay) %7B%0A%0A  log.info('cycle');%0A
 %0A  c
@@ -1703,17 +1703,16 @@
 nction *
- 
 () %7B%0A   
@@ -1717,24 +1717,49 @@
    var error
+;%0A    // ..%0A    if (error
  = yield wri
@@ -1773,25 +1773,41 @@
 rue)
-;
+) %7B
 %0A    
-if (!error)
+  throw error;%0A    %7D else
  %7B%0A 
@@ -1826,12 +1826,13 @@
 ait(
-2500
+delay
 );%0A 
@@ -1836,16 +1836,20 @@
 ;%0A      
+if (
 error = 
@@ -1869,33 +1869,16 @@
 , false)
-;%0A      if (error
 ) throw 
@@ -1893,48 +1893,208 @@
    %7D
- else %7B%0A      throw error;%0A    %7D%0A  %7D)();
+%0A  %7D)();%0A%7D%0A%0A/**%0A * Write value to channel%0A * @param channel%0A * @param value%0A * @returns %7BFunction%7D%0A */%0A%0Afunction write(channel, value) %7B%0A  return function(fn) %7B%0A    gpio.write(channel, value, fn);%0A  %7D
 %0A%7D%0A%0A

remove debugging, add thunkify, cleaning up