Skip to content

Add property style access to thresholds #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 19, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions adafruit_mpr121.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
# pylint: enable=bad-whitespace

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

@property
def threshold(self):
"""The touch threshold."""
buf = bytearray(1)
self._mpr121._read_register_bytes(MPR121_TOUCHTH_0 + 2*self._channel, buf, 1)
return buf[0]

@threshold.setter
def threshold(self, value):
self._mpr121._write_register_byte(MPR121_TOUCHTH_0 + 2*self._channel, value)

@property
def release_threshold(self):
"""The release threshold."""
buf = bytearray(1)
self._mpr121._read_register_bytes(MPR121_RELEASETH_0 + 2*self._channel, buf, 1)
return buf[0]

@release_threshold.setter
def release_threshold(self, value):
self._mpr121._write_register_byte(MPR121_RELEASETH_0 + 2*self._channel, value)


class MPR121:
"""Driver for the MPR121 capacitive touch breakout board."""

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

def set_thresholds(self, 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).
"""
if touch < 0 or touch > 255 or release < 0 or release > 255:
raise ValueError('Touch/release must be a byte value 0-255.')
# Set the touch and release register value for all the inputs.
for i in range(12):
self._write_register_byte(MPR121_TOUCHTH_0 + 2*i, touch)
self._write_register_byte(MPR121_RELEASETH_0 + 2*i, release)

def filtered_data(self, pin):
"""Return filtered data register value for the provided pin (0-11).
Useful for debugging.
Expand Down