From 791e9ed532314d5059dc4832b007574a606697b4 Mon Sep 17 00:00:00 2001 From: Pi user Date: Tue, 22 Oct 2013 10:50:47 +0100 Subject: [PATCH] Now added the drums.py file... --- drums.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 drums.py diff --git a/drums.py b/drums.py new file mode 100644 index 0000000..9eb7bd2 --- /dev/null +++ b/drums.py @@ -0,0 +1,37 @@ +import pygame +import RPi.GPIO as gpio +import logging + +logger = logging.getLogger(__name__) +loggerFileHandler = logging.FileHandler('drums.log') +loggerFileHandlerFormatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s') +loggerFileHandler.setFormatter(loggerFileHandlerFormatter) +logger.addHandler(loggerFileHandler) +logger.setLevel(logging.WARNING) +logger.setLevel(logging.INFO) + +gpio.setmode(gpio.BCM) +pins = [25, 24, 23, 18] +# pins = [18] + +drums = ['sounds/hihat.wav', + 'sounds/maracas.wav', + 'sounds/slowdrum.wav', + 'sounds/snare.wav'] + +pygame.mixer.init() + +sounds = {} +for pin, wav in zip(pins, drums): + sounds[pin] = pygame.mixer.Sound(wav) + +def handle_sound(pin): + sounds[pin].play() + logger.info("Played {0}".format(pin)) + +for pin in pins: + gpio.setup(pin, gpio.IN) + gpio.add_event_detect(pin, gpio.RISING, callback=handle_sound, bouncetime=100) + +while True: + pass -- 2.34.1