Skip to content

Commit 00009f6

Browse files
authored
Merge pull request #21 from jposada202020/separating_properties
Separating_properties
2 parents 0e1f9df + 92fa6ee commit 00009f6

File tree

6 files changed

+146
-17
lines changed

6 files changed

+146
-17
lines changed

adafruit_cap1188/cap1188.py

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# SPDX-FileCopyrightText: 2018 Carter Nelson for Adafruit Industries
2-
#
32
# SPDX-License-Identifier: MIT
43

54
"""
@@ -8,28 +7,32 @@
87
98
CircuitPython driver for the CAP1188 8-Key Capacitive Touch Sensor Breakout.
109
11-
* Author(s): Carter Nelson
10+
* Author(s): Carter Nelson, Jeremiah Rose, Jose David M.
1211
1312
Implementation Notes
1413
--------------------
1514
1615
**Hardware:**
1716
18-
* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout <https://www.adafruit.com/product/1602>`_
17+
* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout
18+
<https://www.adafruit.com/product/1602>`_ (Product ID: 1602)
1919
2020
**Software and Dependencies:**
2121
2222
* Adafruit CircuitPython firmware for the supported boards:
23-
https://github.com/adafruit/circuitpython/releases
23+
https://circuitpython.org/downloads
24+
25+
* Adafruit's Bus Device library:
26+
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2427
25-
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2628
"""
2729

2830
from micropython import const
2931

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

35+
3336
_CAP1188_MID = const(0x5D)
3437
_CAP1188_PID = const(0x50)
3538
_CAP1188_MAIN_CONTROL = const(0x00)
@@ -48,6 +51,7 @@
4851
const(0x17),
4952
)
5053
_CAP1188_SENSITIVTY = const(0x1F)
54+
_CAP1188_AVERAGING = const(0x24)
5155
_CAP1188_CAL_ACTIVATE = const(0x26)
5256
_CAP1188_MULTI_TOUCH_CFG = const(0x2A)
5357
_CAP1188_THESHOLD_1 = const(0x30)
@@ -57,7 +61,11 @@
5761
_CAP1188_MANU_ID = const(0xFE)
5862
_CAP1188_REVISION = const(0xFF)
5963

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

6270

6371
class CAP1188_Channel:
@@ -152,6 +160,81 @@ def sensitivity(self, value):
152160
new_setting = self._read_register(_CAP1188_SENSITIVTY) & 0x8F | value
153161
self._write_register(_CAP1188_SENSITIVTY, new_setting)
154162

163+
@property
164+
def averaging(self):
165+
"""Samples that are taken for all active channels during the
166+
sensor cycle. All samples are taken consecutively on
167+
the same channel before the next channel is sampled
168+
and the result is averaged over the number of samples measured
169+
before updating the measured results
170+
171+
if CS1, CS2, and CS3 are sampled during the sensor cycle,
172+
and the AVG[2:0] bits are set to take 4 samples per channel,
173+
then the full sensor cycle will be:
174+
CS1, CS1, CS1, CS1, CS2, CS2, CS2, CS2, CS3, CS3, CS3, CS3.
175+
"""
176+
177+
register = self._read_register(_CAP1188_AVERAGING)
178+
179+
return _AVG[register >> 4 & 0x07]
180+
181+
@averaging.setter
182+
def averaging(self, value):
183+
if value not in _AVG:
184+
raise ValueError("Avg must be one of: {}".format(_AVG))
185+
register = self._read_register(_CAP1188_AVERAGING)
186+
register = register & 0x8F
187+
avg = _AVG.index(value)
188+
avg_value = register | avg << 4
189+
self._write_register(_CAP1188_AVERAGING, avg_value)
190+
191+
@property
192+
def sample(self):
193+
"""Determines the overall cycle time for all measured channels
194+
during normal operation. All measured channels are sampled at the
195+
beginning of the cycle time. If additional time is remaining, then
196+
the device is placed into a lower power state for the remaining
197+
duration of the cycle."""
198+
199+
register = self._read_register(_CAP1188_AVERAGING)
200+
201+
return _SAMP_TIME[register >> 2 & 0x03]
202+
203+
@sample.setter
204+
def sample(self, value):
205+
if value not in _SAMP_TIME:
206+
raise ValueError("Sample Time must be one of: {}".format(_SAMP_TIME))
207+
register = self._read_register(_CAP1188_AVERAGING)
208+
register = register & 0xF3
209+
samp_time = _SAMP_TIME.index(value)
210+
sample_value = register | samp_time << 2
211+
self._write_register(_CAP1188_AVERAGING, sample_value)
212+
213+
@property
214+
def cycle(self):
215+
"""The programmed cycle time is only maintained if
216+
the total averaging time for all samples is less
217+
than the programmed cycle. The AVG[2:0] bits will
218+
take priority so that if more samples are required
219+
than would normally be allowed during the cycle
220+
time, the cycle time will be extended as necessary
221+
to accommodate the number of samples to be measured.
222+
"""
223+
224+
register = self._read_register(_CAP1188_AVERAGING)
225+
226+
return _CYCLE_TIME[register & 0x03]
227+
228+
@cycle.setter
229+
def cycle(self, value):
230+
if value not in _CYCLE_TIME:
231+
raise ValueError("Cycle Time must be one of: {}".format(_CYCLE_TIME))
232+
register = self._read_register(_CAP1188_AVERAGING)
233+
register = register & 0xFC
234+
cycle_time = _CYCLE_TIME.index(value)
235+
cycle_value = register | cycle_time
236+
self._write_register(_CAP1188_AVERAGING, cycle_value)
237+
155238
@property
156239
def thresholds(self):
157240
"""Touch threshold value for all channels."""
@@ -190,7 +273,7 @@ def _read_register(self, address):
190273
raise NotImplementedError
191274

192275
def _write_register(self, address, value):
193-
"""Write 8 bit value to registter at address."""
276+
"""Write 8 bit value to register at address."""
194277
raise NotImplementedError
195278

196279
def _read_block(self, start, length):

adafruit_cap1188/i2c.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
1616
**Hardware:**
1717
18-
* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout <https://www.adafruit.com/product/1602>`_
18+
* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout
19+
<https://www.adafruit.com/product/1602>`_ (Product ID: 1602)
1920
2021
**Software and Dependencies:**
2122
2223
* Adafruit CircuitPython firmware for the supported boards:
23-
https://github.com/adafruit/circuitpython/releases
24+
https://circuitpython.org/downloads
25+
26+
* Adafruit's Bus Device library:
27+
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2428
25-
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2629
"""
2730

2831
import adafruit_bus_device.i2c_device as i2c_device

adafruit_cap1188/spi.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
1616
**Hardware:**
1717
18-
* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout <https://www.adafruit.com/product/1602>`_
18+
* `CAP1188 - 8-Key Capacitive Touch Sensor Breakout
19+
<https://www.adafruit.com/product/1602>`_ (Product ID: 1602)
1920
2021
**Software and Dependencies:**
2122
2223
* Adafruit CircuitPython firmware for the supported boards:
23-
https://github.com/adafruit/circuitpython/releases
24+
https://circuitpython.org/downloads
25+
26+
* Adafruit's Bus Device library:
27+
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2428
25-
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2629
"""
2730

2831
import adafruit_bus_device.spi_device as spi_device

docs/examples.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,13 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/cap1188_simpletest.py
77
:caption: examples/cap1188_simpletest.py
88
:linenos:
9+
10+
Advance test
11+
------------
12+
13+
This example show the new feature included in the library allowing the possibilite the
14+
configure the averaging, cycle and sample. For reference please see Sensor Datasheet.
15+
16+
.. literalinclude:: ../examples/cap118_advancedtest.py
17+
:caption: examples/cap118_advancedtest.py
18+
:linenos:

examples/cap1188_simpletest.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
# SPDX-License-Identifier: MIT
33

44
import board
5-
import busio
6-
7-
# I2C setup
85
from adafruit_cap1188.i2c import CAP1188_I2C
96

10-
i2c = busio.I2C(board.SCL, board.SDA)
7+
i2c = board.I2C() # uses board.SCL and board.SDA
118
cap = CAP1188_I2C(i2c)
129

1310
# SPI setup
1411
# from digitalio import DigitalInOut, Direction
1512
# from adafruit_cap1188.spi import CAP1188_SPI
16-
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
13+
# spi = board.SPI()
1714
# cs = DigitalInOut(board.D5)
1815
# cap = CAP1188_SPI(spi, cs)
1916

examples/cap118_advancedtest.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SPDX-FileCopyrightText: 2021 Jose David M.
2+
# SPDX-License-Identifier: MIT
3+
4+
# To use in the REPL >>> import cap1188_advancetest
5+
6+
import board
7+
from adafruit_cap1188.i2c import CAP1188_I2C
8+
9+
i2c = board.I2C()
10+
cap = CAP1188_I2C(i2c)
11+
12+
print(f"Sensor Initial Configuration Values: {cap.averaging, cap.sample, cap.cycle}")
13+
14+
averages = (1, 2, 4, 8, 16, 32, 64, 128)
15+
samples = ("320us", "640us", "1.28ms", "2.56ms")
16+
cycles = ("35ms", "70ms", "105ms", "140ms")
17+
18+
print("Setting Up Averages")
19+
for i in averages:
20+
cap.averaging = i
21+
print(f"Average: {cap.averaging}")
22+
23+
print("Setting Up Samples")
24+
for i in samples:
25+
cap.sample = i
26+
print(f"Sample: {cap.sample}")
27+
28+
print("Setting Up Samples")
29+
for i in cycles:
30+
cap.cycle = i
31+
print(f"Cycle: {cap.cycle}")
32+
33+
print("Done!")

0 commit comments

Comments
 (0)