Eric Xu / Mbed 2 deprecated Arcade_BlueO

Dependencies:   4DGL-uLCD-SE PinDetect SDFileSystem mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mpr121.cpp Source File

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 // EDITED CODE
00031 Mpr121::Mpr121()
00032 {
00033 }    
00034     
00035 Mpr121::Mpr121(I2C *i2c, Address i2cAddress)
00036 {
00037     this->i2c = i2c;
00038     
00039     address = i2cAddress;
00040            
00041     // Configure the MPR121 settings to default
00042     this->configureSettings();
00043 }
00044 
00045    
00046 void Mpr121::configureSettings()
00047 {
00048     // Put the MPR into setup mode
00049     this->write(ELE_CFG,0x00);
00050     
00051     // Electrode filters for when data is > baseline
00052     unsigned char gtBaseline[] = {
00053          0x01,  //MHD_R
00054          0x01,  //NHD_R 
00055          0x00,  //NCL_R
00056          0x00   //FDL_R
00057          };
00058          
00059     writeMany(MHD_R,gtBaseline,4);   
00060                  
00061      // Electrode filters for when data is < baseline   
00062      unsigned char ltBaseline[] = {
00063         0x01,   //MHD_F
00064         0x01,   //NHD_F
00065         0xFF,   //NCL_F
00066         0x02    //FDL_F
00067         };
00068         
00069     writeMany(MHD_F,ltBaseline,4);
00070         
00071     // Electrode touch and release thresholds
00072     unsigned char electrodeThresholds[] = {
00073         E_THR_T, // Touch Threshhold
00074         E_THR_R  // Release Threshold
00075         };
00076 
00077     for(int i=0; i<12; i++){
00078         int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2);
00079     }   
00080 
00081     // Proximity Settings
00082     unsigned char proximitySettings[] = {
00083         0xff,   //MHD_Prox_R
00084         0xff,   //NHD_Prox_R
00085         0x00,   //NCL_Prox_R
00086         0x00,   //FDL_Prox_R
00087         0x01,   //MHD_Prox_F
00088         0x01,   //NHD_Prox_F
00089         0xFF,   //NCL_Prox_F
00090         0xff,   //FDL_Prox_F
00091         0x00,   //NHD_Prox_T
00092         0x00,   //NCL_Prox_T
00093         0x00    //NFD_Prox_T
00094         };
00095     writeMany(MHDPROXR,proximitySettings,11);
00096 
00097     unsigned char proxThresh[] = {
00098         PROX_THR_T, // Touch Threshold
00099         PROX_THR_R  // Release Threshold
00100         };
00101     writeMany(EPROXTTH,proxThresh,2); 
00102        
00103     this->write(FIL_CFG,0x04);
00104     
00105     // Set the electrode config to transition to active mode
00106     this->write(ELE_CFG,0x0c);
00107 }
00108 
00109 void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){
00110     
00111     if(electrode > 11) return;
00112     
00113     // Get the current mode
00114     unsigned char mode = this->read(ELE_CFG);
00115     
00116     // Put the MPR into setup mode
00117     this->write(ELE_CFG,0x00);
00118     
00119     // Write the new threshold
00120     this->write((ELE0_T+(electrode*2)), touch);
00121     this->write((ELE0_T+(electrode*2)+1), release);
00122     
00123     //Restore the operating mode
00124     this->write(ELE_CFG, mode);
00125 }
00126     
00127     
00128 unsigned char Mpr121::read(int key){
00129 
00130     unsigned char data[2];
00131     
00132     //Start the command
00133     i2c->start();
00134 
00135     // Address the target (Write mode)
00136     int ack1= i2c->write(address);
00137 
00138     // Set the register key to read
00139     int ack2 = i2c->write(key);
00140 
00141     // Re-start for read of data
00142     i2c->start();
00143 
00144     // Re-send the target address in read mode
00145     int ack3 = i2c->write(address+1);
00146 
00147     // Read in the result
00148     data[0] = i2c->read(0); 
00149 
00150     // Reset the bus        
00151     i2c->stop();
00152 
00153     return data[0];
00154 }
00155 
00156 
00157 int Mpr121::write(int key, unsigned char value){
00158     
00159     //Start the command
00160     i2c->start();
00161 
00162     // Address the target (Write mode)
00163     int ack1= i2c->write(address);
00164 
00165     // Set the register key to write
00166     int ack2 = i2c->write(key);
00167 
00168     // Read in the result
00169     int ack3 = i2c->write(value); 
00170 
00171     // Reset the bus        
00172     i2c->stop();
00173     
00174     return (ack1+ack2+ack3)-3;
00175 }
00176 
00177 
00178 int Mpr121::writeMany(int start, unsigned char* dataSet, int length){
00179     //Start the command
00180     i2c->start();
00181 
00182     // Address the target (Write mode)
00183     int ack= i2c->write(address);
00184     if(ack!=1){
00185         return -1;
00186     }
00187     
00188     // Set the register key to write
00189     ack = i2c->write(start);
00190     if(ack!=1){
00191         return -1;
00192     }
00193 
00194     // Write the date set
00195     int count = 0;
00196     while(ack==1 && (count < length)){
00197         ack = i2c->write(dataSet[count]);
00198         count++;
00199     } 
00200     // Stop the cmd
00201     i2c->stop();
00202     
00203     return count;
00204 }
00205       
00206 
00207 bool Mpr121::getProximityMode(){
00208     if(this->read(ELE_CFG) > 0x0c)
00209         return true;
00210     else
00211         return false;
00212 }
00213 
00214 void Mpr121::setProximityMode(bool mode){
00215     this->write(ELE_CFG,0x00);
00216     if(mode){
00217         this->write(ELE_CFG,0x30); //Sense proximity from ALL pads
00218     } else {
00219         this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active.
00220     }
00221 }
00222 
00223 
00224 int Mpr121::readTouchData(){
00225     return this->read(0x00);
00226 }
00227 
00228 // EDITED CODE
00229 Mpr121& Mpr121::operator=(const Mpr121 &MPR121) {
00230     i2c = MPR121.i2c;
00231     address = MPR121.address;
00232     return *this;
00233 }