Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem app epson mbed msp430 pl tests
at42qt2120.cpp
00001 // 00002 // Filename: at42qt2120.cpp 00003 // 00004 // Atmel AT42QT2120 12 channel touch sensor. 00005 // 00006 00007 #include "at42qt2120.h" 00008 00009 #include "log.h" 00010 00011 namespace HAL { 00012 00013 // This chip has a singe fixed I2C address. 00014 const int ADDRESS = 0x1c; 00015 00016 // Chip Id hardcoded in the silicon. 00017 const int AT42QT2120_ID = 0x3e; 00018 00019 AT42QT2120::AT42QT2120(I2C &i2cin) 00020 : i2c(i2cin) 00021 { 00022 #ifdef VERBOSE 00023 if(Read((REG_AT42QT2120) QT_CHIP_ID) != AT42QT2120_ID) 00024 Log("AT42QT2120 bad chip id"); 00025 else 00026 Log("AT42QT2120 chip id: Ok"); 00027 00028 char version[20]; 00029 int ver = Read((REG_AT42QT2120) QT_FIRMWARE_VERSION); 00030 sprintf(version, "AT42QT2120 version: %u.%u", ver >> 4, ver & 0x0f); 00031 Log(version); 00032 #endif 00033 00034 for(uint8_t key = 0; key < 12; key++) 00035 SetKeyControl(key, false, 0, false, false); 00036 00037 Calibrate(); 00038 } 00039 00040 bool AT42QT2120::ReadStatus(Status &status) 00041 { 00042 char buffer[4]; 00043 00044 buffer[0] = Read(QT_DETECTION_STATUS); 00045 buffer[1] = Read(QT_KEY_STATUS); 00046 buffer[2] = Read(QT_KEY_STATUS2); 00047 buffer[3] = Read(QT_SLIDER_POSITION); 00048 00049 #ifdef VERBOSE 00050 char msg[32]; 00051 sprintf(msg, "Touch: %x %x %x %x", buffer[0], buffer[1], buffer[2], buffer[3]); 00052 #endif 00053 00054 status.keyschanged = (buffer[0] & TDET) != 0; 00055 status.keys = buffer[2]; 00056 status.keys = status.keys << 8; 00057 status.keys += buffer[1]; 00058 00059 status.sliderchanged = (buffer[0] & SDET) != 0; 00060 status.slider = buffer[3]; 00061 00062 return true; 00063 } 00064 00065 bool AT42QT2120::Calibrate() 00066 { 00067 #ifdef VERBOSE 00068 Log("AT42QT2120 calibration start"); 00069 #endif 00070 00071 bool status = Write(QT_CALIBRATE, 0xff); 00072 if(status) 00073 { 00074 while(Read(QT_DETECTION_STATUS) & 0x80); 00075 } 00076 00077 #ifdef VERBOSE 00078 if(status) 00079 Log("AT42QT2120 calibration finish"); 00080 else 00081 Log("AT42QT2120 calibration fail"); 00082 #endif 00083 return status; 00084 } 00085 00086 bool AT42QT2120::SetKeyControl(uint8_t key, bool guard, uint8_t group, bool gpo, bool enable) 00087 { 00088 bool status = false; 00089 00090 if(key < 12 && group < 4) 00091 { 00092 uint8_t config = 0; 00093 if(!enable) 00094 config |= CTRL_EN; 00095 if(gpo) 00096 config |= CTRL_GPO; 00097 if(group) 00098 config |= group << 2; 00099 if(guard) 00100 config |= CTRL_GUARD; 00101 00102 uint8_t reg = key + 28; // 28 = QT_KEY0_CTRL 00103 status = Write((HAL::REG_AT42QT2120) reg, config); 00104 } 00105 00106 return status; 00107 } 00108 00109 void AT42QT2120::SetSliderOptions(SLIDERMODE_AT42QT2120 mode) 00110 { 00111 switch(mode) 00112 { 00113 case SLIDER: 00114 Write(QT_SLIDER_OPTION, 0x80); 00115 break; 00116 00117 case WHEEL: 00118 Write(QT_SLIDER_OPTION, 0xc0); 00119 break; 00120 00121 default: 00122 Write(QT_SLIDER_OPTION, 0x0); 00123 break; 00124 } 00125 } 00126 00127 int AT42QT2120::Read(REG_AT42QT2120 reg) 00128 { 00129 if(i2c.write(ADDRESS << 1, (const char *) ®, 1, true)) 00130 { 00131 i2c.stop(); 00132 return 0; 00133 } 00134 00135 char data[2]; 00136 i2c.read(ADDRESS << 1, data, 1); 00137 00138 return data[0]; 00139 } 00140 00141 bool AT42QT2120::Write(REG_AT42QT2120 reg, uint8_t value) 00142 { 00143 uint8_t regnum = reg; 00144 const char data[2] = { regnum, value }; 00145 00146 return i2c.write(ADDRESS << 1, data, 2) == 0; 00147 } 00148 00149 } // End HAL namespace. 00150
Generated on Tue Jul 12 2022 21:14:34 by
