Something works... not sure if it's useful
authorPi user <pi@njae.me.uk>
Sun, 20 Oct 2013 16:56:13 +0000 (17:56 +0100)
committerPi user <pi@njae.me.uk>
Sun, 20 Oct 2013 16:56:13 +0000 (17:56 +0100)
glove.py

index 03a56bc473e321bd3b759bfe6c4e985a3591cccb..ec8dd5d7f45743f4fadf3bb78d85cf8af5b75aae 100644 (file)
--- a/glove.py
+++ b/glove.py
@@ -19,6 +19,7 @@ guitars = ['sounds/guitar1.wav',
            'sounds/guitar5.wav']
 
 pins = [25, 24, 18, 22]
+pins = [25]
 
 pygame.mixer.init()
 sounds = [pygame.mixer.Sound(g) for g in guitars]
@@ -30,15 +31,32 @@ for pin in pins:
 last_states = [gpio.input(p) for p in pins]
 these_states = list(last_states)
 
+
+def handle_sound(pin):
+    if gpio.input(pin):
+        sounds[pins.index(pin)].play()
+        logger.info("Started playing {0}".format(pins.index(pin)))
+    else:
+        sounds[pins.index(pin)].stop()
+        logger.info("Stopped playing {0}".format(pins.index(pin)))
+
+
+for pin in pins:
+    gpio.add_event_detect(pin, gpio.BOTH, callback=handle_sound, bouncetime=200)
+#    gpio.add_event_detect(pin, gpio.FALLING, callback=stop_sound, bouncetime=100)
+
 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
+
+#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))