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 Switch = require('jsupm_grove');
var button = new Switch.GroveButton(2);
var Control = require('jsupm_joystick12');
var joyStick = new Control.Joystick12(2);
var LCD = require("jsupm_i2clcd");
var lcd = new LCD.Jhd1313m1(6, 0x3E, 0x62);
var BUZZER = require('jsupm_buzzer');
var buzzer = new BUZZER.Buzzer(2);
var f3js = require('f3js')
, c = f3js.createContainer()
, x = 10
, y = 10
, width = 160
, height = 105
, thickness = 42
, jw = 7 /* かみ合わせ幅 [5, 10] */
, jh = 2 /* 板厚[mm] [1, 5] */;
c.x = x;
c.y = y;
var f3js_contr = require('f3js')
, c_contr1 = f3js_contr.createContainer()
, x_contr1 = 340
, y_contr1 = 10
, c_contr2 = f3js_contr.createContainer()
, x_contr2 = 395
, y_contr2 = 10
, width_contr = 140
, height_contr = 50
, thickness_contr = 23
, jw_contr = 7
, jh_contr = 2;
c_contr1.x = x_contr1;
c_contr1.y = y_contr1;
c_contr2.x = x_contr2;
c_contr2.y = y_contr2;
// 本体側
// 表の面を作る
var p = c.jrc(0, 0, width, height, jw, jh);
/*
// 上記は次のコードと同じ
//var p = c.drawJointRectangle(0, 0, width, height, jw, jh);
// または次のコードと同じ
var p = c.createPath();
p.moveTo(0, 0);
p.jointTo(width, 0, jw, jh);
p.jointTo(width, height, jw, jh);
p.jointTo(0, height, jw, jh);
p.jointTo(0, 0, jw, jh);
p.close();
*/
// 表面にいろいろ配置する
c.add(lcd, { x: 80 + 0, y: 30 + 0 });
c.add(button, { x: 140, y: 40 });
c.add(buzzer, { x: 40, y: 70 });
// 配線用の穴を開ける
c.drawRectangle(136, 33, 8, 2);
c.drawRectangle(138, 38, 10, 8);
c.drawRectangle(135, 89, 10, 10);
c.drawRectangle(15, 89, 10, 10);
c.drawRectangle(89, 62, 31, 16);
c.drawCircle(113, 60, 1);
c.drawCircle(113, 80, 1);
c.drawCircle(83, 70, 1);
c.drawCircle(70, 155.5, 1.5);
c.drawCircle(70, 183, 1.5);
// 側面と裏面を作る
var ps = p.extrude(45);
ps[0].x = width + 5;
ps[0].y = jh;
ps[1].x = width + 5;
ps[1].y = (thickness + 5) * 2 + jh;
ps[2].x = width + 5;
ps[2].y = thickness + 5 + jh;
ps[3].x = width + 5;
ps[3].y = (thickness + 5) * 3 + jh;
ps[4].x = 0; // 最後の要素が裏面
ps[4].y = height + 5; // 表の面の真下に配置する
for (var i = 0; i < ps.length; i ++) {
c.add(ps[i]);
}
// 側面の一つに電源用の穴を開ける
ps[1].drawRectangle(jh + 5, thickness - jh - 2, height - jh*2 - 10, -10);
//extra
f3js.text('SSH', 185, 170);
f3js.text('Production', 185, 180)
// コントローラ側
// 表面
var p_contr1 = c_contr1.jrc(0, 0, height_contr, width_contr, jw_contr, jh_contr);
var p_contr2 = c_contr2.jrc(0, 0, height_contr, width_contr, jw_contr, jh_contr);
// 側面と裏面
var ps_contr1 = p_contr1.extrude(23);
ps_contr1[0].x = -height_contr - 5;
ps_contr1[0].y = (thickness + 5) * 2 + jh_contr;
ps_contr1[1].x = -width_contr - 5;
ps_contr1[1].y = width_contr * 2 - thickness_contr * 2 + 5;
ps_contr1[2].x = -height_contr - 5;
ps_contr1[2].y = (thickness + 5) * 2 + thickness_contr + jh_contr * 2;
ps_contr1[3].x = -width_contr - 5;
ps_contr1[3].y = width_contr * 2 - thickness_contr + jh_contr + 5;
ps_contr1[4].x = 0;
ps_contr1[4].y = width_contr + 5;
for (i = 0; i < ps_contr1.length; i ++) {
c_contr1.add(ps_contr1[i]);
}
var ps_contr2 = p_contr2.extrude(23);
ps_contr2[0].x = (-height_contr - 5) * 2;
ps_contr2[0].y = (thickness + 5) * 2 + thickness_contr * 2 + jh_contr * 3;
ps_contr2[1].x = -width_contr * 2 - height_contr - 5 * 3;
ps_contr2[1].y = width_contr * 2 - thickness_contr * 2 + 5;
ps_contr2[2].x = (-height_contr - 5) * 2;
ps_contr2[2].y = (thickness + 5) * 2 + thickness_contr * 3 + jh_contr * 4;
ps_contr2[3].x = -width_contr * 2 - height_contr - 5 * 3;
ps_contr2[3].y = width_contr * 2 - thickness_contr + jh_contr + 5;
ps_contr2[4].x = 0;
ps_contr2[4].y = width_contr + 5;
for (i = 0; i < ps_contr2.length; i ++) {
c_contr2.add(ps_contr2[i]);
}
//ボタン類
var b1 = c_contr1.add(button, { x: 29, y: width_contr + 40 });
var b2 = c_contr2.add(button, { x: 29, y: width_contr + 40 });
b1.rotate(-90);
b2.rotate(-90);
var st1 = c_contr1.add(joyStick, { x: 25, y: 110 });
var st2 = c_contr2.add(joyStick, { x: 25, y: 110 });
st1.rotate(-90);
st2.rotate(-90);
//ボタン類用の穴
c.drawRectangle(351, 175, 8, 10);
c.drawRectangle(362, 175, 2, 10);
c.drawRectangle(406, 175, 8, 10);
c.drawRectangle(417, 175, 2, 10);
c.drawCircle(355, 255, 12);
c.drawCircle(410, 255, 12);
//配線用の穴
c.drawRectangle(105, 244, 10, 10);
c.drawRectangle(105, 268, 10, 10);
//ボタンキャップ
var cap1 = c.drawCircle(175, 200, 10, 10);
cap1.fill = true;
c.drawRectangle(171, 194, 2, 2);
c.drawRectangle(177, 194, 2, 2);
c.drawRectangle(171, 204, 2, 2);
c.drawRectangle(177, 204, 2, 2);
c.drawCircle(205, 200, 10, 10);
c.drawRectangle(201, 194, 8, 12);
c.drawCircle(235, 200, 10, 10);
c.drawRectangle(230, 194, 10, 12);
var cap2 = c.drawCircle(190, 220, 10, 10);
cap2.fill = true;
c.drawRectangle(186, 214, 2, 2);
c.drawRectangle(192, 214, 2, 2);
c.drawRectangle(186, 224, 2, 2);
c.drawRectangle(192, 224, 2, 2);
c.drawCircle(220, 220, 10, 10);
c.drawRectangle(216, 214, 8, 12);
c.drawCircle(250, 220, 10, 10);
c.drawRectanglE(215, 220, 10, 12);
// 以降はゲームのソースコード
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);
});