User input whilst pygame music is playing

Viewed 21

I recently picked up a Raspberry PI and have a few hours free so I decided to create an alarm clock for fun. I've written some boiler plate code just to mock up the basic flow but I am struggling with how to simulataneously play the alarm tone but also listen out for a user input(snooze button).

import RPi.GPIO as GPIO
import time
import datetime
import pygame


class Alarm:
    def __init__(self, dayOfWeek, timeOfDay):
               self.dayOfWeek = dayOfWeek
               self.timeOfDay = timeOfDay
    def CheckAlarmState(self):
               for day in self.dayOfWeek:
                   if(day == datetime.datetime.today().weekday()):
                       pygame.mixer.music.play()
                       GPIO.output(18, GPIO.HIGH)
                       time.sleep(1)
                       GPIO.output(18, GPIO.LOW)


pygame.mixer.init()
pygame.mixer.music.load("myFile.wav")
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT)
dayOfWeek = [0,1,2,3,4,5]
alarm = Alarm(dayOfWeek, 5)
alarm.CheckAlarmState()
0 Answers
Related