Skip to content

Commit b7b41c8

Browse files
authored
Merge pull request #48 from jepler/7seg-counter
Simplify first 7-segment example into a counter
2 parents aa651b1 + c321011 commit b7b41c8

File tree

2 files changed

+19
-36
lines changed

2 files changed

+19
-36
lines changed

examples/pioasm_7seg.py

+19-35
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
77
By updating the buffer being written to the display, the shown digits can be changed.
88
9-
The main program repeatedly shows random digits which 'lock' after a short
10-
time. After all digits have locked, it blanks for a short time and then repeats.
11-
It also demonstrates the use of `asyncio` to perform multiple tasks.
9+
The main program just counts up, looping back to 0000 after 9999.
1210
1311
This example is designed for a Raspberry Pi Pico and bare LED display. For
1412
simplicity, it is wired without any current limiting resistors, instead relying
@@ -39,9 +37,8 @@
3937
* Pico GP16 to LED matrix 14 (COM1)
4038
"""
4139

42-
import asyncio
43-
import random
4440
import array
41+
import time
4542
import board
4643
import rp2pio
4744
import adafruit_pioasm
@@ -141,38 +138,25 @@ def __setitem__(self, i, v):
141138
else:
142139
self._buf[i] = DIGITS_WT[v] & ~COM_WT[i]
143140

144-
145-
async def digit_locker(s, i, wait):
146-
delay = 30
147-
d = random.randint(0, 9)
148-
while delay < 300:
149-
d = (d + random.randint(1, 9)) % 10 # Tick to a new digit other than 'd'
150-
s[i] = d
151-
await asyncio.sleep(delay / 1000)
152-
if wait:
153-
wait -= 1
154-
else:
155-
delay = delay * 1.1
141+
def set_number(self, number):
142+
for j in range(4):
143+
self[3 - j] = number % 10
144+
number //= 10
156145

157146

158-
def shuffle(seq):
159-
for i in range(len(seq) - 1):
160-
j = random.randrange(i + 1, len(seq))
161-
seq[i], seq[j] = seq[j], seq[i]
147+
def count(start=0):
148+
val = start
149+
while True:
150+
yield val
151+
val += 1
162152

163153

164-
async def main():
165-
waits = [100, 175, 225, 250]
154+
def main():
166155
with SMSevenSegment(board.GP9) as s:
167-
while True:
168-
shuffle(waits)
169-
await asyncio.gather(
170-
*(digit_locker(s, i, di) for i, di in enumerate(waits))
171-
)
172-
await asyncio.sleep(1)
173-
for i in range(4):
174-
s[i] = None
175-
await asyncio.sleep(0.5)
176-
177-
178-
asyncio.run(main())
156+
for i in count():
157+
s.set_number(i)
158+
time.sleep(0.05)
159+
160+
161+
if __name__ == "__main__":
162+
main()

examples/pioasm_neopixel_bg.py

-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ def _transmit(self, buf):
140140
NEOPIXEL = board.NEOPIXEL
141141
NUM_PIXELS = 12
142142
pixels = NeoPixelBackground(NEOPIXEL, NUM_PIXELS)
143-
i = 0
144143
while True:
145144
# Around 1 cycle per second
146145
pixels.fill(rainbowio.colorwheel(supervisor.ticks_ms() // 4))

0 commit comments

Comments
 (0)