Driver for the AT42QT1070

Committer:
messi1
Date:
Tue Dec 22 21:45:00 2015 +0000
Revision:
2:cee8b532fc22
Parent:
1:38001f05dab3
The driver is now working

Who changed what in which revision?

UserRevisionLine numberNew 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 /**
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 2:cee8b532fc22 127 REG_GUARD = 53, // FastOutDI/Max Cal/Guard channel
messi1 2:cee8b532fc22 128 REG_LP = 54, // low power
messi1 2:cee8b532fc22 129 REG_MAXON = 55, // max on duration
messi1 0:607af852fb64 130 REG_CALIBRATE = 56,
messi1 2:cee8b532fc22 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 2:cee8b532fc22 136 DET_TOUCH = 0x01,
messi1 0:607af852fb64 137 // 0x02-0x20 reserved
messi1 2:cee8b532fc22 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 2:cee8b532fc22 149 AT42QT1070(PinName sda, PinName sck, 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 * Reads the Chip ID register on the sensor
messi1 0:607af852fb64 158 *
messi1 0:607af852fb64 159 * @return Value of the Chip ID register
messi1 0:607af852fb64 160 */
messi1 0:607af852fb64 161 uint8_t readChipID(void);
messi1 0:607af852fb64 162
messi1 0:607af852fb64 163 /**
messi1 0:607af852fb64 164 * Reads the current touch status and detection state
messi1 0:607af852fb64 165 *
messi1 0:607af852fb64 166 * @return Key status bits for all keys (0-6)
messi1 0:607af852fb64 167 */
messi1 0:607af852fb64 168 void updateState();
messi1 0:607af852fb64 169
messi1 0:607af852fb64 170
messi1 0:607af852fb64 171 /**
messi1 0:607af852fb64 172 * Reads the current low-power mode setting
messi1 0:607af852fb64 173 *
messi1 0:607af852fb64 174 * @return Low-power mode setting from the sensor
messi1 0:607af852fb64 175 */
messi1 1:38001f05dab3 176 uint8_t getLowPowerMode(void);
messi1 0:607af852fb64 177
messi1 0:607af852fb64 178 /**
messi1 0:607af852fb64 179 * Changes the low-pomer mode setting on the sensor
messi1 0:607af852fb64 180 *
messi1 0:607af852fb64 181 * @param mode dDsired new mode
messi1 0:607af852fb64 182 * @return New setting on the sensor
messi1 0:607af852fb64 183 */
messi1 1:38001f05dab3 184 uint8_t setLowPowerMode(uint8_t mode);
messi1 0:607af852fb64 185
messi1 0:607af852fb64 186
messi1 0:607af852fb64 187 /**
messi1 0:607af852fb64 188 * Reads the current averaging factor setting for a key
messi1 0:607af852fb64 189 *
messi1 0:607af852fb64 190 * @param key Key being read
messi1 0:607af852fb64 191 * @return Averaging factor
messi1 0:607af852fb64 192 */
messi1 0:607af852fb64 193 uint8_t getAVE(uint8_t key);
messi1 0:607af852fb64 194
messi1 0:607af852fb64 195 /**
messi1 0:607af852fb64 196 * Changes the averaging factor setting for a key
messi1 0:607af852fb64 197 *
messi1 0:607af852fb64 198 * @param key Key being changed
messi1 0:607af852fb64 199 * @param ave New averaging factor
messi1 0:607af852fb64 200 * @return New averaging factor as read from the device
messi1 0:607af852fb64 201 */
messi1 0:607af852fb64 202 uint8_t setAVE(uint8_t key, uint8_t ave);
messi1 0:607af852fb64 203
messi1 0:607af852fb64 204 /**
messi1 0:607af852fb64 205 * Reads the AKS group of which a key is part
messi1 0:607af852fb64 206 *
messi1 0:607af852fb64 207 * @param key Key (0-6) being queried
messi1 0:607af852fb64 208 * @return AKS group of which the key is part
messi1 0:607af852fb64 209 */
messi1 0:607af852fb64 210 uint8_t getAKSGroup(uint8_t key);
messi1 0:607af852fb64 211
messi1 0:607af852fb64 212 /**
messi1 0:607af852fb64 213 * Changes the AKS group of which a key is part
messi1 0:607af852fb64 214 *
messi1 0:607af852fb64 215 * @param key Key (0-6) being changed
messi1 0:607af852fb64 216 * @param group New group for the key
messi1 0:607af852fb64 217 * @return New value on the sensor
messi1 0:607af852fb64 218 */
messi1 0:607af852fb64 219 uint8_t setAKSGroup(uint8_t key, uint8_t group);
messi1 0:607af852fb64 220
messi1 0:607af852fb64 221 /**
messi1 0:607af852fb64 222 * Returns the overflow indicator
messi1 0:607af852fb64 223 *
messi1 0:607af852fb64 224 * @return True if overflow is indicated
messi1 0:607af852fb64 225 */
messi1 1:38001f05dab3 226 bool isOverflowed() { return _overflow; };
messi1 0:607af852fb64 227
messi1 0:607af852fb64 228 /**
messi1 0:607af852fb64 229 * Returns the calibrating indicator
messi1 0:607af852fb64 230 *
messi1 0:607af852fb64 231 * @return True if calibration is in progress
messi1 0:607af852fb64 232 */
messi1 0:607af852fb64 233 bool isCalibrating() { return _calibrating; };
messi1 0:607af852fb64 234
messi1 0:607af852fb64 235 /**
messi1 0:607af852fb64 236 * Issues a reset command
messi1 0:607af852fb64 237 *
messi1 0:607af852fb64 238 * @return True if successful
messi1 0:607af852fb64 239 */
messi1 0:607af852fb64 240 bool reset();
messi1 0:607af852fb64 241
messi1 0:607af852fb64 242 /**
messi1 0:607af852fb64 243 * Issues a calibrate command
messi1 0:607af852fb64 244 *
messi1 0:607af852fb64 245 * @return True if successful
messi1 0:607af852fb64 246 */
messi1 0:607af852fb64 247 bool calibrate();
messi1 0:607af852fb64 248
messi1 0:607af852fb64 249 /**
messi1 0:607af852fb64 250 * Gets the current button states
messi1 0:607af852fb64 251 *
messi1 0:607af852fb64 252 * @returns Button states
messi1 0:607af852fb64 253 */
messi1 1:38001f05dab3 254 uint8_t getButtonsState();
messi1 1:38001f05dab3 255
messi1 1:38001f05dab3 256 bool isButtonPressed(const uint8_t button);
messi1 1:38001f05dab3 257
messi1 1:38001f05dab3 258 private:
messi1 1:38001f05dab3 259 /**
messi1 1:38001f05dab3 260 * Writes a byte value into the register
messi1 1:38001f05dab3 261 *
messi1 1:38001f05dab3 262 * @param reg Register location to write into
messi1 1:38001f05dab3 263 * @param byte Byte to write
messi1 1:38001f05dab3 264 * @return True if successful
messi1 1:38001f05dab3 265 */
messi1 1:38001f05dab3 266 bool writeByte(uint8_t reg, uint8_t byte);
messi1 1:38001f05dab3 267
messi1 1:38001f05dab3 268 /**
messi1 1:38001f05dab3 269 * Writes a word value into the register. Note: the device must have the
messi1 1:38001f05dab3 270 * auto-increment bit set in the MODE1 register to work.
messi1 1:38001f05dab3 271 *
messi1 1:38001f05dab3 272 * @param reg Register location to write into
messi1 1:38001f05dab3 273 * @param word Word to write
messi1 1:38001f05dab3 274 * @return True if successful
messi1 1:38001f05dab3 275 */
messi1 1:38001f05dab3 276 bool writeWord(uint8_t reg, uint16_t word);
messi1 1:38001f05dab3 277
messi1 1:38001f05dab3 278 /**
messi1 1:38001f05dab3 279 * Read a byte value from the register
messi1 1:38001f05dab3 280 *
messi1 1:38001f05dab3 281 * @param reg Register location to read from
messi1 1:38001f05dab3 282 * @return Value in the specified register
messi1 1:38001f05dab3 283 */
messi1 1:38001f05dab3 284 uint8_t readByte(uint8_t reg);
messi1 1:38001f05dab3 285
messi1 1:38001f05dab3 286 /**
messi1 1:38001f05dab3 287 * Read a word value from the register. Note: the device must have the
messi1 1:38001f05dab3 288 * auto-increment bit set in the MODE1 register to work.
messi1 1:38001f05dab3 289 *
messi1 1:38001f05dab3 290 * @param reg Register location to read from
messi1 1:38001f05dab3 291 * @return Value in the specified register
messi1 1:38001f05dab3 292 */
messi1 1:38001f05dab3 293 uint16_t readWord(uint8_t reg);
messi1 0:607af852fb64 294
messi1 0:607af852fb64 295 private:
messi1 0:607af852fb64 296 uint8_t _buttonStates;
messi1 0:607af852fb64 297 bool _calibrating;
messi1 0:607af852fb64 298 bool _overflow;
messi1 0:607af852fb64 299
messi1 1:38001f05dab3 300 mbed::I2C _i2cPort;
messi1 0:607af852fb64 301 uint8_t _addr;
messi1 0:607af852fb64 302 };