2024-05-12 10:19:11 +00:00
|
|
|
# asnano.py Test/demo program for use of nanogui with asyncio
|
2018-09-23 17:46:04 +00:00
|
|
|
# Uses Adafruit ssd1351-based OLED displays (change height to suit)
|
|
|
|
# Adafruit 1.5" 128*128 OLED display: https://www.adafruit.com/product/1431
|
|
|
|
# Adafruit 1.27" 128*96 display https://www.adafruit.com/product/1673
|
|
|
|
|
2020-06-12 14:08:59 +00:00
|
|
|
# Copyright (c) 2020 Peter Hinch
|
|
|
|
# Released under the MIT License (MIT) - see LICENSE file
|
2018-09-23 17:46:04 +00:00
|
|
|
|
2020-11-05 10:10:21 +00:00
|
|
|
# Initialise hardware and framebuf before importing modules.
|
|
|
|
from color_setup import ssd # Create a display instance
|
2018-09-23 17:46:04 +00:00
|
|
|
|
2024-05-12 10:19:11 +00:00
|
|
|
import asyncio
|
2018-09-23 17:46:04 +00:00
|
|
|
import pyb
|
|
|
|
import uos
|
2020-11-03 16:22:08 +00:00
|
|
|
from gui.core.writer import CWriter
|
2020-11-03 18:43:24 +00:00
|
|
|
from gui.core.nanogui import refresh
|
|
|
|
from gui.widgets.led import LED
|
|
|
|
from gui.widgets.meter import Meter
|
|
|
|
|
2018-09-23 17:46:04 +00:00
|
|
|
refresh(ssd)
|
|
|
|
|
|
|
|
# Fonts
|
2020-11-03 16:22:08 +00:00
|
|
|
import gui.fonts.arial10 as arial10
|
|
|
|
import gui.fonts.freesans20 as freesans20
|
2018-09-23 17:46:04 +00:00
|
|
|
|
2020-11-03 16:22:08 +00:00
|
|
|
from gui.core.colors import *
|
2018-09-23 17:46:04 +00:00
|
|
|
|
|
|
|
CWriter.set_textpos(ssd, 0, 0) # In case previous tests have altered it
|
|
|
|
wri = CWriter(ssd, arial10, GREEN, BLACK, verbose=False)
|
|
|
|
wri.set_clip(True, True, False)
|
|
|
|
|
2024-05-12 10:19:11 +00:00
|
|
|
color = lambda v: RED if v > 0.7 else YELLOW if v > 0.5 else GREEN
|
|
|
|
txt = lambda v: "ovr" if v > 0.7 else "high" if v > 0.5 else "ok"
|
|
|
|
|
2018-09-23 17:46:04 +00:00
|
|
|
|
|
|
|
async def meter(n, x, text, t):
|
2024-05-12 10:19:11 +00:00
|
|
|
print("Meter {} test.".format(n))
|
|
|
|
m = Meter(
|
|
|
|
wri,
|
|
|
|
5,
|
|
|
|
x,
|
|
|
|
divisions=4,
|
|
|
|
ptcolor=YELLOW,
|
|
|
|
label=text,
|
|
|
|
style=Meter.BAR,
|
|
|
|
legends=("0.0", "0.5", "1.0"),
|
|
|
|
)
|
|
|
|
l = LED(wri, ssd.height - 16 - wri.height, x, bdcolor=YELLOW, label="over")
|
2018-09-23 17:46:04 +00:00
|
|
|
while True:
|
2024-05-12 10:19:11 +00:00
|
|
|
v = int.from_bytes(uos.urandom(3), "little") / 16777216
|
2018-09-23 17:46:04 +00:00
|
|
|
m.value(v, color(v))
|
|
|
|
l.color(color(v))
|
|
|
|
l.text(txt(v), fgcolor=color(v))
|
|
|
|
refresh(ssd)
|
|
|
|
await asyncio.sleep_ms(t)
|
|
|
|
|
2024-05-12 10:19:11 +00:00
|
|
|
|
2018-09-23 17:46:04 +00:00
|
|
|
async def flash(n, t):
|
|
|
|
led = pyb.LED(n)
|
2020-06-12 14:08:59 +00:00
|
|
|
while True:
|
|
|
|
led.toggle()
|
|
|
|
await asyncio.sleep_ms(t)
|
2018-09-23 17:46:04 +00:00
|
|
|
|
2024-05-12 10:19:11 +00:00
|
|
|
|
2020-06-12 14:08:59 +00:00
|
|
|
async def killer(tasks):
|
2018-09-23 17:46:04 +00:00
|
|
|
sw = pyb.Switch()
|
|
|
|
while not sw():
|
|
|
|
await asyncio.sleep_ms(100)
|
2020-06-12 14:08:59 +00:00
|
|
|
for task in tasks:
|
|
|
|
task.cancel()
|
|
|
|
|
2024-05-12 10:19:11 +00:00
|
|
|
|
2020-06-12 14:08:59 +00:00
|
|
|
async def main():
|
|
|
|
tasks = []
|
2024-05-12 10:19:11 +00:00
|
|
|
tasks.append(asyncio.create_task(meter(1, 2, "left", 700)))
|
|
|
|
tasks.append(asyncio.create_task(meter(2, 50, "right", 1000)))
|
|
|
|
tasks.append(asyncio.create_task(meter(3, 98, "bass", 1500)))
|
2020-06-12 14:08:59 +00:00
|
|
|
tasks.append(asyncio.create_task(flash(1, 200)))
|
|
|
|
tasks.append(asyncio.create_task(flash(2, 233)))
|
|
|
|
await killer(tasks)
|
2018-09-23 17:46:04 +00:00
|
|
|
|
2024-05-12 10:19:11 +00:00
|
|
|
|
|
|
|
print("Press Pyboard usr button to stop test.")
|
2020-06-12 14:08:59 +00:00
|
|
|
try:
|
|
|
|
asyncio.run(main())
|
2024-05-12 10:19:11 +00:00
|
|
|
finally: # Reset asyncio case of KeyboardInterrupt
|
2020-06-12 14:08:59 +00:00
|
|
|
asyncio.new_event_loop()
|