Skip to content

Separating_properties #21

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 2 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
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
95 changes: 89 additions & 6 deletions adafruit_cap1188/cap1188.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# SPDX-FileCopyrightText: 2018 Carter Nelson for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Expand All @@ -8,28 +7,32 @@

CircuitPython driver for the CAP1188 8-Key Capacitive Touch Sensor Breakout.

* Author(s): Carter Nelson
* Author(s): Carter Nelson, Jeremiah Rose, Jose David M.

Implementation Notes
--------------------

**Hardware:**

* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout <https://www.adafruit.com/product/1602>`_
* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout
<https://www.adafruit.com/product/1602>`_ (Product ID: 1602)

**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
https://circuitpython.org/downloads

* Adafruit's Bus Device library:
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice

* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""

from micropython import const

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CAP1188.git"


_CAP1188_MID = const(0x5D)
_CAP1188_PID = const(0x50)
_CAP1188_MAIN_CONTROL = const(0x00)
Expand All @@ -48,6 +51,7 @@
const(0x17),
)
_CAP1188_SENSITIVTY = const(0x1F)
_CAP1188_AVERAGING = const(0x24)
_CAP1188_CAL_ACTIVATE = const(0x26)
_CAP1188_MULTI_TOUCH_CFG = const(0x2A)
_CAP1188_THESHOLD_1 = const(0x30)
Expand All @@ -57,7 +61,11 @@
_CAP1188_MANU_ID = const(0xFE)
_CAP1188_REVISION = const(0xFF)


_SENSITIVITY = (128, 64, 32, 16, 8, 4, 2, 1)
_AVG = (1, 2, 4, 8, 16, 32, 64, 128)
_SAMP_TIME = ("320us", "640us", "1.28ms", "2.56ms")
_CYCLE_TIME = ("35ms", "70ms", "105ms", "140ms")


class CAP1188_Channel:
Expand Down Expand Up @@ -152,6 +160,81 @@ def sensitivity(self, value):
new_setting = self._read_register(_CAP1188_SENSITIVTY) & 0x8F | value
self._write_register(_CAP1188_SENSITIVTY, new_setting)

@property
def averaging(self):
"""Samples that are taken for all active channels during the
sensor cycle. All samples are taken consecutively on
the same channel before the next channel is sampled
and the result is averaged over the number of samples measured
before updating the measured results

if CS1, CS2, and CS3 are sampled during the sensor cycle,
and the AVG[2:0] bits are set to take 4 samples per channel,
then the full sensor cycle will be:
CS1, CS1, CS1, CS1, CS2, CS2, CS2, CS2, CS3, CS3, CS3, CS3.
"""

register = self._read_register(_CAP1188_AVERAGING)

return _AVG[register >> 4 & 0x07]

@averaging.setter
def averaging(self, value):
if value not in _AVG:
raise ValueError("Avg must be one of: {}".format(_AVG))
register = self._read_register(_CAP1188_AVERAGING)
register = register & 0x8F
avg = _AVG.index(value)
avg_value = register | avg << 4
self._write_register(_CAP1188_AVERAGING, avg_value)

@property
def sample(self):
"""Determines the overall cycle time for all measured channels
during normal operation. All measured channels are sampled at the
beginning of the cycle time. If additional time is remaining, then
the device is placed into a lower power state for the remaining
duration of the cycle."""

register = self._read_register(_CAP1188_AVERAGING)

return _SAMP_TIME[register >> 2 & 0x03]

@sample.setter
def sample(self, value):
if value not in _SAMP_TIME:
raise ValueError("Sample Time must be one of: {}".format(_SAMP_TIME))
register = self._read_register(_CAP1188_AVERAGING)
register = register & 0xF3
samp_time = _SAMP_TIME.index(value)
sample_value = register | samp_time << 2
self._write_register(_CAP1188_AVERAGING, sample_value)

@property
def cycle(self):
"""The programmed cycle time is only maintained if
the total averaging time for all samples is less
than the programmed cycle. The AVG[2:0] bits will
take priority so that if more samples are required
than would normally be allowed during the cycle
time, the cycle time will be extended as necessary
to accommodate the number of samples to be measured.
"""

register = self._read_register(_CAP1188_AVERAGING)

return _CYCLE_TIME[register & 0x03]

@cycle.setter
def cycle(self, value):
if value not in _CYCLE_TIME:
raise ValueError("Cycle Time must be one of: {}".format(_CYCLE_TIME))
register = self._read_register(_CAP1188_AVERAGING)
register = register & 0xFC
cycle_time = _CYCLE_TIME.index(value)
cycle_value = register | cycle_time
self._write_register(_CAP1188_AVERAGING, cycle_value)

@property
def thresholds(self):
"""Touch threshold value for all channels."""
Expand Down Expand Up @@ -190,7 +273,7 @@ def _read_register(self, address):
raise NotImplementedError

def _write_register(self, address, value):
"""Write 8 bit value to registter at address."""
"""Write 8 bit value to register at address."""
raise NotImplementedError

def _read_block(self, start, length):
Expand Down
9 changes: 6 additions & 3 deletions adafruit_cap1188/i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@

**Hardware:**

* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout <https://www.adafruit.com/product/1602>`_
* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout
<https://www.adafruit.com/product/1602>`_ (Product ID: 1602)

**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
https://circuitpython.org/downloads

* Adafruit's Bus Device library:
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice

* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""

import adafruit_bus_device.i2c_device as i2c_device
Expand Down
9 changes: 6 additions & 3 deletions adafruit_cap1188/spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@

**Hardware:**

* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout <https://www.adafruit.com/product/1602>`_
* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout
<https://www.adafruit.com/product/1602>`_ (Product ID: 1602)

**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
https://circuitpython.org/downloads

* Adafruit's Bus Device library:
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice

* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""

import adafruit_bus_device.spi_device as spi_device
Expand Down
10 changes: 10 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ Ensure your device works with this simple test.
.. literalinclude:: ../examples/cap1188_simpletest.py
:caption: examples/cap1188_simpletest.py
:linenos:

Advance test
------------

This example show the new feature included in the library allowing the possibilite the
configure the averaging, cycle and sample. For reference please see Sensor Datasheet.

.. literalinclude:: ../examples/cap118_advancedtest.py
:caption: examples/cap118_advancedtest.py
:linenos:
7 changes: 2 additions & 5 deletions examples/cap1188_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
# SPDX-License-Identifier: MIT

import board
import busio

# I2C setup
from adafruit_cap1188.i2c import CAP1188_I2C

i2c = busio.I2C(board.SCL, board.SDA)
i2c = board.I2C() # uses board.SCL and board.SDA
cap = CAP1188_I2C(i2c)

# SPI setup
# from digitalio import DigitalInOut, Direction
# from adafruit_cap1188.spi import CAP1188_SPI
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
# spi = board.SPI()
# cs = DigitalInOut(board.D5)
# cap = CAP1188_SPI(spi, cs)

Expand Down
33 changes: 33 additions & 0 deletions examples/cap118_advancedtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# SPDX-FileCopyrightText: 2021 Jose David M.
# SPDX-License-Identifier: MIT

# To use in the REPL >>> import cap1188_advancetest

import board
from adafruit_cap1188.i2c import CAP1188_I2C

i2c = board.I2C()
cap = CAP1188_I2C(i2c)

print(f"Sensor Initial Configuration Values: {cap.averaging, cap.sample, cap.cycle}")

averages = (1, 2, 4, 8, 16, 32, 64, 128)
samples = ("320us", "640us", "1.28ms", "2.56ms")
cycles = ("35ms", "70ms", "105ms", "140ms")

print("Setting Up Averages")
for i in averages:
cap.averaging = i
print(f"Average: {cap.averaging}")

print("Setting Up Samples")
for i in samples:
cap.sample = i
print(f"Sample: {cap.sample}")

print("Setting Up Samples")
for i in cycles:
cap.cycle = i
print(f"Cycle: {cap.cycle}")

print("Done!")