Keyboard working
authorPi user <pi@njae.me.uk>
Mon, 21 Oct 2013 15:01:48 +0000 (16:01 +0100)
committerPi user <pi@njae.me.uk>
Mon, 21 Oct 2013 15:01:48 +0000 (16:01 +0100)
16 files changed:
keyboard.py [new file with mode: 0644]
sounds/cymbal.wav [deleted file]
sounds/keyboard-a.wav [new file with mode: 0644]
sounds/keyboard-asharp.wav [new file with mode: 0644]
sounds/keyboard-b.wav [new file with mode: 0644]
sounds/keyboard-c.wav [new file with mode: 0644]
sounds/keyboard-csharp.wav [new file with mode: 0644]
sounds/keyboard-d.wav [new file with mode: 0644]
sounds/keyboard-dsharp.wav [new file with mode: 0644]
sounds/keyboard-e.wav [new file with mode: 0644]
sounds/keyboard-f.wav [new file with mode: 0644]
sounds/keyboard-fsharp-high.wav [new file with mode: 0644]
sounds/keyboard-fsharp-low.wav [new file with mode: 0644]
sounds/keyboard-g-high.wav [new file with mode: 0644]
sounds/keyboard-g.wav [new file with mode: 0644]
sounds/keyboard-gsharp.wav [new file with mode: 0644]

diff --git a/keyboard.py b/keyboard.py
new file mode 100644 (file)
index 0000000..9b4183c
--- /dev/null
@@ -0,0 +1,63 @@
+import pygame
+import RPi.GPIO as gpio
+import logging
+
+logger = logging.getLogger(__name__)
+loggerFileHandler = logging.FileHandler('keyboard.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)
+if gpio.RPI_REVISION == 1:
+    pins = [22, 21, 17, 4, 25, 24, 23, 18]
+else:
+    pins = [22, 27, 17, 4, 25, 24, 23, 18]
+# pins = [25]
+
+notes = ['sounds/keyboard-fsharp-low.wav',
+         'sounds/keyboard-g.wav',
+         'sounds/keyboard-gsharp.wav',
+         'sounds/keyboard-a.wav',
+         'sounds/keyboard-asharp.wav',
+         'sounds/keyboard-b.wav',
+         'sounds/keyboard-c.wav',
+         'sounds/keyboard-csharp.wav',
+         'sounds/keyboard-d.wav',
+         'sounds/keyboard-dsharp.wav',
+         'sounds/keyboard-e.wav',
+         'sounds/keyboard-f.wav',
+         'sounds/keyboard-fsharp-high.wav']
+
+notes = ['sounds/keyboard-g.wav',
+         'sounds/keyboard-a.wav',
+         'sounds/keyboard-b.wav',
+         'sounds/keyboard-c.wav',
+         'sounds/keyboard-d.wav',
+         'sounds/keyboard-e.wav',
+         'sounds/keyboard-f.wav',
+         'sounds/keyboard-g-high.wav']
+
+
+pygame.mixer.init()
+
+sounds = {}
+for pin, wav in zip(pins, notes):
+    sounds[pin] = pygame.mixer.Sound(wav)
+
+def handle_sound(pin):
+    if gpio.input(pin):
+        sounds[pin].play()
+        logger.info("Started playing {0}".format(pins.index(pin)))
+    else:
+        sounds[pin].stop()
+        logger.info("Stopped playing {0}".format(pins.index(pin)))
+
+for pin in pins:
+    gpio.setup(pin, gpio.IN)
+    gpio.add_event_detect(pin, gpio.BOTH, callback=handle_sound, bouncetime=50)
+
+while True:
+    pass
diff --git a/sounds/cymbal.wav b/sounds/cymbal.wav
deleted file mode 100644 (file)
index 7c2e528..0000000
Binary files a/sounds/cymbal.wav and /dev/null differ
diff --git a/sounds/keyboard-a.wav b/sounds/keyboard-a.wav
new file mode 100644 (file)
index 0000000..4182c93
Binary files /dev/null and b/sounds/keyboard-a.wav differ
diff --git a/sounds/keyboard-asharp.wav b/sounds/keyboard-asharp.wav
new file mode 100644 (file)
index 0000000..7ab11f8
Binary files /dev/null and b/sounds/keyboard-asharp.wav differ
diff --git a/sounds/keyboard-b.wav b/sounds/keyboard-b.wav
new file mode 100644 (file)
index 0000000..edeff46
Binary files /dev/null and b/sounds/keyboard-b.wav differ
diff --git a/sounds/keyboard-c.wav b/sounds/keyboard-c.wav
new file mode 100644 (file)
index 0000000..6f46220
Binary files /dev/null and b/sounds/keyboard-c.wav differ
diff --git a/sounds/keyboard-csharp.wav b/sounds/keyboard-csharp.wav
new file mode 100644 (file)
index 0000000..0ee2587
Binary files /dev/null and b/sounds/keyboard-csharp.wav differ
diff --git a/sounds/keyboard-d.wav b/sounds/keyboard-d.wav
new file mode 100644 (file)
index 0000000..c3f1c28
Binary files /dev/null and b/sounds/keyboard-d.wav differ
diff --git a/sounds/keyboard-dsharp.wav b/sounds/keyboard-dsharp.wav
new file mode 100644 (file)
index 0000000..31878a2
Binary files /dev/null and b/sounds/keyboard-dsharp.wav differ
diff --git a/sounds/keyboard-e.wav b/sounds/keyboard-e.wav
new file mode 100644 (file)
index 0000000..0e5826d
Binary files /dev/null and b/sounds/keyboard-e.wav differ
diff --git a/sounds/keyboard-f.wav b/sounds/keyboard-f.wav
new file mode 100644 (file)
index 0000000..d040aa1
Binary files /dev/null and b/sounds/keyboard-f.wav differ
diff --git a/sounds/keyboard-fsharp-high.wav b/sounds/keyboard-fsharp-high.wav
new file mode 100644 (file)
index 0000000..6fb7b42
Binary files /dev/null and b/sounds/keyboard-fsharp-high.wav differ
diff --git a/sounds/keyboard-fsharp-low.wav b/sounds/keyboard-fsharp-low.wav
new file mode 100644 (file)
index 0000000..951779e
Binary files /dev/null and b/sounds/keyboard-fsharp-low.wav differ
diff --git a/sounds/keyboard-g-high.wav b/sounds/keyboard-g-high.wav
new file mode 100644 (file)
index 0000000..f002fbf
Binary files /dev/null and b/sounds/keyboard-g-high.wav differ
diff --git a/sounds/keyboard-g.wav b/sounds/keyboard-g.wav
new file mode 100644 (file)
index 0000000..4597b7d
Binary files /dev/null and b/sounds/keyboard-g.wav differ
diff --git a/sounds/keyboard-gsharp.wav b/sounds/keyboard-gsharp.wav
new file mode 100644 (file)
index 0000000..4b9e8db
Binary files /dev/null and b/sounds/keyboard-gsharp.wav differ