Charith Dassanayake / CPPToPigpio - Testing Programs
Revision:
0:9a49249d42b3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MPR121/MPR121.cc	Wed Dec 13 00:06:47 2017 -0500
@@ -0,0 +1,223 @@
+#ifndef CPP_TO_PIGPIO_I2C_HEADER
+#include <CPPToPigpio.h>
+#endif
+#include <sstream>
+#include <string>
+#include <list>
+ 
+#include "MPR121.h"
+Mpr121::Mpr121(CPPToPigpio::I2C *i2c, Address i2cAddress)
+{
+    this->i2c = i2c;
+    
+    address = i2cAddress;
+           
+    // Configure the MPR121 settings to default
+    this->configureSettings();
+}
+ 
+   
+void Mpr121::configureSettings()
+{
+    // Put the MPR into setup mode
+    this->write(ELE_CFG,0x00);
+    
+    // Electrode filters for when data is > baseline
+    unsigned char gtBaseline[] = {
+         0x01,  //MHD_R
+         0x01,  //NHD_R 
+         0x00,  //NCL_R
+         0x00   //FDL_R
+         };
+         
+    writeMany(MHD_R,gtBaseline,4);   
+                 
+     // Electrode filters for when data is < baseline   
+     unsigned char ltBaseline[] = {
+        0x01,   //MHD_F
+        0x01,   //NHD_F
+        0xFF,   //NCL_F
+        0x02    //FDL_F
+        };
+        
+    writeMany(MHD_F,ltBaseline,4);
+        
+    // Electrode touch and release thresholds
+    unsigned char electrodeThresholds[] = {
+        E_THR_T, // Touch Threshhold
+        E_THR_R  // Release Threshold
+        };
+ 
+    for(int i=0; i<12; i++){
+        int result = writeMany((ELE0_T+(i*2)),electrodeThresholds,2);
+    }   
+ 
+    // Proximity Settings
+    unsigned char proximitySettings[] = {
+        0xff,   //MHD_Prox_R
+        0xff,   //NHD_Prox_R
+        0x00,   //NCL_Prox_R
+        0x00,   //FDL_Prox_R
+        0x01,   //MHD_Prox_F
+        0x01,   //NHD_Prox_F
+        0xFF,   //NCL_Prox_F
+        0xff,   //FDL_Prox_F
+        0x00,   //NHD_Prox_T
+        0x00,   //NCL_Prox_T
+        0x00    //NFD_Prox_T
+        };
+    writeMany(MHDPROXR,proximitySettings,11);
+ 
+    unsigned char proxThresh[] = {
+        PROX_THR_T, // Touch Threshold
+        PROX_THR_R  // Release Threshold
+        };
+    writeMany(EPROXTTH,proxThresh,2); 
+       
+    this->write(FIL_CFG,0x04);
+    
+    // Set the electrode config to transition to active mode
+    this->write(ELE_CFG,0x0c);
+}
+ 
+void Mpr121::setElectrodeThreshold(int electrode, unsigned char touch, unsigned char release){
+    
+    if(electrode > 11) return;
+    
+    // Get the current mode
+    unsigned char mode = this->read(ELE_CFG);
+    
+    // Put the MPR into setup mode
+    this->write(ELE_CFG,0x00);
+    
+    // Write the new threshold
+    this->write((ELE0_T+(electrode*2)), touch);
+    this->write((ELE0_T+(electrode*2)+1), release);
+    
+    //Restore the operating mode
+    this->write(ELE_CFG, mode);
+}
+    
+    
+unsigned char Mpr121::read(int key){
+ 
+    unsigned int data[2];
+    char out[2]={0};
+    char cmd[] = {CPPToPigpio::I2C::Start, CPPToPigpio::I2C::Addr, address, CPPToPigpio::I2C::Write,
+		0x01, key, CPPToPigpio::I2C::Start, CPPToPigpio::I2C::Read,0x02, CPPToPigpio::I2C::Stop, 
+		CPPToPigpio::I2C::End};
+	int ack = i2c->bang(cmd, 11, out, 2, false);
+	//printf("ack_read: %d", ack);
+    //Start the command
+    /*i2c->start();
+	
+    // Address the target (Write mode)
+    int ack1= i2c->write(address);
+ 
+    // Set the register key to read
+    int ack2 = i2c->write(key);
+ 
+    // Re-start for read of data
+    i2c->start();
+ 
+    // Re-send the target address in read mode
+    int ack3 = i2c->write(address+1);
+ 
+    // Read in the result
+    data[0] = i2c->read(0); 
+ 
+    // Reset the bus        
+    i2c->stop();*/
+	data[0] = out[0];
+    return (unsigned char)data[0];
+}
+ 
+ 
+int Mpr121::write(int key, unsigned char value){
+    
+    char out[1]={0};
+    char keyc = static_cast<char>(key);
+    char valc = static_cast<char>(value);
+    char cmd[] = {CPPToPigpio::I2C::Start, CPPToPigpio::I2C::Addr, address, CPPToPigpio::I2C::Write,
+		0x02, keyc, valc, CPPToPigpio::I2C::Stop, CPPToPigpio::I2C::End};
+	int ack = i2c->bang(cmd, 9, out, 1, false);
+    //printf("ack_write: %d", ack);
+    //Start the command
+    /*i2c->start();
+ 
+    // Address the target (Write mode)
+    int ack1= i2c->write(address);
+ 
+    // Set the register key to write
+    int ack2 = i2c->write(key);
+ 
+    // Read in the result
+    int ack3 = i2c->write(value); 
+ 
+    // Reset the bus        
+    i2c->stop();*/
+    
+    return ack-1;
+}
+ 
+ 
+int Mpr121::writeMany(int start, unsigned char* dataSet, int length){
+    //Start the command
+    /*i2c->start();
+ 
+    // Address the target (Write mode)
+    int ack= i2c->write(address);
+    if(ack!=1){
+        return -1;
+    }
+    
+    // Set the register key to write
+    ack = i2c->write(start);
+    if(ack!=1){
+        return -1;
+    }
+ 
+    // Write the date set
+    int count = 0;
+    while(ack==1 && (count < length)){
+        ack = i2c->write(dataSet[count]);
+        count++;
+    } 
+    // Stop the cmd
+    i2c->stop();*/
+    
+    char datasetb[length+1] = {0};
+    datasetb[0] = start;
+    for (int i=0; i<length; i++){
+		datasetb[i+1]=static_cast<char>(dataSet[i]);
+		//printf("a: %x, b :%x\n", dataSet[i], datasetb[i+1]);
+	}
+	//printf("here\n");
+	int count = i2c->write(address, datasetb, length+1, false);
+    
+    
+    
+    return count;
+}
+      
+ 
+bool Mpr121::getProximityMode(){
+    if(this->read(ELE_CFG) > 0x0c)
+        return true;
+    else
+        return false;
+}
+ 
+void Mpr121::setProximityMode(bool mode){
+    this->write(ELE_CFG,0x00);
+    if(mode){
+        this->write(ELE_CFG,0x30); //Sense proximity from ALL pads
+    } else {
+        this->write(ELE_CFG,0x0c); //Sense touch, all 12 pads active.
+    }
+}
+ 
+ 
+int Mpr121::readTouchData(){
+    return this->read(0x00);
+}