Uploading Micro:Bit HEX files on the Mac

The BBC Micro:Bit (or MicroBit) is a great, cheap piece of kit for budding coders. It isn’t Raspberry Pi Zero, but it is geared around helping develop coding skills in visual blocks, JavaScript, or Python that you can see interacting on the device without the need to have lots of additional hardware. It has LEDs, GPIO pins, buttons, a gyroscope, some rudimentary options for measuring temperatures and light levels. It’s just a solid little gadget that can be found in many UK schools today. I’ve been using them as an option for my children for coding and experimenting for some time, and there are some rough edges - particularly around streamlining the process of getting the compiled code onto the device.

The compiled code for Micro:Bit results in the generation of a .HEX file. These files can be transmitted from tablets to the Micro:Bit via Bluetooth. I’ve had numerous challenges in setting up Bluetooth and getting it to stick (you can blank out the option if you start dabbling with Python on the Micro:Bit for example), and the transfer speed is mind numbingly slow. My preference is therefore to transfer via a direct cable connection which is much more reliable and at least an order of magnitude faster in most cases.

The direct cable connection sees the Micro:Bit file system mount on a computer as a storage device. Loading the HEX file is then simply a case of copying the file to the Micro:Bit storage, after which the Micro:Bit automatically restarts and loads the HEX file.

If you are using a Windows laptop, I heartily recommend a little utility from Microsoft that will watch your downloads folder and copy the file to an attached Micro:Bit automatically. In fact I liked this so much I wanted something similar for when we were using a Mac.

The most obvious choice here was simply to create a Hazel rule to file a copy of the HEX file to the Micro:Bit, except that I do find that sometimes we’ll download the HEX file before we’ve plugged the Micro:Bit back in. For that reason I purposefully chose to create something I could trigger when I wanted to, and for that I used Keyboard Maestro.

I’ve temporarily stripped out my hot key, but you can see the gist of the macro in the screenshot at the end.

The macro begins by using a shell script to get the file path of the last created HEX file into the Keyboard Maestro variable strFilePath.

cd /Users/stephen/Downloads
ls -t *.hex | awk '{printf("%s",$0);exit}'

The next macro action copies the HEX file to the Micro:Bit and then moves the original HEX file to the trash. Whilst I could just move the file, by copying and trashing, I always have my trash as a fall back if I need to grab a particular revision. Finally a notification is displayed when the operations are complete.

It is quite a simple little macro, but file copying is boring and when you are trying to keep children engaged any little speed bumps can help a surprising amount.

Author: Stephen Millard
Tags: | microbit | keyboard maestro |

Buy me a coffeeBuy me a coffee

Related posts that you may also like to read