// configure PWM: http://digitaldrive.com/?p=146

// sudo sh c "echo am33xx_pwm > /sys/devices/bone_capemgr.9/slots"

// sudo sh c "echo bone_pwm_P8_13 > /sys/devices/bone_capemgr.9/slots"

// sudo sh c "echo bone_pwm_P8_34 > /sys/devices/bone_capemgr.9/slots"

// ls /sys/devices/ocp.3/

// cd /var/lib/cloud9/websocketserver/

// ./led_pins.sh


// cd /var/lib/cloud9/websocketserver/beagleboneblack/websocketclient

// node webserver.js


var WebSocketServer = require('ws').Server;

var bbbPWM = require("./bbbpwm");

var b = require('bonescript');


// Instantiate WebSocket server.

var wss = new WebSocketServer({

   port : 8150

});


// GPIO 3.3V 4mA 6mA

// VDD 3.3 3.3V 250mA

// VDD 5V 5V* 1000mA (VDD only works when the 5V barrel jack is used)

// SYS 5V 5V 250mA


// the last parameter is the period (here: 20ms and 40ms)

var pwm1 = new bbbPWM('/sys/devices/ocp.3/pwm_test_P8_13.11/', 20000000);

var pwm2 = new bbbPWM('/sys/devices/ocp.3/pwm_test_P8_34.12/', 40000000);


// P8_13: GPOI0_23 (0+23=23)

// P8_34: GPOI2_17 (64+17=81)


/* direction pins */

var dirpwm1 = 'P8_31'; // GPIO1_13 (32+1=33)

var dirpwm2 = 'P8_16'; // GPIO1_14 (32+14=46)


b.pinMode(dirpwm1, b.OUTPUT);

b.pinMode(dirpwm2, b.OUTPUT);


/* direction to be forward or backward */

b.digitalWrite(dirpwm1, b.LOW);

b.digitalWrite(dirpwm2, b.LOW);


// LEDs

b.digitalWrite('P8_32', b.HIGH); // GPIO0_11: 32+11=43

b.digitalWrite('P8_38', b.HIGH); // GPIO2_15: 32+32+15=79

b.digitalWrite('P8_37', b.HIGH); // GPIO2_14: 32+32+14=78

b.digitalWrite('P8_29', b.HIGH); // GPIO2_23: 32+32+23=87

b.digitalWrite('P8_23', b.LOW); // GPIO1_4: 32+4=36

b.digitalWrite('P8_21', b.LOW); // GPIO1_30: 32+30=62

b.digitalWrite('P8_17', b.LOW); // GPIO0_27: 0+27=27


// Handle connections

wss.on('connection', function(ws) {


   // Send message to client that connection has been made.

   ws.send('BBB WebSocket Server Connected!!!');


   // Handle incoming messages.

   ws.on('message', function(message) {


       // set run to 0.

       if (message == 'speed_Up') {

           pwm1.incSpeed();

           pwm2.incSpeed();

           pwm1.go();

           pwm2.go();

           ws.send('The car is speeding UP: ' + pwm1.getSpeed());

       }


       // set run to 1.

       else if (message == 'speed_Down') {

           pwm1.decSpeed();

           pwm2.decSpeed();

           pwm1.go();

           pwm2.go();

           ws.send('The car is speeding DOWN: ' + pwm1.getSpeed());

       }


       // set run to 0.

       else if (message == 'turn_On') {

           pwm1.setSpeed(1);

           pwm2.setSpeed(1);

           pwm1.turnOn();

           pwm2.turnOn();

           ws.send('The car starts moving.');

       }


       else if (message == 'turn_Off') {

           pwm1.turnOff();

           pwm2.turnOff();

           ws.send('The car stops.');

       }


       else if (message == 'turn_Left') {

           pwm1.incSpeed();

           pwm2.decSpeed();

           pwm1.go();

           pwm2.go();

           ws.send('The car turns left.');

       }


       else if (message == 'turn_Right') {

           pwm1.decSpeed();

           pwm2.incSpeed();

           pwm1.go();

           pwm2.go();

           ws.send('The car turns right.');

       }

   });


   // When connection closes.

   ws.on('close', function( ) {

       console.log('stopping client interval');

   });

});