require("Font6x8").add(Graphics);
var MAXTIME = 60;
var lights = [];
var currentLight = undefined;
var lastLight = undefined;
var score = 0;
var timer = 0;
var timerInterval;
function addrToId(addr) {
var d = addr.substr(12,5).split(":").map(n=>parseInt(n,16));
return (d[0]<<8)|d[1];
}
function show(txt) {
g.clear(1).setFont6x8().setFontAlign(0,-1).drawString(txt,16,0).flip();
}
function show2(txta, txtb) {
g.clear(1).setFont6x8().setFontAlign(-1,-1).drawString(txta,0,0);
g.setFontAlign(1,-1).drawString(txtb,31,0).flip();
}
function startDeviceScan() {
show("Scan...");
NRF.findDevices(function(devices) {
show(`Got ${devices.length}`);
print(" "+devices.map(d=>d.id+" => "+addrToId(d.id)).join("\n "));
lights = devices.map( d => addrToId(d.id) );
if (lights.length>2)
setTimeout(startGame, 1000);
}, {timeout : 5000, filters : [{ namePrefix:"BTK" }] });
}
function showNewLight() {
var light;
do {
light = lights[Math.floor(Math.random()*lights.length*0.999)];
} while (light==currentLight || light==lastLight);
lastLight = currentLight;
currentLight = light;
console.log("Light ", light);
NRF.setAdvertising({},{
showName:false,
manufacturer:0x0590,
manufacturerData:[0,0,light>>8,light&0xFF],
whenConnected:true,
interval: 100
});
}
function stopLights() {
console.log("Stop Lights");
NRF.setAdvertising({},{
showName:false,
manufacturer:0x0590,
manufacturerData:[0,0,0,0],
whenConnected:true,
interval: 100
});
setTimeout(function() {
NRF.setAdvertising({},{showName:true, interval:375});
}, 5000);
}
setWatch(showNewLight, BTN1, {repeat:true});
function startGame() {
show("GO!");
timer = MAXTIME;
score = 0;
showNewLight();
NRF.setScan(function(d) {
var m = d.manufacturerData;
console.log(addrToId(d.id), m);
if (addrToId(d.id) == currentLight) {
var time = (m[2]<<8) | m[3];
console.log("Light pressed", time+" ms");
score++;
show2(timer+"s", score);
showNewLight();
}
}, { filters: [{ manufacturerData:{0x0590:{}} }] });
if (timerInterval) clearInterval();
timerInterval = setInterval(function() {
if (timer>0) {
timer--;
show2(timer+"s", score);
if (timer==0) stopLights(); // turn lights off
} else {
show("="+score+"=");
setTimeout(function() {
show("");
}, 500);
}
}, 1000);
}
startDeviceScan();
I tried it and it does seem to scan and display something.
Remember to get it to work with the Pucks they have to have been turned on by pressing them, and you need them and Pixl.js to be disconnected from the Web IDE
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Ok, maybe try this code on the Pixl:
I tried it and it does seem to scan and display something.
Remember to get it to work with the Pucks they have to have been turned on by pressing them, and you need them and Pixl.js to be disconnected from the Web IDE