OLED picture — Smart Home (Python)
This example is written twice, once for each firmware. Pick the tab that matches the firmware of your Smart Home: the tab you pick is remembered on the other examples.
Firmware 3.0 renamed the Python names a program writes, and kept none of the old ones: a Smart Home on 2.x does not know the 3.0 names, and a Smart Home on 3.0 does not know the 2.x ones. The name map lists every change. If you open the example from inside MoBot, it picks the version for the firmware of your Smart Home. If you download a file from this page, use the one in the tab for your firmware.
Drawing on the screen one pixel at a time. There are blocks for text, lines, boxes and circles. This is the thing underneath all of them: handing the screen a picture of every single pixel at once — 8192 of them, packed into 1024 numbers.
Once you can do this you can draw anything: a graph, a game, a shape nobody wrote a block for.
This is the version for a house on firmware 3.0, where the screen is
home.display. For a house that still runs firmware 2.x, MoBot opens the 2.x
version of this example instead.
Project for firmware 3.0: 03-oled-picture.firmware-3.mobot. Open it in MoBot.
What you need
Section titled “What you need”- A MoBot Smart Home board with its OLED screen fitted (SCL → GPIO 47, SDA → GPIO 48).
- A USB cable and the serial monitor.
What you should see
Section titled “What you should see”| part | on the screen |
|---|---|
| 1 | a checkerboard of 8 × 8 squares |
| 2 | stripes — two lit columns, two dark, and the pattern shifts every four rows |
| 3 | a wave running twice across the screen |
| 4 | a ball bouncing around inside a box for twelve seconds. The colour light flashes red when it hits a side wall and blue when it hits the top or the bottom |
| 5 | the last frame inverted — everything that was lit goes dark and everything dark lights up |
| end | the happy face is back |
If part 4 looks jerky, that is honest: every frame is a whole 1024-byte transfer down the I²C wires, so the screen manages about twenty pictures a second.
How a picture is stored
Section titled “How a picture is stored”The screen is 128 pixels across and 64 down.Eight pixels side by side share ONE number (one byte).128 / 8 = 16 numbers make one row across.16 x 64 rows = 1024 numbers altogether.
In each number the BIGGEST bit is the LEFTMOST pixel:
bit: 7 6 5 4 3 2 1 0 pixel: x x+1 x+2 x+3 x+4 x+5 x+6 x+7
A 1 means the pixel is lit.So the pixel at (x, y) lives in number y * 16 + x // 8, and its bit is
128 >> (x % 8). That is the whole trick:
def plot(buf, x, y): buf[y * 16 + (x >> 3)] |= 128 >> (x & 7)>> 3 is a quick way to write // 8, and & 7 is a quick way to write % 8.
When the picture is ready, one line sends it:
home.display.show_frame(frame)The program
Section titled “The program”This is a reading copy of the program in the project: editing it changes nothing, so open the project and edit it there.
# main.py — OLED picture (Python) (iot)## A reading copy of one file inside 03-oled-picture.firmware-3.mobot.# Editing this copy changes nothing: open the project and edit it there.# This is the entry point — the board runs it as main.py.# Written for firmware 3.0; the 2.x version is 03-oled-picture.main.py.
"""OLED picture - MoBot Smart Home: drawing one pixel at a time.
The screen has blocks for text, lines, boxes and circles. This is the thingunderneath all of them: handing the screen a picture of every single pixel atonce, 8192 of them, as 1024 numbers.
How the picture is stored:
The screen is 128 pixels across and 64 down. Eight pixels side by side share ONE number (one byte). 128 / 8 = 16 numbers for one row across. 16 x 64 rows = 1024 numbers altogether.
In each number the BIGGEST bit is the LEFTMOST pixel:
bit: 7 6 5 4 3 2 1 0 pixel: x x+1 ... x+7
A 1 means the pixel is lit.
So the pixel at (x, y) lives in number y * 16 + x // 8 ,and its bit is 128 >> (x % 8) .
That one line of arithmetic is the whole trick, and once you have it you candraw anything: shapes nobody wrote a block for, graphs, animations, games."""
import time
WIDTH = 128HEIGHT = 64ROW = WIDTH // 8 # 16 numbers per rowBLANK = bytes(ROW * HEIGHT)
display = home.displayframe = bytearray(BLANK)
def set_colour(red, green, blue): """Light the RGB lamp: red, green and blue, each 0-255, in that order.""" home.rgb_led.set_color(red, green, blue)
def clear(buf): """Wipe the picture. Copying 1024 zeros in one go is much faster than a loop.""" buf[:] = BLANK
def plot(buf, x, y): """Light up one pixel.""" if 0 <= x < WIDTH and 0 <= y < HEIGHT: buf[y * ROW + (x >> 3)] |= 128 >> (x & 7)
def blob(buf, cx, cy, radius): """A filled circle, drawn one pixel at a time.""" for dy in range(-radius, radius + 1): for dx in range(-radius, radius + 1): if dx * dx + dy * dy <= radius * radius: plot(buf, cx + dx, cy + dy)
# ------------------------------------------------------------------ 1 -------# A checkerboard. Every other 8-pixel block, every other row.
print("Part 1: a checkerboard.")clear(frame)for row in range(HEIGHT): for byte in range(ROW): if ((row // 8) + byte) % 2 == 0: frame[row * ROW + byte] = 0xFFdisplay.show_frame(frame)time.sleep(2)
# ------------------------------------------------------------------ 2 -------# Stripes, straight from the bit pattern. 0b10101010 is 0xAA.
print("Part 2: stripes.")clear(frame)for row in range(HEIGHT): pattern = 0xAA if (row % 4) < 2 else 0x55 for byte in range(ROW): frame[row * ROW + byte] = patterndisplay.show_frame(frame)time.sleep(2)
# ------------------------------------------------------------------ 3 -------# A graph: one dot per column, following a wave. No block can draw this.
print("Part 3: a wave.")clear(frame)try: from math import sin, pi for x in range(WIDTH): y = int(32 + 26 * sin(x * 4 * pi / WIDTH)) plot(frame, x, y) plot(frame, x, y + 1)except ImportError: for x in range(WIDTH): plot(frame, x, 32)display.show_frame(frame)time.sleep(2)
# ------------------------------------------------------------------ 4 -------# An animation: a ball bouncing around inside a box.
print("Part 4: a bouncing ball. Twelve seconds.")ball_x = 20ball_y = 20step_x = 3step_y = 2RADIUS = 5
for _ in range(300): clear(frame)
# The walls. for x in range(WIDTH): plot(frame, x, 0) plot(frame, x, HEIGHT - 1) for y in range(HEIGHT): plot(frame, 0, y) plot(frame, WIDTH - 1, y)
blob(frame, ball_x, ball_y, RADIUS) display.show_frame(frame)
ball_x = ball_x + step_x ball_y = ball_y + step_y if ball_x <= RADIUS + 1 or ball_x >= WIDTH - RADIUS - 2: step_x = -step_x set_colour(255, 0, 0) if ball_y <= RADIUS + 1 or ball_y >= HEIGHT - RADIUS - 2: step_y = -step_y set_colour(0, 0, 255)
# ------------------------------------------------------------------ 5 -------# Turn every pixel round: white becomes black and black becomes white.
print("Part 5: inverted.")set_colour(0, 0, 0)for i in range(len(frame)): frame[i] = frame[i] ^ 0xFFdisplay.show_frame(frame)time.sleep(2)
display.show_emoji("happy")print("Finished.")Try changing it
Section titled “Try changing it”- Change
0xAAin part 2 to0xF0— four lit, four dark — and see the stripes get fatter. - Make the ball bigger by changing
RADIUS, or add a second ball. - Draw a graph of the light sensor (
home.light_sensor.level()): one column per reading, scrolling left. - Write your name in giant pixels using
plot()in a loop.
Drawing on the screen one pixel at a time. There are blocks for text, lines, boxes and circles. This is the thing underneath all of them: handing the screen a picture of every single pixel at once — 8192 of them, packed into 1024 numbers.
Once you can do this you can draw anything: a graph, a game, a shape nobody wrote a block for.
Project for firmware 2.x: 03-oled-picture.mobot. Open it in MoBot.
What you need
Section titled “What you need”- A MoBot Smart Home board with its OLED screen fitted (SCL → GPIO 47, SDA → GPIO 48).
- A USB cable and the serial monitor.
What you should see
Section titled “What you should see”| part | on the screen |
|---|---|
| 1 | a checkerboard of 8 × 8 squares |
| 2 | stripes — two lit columns, two dark, and the pattern shifts every four rows |
| 3 | a wave running twice across the screen |
| 4 | a ball bouncing around inside a box for twelve seconds. The colour light flashes red when it hits a side wall and blue when it hits the top or the bottom |
| 5 | the last frame inverted — everything that was lit goes dark and everything dark lights up |
| end | the happy face is back |
If part 4 looks jerky, that is honest: every frame is a whole 1024-byte transfer down the I²C wires, so the screen manages about twenty pictures a second.
How a picture is stored
Section titled “How a picture is stored”The screen is 128 pixels across and 64 down.Eight pixels side by side share ONE number (one byte).128 / 8 = 16 numbers make one row across.16 x 64 rows = 1024 numbers altogether.
In each number the BIGGEST bit is the LEFTMOST pixel:
bit: 7 6 5 4 3 2 1 0 pixel: x x+1 x+2 x+3 x+4 x+5 x+6 x+7
A 1 means the pixel is lit.So the pixel at (x, y) lives in number y * 16 + x // 8, and its bit is
128 >> (x % 8). That is the whole trick:
def plot(buf, x, y): buf[y * 16 + (x >> 3)] |= 128 >> (x & 7)>> 3 is a quick way to write // 8, and & 7 is a quick way to write % 8.
The program
Section titled “The program”This is a reading copy of the program in the project: editing it changes nothing, so open the project and edit it there.
# main.py — OLED picture (Python) (iot)## A reading copy of one file inside 03-oled-picture.mobot.# Editing this copy changes nothing: open the project and edit it there.# This is the entry point — the board runs it as main.py.
"""OLED picture - MoBot Smart Home: drawing one pixel at a time.
The screen has blocks for text, lines, boxes and circles. This is the thingunderneath all of them: handing the screen a picture of every single pixel atonce, 8192 of them, as 1024 numbers.
How the picture is stored:
The screen is 128 pixels across and 64 down. Eight pixels side by side share ONE number (one byte). 128 / 8 = 16 numbers for one row across. 16 x 64 rows = 1024 numbers altogether.
In each number the BIGGEST bit is the LEFTMOST pixel:
bit: 7 6 5 4 3 2 1 0 pixel: x x+1 ... x+7
A 1 means the pixel is lit.
So the pixel at (x, y) lives in number y * 16 + x // 8 ,and its bit is 128 >> (x % 8) .
That one line of arithmetic is the whole trick, and once you have it you candraw anything: shapes nobody wrote a block for, graphs, animations, games."""
import time
WIDTH = 128HEIGHT = 64ROW = WIDTH // 8 # 16 numbers per rowBLANK = bytes(ROW * HEIGHT)
oled = mobotIOT.oledframe = bytearray(BLANK)
def set_colour(red, green, blue): """Light the RGB lamp. Each value is 0-100.
Firmware 2.0 names each lamp pin by the colour it really drives, so write() takes red, green, blue in that order and set_colour(100, 0, 0) is red. (Boards on older firmware had red and green the other way round and needed the two swapped here; if yours shows green for red, flash 2.0.) """ mobotIOT.rgb_led.write(red, green, blue)
def clear(buf): """Wipe the picture. Copying 1024 zeros in one go is much faster than a loop.""" buf[:] = BLANK
def plot(buf, x, y): """Light up one pixel.""" if 0 <= x < WIDTH and 0 <= y < HEIGHT: buf[y * ROW + (x >> 3)] |= 128 >> (x & 7)
def blob(buf, cx, cy, radius): """A filled circle, drawn one pixel at a time.""" for dy in range(-radius, radius + 1): for dx in range(-radius, radius + 1): if dx * dx + dy * dy <= radius * radius: plot(buf, cx + dx, cy + dy)
# ------------------------------------------------------------------ 1 -------# A checkerboard. Every other 8-pixel block, every other row.
print("Part 1: a checkerboard.")clear(frame)for row in range(HEIGHT): for byte in range(ROW): if ((row // 8) + byte) % 2 == 0: frame[row * ROW + byte] = 0xFFoled.display_frame(frame)time.sleep(2)
# ------------------------------------------------------------------ 2 -------# Stripes, straight from the bit pattern. 0b10101010 is 0xAA.
print("Part 2: stripes.")clear(frame)for row in range(HEIGHT): pattern = 0xAA if (row % 4) < 2 else 0x55 for byte in range(ROW): frame[row * ROW + byte] = patternoled.display_frame(frame)time.sleep(2)
# ------------------------------------------------------------------ 3 -------# A graph: one dot per column, following a wave. No block can draw this.
print("Part 3: a wave.")clear(frame)try: from math import sin, pi for x in range(WIDTH): y = int(32 + 26 * sin(x * 4 * pi / WIDTH)) plot(frame, x, y) plot(frame, x, y + 1)except ImportError: for x in range(WIDTH): plot(frame, x, 32)oled.display_frame(frame)time.sleep(2)
# ------------------------------------------------------------------ 4 -------# An animation: a ball bouncing around inside a box.
print("Part 4: a bouncing ball. Twelve seconds.")ball_x = 20ball_y = 20step_x = 3step_y = 2RADIUS = 5
for _ in range(300): clear(frame)
# The walls. for x in range(WIDTH): plot(frame, x, 0) plot(frame, x, HEIGHT - 1) for y in range(HEIGHT): plot(frame, 0, y) plot(frame, WIDTH - 1, y)
blob(frame, ball_x, ball_y, RADIUS) oled.display_frame(frame)
ball_x = ball_x + step_x ball_y = ball_y + step_y if ball_x <= RADIUS + 1 or ball_x >= WIDTH - RADIUS - 2: step_x = -step_x set_colour(100, 0, 0) if ball_y <= RADIUS + 1 or ball_y >= HEIGHT - RADIUS - 2: step_y = -step_y set_colour(0, 0, 100)
# ------------------------------------------------------------------ 5 -------# Turn every pixel round: white becomes black and black becomes white.
print("Part 5: inverted.")set_colour(0, 0, 0)for i in range(len(frame)): frame[i] = frame[i] ^ 0xFFoled.display_frame(frame)time.sleep(2)
oled.clear_screen()oled.show_emoji("happy")print("Finished.")Try changing it
Section titled “Try changing it”- Change
0xAAin part 2 to0xF0— four lit, four dark — and see the stripes get fatter. - Make the ball bigger by changing
RADIUS, or add a second ball. - Draw a graph of the light sensor: one column per reading, scrolling left.
- Write your name in giant pixels using
plot()in a loop.