42
42
# Register addresses. Unused registers commented out to save memory.
43
43
# pylint: disable=bad-whitespace
44
44
MPR121_I2CADDR_DEFAULT = const (0x5A )
45
- MPR121_TOUCHSTATUS_L = const (0x00 )
46
- #MPR121_TOUCHSTATUS_H = const(0x01)
47
- MPR121_FILTDATA_0L = const (0x04 )
48
- #MPR121_FILTDATA_0H = const(0x05)
49
- MPR121_BASELINE_0 = const (0x1E )
50
- MPR121_MHDR = const (0x2B )
51
- MPR121_NHDR = const (0x2C )
52
- MPR121_NCLR = const (0x2D )
53
- MPR121_FDLR = const (0x2E )
54
- MPR121_MHDF = const (0x2F )
55
- MPR121_NHDF = const (0x30 )
56
- MPR121_NCLF = const (0x31 )
57
- MPR121_FDLF = const (0x32 )
58
- MPR121_NHDT = const (0x33 )
59
- MPR121_NCLT = const (0x34 )
60
- MPR121_FDLT = const (0x35 )
61
- MPR121_TOUCHTH_0 = const (0x41 )
62
- MPR121_RELEASETH_0 = const (0x42 )
63
- MPR121_DEBOUNCE = const (0x5B )
64
- MPR121_CONFIG1 = const (0x5C )
65
- MPR121_CONFIG2 = const (0x5D )
66
- #MPR121_CHARGECURR_0 = const(0x5F)
67
- #MPR121_CHARGETIME_1 = const(0x6C)
68
- MPR121_ECR = const (0x5E )
69
- #MPR121_AUTOCONFIG0 = const(0x7B)
70
- #MPR121_AUTOCONFIG1 = const(0x7C)
71
- #MPR121_UPLIMIT = const(0x7D)
72
- #MPR121_LOWLIMIT = const(0x7E)
73
- #MPR121_TARGETLIMIT = const(0x7F)
74
- #MPR121_GPIODIR = const(0x76)
75
- #MPR121_GPIOEN = const(0x77)
76
- #MPR121_GPIOSET = const(0x78)
77
- #MPR121_GPIOCLR = const(0x79)
78
- #MPR121_GPIOTOGGLE = const(0x7A)
79
- MPR121_SOFTRESET = const (0x80 )
45
+ MPR121_TOUCHSTATUS_L = const (0x00 )
46
+ # MPR121_TOUCHSTATUS_H = const(0x01)
47
+ MPR121_FILTDATA_0L = const (0x04 )
48
+ # MPR121_FILTDATA_0H = const(0x05)
49
+ MPR121_BASELINE_0 = const (0x1E )
50
+ MPR121_MHDR = const (0x2B )
51
+ MPR121_NHDR = const (0x2C )
52
+ MPR121_NCLR = const (0x2D )
53
+ MPR121_FDLR = const (0x2E )
54
+ MPR121_MHDF = const (0x2F )
55
+ MPR121_NHDF = const (0x30 )
56
+ MPR121_NCLF = const (0x31 )
57
+ MPR121_FDLF = const (0x32 )
58
+ MPR121_NHDT = const (0x33 )
59
+ MPR121_NCLT = const (0x34 )
60
+ MPR121_FDLT = const (0x35 )
61
+ MPR121_TOUCHTH_0 = const (0x41 )
62
+ MPR121_RELEASETH_0 = const (0x42 )
63
+ MPR121_DEBOUNCE = const (0x5B )
64
+ MPR121_CONFIG1 = const (0x5C )
65
+ MPR121_CONFIG2 = const (0x5D )
66
+ # MPR121_CHARGECURR_0 = const(0x5F)
67
+ # MPR121_CHARGETIME_1 = const(0x6C)
68
+ MPR121_ECR = const (0x5E )
69
+ # MPR121_AUTOCONFIG0 = const(0x7B)
70
+ # MPR121_AUTOCONFIG1 = const(0x7C)
71
+ # MPR121_UPLIMIT = const(0x7D)
72
+ # MPR121_LOWLIMIT = const(0x7E)
73
+ # MPR121_TARGETLIMIT = const(0x7F)
74
+ # MPR121_GPIODIR = const(0x76)
75
+ # MPR121_GPIOEN = const(0x77)
76
+ # MPR121_GPIOSET = const(0x78)
77
+ # MPR121_GPIOCLR = const(0x79)
78
+ # MPR121_GPIOTOGGLE = const(0x7A)
79
+ MPR121_SOFTRESET = const (0x80 )
80
80
# pylint: enable=bad-whitespace
81
81
82
- class MPR121_Channel ():
82
+
83
+ class MPR121_Channel :
83
84
# pylint: disable=protected-access
84
85
"""Helper class to represent a touch channel on the MPR121. Not meant to
85
86
be used directly."""
87
+
86
88
def __init__ (self , mpr121 , channel ):
87
89
self ._mpr121 = mpr121
88
90
self ._channel = channel
@@ -101,23 +103,25 @@ def raw_value(self):
101
103
def threshold (self ):
102
104
"""The touch threshold."""
103
105
buf = bytearray (1 )
104
- self ._mpr121 ._read_register_bytes (MPR121_TOUCHTH_0 + 2 * self ._channel , buf , 1 )
106
+ self ._mpr121 ._read_register_bytes (MPR121_TOUCHTH_0 + 2 * self ._channel , buf , 1 )
105
107
return buf [0 ]
106
108
107
109
@threshold .setter
108
110
def threshold (self , value ):
109
- self ._mpr121 ._write_register_byte (MPR121_TOUCHTH_0 + 2 * self ._channel , value )
111
+ self ._mpr121 ._write_register_byte (MPR121_TOUCHTH_0 + 2 * self ._channel , value )
110
112
111
113
@property
112
114
def release_threshold (self ):
113
115
"""The release threshold."""
114
116
buf = bytearray (1 )
115
- self ._mpr121 ._read_register_bytes (MPR121_RELEASETH_0 + 2 * self ._channel , buf , 1 )
117
+ self ._mpr121 ._read_register_bytes (
118
+ MPR121_RELEASETH_0 + 2 * self ._channel , buf , 1
119
+ )
116
120
return buf [0 ]
117
121
118
122
@release_threshold .setter
119
123
def release_threshold (self , value ):
120
- self ._mpr121 ._write_register_byte (MPR121_RELEASETH_0 + 2 * self ._channel , value )
124
+ self ._mpr121 ._write_register_byte (MPR121_RELEASETH_0 + 2 * self ._channel , value )
121
125
122
126
123
127
class MPR121 :
@@ -126,12 +130,12 @@ class MPR121:
126
130
def __init__ (self , i2c , address = MPR121_I2CADDR_DEFAULT ):
127
131
self ._i2c = i2c_device .I2CDevice (i2c , address )
128
132
self ._buffer = bytearray (2 )
129
- self ._channels = [None ]* 12
133
+ self ._channels = [None ] * 12
130
134
self .reset ()
131
135
132
136
def __getitem__ (self , key ):
133
137
if key < 0 or key > 11 :
134
- raise IndexError (' Pin must be a value 0-11.' )
138
+ raise IndexError (" Pin must be a value 0-11." )
135
139
if self ._channels [key ] is None :
136
140
self ._channels [key ] = MPR121_Channel (self , key )
137
141
return self ._channels [key ]
@@ -169,17 +173,19 @@ def reset(self):
169
173
"""
170
174
# Write to the reset register.
171
175
self ._write_register_byte (MPR121_SOFTRESET , 0x63 )
172
- time .sleep (0.001 ) # This 1ms delay here probably isn't necessary but can't hurt.
176
+ time .sleep (
177
+ 0.001
178
+ ) # This 1ms delay here probably isn't necessary but can't hurt.
173
179
# Set electrode configuration to default values.
174
180
self ._write_register_byte (MPR121_ECR , 0x00 )
175
181
# Check CDT, SFI, ESI configuration is at default values.
176
182
self ._read_register_bytes (MPR121_CONFIG2 , self ._buffer , 1 )
177
183
if self ._buffer [0 ] != 0x24 :
178
- raise RuntimeError (' Failed to find MPR121 in expected config state!' )
184
+ raise RuntimeError (" Failed to find MPR121 in expected config state!" )
179
185
# Default touch and release thresholds
180
186
for i in range (12 ):
181
- self ._write_register_byte (MPR121_TOUCHTH_0 + 2 * i , 12 )
182
- self ._write_register_byte (MPR121_RELEASETH_0 + 2 * i , 6 )
187
+ self ._write_register_byte (MPR121_TOUCHTH_0 + 2 * i , 12 )
188
+ self ._write_register_byte (MPR121_RELEASETH_0 + 2 * i , 6 )
183
189
# Configure baseline filtering control registers.
184
190
self ._write_register_byte (MPR121_MHDR , 0x01 )
185
191
self ._write_register_byte (MPR121_NHDR , 0x01 )
@@ -194,26 +200,28 @@ def reset(self):
194
200
self ._write_register_byte (MPR121_FDLT , 0x00 )
195
201
# Set other configuration registers.
196
202
self ._write_register_byte (MPR121_DEBOUNCE , 0 )
197
- self ._write_register_byte (MPR121_CONFIG1 , 0x10 ) # default, 16uA charge current
198
- self ._write_register_byte (MPR121_CONFIG2 , 0x20 ) # 0.5uS encoding, 1ms period
203
+ self ._write_register_byte (MPR121_CONFIG1 , 0x10 ) # default, 16uA charge current
204
+ self ._write_register_byte (MPR121_CONFIG2 , 0x20 ) # 0.5uS encoding, 1ms period
199
205
# Enable all electrodes.
200
- self ._write_register_byte (MPR121_ECR , 0x8F ) # start with first 5 bits of baseline tracking
206
+ self ._write_register_byte (
207
+ MPR121_ECR , 0x8F
208
+ ) # start with first 5 bits of baseline tracking
201
209
202
210
def filtered_data (self , pin ):
203
211
"""Return filtered data register value for the provided pin (0-11).
204
212
Useful for debugging.
205
213
"""
206
214
if pin < 0 or pin > 11 :
207
- raise ValueError (' Pin must be a value 0-11.' )
208
- self ._read_register_bytes (MPR121_FILTDATA_0L + pin * 2 , self ._buffer )
215
+ raise ValueError (" Pin must be a value 0-11." )
216
+ self ._read_register_bytes (MPR121_FILTDATA_0L + pin * 2 , self ._buffer )
209
217
return ((self ._buffer [1 ] << 8 ) | (self ._buffer [0 ])) & 0xFFFF
210
218
211
219
def baseline_data (self , pin ):
212
220
"""Return baseline data register value for the provided pin (0-11).
213
221
Useful for debugging.
214
222
"""
215
223
if pin < 0 or pin > 11 :
216
- raise ValueError (' Pin must be a value 0-11.' )
224
+ raise ValueError (" Pin must be a value 0-11." )
217
225
self ._read_register_bytes (MPR121_BASELINE_0 + pin , self ._buffer , 1 )
218
226
return self ._buffer [0 ] << 2
219
227
@@ -229,6 +237,6 @@ def is_touched(self, pin):
229
237
False.
230
238
"""
231
239
if pin < 0 or pin > 11 :
232
- raise ValueError (' Pin must be a value 0-11.' )
240
+ raise ValueError (" Pin must be a value 0-11." )
233
241
touches = self .touched ()
234
242
return (touches & (1 << pin )) > 0
0 commit comments