Use new FB metods. Requires MP>=V1.20

encoder_driver
peterhinch 2023-04-30 12:12:20 +01:00
rodzic 9227e1c215
commit eb1f7f1987
1 zmienionych plików z 37 dodań i 40 usunięć

Wyświetl plik

@ -11,10 +11,14 @@ import uasyncio as asyncio
from time import ticks_diff, ticks_ms from time import ticks_diff, ticks_ms
import gc import gc
from array import array from array import array
import sys
from gui.core.colors import * from gui.core.colors import *
from gui.primitives import Pushbutton from gui.primitives import Pushbutton
if sys.implementation.version < (1, 20, 0):
raise OSError("Firmware V1.20 or later required.")
# Globally available singleton objects # Globally available singleton objects
display = None # Singleton instance display = None # Singleton instance
ssd = None ssd = None
@ -121,25 +125,29 @@ class Input:
class DisplayIP: class DisplayIP:
# Populate array for clipped rect # Populate array for clipped rect
@staticmethod @staticmethod
def crect(x, y, w, h, acr = array("H", (0 for _ in range(16)))): def crect(x, y, w, h):
c = 4 # Clip pixels c = 4 # Clip pixels
acr[0] = x + c return array(
acr[1] = y "H",
acr[2] = x + w - c (
acr[3] = y x + c,
acr[4] = x + w y,
acr[5] = y + c x + w - c,
acr[6] = x + w y,
acr[7] = y + h - c x + w,
acr[8] = x + w - c y + c,
acr[9] = y + h x + w,
acr[10] = x + c y + h - c,
acr[11] = y + h x + w - c,
acr[12] = x y + h,
acr[13] = y + h - c x + c,
acr[14] = x y + h,
acr[15] = y + c x,
return acr y + h - c,
x,
y + c,
),
)
def __init__(self, ipdev): def __init__(self, ipdev):
self.ipdev = ipdev self.ipdev = ipdev
@ -167,11 +175,9 @@ class DisplayIP:
# Greying out has only one option given limitation of 4-bit display driver # Greying out has only one option given limitation of 4-bit display driver
# It would be possible to do better with RGB565 but would need inverse transformation # It would be possible to do better with RGB565 but would need inverse transformation
# to (r, g, b), scale and re-convert to integer. # to (r, g, b), scale and re-convert to integer.
def _getcolor(self, color): def _getcolor(self, color):
# Takes in an integer color, bit size dependent on driver # Takes in an integer color, bit size dependent on driver
return ( return color_map[GREY_OUT] if self._is_grey and color != color_map[BG] else color
color_map[GREY_OUT] if self._is_grey and color != color_map[BG] else color
)
def usegrey(self, val): # display.usegrey(True) sets greyed-out def usegrey(self, val): # display.usegrey(True) sets greyed-out
self._is_grey = val self._is_grey = val
@ -216,8 +222,9 @@ class DisplayIP:
# Define an input device and populate global ssd and display objects. # Define an input device and populate global ssd and display objects.
class Display(DisplayIP): class Display(DisplayIP):
def __init__(self, objssd, nxt, sel, prev=None, incr=None, decr=None, def __init__(
encoder=False, touch=False): self, objssd, nxt, sel, prev=None, incr=None, decr=None, encoder=False, touch=False
):
global display, ssd global display, ssd
ssd = objssd ssd = objssd
if touch: if touch:
@ -390,9 +397,7 @@ class Screen:
self.width = ssd.width self.width = ssd.width
self.row = 0 self.row = 0
self.col = 0 self.col = 0
if ( if Screen.current_screen is None and Screen.do_gc: # Initialising class and task
Screen.current_screen is None and Screen.do_gc
): # Initialising class and task
# Here we create singleton tasks # Here we create singleton tasks
asyncio.create_task(self._garbage_collect()) asyncio.create_task(self._garbage_collect())
Screen.current_screen = self Screen.current_screen = self
@ -510,8 +515,8 @@ class Screen:
# Very basic window class. Cuts a rectangular hole in a screen on which # Very basic window class. Cuts a rectangular hole in a screen on which
# content may be drawn. # content may be drawn.
class Window(Screen): class Window(Screen):
_value = None _value = None
# Allow a Window to store an arbitrary object. Retrieval may be # Allow a Window to store an arbitrary object. Retrieval may be
# done by caller, after the Window instance was deleted # done by caller, after the Window instance was deleted
@classmethod @classmethod
@ -656,9 +661,7 @@ class Widget:
if hasattr(self, "label"): if hasattr(self, "label"):
self.label.value(text, invert, fgcolor, bgcolor, bdcolor) self.label.value(text, invert, fgcolor, bgcolor, bdcolor)
else: else:
raise ValueError( raise ValueError("Method {}.text does not exist.".format(self.__class__.__name__))
"Method {}.text does not exist.".format(self.__class__.__name__)
)
# Called from subclass prior to populating framebuf with control # Called from subclass prior to populating framebuf with control
def show(self, black=True): def show(self, black=True):
@ -672,9 +675,7 @@ class Widget:
dev = display.usegrey(self._greyed_out) dev = display.usegrey(self._greyed_out)
x = self.col x = self.col
y = self.row y = self.row
dev.fill_rect( dev.fill_rect(x, y, self.width, self.height, color_map[BG] if black else self.bgcolor)
x, y, self.width, self.height, color_map[BG] if black else self.bgcolor
)
return True return True
# Called by Screen.show(). Draw background and bounding box if required. # Called by Screen.show(). Draw background and bounding box if required.
@ -777,9 +778,7 @@ class LinearIO(Widget):
): ):
self.min_delta = min_delta self.min_delta = min_delta
self.max_delta = max_delta self.max_delta = max_delta
super().__init__( super().__init__(writer, row, col, height, width, fgcolor, bgcolor, bdcolor, value, active)
writer, row, col, height, width, fgcolor, bgcolor, bdcolor, value, active
)
self.adjustable = True # Can show adjustable border self.adjustable = True # Can show adjustable border
self.do_precision = prcolor is not False self.do_precision = prcolor is not False
if self.do_precision: if self.do_precision:
@ -794,9 +793,7 @@ class LinearIO(Widget):
# Handle increase and decrease buttons. Redefined by textbox.py, scale_log.py # Handle increase and decrease buttons. Redefined by textbox.py, scale_log.py
async def btnhan(self, button, up, d): async def btnhan(self, button, up, d):
maxd = ( maxd = self.max_delta if self.precision() else d * 4 # Why move fast in precision mode?
self.max_delta if self.precision() else d * 4
) # Why move fast in precision mode?
t = ticks_ms() t = ticks_ms()
while button(): while button():
await asyncio.sleep_ms(0) # Quit fast on button release await asyncio.sleep_ms(0) # Quit fast on button release