Update: main.py

This commit is contained in:
Siroco 2022-10-03 20:47:06 +02:00
parent d9a266aac1
commit 9847063a68
No known key found for this signature in database
GPG Key ID: 1324098302A514B0

59
main.py
View File

@ -1,33 +1,76 @@
#!/bin/python3 #!/bin/python3 -u
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library ## -U option log to syslog on systemctl
import RPi.GPIO as GPIO
import pygame import pygame
import threading import threading
from datetime import datetime, timedelta
import time import time
import random
def play_sound(): ## Define sounds path
intro = "/home/museoa/INTRO_CHIMES.mp3"
night = "/home/museoa/MEP_NOCHE.mp3"
# Define mute hours
# From 9.00 to 21.00
open_hour = 9
close_hour = 21
def _play(path):
if not pygame.mixer.music.get_busy() : if not pygame.mixer.music.get_busy() :
print("Start Sound") pygame.mixer.music.load(path)
pygame.mixer.music.play() pygame.mixer.music.play(0,0.0)
while pygame.mixer.music.get_busy(): while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10) pygame.time.Clock().tick(10)
print("Ended Sound");
pygame.mixer.music.stop()
def play_sound():
print("play now..")
sounds = [night]
if not pygame.mixer.music.get_busy() :
print("Start Sound")
_play(intro)
_play(random.choice(sounds))
else : else :
print("Stop Sound") print("Stop Sound")
pygame.mixer.music.stop() pygame.mixer.music.fadeout(5)
def button_callback(channel): def button_callback(channel):
print("Button was pushed!") print("Button was pushed!")
# from 9.00 to 21.00
now = datetime.now()
if (now.hour < close_hour and now.hour >= open_hour):
x = threading.Thread(target=play_sound) x = threading.Thread(target=play_sound)
x.start() x.start()
def main():
## init pygame ## init pygame
pygame.mixer.init() pygame.mixer.init()
pygame.mixer.music.load('./audio.mp3')
GPIO.setwarnings(False) # Ignore warning for now GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off) GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback, bouncetime=10000) # Setup event on pin 10 rising edge 10s wait GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback, bouncetime=10000) # Setup event on pin 10 rising edge 10s wait
message = input("Press enter to quit\n\n") # Run until someone presses enter
while (True):
## Play schedule
now = datetime.now()
## 20:00 AM
if (now.hour == 20 and now.minute == 0):
_play(intro)
_play(night)
time.sleep(60)
GPIO.cleanup() # Clean up GPIO.cleanup() # Clean up
if __name__ == "__main__":
main()