A device driver for the Freescale MPR121 capactive touch IC. Not optimized for any particular system, just a starting point to get the chip up in running in no time. Changes to registers init() method will tailor the library for end system use.

Dependents:   Seeed_Grove_I2C_Touch_Example MPR121_HelloWorld mbed_petbottle_holder_shikake test_DEV-10508 ... more

Datasheet:

http://cache.freescale.com/files/sensors/doc/data_sheet/MPR121.pdf

Information

Must add pull-ups to the I2C bus!!

Files at this revision

API Documentation at this revision

Comitter:
sam_grove
Date:
Fri Mar 29 21:32:38 2013 +0000
Parent:
1:cee45334b36a
Child:
3:828260f21de6
Commit message:
Updated documentation and added LogUtil class

Changed in this revision

MPR121.cpp Show annotated file Show diff for this revision Revisions of this file
MPR121.h Show annotated file Show diff for this revision Revisions of this file
--- a/MPR121.cpp	Thu Mar 07 23:57:27 2013 +0000
+++ b/MPR121.cpp	Fri Mar 29 21:32:38 2013 +0000
@@ -150,7 +150,7 @@
     for(int i=0; i<0x80; i++)
     {
         reg_val = MPR121::readRegister(i);
-        printf("Reg 0x%02x: 0x%02x \n", i, reg_val);
+        LOG("Reg 0x%02x: 0x%02x \n", i, reg_val);
     }
     
     return;
@@ -168,7 +168,7 @@
     
     if(0 != oor_val)
     {
-        error("%s %d: MPR121 OOR failure - 0x%04x\n", __FILE__, __LINE__, oor_val);
+        ERROR("MPR121 OOR failure - 0x%04x\n", oor_val);
     }
    
     _button = reg_val;
@@ -188,7 +188,7 @@
     
     if(0 != result)
     {
-        error("%s %d: I2c write failed\n", __FILE__, __LINE__);
+        ERROR("I2c write failed\n");
     }
     
     return;
@@ -196,6 +196,30 @@
 
 uint8_t MPR121::readRegister(uint8_t const reg) const
 {
+    // from https://github.com/mbedmicro/mbed/blob/master/libraries/tests/peripherals/MMA8451Q/MMA8451Q.cpp
+//    char t[1] = {reg};
+//    uint8_t data;
+//    _i2c->write(_i2c_addr, t, 1, true);
+//    _i2c->read(_i2c_addr, (char *)data, 1);
+//    return data;
+
+    // modified and still wont work - need a scope
+//    char buf[1] = {reg};
+//    uint8_t w_result = 0, r_result = 0;
+//    uint8_t data;
+//    
+//    __disable_irq();
+//    w_result = _i2c->write(_i2c_addr, buf, 1, true);
+//    r_result = _i2c->read(_i2c_addr, (char *)&data, 2);
+//    __enable_irq();
+//    
+//    if((0 != w_result) || (0 != r_result))
+//    {
+//        ERROR("I2c read failed: %d, %d\n", w_result, r_result);
+//    }
+//    
+//    return (uint8_t)data;
+    
     uint8_t result = 1, data = 0;
     
     __disable_irq(); // Tickers and other timebase events can jack up the I2C bus
@@ -212,7 +236,7 @@
     
     if(1 != result)
     {
-        error("%s %d: I2C read failed\n", __FILE__, __LINE__);
+        ERROR("I2C read failed\n");
     }
     
     return data;
--- a/MPR121.h	Thu Mar 07 23:57:27 2013 +0000
+++ b/MPR121.h	Fri Mar 29 21:32:38 2013 +0000
@@ -24,42 +24,52 @@
 #define MPR121_H
 
 #include "mbed.h"
+#include "LogUtil.h"
 
 /** Using the Sparkfun SEN-10250
  *
  * Example:
  * @code
- *  #include "mbed.h"
- *  #include "MPR121.h"
- *
- *  DigitalOut myled(LED1);
- *  Serial pc(USBTX, USBRX);
+ * #include "mbed.h"
+ * #include "MPR121.h"
+ * 
+ * // TODO: put IC in low power mode when disabled
  * 
- *  I2C i2c(p28, p27);
- *  InterruptIn irq(p26);
- *  MPR121 touch_pad(i2c, irq, MPR121::ADDR_VSS);
+ * DigitalOut myled(LED1);
+ * DigitalOut off(LED4);
+ * Timer t;
+ * 
+ * LogUtil logger;
+ * 
+ * I2C i2c(p28, p27);
+ * InterruptIn irq(p26);
+ * MPR121 touch_pad(i2c, irq, MPR121::ADDR_VSS);
  * 
- *  int main() 
- *  {
- *      pc.baud(921600);    // boost the communication speed
- *      printf("\033[2J");  // clear the terminal
- *      printf("\033[1;1H");// and set the cursor to home
- *      wait(0.1f);
- *      printf("*******************************************************\n");
- *         
- *      touch_pad.init();
- *      touch_pad.enable();
- *    
- *      while(1)
- *      {
- *          if(touch_pad.isPressed())
- *          {
- *              uint16_t button_val = touch_pad.buttonPressed();
- *              printf("button = 0x%04x\n", button_val);
- *              myled = (button_val>0) ? 1 : 0;
- *          }    
- *      }
- *  }
+ * int main() 
+ * {       
+ *     touch_pad.init();
+ *     touch_pad.enable();
+ *     t.start();
+ *     while(1)
+ *     {
+ *         if(touch_pad.isPressed())
+ *         {
+ *             uint16_t button_val = touch_pad.buttonPressed();
+ *             LOG("button = 0x%04x\n", button_val);
+ *             myled = (button_val>0) ? 1 : 0;
+ *         }
+ *         if(t.read_ms() > 5000)
+ *         {
+ *             touch_pad.disable();
+ *             off = 1;
+ *             wait(5.0f);
+ *             off = 0;
+ *             touch_pad.enable();
+ *             t.reset();
+ *         }
+ *             
+ *     }
+ * }
  * @endcode
  */