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 Buzzer = require("jsupm_buzzer");
var buzzer = new Buzzer.Buzzer(3);
var grove =require("jsupm_grove");
var button = new grove.GroveButton(2);
var led = new grove.GroveLed(4);
var LCD = require("jsupm_i2clcd");
var lcd = new LCD.Jhd1313m1(6, 0x3E, 0x62);
var upmMicrophone = require("jsupm_mic");
// Attach microphone to analog port A0
var myMic = new upmMicrophone.Microphone(0);
var threshContext = new upmMicrophone.thresholdContext;
threshContext.averageReading = 0;
threshContext.runningAverage = 0;
threshContext.averagedOver = 2;
var is_running = false;
buzzer.setVolume(0.1);
var f3js = require('f3js')
, c = f3js.createContainer()
, x = 10
, y = 10
, width = 100
, height = 105
, thickness = 42
, jw = 7 /* かみ合わせ幅 [5, 10] */
, jh = 2 /* 板厚[mm] [1, 5] */;
c.x = x;
c.y = y;
// 表の面を作る
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(buzzer, { x: 50, y: 80});
c.add(lcd, { x: 50 + 0, y: 30 + 0 });
c.add(led, 20, 80);
c.add(button, { x: 80 + 0, y: 80 + 0 });
// 配線用の穴を開ける
c.drawRectangle(15, 59, 10, 6);
c.drawRectangle(45, 59, 10, 6);
c.drawRectangle(75, 59, 10, 6);
// 側面と裏面を作る
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 - 5, height - jh*2 - 10, -10);
// 以降はゲームのソースコード
var idleInterval = false;
function startIdle() {
idleInterval = setInterval(function() {
lcd.setCursor(0, 0);
lcd.write('PUSH BUTTON!! ');
if (button.value() > 0) {
clearInterval(idleInterval);
idleInterval = false;
startGame();
}
}, 100);
}
var interval = false, score;
function stopGame() {
lcd.setCursor(0,0);
lcd.write('your sore is '+String(score));
var nextinterval = setInterval(function(){
if (interval) {
clearInterval(interval);
interval = false;
}
if (!idleInterval && (button.value() > 0)) {
clearInterval(nextinterval);
startIdle();
}
},100);
}
function startGame() {
for (i = 0; i < 2; i++) {
lcd.setCursor(i, 0);
lcd.write(' ');
}
var t = 0;
var obstacles = [];
score = 0;
interval = setInterval(function() {
if (t === 120) {
lcd.setColor(255, 255, 255);
obstacles = [];
stopGame();
return;
}
var buffer = new upmMicrophone.uint16Array(128);
var len = myMic.getSampledWindow(2, 10, buffer);
if (len)
{
var thresh = myMic.findThreshold(threshContext, 150, buffer, len);
}
led.off();
console.log('time: ' + thresh);
if (t==15 || t==21 || t==27 || t==33|| t==37|| t==39|| t==45|| t==49|| t==51|| t==63|| t==69|| t==75|| t==81|| t==85|| t==87|| t==93|| t==97|| t==99)
obstacles.push(15);
for (i = 0; i < 2; i++) {
lcd.setCursor(i, 0);
lcd.write(' ');
}
lcd.setCursor(0,2);
lcd.write('X');
for (i = 0; i < obstacles.length; i++) {
lcd.setCursor(1, obstacles[i]);
lcd.write('O');
obstacles[i] -= 1;
}
while (obstacles[0] === -1) {
obstacles.shift();
}
t += 1;
if(t==48 || t==60 || t==108) buzzer.playSound(1915,50000); //DO
if(t==30 || t==36 || t==42|| t==54|| t==66|| t==102|| t==114) buzzer.playSound(1519,50000); //MI
if(t==52 || t==64 || t==100 || t==112) buzzer.playSound(1275,50000); //SO
if(t==78 || t==84 || t==90) buzzer.playSound(1014,50000); //SI
if(t==96) buzzer.playSound(956,50000); //HDO
if(t==115) buzzer.stopSound();
if (obstacles[0] === 0 && thresh > 0 ){
score = (score + 1) % 24;
led.on();
}
}, 100);
}
startIdle();
process.on('SIGINT', function() {
if (interval) {
clearInterval(interval);
}
lcd.setColor(255, 255, 255);
process.exit(0);
});