var mraa = require('mraa');

// Map GPIO block pins to MRAA pin numbers
// Reference: https://learn.sparkfun.com/tutorials/installing-libmraa-on-ubilinux-for-edison
var pins = {
  GP44: 31,
  GP45: 45,
  GP46: 32,
  GP47: 46
};

// Initialize LED controls
var leds = {
  R: new mraa.Gpio(pins['GP45']),
  G: new mraa.Gpio(pins['GP44']),
  B: new mraa.Gpio(pins['GP47']),
  Y: new mraa.Gpio(pins['GP46'])
};

// Set direction of LED controls to out
for(var color in leds) {
  leds[color].dir(mraa.DIR_OUT);
}

function toggleLed(led, state) {
  led.write(state ? 1 : 0);
}

function toggleLeds(leds, state) {
  for(var color in leds) {
    leds[color].write(state ? 1 : 0);
  }
}

// WebSocket communications
module.exports = function (socket) {
  socket.on('red', function(data) {
    toggleLed(leds['R'], data.state);
  });

  socket.on('green', function(data) {
    toggleLed(leds['G'], data.state);
  });

  socket.on('blue', function(data) {
    toggleLed(leds['B'], data.state);
  });

  socket.on('yellow', function(data) {
    toggleLed(leds['Y'], data.state);
  });

  // Handle Ctrl+C event
  process.on('SIGINT', function() {
    toggleLeds(leds, false);
    socket.disconnect();
    process.exit();
  });
};

@@ -264,17 +264,19 @@
 s = %7B%0A  
-R
+red
 : new mr
@@ -300,17 +300,21 @@
 5'%5D),%0A  
-G
+green
 : new mr
@@ -338,17 +338,20 @@
 4'%5D),%0A  
-B
+blue
 : new mr
@@ -375,17 +375,22 @@
 7'%5D),%0A  
-Y
+yellow
 : new mr
@@ -687,16 +687,114 @@
 %0A  %7D%0A%7D%0A%0A
+function printLedState(color, state) %7B%0A  console.log('color: ' + color + ', state: ' + state);%0A%7D%0A%0A
 // WebSo
@@ -910,11 +910,51 @@
 ds%5B'
-R'%5D
+red'%5D, data.state);%0A    printLedState('red'
 , da
@@ -1033,11 +1033,55 @@
 ds%5B'
-G'%5D
+green'%5D, data.state);%0A    printLedState('green'
 , da
@@ -1159,11 +1159,53 @@
 ds%5B'
-B'%5D
+blue'%5D, data.state);%0A    printLedState('blue'
 , da
@@ -1285,11 +1285,57 @@
 ds%5B'
-Y'%5D
+yellow'%5D, data.state);%0A    printLedState('yellow'
 , da

Update 'leds' key and add print statements