import pygame
import RPi.GPIO as gpio
-import time
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
logger.setLevel(logging.INFO)
+gpio.setmode(gpio.BCM)
+pins = [25, 24, 18, 22]
+# pins = [25]
guitars = ['sounds/guitar1.wav',
'sounds/guitar2.wav',
'sounds/guitar4.wav',
'sounds/guitar5.wav']
-pins = [25, 24, 18, 22]
-
pygame.mixer.init()
-sounds = [pygame.mixer.Sound(g) for g in guitars]
-gpio.setmode(gpio.BCM)
+sounds = {}
+for pin, wav in zip(pins, guitars):
+ sounds[pin] = pygame.mixer.Sound(wav)
+
+def handle_sound(pin):
+ if not gpio.input(pin):
+ sounds[pin].play()
+ logger.info("Started playing {0}".format(pins.index(pin)))
+ else:
+ sounds[pin].stop()
+ logger.info("Stopped playing {0}".format(pins.index(pin)))
+
for pin in pins:
gpio.setup(pin, gpio.IN)
-
-last_states = [gpio.input(p) for p in pins]
-these_states = list(last_states)
+ gpio.add_event_detect(pin, gpio.BOTH, callback=handle_sound, bouncetime=200)
while True:
- these_states = [gpio.input(p) for p in pins]
- logger.debug("These: {0}, Last: {1}".format(these_states, last_states))
- for i in range(len(pins)):
- if these_states[i] and not last_states[i]:
- sounds[i].play()
- last_states[i] = these_states[i]
- logger.info("Started playing {0}".format(i))
- if not these_states[i] and last_states[i]:
- sounds[i].stop()
- last_states[i] = these_states[i]
- logger.info("Stopped playing {0}".format(i))
+ pass