Skip to content

Simplify first 7-segment example into a counter #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions examples/pioasm_7seg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

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
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.
The main program just counts up, looping back to 0000 after 9999.

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
Expand Down Expand Up @@ -39,9 +37,8 @@
* Pico GP16 to LED matrix 14 (COM1)
"""

import asyncio
import random
import array
import time
import board
import rp2pio
import adafruit_pioasm
Expand Down Expand Up @@ -141,38 +138,25 @@ def __setitem__(self, i, v):
else:
self._buf[i] = DIGITS_WT[v] & ~COM_WT[i]


async def digit_locker(s, i, wait):
delay = 30
d = random.randint(0, 9)
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 set_number(self, number):
for j in range(4):
self[3 - j] = number % 10
number //= 10


def shuffle(seq):
for i in range(len(seq) - 1):
j = random.randrange(i + 1, len(seq))
seq[i], seq[j] = seq[j], seq[i]
def count(start=0):
val = start
while True:
yield val
val += 1


async def main():
waits = [100, 175, 225, 250]
def main():
with SMSevenSegment(board.GP9) as s:
while True:
shuffle(waits)
await asyncio.gather(
*(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())
for i in count():
s.set_number(i)
time.sleep(0.05)


if __name__ == "__main__":
main()
1 change: 0 additions & 1 deletion examples/pioasm_neopixel_bg.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ def _transmit(self, buf):
NEOPIXEL = board.NEOPIXEL
NUM_PIXELS = 12
pixels = NeoPixelBackground(NEOPIXEL, NUM_PIXELS)
i = 0
while True:
# Around 1 cycle per second
pixels.fill(rainbowio.colorwheel(supervisor.ticks_ms() // 4))