From d2be7e398a8d66d29f119931964a93eb07882adf Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Mon, 3 May 2021 18:12:08 -0400 Subject: [PATCH 1/2] Separating_properties --- adafruit_cap1188/cap1188.py | 93 +++++++++++++++++++++++++++++++--- adafruit_cap1188/i2c.py | 9 ++-- adafruit_cap1188/spi.py | 9 ++-- examples/cap1188_simpletest.py | 7 +-- 4 files changed, 101 insertions(+), 17 deletions(-) diff --git a/adafruit_cap1188/cap1188.py b/adafruit_cap1188/cap1188.py index c820d21..759b61b 100644 --- a/adafruit_cap1188/cap1188.py +++ b/adafruit_cap1188/cap1188.py @@ -1,5 +1,4 @@ # SPDX-FileCopyrightText: 2018 Carter Nelson for Adafruit Industries -# # SPDX-License-Identifier: MIT """ @@ -8,21 +7,24 @@ 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 `_ +* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout + `_ (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 @@ -30,6 +32,7 @@ __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) @@ -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) @@ -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: @@ -129,7 +137,8 @@ def __getitem__(self, key): def touched_pins(self): """A tuple of touched state for all pins.""" touched = self.touched() - return tuple(bool(touched >> i & 1) for i in range(8)) + # pylint: disable=consider-using-generator + return tuple([bool(touched >> i & 0x01) for i in range(8)]) def touched(self): """Return 8 bit value representing touch state of all pins.""" @@ -152,6 +161,78 @@ 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) + 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) + 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) + 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.""" diff --git a/adafruit_cap1188/i2c.py b/adafruit_cap1188/i2c.py index 2a8127b..efc9497 100644 --- a/adafruit_cap1188/i2c.py +++ b/adafruit_cap1188/i2c.py @@ -15,14 +15,17 @@ **Hardware:** -* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout `_ +* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout + `_ (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 diff --git a/adafruit_cap1188/spi.py b/adafruit_cap1188/spi.py index 89cf647..0ca6d54 100644 --- a/adafruit_cap1188/spi.py +++ b/adafruit_cap1188/spi.py @@ -15,14 +15,17 @@ **Hardware:** -* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout `_ +* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout + `_ (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 diff --git a/examples/cap1188_simpletest.py b/examples/cap1188_simpletest.py index 84fcd7b..cf55645 100644 --- a/examples/cap1188_simpletest.py +++ b/examples/cap1188_simpletest.py @@ -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) From 92fa6ee55e811c8ffdd6c025f9fae2f74e24766e Mon Sep 17 00:00:00 2001 From: jposada202020 Date: Thu, 6 May 2021 22:47:35 -0400 Subject: [PATCH 2/2] Adding_Bitmask and new Example. --- adafruit_cap1188/cap1188.py | 8 +++++--- docs/examples.rst | 10 ++++++++++ examples/cap118_advancedtest.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 examples/cap118_advancedtest.py diff --git a/adafruit_cap1188/cap1188.py b/adafruit_cap1188/cap1188.py index 759b61b..ff115b6 100644 --- a/adafruit_cap1188/cap1188.py +++ b/adafruit_cap1188/cap1188.py @@ -137,8 +137,7 @@ def __getitem__(self, key): def touched_pins(self): """A tuple of touched state for all pins.""" touched = self.touched() - # pylint: disable=consider-using-generator - return tuple([bool(touched >> i & 0x01) for i in range(8)]) + return tuple(bool(touched >> i & 1) for i in range(8)) def touched(self): """Return 8 bit value representing touch state of all pins.""" @@ -184,6 +183,7 @@ 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) @@ -205,6 +205,7 @@ 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) @@ -229,6 +230,7 @@ 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) @@ -271,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): diff --git a/docs/examples.rst b/docs/examples.rst index 52f3344..97060eb 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -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: diff --git a/examples/cap118_advancedtest.py b/examples/cap118_advancedtest.py new file mode 100644 index 0000000..879d396 --- /dev/null +++ b/examples/cap118_advancedtest.py @@ -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!")