In this workshop we’ll figure out how to use Web Bluetooth to interact with other devices - but also how to use Espruino on embedded devices to develop our own Bluetooth-enabled hardware.
The first step is to make sure you can connect to your Badge via Web Bluetooth. We’ll do this with the Espruino IDE - once connected you’ll be able to fully reprogram your badge if you want to.
Web Bluetooth
- if you don’t, check out Espruino’s Guide and if there’s nothing useful there, seek help. We have a few USB dongles that’ll work on older Macs and PCs.Make Connectable
Web Bluetooth
Now you’re connected, you could try customising your Badge’s backlight…
Communications
turn off
Reset before send
and ensure Save on Send
is set to To RAM
(this will
stop you accidentally overwriting your badge’s code).Badge.patterns.green=()=>{
var n=0;
return [()=>{
n+=50;
if (n>1536)n=0;
NC.ledTop([0,Math.max(255-Math.abs(n-1024),0),0]);
NC.ledBottom([0,Math.max(255-Math.abs(n-1384),0),0]);
NC.backlight([0,Math.max(255-Math.abs(n-640),0),0,
0,Math.max(255-Math.abs(n-512),0),0,
0,Math.max(255-Math.abs(n-384),0),0,
0,Math.max(255-Math.abs(n-256),0),0]);
},50];
};
// Run the pattern right now
Badge.pattern("green");
So what’s going on? The badge’s lights are controlled by arrays of
blue, green, red
values, all of which are between 0 and 255.
ledTop
and ledBottom
are just one colour, so take 3 elements.
However the LCD backlight
has 4 separate LEDs, so it has 12 elements,
[B1,G1,R1, B2,G2,R2, B3,G3,R3, B4,G4,R4]
for the 4 LEDs.
E.HSBtoRGB
to get
different hues easily - check out the Badge’s code
for some examples.upload
again:require("Storage").write(".boot0",`
Badge=global.Badge||{};
Badge.patterns=Badge.patterns||{};
// --------------------------------- Your code
Badge.patterns.green=()=>{
var n=0;
return [()=>{
n+=50;
if (n>1536)n=0;
NC.ledTop([0,Math.max(255-Math.abs(n-1024),0),0]);
NC.ledBottom([0,Math.max(255-Math.abs(n-1384),0),0]);
NC.backlight([0,Math.max(255-Math.abs(n-640),0),0,
0,Math.max(255-Math.abs(n-512),0),0,
0,Math.max(255-Math.abs(n-384),0),0,
0,Math.max(255-Math.abs(n-256),0),0]);
},50];
};
Badge.defaultPattern = "green"; // <--------- pattern name here
`);
load()
to have the badge reload everything from
flash memory, along with your pattern!For making badge apps, and other stuff, check out the Nodeconf EU Badge’s documentation and examples.
If you’re interested in playing with Espruino further…
Tutorial
g
. You just need to call g.flip()
to update your changes to the LCD display.If the IDE at http://www.espruino.com/ide worked for you then great, you’re sorted!
If it didn’t and it couldn’t be made to work, but you can get Node.js and Noble working then you can use the Web Bluetooth wrapper for Node to follow along with more or less the same code (you can also use the Espruino command-line tools to communicate with your badge.