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.
mpr121.cpp
00001 /* 00002 Copyright (c) 2011 Anthony Buckton (abuckton [at] blackink [dot} net {dot} au) 00003 00004 Permission is hereby granted, free of charge, to any person obtaining a copy 00005 of this software and associated documentation files (the "Software"), to deal 00006 in the Software without restriction, including without limitation the rights 00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 copies of the Software, and to permit persons to whom the Software is 00009 furnished to do so, subject to the following conditions: 00010 00011 The above copyright notice and this permission notice shall be included in 00012 all copies or substantial portions of the Software. 00013 00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 THE SOFTWARE. 00021 */ 00022 00023 #include <mbed.h> 00024 #include <sstream> 00025 #include <string> 00026 #include <list> 00027 00028 #include <mpr121.h> 00029 00030 Mpr121::Mpr121(I2C *i2c, Address i2cAddress) 00031 { 00032 this->i2c = i2c; 00033 00034 address = i2cAddress; 00035 00036 // Configure the MPR121 settings to default 00037 00038 this->configureSettings(); 00039 } 00040 00041 00042 void Mpr121::configureSettings() 00043 { 00044 // Put the MPR into setup mode 00045 this->write(ELE_CFG,0x00); 00046 00047 // Electrode filters for when data is > baseline 00048 unsigned char gtBaseline[] = { 00049 0x01, //MHD_R 00050 0x01, //NHD_R 00051 0x00, //NCL_R 00052 0x00 //FDL_R 00053 }; 00054 00055 writeMany(MHD_R,gtBaseline,4); 00056 00057 // Electrode filters for when data is < baseline 00058 unsigned char ltBaseline[] = { 00059 0x01, //MHD_F 00060 0x01, //NHD_F 00061 0xFF, //NCL_F 00062 0x02 //FDL_F 00063 }; 00064 00065 writeMany(MHD_F,ltBaseline,4); 00066 00067 // Electrode touch and release thresholds 00068 unsigned char electrodeThresholds[] = { 00069 E_THR_T, // Touch Threshhold 00070 E_THR_R // Release Threshold 00071 }; 00072 00073 for(int i=0; i<12; i++){ 00074 int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2); 00075 } 00076 00077 // Proximity Settings 00078 unsigned char proximitySettings[] = { 00079 0xff, //MHD_Prox_R 00080 0xff, //NHD_Prox_R 00081 0x00, //NCL_Prox_R 00082 0x00, //FDL_Prox_R 00083 0x01, //MHD_Prox_F 00084 0x01, //NHD_Prox_F 00085 0xFF, //NCL_Prox_F 00086 0xff, //FDL_Prox_F 00087 0x00, //NHD_Prox_T 00088 0x00, //NCL_Prox_T 00089 0x00 //NFD_Prox_T 00090 }; 00091 writeMany(MHDPROXR,proximitySettings,11); 00092 00093 unsigned char proxThresh[] = { 00094 PROX_THR_T, // Touch Threshold 00095 PROX_THR_R // Release Threshold 00096 }; 00097 writeMany(EPROXTTH,proxThresh,2); 00098 00099 this->write(FIL_CFG,0x04); 00100 00101 // Set the electrode config to transition to active mode 00102 this->write(ELE_CFG,0x0c); 00103 } 00104 00105 void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){ 00106 00107 if(electrode > 11) return; 00108 00109 // Get the current mode 00110 unsigned char mode = this->read(ELE_CFG); 00111 00112 // Put the MPR into setup mode 00113 this->write(ELE_CFG,0x00); 00114 00115 // Write the new threshold 00116 this->write((ELE0_T+(electrode*2)), touch); 00117 this->write((ELE0_T+(electrode*2)+1), release); 00118 00119 //Restore the operating mode 00120 this->write(ELE_CFG, mode); 00121 } 00122 00123 00124 unsigned char Mpr121::read(int key){ 00125 00126 unsigned char data[2]; 00127 00128 //Start the command 00129 i2c->start(); 00130 00131 // Address the target (Write mode) 00132 int ack1= i2c->write(address); 00133 00134 // Set the register key to read 00135 int ack2 = i2c->write(key); 00136 00137 // Re-start for read of data 00138 i2c->start(); 00139 00140 // Re-send the target address in read mode 00141 int ack3 = i2c->write(address+1); 00142 00143 // Read in the result 00144 data[0] = i2c->read(0); 00145 00146 // Reset the bus 00147 i2c->stop(); 00148 00149 return data[0]; 00150 } 00151 00152 00153 int Mpr121::write(int key, unsigned char value){ 00154 00155 //Start the command 00156 i2c->start(); 00157 00158 // Address the target (Write mode) 00159 int ack1= i2c->write(address); 00160 00161 // Set the register key to write 00162 int ack2 = i2c->write(key); 00163 00164 // Read in the result 00165 int ack3 = i2c->write(value); 00166 00167 // Reset the bus 00168 i2c->stop(); 00169 00170 return (ack1+ack2+ack3)-3; 00171 } 00172 00173 00174 int Mpr121::writeMany(int start, unsigned char* dataSet, int length){ 00175 //Start the command 00176 i2c->start(); 00177 00178 // Address the target (Write mode) 00179 int ack= i2c->write(address); 00180 if(ack!=1){ 00181 return -1; 00182 } 00183 00184 // Set the register key to write 00185 ack = i2c->write(start); 00186 if(ack!=1){ 00187 return -1; 00188 } 00189 00190 // Write the date set 00191 int count = 0; 00192 while(ack==1 && (count < length)){ 00193 ack = i2c->write(dataSet[count]); 00194 count++; 00195 } 00196 // Stop the cmd 00197 i2c->stop(); 00198 00199 return count; 00200 } 00201 00202 00203 bool Mpr121::getProximityMode(){ 00204 if(this->read(ELE_CFG) > 0x0c) 00205 return true; 00206 else 00207 return false; 00208 } 00209 00210 void Mpr121::setProximityMode(bool mode){ 00211 this->write(ELE_CFG,0x00); 00212 if(mode){ 00213 this->write(ELE_CFG,0x30); //Sense proximity from ALL pads 00214 } else { 00215 this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active. 00216 } 00217 } 00218 00219 00220 int Mpr121::readTouchData(){ 00221 return this->read(0x00); 00222 }
Generated on Wed Jul 13 2022 18:08:25 by
1.7.2