/**
 * LED blinking app for Intel Edison
 * see http://f3js.org/projects/edit?id=181 for Raspberry Pi version 
 */
// variables
var n = 4 // number of LEDs [1,4]
  , width = 130
  , height = 105
  , thickness = 45
// view
var f3js = require('f3js')
  , c = f3js.createContainer()
  , rect = c.drawJointRectangle(0, 0, width, height)
  , line = c.drawLine(30, height/2 - 5, width - 30, height/2 - 5);
// LEDs
var groveSensor = require('jsupm_grove')
  , leds = [];
// use this line as the guide path
line.layout = { name: 'distribute', rotate: false };
for (var i = 0; i < n; i ++) {
  var led = new groveSensor.GroveLed(i+2);
  // put an LED module
  var ledc = c.add(led, line);
  // open a hole for the wire
  ledc.drawRectangle(- 10, 15, 20, 10);
  // start blinking the LED
  leds.push(led);
  setTimeout(function () {
    setInterval(blink(i), 1000);
  }, i * 100);
}
// make the specified LED blink
function blink(i) {
  var id = i, on = false;
  return function () {
    leds[id][on?'off':'on']();
    on = !on;
  };
}
// set some offset
c.x = 10; c.y = 10;
// extrude the main panel to form a box shape
rect.jointHeight = 2;
var planes = rect.extrude(thickness);
// put side boards
planes[0].x = 0;
planes[0].y = height + rect.jointHeight;
c.add(planes[0]);
planes[1].x = width;
planes[1].y = height + rect.jointHeight;
c.add(planes[1]);
planes[2].x = height;
planes[2].y = height + thickness + rect.jointHeight;
c.add(planes[2]);
planes[3].x = 0;
planes[3].y = height + thickness + rect.jointHeight;
c.add(planes[3]);
planes[4].x = width; planes[4].y = 0;
c.add(planes[4]);
// put a side hall
planes[3].drawRectangle(
  5, thickness - rect.jointHeight - 15,
  height - 10, 10);
// initialize the application
console.log('ready');
process.on('SIGINT', onExit);
// clean up before the death
function onExit() {
  if (i % 2 !== 0) led.off();
  if (waiting) clearInterval(waiting);
}