/
nap_button.py
70 lines (57 loc) · 1.91 KB
/
nap_button.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
59
60
61
62
63
64
65
66
67
68
69
70
import time
from pathlib import Path
import RPi.GPIO as GPIO
from pygame import mixer, error as pygame_error
from config.nap_button_config import *
mixer.init()
GPIO.setmode(GPIO.BCM)
GPIO_BUTTON = button_pin
GPIO.setup(GPIO_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
class SleepMachine:
def __init__(self):
"""
Initialize the sleep machine
"""
print("Initializing sleep machine")
self.playing = False
self.button_push_time = 0
self.debounce_time = button_debounce_time
mixer.music.set_volume(volume)
sleep_noise = Path(__file__).parent / 'audio' / f'{audio_file_name}'
try:
mixer.music.load(sleep_noise)
except pygame_error:
raise Exception(f"Could not load audio file: {sleep_noise}")
print(f"Sleep machine initialized with audio file: {sleep_noise}")
def play_pause_sound(self):
"""
Play or pause the sound
:return:
"""
if self.playing:
mixer.music.pause()
self.playing = False
print("Pausing sound")
else:
mixer.music.play(loops=-1)
self.playing = True
print("Playing sound")
def button_callback(self, channel):
"""
Callback function to handle button presses, with debounce
:return:
"""
if time.time() - self.button_push_time > self.debounce_time:
print(f"Button pressed at channel: {channel}")
self.button_push_time = time.time()
self.play_pause_sound()
SleepMachineAccess = SleepMachine()
# Add an event detection to the button
GPIO.add_event_detect(GPIO_BUTTON, GPIO.FALLING, callback=SleepMachineAccess.button_callback)
# Keep the program running and provide a way to exit and cleanup the GPIO
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
print("Exiting.")