Driver for the AT42QT1070
include/at42qt1070.h@1:38001f05dab3, 2015-12-20 (annotated)
- Committer:
- messi1
- Date:
- Sun Dec 20 19:38:45 2015 +0000
- Revision:
- 1:38001f05dab3
- Parent:
- 0:607af852fb64
- Child:
- 2:cee8b532fc22
Now it compiles. No tests yet
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 | 1:38001f05dab3 | 27 | //#include <sys/time.h> |
messi1 | 0:607af852fb64 | 28 | |
messi1 | 0:607af852fb64 | 29 | #include <string> |
messi1 | 1:38001f05dab3 | 30 | #include <I2C.h> |
messi1 | 1:38001f05dab3 | 31 | #include "PinNames.h" |
messi1 | 1:38001f05dab3 | 32 | //#include <mraa/i2c.h> |
messi1 | 0:607af852fb64 | 33 | |
messi1 | 0:607af852fb64 | 34 | #define AT42QT1070_I2C_BUS 0 |
messi1 | 0:607af852fb64 | 35 | #define AT42QT1070_DEFAULT_I2C_ADDR 0x1b |
messi1 | 0:607af852fb64 | 36 | |
messi1 | 0:607af852fb64 | 37 | namespace upm |
messi1 | 0:607af852fb64 | 38 | { |
messi1 | 0:607af852fb64 | 39 | /** |
messi1 | 0:607af852fb64 | 40 | * @brief Atmel* AT42QT1070 QTouch* Sensor library |
messi1 | 0:607af852fb64 | 41 | * @defgroup at42qt1070 libupm-at42qt1070 |
messi1 | 0:607af852fb64 | 42 | * @ingroup seeed i2c touch |
messi1 | 0:607af852fb64 | 43 | */ |
messi1 | 0:607af852fb64 | 44 | |
messi1 | 0:607af852fb64 | 45 | /** |
messi1 | 0:607af852fb64 | 46 | * @library at42qt1070 |
messi1 | 0:607af852fb64 | 47 | * @sensor at42qt1070 |
messi1 | 0:607af852fb64 | 48 | * @comname AT42QT1070 QTouch Sensor |
messi1 | 0:607af852fb64 | 49 | * @altname Grove QTouch Sensor |
messi1 | 0:607af852fb64 | 50 | * @type touch |
messi1 | 0:607af852fb64 | 51 | * @man seeed |
messi1 | 0:607af852fb64 | 52 | * @con i2c |
messi1 | 0:607af852fb64 | 53 | * |
messi1 | 0:607af852fb64 | 54 | * @brief API for the Atmel AT42QT1070 QTouch Sensor |
messi1 | 0:607af852fb64 | 55 | * |
messi1 | 0:607af852fb64 | 56 | * This class implements support for the Atmel AT42QT1070 QTouch |
messi1 | 0:607af852fb64 | 57 | * sensor, which supports 7 capacitive buttons. |
messi1 | 0:607af852fb64 | 58 | * |
messi1 | 0:607af852fb64 | 59 | * It was developed using a Grove-Q Touch Sensor board. |
messi1 | 0:607af852fb64 | 60 | * |
messi1 | 0:607af852fb64 | 61 | * @image html at42qt1070.jpg |
messi1 | 0:607af852fb64 | 62 | * @snippet at42qt1070.cxx Interesting |
messi1 | 0:607af852fb64 | 63 | */ |
messi1 | 0:607af852fb64 | 64 | class AT42QT1070 |
messi1 | 0:607af852fb64 | 65 | { |
messi1 | 0:607af852fb64 | 66 | public: |
messi1 | 0:607af852fb64 | 67 | // registers |
messi1 | 0:607af852fb64 | 68 | typedef enum { |
messi1 | 0:607af852fb64 | 69 | REG_CHIPID = 0, |
messi1 | 0:607af852fb64 | 70 | REG_FWVERS = 1, |
messi1 | 0:607af852fb64 | 71 | |
messi1 | 0:607af852fb64 | 72 | REG_DETSTATUS = 2, // detection status |
messi1 | 0:607af852fb64 | 73 | REG_KEYSTATUS = 3, // key status |
messi1 | 0:607af852fb64 | 74 | |
messi1 | 0:607af852fb64 | 75 | REG_KEYSIG0_H = 4, // key signal |
messi1 | 0:607af852fb64 | 76 | REG_KEYSIG0_L = 5, |
messi1 | 0:607af852fb64 | 77 | REG_KEYSIG1_H = 6, |
messi1 | 0:607af852fb64 | 78 | REG_KEYSIG1_L = 7, |
messi1 | 0:607af852fb64 | 79 | REG_KEYSIG2_H = 8, |
messi1 | 0:607af852fb64 | 80 | REG_KEYSIG2_L = 9, |
messi1 | 0:607af852fb64 | 81 | REG_KEYSIG3_H = 10, |
messi1 | 0:607af852fb64 | 82 | REG_KEYSIG3_L = 11, |
messi1 | 0:607af852fb64 | 83 | REG_KEYSIG4_H = 12, |
messi1 | 0:607af852fb64 | 84 | REG_KEYSIG4_L = 13, |
messi1 | 0:607af852fb64 | 85 | REG_KEYSIG5_H = 14, |
messi1 | 0:607af852fb64 | 86 | REG_KEYSIG5_L = 15, |
messi1 | 0:607af852fb64 | 87 | REG_KEYSIG6_H = 16, |
messi1 | 0:607af852fb64 | 88 | REG_KEYSIG6_L = 17, |
messi1 | 0:607af852fb64 | 89 | |
messi1 | 0:607af852fb64 | 90 | REG_REFDATA0_H = 18, // key reference data |
messi1 | 0:607af852fb64 | 91 | REG_REFDATA0_L = 19, |
messi1 | 0:607af852fb64 | 92 | REG_REFDATA1_H = 20, |
messi1 | 0:607af852fb64 | 93 | REG_REFDATA1_L = 21, |
messi1 | 0:607af852fb64 | 94 | REG_REFDATA2_H = 22, |
messi1 | 0:607af852fb64 | 95 | REG_REFDATA2_L = 23, |
messi1 | 0:607af852fb64 | 96 | REG_REFDATA3_H = 24, |
messi1 | 0:607af852fb64 | 97 | REG_REFDATA3_L = 25, |
messi1 | 0:607af852fb64 | 98 | REG_REFDATA4_H = 26, |
messi1 | 0:607af852fb64 | 99 | REG_REFDATA4_L = 27, |
messi1 | 0:607af852fb64 | 100 | REG_REFDATA5_H = 28, |
messi1 | 0:607af852fb64 | 101 | REG_REFDATA5_L = 29, |
messi1 | 0:607af852fb64 | 102 | REG_REFDATA6_H = 30, |
messi1 | 0:607af852fb64 | 103 | REG_REFDATA6_L = 31, |
messi1 | 0:607af852fb64 | 104 | |
messi1 | 0:607af852fb64 | 105 | REG_NTHR0 = 32, // negative threshold level |
messi1 | 0:607af852fb64 | 106 | REG_NTHR1 = 33, |
messi1 | 0:607af852fb64 | 107 | REG_NTHR2 = 34, |
messi1 | 0:607af852fb64 | 108 | REG_NTHR3 = 35, |
messi1 | 0:607af852fb64 | 109 | REG_NTHR4 = 36, |
messi1 | 0:607af852fb64 | 110 | REG_NTHR5 = 37, |
messi1 | 0:607af852fb64 | 111 | REG_NTHR6 = 38, |
messi1 | 0:607af852fb64 | 112 | |
messi1 | 0:607af852fb64 | 113 | REG_AVE0 = 39, // key suppression |
messi1 | 0:607af852fb64 | 114 | REG_AVE1 = 40, |
messi1 | 0:607af852fb64 | 115 | REG_AVE2 = 41, |
messi1 | 0:607af852fb64 | 116 | REG_AVE3 = 42, |
messi1 | 0:607af852fb64 | 117 | REG_AVE4 = 43, |
messi1 | 0:607af852fb64 | 118 | REG_AVE5 = 44, |
messi1 | 0:607af852fb64 | 119 | REG_AVE6 = 45, |
messi1 | 0:607af852fb64 | 120 | |
messi1 | 0:607af852fb64 | 121 | REG_DI0 = 46, // detection integrator |
messi1 | 0:607af852fb64 | 122 | REG_DI1 = 47, |
messi1 | 0:607af852fb64 | 123 | REG_DI2 = 48, |
messi1 | 0:607af852fb64 | 124 | REG_DI3 = 49, |
messi1 | 0:607af852fb64 | 125 | REG_DI4 = 50, |
messi1 | 0:607af852fb64 | 126 | REG_DI5 = 51, |
messi1 | 0:607af852fb64 | 127 | REG_DI6 = 52, |
messi1 | 0:607af852fb64 | 128 | |
messi1 | 0:607af852fb64 | 129 | REG_GUARD = 53, // FastOutDI/Max Cal/Guard channel |
messi1 | 0:607af852fb64 | 130 | REG_LP = 54, // low power |
messi1 | 0:607af852fb64 | 131 | REG_MAXON = 55, // max on duration |
messi1 | 0:607af852fb64 | 132 | REG_CALIBRATE = 56, |
messi1 | 0:607af852fb64 | 133 | REG_RESET = 57 |
messi1 | 0:607af852fb64 | 134 | } AT42QT1070_REG_T; |
messi1 | 0:607af852fb64 | 135 | |
messi1 | 0:607af852fb64 | 136 | // detection register bits |
messi1 | 0:607af852fb64 | 137 | typedef enum { |
messi1 | 0:607af852fb64 | 138 | DET_TOUCH = 0x01, |
messi1 | 0:607af852fb64 | 139 | // 0x02-0x20 reserved |
messi1 | 0:607af852fb64 | 140 | DET_OVERFLOW = 0x40, |
messi1 | 0:607af852fb64 | 141 | DET_CALIBRATE = 0x80 |
messi1 | 0:607af852fb64 | 142 | } AT42QT1070_DET_T; |
messi1 | 0:607af852fb64 | 143 | |
messi1 | 0:607af852fb64 | 144 | |
messi1 | 0:607af852fb64 | 145 | /** |
messi1 | 0:607af852fb64 | 146 | * AT42QT1070 constructor |
messi1 | 0:607af852fb64 | 147 | * |
messi1 | 0:607af852fb64 | 148 | * @param bus I2C bus to use |
messi1 | 0:607af852fb64 | 149 | * @param address Address for this sensor |
messi1 | 0:607af852fb64 | 150 | */ |
messi1 | 1:38001f05dab3 | 151 | AT42QT1070(PinName sda, PinName sck, int bus, uint8_t address = AT42QT1070_DEFAULT_I2C_ADDR); |
messi1 | 0:607af852fb64 | 152 | |
messi1 | 0:607af852fb64 | 153 | /** |
messi1 | 0:607af852fb64 | 154 | * AT42QT1070 destructor |
messi1 | 0:607af852fb64 | 155 | */ |
messi1 | 0:607af852fb64 | 156 | ~AT42QT1070(); |
messi1 | 0:607af852fb64 | 157 | |
messi1 | 0:607af852fb64 | 158 | /** |
messi1 | 0:607af852fb64 | 159 | * Reads the Chip ID register on the sensor |
messi1 | 0:607af852fb64 | 160 | * |
messi1 | 0:607af852fb64 | 161 | * @return Value of the Chip ID register |
messi1 | 0:607af852fb64 | 162 | */ |
messi1 | 0:607af852fb64 | 163 | uint8_t readChipID(void); |
messi1 | 0:607af852fb64 | 164 | |
messi1 | 0:607af852fb64 | 165 | /** |
messi1 | 0:607af852fb64 | 166 | * Reads the current touch status and detection state |
messi1 | 0:607af852fb64 | 167 | * |
messi1 | 0:607af852fb64 | 168 | * @return Key status bits for all keys (0-6) |
messi1 | 0:607af852fb64 | 169 | */ |
messi1 | 0:607af852fb64 | 170 | void updateState(); |
messi1 | 0:607af852fb64 | 171 | |
messi1 | 0:607af852fb64 | 172 | |
messi1 | 0:607af852fb64 | 173 | /** |
messi1 | 0:607af852fb64 | 174 | * Reads the current low-power mode setting |
messi1 | 0:607af852fb64 | 175 | * |
messi1 | 0:607af852fb64 | 176 | * @return Low-power mode setting from the sensor |
messi1 | 0:607af852fb64 | 177 | */ |
messi1 | 1:38001f05dab3 | 178 | uint8_t getLowPowerMode(void); |
messi1 | 0:607af852fb64 | 179 | |
messi1 | 0:607af852fb64 | 180 | /** |
messi1 | 0:607af852fb64 | 181 | * Changes the low-pomer mode setting on the sensor |
messi1 | 0:607af852fb64 | 182 | * |
messi1 | 0:607af852fb64 | 183 | * @param mode dDsired new mode |
messi1 | 0:607af852fb64 | 184 | * @return New setting on the sensor |
messi1 | 0:607af852fb64 | 185 | */ |
messi1 | 1:38001f05dab3 | 186 | uint8_t setLowPowerMode(uint8_t mode); |
messi1 | 0:607af852fb64 | 187 | |
messi1 | 0:607af852fb64 | 188 | |
messi1 | 0:607af852fb64 | 189 | /** |
messi1 | 0:607af852fb64 | 190 | * Reads the current averaging factor setting for a key |
messi1 | 0:607af852fb64 | 191 | * |
messi1 | 0:607af852fb64 | 192 | * @param key Key being read |
messi1 | 0:607af852fb64 | 193 | * @return Averaging factor |
messi1 | 0:607af852fb64 | 194 | */ |
messi1 | 0:607af852fb64 | 195 | uint8_t getAVE(uint8_t key); |
messi1 | 0:607af852fb64 | 196 | |
messi1 | 0:607af852fb64 | 197 | /** |
messi1 | 0:607af852fb64 | 198 | * Changes the averaging factor setting for a key |
messi1 | 0:607af852fb64 | 199 | * |
messi1 | 0:607af852fb64 | 200 | * @param key Key being changed |
messi1 | 0:607af852fb64 | 201 | * @param ave New averaging factor |
messi1 | 0:607af852fb64 | 202 | * @return New averaging factor as read from the device |
messi1 | 0:607af852fb64 | 203 | */ |
messi1 | 0:607af852fb64 | 204 | uint8_t setAVE(uint8_t key, uint8_t ave); |
messi1 | 0:607af852fb64 | 205 | |
messi1 | 0:607af852fb64 | 206 | /** |
messi1 | 0:607af852fb64 | 207 | * Reads the AKS group of which a key is part |
messi1 | 0:607af852fb64 | 208 | * |
messi1 | 0:607af852fb64 | 209 | * @param key Key (0-6) being queried |
messi1 | 0:607af852fb64 | 210 | * @return AKS group of which the key is part |
messi1 | 0:607af852fb64 | 211 | */ |
messi1 | 0:607af852fb64 | 212 | uint8_t getAKSGroup(uint8_t key); |
messi1 | 0:607af852fb64 | 213 | |
messi1 | 0:607af852fb64 | 214 | /** |
messi1 | 0:607af852fb64 | 215 | * Changes the AKS group of which a key is part |
messi1 | 0:607af852fb64 | 216 | * |
messi1 | 0:607af852fb64 | 217 | * @param key Key (0-6) being changed |
messi1 | 0:607af852fb64 | 218 | * @param group New group for the key |
messi1 | 0:607af852fb64 | 219 | * @return New value on the sensor |
messi1 | 0:607af852fb64 | 220 | */ |
messi1 | 0:607af852fb64 | 221 | uint8_t setAKSGroup(uint8_t key, uint8_t group); |
messi1 | 0:607af852fb64 | 222 | |
messi1 | 0:607af852fb64 | 223 | /** |
messi1 | 0:607af852fb64 | 224 | * Returns the overflow indicator |
messi1 | 0:607af852fb64 | 225 | * |
messi1 | 0:607af852fb64 | 226 | * @return True if overflow is indicated |
messi1 | 0:607af852fb64 | 227 | */ |
messi1 | 1:38001f05dab3 | 228 | bool isOverflowed() { return _overflow; }; |
messi1 | 0:607af852fb64 | 229 | |
messi1 | 0:607af852fb64 | 230 | /** |
messi1 | 0:607af852fb64 | 231 | * Returns the calibrating indicator |
messi1 | 0:607af852fb64 | 232 | * |
messi1 | 0:607af852fb64 | 233 | * @return True if calibration is in progress |
messi1 | 0:607af852fb64 | 234 | */ |
messi1 | 0:607af852fb64 | 235 | bool isCalibrating() { return _calibrating; }; |
messi1 | 0:607af852fb64 | 236 | |
messi1 | 0:607af852fb64 | 237 | /** |
messi1 | 0:607af852fb64 | 238 | * Issues a reset command |
messi1 | 0:607af852fb64 | 239 | * |
messi1 | 0:607af852fb64 | 240 | * @return True if successful |
messi1 | 0:607af852fb64 | 241 | */ |
messi1 | 0:607af852fb64 | 242 | bool reset(); |
messi1 | 0:607af852fb64 | 243 | |
messi1 | 0:607af852fb64 | 244 | /** |
messi1 | 0:607af852fb64 | 245 | * Issues a calibrate command |
messi1 | 0:607af852fb64 | 246 | * |
messi1 | 0:607af852fb64 | 247 | * @return True if successful |
messi1 | 0:607af852fb64 | 248 | */ |
messi1 | 0:607af852fb64 | 249 | bool calibrate(); |
messi1 | 0:607af852fb64 | 250 | |
messi1 | 0:607af852fb64 | 251 | /** |
messi1 | 0:607af852fb64 | 252 | * Gets the current button states |
messi1 | 0:607af852fb64 | 253 | * |
messi1 | 0:607af852fb64 | 254 | * @returns Button states |
messi1 | 0:607af852fb64 | 255 | */ |
messi1 | 1:38001f05dab3 | 256 | uint8_t getButtonsState(); |
messi1 | 1:38001f05dab3 | 257 | |
messi1 | 1:38001f05dab3 | 258 | bool isButtonPressed(const uint8_t button); |
messi1 | 1:38001f05dab3 | 259 | |
messi1 | 1:38001f05dab3 | 260 | private: |
messi1 | 1:38001f05dab3 | 261 | /** |
messi1 | 1:38001f05dab3 | 262 | * Writes a byte value into the register |
messi1 | 1:38001f05dab3 | 263 | * |
messi1 | 1:38001f05dab3 | 264 | * @param reg Register location to write into |
messi1 | 1:38001f05dab3 | 265 | * @param byte Byte to write |
messi1 | 1:38001f05dab3 | 266 | * @return True if successful |
messi1 | 1:38001f05dab3 | 267 | */ |
messi1 | 1:38001f05dab3 | 268 | bool writeByte(uint8_t reg, uint8_t byte); |
messi1 | 1:38001f05dab3 | 269 | |
messi1 | 1:38001f05dab3 | 270 | /** |
messi1 | 1:38001f05dab3 | 271 | * Writes a word value into the register. Note: the device must have the |
messi1 | 1:38001f05dab3 | 272 | * auto-increment bit set in the MODE1 register to work. |
messi1 | 1:38001f05dab3 | 273 | * |
messi1 | 1:38001f05dab3 | 274 | * @param reg Register location to write into |
messi1 | 1:38001f05dab3 | 275 | * @param word Word to write |
messi1 | 1:38001f05dab3 | 276 | * @return True if successful |
messi1 | 1:38001f05dab3 | 277 | */ |
messi1 | 1:38001f05dab3 | 278 | bool writeWord(uint8_t reg, uint16_t word); |
messi1 | 1:38001f05dab3 | 279 | |
messi1 | 1:38001f05dab3 | 280 | /** |
messi1 | 1:38001f05dab3 | 281 | * Read a byte value from the register |
messi1 | 1:38001f05dab3 | 282 | * |
messi1 | 1:38001f05dab3 | 283 | * @param reg Register location to read from |
messi1 | 1:38001f05dab3 | 284 | * @return Value in the specified register |
messi1 | 1:38001f05dab3 | 285 | */ |
messi1 | 1:38001f05dab3 | 286 | uint8_t readByte(uint8_t reg); |
messi1 | 1:38001f05dab3 | 287 | |
messi1 | 1:38001f05dab3 | 288 | /** |
messi1 | 1:38001f05dab3 | 289 | * Read a word value from the register. Note: the device must have the |
messi1 | 1:38001f05dab3 | 290 | * auto-increment bit set in the MODE1 register to work. |
messi1 | 1:38001f05dab3 | 291 | * |
messi1 | 1:38001f05dab3 | 292 | * @param reg Register location to read from |
messi1 | 1:38001f05dab3 | 293 | * @return Value in the specified register |
messi1 | 1:38001f05dab3 | 294 | */ |
messi1 | 1:38001f05dab3 | 295 | uint16_t readWord(uint8_t reg); |
messi1 | 0:607af852fb64 | 296 | |
messi1 | 0:607af852fb64 | 297 | private: |
messi1 | 0:607af852fb64 | 298 | uint8_t _buttonStates; |
messi1 | 0:607af852fb64 | 299 | bool _calibrating; |
messi1 | 0:607af852fb64 | 300 | bool _overflow; |
messi1 | 0:607af852fb64 | 301 | |
messi1 | 1:38001f05dab3 | 302 | mbed::I2C _i2cPort; |
messi1 | 0:607af852fb64 | 303 | uint8_t _addr; |
messi1 | 0:607af852fb64 | 304 | }; |
messi1 | 0:607af852fb64 | 305 | } |