X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=glove.py;h=942d83bb0b1b7bfb0e3fd65bed58cbf284f5cddc;hb=6470db67887388e51816186ccd08c5ed232b8417;hp=b63f64824036e2ee4708c6172b9ec134dd1136e6;hpb=bb4de2820c94c1fe96b095028d99875fb4f40908;p=pi-music.git diff --git a/glove.py b/glove.py index b63f648..942d83b 100644 --- a/glove.py +++ b/glove.py @@ -1,16 +1,5 @@ import pygame import RPi.GPIO as gpio -import time -import logging - -logger = logging.getLogger(__name__) -loggerFileHandler = logging.FileHandler('glove.log') -loggerFileHandlerFormatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s') -loggerFileHandler.setFormatter(loggerFileHandlerFormatter) -logger.addHandler(loggerFileHandler) -logger.setLevel(logging.WARNING) -logger.setLevel(logging.INFO) - guitars = ['sounds/guitar1.wav', 'sounds/guitar2.wav', @@ -20,25 +9,19 @@ guitars = ['sounds/guitar1.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) +def handle_sound(pin): + if not gpio.input(pin): + sounds[pins.index(pin)].play() + else: + sounds[pins.index(pin)].stop() gpio.setmode(gpio.BCM) -gpio.setup(25, gpio.IN) +pins = [25, 24, 18, 22] -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