Added worksheets
[pi-music.git] / glove.py
index b63f64824036e2ee4708c6172b9ec134dd1136e6..22e8cbfb9ec27fb11eab18df89925c69bc06e60c 100644 (file)
--- a/glove.py
+++ b/glove.py
@@ -1,6 +1,5 @@
 import pygame
 import RPi.GPIO as gpio
-import time
 import logging
 
 logger = logging.getLogger(__name__)
@@ -11,6 +10,9 @@ logger.addHandler(loggerFileHandler)
 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',
@@ -19,26 +21,22 @@ guitars = ['sounds/guitar1.wav',
            'sounds/guitar5.wav']
 
 pygame.mixer.init()
-sounds = [pygame.mixer.Sound(g) for g in guitars]
-#for s in sounds:
-#    s.play()
-#    time.sleep(0.5)
 
-#time.sleep(2)
+sounds = {}
+for pin, wav in zip(pins, guitars):
+    sounds[pin] = pygame.mixer.Sound(wav)
 
-gpio.setmode(gpio.BCM)
-gpio.setup(25, gpio.IN)
+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)))
 
-last_switch = gpio.LOW
+for pin in pins:
+    gpio.setup(pin, gpio.IN)
+    gpio.add_event_detect(pin, gpio.BOTH, callback=handle_sound, bouncetime=200)
 
 while True:
-    this_switch = gpio.input(25)
-    logger.debug("This: {0}, Last: {1}".format(this_switch, last_switch))
-    if this_switch and not last_switch:
-        sounds[0].play()
-        last_switch = this_switch
-        logger.info("Started playing")
-    if not this_switch and last_switch:
-        sounds[0].stop()
-        last_switch = this_switch
-        logger.info("Stopped playing")
+    pass