// Load GrovePi module
var GrovePi = require('node-grovepi').GrovePi;
if (typeof GrovePi.sensors.DigitalOutput !== 'function') {
// old npm module impl forgot "module.exports," resulting in an empty JS object.
// fill the content by myself...
// cf. https://github.com/DexterInd/GrovePi/commit/513c67e33e9f8fb99617067ea40d8e52da4e014f#diff-15b138624f4846fbbec7ff8dd7031505
GrovePi.sensors.DigitalOutput = function GenericDigitalOutputSensor(pin) {
GrovePi.sensors.base.Digital.apply(this, Array.prototype.slice.call(arguments));
};
DigitalOutput.prototype = new GrovePi.sensors.base.Digital();
DigitalOutput.prototype.on = function() {
this.board.pinMode(this.board.OUTPUT);
var write = this.board.writeBytes(
GrovePi.commands.dWrite.concat([this.pin, 1, GrovePi.commands.unused]));
return !!write;
};
DigitalOutput.prototype.off = function() {
this.board.pinMode(this.board.OUTPUT);
var write = this.board.writeBytes(
GrovePi.commands.dWrite.concat([this.pin, 0, GrovePi.commands.unused]));
return !!write;
};
}
// Initialize connection to the GrovePi board.
var board = new GrovePi.board({
debug: true
, onError: function (err) {
console.error(err);
}
, onInit: function (init) {
if (!init) {
// onError is called.
return;
}
console.log('GrovePi version:', board.version());
led = new GrovePi.sensors.DigitalOutput(2);
waiting = setInterval(blink, 1000);
}
});
board.init();
// Turn the LED on and off 10 times, pausing one second
// between transitions
var led, waiting, i = 0;
function blink() {
if (i ++ % 2 === 0) led.on();
else led.off();
if (i >= 20) onExit();
}
process.on('SIGINT', onExit);
function onExit(err) {
if (waiting) clearInterval(waiting);
if (board) board.close();
process.removeAllListeners();
process.exit();
if (err) console.error(err);
}
// Create a box to hold the LED
var f3js = require('f3js')
, x = 10
, y = 10
, width = 130
, height = 105
, thickness = 45;
// put base board
var rect = f3js.drawJointRectangle(
x, y, width, height);
f3js.add(led, x + width / 2, y + height / 2);
// 配線穴
f3js.drawRectangle(x + width / 2 - 10, y + height / 2 + 20, 20, 10);
// put side boards
var planes = rect.extrude(thickness);
var margin = 5;
planes[0].x = x;
planes[0].y = y + height + margin;
f3js.add(planes[0]);
planes[1].x = x + width;
planes[1].y = y + height + margin;
f3js.add(planes[1]);
planes[2].x = x + height;
planes[2].y = y + height + thickness + margin;
f3js.add(planes[2]);
planes[3].x = x;
planes[3].y = y + height + thickness + margin;
f3js.add(planes[3]);
planes[4].x = width; planes[4].y = 0;
f3js.add(planes[4]);
// put a side hall
f3js.drawRectangle(
x + 5, y + height + thickness * 2 + margin - 3 - 15,
height - 10, 10);