Skip to content

Commit c9e410d

Browse files
authored
Merge pull request #14 from caternuson/iss12
Add property style access to thresholds
2 parents 29716cb + c72c36f commit c9e410d

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

adafruit_mpr121.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
# pylint: enable=bad-whitespace
8181

8282
class MPR121_Channel():
83+
# pylint: disable=protected-access
8384
"""Helper class to represent a touch channel on the MPR121. Not meant to
8485
be used directly."""
8586
def __init__(self, mpr121, channel):
@@ -96,6 +97,29 @@ def raw_value(self):
9697
"""The raw touch measurement."""
9798
return self._mpr121.filtered_data(self._channel)
9899

100+
@property
101+
def threshold(self):
102+
"""The touch threshold."""
103+
buf = bytearray(1)
104+
self._mpr121._read_register_bytes(MPR121_TOUCHTH_0 + 2*self._channel, buf, 1)
105+
return buf[0]
106+
107+
@threshold.setter
108+
def threshold(self, value):
109+
self._mpr121._write_register_byte(MPR121_TOUCHTH_0 + 2*self._channel, value)
110+
111+
@property
112+
def release_threshold(self):
113+
"""The release threshold."""
114+
buf = bytearray(1)
115+
self._mpr121._read_register_bytes(MPR121_RELEASETH_0 + 2*self._channel, buf, 1)
116+
return buf[0]
117+
118+
@release_threshold.setter
119+
def release_threshold(self, value):
120+
self._mpr121._write_register_byte(MPR121_RELEASETH_0 + 2*self._channel, value)
121+
122+
99123
class MPR121:
100124
"""Driver for the MPR121 capacitive touch breakout board."""
101125

@@ -153,7 +177,10 @@ def reset(self):
153177
self._read_register_bytes(MPR121_CONFIG2, self._buffer, 1)
154178
if self._buffer[0] != 0x24:
155179
raise RuntimeError('Failed to find MPR121 in expected config state!')
156-
self.set_thresholds(12, 6)
180+
# Default touch and release thresholds
181+
for i in range(12):
182+
self._write_register_byte(MPR121_TOUCHTH_0 + 2*i, 12)
183+
self._write_register_byte(MPR121_RELEASETH_0 + 2*i, 6)
157184
# Configure baseline filtering control registers.
158185
self._write_register_byte(MPR121_MHDR, 0x01)
159186
self._write_register_byte(MPR121_NHDR, 0x01)
@@ -173,18 +200,6 @@ def reset(self):
173200
# Enable all electrodes.
174201
self._write_register_byte(MPR121_ECR, 0x8F) # start with first 5 bits of baseline tracking
175202

176-
def set_thresholds(self, touch, release):
177-
"""Set the touch and release threshold for all inputs to the provided
178-
values. Both touch and release should be a value between 0 to 255
179-
(inclusive).
180-
"""
181-
if touch < 0 or touch > 255 or release < 0 or release > 255:
182-
raise ValueError('Touch/release must be a byte value 0-255.')
183-
# Set the touch and release register value for all the inputs.
184-
for i in range(12):
185-
self._write_register_byte(MPR121_TOUCHTH_0 + 2*i, touch)
186-
self._write_register_byte(MPR121_RELEASETH_0 + 2*i, release)
187-
188203
def filtered_data(self, pin):
189204
"""Return filtered data register value for the provided pin (0-11).
190205
Useful for debugging.

0 commit comments

Comments
 (0)