Skip to content

Commit 6a94293

Browse files
authored
Merge pull request #53 from makermelissa/main
Fix Setbackground for CP7 and add PinAlarm and TouchAlarm support
2 parents 9ff9521 + 6d99d98 commit 6a94293

File tree

2 files changed

+60
-32
lines changed

2 files changed

+60
-32
lines changed

adafruit_portalbase/__init__.py

100644100755
Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -312,28 +312,68 @@ def set_text_color(self, color, index=0):
312312
if self._text[index]["label"] is not None:
313313
self._text[index]["label"].color = color
314314

315-
def exit_and_deep_sleep(self, sleep_time):
315+
def create_time_alarm(self, sleep_time):
316316
"""
317-
Stops the current program and enters deep sleep. The program is restarted from the beginning
318-
after a certain period of time.
319-
320-
See https://circuitpython.readthedocs.io/en/latest/shared-bindings/alarm/index.html for more
321-
details.
317+
Create a TimeAlarm based on the specified amount of delay
322318
323319
:param float sleep_time: The amount of time to sleep in seconds
324320
325321
"""
326322
if self._alarm:
327-
pause = self._alarm.time.TimeAlarm(
323+
return self._alarm.time.TimeAlarm(
328324
monotonic_time=time.monotonic() + sleep_time
329325
)
330-
self._alarm.exit_and_deep_sleep_until_alarms(pause)
331-
else:
332-
raise NotImplementedError(
333-
"Deep sleep not supported. Make sure you have the latest CircuitPython."
334-
)
326+
raise NotImplementedError(
327+
"Alarms not supported. Make sure you have the latest CircuitPython."
328+
)
329+
330+
def create_pin_alarm(self, pin, value, edge=False, pull=False):
331+
"""
332+
Create a PinAlarm that is triggered when the pin has a specific value
333+
334+
:param microcontroller.Pin pin: The trigger pin.
335+
:param bool value: The value on which to trigger.
336+
:param bool edge: Trigger only when there is a transition.
337+
:param bool pull: Enable a pull-up or pull-down for the ``pin``.
335338
336-
def enter_light_sleep(self, sleep_time):
339+
"""
340+
if self._alarm:
341+
return self._alarm.time.PinAlarm(pin, value, edge, pull)
342+
raise NotImplementedError(
343+
"Alarms not supported. Make sure you have the latest CircuitPython."
344+
)
345+
346+
def create_touch_alarm(self, pin):
347+
"""
348+
Create a TouchAlarm that is triggered when the pin is touched.
349+
350+
:param microcontroller.Pin pin: The trigger pin.
351+
"""
352+
if self._alarm:
353+
return self._alarm.time.TouchAlarm(pin)
354+
raise NotImplementedError(
355+
"Alarms not supported. Make sure you have the latest CircuitPython."
356+
)
357+
358+
def exit_and_deep_sleep(self, alarms):
359+
"""
360+
Stops the current program and enters deep sleep. The program is restarted from the beginning
361+
after the alarm or alarms are triggered.
362+
363+
See https://circuitpython.readthedocs.io/en/latest/shared-bindings/alarm/index.html for more
364+
details.
365+
366+
:param float alarms: The alarm or alarms to use as a trigger
367+
368+
"""
369+
370+
# For backwards compatibility
371+
if isinstance(alarms, (float, int)):
372+
alarms = self.create_time_alarm(alarms)
373+
374+
self._alarm.exit_and_deep_sleep_until_alarms(alarms)
375+
376+
def enter_light_sleep(self, alarms):
337377
"""
338378
Enter light sleep and resume the program after a certain period of time.
339379
@@ -343,15 +383,11 @@ def enter_light_sleep(self, sleep_time):
343383
:param float sleep_time: The amount of time to sleep in seconds
344384
345385
"""
346-
if self._alarm:
347-
pause = self._alarm.time.TimeAlarm(
348-
monotonic_time=time.monotonic() + sleep_time
349-
)
350-
self._alarm.light_sleep_until_alarms(pause)
351-
else:
352-
raise NotImplementedError(
353-
"Hardware light sleep not supported. Make sure you have the latest CircuitPython."
354-
)
386+
# For backwards compatibility
387+
if isinstance(alarms, (float, int)):
388+
alarms = self.create_time_alarm(alarms)
389+
390+
self._alarm.light_sleep_until_alarms(alarms)
355391

356392
def _fetch_set_text(self, val, index=0):
357393
self.set_text(val, index=index)

adafruit_portalbase/graphics.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def __init__(self, display, *, default_bg=0x000000, scale=1, debug=False):
5353
if self._debug:
5454
print("Init background")
5555
self._bg_group = displayio.Group()
56-
self._bg_file = None
5756
self.splash.append(self._bg_group)
5857

5958
# set the default background
@@ -78,18 +77,11 @@ def set_background(self, file_or_color, position=None):
7877

7978
if not file_or_color:
8079
return # we're done, no background desired
81-
if self._bg_file:
82-
self._bg_file.close()
8380
if isinstance(file_or_color, str): # its a filenme:
84-
with open(file_or_color, "rb") as self._bg_file:
85-
background = displayio.OnDiskBitmap(self._bg_file)
81+
background = displayio.OnDiskBitmap(file_or_color)
8682
self._bg_sprite = displayio.TileGrid(
8783
background,
88-
pixel_shader=getattr(
89-
background, "pixel_shader", displayio.ColorConverter()
90-
),
91-
# TODO: Once CP6 is no longer supported, replace the above line with below
92-
# pixel_shader=background.pixel_shader,
84+
pixel_shader=background.pixel_shader,
9385
x=position[0],
9486
y=position[1],
9587
)

0 commit comments

Comments
 (0)