Driver for the AT42QT1070
include/at42qt1070.h@0:607af852fb64, 2015-12-03 (annotated)
- Committer:
- messi1
- Date:
- Thu Dec 03 23:01:34 2015 +0000
- Revision:
- 0:607af852fb64
- Child:
- 1:38001f05dab3
Init version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
messi1 | 0:607af852fb64 | 1 | /* |
messi1 | 0:607af852fb64 | 2 | * Author: Jon Trulson <jtrulson@ics.com> |
messi1 | 0:607af852fb64 | 3 | * Copyright (c) 2015 Intel Corporation. |
messi1 | 0:607af852fb64 | 4 | * |
messi1 | 0:607af852fb64 | 5 | * Permission is hereby granted, free of charge, to any person obtaining |
messi1 | 0:607af852fb64 | 6 | * a copy of this software and associated documentation files (the |
messi1 | 0:607af852fb64 | 7 | * "Software"), to deal in the Software without restriction, including |
messi1 | 0:607af852fb64 | 8 | * without limitation the rights to use, copy, modify, merge, publish, |
messi1 | 0:607af852fb64 | 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
messi1 | 0:607af852fb64 | 10 | * permit persons to whom the Software is furnished to do so, subject to |
messi1 | 0:607af852fb64 | 11 | * the following conditions: |
messi1 | 0:607af852fb64 | 12 | * |
messi1 | 0:607af852fb64 | 13 | * The above copyright notice and this permission notice shall be |
messi1 | 0:607af852fb64 | 14 | * included in all copies or substantial portions of the Software. |
messi1 | 0:607af852fb64 | 15 | * |
messi1 | 0:607af852fb64 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
messi1 | 0:607af852fb64 | 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
messi1 | 0:607af852fb64 | 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
messi1 | 0:607af852fb64 | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
messi1 | 0:607af852fb64 | 20 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
messi1 | 0:607af852fb64 | 21 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
messi1 | 0:607af852fb64 | 22 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
messi1 | 0:607af852fb64 | 23 | */ |
messi1 | 0:607af852fb64 | 24 | #pragma once |
messi1 | 0:607af852fb64 | 25 | |
messi1 | 0:607af852fb64 | 26 | #include <stdint.h> |
messi1 | 0:607af852fb64 | 27 | #include <sys/time.h> |
messi1 | 0:607af852fb64 | 28 | |
messi1 | 0:607af852fb64 | 29 | #include <string> |
messi1 | 0:607af852fb64 | 30 | #include <mraa/i2c.h> |
messi1 | 0:607af852fb64 | 31 | |
messi1 | 0:607af852fb64 | 32 | #define AT42QT1070_I2C_BUS 0 |
messi1 | 0:607af852fb64 | 33 | #define AT42QT1070_DEFAULT_I2C_ADDR 0x1b |
messi1 | 0:607af852fb64 | 34 | |
messi1 | 0:607af852fb64 | 35 | namespace upm |
messi1 | 0:607af852fb64 | 36 | { |
messi1 | 0:607af852fb64 | 37 | /** |
messi1 | 0:607af852fb64 | 38 | * @brief Atmel* AT42QT1070 QTouch* Sensor library |
messi1 | 0:607af852fb64 | 39 | * @defgroup at42qt1070 libupm-at42qt1070 |
messi1 | 0:607af852fb64 | 40 | * @ingroup seeed i2c touch |
messi1 | 0:607af852fb64 | 41 | */ |
messi1 | 0:607af852fb64 | 42 | |
messi1 | 0:607af852fb64 | 43 | /** |
messi1 | 0:607af852fb64 | 44 | * @library at42qt1070 |
messi1 | 0:607af852fb64 | 45 | * @sensor at42qt1070 |
messi1 | 0:607af852fb64 | 46 | * @comname AT42QT1070 QTouch Sensor |
messi1 | 0:607af852fb64 | 47 | * @altname Grove QTouch Sensor |
messi1 | 0:607af852fb64 | 48 | * @type touch |
messi1 | 0:607af852fb64 | 49 | * @man seeed |
messi1 | 0:607af852fb64 | 50 | * @con i2c |
messi1 | 0:607af852fb64 | 51 | * |
messi1 | 0:607af852fb64 | 52 | * @brief API for the Atmel AT42QT1070 QTouch Sensor |
messi1 | 0:607af852fb64 | 53 | * |
messi1 | 0:607af852fb64 | 54 | * This class implements support for the Atmel AT42QT1070 QTouch |
messi1 | 0:607af852fb64 | 55 | * sensor, which supports 7 capacitive buttons. |
messi1 | 0:607af852fb64 | 56 | * |
messi1 | 0:607af852fb64 | 57 | * It was developed using a Grove-Q Touch Sensor board. |
messi1 | 0:607af852fb64 | 58 | * |
messi1 | 0:607af852fb64 | 59 | * @image html at42qt1070.jpg |
messi1 | 0:607af852fb64 | 60 | * @snippet at42qt1070.cxx Interesting |
messi1 | 0:607af852fb64 | 61 | */ |
messi1 | 0:607af852fb64 | 62 | class AT42QT1070 |
messi1 | 0:607af852fb64 | 63 | { |
messi1 | 0:607af852fb64 | 64 | public: |
messi1 | 0:607af852fb64 | 65 | // registers |
messi1 | 0:607af852fb64 | 66 | typedef enum { |
messi1 | 0:607af852fb64 | 67 | REG_CHIPID = 0, |
messi1 | 0:607af852fb64 | 68 | REG_FWVERS = 1, |
messi1 | 0:607af852fb64 | 69 | |
messi1 | 0:607af852fb64 | 70 | REG_DETSTATUS = 2, // detection status |
messi1 | 0:607af852fb64 | 71 | REG_KEYSTATUS = 3, // key status |
messi1 | 0:607af852fb64 | 72 | |
messi1 | 0:607af852fb64 | 73 | REG_KEYSIG0_H = 4, // key signal |
messi1 | 0:607af852fb64 | 74 | REG_KEYSIG0_L = 5, |
messi1 | 0:607af852fb64 | 75 | REG_KEYSIG1_H = 6, |
messi1 | 0:607af852fb64 | 76 | REG_KEYSIG1_L = 7, |
messi1 | 0:607af852fb64 | 77 | REG_KEYSIG2_H = 8, |
messi1 | 0:607af852fb64 | 78 | REG_KEYSIG2_L = 9, |
messi1 | 0:607af852fb64 | 79 | REG_KEYSIG3_H = 10, |
messi1 | 0:607af852fb64 | 80 | REG_KEYSIG3_L = 11, |
messi1 | 0:607af852fb64 | 81 | REG_KEYSIG4_H = 12, |
messi1 | 0:607af852fb64 | 82 | REG_KEYSIG4_L = 13, |
messi1 | 0:607af852fb64 | 83 | REG_KEYSIG5_H = 14, |
messi1 | 0:607af852fb64 | 84 | REG_KEYSIG5_L = 15, |
messi1 | 0:607af852fb64 | 85 | REG_KEYSIG6_H = 16, |
messi1 | 0:607af852fb64 | 86 | REG_KEYSIG6_L = 17, |
messi1 | 0:607af852fb64 | 87 | |
messi1 | 0:607af852fb64 | 88 | REG_REFDATA0_H = 18, // key reference data |
messi1 | 0:607af852fb64 | 89 | REG_REFDATA0_L = 19, |
messi1 | 0:607af852fb64 | 90 | REG_REFDATA1_H = 20, |
messi1 | 0:607af852fb64 | 91 | REG_REFDATA1_L = 21, |
messi1 | 0:607af852fb64 | 92 | REG_REFDATA2_H = 22, |
messi1 | 0:607af852fb64 | 93 | REG_REFDATA2_L = 23, |
messi1 | 0:607af852fb64 | 94 | REG_REFDATA3_H = 24, |
messi1 | 0:607af852fb64 | 95 | REG_REFDATA3_L = 25, |
messi1 | 0:607af852fb64 | 96 | REG_REFDATA4_H = 26, |
messi1 | 0:607af852fb64 | 97 | REG_REFDATA4_L = 27, |
messi1 | 0:607af852fb64 | 98 | REG_REFDATA5_H = 28, |
messi1 | 0:607af852fb64 | 99 | REG_REFDATA5_L = 29, |
messi1 | 0:607af852fb64 | 100 | REG_REFDATA6_H = 30, |
messi1 | 0:607af852fb64 | 101 | REG_REFDATA6_L = 31, |
messi1 | 0:607af852fb64 | 102 | |
messi1 | 0:607af852fb64 | 103 | REG_NTHR0 = 32, // negative threshold level |
messi1 | 0:607af852fb64 | 104 | REG_NTHR1 = 33, |
messi1 | 0:607af852fb64 | 105 | REG_NTHR2 = 34, |
messi1 | 0:607af852fb64 | 106 | REG_NTHR3 = 35, |
messi1 | 0:607af852fb64 | 107 | REG_NTHR4 = 36, |
messi1 | 0:607af852fb64 | 108 | REG_NTHR5 = 37, |
messi1 | 0:607af852fb64 | 109 | REG_NTHR6 = 38, |
messi1 | 0:607af852fb64 | 110 | |
messi1 | 0:607af852fb64 | 111 | REG_AVE0 = 39, // key suppression |
messi1 | 0:607af852fb64 | 112 | REG_AVE1 = 40, |
messi1 | 0:607af852fb64 | 113 | REG_AVE2 = 41, |
messi1 | 0:607af852fb64 | 114 | REG_AVE3 = 42, |
messi1 | 0:607af852fb64 | 115 | REG_AVE4 = 43, |
messi1 | 0:607af852fb64 | 116 | REG_AVE5 = 44, |
messi1 | 0:607af852fb64 | 117 | REG_AVE6 = 45, |
messi1 | 0:607af852fb64 | 118 | |
messi1 | 0:607af852fb64 | 119 | REG_DI0 = 46, // detection integrator |
messi1 | 0:607af852fb64 | 120 | REG_DI1 = 47, |
messi1 | 0:607af852fb64 | 121 | REG_DI2 = 48, |
messi1 | 0:607af852fb64 | 122 | REG_DI3 = 49, |
messi1 | 0:607af852fb64 | 123 | REG_DI4 = 50, |
messi1 | 0:607af852fb64 | 124 | REG_DI5 = 51, |
messi1 | 0:607af852fb64 | 125 | REG_DI6 = 52, |
messi1 | 0:607af852fb64 | 126 | |
messi1 | 0:607af852fb64 | 127 | REG_GUARD = 53, // FastOutDI/Max Cal/Guard channel |
messi1 | 0:607af852fb64 | 128 | REG_LP = 54, // low power |
messi1 | 0:607af852fb64 | 129 | REG_MAXON = 55, // max on duration |
messi1 | 0:607af852fb64 | 130 | REG_CALIBRATE = 56, |
messi1 | 0:607af852fb64 | 131 | REG_RESET = 57 |
messi1 | 0:607af852fb64 | 132 | } AT42QT1070_REG_T; |
messi1 | 0:607af852fb64 | 133 | |
messi1 | 0:607af852fb64 | 134 | // detection register bits |
messi1 | 0:607af852fb64 | 135 | typedef enum { |
messi1 | 0:607af852fb64 | 136 | DET_TOUCH = 0x01, |
messi1 | 0:607af852fb64 | 137 | // 0x02-0x20 reserved |
messi1 | 0:607af852fb64 | 138 | DET_OVERFLOW = 0x40, |
messi1 | 0:607af852fb64 | 139 | DET_CALIBRATE = 0x80 |
messi1 | 0:607af852fb64 | 140 | } AT42QT1070_DET_T; |
messi1 | 0:607af852fb64 | 141 | |
messi1 | 0:607af852fb64 | 142 | |
messi1 | 0:607af852fb64 | 143 | /** |
messi1 | 0:607af852fb64 | 144 | * AT42QT1070 constructor |
messi1 | 0:607af852fb64 | 145 | * |
messi1 | 0:607af852fb64 | 146 | * @param bus I2C bus to use |
messi1 | 0:607af852fb64 | 147 | * @param address Address for this sensor |
messi1 | 0:607af852fb64 | 148 | */ |
messi1 | 0:607af852fb64 | 149 | AT42QT1070(int bus, uint8_t address = AT42QT1070_DEFAULT_I2C_ADDR); |
messi1 | 0:607af852fb64 | 150 | |
messi1 | 0:607af852fb64 | 151 | /** |
messi1 | 0:607af852fb64 | 152 | * AT42QT1070 destructor |
messi1 | 0:607af852fb64 | 153 | */ |
messi1 | 0:607af852fb64 | 154 | ~AT42QT1070(); |
messi1 | 0:607af852fb64 | 155 | |
messi1 | 0:607af852fb64 | 156 | /** |
messi1 | 0:607af852fb64 | 157 | * Writes a byte value into the register |
messi1 | 0:607af852fb64 | 158 | * |
messi1 | 0:607af852fb64 | 159 | * @param reg Register location to write into |
messi1 | 0:607af852fb64 | 160 | * @param byte Byte to write |
messi1 | 0:607af852fb64 | 161 | * @return True if successful |
messi1 | 0:607af852fb64 | 162 | */ |
messi1 | 0:607af852fb64 | 163 | bool writeByte(uint8_t reg, uint8_t byte); |
messi1 | 0:607af852fb64 | 164 | |
messi1 | 0:607af852fb64 | 165 | /** |
messi1 | 0:607af852fb64 | 166 | * Writes a word value into the register. Note: the device must have the |
messi1 | 0:607af852fb64 | 167 | * auto-increment bit set in the MODE1 register to work. |
messi1 | 0:607af852fb64 | 168 | * |
messi1 | 0:607af852fb64 | 169 | * @param reg Register location to write into |
messi1 | 0:607af852fb64 | 170 | * @param word Word to write |
messi1 | 0:607af852fb64 | 171 | * @return True if successful |
messi1 | 0:607af852fb64 | 172 | */ |
messi1 | 0:607af852fb64 | 173 | bool writeWord(uint8_t reg, uint16_t word); |
messi1 | 0:607af852fb64 | 174 | |
messi1 | 0:607af852fb64 | 175 | /** |
messi1 | 0:607af852fb64 | 176 | * Read a byte value from the register |
messi1 | 0:607af852fb64 | 177 | * |
messi1 | 0:607af852fb64 | 178 | * @param reg Register location to read from |
messi1 | 0:607af852fb64 | 179 | * @return Value in the specified register |
messi1 | 0:607af852fb64 | 180 | */ |
messi1 | 0:607af852fb64 | 181 | uint8_t readByte(uint8_t reg); |
messi1 | 0:607af852fb64 | 182 | |
messi1 | 0:607af852fb64 | 183 | /** |
messi1 | 0:607af852fb64 | 184 | * Read a word value from the register. Note: the device must have the |
messi1 | 0:607af852fb64 | 185 | * auto-increment bit set in the MODE1 register to work. |
messi1 | 0:607af852fb64 | 186 | * |
messi1 | 0:607af852fb64 | 187 | * @param reg Register location to read from |
messi1 | 0:607af852fb64 | 188 | * @return Value in the specified register |
messi1 | 0:607af852fb64 | 189 | */ |
messi1 | 0:607af852fb64 | 190 | uint16_t readWord(uint8_t reg); |
messi1 | 0:607af852fb64 | 191 | |
messi1 | 0:607af852fb64 | 192 | |
messi1 | 0:607af852fb64 | 193 | /** |
messi1 | 0:607af852fb64 | 194 | * Reads the Chip ID register on the sensor |
messi1 | 0:607af852fb64 | 195 | * |
messi1 | 0:607af852fb64 | 196 | * @return Value of the Chip ID register |
messi1 | 0:607af852fb64 | 197 | */ |
messi1 | 0:607af852fb64 | 198 | uint8_t readChipID(void); |
messi1 | 0:607af852fb64 | 199 | |
messi1 | 0:607af852fb64 | 200 | /** |
messi1 | 0:607af852fb64 | 201 | * Reads the current touch status and detection state |
messi1 | 0:607af852fb64 | 202 | * |
messi1 | 0:607af852fb64 | 203 | * @return Key status bits for all keys (0-6) |
messi1 | 0:607af852fb64 | 204 | */ |
messi1 | 0:607af852fb64 | 205 | void updateState(); |
messi1 | 0:607af852fb64 | 206 | |
messi1 | 0:607af852fb64 | 207 | |
messi1 | 0:607af852fb64 | 208 | /** |
messi1 | 0:607af852fb64 | 209 | * Reads the current low-power mode setting |
messi1 | 0:607af852fb64 | 210 | * |
messi1 | 0:607af852fb64 | 211 | * @return Low-power mode setting from the sensor |
messi1 | 0:607af852fb64 | 212 | */ |
messi1 | 0:607af852fb64 | 213 | uint8_t getLPMode(void); |
messi1 | 0:607af852fb64 | 214 | |
messi1 | 0:607af852fb64 | 215 | /** |
messi1 | 0:607af852fb64 | 216 | * Changes the low-pomer mode setting on the sensor |
messi1 | 0:607af852fb64 | 217 | * |
messi1 | 0:607af852fb64 | 218 | * @param mode dDsired new mode |
messi1 | 0:607af852fb64 | 219 | * @return New setting on the sensor |
messi1 | 0:607af852fb64 | 220 | */ |
messi1 | 0:607af852fb64 | 221 | uint8_t setLPMode(uint8_t mode); |
messi1 | 0:607af852fb64 | 222 | |
messi1 | 0:607af852fb64 | 223 | |
messi1 | 0:607af852fb64 | 224 | /** |
messi1 | 0:607af852fb64 | 225 | * Reads the current averaging factor setting for a key |
messi1 | 0:607af852fb64 | 226 | * |
messi1 | 0:607af852fb64 | 227 | * @param key Key being read |
messi1 | 0:607af852fb64 | 228 | * @return Averaging factor |
messi1 | 0:607af852fb64 | 229 | */ |
messi1 | 0:607af852fb64 | 230 | uint8_t getAVE(uint8_t key); |
messi1 | 0:607af852fb64 | 231 | |
messi1 | 0:607af852fb64 | 232 | /** |
messi1 | 0:607af852fb64 | 233 | * Changes the averaging factor setting for a key |
messi1 | 0:607af852fb64 | 234 | * |
messi1 | 0:607af852fb64 | 235 | * @param key Key being changed |
messi1 | 0:607af852fb64 | 236 | * @param ave New averaging factor |
messi1 | 0:607af852fb64 | 237 | * @return New averaging factor as read from the device |
messi1 | 0:607af852fb64 | 238 | */ |
messi1 | 0:607af852fb64 | 239 | uint8_t setAVE(uint8_t key, uint8_t ave); |
messi1 | 0:607af852fb64 | 240 | |
messi1 | 0:607af852fb64 | 241 | /** |
messi1 | 0:607af852fb64 | 242 | * Reads the AKS group of which a key is part |
messi1 | 0:607af852fb64 | 243 | * |
messi1 | 0:607af852fb64 | 244 | * @param key Key (0-6) being queried |
messi1 | 0:607af852fb64 | 245 | * @return AKS group of which the key is part |
messi1 | 0:607af852fb64 | 246 | */ |
messi1 | 0:607af852fb64 | 247 | uint8_t getAKSGroup(uint8_t key); |
messi1 | 0:607af852fb64 | 248 | |
messi1 | 0:607af852fb64 | 249 | /** |
messi1 | 0:607af852fb64 | 250 | * Changes the AKS group of which a key is part |
messi1 | 0:607af852fb64 | 251 | * |
messi1 | 0:607af852fb64 | 252 | * @param key Key (0-6) being changed |
messi1 | 0:607af852fb64 | 253 | * @param group New group for the key |
messi1 | 0:607af852fb64 | 254 | * @return New value on the sensor |
messi1 | 0:607af852fb64 | 255 | */ |
messi1 | 0:607af852fb64 | 256 | uint8_t setAKSGroup(uint8_t key, uint8_t group); |
messi1 | 0:607af852fb64 | 257 | |
messi1 | 0:607af852fb64 | 258 | /** |
messi1 | 0:607af852fb64 | 259 | * Returns the overflow indicator |
messi1 | 0:607af852fb64 | 260 | * |
messi1 | 0:607af852fb64 | 261 | * @return True if overflow is indicated |
messi1 | 0:607af852fb64 | 262 | */ |
messi1 | 0:607af852fb64 | 263 | bool isOverflowed() { return m_overflow; }; |
messi1 | 0:607af852fb64 | 264 | |
messi1 | 0:607af852fb64 | 265 | /** |
messi1 | 0:607af852fb64 | 266 | * Returns the calibrating indicator |
messi1 | 0:607af852fb64 | 267 | * |
messi1 | 0:607af852fb64 | 268 | * @return True if calibration is in progress |
messi1 | 0:607af852fb64 | 269 | */ |
messi1 | 0:607af852fb64 | 270 | bool isCalibrating() { return _calibrating; }; |
messi1 | 0:607af852fb64 | 271 | |
messi1 | 0:607af852fb64 | 272 | /** |
messi1 | 0:607af852fb64 | 273 | * Issues a reset command |
messi1 | 0:607af852fb64 | 274 | * |
messi1 | 0:607af852fb64 | 275 | * @return True if successful |
messi1 | 0:607af852fb64 | 276 | */ |
messi1 | 0:607af852fb64 | 277 | bool reset(); |
messi1 | 0:607af852fb64 | 278 | |
messi1 | 0:607af852fb64 | 279 | /** |
messi1 | 0:607af852fb64 | 280 | * Issues a calibrate command |
messi1 | 0:607af852fb64 | 281 | * |
messi1 | 0:607af852fb64 | 282 | * @return True if successful |
messi1 | 0:607af852fb64 | 283 | */ |
messi1 | 0:607af852fb64 | 284 | bool calibrate(); |
messi1 | 0:607af852fb64 | 285 | |
messi1 | 0:607af852fb64 | 286 | /** |
messi1 | 0:607af852fb64 | 287 | * Gets the current button states |
messi1 | 0:607af852fb64 | 288 | * |
messi1 | 0:607af852fb64 | 289 | * @returns Button states |
messi1 | 0:607af852fb64 | 290 | */ |
messi1 | 0:607af852fb64 | 291 | uint8_t getButtons() { return _buttonStates; }; |
messi1 | 0:607af852fb64 | 292 | |
messi1 | 0:607af852fb64 | 293 | private: |
messi1 | 0:607af852fb64 | 294 | uint8_t _buttonStates; |
messi1 | 0:607af852fb64 | 295 | bool _calibrating; |
messi1 | 0:607af852fb64 | 296 | bool _overflow; |
messi1 | 0:607af852fb64 | 297 | |
messi1 | 0:607af852fb64 | 298 | mraa_i2c_context _i2c; |
messi1 | 0:607af852fb64 | 299 | uint8_t _addr; |
messi1 | 0:607af852fb64 | 300 | }; |
messi1 | 0:607af852fb64 | 301 | } |