Skip to content
Snippets Groups Projects
Commit 6e1d8c26 authored by SurajSSingh's avatar SurajSSingh
Browse files

Added Simple Alarm

parent afe3483e
No related branches found
No related tags found
No related merge requests found
*.pdf *.pdf
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.10 (Final Project)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (Final Project)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Final Project.iml" filepath="$PROJECT_DIR$/.idea/Final Project.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
# Code adapted from https://raspberrydiy.com/raspberry-pi-alarm-clock/ # # Code adapted from https://raspberrydiy.com/raspberry-pi-alarm-clock/
import vlc # import vlc
from time import sleep # from time import sleep
# # from dataclasses import dataclass, field
#
# # @dataclass
# # class AlarmClock:
#
# # sound: str
#
#
# # Constants
# VLC_INSTANCE = vlc.Instance("--input-repeat=999")
# PLAYER = VLC_INSTANCE.media_player_new()
# SONG = VLC_INSTANCE.media_new("/home/pi/Music/song.mp3")
# VOLUME = 100
#
# PLAYER.set_media(SONG)
# PLAYER.audio_set_volume(VOLUME)
#
# def play_alarm(volume: int = VOLUME):
# PLAYER.set_media(SONG)
# PLAYER.audio_set_volume(volume)
#
# def stop_alarm():
# pass
#
# def snooze_alarm(snooze_time: int = 60):
# pass
#
#
#
from dataclasses import dataclass
from enum import Enum, auto
from datetime import time, datetime
from typing import Optional, Any
from time import sleep
import vlc
# 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)
DEFAULT_VOLUME = 50
class AlarmState(Enum):
SET = auto(),
STARTED = auto(),
PLAYING = auto(),
SNOOZED = auto(),
DEACTIVATED = auto(),
@dataclass
class AlarmClock:
player: Optional
wake_time: time
current_state: AlarmState = AlarmState.DEACTIVATED
def set_alarm(self, wake_time: time) -> time:
print(f"SETTING ALARM to: {wake_time}")
self.current_state = AlarmState.SET
self.wake_time = wake_time
return self.wake_time
def start_alarm(self, check_frequency: int = 1) -> bool:
print(f"STARTING ALARM")
self.current_state = AlarmState.STARTED
now = datetime.now().time()
if now >= self.wake_time:
return False
sleep_time_amount = check_frequency if check_frequency > 1 else 1
while now < self.wake_time:
# Sleep for some time
sleep(sleep_time_amount)
now = datetime.now().time()
self.sound_alarm()
return True
def sound_alarm(self, sound: Optional = None, volume: int = DEFAULT_VOLUME):
print(f"SOUNDING ALARM")
self.current_state = AlarmState.PLAYING
if sound is not None:
self.player.set_media(sound)
self.player.audio_set_volume(volume)
self.player.play()
def snooze_alarm(self, snooze_time: int = 60):
print(f"SNOOZING ALARM")
self.current_state = AlarmState.SNOOZED
self.player.pause()
# Maybe make this async later
sleep(snooze_time)
self.player.play()
if self.current_state is AlarmState.SNOOZED:
self.sound_alarm()
def stop_alarm(self, deactivate: bool = True):
print(f"STOPPING ALARM")
if self.current_state is AlarmState.PLAYING:
self.player.stop()
self.current_state = AlarmState.DEACTIVATED if deactivate else AlarmState.SET
def simple_alarm_mode():
alarm_clock = AlarmClock(math, ALARM_TIME)
ALARM_TIME = time(7, 0, 0)
if __name__ == '__main__':
simple_alarm_mode()
\ No newline at end of file
class VlcTestInstance:
pass
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment