The Alarm State

__init__

The alarm state starts off simply: the constructor initializes the alarm repeat time (how long between instances of playing the alarm sound file).

Download: file
def __init__(self):
    super().__init__()
    self.sound_alarm_time = None

tick

The tick method does two things.

  1. It checks whether the snooze button is pressed. If so, it captures the time and transitions back to the time state. Recall that the time state uses snooze_time to decide when you reenter the alarm state.
  2. It checks to see if it's time to play the alarm sound. If so, it plays it.
Download: file
    def tick(self, now):
        global snooze_time

        # is the snooze button pushed
        if not snooze_button.value:
            snooze_time = now
            change_to_state('time')
            return

        # is it time to sound the alarm?
        if self.sound_alarm_time and (now - self.sound_alarm_time) > alarm_interval:
            self.sound_alarm_time = now
            pyportal.play_file(alarm_file)

touch

If there's a touch anywhere on the screen cancel any snooze that might be active and go back the the time state.

Download: file
    def touch(self, t, touched):
        global snooze_time
        if t and not touched:
            snooze_time = None
            change_to_state('time')
        return bool(t)

enter

This sets the alarm time to start the process of playing the alarm sound periodically. It then sets the backlight to full brightness, sets the background to the alarm image, and updates the display.

Download: file
    def enter(self):
        global low_light
        self.sound_alarm_time = time.monotonic()
        pyportal.set_backlight(1.00)
        pyportal.set_background(alarm_background)
        low_light = False
        board.DISPLAY.refresh_soon()
        board.DISPLAY.wait_for_frame()

exit

Exit calls the base class's implementation and sets whether the alarm is armed based on whether snooze is active.

Download: file
    def exit(self):
        global alarm_armed
        super().exit()
        alarm_armed = bool(snooze_time)
This guide was first published on Mar 27, 2019. It was last updated on Mar 27, 2019. This page (The Alarm State) was last updated on Oct 21, 2019.