Newer
Older
from datetime import time, datetime
from time import sleep
# Simple Alarm Clock has 5 functionalities:
# 1. Set Alarm Time - Set the time for the alarm to sound, does NOT make the alarm active
# 2. Start Alarm - Make the alarm active, which will run the timer until it reaches the specified time
# 3. Sound Alarm - Play the sound for the alarm
# 4. Snooze Alarm - Stop the alarm sound and wait for some time to play the alarm again
# 5. Stop Alarm - If the alarm is active or is playing, stop the alarm (deactivate state)
SNOOZE_KEY = keyboard.Key.up
ALARM_OFF_KEY = keyboard.Key.esc
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class SimpleAlarmClock(BaseAlarmClock):
def set_alarm(self, wake_time: time) -> time:
print(f"SETTING ALARM to: {wake_time}")
return super(SimpleAlarmClock, self).set_alarm(wake_time)
def start_alarm(self) -> None:
print("STARTING ALARM")
super(SimpleAlarmClock, self).start_alarm()
def sound_alarm(self) -> None:
print("SOUNDING ALARM")
super(SimpleAlarmClock, self).sound_alarm()
def snooze_alarm(self) -> None:
print("SNOOZING ALARM")
super(SimpleAlarmClock, self).snooze_alarm()
def stop_alarm(self, deactivate: bool = True) -> None:
print("STOPPING ALARM")
if self._current_state is AlarmState.PLAYING:
print("STOPPED PLAYING ALARM")
super(SimpleAlarmClock, self).stop_alarm()
def simple_alarm_mode(self):
self.start_alarm()
while not self.alarm_check_reached(datetime.now().time()):
print(f"ALARM SLEEPING @: {datetime.now().time()}")
sleep(1)
self.sound_alarm()
while self.current_state is not AlarmState.DEACTIVATED:
with keyboard.Events() as events:
for event in events:
if event.key == ALARM_OFF_KEY:
self.stop_alarm()
break
elif event.key == SNOOZE_KEY:
self.snooze_alarm()
sleep(SNOOZE_SEC)
self.sound_alarm()
break
alarm_clock = SimpleAlarmClock(ALARM_TIME)
alarm_clock.simple_alarm_mode()