Atmel AT42QT1070 Seven-channel QTouch® Touch Sensor IC

Files at this revision

API Documentation at this revision

Comitter:
mederic
Date:
Wed Jun 27 06:25:51 2018 +0000
Child:
1:324c2c093ab8
Commit message:
1st release

Changed in this revision

QT1070.cpp Show annotated file Show diff for this revision Revisions of this file
QT1070.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QT1070.cpp	Wed Jun 27 06:25:51 2018 +0000
@@ -0,0 +1,120 @@
+#include "QT1070.h"
+
+// Detection Status REG bits
+#define QT1070_BIT_CALIB    7
+#define QT1070_BIT_OVFLW    6
+#define QT1070_BIT_TOUCH    0
+
+// AVEAKS REG bits
+#define QT1070_BIT_AVE      2
+
+// FOMOGRD REG bits
+#define QT1070_BIT_FO       5
+#define QT1070_BIT_MAX_CAL  4
+#define QT1070_MSL_GUARD    0xF0
+
+QT1070::QT1070(I2C *i2c, PinName change):_i2c(i2c),_change(change){
+    reset();
+}
+
+char QT1070::key(void){
+    return read(REG_KEYSTAT);
+}
+ 
+bool QT1070::change(void){
+    if(_change.is_connected()){
+        return !_change;
+    }else{
+        return (read(REG_STATUS)&(1<<QT1070_BIT_TOUCH));
+    }     
+}
+
+unsigned short QT1070::signal(char key){
+    key%=KEY_NUM;
+    return readW(REG_KEYSIG0+(key<<1));
+}
+
+unsigned short QT1070::ref(char key){
+    key%=KEY_NUM;
+    return readW(REG_REFDAT0+(key<<1));
+}
+
+char QT1070::nthresh(char key, char val){
+    key%=KEY_NUM;
+    if (val){
+        write(REG_NTHR0+key,val);
+    }
+    return read(REG_NTHR0+key);
+}
+
+char QT1070::aksAve(char key, char group, char ave){
+    key%=KEY_NUM;
+    ave%=AVEMAX;
+    group%=AKSMAX;
+    write(REG_AVEAKS0+key,(ave<<QT1070_BIT_AVE)|(group));
+    return read(REG_AVEAKS0+key);
+}
+
+void QT1070::detectInt(char key, char val){
+    key%=KEY_NUM;
+    val |= 0x02;
+    write(REG_DI0+key,val);
+}
+
+void QT1070::foMaxCal(bool fo, bool maxcal){
+    char reg = read(REG_FOMOGRD);
+    reg &= ~((1<<QT1070_BIT_FO) | (1<<QT1070_BIT_MAX_CAL));
+    reg |= (fo<<QT1070_BIT_FO) | (maxcal<<QT1070_BIT_MAX_CAL);
+    write(REG_FOMOGRD, reg);
+}
+
+void QT1070::guard(char key){
+    key%=KEY_NUM;
+    char reg = read(REG_FOMOGRD);
+    reg &= QT1070_MSL_GUARD;
+    reg |= key;
+    write(REG_FOMOGRD ,reg);
+}
+
+void QT1070::lowpwr(unsigned short ms){
+    char reg  = (ms%2040)>>3;
+    write(REG_LOWPWR, reg);
+    
+}
+
+void QT1070::maxOnDuration(unsigned short ms){
+     write(REG_MAXOND , ms>>4);
+}
+
+void QT1070::calibrate(void){
+    write(REG_CALIB ,255);
+}
+
+void QT1070::reset(void){
+    write(REG_RESET ,255);
+}
+
+void QT1070::write(char reg, char data){
+    char buf[2] = {reg,data};
+    _i2c->write(I2C_ADDR, buf, 2);
+}
+
+void QT1070::writeW(char reg, unsigned short data){
+    char buf[3] = {reg,data>>8,data&0xff};
+    _i2c->write(I2C_ADDR, buf, 3);
+}
+
+char QT1070::read(char reg){
+    char ret;
+    _i2c->write(I2C_ADDR, &reg, 1);
+    _i2c->read(I2C_ADDR, &ret, 1);
+    return ret;  
+}
+
+unsigned short QT1070::readW(char reg){
+    char ret[2];
+    _i2c->write(I2C_ADDR, &reg, 1);
+    _i2c->read(I2C_ADDR, ret, 2);
+    return ret[0]<<8 | ret[1];  
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QT1070.h	Wed Jun 27 06:25:51 2018 +0000
@@ -0,0 +1,146 @@
+#ifndef QT1070_H
+#define QT1070_H
+
+#include <stdint.h>
+#include "mbed.h"
+
+/** AT42QT1070 class
+* Seven-channel QTouch® Touch Sensor Driver
+*
+* Example:
+* @code
+* #include "mbed.h"
+* #include "QT1070.h"
+*
+* DigitalOut led(LED1);
+* I2C i2c(PB_7,PB_6);
+* QT1070 touch(&i2c, PA_11);
+*
+* int main() {
+*     touch.guard(0);     
+*     touch.aksAve(5,0,0);    // Disable key5 AVE=0
+*     touch.aksAve(6,0,0);    // Disable key6 AVE=0
+*
+*     while(1) {
+*         if(touch.change()){
+*             led=!led;
+*             printf("Key %d!!!!\r\n", touch.key());
+*         }
+*     }
+* }
+* @endcode
+*/
+    
+class QT1070{
+    public:
+        enum state{
+            I2C_ADDR = 0x36,    //I2C adress 
+            KEY_NUM = 0x07,     //Number of channel
+            AVEMAX = 32,        //Averaging maximum
+            AKSMAX = 3          //Group maximum
+        };
+        
+        //Register map
+        enum reg{
+            REG_CHIPID  = 0,
+            REG_FIRMW   = 1,
+            REG_STATUS  = 2,
+            REG_KEYSTAT = 3,
+            REG_KEYSIG0 = 4,
+            REG_REFDAT0 = 18,
+            REG_NTHR0   = 32,
+            REG_AVEAKS0 = 39,
+            REG_DI0     = 46,
+            REG_FOMOGRD = 53,
+            REG_LOWPWR  = 54,
+            REG_MAXOND  = 55,
+            REG_CALIB   = 56,
+            REG_RESET   = 57   
+        };
+        
+        /** Create PCA9633 instance + config method
+        * @param *i2c initialized I2C bus to use
+        * @param change PinName if connected 
+        */
+        QT1070(I2C *i2c, PinName change=NC);
+         
+        /** Get key reg.
+        * @rerun 0 if not, 2^key if hit
+        */
+        char key(void);
+        
+        /** Check a change in status 
+        * @return true in change occurs
+        */
+        bool change(void);
+        
+        /** Get signal.
+        * @param key <0,6> key number
+        */
+        unsigned short signal(char key);
+        
+        /** Get reference.
+        * @param key <0,6> key number
+        */
+        unsigned short ref(char key);
+        
+        /** Set/Get negative threshold.
+        * @param key <0,6> key number
+        * @param val <1,255> (Getter if 0)
+        */
+        char nthresh(char key, char val=0);
+        
+        /** Set/Get adjacent supresssor group and averaging.
+        * @param key <0,6> key number
+        * @param group <0,3> adjacent key suppressor group of the key
+        * @param ave <1,31> averaged signal value
+        */
+        char aksAve(char key, char group=1, char ave=8);
+        
+        /** set DI.
+        * @param key <0,6> key number
+        * @param val <2,255> how measurement before confirm key hit
+        */
+        void detectInt(char key, char val);
+        
+        /** Set fastout and Maxcal.
+        * @param fo set the digital integrator of 4 for all channel
+        * @param maxcal if set only the key timeout was recalibrated   
+        */
+        void foMaxCal(bool fo, bool maxcal=false);
+        
+        /** Set a key to be guard channel (priority filtering).
+        * @param key <0,6> key number
+        */
+        void guard(char key);
+        
+        /** Set interval between key measurment to reduce pwr.
+        * @param ms <8,2040>[ms] in step of 8ms
+        */
+        void lowpwr(unsigned short ms);
+        
+        /** Set how long any key can be touch before it recalibrates itself.
+        * @param ms <0,40800>[ms] in step of 160ms 
+        */
+        void maxOnDuration(unsigned short ms);
+        
+        /** Trig a calibration cycle
+        */
+        void calibrate(void);
+        
+        /** Trig a RESET
+        */
+        void reset(void);
+    
+    protected:     
+        void write(char reg, char data);
+        void writeW(char reg, unsigned short data);
+        char read(char reg);
+        unsigned short readW(char reg);
+
+    private:
+        I2C *_i2c;
+        DigitalIn _change;
+};
+ 
+#endif