diff --git a/adafruit_portalbase/__init__.py b/adafruit_portalbase/__init__.py old mode 100644 new mode 100755 index d2b038c..510784d --- a/adafruit_portalbase/__init__.py +++ b/adafruit_portalbase/__init__.py @@ -312,28 +312,68 @@ def set_text_color(self, color, index=0): if self._text[index]["label"] is not None: self._text[index]["label"].color = color - def exit_and_deep_sleep(self, sleep_time): + def create_time_alarm(self, sleep_time): """ - Stops the current program and enters deep sleep. The program is restarted from the beginning - after a certain period of time. - - See https://circuitpython.readthedocs.io/en/latest/shared-bindings/alarm/index.html for more - details. + Create a TimeAlarm based on the specified amount of delay :param float sleep_time: The amount of time to sleep in seconds """ if self._alarm: - pause = self._alarm.time.TimeAlarm( + return self._alarm.time.TimeAlarm( monotonic_time=time.monotonic() + sleep_time ) - self._alarm.exit_and_deep_sleep_until_alarms(pause) - else: - raise NotImplementedError( - "Deep sleep not supported. Make sure you have the latest CircuitPython." - ) + raise NotImplementedError( + "Alarms not supported. Make sure you have the latest CircuitPython." + ) + + def create_pin_alarm(self, pin, value, edge=False, pull=False): + """ + Create a PinAlarm that is triggered when the pin has a specific value + + :param microcontroller.Pin pin: The trigger pin. + :param bool value: The value on which to trigger. + :param bool edge: Trigger only when there is a transition. + :param bool pull: Enable a pull-up or pull-down for the ``pin``. - def enter_light_sleep(self, sleep_time): + """ + if self._alarm: + return self._alarm.time.PinAlarm(pin, value, edge, pull) + raise NotImplementedError( + "Alarms not supported. Make sure you have the latest CircuitPython." + ) + + def create_touch_alarm(self, pin): + """ + Create a TouchAlarm that is triggered when the pin is touched. + + :param microcontroller.Pin pin: The trigger pin. + """ + if self._alarm: + return self._alarm.time.TouchAlarm(pin) + raise NotImplementedError( + "Alarms not supported. Make sure you have the latest CircuitPython." + ) + + def exit_and_deep_sleep(self, alarms): + """ + Stops the current program and enters deep sleep. The program is restarted from the beginning + after the alarm or alarms are triggered. + + See https://circuitpython.readthedocs.io/en/latest/shared-bindings/alarm/index.html for more + details. + + :param float alarms: The alarm or alarms to use as a trigger + + """ + + # For backwards compatibility + if isinstance(alarms, (float, int)): + alarms = self.create_time_alarm(alarms) + + self._alarm.exit_and_deep_sleep_until_alarms(alarms) + + def enter_light_sleep(self, alarms): """ Enter light sleep and resume the program after a certain period of time. @@ -343,15 +383,11 @@ def enter_light_sleep(self, sleep_time): :param float sleep_time: The amount of time to sleep in seconds """ - if self._alarm: - pause = self._alarm.time.TimeAlarm( - monotonic_time=time.monotonic() + sleep_time - ) - self._alarm.light_sleep_until_alarms(pause) - else: - raise NotImplementedError( - "Hardware light sleep not supported. Make sure you have the latest CircuitPython." - ) + # For backwards compatibility + if isinstance(alarms, (float, int)): + alarms = self.create_time_alarm(alarms) + + self._alarm.light_sleep_until_alarms(alarms) def _fetch_set_text(self, val, index=0): self.set_text(val, index=index) diff --git a/adafruit_portalbase/graphics.py b/adafruit_portalbase/graphics.py index 46040e9..867ffad 100644 --- a/adafruit_portalbase/graphics.py +++ b/adafruit_portalbase/graphics.py @@ -53,7 +53,6 @@ def __init__(self, display, *, default_bg=0x000000, scale=1, debug=False): if self._debug: print("Init background") self._bg_group = displayio.Group() - self._bg_file = None self.splash.append(self._bg_group) # set the default background @@ -78,18 +77,11 @@ def set_background(self, file_or_color, position=None): if not file_or_color: return # we're done, no background desired - if self._bg_file: - self._bg_file.close() if isinstance(file_or_color, str): # its a filenme: - with open(file_or_color, "rb") as self._bg_file: - background = displayio.OnDiskBitmap(self._bg_file) + background = displayio.OnDiskBitmap(file_or_color) self._bg_sprite = displayio.TileGrid( background, - pixel_shader=getattr( - background, "pixel_shader", displayio.ColorConverter() - ), - # TODO: Once CP6 is no longer supported, replace the above line with below - # pixel_shader=background.pixel_shader, + pixel_shader=background.pixel_shader, x=position[0], y=position[1], )