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