====== Capteurs de touche tactile ======
Chipset mpr121 super pratique, il faut juste faire très attention au cablage , ne pas croiser les files, ne pas les mettre trop proche etc...
{{:python:touches2osc.py|}}
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,time,liblo,mpr121
# Use mpr121 class for everything else
from smbus import SMBus
BUS_NUMBER = 1
IP='127.0.0.1'
PORT=9001
carte = [0x5a,0x5b,0x5c,0x5d]
osc_path = ['/touche/c/','/touche/b/','/touche/c/','/touche/d/']
try:
mpr121.TOU_THRESH = 0x30
mpr121.REL_THRESH = 0x33
mpr121.setup(carte[0])
mpr121.setup(carte[1])
mpr121.setup(carte[2])
mpr121.setup(carte[3])
target = liblo.Address('127.0.0.1',9001)
except liblo.AddressError, err:
print str(err)
sys.exit()
while True:
for nocart in range(3):
s = ''
touchData = mpr121.readData(carte[nocart])
if touchData & (1 << 12):
s += 'S'
if (touchData & 0xFFF) == 0:
s += 'N'
else:
for i in range(12):
if touchData & (1 << i):
s += str(i)
# print(s)
liblo.send(target, osc_path[nocart], s)
time.sleep(.2) # to less consumption of CPU
et l'interface au mpr121
{{:python:mpr121.py|}}
# Based on Arduino example by Jim Lindblom
# http://bildr.org/2011/05/mpr121_arduino/
import smbus
bus = smbus.SMBus(1)
# MPR121 Register Defines
MHD_R = 0x2B
NHD_R = 0x2C
NCL_R = 0x2D
FDL_R = 0x2E
MHD_F = 0x2F
NHD_F = 0x30
NCL_F = 0x31
FDL_F = 0x32
ELE0_T = 0x41
ELE0_R = 0x42
ELE1_T = 0x43
ELE1_R = 0x44
ELE2_T = 0x45
ELE2_R = 0x46
ELE3_T = 0x47
ELE3_R = 0x48
ELE4_T = 0x49
ELE4_R = 0x4A
ELE5_T = 0x4B
ELE5_R = 0x4C
ELE6_T = 0x4D
ELE6_R = 0x4E
ELE7_T = 0x4F
ELE7_R = 0x50
ELE8_T = 0x51
ELE8_R = 0x52
ELE9_T = 0x53
ELE9_R = 0x54
ELE10_T = 0x55
ELE10_R = 0x56
ELE11_T = 0x57
ELE11_R = 0x58
FIL_CFG = 0x5D
ELE_CFG = 0x5E
GPIO_CTRL0 = 0x73
GPIO_CTRL1 = 0x74
GPIO_DATA = 0x75
GPIO_DIR = 0x76
GPIO_EN = 0x77
GPIO_SET = 0x78
GPIO_CLEAR = 0x79
GPIO_TOGGLE = 0x7A
ATO_CFG0 = 0x7B
ATO_CFGU = 0x7D
ATO_CFGL = 0x7E
ATO_CFGT = 0x7F
# Global Constants
TOU_THRESH = 0x06
REL_THRESH = 0x0A
# Routines
def readData(address):
MSB = bus.read_byte_data(address, 0x00)
LSB = bus.read_byte_data(address, 0x01)
touchData = ((MSB << 8) |LSB)
return touchData;
def setup(address):
bus.write_byte_data(address, ELE_CFG, 0x00)
# Section A - Controls filtering when data is > baseline.
bus.write_byte_data(address, MHD_R, 0x01)
bus.write_byte_data(address, NHD_R, 0x01)
bus.write_byte_data(address, NCL_R, 0x00)
bus.write_byte_data(address, FDL_R, 0x00)
# Section B - Controls filtering when data is < baseline.
bus.write_byte_data(address, MHD_F, 0x01)
bus.write_byte_data(address, NHD_F, 0x01)
bus.write_byte_data(address, NCL_F, 0xFF)
bus.write_byte_data(address, FDL_F, 0x02)
#Section C - Sets touch and release thresholds for each electrode
bus.write_byte_data(address, ELE0_T, TOU_THRESH)
bus.write_byte_data(address, ELE0_R, REL_THRESH)
bus.write_byte_data(address, ELE1_T, TOU_THRESH)
bus.write_byte_data(address, ELE1_R, REL_THRESH)
bus.write_byte_data(address, ELE2_T, TOU_THRESH)
bus.write_byte_data(address, ELE2_R, REL_THRESH)
bus.write_byte_data(address, ELE3_T, TOU_THRESH)
bus.write_byte_data(address, ELE3_R, REL_THRESH)
bus.write_byte_data(address, ELE4_T, TOU_THRESH)
bus.write_byte_data(address, ELE4_R, REL_THRESH)
bus.write_byte_data(address, ELE5_T, TOU_THRESH)
bus.write_byte_data(address, ELE5_R, REL_THRESH)
bus.write_byte_data(address, ELE6_T, TOU_THRESH)
bus.write_byte_data(address, ELE6_R, REL_THRESH)
bus.write_byte_data(address, ELE7_T, TOU_THRESH)
bus.write_byte_data(address, ELE7_R, REL_THRESH)
bus.write_byte_data(address, ELE8_T, TOU_THRESH)
bus.write_byte_data(address, ELE8_R, REL_THRESH)
bus.write_byte_data(address, ELE9_T, TOU_THRESH)
bus.write_byte_data(address, ELE9_R, REL_THRESH)
bus.write_byte_data(address, ELE10_T, TOU_THRESH)
bus.write_byte_data(address, ELE10_R, REL_THRESH)
bus.write_byte_data(address, ELE11_T, TOU_THRESH)
bus.write_byte_data(address, ELE11_R, REL_THRESH)
# Section D
# Set the Filter Configuration
# Set ESI2
bus.write_byte_data(address, FIL_CFG, 0x04)
# Section E
# Electrode Configuration
# Set ELE_CFG to 0x00 to return to standby mode
bus.write_byte_data(address, ELE_CFG, 0x0C) # Enables all 12 Electrodes