Start of a microbit mpr121 library

Dependents:   microbitmpr121-example

Revision:
6:103a5a2ca571
Parent:
5:4a8384331ca7
Child:
7:ac942cee2975
--- a/MicroBitMpr121.cpp	Mon Jan 16 19:43:18 2017 +0000
+++ b/MicroBitMpr121.cpp	Mon Jan 16 21:12:52 2017 +0000
@@ -23,69 +23,110 @@
 DEALINGS IN THE SOFTWARE.
 */
 
-/**
-  * Class definition for MicroBit Mpr121.
-  *
-  * Represents an implementation of the Mpr121.
-  * Also includes ...
-  */
 #include "MicroBitConfig.h"
 #include "MicroBitPin.h"
 #include "MicroBitMpr121.h"
 #include "MicroBitFiber.h"
 #include "ErrorNo.h"
 
-/**
-  * An initialisation member function.
-  *
-  * @param id the unique identifier for this instance.
-  *
-  * @param address the base address  on the i2c bus.
-  */
 void MicroBitMpr121::init(uint16_t id, uint16_t address)
 {
     this->id = id;
     this->address = address;
 
+    // setup and registers - start with POR values (must be in stop mode)
+    MicroBitMpr121::writeCommand(SRST, 0x63); //REG 0x80
+
+    // Baseline Filtering Control Register (changes response sensitivity)
+    // http://cache.freescale.com/files/sensors/doc/app_note/AN3891.pdf
+    MicroBitMpr121::writeCommand(MHDR, 0x1);  //REG 0x2B
+    MicroBitMpr121::writeCommand(NHDR, 0x1);  //REG 0x2C
+    MicroBitMpr121::writeCommand(NCLR, 0x0);  //REG 0x2D
+    MicroBitMpr121::writeCommand(FDLR, 0x0);  //REG 0x2E
+    MicroBitMpr121::writeCommand(MHDF, 0x1);  //REG 0x2F
+    MicroBitMpr121::writeCommand(NHDF, 0x1);  //REG 0x30
+    MicroBitMpr121::writeCommand(NCLF, 0xFF); //REG 0x31
+    MicroBitMpr121::writeCommand(FDLF, 0x2);  //REG 0x32
+
+    // Touch / Release Threshold
+    // cache.freescale.com/files/sensors/doc/app_note/AN3892.pdf
+    for(int i=0; i<(12*2); i+=2) { // touch
+        MicroBitMpr121::writeCommand(static_cast<MPR121_REGISTER>(E0TTH+i), 0x20); //REG 0x41...0x58 odd
+    }
+    for(int i=0; i<(12*2); i+=2) { // release
+        MicroBitMpr121::writeCommand(static_cast<MPR121_REGISTER>(E0RTH+i), 0x10); //REG 0x41...0x58 even
+    }
+
+    // Debounce Register DR=b6...4, DT=b2...0
+    MicroBitMpr121::writeCommand(DT_DR, 0x11); //REG 0x5B
+
+    // Filter and Global CDC CDT Configuration (sample time, charge current)
+    MicroBitMpr121::writeCommand(CDC_CONFIG, 0x10); //REG 0x5C default 10
+    MicroBitMpr121::writeCommand(CDT_CONFIG, 0x20); //REG 0x5D default 24
+
+    // Auto-Configuration Registers
+    // http://cache.freescale.com/files/sensors/doc/app_note/AN3889.pdf
+    MicroBitMpr121::writeCommand(AUTO_CFG0, 0x33); // REG 0x7B
+    MicroBitMpr121::writeCommand(AUTO_CFG1, 0x07); // REG 0x7C
+    MicroBitMpr121::writeCommand(USL, 0xc9);       // REG 0x7D((3.3-.07)/3.3) * 256
+    MicroBitMpr121::writeCommand(LSL, 0x83);       // REG 0x7E((3.3-.07)/3.3) * 256 * 0.65f
+    MicroBitMpr121::writeCommand(TL,  0xb5);       // REG 0x7F((3.3-.07)/3.3) * 256 * 0.9f
+    // 255 > USL > TL > LSL > 0
+
+    // Electrode Configuration Register - enable all 12 and start
+    MicroBitMpr121::writeCommand(ECR, 0x8f);
+
     // Indicate that we're up and running.
     status |= MICROBIT_COMPONENT_RUNNING;
 }
 
-/**
-  * Constructor.
-  * Create a software representation.
-  *
-  * @param _i2c an instance of i2c
-  *
-  * @param address the address register on the i2c bus. Defaults to MPR121_DEFAULT_ADDR.
-  *
-  * @param id the ID of the new object. Defaults to MPR121_DEFAULT_ADDR.
-  *
-  * @code
-  * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);
-  *
-  * MicroBitMpr121 mpr121(i2c);
-  * @endcode
-  */
-MicroBitMpr121::MicroBitMpr121(MicroBitI2C& _i2c, uint16_t address, DigitalIn interrupt, uint16_t id) :
-    int1(interrupt),
+MicroBitMpr121::MicroBitMpr121(MicroBitI2C& _i2c, uint16_t address, DigitalIn intPin, uint16_t id) :
+    int1(intPin), // Not sure what this does :( and is our pin needs to be pull up !!!!!!!!!!!!!!
     i2c(_i2c)
- 
+
 {
     init(id, address);
 }
 
-/**
-  * Issues a standard, 2 byte I2C command write.
-  *
-  * Blocks the calling thread until complete.
-  *
-  * @param reg The address of the register to write to.
-  *
-  * @param value The value to write.
-  *
-  * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if the the write request failed.
-  */
+void  MicroBitMpr121::enable(void)
+{
+    _button = 0;
+    _button_has_changed = 0;
+    // enable the 12 electrodes - allow disable to put device into
+    //  lower current consumption mode
+    MicroBitMpr121::writeCommand(ECR, 0x8f);
+    // and attach the interrupt handler TODO
+    // _irq->fall(this, &MicroBitMpr121::handler);
+
+    return;
+}
+
+void MicroBitMpr121::registerDump() const // What is the const purpose
+{
+    int reg_val = 0;
+
+    for(int i=0; i<0x80; i++) {
+        reg_val = MicroBitMpr121::read8(i); // TODO Test reg_val == 0
+        printf("Reg 0x%02x: 0x%02x \n", i, reg_val);
+    }
+
+    return;
+}
+
+void  MicroBitMpr121::disable(void)
+{
+    // detach the interrupt handler TODO
+    //_irq->fall(NULL);
+    _button = 0;
+    _button_has_changed = 0;
+    //  put the device in low current consumption mode - dont re-init registers
+    MicroBitMpr121::writeCommand(ECR, 0x0);
+    MicroBitMpr121::writeCommand(AUTO_CFG0, 0x0); // REG 0x7B
+    MicroBitMpr121::writeCommand(AUTO_CFG1, 0x0); // REG 0x7C
+
+    return;
+}
+
 int MicroBitMpr121::writeCommand(uint8_t reg, uint8_t value)
 {
     uint8_t command[2];
@@ -95,19 +136,6 @@
     return i2c.write(address, (const char *)command, 2);
 }
 
-/**
-  * Issues a read command, copying data into the specified buffer.
-  *
-  * Blocks the calling thread until complete.
-  *
-  * @param reg The address of the register to access.
-  *
-  * @param buffer Memory area to read the data into.
-  *
-  * @param length The number of bytes to read.
-  *
-  * @return MICROBIT_OK on success, MICROBIT_INVALID_PARAMETER or MICROBIT_I2C_ERROR if the the read request failed.
-  */
 int MicroBitMpr121::readCommand(uint8_t reg, uint8_t* buffer, int length)
 {
     int result;
@@ -126,15 +154,6 @@
     return MICROBIT_OK;
 }
 
-/**
-  * Issues a read of a given address, and returns the value.
-  *
-  * Blocks the calling thread until complete.
-  *
-  * @param reg The address of the 16 bit register to access.
-  *
-  * @return The register value, interpreted as a 16 but signed value, or MICROBIT_I2C_ERROR if the read request failed.
-  */
 int MicroBitMpr121::read16(uint8_t reg)
 {
     uint8_t cmd[2];
@@ -155,15 +174,6 @@
     return (int16_t) ((cmd[1] | (cmd[0] << 8))); //concatenate the MSB and LSB
 }
 
-/**
-  * Issues a read of a given address, and returns the value.
-  *
-  * Blocks the calling thread until complete.
-  *
-  * @param reg The address of the 16 bit register to access.
-  *
-  * @return The register value, interpreted as a 8 bit unsigned value, or MICROBIT_I2C_ERROR if the magnetometer could not be accessed.
-  */
 int MicroBitMpr121::read8(uint8_t reg)
 {
     uint8_t data;
@@ -177,41 +187,23 @@
     return data;
 }
 
-/**
-  * Periodic callback from MicroBit idle thread.
-  *
-  * Calls ...
-  */
 void MicroBitMpr121::idleTick()
 {
-    
+
 }
 
-/**
-  * Attempts to read the 8 bit ID from the mpr121, this can be used for
-  * validation purposes. TODO?
-  *
-  * @return the 8 bit ID returned by the mpr121, or MICROBIT_I2C_ERROR if the request fails.
-  *
-  * @code
-  * mpr121.whoAmI();
-  * @endcode
-  */
 int MicroBitMpr121::whoAmI()
 {
     uint8_t data;
     int result;
 
- //   result = readCommand(MPR121_WHOAMI, &data, 1);
+//   result = readCommand(MPR121_WHOAMI, &data, 1);
     if (result != MICROBIT_OK)
         return MICROBIT_I2C_ERROR;
 
     return (int)data;
 }
 
-/**
-  * Destructor, where we deregister this instance from the array of fiber components.
-  */
 MicroBitMpr121::~MicroBitMpr121()
 {
     fiber_remove_idle_component(this);