Display¶
How it works¶
There are 25 LEDs set out like the picture below. The LEDs are numbered from (0,0) in the top left hand corner, to (4,4) in the bottom right hand corner. You can use the LEDs like a very tiny screen to display single characters, a string of characters or a small picture like the smiley face here. The LEDs can be set to different brightnesses.
Controlling the single LEDs¶
Each LED can be controlled individually, setting it’s brightness from 0 to 9 (full on).
You can loop on the rows and columns to set all pixels in one go.
from microbit import *
display.clear()
for x in range(0, 5):
for y in range(0, 5):
display.set_pixel(x,y,9)
Clearing the display¶
You can clear the display with the code above, replacing 9 with 0 as an intensity. Alternative there’s a display.clear() function that does just that.
Displaying text¶
You can display a string of text static or scrolling on the LED:
from microbit import *
# static text
display.show("Hello!)
# scrolling text
display.scroll("Hello!")
Displaying images¶
The display can be configured to show images. Each image is just a list of pixel intensities with 5x5=25 cells.
There’s a number of built-in images:
- Image.HEART, Image.HEART_SMALL
- Image.HAPPY, Image.SMILE, Image.SAD, Image.CONFUSED, Image.ANGRY, Image.ASLEEP, Image.SURPRISED, Image.SILLY, Image.FABULOUS,
- Image.MEH, Image.YES, Image.NO
- Image.ARROW_N, Image.ARROW_NE, Image.ARROW_E, Image.ARROW_SE, Image.ARROW_S, Image.ARROW_SW, Image.ARROW_W, Image.ARROW_NW
- Image.MUSIC_CROTCHET, Image.MUSIC_QUAVER, Image.MUSIC_QUAVERS
- Image.XMAS, Image.PACMAN, Image.TARGET, Image.ROLLERSKATE, Image.STICKFIGURE, Image.GHOST, Image.SWORD, Image.UMBRELLA
- Image.RABBIT, Image.COW, Image.DUCK, Image.HOUSE, Image.TORTOISE, Image.BUTTERFLY, Image.GIRAFFE, Image.SNAKE
These can be shown with
from microbit import *
display.show(Image.COW)
For custom images, it is necessary to encode them as following:
from microbit import *
boat = Image("05050:"
"05050:"
"05050:"
"99999:"
"09990")
display.show(boat)
Animations¶
It’s possible to display a group of images in sequence thus creating an animation.
For example it’s possible to draw a spinning arrow:
from microbit import *
display.show(Image.ALL_ARROWS, loop=True, delay=100)
Image.ALL_ARROWS is just a list of all the Image.ARROW_* images, and in the same way you can pass in a list of custom images.