Draw on the screen — Smart Home
The little OLED screen is wiped clean and then a picture is drawn on it: the word MoBot, the words Smart Home under it, a line straight across, a box on the left and a circle on the right.
Then the program finishes. This one draws its picture once and stops, so it has no repeat forever block.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Home.
The program
Section titled “The program”Read the program as text
- MoBot Start
- Print to serial
"Drawing on the screen" - Clear OLED Screen
- Display Text
"MoBot"at X: 0 Y: 0 . - Display Text
"Smart Home"at X: 0 Y: 10 . - Draw Line from X0: 0 Y0: 24 to X1: 127 Y1: 24 .
- Draw Rectangle at X: 10 Y: 34 Width: 44 Height: 24 .
- Draw Circle at X: 96 Y: 46 Radius: 14 .
- Print to serial
"Look at the screen"
What you need
Section titled “What you need”- A Smart Home board with its OLED screen.
- A USB cable and the serial monitor.
How to run it
Section titled “How to run it”- Open 08-oled-shapes.mobot in MoBot.
- Plug in, pick the port, press Send to Device.
- Look at the screen.
What you should see
Section titled “What you should see”The screen goes blank, then the picture appears. Here it is as a sketch, with every character standing for 2 dots across and 4 dots down:
+----------------------------------------------------------------+|M o B o t || ||S m a r t H o m e || || || ||################################################################|| || ###################### ######## || # # ### ## || # # ## # || # # # ## || # # ## # || # # ## ### || ###################### ######### || # |+----------------------------------------------------------------+In the serial monitor you see [MOBOT] Drawing on the screen first and
[MOBOT] Look at the screen last. The picture is drawn in between.
The Clear OLED Screen block comes first, so whatever the board was showing before is wiped away before the drawing starts.
Where things go on the screen
Section titled “Where things go on the screen”The screen is 128 dots across and 64 dots down. Every dot has two numbers:
- X is how far across it is from the left, from 0 to 127.
- Y is how far down it is from the top, from 0 to 63.
The top-left corner is X 0, Y 0. Y counts downwards, so Y 60 is near the bottom of the screen, not near the top.
Text works the same way. Display Text puts the top-left corner of the first
letter at X and Y. Each letter is 8 dots wide and 8 dots tall, so one line holds
up to 16 letters. MoBot is 5 letters, which is 40 dots across, and Smart Home
is 10 letters, which is 80 dots across.
What each block takes
Section titled “What each block takes”| block | the numbers it takes | in this program |
|---|---|---|
| Draw Line from X0 Y0 to X1 Y1 | the dot where the line starts and the dot where it ends | from 0, 24 to 127, 24: straight across the screen at Y 24 |
| Draw Rectangle at X Y Width Height | the top-left corner, then how many dots wide and how many dots tall | at 10, 34, 44 wide and 24 tall: it covers X 10 to 53 and Y 34 to 57 |
| Draw Circle at X Y Radius | the middle, then the radius: how many dots it reaches out from the middle | middle 96, 46 and radius 14: it covers X 82 to 110 and Y 32 to 60 |
Take the rectangle: it starts at dot 10 and is 44 dots wide, so its right edge is dot 53. It starts at dot 34 down and is 24 dots tall, so its bottom edge is dot 57. The last two numbers of the rectangle are a size, not a far corner. The line is the other way round: its last two numbers really are the dot where it ends.
The rectangle and the circle are drawn as outlines, not filled in.
Everything fits on the screen: the furthest dots the program draws are X 127 (the end of the line) and Y 60 (the bottom of the circle), and the screen goes to X 127 and Y 63. A dot outside those numbers is simply not drawn.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
iot_oledclear |
Wipes the screen. |
iot_oledtext |
Writes words with their top-left corner at X and Y. |
iot_oleddrawline |
A line from one dot to another. |
iot_oleddrawrect |
A box: top-left corner, then width and height. |
iot_oleddrawcircle |
A circle: its middle, then its radius. |
The Python code
Section titled “The Python code”The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.
# Draw on the screen — iot (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 08-oled-shapes.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.# The firmware 2.x version is 08-oled-shapes.main.py.
from machine import Pinimport time
print("[MOBOT]", 'Drawing on the screen')home.display.clear()home.display.show_text('MoBot', 0, 0)home.display.show_text('Smart Home', 0, 10)home.display.draw_line(0, 24, 127, 24)home.display.draw_rectangle(10, 34, 44, 24)home.display.draw_circle(96, 46, 14)print("[MOBOT]", 'Look at the screen')# Draw on the screen — iot## This is what MoBot puts on the board as main.py when you open# 08-oled-shapes.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.
from machine import Pinimport time
print("[MOBOT]", 'Drawing on the screen')mobotIOT.oled.clear_screen()mobotIOT.oled.display_text('MoBot', 0, 0)mobotIOT.oled.display_text('Smart Home', 0, 10)mobotIOT.oled.draw_line(0, 24, 127, 24)mobotIOT.oled.draw_rectangle(10, 34, 44, 24)mobotIOT.oled.draw_circle(96, 46, 14)print("[MOBOT]", 'Look at the screen')Try changing it
Section titled “Try changing it”- Change Radius 14 to 17. The circle now just touches the bottom of the screen. Make it 18 and the bottom of the circle runs off the screen: the dots that do not fit are not drawn.
- Change the rectangle to X 0, Y 0, Width 128, Height 64. It is now a frame around the whole screen.
- Give the rectangle Width 100 and Height 4 and it becomes a long thin bar.
- Write your own name instead of
MoBot. A line holds up to 16 letters. - Move the Clear OLED Screen block to the very end and the whole picture vanishes the moment it is drawn. A good way to see what “clear” means.