Skip to content

Add threshold and sensitivity control #7

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 5 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
55 changes: 55 additions & 0 deletions adafruit_cap1188/cap1188.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,21 @@
const(0x15),
const(0x16),
const(0x17))
CAP1188_SENSITIVTY = const(0x1F)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these all start with _ to save memory? I realize you didn't change it here but its probably worth doing. Variable names take a lot of memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Just not being good at being consistent. Names changed.

CAP1188_CAL_ACTIVATE = const(0x26)
CAP1188_MULTI_TOUCH_CFG = const(0x2A)
CAP1188_THESHOLD_1 = const(0x30)
CAP1188_STANDBY_CFG = const(0x41)
CAP1188_LED_LINKING = const(0x72)
CAP1188_PRODUCT_ID = const(0xFD)
CAP1188_MANU_ID = const(0xFE)
CAP1188_REVISION = const(0xFF)
# pylint: enable=bad-whitespace

_SENSITIVITY = (128, 64, 32, 16, 8, 4, 2, 1)

class CAP1188_Channel:
# pylint: disable=protected-access
"""Helper class to represent a touch channel on the CAP1188. Not meant to
be used directly."""
def __init__(self, cap1188, pin):
Expand All @@ -89,6 +94,18 @@ def raw_value(self):
"""The raw touch measurement."""
return self._cap1188.delta_count(self._pin)

@property
def threshold(self):
"""The touch threshold value."""
return self._cap1188._read_register(CAP1188_THESHOLD_1 + self._pin - 1)

@threshold.setter
def threshold(self, value):
value = int(value)
if not 0 <= value <= 127:
raise ValueError("Threshold value must be in range 0 to 127.")
self._cap1188._write_register(CAP1188_THESHOLD_1 + self._pin - 1, value)

def recalibrate(self):
"""Perform a self recalibration."""
self._cap1188.recalibrate_pins(1 << self._pin - 1)
Expand All @@ -106,6 +123,7 @@ def __init__(self):
self._channels = [None]*8
self._write_register(CAP1188_LED_LINKING, 0xFF) # turn on LED linking
self._write_register(CAP1188_MULTI_TOUCH_CFG, 0x00) # allow multi touch
self._write_register(0x2F, 0x10) # turn off input-1-sets-all-inputs feature
self.recalibrate()

def __getitem__(self, key):
Expand All @@ -131,6 +149,35 @@ def touched(self):
# return only currently touched pins
return self._read_register(CAP1188_INPUT_STATUS)

@property
def sensitivity(self):
"""The sensitvity of touch detections. Range is 1 (least) to 128 (most)."""
return _SENSITIVITY[self._read_register(CAP1188_SENSITIVTY) >> 4 & 0x07]

@sensitivity.setter
def sensitivity(self, value):
if value not in _SENSITIVITY:
raise ValueError("Sensitivty must be one of: {}".format(_SENSITIVITY))
value = _SENSITIVITY.index(value) << 4
new_setting = self._read_register(CAP1188_SENSITIVTY) & 0x8F | value
self._write_register(CAP1188_SENSITIVTY, new_setting)

@property
def thresholds(self):
"""Touch threshold value for all channels."""
return self.threshold_values()

@thresholds.setter
def thresholds(self, value):
value = int(value)
if not 0 <= value <= 127:
raise ValueError("Threshold value must be in range 0 to 127.")
self._write_block(CAP1188_THESHOLD_1, bytearray((value,)*8))

def threshold_values(self):
"""Return tuple of touch threshold values for all channels."""
return tuple(self._read_block(CAP1188_THESHOLD_1, 8))

def recalibrate(self):
"""Perform a self recalibration on all the pins."""
self.recalibrate_pins(0xFF)
Expand All @@ -155,3 +202,11 @@ def _read_register(self, address):
def _write_register(self, address, value):
"""Write 8 bit value to registter at address."""
raise NotImplementedError

def _read_block(self, start, length):
"""Return byte array of values from start address to length."""
raise NotImplementedError

def _write_block(self, start, data):
"""Write out data beginning at start address."""
raise NotImplementedError
13 changes: 13 additions & 0 deletions adafruit_cap1188/i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,16 @@ def _write_register(self, address, value):
self._buf[1] = value
with self._i2c as i2c:
i2c.write(self._buf)

def _read_block(self, start, length):
"""Return byte array of values from start address to length."""
result = bytearray(length)
with self._i2c as i2c:
i2c.write(bytes((start,)))
i2c.readinto(result)
return result

def _write_block(self, start, data):
"""Write out data beginning at start address."""
with self._i2c as i2c:
i2c.write(bytes((start,)) + data)
30 changes: 28 additions & 2 deletions adafruit_cap1188/spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,45 @@ def __init__(self, spi, cs):
super().__init__()

def _read_register(self, address):
# pylint: disable=no-member
"""Return 8 bit value of register at address."""
self._buf[0] = CAP1188_SPI_SET_ADDR
self._buf[1] = address
self._buf[2] = CAP1188_SPI_READ_DATA
with self._spi as spi:
spi.write_readinto(self._buf, self._buf) # pylint: disable=no-member
spi.write_readinto(self._buf, self._buf)
return self._buf[3]

def _write_register(self, address, value):
# pylint: disable=no-member
"""Write 8 bit value to registter at address."""
self._buf[0] = CAP1188_SPI_SET_ADDR
self._buf[1] = address
self._buf[2] = CAP1188_SPI_WRITE_DATA
self._buf[3] = value
with self._spi as spi:
spi.write(self._buf) # pylint: disable=no-member
spi.write(self._buf)

def _read_block(self, start, length):
# pylint: disable=no-member
"""Return byte array of values from start address to length."""
self._buf[0] = CAP1188_SPI_SET_ADDR
self._buf[1] = start
self._buf[2] = CAP1188_SPI_READ_DATA
result = bytearray((CAP1188_SPI_READ_DATA,)*length)
with self._spi as spi:
spi.write(self._buf, end=3)
spi.write_readinto(result, result)
return result

def _write_block(self, start, data):
# pylint: disable=no-member
"""Write out data beginning at start address."""
self._buf[0] = CAP1188_SPI_SET_ADDR
self._buf[1] = start
with self._spi as spi:
spi.write(self._buf, end=2)
self._buf[0] = CAP1188_SPI_WRITE_DATA
for value in data:
self._buf[1] = value
spi.write(self._buf, end=2)