abstracting display, touch and sd

This commit is contained in:
Adolfo Reyna
2026-01-28 20:12:41 -05:00
parent 57426c6e7d
commit adfbef7228
396 changed files with 101836 additions and 272 deletions

View File

@@ -0,0 +1,125 @@
# ePaper2in9_test_async.py Demo program for nano_gui on 2.9" EPD display
# Released under the MIT License (MIT). See LICENSE.
# Copyright (c) 2020 Peter Hinch
# color_setup must set landcsape True, asyn True and must not set demo_mode
from cmath import exp, pi
import uasyncio as asyncio
from color_setup import ssd
# On a monochrome display Writer is more efficient than CWriter.
from gui.core.writer import Writer
from gui.core.nanogui import refresh
from gui.widgets.meter import Meter
from gui.widgets.label import Label
from gui.widgets.dial import Dial, Pointer
# Fonts
import gui.fonts.arial10 as arial10
import gui.fonts.font6 as small
ssd._asyn = True # HACK to make it config agnostic
# Some ports don't support uos.urandom.
# See https://github.com/peterhinch/micropython-samples/tree/master/random
def xorshift64star(modulo, seed = 0xf9ac6ba4):
x = seed
def func():
nonlocal x
x ^= x >> 12
x ^= ((x << 25) & 0xffffffffffffffff) # modulo 2**64
x ^= x >> 27
return (x * 0x2545F4914F6CDD1D) % modulo
return func
async def compass(evt):
wri = Writer(ssd, arial10, verbose=False)
wri.set_clip(False, False, False)
v1 = 0 + 0.9j
v2 = exp(0 - (pi / 6) * 1j)
dial = Dial(wri, 5, 5, height = 75, ticks = 12, bdcolor=None,
label='Direction', style = Dial.COMPASS)
ptr = Pointer(dial)
while True:
ptr.value(v1)
v1 *= v2
await evt.wait()
async def multi_fields(evt):
wri = Writer(ssd, small, verbose=False)
wri.set_clip(False, False, False)
nfields = []
dy = small.height() + 10
row = 2
col = 100
width = wri.stringlen('99.990')
for txt in ('X:', 'Y:', 'Z:'):
Label(wri, row, col, txt)
nfields.append(Label(wri, row, col, width, bdcolor=None)) # Draw border
row += dy
random = xorshift64star(2**24 - 1)
while True:
for _ in range(10):
for field in nfields:
value = random() / 167772
field.value('{:5.2f}'.format(value))
await evt.wait()
async def meter(evt):
wri = Writer(ssd, arial10, verbose=False)
wri.set_clip(False, False, False)
row = 10
col = 170
args = {'height' : 80,
'width' : 15,
'divisions' : 4,
'style' : Meter.BAR}
m0 = Meter(wri, row, col, legends=('0.0', '0.5', '1.0'), **args)
m1 = Meter(wri, row, col + 40, legends=('-1', '0', '+1'), **args)
m2 = Meter(wri, row, col + 80, legends=('-1', '0', '+1'), **args)
random = xorshift64star(2**24 - 1)
while True:
steps = 10
for n in range(steps + 1):
m0.value(random() / 16777216)
m1.value(n/steps)
m2.value(1 - n/steps)
await evt.wait()
async def main():
refresh(ssd, True) # Clear display
await ssd.wait()
print('Ready')
evt = asyncio.Event()
asyncio.create_task(meter(evt))
asyncio.create_task(multi_fields(evt))
asyncio.create_task(compass(evt))
while True:
# Normal procedure before refresh, but 10s sleep should mean it always returns immediately
await ssd.wait()
refresh(ssd) # Launches ._as_show()
await ssd.updated()
# Content has now been shifted out so coros can update
# framebuffer in background
evt.set()
evt.clear()
await asyncio.sleep(10) # Allow for slow refresh
tstr = '''Test of asynchronous code updating the EPD. This should
not be run for long periods as the EPD should not be updated more
frequently than every 180s.
'''
print(tstr)
try:
asyncio.run(main())
except KeyboardInterrupt:
# Defensive code: avoid leaving EPD hardware in an undefined state.
print('Waiting for display to become idle')
ssd.sleep() # Synchronous code. May block for 5s if display is updating.
finally:
_ = asyncio.new_event_loop()

View File

@@ -0,0 +1,79 @@
# ePaper2in9_test_sync.py Demo of synchronous code on 2.9" EPD display
# Released under the MIT License (MIT). See LICENSE.
# Copyright (c) 2020 Peter Hinch
# color_setup must set landcsape True, asyn False and must not set demo_mode
from math import pi, sin
from color_setup import ssd
from gui.core.writer import Writer
from gui.core.nanogui import refresh
from gui.core.fplot import CartesianGraph, Curve
from gui.widgets.meter import Meter
from gui.widgets.label import Label
from gui.widgets.dial import Dial, Pointer
# Fonts
import gui.fonts.arial10 as arial10
import gui.fonts.freesans20 as large
wri = Writer(ssd, arial10, verbose=False)
wri.set_clip(False, False, False)
wri_large = Writer(ssd, large, verbose=False)
wri_large.set_clip(False, False, False)
# 296*128
def graph():
row, col, ht, wd = 5, 140, 75, 150
def populate():
x = -0.998
while x < 1.01:
z = 6 * pi * x
y = sin(z) / z
yield x, y
x += 0.05
g = CartesianGraph(wri, row, col, height = ht, width = wd, bdcolor=False)
curve2 = Curve(g, None, populate())
Label(wri, row + ht + 5, col - 10, '-2.0 t: secs')
Label(wri, row + ht + 5, col - 8 + int(wd//2), '0.0')
Label(wri, row + ht + 5, col - 10 + wd, '2.0')
def compass():
dial = Dial(wri, 5, 5, height = 75, ticks = 12, bdcolor=None,
label='Direction', style = Dial.COMPASS)
ptr = Pointer(dial)
ptr.value(1 + 1j)
def meter():
m = Meter(wri, 5, 100, height = 75, divisions = 4,
label='Peak', style=Meter.BAR, legends=('0', '50', '100'))
m.value(0.72)
def labels():
row = 100
col = 0
Label(wri_large, row, col, 'Seismograph')
col = 140
Label(wri, row, col + 0, 'Event time')
Label(wri, row, col + 60, '01:35', bdcolor=None)
Label(wri, row, col + 95, 'UTC')
row = 115
Label(wri, row, col + 0, 'Event date')
Label(wri, row, col + 60, '6th Jan 2021', bdcolor=None)
def main():
refresh(ssd, True)
graph()
compass()
meter()
labels()
ssd.wait_until_ready()
refresh(ssd)
print('Waiting for display update')
ssd.wait_until_ready()
main()

View File

@@ -0,0 +1,124 @@
# ePaper_test.py Demo program for nano_gui on an Waveshare ePaper screen
# Released under the MIT License (MIT). See LICENSE.
# Copyright (c) 2020 Peter Hinch
# color_setup must set landcsape False, asyn True and must not set demo_mode
import uasyncio as asyncio
from color_setup import ssd
from gui.core.writer import Writer
from gui.core.nanogui import refresh
from gui.widgets.meter import Meter
from gui.widgets.label import Label
# Fonts
import gui.fonts.arial10 as arial10
import gui.fonts.courier20 as fixed
import gui.fonts.font6 as small
# Some ports don't support uos.urandom.
# See https://github.com/peterhinch/micropython-samples/tree/master/random
def xorshift64star(modulo, seed = 0xf9ac6ba4):
x = seed
def func():
nonlocal x
x ^= x >> 12
x ^= ((x << 25) & 0xffffffffffffffff) # modulo 2**64
x ^= x >> 27
return (x * 0x2545F4914F6CDD1D) % modulo
return func
async def fields(evt):
wri = Writer(ssd, fixed, verbose=False)
wri.set_clip(False, False, False)
textfield = Label(wri, 0, 2, wri.stringlen('longer'))
numfield = Label(wri, 25, 2, wri.stringlen('99.990'), bdcolor=None)
countfield = Label(wri, 0, 90, wri.stringlen('1'))
n = 1
random = xorshift64star(65535)
while True:
for s in ('short', 'longer', '1', ''):
textfield.value(s)
numfield.value('{:5.2f}'.format(random() /1000))
countfield.value('{:1d}'.format(n))
n += 1
await evt.wait()
async def multi_fields(evt):
wri = Writer(ssd, small, verbose=False)
wri.set_clip(False, False, False)
nfields = []
dy = small.height() + 10
y = 80
col = 20
width = wri.stringlen('99.990')
for txt in ('X:', 'Y:', 'Z:'):
Label(wri, y, 0, txt)
nfields.append(Label(wri, y, col, width, bdcolor=None)) # Draw border
y += dy
random = xorshift64star(2**24 - 1)
while True:
for _ in range(10):
for field in nfields:
value = random() / 167772
field.value('{:5.2f}'.format(value))
await evt.wait()
async def meter(evt):
wri = Writer(ssd, arial10, verbose=False)
args = {'height' : 80,
'width' : 15,
'divisions' : 4,
'style' : Meter.BAR}
m0 = Meter(wri, 165, 2, legends=('0.0', '0.5', '1.0'), **args)
m1 = Meter(wri, 165, 62, legends=('-1', '0', '+1'), **args)
m2 = Meter(wri, 165, 122, legends=('-1', '0', '+1'), **args)
random = xorshift64star(2**24 - 1)
while True:
steps = 10
for n in range(steps + 1):
m0.value(random() / 16777216)
m1.value(n/steps)
m2.value(1 - n/steps)
await evt.wait()
async def main():
ssd.fill(1)
ssd.show()
await ssd.wait()
refresh(ssd, True) # Clear display
await ssd.wait()
print('Ready')
evt = asyncio.Event()
asyncio.create_task(meter(evt))
asyncio.create_task(multi_fields(evt))
asyncio.create_task(fields(evt))
while True:
# Normal procedure before refresh, but 10s sleep should mean it always returns immediately
await ssd.wait()
refresh(ssd) # Launches ._as_show()
await ssd.updated()
# Content has now been shifted out so coros can update
# framebuffer in background
evt.set()
evt.clear()
await asyncio.sleep(9) # Allow for slow refresh
tstr = '''Runs the following tests, updates every 10s
fields() Label test with dynamic data.
multi_fields() More Labels.
meter() Demo of Meter object.
'''
print(tstr)
try:
asyncio.run(main())
except KeyboardInterrupt:
print('Waiting for display to become idle')
ssd.wait_until_ready() # Synchronous code
finally:
_ = asyncio.new_event_loop()

View File

@@ -0,0 +1,132 @@
# ePaper_test_B.py Demo program for nano_gui on an Waveshare ePaper B screen
# Released under the MIT License (MIT). See LICENSE.
# Copyright (c) 2020 Peter Hinch
# color_setup must set landcsape False, asyn True and must not set demo_mode
import uasyncio as asyncio
from color_setup import ssd
from color_setup import ssdred
from gui.core.writer import Writer
from gui.core.nanogui import refresh
from gui.widgets.meter import Meter
from gui.widgets.label import Label
# Fonts
import gui.fonts.arial10 as arial10
import gui.fonts.courier20 as fixed
import gui.fonts.font6 as small
# Some ports don't support uos.urandom.
# See https://github.com/peterhinch/micropython-samples/tree/master/random
def xorshift64star(modulo, seed = 0xf9ac6ba4):
x = seed
def func():
nonlocal x
x ^= x >> 12
x ^= ((x << 25) & 0xffffffffffffffff) # modulo 2**64
x ^= x >> 27
return (x * 0x2545F4914F6CDD1D) % modulo
return func
async def fields(evt):
wri = Writer(ssd, fixed, verbose=False)
wri.set_clip(False, False, False)
textfield = Label(wri, 0, 2, wri.stringlen('longer'))
numfield = Label(wri, 25, 2, wri.stringlen('99.990'), bdcolor=None)
countfield = Label(wri, 0, 90, wri.stringlen('1'))
n = 1
random = xorshift64star(65535)
while True:
for s in ('short', 'longer', '1', ''):
textfield.value(s)
numfield.value('{:5.2f}'.format(random() /1000))
countfield.value('{:1d}'.format(n))
n += 1
await evt.wait()
async def multi_fields(evt):
wri = Writer(ssd, small, verbose=False)
wri.set_clip(False, False, False)
nfields = []
dy = small.height() + 10
y = 80
col = 20
width = wri.stringlen('99.990')
for txt in ('X:', 'Y:', 'Z:'):
Label(wri, y, 0, txt)
nfields.append(Label(wri, y, col, width, bdcolor=None)) # Draw border
y += dy
random = xorshift64star(2**24 - 1)
while True:
for _ in range(10):
for field in nfields:
value = random() / 167772
field.value('{:5.2f}'.format(value))
await evt.wait()
async def meter(evt):
wri = Writer(ssdred, arial10, verbose=False)
args = {'height' : 80,
'width' : 15,
'divisions' : 4,
'style' : Meter.BAR}
m0 = Meter(wri, 165, 2, legends=('0.0', '0.5', '1.0'), **args)
m1 = Meter(wri, 165, 62, legends=('-1', '0', '+1'), **args)
m2 = Meter(wri, 165, 122, legends=('-1', '0', '+1'), **args)
random = xorshift64star(2**24 - 1)
while True:
steps = 10
for n in range(steps + 1):
m0.value(random() / 16777216)
m1.value(n/steps)
m2.value(1 - n/steps)
await evt.wait()
async def main():
ssdred.fill(1)
ssd.fill(1)
ssdred.show()
ssd.show()
await ssd.wait()
refresh(ssdred, True)
refresh(ssd, True) # Clear display
await ssd.wait()
print('Ready')
evt = asyncio.Event()
asyncio.create_task(meter(evt))
asyncio.create_task(multi_fields(evt))
asyncio.create_task(fields(evt))
while True:
# Normal procedure before refresh, but 10s sleep should mean it always returns immediately
await ssd.wait()
refresh(ssdred)
refresh(ssd) # Launches ._as_show()
await ssd.updated()
# Content has now been shifted out so coros can update
# framebuffer in background
evt.set()
evt.clear()
await asyncio.sleep(9) # Allow for slow refresh
tstr = '''Runs the following tests, updates every 10s
fields() Label test with dynamic data.
multi_fields() More Labels.
meter() Demo of Meter object.
'''
print(tstr)
try:
asyncio.run(main())
except KeyboardInterrupt:
print('Waiting for display to become idle')
ssd.wait_until_ready() # Synchronous code
finally:
_ = asyncio.new_event_loop()