Seven segment — 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.
The little red three-digit display, doing everything it can do. Counting, counting down, decimals, the nine letters it can draw, and finally switching the seven bars on and off one at a time.
This is the version for a house on firmware 3.0, where the display is
home.number_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: 02-seven-segment.firmware-3.mobot. Open it in MoBot.
What you need
Section titled “What you need”- A MoBot Smart Home board with its 3-digit TM1637 display fitted (CLK → GPIO 40, DIO → GPIO 39).
- A USB cable and the serial monitor.
What you should see
Section titled “What you should see”| part | on the display |
|---|---|
| 1 | 0, 7, 42, 100, 999 — each pushed to the right, then a countdown from 20 to 0 |
| 2 | 3.14, 25.5, 7.25, 0.5, -1.5, 123 and then --- for a number too big to fit |
| 3 | HEL, CAF, bAd, ---, PLA, then abc drawn as AbC |
| 3 | then Hi! is refused: the display does not change, and the monitor prints why |
| 4 | one bar at a time on all three digits: top, top-right, bottom-right, bottom, bottom-left, top-left, middle, then the dot |
| 4 | then a ring spinning round three times |
| 5 | three shapes built by naming the bars |
| end | 8.8.8. — every bar and every dot lit. That is the test: if any bar stays dark, that bar is broken |
Decimals: you place the dot
Section titled “Decimals: you place the dot”The display has no decimal numbers of its own. You give it a whole number and say which digit’s dot to light:
home.number_display.show_number(314, [True, False, False]) # 3.14home.number_display.show_number(255, [False, True, False]) # 25.5show_number() never puts a 0 in front, though, so 0.5 would come out as
.5. That is why show_decimal() in this file draws every bar itself with
set_digit() (the bars are explained below). It tries two decimal places, then
one, then none, and shows the first version that fits in three digits — a minus
sign takes a digit too, and zeros at the end are dropped, so 0.50 shows as
0.5. A number that cannot fit any way round shows --- rather than lying to
you. That is why 123.456 shows 123.
The nine letters
Section titled “The nine letters”A b C d E F H L P — and that is all of them, plus 0 to 9, a dash
- and a space. show_text() takes up to three of them, pushed to the right
like a number. The Display Text block does exactly the same.
Capital or small does not matter: each letter comes out the one way seven bars
can draw it. b and d always look small, because a capital B looks exactly
like an 8 and a capital D like a 0; the others always look capital. So
show_text("abc") shows AbC.
Anything else — i, O, a dot, more than three characters — is refused
with a ValueError that says what works, and the display keeps what it was
showing:
The number display can't draw 'i'. It can show 0 to 9, the letters A b C d E F H L P (capital or small), a dash - and a space.The bars
Section titled “The bars”Each digit has seven bars and a dot, and each has a name:
AAA F B GGG E C DDD DPset_digit(index, bars) lights exactly the bars you name on one digit (0,
1 or 2, from the left) and switches the others off. ["B", "C"] is a 1.
Work out 8 yourself, then check it: it is every bar from A to G.
The three blocks
Section titled “The three blocks”There are three number-display blocks:
| block | what it does |
|---|---|
Display Number (iot_7segvar) |
shows a whole number: it is show_number(...) |
Display Text (iot_7segtext) |
shows up to three characters: it is show_text(...), firmware 3.0 only |
Set digit segments (iot_7seg) |
lights the bars you name on one digit: it is set_digit(...) |
Parts 4 and 5 of this file do the same thing from Python.
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 — Seven segment (Python) (iot)## A reading copy of one file inside 02-seven-segment.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 02-seven-segment.main.py.
"""Seven segment - MoBot Smart Home.
The little red three-digit display, doing everything it can do.
There are three number-display blocks. "Display Number" works fine but onlytakes whole numbers. "Display Text" shows up to three letters - it is the sameshow_text() as part 3. The third one - the one that lets you switch individualbars on and off - DOES NOTHING when you open a saved project: it draws itselfon the screen and then produces a comment instead of code.
Everything below that is not a block is Python only:
number_display.show_number(314, [True, False, False]) 3.14 - you place the dot number_display.set_digit(0, ["A", "B", "G"]) switch the bars yourself"""
import time
number_display = home.number_display
if number_display is None: print("The number display did not start.") raise SystemExit
# ------------------------------------------------------------------ 1 -------# Numbers. show_number() pushes them to the right and pads with spaces.
print("Part 1: counting.")for value in (0, 7, 42, 100, 999): print(" ", value) number_display.show_number(value) time.sleep(0.8)
print(" counting down from 20")for value in range(20, -1, -1): number_display.show_number(value) time.sleep(0.12)
# ------------------------------------------------------------------ 2 -------# Decimals. The display has no decimal numbers of its own: you give it a whole# number and say which digit's dot to light. 314 with the first dot lit is 3.14:## number_display.show_number(314, [True, False, False])## But show_number() never puts a 0 in front, so 0.5 would come out as " .5".# That is why show_decimal() below draws every bar itself with set_digit() -# the same bars part 4 switches on one at a time.## show_decimal() tries 2 decimal places, then 1, then none, and uses the first# that fits in three digits. A minus sign takes a digit too, and zeros at the# end are dropped, so 0.50 shows as 0.5. If nothing fits it shows ---.
# The bars that draw each character.BARS = { "0": "ABCDEF", "1": "BC", "2": "ABDEG", "3": "ABCDG", "4": "BCFG", "5": "ACDFG", "6": "ACDEFG", "7": "ABC", "8": "ABCDEFG", "9": "ABCDFG", "-": "G", " ": "",}
def show_decimal(value): for places in (2, 1, 0): whole = round(abs(value) * 10 ** places) # 3.14159 -> 314 while places > 0 and whole % 10 == 0: # 0.50 -> 0.5, 20.0 -> 20 whole = whole // 10 places = places - 1 text = str(whole) while len(text) <= places: # 0.05 -> "005": 0 . 0 5 text = "0" + text if value < 0 and whole != 0: text = "-" + text if len(text) <= 3: text = " " * (3 - len(text)) + text # pushed to the right for digit in range(3): bars = list(BARS[text[digit]]) if places > 0 and digit == 2 - places: bars.append("DP") # the dot sits after this digit number_display.set_digit(digit, bars) return number_display.show_text("---")
print("Part 2: decimals.")for value in (3.14159, 25.5, 7.25, 0.5, -1.5, 123.456, 99999.0): show_decimal(value) print(" ", value) time.sleep(1.0)
# ------------------------------------------------------------------ 3 -------# Letters. Only nine letters can be drawn with seven bars:# A b C d E F H L P# Capital or small, each comes out the one way the display can draw it.
print("Part 3: words, sort of.")for word in ("HEL", "CAF", "bAd", "---", "PLA", "abc"): number_display.show_text(word) print(" ", word) time.sleep(1.0)
# Any other letter is refused with a message that says what works - and the# display keeps what it was showing.print(" 'Hi!' - watch the display NOT change")try: number_display.show_text("Hi!")except ValueError as error: print(" refused:", error)time.sleep(1.5)
# ------------------------------------------------------------------ 4 -------# The bars themselves. Each digit has seven bars and a dot, named like this:## AAA# F B# GGG# E C# DDD DP
print("Part 4: switching the bars on one at a time.")for bar in ("A", "B", "C", "D", "E", "F", "G", "DP"): for digit in range(3): number_display.set_digit(digit, [bar]) print(" bar", bar) time.sleep(0.5)
print(" a spinning ring")RING = ("A", "B", "C", "D", "E", "F")for turn in range(3): for bar in RING: for digit in range(3): number_display.set_digit(digit, [bar]) time.sleep(0.08)
# Your own shapes, one digit at a time.print("Part 5: three shapes built by naming the bars.")number_display.show_text("") # "" blanks the displaynumber_display.set_digit(0, ["A", "B", "G"])number_display.set_digit(1, ["D", "E", "G"])number_display.set_digit(2, ["A", "F", "G", "DP"])time.sleep(3)
# The classic display test: every bar and every dot, all at once.number_display.show_number(888, [True, True, True])print("Finished - every bar and every dot should now be lit.")Try changing it
Section titled “Try changing it”- Show the temperature:
show_decimal(home.temperature_sensor.temperature())(it can beNonefor the first second or two after switching on). - Make a stopwatch that counts up in tenths.
- Design your own letter out of bars and show it with
set_digit(...).
The little red three-digit display, doing everything it can do. Counting, counting down, decimals, the nine letters it can draw, and finally switching the seven bars on and off one at a time.
Project for firmware 2.x: 02-seven-segment.mobot. Open it in MoBot.
What you need
Section titled “What you need”- A MoBot Smart Home board with its 3-digit TM1637 display fitted (CLK → GPIO 40, DIO → GPIO 39).
- A USB cable and the serial monitor.
What you should see
Section titled “What you should see”| part | on the display |
|---|---|
| 1 | 0, 7, 42, 100, 999 — each pushed to the right, then a countdown from 20 to 0 |
| 2 | 3.14, 25.5, 0.5, 123 and then --- for a number too big to fit |
| 3 | HEL, CAF, bAd, A.b.C (with two dots lit), 1.23, ---, PLA |
| 3 | then abc in lower case — only the middle b lights; a and c come out blank. It is not case-blind |
| 4 | one bar at a time: top, top-right, bottom-right, bottom, bottom-left, top-left, middle, then the dot |
| 4 | then a ring spinning round three times |
| 5 | three shapes built by naming the bars |
| end | 8.8.8. — every bar and every dot lit. That is the test: if any bar stays dark, that bar is broken |
Why 123.456 shows 123
Section titled “Why 123.456 shows 123”The display only has three digits, so float() tries hardest first: two decimal
places, then one, then none, and shows the first version that fits. A number
that cannot fit any way round shows --- rather than lying to you.
What the bits mean
Section titled “What the bits mean”Each digit is one byte, and each bit is one bar:
aaa bit 0 = a (top) bit 4 = e (bottom left) f b bit 1 = b (top right) bit 5 = f (top left) ggg bit 2 = c (bottom right) bit 6 = g (middle) e c bit 3 = d (bottom) bit 7 = the dot ddd .So 0x3F is 0b00111111 — a, b, c, d, e and f but not the middle — which is a
nought. Work out 8 yourself, then check it: it is 0x7F.
The nine letters
Section titled “The nine letters”A b C d E F H L P — and that is all of them. b and d are
lower-case shapes because an upper-case B looks exactly like an 8 and an
upper-case D looks exactly like a 0. Everything else comes out blank.
The two blocks
Section titled “The two blocks”There are two 7-segment blocks:
| block | what it does |
|---|---|
Display Number (iot_7segvar) |
shows a whole number: it is seg.number(...) |
Set digit segments (iot_7seg) |
lights the bars you name on one digit: it is seg.set_segments_for_digit(...) |
Parts 4 and 5 of this file do the same thing from Python — and they can do more than the block offers.
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 — Seven segment (Python) (iot)## A reading copy of one file inside 02-seven-segment.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.
"""Seven segment - MoBot Smart Home.
The little red three-digit display, doing everything it can do.
There are two 7-segment blocks. One of them - the one that lets you switchindividual bars on and off - DOES NOTHING when you open a saved project: itdraws itself on the screen and then produces a comment instead of code. Theother one, "Display number", works fine but only takes whole numbers.
Everything below is Python only:
seg.show("A.b") letters, and dots seg.float(3.14) decimals, fitted into three digits seg.raw([0x39, 0, 0]) switch the seven bars on and off yourself seg.set_segments_for_digit(0, ['A', 'B', 'G']) the same, by name"""
import time
seg = mobotIOT.seg
if seg is None: print("The 7-segment display did not start.") raise SystemExit
# ------------------------------------------------------------------ 1 -------# Numbers. number() pushes them to the right and pads with spaces.
print("Part 1: counting.")for value in (0, 7, 42, 100, 999): print(" ", value) seg.number(value) time.sleep(0.8)
print(" counting down from 20")for value in range(20, -1, -1): seg.number(value) time.sleep(0.12)
# ------------------------------------------------------------------ 2 -------# Decimals. float() tries 2 decimal places, then 1, then none, and uses the# first that fits in three digits. If nothing fits it shows ---.
print("Part 2: decimals.")for value in (3.14159, 25.5, 0.5, 123.456, 99999.0): seg.float(value) print(" ", value) time.sleep(1.0)
# ------------------------------------------------------------------ 3 -------# Letters. Only nine letters can be drawn with seven bars:# A b C d E F H L P# and a '.' lights the dot of the letter BEFORE it.
print("Part 3: words, sort of.")for word in ("HEL", "CAF", "bAd", "A.b.C", "1.23", "---", "PLA"): seg.show(word) print(" ", word) time.sleep(1.0)
# Careful: the display is not case-insensitive. Lower-case letters that are# not in the list simply come out blank.print(" 'abc' (lower case) - watch the a and the c go blank: only b shows")seg.show("abc")time.sleep(1.5)
# ------------------------------------------------------------------ 4 -------# Raw segments. Each digit is one byte, one bit per bar:## aaa bit 0 = a (top) bit 4 = e (bottom left)# f b bit 1 = b (top right) bit 5 = f (top left)# ggg bit 2 = c (bottom right) bit 6 = g (middle)# e c bit 3 = d (bottom) bit 7 = the dot# ddd .## So 0b00111111 = 0x3F lights a, b, c, d, e, f and draws a nought.
print("Part 4: switching the bars on one at a time.")for bit in range(8): seg.raw([1 << bit, 1 << bit, 1 << bit]) print(" bit", bit, "=", hex(1 << bit)) time.sleep(0.5)
print(" a spinning ring")RING = (0x01, 0x02, 0x04, 0x08, 0x10, 0x20) # a, b, c, d, e, ffor turn in range(3): for bar in RING: seg.raw([bar, bar, bar]) time.sleep(0.08)
# The same idea, but naming the bars instead of counting bits.print("Part 5: the same, by name.")seg.raw([0, 0, 0])seg.set_segments_for_digit(0, ["A", "B", "G"])seg.set_segments_for_digit(1, ["D", "E", "G"])seg.set_segments_for_digit(2, ["A", "F", "G", "DP"])time.sleep(3)
# The classic display test: every bar and every dot, all at once.seg.show("8.8.8.")print("Finished - every bar and every dot should now be lit.")Try changing it
Section titled “Try changing it”- Show the temperature:
seg.float(mobotIOT.dht11.read(0)). - Make a stopwatch that counts up in tenths.
- Design your own letter out of bars and show it with
seg.raw([...]).