var keypress = require('keypress');
var noble = require('noble');

var FORWARD = 127;
var BACKWARD = -127;

console.log('noble - kickass');

keypress(process.stdin);

process.stdin.setRawMode(true);
process.stdin.resume();

process.stdin.on('keypress', function (ch, key) {
  console.log(key.name);

  if (key.name === 'q') {
    setMotor([0, 0, 0, 0]);
  } else if (key.name === 'up') {
    setMotor([FORWARD, 0, BACKWARD, 0]);
  } else if (key.name === 'down') {
    setMotor([BACKWARD, 0, FORWARD, 0]);
  } else if (key.name === 'left') {
    setMotor([0, FORWARD, 0, BACKWARD]);
  } else if (key.name === 'right') {
    setMotor([0, BACKWARD, 0, FORWARD]);
  } else if (key.name === 'r') {
    setMotor([FORWARD, FORWARD, FORWARD, FORWARD]);
  } else if (key.name === 'l') {
    setMotor([BACKWARD, BACKWARD, BACKWARD, BACKWARD]);
  }


  if (key && key.ctrl && key.name === 'c') {
    process.stdin.pause();
    process.exit();
  }
});


noble.on('stateChange', function(state) {
  console.log('on -> stateChange: ' + state);

  if (state === 'poweredOn') {
    noble.startScanning(['51d0']);
  } else {
    noble.stopScanning();
  }
});

var motorCharacteristics = null;

function setMotor(speeds) {
  if (motorCharacteristics) {
    for (var i = 0; i < speeds.length; i++) {
      var buffer = new Buffer(1);

      buffer.writeInt8(speeds[i], 0);

      motorCharacteristics[i].write(buffer);
    }
  }
}

noble.on('discover', function(peripheral) {
  console.log('on -> discover: ' + peripheral);

  noble.stopScanning();

  peripheral.connect(function() {
    peripheral.discoverSomeServicesAndCharacteristics(['51d0'], [], function(error, services, characteristics) {

      motorCharacteristics = characteristics;


      // peripheral.disconnect(function() {
      //   process.exit(0);
      // });
    });
  });
});




@@ -327,24 +327,25 @@
     setMotor
+s
 (%5B0, 0, 0, 0
@@ -386,32 +386,33 @@
 ) %7B%0A    setMotor
+s
 (%5BFORWARD, 0, BA
@@ -464,32 +464,33 @@
 ) %7B%0A    setMotor
+s
 (%5BBACKWARD, 0, F
@@ -546,24 +546,25 @@
     setMotor
+s
 (%5B0, FORWARD
@@ -629,16 +629,17 @@
 setMotor
+s
 (%5B0, BAC
@@ -686,17 +686,17 @@
 me === '
-r
+l
 ') %7B%0A   
@@ -704,16 +704,17 @@
 setMotor
+s
 (%5BFORWAR
@@ -764,33 +764,33 @@
  (key.name === '
-l
+r
 ') %7B%0A    setMoto
@@ -790,16 +790,17 @@
 setMotor
+s
 (%5BBACKWA
@@ -1167,17 +1167,16 @@
 teristic
-s
  = null;
@@ -1194,16 +1194,17 @@
 setMotor
+s
 (speeds)
@@ -1231,21 +1231,51 @@
 teristic
-s
 ) %7B%0A
+    var data = new Buffer(4);%0A%0A
     for 
@@ -1322,49 +1322,12 @@
     
-var buffer = new Buffer(1);%0A%0A      buffer
+data
 .wri
@@ -1348,15 +1348,19 @@
 i%5D, 
-0
+i
 );%0A
-%0A
   
+  %7D%0A%0A
     
@@ -1382,33 +1382,21 @@
 stic
-s%5Bi%5D
 .write(
-buffer);%0A    %7D
+data);
 %0A  %7D
@@ -1690,17 +1690,16 @@
 teristic
-s
  = chara
@@ -1712,95 +1712,11 @@
 tics
-;%0A%0A%0A      // peripheral.disconnect(function() %7B%0A      //   process.exit(0);%0A      // %7D)
+%5B0%5D
 ;%0A  

update for single motor characteristic