Skip to content

Instantly share code, notes, and snippets.

@mcoms
Created May 28, 2015 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcoms/a8f0651de12783e67246 to your computer and use it in GitHub Desktop.
Save mcoms/a8f0651de12783e67246 to your computer and use it in GitHub Desktop.
MPR121 Fruit Piano (Pi-only version)
#!/bin/bash
eval $(dbus-launch --auto-syntax)
/opt/sonic-pi/app/server/bin/sonic-pi-server.rb
#!/bin/bash
nc -vv -ul 4558 > /dev/null
# Based on the MPR121 example...
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import sys
import time
import OSC
import Adafruit_MPR121.MPR121 as MPR121
print '2E0DVX MPR121 Capacitive Touch Sensor Piano'
# Create MPR121 instance.
cap = MPR121.MPR121()
osc = OSC.OSCClient()
# Initialize communication with MPR121 using default I2C bus of device, and
# default I2C address (0x5A). On BeagleBone Black will default to I2C bus 0.
if not cap.begin():
print 'Error initializing MPR121. Check your wiring!'
sys.exit(1)
# 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)
print 'Press Ctrl-C to quit.'
last_touched = cap.touched()
osc.connect(('127.0.0.1', 4557))
c_chord = OSC.OSCMessage()
c_chord.setAddress("/run-code")
c_chord.append('play chord(:C4, :major)')
d_chord = OSC.OSCMessage()
d_chord.setAddress("/run-code")
d_chord.append('play chord(:D4, :major)')
e_chord = OSC.OSCMessage()
e_chord.setAddress("/run-code")
e_chord.append('play chord(:E4, :major)')
f_chord = OSC.OSCMessage()
f_chord.setAddress("/run-code")
f_chord.append('play chord(:F4, :major)')
g_chord = OSC.OSCMessage()
g_chord.setAddress("/run-code")
g_chord.append('play chord(:G4, :major)')
a_chord = OSC.OSCMessage()
a_chord.setAddress("/run-code")
a_chord.append('play chord(:A4, :major)')
b_chord = OSC.OSCMessage()
b_chord.setAddress("/run-code")
b_chord.append('play chord(:B4, :major)')
c2_chord = OSC.OSCMessage()
c2_chord.setAddress("/run-code")
c2_chord.append('play chord(:C5, :major)')
d2_chord = OSC.OSCMessage()
d2_chord.setAddress("/run-code")
d2_chord.append('play chord(:D5, :major)')
e2_chord = OSC.OSCMessage()
e2_chord.setAddress("/run-code")
e2_chord.append('play chord(:E5, :major)')
f2_chord = OSC.OSCMessage()
f2_chord.setAddress("/run-code")
f2_chord.append('play chord(:F5, :major)')
g2_chord = OSC.OSCMessage()
g2_chord.setAddress("/run-code")
g2_chord.append('play chord(:G5, :major)')
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
if current_touched & pin_bit and not last_touched & pin_bit:
print '{0} touched!'.format(i)
if i == 0:
osc.send(c_chord)
elif i == 1:
osc.send(d_chord)
elif i == 2:
osc.send(e_chord)
elif i == 3:
osc.send(f_chord)
elif i == 4:
osc.send(g_chord)
elif i == 5:
osc.send(a_chord)
elif i == 6:
osc.send(b_chord)
elif i == 7:
osc.send(c2_chord)
elif i == 8:
osc.send(d2_chord)
elif i == 9:
osc.send(e2_chord)
elif i == 10:
osc.send(f2_chord)
elif i == 11:
osc.send(g2_chord)
last_touched = current_touched
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment