#DDH, based on code from Adafruit industries, mrGPL
import os,glob
import pygame
DRUM_FOLDER = "KidsDay/drums2"
BANK = os.path.join(os.path.dirname(__file__), DRUM_FOLDER)
pygame.mixer.init(44100, -16, 1, 512)
pygame.mixer.set_num_channels(16)
files = glob.glob(os.path.join(BANK, "*.wav"))
files.sort()
samples = [pygame.mixer.Sound(f) for f in files]
import sys
import time
import Adafruit_MPR121.MPR121 as MPR121
print('Adafruit MPR121 Capacitive Touch Sensor Test')
# Create MPR121 instance.
cap = MPR121.MPR121()
if not cap.begin():
print('Error initializing MPR121. Check your wiring!')
sys.exit(1)
def handle_hit(sensor_id):
# event.channel is a zero based channel index for each pad
# event.pad is the pad number from 1 to 8
samples[sensor_id].play(loops=0)
print("You hit pad {}, playing: {}".format(sensor_id,files[sensor_id]))
# Alternatively, specify a custom I2C address such as 0x5B (ADDR tied to 3.3V),
# 0x5C (ADDR tied to SDA), or 0x5D (ADDR tied to SCL).
#cap.begin(address=0x5B)
# Also you can specify an optional I2C bus with the bus keyword parameter.
#cap.begin(busnum=1)
# Main loop to print a message every time a pin is touched.
print('Press Ctrl-C to quit.')
last_touched = cap.touched()
while True:
current_touched = cap.touched()
# Check each pin's last and current state to see if it was pressed or released.
for i in range(12):
# Each pin is represented by a bit in the touched value. A value of 1
# means the pin is being touched, and 0 means it is not being touched.
pin_bit = 1 << i
# First check if transitioned from not touched to touched.
if current_touched & pin_bit and not last_touched & pin_bit:
print('{0} touched!'.format(i))
handle_hit(i)
# Next check if transitioned from touched to not touched.
if not current_touched & pin_bit and last_touched & pin_bit:
print('{0} released!'.format(i))
# Update last state and wait a short period before repeating.
last_touched = current_touched
time.sleep(0.1)