From eb1f7f198738cc06f87f61914bc17bb4d291a146 Mon Sep 17 00:00:00 2001 From: peterhinch Date: Sun, 30 Apr 2023 12:12:20 +0100 Subject: [PATCH] Use new FB metods. Requires MP>=V1.20 --- gui/core/ugui.py | 77 +++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/gui/core/ugui.py b/gui/core/ugui.py index f05636e..d19d4b5 100644 --- a/gui/core/ugui.py +++ b/gui/core/ugui.py @@ -11,10 +11,14 @@ import uasyncio as asyncio from time import ticks_diff, ticks_ms import gc from array import array +import sys from gui.core.colors import * from gui.primitives import Pushbutton +if sys.implementation.version < (1, 20, 0): + raise OSError("Firmware V1.20 or later required.") + # Globally available singleton objects display = None # Singleton instance ssd = None @@ -121,25 +125,29 @@ class Input: class DisplayIP: # Populate array for clipped rect @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 - acr[0] = x + c - acr[1] = y - acr[2] = x + w - c - acr[3] = y - acr[4] = x + w - acr[5] = y + c - acr[6] = x + w - acr[7] = y + h - c - acr[8] = x + w - c - acr[9] = y + h - acr[10] = x + c - acr[11] = y + h - acr[12] = x - acr[13] = y + h - c - acr[14] = x - acr[15] = y + c - return acr + return array( + "H", + ( + x + c, + y, + x + w - c, + y, + x + w, + y + c, + x + w, + y + h - c, + x + w - c, + y + h, + x + c, + y + h, + x, + y + h - c, + x, + y + c, + ), + ) def __init__(self, ipdev): self.ipdev = ipdev @@ -167,11 +175,9 @@ class DisplayIP: # 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 # 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 - return ( - color_map[GREY_OUT] if self._is_grey and color != color_map[BG] else color - ) + return 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 self._is_grey = val @@ -216,8 +222,9 @@ class DisplayIP: # Define an input device and populate global ssd and display objects. class Display(DisplayIP): - def __init__(self, objssd, nxt, sel, prev=None, incr=None, decr=None, - encoder=False, touch=False): + def __init__( + self, objssd, nxt, sel, prev=None, incr=None, decr=None, encoder=False, touch=False + ): global display, ssd ssd = objssd if touch: @@ -390,9 +397,7 @@ class Screen: self.width = ssd.width self.row = 0 self.col = 0 - if ( - Screen.current_screen is None and Screen.do_gc - ): # Initialising class and task + if Screen.current_screen is None and Screen.do_gc: # Initialising class and task # Here we create singleton tasks asyncio.create_task(self._garbage_collect()) Screen.current_screen = self @@ -510,8 +515,8 @@ class Screen: # Very basic window class. Cuts a rectangular hole in a screen on which # content may be drawn. class Window(Screen): - _value = None + # Allow a Window to store an arbitrary object. Retrieval may be # done by caller, after the Window instance was deleted @classmethod @@ -656,9 +661,7 @@ class Widget: if hasattr(self, "label"): self.label.value(text, invert, fgcolor, bgcolor, bdcolor) else: - raise ValueError( - "Method {}.text does not exist.".format(self.__class__.__name__) - ) + raise ValueError("Method {}.text does not exist.".format(self.__class__.__name__)) # Called from subclass prior to populating framebuf with control def show(self, black=True): @@ -672,9 +675,7 @@ class Widget: dev = display.usegrey(self._greyed_out) x = self.col y = self.row - dev.fill_rect( - x, y, self.width, self.height, color_map[BG] if black else self.bgcolor - ) + dev.fill_rect(x, y, self.width, self.height, color_map[BG] if black else self.bgcolor) return True # Called by Screen.show(). Draw background and bounding box if required. @@ -777,9 +778,7 @@ class LinearIO(Widget): ): self.min_delta = min_delta self.max_delta = max_delta - super().__init__( - writer, row, col, height, width, fgcolor, bgcolor, bdcolor, value, active - ) + super().__init__(writer, row, col, height, width, fgcolor, bgcolor, bdcolor, value, active) self.adjustable = True # Can show adjustable border self.do_precision = prcolor is not False if self.do_precision: @@ -794,9 +793,7 @@ class LinearIO(Widget): # Handle increase and decrease buttons. Redefined by textbox.py, scale_log.py async def btnhan(self, button, up, d): - maxd = ( - self.max_delta if self.precision() else d * 4 - ) # Why move fast in precision mode? + maxd = self.max_delta if self.precision() else d * 4 # Why move fast in precision mode? t = ticks_ms() while button(): await asyncio.sleep_ms(0) # Quit fast on button release