FlexBook / Mbed 2 deprecated FlexBook171204a

Dependencies:   SDFileSystem app epson mbed msp430 pl tests

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers at42qt2120.h Source File

at42qt2120.h

00001 //
00002 // Filename: at42qt2120.h
00003 //
00004 // Atmel AT42QT2120 12 channel touch sensor.
00005 //
00006 
00007 #ifndef AT42QT2120_H
00008 #define AT42QT2120_H
00009 
00010 #include "mbed.h"
00011 
00012 namespace HAL {
00013 
00014 /**
00015  * @brief AT42QT2120 registers.
00016  */
00017 enum REG_AT42QT2120
00018 {
00019 QT_CHIP_ID = 0,
00020 QT_FIRMWARE_VERSION,
00021 QT_DETECTION_STATUS,
00022 QT_KEY_STATUS,
00023 QT_KEY_STATUS2,
00024 QT_SLIDER_POSITION = 5,
00025 QT_CALIBRATE,
00026 QT_RESET,
00027 QT_LP,
00028 QT_TTD,
00029 QT_ATD,
00030 QT_DI,
00031 QT_TRD,
00032 QT_DHT,
00033 QT_SLIDER_OPTION,
00034 QT_CHARDE_TIME,
00035 QT_KEY0_DTHR,
00036 QT_KEY1_DTHR,
00037 QT_KEY2_DTHR,
00038 QT_KEY3_DTHR,
00039 QT_KEY4_DTHR,
00040 QT_KEY5_DTHR,
00041 QT_KEY6_DTHR,
00042 QT_KEY7_DTHR,
00043 QT_KEY8_DTHR,
00044 QT_KEY9_DTHR,
00045 QT_KEY10_DTHR,
00046 QT_KEY11_DTHR,  
00047 QT_KEY0_CTRL,
00048 QT_KEY1_CTRL,
00049 QT_KEY2_CTRL,
00050 QT_KEY3_CTRL,
00051 QT_KEY4_CTRL,
00052 QT_KEY5_CTRL,
00053 QT_KEY6_CTRL,
00054 QT_KEY7_CTRL,
00055 QT_KEY8_CTRL,
00056 QT_KEY9_CTRL,
00057 QT_KEY10_CTRL,
00058 QT_KEY11_CTRL,  
00059 QT_KEY0_PULSE_SCALE,
00060 QT_KEY1_PULSE_SCALE,
00061 QT_KEY2_PULSE_SCALE,
00062 QT_KEY3_PULSE_SCALE,
00063 QT_KEY4_PULSE_SCALE,
00064 QT_KEY5_PULSE_SCALE,
00065 QT_KEY6_PULSE_SCALE,
00066 QT_KEY7_PULSE_SCALE,
00067 QT_KEY8_PULSE_SCALE,
00068 QT_KEY9_PULSE_SCALE,
00069 QT_KEY10_PULSE_SCALE,
00070 QT_KEY11_PULSE_SCALE,
00071 QT_KEY0_SIGNAL,
00072 QT_KEY1_SIGNAL = 54,
00073 QT_KEY2_SIGNAL = 56,
00074 QT_KEY3_SIGNAL = 58,
00075 QT_KEY4_SIGNAL = 60,
00076 QT_KEY5_SIGNAL = 62,
00077 QT_KEY6_SIGNAL = 64,
00078 QT_KEY7_SIGNAL = 66,
00079 QT_KEY8_SIGNAL = 68,
00080 QT_KEY9_SIGNAL = 70,
00081 QT_KEY10_SIGNAL = 72,
00082 QT_KEY11_SIGNAL = 74,   
00083 QT_KEY0_REFERENCE = 76,
00084 QT_KEY1_REFERENCE = 78,
00085 QT_KEY2_REFERENCE = 80,
00086 QT_KEY3_REFERENCE = 82,
00087 QT_KEY4_REFERENCE = 84,
00088 QT_KEY5_REFERENCE = 86,
00089 QT_KEY6_REFERENCE = 88,
00090 QT_KEY7_REFERENCE = 90,
00091 QT_KEY8_REFERENCE = 92,
00092 QT_KEY9_REFERENCE = 94,
00093 QT_KEY10_REFERENCE = 96,
00094 QT_KEY11_REFERENCE = 98,
00095 };
00096 
00097 enum SLIDERMODE_AT42QT2120
00098 {
00099 NONE,
00100 SLIDER,
00101 WHEEL
00102 };
00103 
00104 // Control register bit positions.
00105 const uint8_t CTRL_EN = (1 << 0);
00106 const uint8_t CTRL_GPO = (1 << 1);
00107 const uint8_t CTRL_GUARD = (1 << 4);
00108     
00109 const int KEY0  = (1 << 0);
00110 const int KEY1  = (1 << 1);
00111 const int KEY2  = (1 << 2);
00112 const int KEY3  = (1 << 3);
00113 const int KEY4  = (1 << 4);
00114 const int KEY5  = (1 << 5);
00115 const int KEY6  = (1 << 6);
00116 const int KEY7  = (1 << 7);
00117 const int KEY8  = (1 << 8);
00118 const int KEY9  = (1 << 9);
00119 const int KEY10 = (1 << 10);
00120 const int KEY11 = (1 << 11);
00121 
00122 const uint8_t TDET = (1 << 0);
00123 const uint8_t SDET = (1 << 1);
00124 
00125 /**
00126  * @brief Atmel AT42QT2120 encapsulation.
00127  * This chip is a 12 channel touch sensor.
00128  *
00129  * Example usage:
00130  <pre>
00131     I2C i2c(p28, p27);
00132     AT42QT2120 at42qt2120(i2c);
00133     ...
00134     at42qt2120.Write(QT_RESET, 0);
00135  </pre>
00136  *
00137  * The AT42QT2120 uses pins 28 and 27 (SDA and SCL) for communication
00138  * and the write command resets the chip.
00139  *
00140  * The chip address of the AT42QT2120 is fixed at 0x1c.
00141  *
00142  * Note that this code example does not cover the setup of the chip
00143  * configuration registers.
00144  */
00145 class AT42QT2120
00146 {
00147 public:
00148     /**
00149      * @brief Constructor.
00150      * @param I2C the I2C to use for communication.
00151      */
00152     AT42QT2120(I2C &i2c);
00153 
00154     /**
00155      * @brief Calibrate any enabled touch sensors.
00156      * @return True if calibration was successfull.
00157      */
00158     bool Calibrate();
00159 
00160     /**
00161      * @brief Set the key control bits.
00162      * See section 5.17 of the datasheet for more information.
00163      * All keys are initially disabled in the constructor so this
00164      * function must be called to configure the keys before use.
00165      * After calling this function you should call Calibrate().
00166      * @param key The key to configure.
00167      * @param guard Set to true if this is a guard channel.
00168      * @param group The key group (AKS) number.
00169      * @param gpu Set to true for high output.
00170      * @param enable Enable this key. Unused keys should be disabled.
00171      */
00172     bool SetKeyControl(uint8_t key, bool guard, uint8_t group, bool gpo, bool enable);
00173 
00174     /**
00175      * @brief Set the slider options.
00176      * @param Set to SLIDER, WHEEL or NONE to set the required mode.
00177      */
00178     void SetSliderOptions(SLIDERMODE_AT42QT2120 mode);
00179 
00180     /**
00181      * @brief Touch sensor status.
00182      */
00183     struct Status
00184     {
00185         /**
00186          * @brief True if the keys have changed.
00187          */
00188         bool     keyschanged;
00189 
00190         /**
00191          * @brief The key change bit mask.
00192          * Use KEYn to extract the individual key status.
00193          */
00194         uint16_t keys;
00195 
00196         /**
00197          * @brief True if the slider has changed.
00198          */
00199         bool     sliderchanged;
00200 
00201         /**
00202          * @brief The slider position.
00203          */
00204         bool     slider;
00205     };
00206 
00207     /**
00208      * @brief Read the status of the touch sensor.
00209      * @param status Pointer to hold the status information.
00210      * @return True if the transfer completes successfully.
00211      */
00212     bool ReadStatus(Status &status);
00213 
00214     /**
00215      * @brief Read a register.
00216      * @param reg The register to read.
00217      * @return The register value.
00218      */
00219     int Read(REG_AT42QT2120 reg);
00220 
00221     /**
00222      * @brief Write a register.
00223      * @param reg The register to write.
00224      * @param value The value to write to the register.
00225      * @return True on success.
00226      */
00227     bool Write(REG_AT42QT2120 reg, uint8_t value);
00228 
00229 private:
00230     I2C &i2c;
00231     Status status;
00232 };
00233 
00234 } // End HAL namespace.
00235 
00236 #endif // MCP23S17_H