Friday, September 21, 2018

micro:bit + Espruino + Android = The Cutest REPL You'll Ever Interact With

If you have an Android Phone and a micro:bit, then have I got an experiment for you. Grab your micro:bit, phone and a USB Host on the Go adapter.

On your phone, install the Serial USB Terminal app. On the micro:bit install the espurino JavaScript interpreter by coping the relevant hex file to your board. Using the adapter, plug the micro:bit into your phone. If you have the correct type of USB cable, you should see a screen like this:

Launch the Serial USB Terminal. Under settings, make sure that the USB device is detected and 9600 baud is selected:

Now press the reset button on your micro:bit and you should be greeted by the following message in the terminal:

Your micro:bit is now playing the role of a JavaScript REPL! As noted here you can trivially turn on LEDs, detect the state of buttons, interact with the compass and accelerometer and generally hack away like you would on any other JavaScript console. But the code is being executed on your micro:bit! Here's me playing with the following code:

> i = 0;
> tick = function() { };
> setInterval(function() { tick(); }, 200);
> tic = function() { show(i++); }

I'm amazed at how easy to install and functional espruino is, and I'm impressed that the Serial USB Terminal Just Works. Using this setup I can program my micro:bit on the go, no desktop environment or compilation system needed.

There's obvious room for improvement. Serial USB Terminal forces you to enter code in terms of lines which is a limiting environment to code in. In an ideal world, I'd interact with the USB serial port via Termux and emacs, then I'd be in programmer heaven. The micro:bit is also a tiny platform to be working on, though that can be addressed by upgrading to a ESP32 chip. This chip is far more robust (WiFi anyone?) and runs Espruino as well.

I've always wanted a mini-me sized stand alone computer that I can program in the field. And now I do!

No comments:

Post a Comment