Skip to content

Instantly share code, notes, and snippets.

@timothylhuillier
Created September 1, 2015 14:36
Show Gist options
  • Save timothylhuillier/e80e592a96c4d5652072 to your computer and use it in GitHub Desktop.
Save timothylhuillier/e80e592a96c4d5652072 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import time
import math
import mraa
# ============================================================================
# JGuyon driver MPR121 for LED
# ============================================================================
class TOUCH :
# Register addresses.
__MPR121_I2CADDR_DEFAULT = 0x5A
__MPR121_TOUCHSTATUS_L = 0x00
__MPR121_TOUCHSTATUS_H = 0x01
__MPR121_FILTDATA_0L = 0x04
__MPR121_FILTDATA_0H = 0x05
__MPR121_BASELINE_0 = 0x1E
__MPR121_MHDR = 0x2B
__MPR121_NHDR = 0x2C
__MPR121_NCLR = 0x2D
__MPR121_FDLR = 0x2E
__MPR121_MHDF = 0x2F
__MPR121_NHDF = 0x30
__MPR121_NCLF = 0x31
__MPR121_FDLF = 0x32
__MPR121_NHDT = 0x33
__MPR121_NCLT = 0x34
__MPR121_FDLT = 0x35
__MPR121_TOUCHTH_0 = 0x41
__MPR121_RELEASETH_0 = 0x42
__MPR121_DEBOUNCE = 0x5B
__MPR121_CONFIG1 = 0x5C
__MPR121_CONFIG2 = 0x5D
__MPR121_CHARGECURR_0 = 0x5F
__MPR121_CHARGETIME_1 = 0x6C
__MPR121_ECR = 0x5E
__MPR121_AUTOCONFIG0 = 0x7B
__MPR121_AUTOCONFIG1 = 0x7C
__MPR121_UPLIMIT = 0x7D
__MPR121_LOWLIMIT = 0x7E
__MPR121_TARGETLIMIT = 0x7F
__MPR121_GPIODIR = 0x76
__MPR121_GPIOEN = 0x77
__MPR121_GPIOSET = 0x78
__MPR121_GPIOCLR = 0x79
__MPR121_GPIOTOGGLE = 0x7A
__MPR121_SOFTRESET = 0x80
@classmethod
def __init__(self, debug=False):
if (debug):
print "Config I2C on MPR121"
self.i2c = mraa.I2c(1)
def set_reg_default(self, address):
self.i2c.address(address)
# Soft reset of device.
self.i2c.writeReg(self.__MPR121_SOFTRESET, 0x63)
time.sleep(0.001) # This 1ms delay here probably isn't necessary but can't hurt.
## Configure baseline filtering control registers. ##
# Section A (Rising) - Controls filtering when data is > baseline.
self.i2c.writeReg(self.__MPR121_MHDR, 0x01) # 0x2B MAX HALF DELTA Rising
self.i2c.writeReg(self.__MPR121_NHDR, 0x01) # 0x2C NOISE HALF DELTA Rising
self.i2c.writeReg(self.__MPR121_NCLR, 0x00) # 0x2D NOISE COUNT LIMIT Rising
self.i2c.writeReg(self.__MPR121_FDLR, 0x00) # 0x2E DELAY LIMIT Rising
# Section B (Falling) - Controls filtering when data is < baseline.
self.i2c.writeReg(self.__MPR121_MHDF, 0x01) # 0x2F MAX HALF DELTA Falling
self.i2c.writeReg(self.__MPR121_NHDF, 0x01) # 0x30 NOISE HALF DELTA Falling
self.i2c.writeReg(self.__MPR121_NCLF, 0xFF) # 0x31 NOISE COUNT LIMIT Falling
self.i2c.writeReg(self.__MPR121_FDLF, 0x02) # 0x32 DELAY LIMIT Falling
# Section C (Touched)
# self.i2c.writeReg(self.__MPR121_NHDT, 0x00) # 0x33 Noise half delta touched
# self.i2c.writeReg(self.__MPR121_NCLT, 0x00) # 0x34 Noise counts touched
# self.i2c.writeReg(self.__MPR121_FDLT, 0x00) # 0x35 Filter delay touched
## Set other configuration registers. ##
self.i2c.writeReg(self.__MPR121_CONFIG2, 0x04) # 0x5D
self.i2c.writeReg(self.__MPR121_AUTOCONFIG0, 0x0B) # 0x7B
self.i2c.writeReg(self.__MPR121_UPLIMIT, 0x9C) # 0x7D
self.i2c.writeReg(self.__MPR121_LOWLIMIT, 0x65) # 0x7E
self.i2c.writeReg(self.__MPR121_TARGETLIMIT, 0x8C) # 0x7F
## Enable all electrodes. ##
self.i2c.writeReg(self.__MPR121_ECR, 0x0C) # 0x5E
## All done, everything succeeded! ##
def set_thresholds(self, address, touch, release):
"""Set the touch and release threshold for all inputs to the provided
values. Both touch and release should be a value between 0 to 255
(inclusive).
"""
self.i2c.address(address)
assert touch >= 0 and touch <= 255, 'touch must be between 0-255 (inclusive)'
assert release >= 0 and release <= 255, 'release must be between 0-255 (inclusive)'
# Set the touch and release register value for all the inputs.
for i in range(12):
self.i2c.writeReg(self.__MPR121_TOUCHTH_0 + 2*i, touch)
self.i2c.writeReg(self.__MPR121_RELEASETH_0 + 2*i, release)
def filtered_data(self, address, pin):
"""Return filtered data register value for the provided pin (0-11).
Useful for debugging.
"""
self.i2c.address(address)
assert pin >= 0 and pin < 12, 'pin must be between 0-11 (inclusive)'
return self.i2c.readWordReg(self.__MPR121_FILTDATA_0L + pin*2)
def baseline_data(self, address, pin):
"""Return baseline data register value for the provided pin (0-11).
Useful for debugging.
"""
self.i2c.address(address)
assert pin >= 0 and pin < 12, 'pin must be between 0-11 (inclusive)'
bl = self.i2c.readReg(self.__MPR121_BASELINE_0 + pin)
return bl << 2
def touched(self, address):
"""Return touch state of all pins as a 12-bit value where each bit
represents a pin, with a value of 1 being touched and 0 not being touched.
"""
self.i2c.address(address)
t = self.i2c.readWordReg(self.__MPR121_TOUCHSTATUS_L)
return t & 0x0FFF
def is_touched(self, address, pin):
"""Return True if the specified pin is being touched, otherwise returns
False.
"""
self.i2c.address(address)
assert pin >= 0 and pin < 12, 'pin must be between 0-11 (inclusive)'
t = self.touched()
return (t & (1 << pin)) > 0
def test(self, address):
self.i2c.address(address)
return address
#!/usr/bin/python
import time
import sys
import math
import mraa
import numpy as np
from lib_PCA9685 import LED
from lib_MPR121 import TOUCH
print "Starting ... "
try:
# Setup
LED_IOThinks = LED()
TOUCH_IOThinks = TOUCH()
TOUCH_IOThinks.set_reg_default(0x5A)
TOUCH_IOThinks.set_reg_default(0x5B)
LED_IOThinks.enableLED(0)
channel_n = 0
channel_n1 = 0
channel_n2 = 0
channel_n3 = 0
channel_n4 = 0
channel_n5 = 0
channel_n6 = 0
channel_n7 = 0
volume_push = 0
touch_data_volume = np.zeros(10)
# Loop
print "Start thread [0K]"
timeout = time.time() + 60*3 # 3 minutes
while (time.time() < timeout):
time.sleep(0.03)
if volume_push:
#LED
channel_n7 = channel_n6
LED_IOThinks.setLED(channel_n7, 0)
channel_n6 = channel_n5
LED_IOThinks.setLED(channel_n6, 1)
channel_n5 = channel_n4
LED_IOThinks.setLED(channel_n5, 5)
channel_n4 = channel_n3
LED_IOThinks.setLED(channel_n4, 10)
channel_n3 = channel_n2
LED_IOThinks.setLED(channel_n3, 15)
channel_n2 = channel_n1
LED_IOThinks.setLED(channel_n2, 20)
channel_n1 = channel_n
LED_IOThinks.setLED(channel_n1, 30)
LED_IOThinks.setLED(channel_n, 40)
else:
for i_led in range(10):
LED_IOThinks.setLED(i_led, 0)
# VOLUME
print "volume touch"
for i_vol in range(10):
touch_data_volume[i_vol] = TOUCH_IOThinks.filtered_data(0x5A, i_vol)
print touch_data_volume
# if data<520:
# volume_push = 1
# else:
# volume_push = 0
touch_data_volume_min = touch_data_volume.min()
# Montant a configurer pour la detection du touch
if touch_data_volume_min<550:
volume_push = 1
for index, item in enumerate(touch_data_volume):
if item == touch_data_volume_min:
li = [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
touch_volume_num_pushed = index
if item == 0:
error = -1
channel_n = touch_volume_num_pushed
else:
volume_push = 0
#BOUTON
for i_bouton in range(6):
data = TOUCH_IOThinks.filtered_data(0x5B, i_bouton)
if data<380:
channel_n = 0
channel_n1 = 0
channel_n2 = 0
channel_n3 = 0
channel_n4 = 0
channel_n5 = 0
channel_n6 = 0
channel_n7 = 0
sens = 1
for i_led in range((i_bouton+1)):
for i_led in range(10):
LED_IOThinks.setLED(i_led, 50)
time.sleep(0.05)
for i_led in range(10):
LED_IOThinks.setLED(i_led, 0)
time.sleep(0.05)
LED_IOThinks.enableLED(1)
except:
print sys.exc_info()
finally:
print "Stopping..."
LED_IOThinks.setLED(0, 0)
LED_IOThinks.setLED(1, 0)
LED_IOThinks.setLED(2, 0)
LED_IOThinks.setLED(3, 0)
LED_IOThinks.setLED(4, 0)
LED_IOThinks.setLED(5, 0)
LED_IOThinks.setLED(6, 0)
LED_IOThinks.setLED(7, 0)
LED_IOThinks.setLED(8, 0)
LED_IOThinks.setLED(9, 0)
LED_IOThinks.enableLED(1)
print "Stop thread [0K]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment