2020-11-03 18:43:24 +00:00
|
|
|
# scale.py Test/demo of scale widget for nano-gui
|
|
|
|
|
|
|
|
# Released under the MIT License (MIT). See LICENSE.
|
2021-03-26 10:57:37 +00:00
|
|
|
# Copyright (c) 2020-2021 Peter Hinch
|
2020-11-03 18:43:24 +00:00
|
|
|
|
|
|
|
# Usage:
|
|
|
|
# import gui.demos.scale
|
2020-11-05 10:10:21 +00:00
|
|
|
|
|
|
|
# Initialise hardware and framebuf before importing modules.
|
2024-05-12 10:19:11 +00:00
|
|
|
# Uses asyncio and also the asynchronous do_refresh method if the driver
|
2021-03-26 10:57:37 +00:00
|
|
|
# supports it.
|
|
|
|
|
2020-11-05 10:10:21 +00:00
|
|
|
from color_setup import ssd # Create a display instance
|
|
|
|
|
2020-11-03 18:43:24 +00:00
|
|
|
from gui.core.nanogui import refresh
|
|
|
|
from gui.core.writer import CWriter
|
|
|
|
|
2024-05-12 10:19:11 +00:00
|
|
|
import asyncio
|
2020-11-03 18:43:24 +00:00
|
|
|
from gui.core.colors import *
|
|
|
|
import gui.fonts.arial10 as arial10
|
|
|
|
from gui.widgets.label import Label
|
|
|
|
from gui.widgets.scale import Scale
|
|
|
|
|
|
|
|
# COROUTINES
|
|
|
|
async def radio(scale):
|
|
|
|
cv = 88.0 # Current value
|
|
|
|
val = 108.0 # Target value
|
|
|
|
while True:
|
|
|
|
v1, v2 = val, cv
|
|
|
|
steps = 200
|
|
|
|
delta = (val - cv) / steps
|
|
|
|
for _ in range(steps):
|
|
|
|
cv += delta
|
|
|
|
# Map user variable to -1.0..+1.0
|
2024-05-12 10:19:11 +00:00
|
|
|
scale.value(2 * (cv - 88) / (108 - 88) - 1)
|
2020-11-03 18:43:24 +00:00
|
|
|
await asyncio.sleep_ms(200)
|
|
|
|
val, cv = v2, v1
|
|
|
|
|
2024-05-12 10:19:11 +00:00
|
|
|
|
2020-11-03 18:43:24 +00:00
|
|
|
async def default(scale, lbl):
|
|
|
|
cv = -1.0 # Current
|
|
|
|
val = 1.0
|
|
|
|
while True:
|
|
|
|
v1, v2 = val, cv
|
|
|
|
steps = 400
|
|
|
|
delta = (val - cv) / steps
|
|
|
|
for _ in range(steps):
|
|
|
|
cv += delta
|
|
|
|
scale.value(cv)
|
2024-05-12 10:19:11 +00:00
|
|
|
lbl.value("{:4.3f}".format(cv))
|
|
|
|
if hasattr(ssd, "do_refresh"):
|
|
|
|
# Option to reduce asyncio latency
|
2021-03-26 10:57:37 +00:00
|
|
|
await ssd.do_refresh()
|
|
|
|
else:
|
|
|
|
# Normal synchronous call
|
|
|
|
refresh(ssd)
|
2020-11-03 18:43:24 +00:00
|
|
|
await asyncio.sleep_ms(250)
|
|
|
|
val, cv = v2, v1
|
|
|
|
|
|
|
|
|
|
|
|
def test():
|
2020-11-05 10:10:21 +00:00
|
|
|
def tickcb(f, c):
|
|
|
|
if f > 0.8:
|
|
|
|
return RED
|
|
|
|
if f < -0.8:
|
|
|
|
return BLUE
|
|
|
|
return c
|
2024-05-12 10:19:11 +00:00
|
|
|
|
2020-11-05 10:10:21 +00:00
|
|
|
def legendcb(f):
|
2024-05-12 10:19:11 +00:00
|
|
|
return "{:2.0f}".format(88 + ((f + 1) / 2) * (108 - 88))
|
|
|
|
|
2020-12-14 10:25:42 +00:00
|
|
|
refresh(ssd, True) # Initialise and clear display.
|
2020-11-03 18:43:24 +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
|
|
|
scale1 = Scale(wri, 2, 2, width=124, legendcb=legendcb, pointercolor=RED, fontcolor=YELLOW)
|
2020-11-05 10:10:21 +00:00
|
|
|
asyncio.create_task(radio(scale1))
|
|
|
|
|
2024-05-12 10:19:11 +00:00
|
|
|
lbl = Label(
|
|
|
|
wri, ssd.height - wri.height - 2, 2, 50, bgcolor=DARKGREEN, bdcolor=RED, fgcolor=WHITE
|
|
|
|
)
|
2021-03-26 10:57:37 +00:00
|
|
|
# do_refresh is called with arg 4. In landscape mode this splits screen
|
|
|
|
# into segments of 240/4=60 lines. Here we ensure a scale straddles
|
|
|
|
# this boundary
|
2024-05-12 10:19:11 +00:00
|
|
|
scale = Scale(
|
|
|
|
wri, 55, 2, width=124, tickcb=tickcb, pointercolor=RED, fontcolor=YELLOW, bdcolor=CYAN
|
|
|
|
)
|
2020-11-03 18:43:24 +00:00
|
|
|
asyncio.run(default(scale, lbl))
|
|
|
|
|
2024-05-12 10:19:11 +00:00
|
|
|
|
2020-11-03 18:43:24 +00:00
|
|
|
test()
|