Friday, March 25, 2016

Making the BBC micro:bit display the BBC Micro owl logo

Back in the 1980s I had a BBC Micro Model B. I still have that machine and repaired its power supply not long ago. The logo of the BBC Micro was a dot matrix owl:

Well, I was lucky enough to be lent (for one evening!) the most recent incarnation of the BBC's attempt to teach Britain's children to code: the BBC micro:bit. It's a lovely little machine and I hope many children have fun programming it. Unfortunately, I suffer from the Demon Machine problem: show me a computer and I have to make it do something.

Programming the device is really simple. There are multiple options and an online editor for each language. When you are ready to program the micro:bit it's trivial because the editor downloads a .hex file that you drag and drop onto the device (it appears mounted as a volume).

I chose to use MicroPython and write a small program that displays the (old) BBC Micro logo (the owl above) on the micro:bit's 5x5 LEDs. Since the owl is much larger than 5x5 pixels I've used the accelerometer to allow the user to scroll around the owl by tilting the micro:bit.

The code is here on Github. But to get a sense of how easy it is, here's the main loop that does all the work.

while True:
    lastx = ox
    lasty = oy
    xa = accelerometer.get_x()
    if abs(xa) > sensitivity:
        if xa > 0 and ox > 0:
            ox -= 1
        elif xa < 0 and ox < w - 5:
            ox += 1

    ya = accelerometer.get_y()
    if abs(ya) > sensitivity:
        if ya > 0 and oy > 0:
            oy -= 1
        elif ya < 0 and oy < h - 5:
            oy += 1

    if lastx != ox or lasty != oy:

The paint() function draws a 5x5 section of the owl (which is stored in an array) starting from position (ox, oy).  The height and width of the owl are stored in h and w.

def paint():
    for x in range(0, 5):
        for y in range(0, 5):
            display.set_pixel(x, y, 9 * owl[y+oy][x+ox])

And here's what that looks like on the micro:bit itself:

Hmm. I wonder if the micro:bit could be used as a game controller for Elite?

No comments: