Merge pull request #48 from jepler/7seg-counter

Simplify first 7-segment example into a counter
This commit is contained in:
Scott Shawcroft 2022-05-10 10:45:10 -07:00 committed by GitHub
commit b7b41c816c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 34 deletions

View file

@ -6,9 +6,7 @@
By updating the buffer being written to the display, the shown digits can be changed. By updating the buffer being written to the display, the shown digits can be changed.
The main program repeatedly shows random digits which 'lock' after a short The main program just counts up, looping back to 0000 after 9999.
time. After all digits have locked, it blanks for a short time and then repeats.
It also demonstrates the use of `asyncio` to perform multiple tasks.
This example is designed for a Raspberry Pi Pico and bare LED display. For This example is designed for a Raspberry Pi Pico and bare LED display. For
simplicity, it is wired without any current limiting resistors, instead relying simplicity, it is wired without any current limiting resistors, instead relying
@ -39,9 +37,8 @@ Wiring:
* Pico GP16 to LED matrix 14 (COM1) * Pico GP16 to LED matrix 14 (COM1)
""" """
import asyncio
import random
import array import array
import time
import board import board
import rp2pio import rp2pio
import adafruit_pioasm import adafruit_pioasm
@ -141,38 +138,25 @@ class SMSevenSegment:
else: else:
self._buf[i] = DIGITS_WT[v] & ~COM_WT[i] self._buf[i] = DIGITS_WT[v] & ~COM_WT[i]
def set_number(self, number):
async def digit_locker(s, i, wait): for j in range(4):
delay = 30 self[3 - j] = number % 10
d = random.randint(0, 9) number //= 10
while delay < 300:
d = (d + random.randint(1, 9)) % 10 # Tick to a new digit other than 'd'
s[i] = d
await asyncio.sleep(delay / 1000)
if wait:
wait -= 1
else:
delay = delay * 1.1
def shuffle(seq): def count(start=0):
for i in range(len(seq) - 1): val = start
j = random.randrange(i + 1, len(seq)) while True:
seq[i], seq[j] = seq[j], seq[i] yield val
val += 1
async def main(): def main():
waits = [100, 175, 225, 250]
with SMSevenSegment(board.GP9) as s: with SMSevenSegment(board.GP9) as s:
while True: for i in count():
shuffle(waits) s.set_number(i)
await asyncio.gather( time.sleep(0.05)
*(digit_locker(s, i, di) for i, di in enumerate(waits))
)
await asyncio.sleep(1)
for i in range(4):
s[i] = None
await asyncio.sleep(0.5)
asyncio.run(main()) if __name__ == "__main__":
main()

View file

@ -140,7 +140,6 @@ if __name__ == "__main__":
NEOPIXEL = board.NEOPIXEL NEOPIXEL = board.NEOPIXEL
NUM_PIXELS = 12 NUM_PIXELS = 12
pixels = NeoPixelBackground(NEOPIXEL, NUM_PIXELS) pixels = NeoPixelBackground(NEOPIXEL, NUM_PIXELS)
i = 0
while True: while True:
# Around 1 cycle per second # Around 1 cycle per second
pixels.fill(rainbowio.colorwheel(supervisor.ticks_ms() // 4)) pixels.fill(rainbowio.colorwheel(supervisor.ticks_ms() // 4))