Provide the source code of a microcontroller or tiny computer in JavaScript. Node.js-based computers are supported. Require f3js
package and use its API to design the device enclosure.
var GroveSensor = require('jsupm_grove');
var button = new GroveSensor.GroveButton(2);
var motor = new GroveSensor.GroveLed(3);
var GCL = require("jsupm_grovecircularled");
var circle = new GCL.GroveCircularLED(6, 5);
var LCD = require("jsupm_i2clcd");
var lcd = new LCD.Jhd1313m1(6, 0x3E, 0x62);
var f3js = require('f3js')
, x = 10
, y = 10
, width = 160
, height = 105
, thickness = 42
, dw = 7 /* かみ合わせ幅 [5, 10] */
, dh = 5 /* 板厚[mm] [1, 5] */
, useCountdown = true /* Use cowntdown */;
var p = f3js.createPath();
p.moveTo(x, y);
p.dovetailTo(x + width, y, dw, dh);
p.dovetailTo(x + width, y + height, dw, dh);
p.dovetailTo(x, y + height, dw, dh);
p.dovetailTo(x, y, dw, dh);
p.close();
p.fill = false;
var ps = p.extrude(45);
ps[0].x = x + width + 5;
ps[0].y = y + dh;
ps[1].x = x + width + 5;
ps[1].y = y + (thickness + 5) * 2 + dh;
ps[2].x = x + width + 5;
ps[2].y = y + thickness + 5 + dh;
ps[3].x = x + width + 5;
ps[3].y = y + (thickness + 5) * 3 + dh;
ps[4].x = 0;
ps[4].y = height + 5;
for (var i = 0; i < ps.length; i ++) {
f3js.add(ps[i]);
}
f3js.drawRectangle(
x + width + dh + 10
, y + (thickness + 5) * 2 + thickness - 12 - dh * 2
, height - dh * 2 - 10
, 10);
f3js.add(circle, x + 130, y + 30);
f3js.add(lcd, x + 50, y + 30);
// Vibration motor
f3js.drawRectangle(x + 15, y + 60, 10, 4);
f3js.add(button, x + 20, y + 80);
// Sensor
f3js.drawRectangle(x + 135, y + 60, 10, 4);
f3js.add(button, x + 140, y + 80);
var idleInterval = false;
function startIdle() {
idleInterval = setInterval(function() {
if (button.value() > 0) {
clearInterval(idleInterval);
idleInterval = false;
startGame();
}
}, 100);
}
var interval = false, score;
function stopGame() {
if (interval) {
clearInterval(interval);
interval = false;
}
if (!idleInterval) {
startIdle();
}
}
function startGame() {
for (i = 0; i < 2; i++) {
lcd.setCursor(i, 0);
lcd.write(' ');
}
var level = 0, jump = 0, pause = 0;
var obstacles = [];
score = 0;
interval = setInterval(function() {
if (pause > 1) {
pause -= 1;
return;
} else if (pause === 1) {
motor.write(0);
lcd.setColor(255, 255, 255);
jump = 0;
obstacles = [];
pause -= 1;
stopGame();
return;
}
//circle.setSpinner(level);
//level = (level + 1) % 24;
if (jump === 0 && button.value() > 0)
jump = 3;
else if (jump !== 0)
jump -= 1;
console.log('jump: ' + obstacles);
if (Math.random() < 0.1)
obstacles.push(15);
for (i = 0; i < 2; i++) {
lcd.setCursor(i, 0);
lcd.write(' ');
}
if (jump > 0)
lcd.setCursor(0, 0);
else
lcd.setCursor(1, 0);
lcd.write('O');
for (i = 0; i < obstacles.length; i++) {
lcd.setCursor(1, obstacles[i]);
lcd.write('X');
obstacles[i] -= 1;
}
if (jump === 0 && obstacles[0] === -1) {
lcd.setColor(255, 0, 0);
motor.write(1);
pause = 10;
}
while (obstacles[0] === -1) {
obstacles.shift();
score = (score + 1) % 24;
}
circle.setLevel(score);
}, 100);
}
startIdle();
process.on('SIGINT', function() {
if (interval) {
clearInterval(interval);
}
circle.setLevel(0);
circle = null;
GCL.cleanUp();
lcd.setColor(255, 255, 255);
motor.write(0);
process.exit(0);
});