|
1 | 1 | # SPDX-FileCopyrightText: 2018 Carter Nelson for Adafruit Industries
|
2 |
| -# |
3 | 2 | # SPDX-License-Identifier: MIT
|
4 | 3 |
|
5 | 4 | """
|
|
8 | 7 |
|
9 | 8 | CircuitPython driver for the CAP1188 8-Key Capacitive Touch Sensor Breakout.
|
10 | 9 |
|
11 |
| -* Author(s): Carter Nelson |
| 10 | +* Author(s): Carter Nelson, Jeremiah Rose, Jose David M. |
12 | 11 |
|
13 | 12 | Implementation Notes
|
14 | 13 | --------------------
|
15 | 14 |
|
16 | 15 | **Hardware:**
|
17 | 16 |
|
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) |
19 | 19 |
|
20 | 20 | **Software and Dependencies:**
|
21 | 21 |
|
22 | 22 | * 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 |
24 | 27 |
|
25 |
| -* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
26 | 28 | """
|
27 | 29 |
|
28 | 30 | from micropython import const
|
29 | 31 |
|
30 | 32 | __version__ = "0.0.0-auto.0"
|
31 | 33 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CAP1188.git"
|
32 | 34 |
|
| 35 | + |
33 | 36 | _CAP1188_MID = const(0x5D)
|
34 | 37 | _CAP1188_PID = const(0x50)
|
35 | 38 | _CAP1188_MAIN_CONTROL = const(0x00)
|
|
48 | 51 | const(0x17),
|
49 | 52 | )
|
50 | 53 | _CAP1188_SENSITIVTY = const(0x1F)
|
| 54 | +_CAP1188_AVERAGING = const(0x24) |
51 | 55 | _CAP1188_CAL_ACTIVATE = const(0x26)
|
52 | 56 | _CAP1188_MULTI_TOUCH_CFG = const(0x2A)
|
53 | 57 | _CAP1188_THESHOLD_1 = const(0x30)
|
|
57 | 61 | _CAP1188_MANU_ID = const(0xFE)
|
58 | 62 | _CAP1188_REVISION = const(0xFF)
|
59 | 63 |
|
| 64 | + |
60 | 65 | _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") |
61 | 69 |
|
62 | 70 |
|
63 | 71 | class CAP1188_Channel:
|
@@ -152,6 +160,81 @@ def sensitivity(self, value):
|
152 | 160 | new_setting = self._read_register(_CAP1188_SENSITIVTY) & 0x8F | value
|
153 | 161 | self._write_register(_CAP1188_SENSITIVTY, new_setting)
|
154 | 162 |
|
| 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 | + |
155 | 238 | @property
|
156 | 239 | def thresholds(self):
|
157 | 240 | """Touch threshold value for all channels."""
|
@@ -190,7 +273,7 @@ def _read_register(self, address):
|
190 | 273 | raise NotImplementedError
|
191 | 274 |
|
192 | 275 | def _write_register(self, address, value):
|
193 |
| - """Write 8 bit value to registter at address.""" |
| 276 | + """Write 8 bit value to register at address.""" |
194 | 277 | raise NotImplementedError
|
195 | 278 |
|
196 | 279 | def _read_block(self, start, length):
|
|
0 commit comments