Skip to content

Commit

Permalink
Merge pull request #53 from makermelissa/main
Browse files Browse the repository at this point in the history
Fix Setbackground for CP7 and add PinAlarm and TouchAlarm support
  • Loading branch information
dhalbert authored Dec 1, 2021
2 parents 9ff9521 + 6d99d98 commit 6a94293
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 32 deletions.
80 changes: 58 additions & 22 deletions adafruit_portalbase/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
12 changes: 2 additions & 10 deletions adafruit_portalbase/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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],
)
Expand Down

0 comments on commit 6a94293

Please sign in to comment.