From: Pi user Date: Mon, 21 Oct 2013 13:00:11 +0000 (+0100) Subject: Now using a dict to store the sounds X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=efd304bea26222ef10075c1040b86b19fde23737;p=pi-music.git Now using a dict to store the sounds --- diff --git a/glove.py b/glove.py index 5208c84..22e8cbf 100644 --- 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,20 +21,19 @@ guitars = ['sounds/guitar1.wav', 'sounds/guitar5.wav'] pygame.mixer.init() -sounds = [pygame.mixer.Sound(g) for g in guitars] + +sounds = {} +for pin, wav in zip(pins, guitars): + sounds[pin] = pygame.mixer.Sound(wav) def handle_sound(pin): if not gpio.input(pin): - sounds[pins.index(pin)].play() + sounds[pin].play() logger.info("Started playing {0}".format(pins.index(pin))) else: - sounds[pins.index(pin)].stop() + sounds[pin].stop() logger.info("Stopped playing {0}".format(pins.index(pin))) -gpio.setmode(gpio.BCM) -pins = [25, 24, 18, 22] -# pins = [25] - for pin in pins: gpio.setup(pin, gpio.IN) gpio.add_event_detect(pin, gpio.BOTH, callback=handle_sound, bouncetime=200)