2021-06-09 16:11:48 +00:00
|
|
|
# various.py micro-gui demo of multiple controls on a large display
|
|
|
|
|
2021-06-24 17:42:16 +00:00
|
|
|
# Released under the MIT License (MIT). See LICENSE.
|
|
|
|
# Copyright (c) 2021 Peter Hinch
|
|
|
|
|
2021-06-09 16:11:48 +00:00
|
|
|
# Initialise hardware and framebuf before importing modules.
|
2021-06-12 10:59:12 +00:00
|
|
|
# Create SSD instance. Must be done first because of RAM use.
|
2021-06-20 15:46:14 +00:00
|
|
|
import hardware_setup
|
2021-06-12 10:59:12 +00:00
|
|
|
|
2021-06-20 15:46:14 +00:00
|
|
|
from gui.core.ugui import Screen, ssd
|
2021-06-09 16:11:48 +00:00
|
|
|
from gui.core.writer import CWriter
|
|
|
|
import gui.fonts.arial10 as arial10 # Font for CWriter
|
|
|
|
from gui.core.colors import *
|
|
|
|
# Widgets
|
|
|
|
from gui.widgets.label import Label
|
|
|
|
from gui.widgets.dial import Dial, Pointer
|
|
|
|
from gui.widgets.meter import Meter
|
|
|
|
from gui.widgets.scale import Scale
|
|
|
|
from gui.widgets.buttons import Button, ButtonList, RadioButtons, CloseButton
|
|
|
|
from gui.widgets.checkbox import Checkbox
|
|
|
|
from gui.widgets.led import LED
|
|
|
|
|
|
|
|
import cmath
|
|
|
|
import uasyncio as asyncio
|
|
|
|
import utime
|
|
|
|
import gc
|
|
|
|
|
|
|
|
|
|
|
|
class FooScreen(Screen):
|
|
|
|
def __init__(self):
|
|
|
|
buttons = []
|
|
|
|
|
|
|
|
# A ButtonList with two entries
|
|
|
|
table_buttonset = (
|
|
|
|
{'fgcolor' : RED, 'text' : 'Disable', 'args' : (buttons, True)},
|
|
|
|
{'fgcolor' : GREEN, 'text' : 'Enable', 'args' : (buttons, False)},
|
|
|
|
)
|
|
|
|
|
|
|
|
table_radiobuttons = (
|
|
|
|
{'text' : '1', 'args' : ('1',)},
|
|
|
|
{'text' : '2', 'args' : ('2',)},
|
|
|
|
{'text' : '3', 'args' : ('3',)},
|
|
|
|
{'text' : '4', 'args' : ('4',)},
|
|
|
|
)
|
|
|
|
|
|
|
|
def tickcb(f, c):
|
|
|
|
if f > 0.8:
|
|
|
|
return RED
|
|
|
|
if f < -0.8:
|
|
|
|
return BLUE
|
|
|
|
return c
|
|
|
|
|
|
|
|
def bcb(b):
|
|
|
|
print('Button pressed', b)
|
|
|
|
|
|
|
|
super().__init__()
|
|
|
|
self.rb0 = None
|
|
|
|
self.bs0 = None
|
|
|
|
wri = CWriter(ssd, arial10, GREEN, BLACK, verbose=False)
|
2021-06-11 14:33:05 +00:00
|
|
|
lbltim = Label(wri, 65, 100, 'this is a test', bdcolor=RED)
|
2021-06-09 16:11:48 +00:00
|
|
|
|
2021-06-11 14:33:05 +00:00
|
|
|
m0 = Meter(wri, 10, 240, divisions = 4, ptcolor=YELLOW, height=80, width=15,
|
|
|
|
label='Meter example', style=Meter.BAR, legends=('0.0', '0.5', '1.0'))
|
2021-06-09 16:11:48 +00:00
|
|
|
# Instantiate displayable objects. bgcolor forces complete redraw.
|
|
|
|
dial = Dial(wri, 2, 2, height = 75, ticks = 12, bgcolor=BLACK, bdcolor=None, label=120) # Border in fg color
|
|
|
|
scale = Scale(wri, 2, 100, width = 124, tickcb = tickcb,
|
|
|
|
pointercolor=RED, fontcolor=YELLOW, bdcolor=CYAN)
|
|
|
|
|
2021-06-11 14:33:05 +00:00
|
|
|
row = 105
|
|
|
|
col = 2
|
|
|
|
Label(wri, row, col, 'Normal buttons')
|
2021-06-09 16:11:48 +00:00
|
|
|
# Four Button instances
|
|
|
|
row = 120
|
|
|
|
ht = 30
|
|
|
|
for i, s in enumerate(('a', 'b', 'c', 'd')):
|
|
|
|
col= 2 + i * (ht + 5)
|
|
|
|
buttons.append(Button(wri, row, col, height=ht, callback=bcb, text=s, litcolor=RED, shape=CIRCLE, bgcolor=DARKGREEN))
|
|
|
|
|
|
|
|
# ButtonList
|
|
|
|
self.bs = ButtonList(self.callback)
|
|
|
|
self.bs0 = None
|
|
|
|
col+= 50
|
2021-06-11 14:33:05 +00:00
|
|
|
Label(wri, row - 15, col, 'ButtonList')
|
2021-06-09 16:11:48 +00:00
|
|
|
for t in table_buttonset: # Buttons overlay each other at same location
|
2021-06-11 14:33:05 +00:00
|
|
|
button = self.bs.add_button(wri, row, col, shape=RECTANGLE, textcolor=BLUE, height=30, **t)
|
2021-06-09 16:11:48 +00:00
|
|
|
if self.bs0 is None: # Save for reset button callback
|
|
|
|
self.bs0 = button
|
|
|
|
|
|
|
|
# Reset button
|
|
|
|
col+= 60
|
|
|
|
btn = Button(wri, row, col, height=30, callback=self.rstcb, text='reset', litcolor=RED, fgcolor=GREEN, bgcolor=DARKGREEN)
|
|
|
|
|
2021-06-11 14:33:05 +00:00
|
|
|
col = 2
|
|
|
|
row = 170
|
|
|
|
Label(wri, row, col, 'Radio buttons')
|
2021-06-09 16:11:48 +00:00
|
|
|
# Radio buttons
|
2021-06-11 14:33:05 +00:00
|
|
|
row = 185
|
2021-06-09 16:11:48 +00:00
|
|
|
self.rb = RadioButtons(BLUE, self.rbcb) # color of selected button
|
|
|
|
self.rb0 = None
|
|
|
|
for t in table_radiobuttons:
|
2021-06-11 14:33:05 +00:00
|
|
|
button = self.rb.add_button(wri, row, col, textcolor = WHITE,
|
2021-06-09 16:11:48 +00:00
|
|
|
fgcolor = BLUE, bgcolor = DARKBLUE, shape=CIRCLE, height = 30, **t)
|
|
|
|
if self.rb0 is None: # Save for reset button callback
|
|
|
|
self.rb0 = button
|
|
|
|
col+= 35
|
|
|
|
# Checkbox
|
|
|
|
col+= 35
|
2021-06-11 14:33:05 +00:00
|
|
|
Label(wri, row - 15, col, 'Checkbox and LED')
|
|
|
|
Checkbox(wri, row, col, callback=self.cbcb)
|
2021-06-09 16:11:48 +00:00
|
|
|
col+= 40
|
2021-06-11 14:33:05 +00:00
|
|
|
self.led = LED(wri, row, col, color=YELLOW, bdcolor=GREEN)
|
2021-06-09 16:11:48 +00:00
|
|
|
CloseButton(wri)
|
|
|
|
asyncio.create_task(run(dial, lbltim, m0, scale))
|
|
|
|
|
|
|
|
|
|
|
|
def callback(self, button, buttons, val):
|
|
|
|
buttons[2].greyed_out(val)
|
|
|
|
|
|
|
|
def rbcb(self, button, val):
|
|
|
|
print('RadioButtons callback', val)
|
|
|
|
|
|
|
|
def rstcb(self, button):
|
|
|
|
print('Reset button: init ButtonList and RadioButtons')
|
2021-06-11 14:33:05 +00:00
|
|
|
self.bs.value(self.bs0)
|
2021-06-09 16:11:48 +00:00
|
|
|
self.rb.value(self.rb0)
|
|
|
|
|
|
|
|
def cbcb(self, cb):
|
|
|
|
self.led.value(cb.value())
|
|
|
|
gc.collect()
|
|
|
|
print('Free RAM:', gc.mem_free())
|
|
|
|
|
|
|
|
async def run(dial, lbltim, m0, scale):
|
|
|
|
days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
|
|
|
|
'Sunday')
|
|
|
|
months = ('Jan', 'Feb', 'March', 'April', 'May', 'June', 'July',
|
|
|
|
'Aug', 'Sept', 'Oct', 'Nov', 'Dec')
|
|
|
|
uv = lambda phi : cmath.rect(1, phi) # Return a unit vector of phase phi
|
|
|
|
pi = cmath.pi
|
|
|
|
hrs = Pointer(dial)
|
|
|
|
mins = Pointer(dial)
|
|
|
|
secs = Pointer(dial)
|
|
|
|
|
|
|
|
hstart = 0 + 0.7j # Pointer lengths and position at top
|
|
|
|
mstart = 0 + 0.92j
|
|
|
|
sstart = 0 + 0.92j
|
|
|
|
|
|
|
|
cv = -1.0 # Scale
|
|
|
|
dv = 0.005
|
|
|
|
while True:
|
|
|
|
t = utime.localtime()
|
|
|
|
hrs.value(hstart * uv(-t[3]*pi/6 - t[4]*pi/360), YELLOW)
|
|
|
|
mins.value(mstart * uv(-t[4] * pi/30), YELLOW)
|
|
|
|
secs.value(sstart * uv(-t[5] * pi/30), RED)
|
|
|
|
lbltim.value('{:02d}.{:02d}.{:02d}'.format(t[3], t[4], t[5]))
|
|
|
|
dial.text('{} {} {} {}'.format(days[t[6]], t[2], months[t[1] - 1], t[0]))
|
|
|
|
m0.value(t[5]/60)
|
|
|
|
scale.value(cv)
|
|
|
|
await asyncio.sleep_ms(200)
|
|
|
|
cv += dv
|
|
|
|
if abs(cv) > 1.0:
|
|
|
|
dv = -dv
|
|
|
|
cv += dv
|
|
|
|
|
|
|
|
|
|
|
|
def test():
|
2021-06-12 10:59:12 +00:00
|
|
|
if ssd.height < 240 or ssd.width < 320:
|
2021-06-09 16:11:48 +00:00
|
|
|
print(' This test requires a display of at least 320x240 pixels.')
|
|
|
|
else:
|
|
|
|
print('Testing micro-gui...')
|
|
|
|
Screen.change(FooScreen)
|
|
|
|
|
|
|
|
test()
|