rpi-gpio-switch/main.py
2022-10-03 20:47:06 +02:00

77 lines
1.8 KiB
Python

#!/bin/python3 -u
## -U option log to syslog on systemctl
import RPi.GPIO as GPIO
import pygame
import threading
from datetime import datetime, timedelta
import time
import random
## 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() :
pygame.mixer.music.load(path)
pygame.mixer.music.play(0,0.0)
while pygame.mixer.music.get_busy():
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 :
print("Stop Sound")
pygame.mixer.music.fadeout(5)
def button_callback(channel):
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.start()
def main():
## init pygame
pygame.mixer.init()
GPIO.setwarnings(False) # Ignore warning for now
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.add_event_detect(10,GPIO.RISING,callback=button_callback, bouncetime=10000) # Setup event on pin 10 rising edge 10s wait
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
if __name__ == "__main__":
main()