Control a BBC micro:bit from Node.js using BLE and Haxe
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.vscode
img
src
.gitignore
README.md
README_HAXE.MD
TODO.MD
build.hxml
build_all.hxml
build_debug.hxml
build_release.hxml
icon.jpg
icon.png
package.json

README.md

hx-node-bbc-microbit

Control a BBC micro:bit from Node.js using BLE and Haxe

These are the Haxe node.js externs from https://github.com/sandeepmistry/node-bbc-microbit and follows the api

The Micro Bit (also referred to as BBC Micro Bit, stylised as micro:bit) is an ARM-based embedded system designed by the BBC for use in computer education in the UK.

The board is 4 cm × 5 cm and has an ARM Cortex-M0 processor, accelerometer and magnetometer sensors, Bluetooth and USB connectivity, a display consisting of 25 LEDs, two programmable buttons, and can be powered by either USB or an external battery pack.[2] The device inputs and outputs are through five ring connectors that are part of the 23-pin edge connector.

wikipedia

Haxe?

Read more about Haxe

Installation Haxe/NPM

1. follow instructions to install bbc-microbit

Make sure to follow the instructions for noble too.

2. install dependencies with npm

npm install

3. install haxelib

haxelib git bbc-microbit https://github.com/MatthijsKamstra/hx-node-bbc-microbit

Haxelib

use this repo locally

haxelib dev bbc-microbit path/to/folder/src

or this git repo

haxelib git bbc-microbit https://github.com/MatthijsKamstra/hx-node-bbc-microbit

don't forget to add it to your build file

-lib bbc-microbit

Visual Studio Code

I would suggest to us Visual Studio Code, it's a very nice editor and lets you set breakpoints in your js file

But other code-editors may work as well!

Haxe niceties

create a (LED)pattern from an array

var X = [
	1, 0, 0, 0, 1,
	0, 1, 0, 1, 0,
	0, 0, 1, 0, 0,
	0, 1, 0, 1, 0,
	1, 0, 0, 0, 1,
];
var buffer = new Buffer(new Pattern().createLedMatrixBuffer(X));

// follow example "LedPatternHx"
// or use a default Pattern
var buffer = new Buffer(new Pattern().createLedMatrixBuffer(Pattern.HEART));

Use string to listen to or EventName

import BBCMicrobit;

// strings will work
microbit.on('buttonAChange', function(value) {
	console.log('\ton -> button A change');
});

// prevent spelling mistakes with EventName
microbit.on(EventName.BUTTON_A_CHANGE, function(value) {
	console.log('\ton -> button A change');
});

Use the fixed values for AccelerometerPeriod Period

import BBCMicrobit;

// numbers will work
microbit.writeAccelerometerPeriod(160, function(err) {
	console.log('\taccelerometer period set');
});

// prevent using wrong value use Period
microbit.writeAccelerometerPeriod(Period.NR_160, function(err) {
	console.log('\taccelerometer period set');
});

Difference

  • UART doesn't work: check api
  • Light sensor doesn't work: it works with a trick read more but with this node.js code unable to access.
  • The code is not written on the micro:bit, remove power (switch off) and the code is gone
  • EventListeners examples doesn't work: the code is not uploaded to NPM yet (but if you want to use it read this)

Fix EventListener

After installing the dependencies via npm install.
You need to update the node_modules/bbc-microbit folder with two files:

And that will make that work.

I have created an issue "microbit.subscribeEvents" but it's not fixed yet.

Examples

most examples from https://github.com/sandeepmistry/node-bbc-microbit are "redone" in Haxe

Blink

Source