Merged in use of dict
authorPi user <pi@njae.me.uk>
Mon, 21 Oct 2013 13:02:35 +0000 (14:02 +0100)
committerPi user <pi@njae.me.uk>
Mon, 21 Oct 2013 13:02:35 +0000 (14:02 +0100)
1  2 
glove.py

diff --cc glove.py
index 942d83bb0b1b7bfb0e3fd65bed58cbf284f5cddc,22e8cbfb9ec27fb11eab18df89925c69bc06e60c..b2f2075aedd0221278856edaad8e1856933b97d1
+++ b/glove.py
@@@ -1,23 -1,38 +1,25 @@@
  import pygame
  import RPi.GPIO as gpio
 -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)
  
 -# pins = [25]
+ gpio.setmode(gpio.BCM)
+ pins = [25, 24, 18, 22]
  guitars = ['sounds/guitar1.wav',
             'sounds/guitar2.wav',
             'sounds/guitar3.wav',
--           'sounds/guitar4.wav',
--           'sounds/guitar5.wav']
++           'sounds/guitar4.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()
- gpio.setmode(gpio.BCM)
- pins = [25, 24, 18, 22]
+         sounds[pin].stop()
 -        logger.info("Stopped playing {0}".format(pins.index(pin)))
  
  for pin in pins:
      gpio.setup(pin, gpio.IN)