Start of a microbit mpr121 library

Dependents:   microbitmpr121-example

Revision:
6:103a5a2ca571
Parent:
5:4a8384331ca7
Child:
8:cda72c6b04c0
--- a/MicroBitMpr121.h	Mon Jan 16 19:43:18 2017 +0000
+++ b/MicroBitMpr121.h	Mon Jan 16 21:12:52 2017 +0000
@@ -67,9 +67,67 @@
     DigitalIn               int1;                     // Data ready interrupt.
     MicroBitI2C&            i2c;                      // The I2C interface the sensor is connected to.
 
+    volatile uint16_t       _button;
+    volatile uint32_t       _button_has_changed;
+
+    /** The interrupt handler for the IRQ pin
+    */
+    void handler(void);
+
 public:
 
     /**
+     *  @enum MPR121_ADDR
+     *  @brief Possible terminations for the ADDR pin
+     */
+    enum MPR121_ADDR {
+        ADDR_VSS = 0x5A, /*!< ADDR connected to VSS */
+        ADDR_VDD,    /*!< ADDR connected to VDD */
+        ADDR_SCL,    /*!< ADDR connected to SDA */
+        ADDR_SDA     /*!< ADDR connected to SCL */
+    };
+
+    /**
+    *  @enum MPR121_REGISTER
+    *  @brief The device register map
+    */
+    enum MPR121_REGISTER {
+        ELE0_7_STAT = 0x00,
+        ELE8_11_STAT, ELE0_7_OOR_STAT, ELE8_11_OOR_STAT, EFD0LB, EFD0HB,
+        EFD1LB, EFD1HB, EFD2LB, EFD2HB, EFD3LB, EFD3HB, EFD4LB, EFD4HB, EFD5LB, EFD5HB,
+
+        EFD6LB = 0x10,
+        EFD6HB, EFD7LB, EFD7HB, EFD8LB, EFD8HB, EFD9LB, EFD9HB, EFD10LB,
+        EFD10HB, EFD11LB, EFD11HB, EFDPROXLB, EFDPROXHB, E0BV, E1BV,
+
+        E2BV = 0x20,
+        E3BV, E4BV, E5BV, E6BV, E7BV, E8BV, E9BV, E10BV, E11BV, EPROXBV,
+        MHDR, NHDR, NCLR, FDLR, MHDF,
+
+        NHDF = 0x30,
+        NCLF, FDLF, NHDT, NCLT, FDLT, MHDPROXR, NHDPROXR, NCLPROXR,
+        FDLPROXR, MHDPROXF, NHDPROXF, NCLPROXF, FDLPROXF, NHDPROXT, NCLPROXT,
+
+        FDLPROXT = 0x40,
+        E0TTH, E0RTH, E1TTH, E1RTH, E2TTH, E2RTH, E3TTH, E3RTH,
+        E4TTH, E4RTH, E5TTH, E5RTH, E6TTH, E6RTH, E7TTH,
+
+        E7RTH = 0x50,
+        E8TTH, E8RTH, E9TTH, E9RTH, E10TTH, E10RTH, E11TTH, E11RTH,
+        EPROXTTH, EPROXRTH, DT_DR, CDC_CONFIG, CDT_CONFIG, ECR, CDC0,
+
+        CDC1 = 0x60,
+        CDC2, CDC3, CDC4, CDC5, CDC6, CDC7, CDC8, CDC9, CDC10, CDC11, CDCPROX, CDT0_CDT1,
+        CDT2_CDT3, CDT4_CDT5, CDT6_CDT7,
+
+        CDT8_CDT9 = 0x70,
+        CDT10_CDT11, CDTPROX, GPIO_CTRL0, GPIO_CTRL1, GPIO_DATA, GPIO_DIR, GPIO_EN, GPIO_SET,
+        GPIO_CLR, GPIO_TOGGLE, AUTO_CFG0, AUTO_CFG1, USL, LSL, TL,
+
+        SRST = 0x80
+    };
+
+    /**
     * Constructor.
     * Create a software representation of a mpr121.
     *
@@ -88,110 +146,29 @@
     MicroBitMpr121(MicroBitI2C& _i2c, uint16_t address = MPR121_DEFAULT_ADDR, DigitalIn interupt = MICROBIT_PIN_P16, uint16_t id = MPR121_DEFAULT_ADDR);
 
     /**
-      * Configures the compass for the sample rate defined in this object.
-      * The nearest values are chosen to those defined that are supported by the hardware.
-      * The instance variables are then updated to reflect reality.
+      * Attempts to read the 8 bit ID for validation purposes. TODO IF RELEVANT
       *
-      * @return MICROBIT_OK or MICROBIT_I2C_ERROR if the magnetometer could not be configured.
-      */
-    int configure();
-
-    /**
-      * Attempts to set the sample rate of the compass to the specified value (in ms).
-      *
-      * @param period the requested time between samples, in milliseconds.
-      *
-      * @return MICROBIT_OK or MICROBIT_I2C_ERROR if the magnetometer could not be updated.
+      * @return the 8 bit ID, or MICROBIT_I2C_ERROR if the request fails.
       *
       * @code
-      * // sample rate is now 20 ms.
-      * compass.setPeriod(20);
-      * @endcode
-      *
-      * @note The requested rate may not be possible on the hardware. In this case, the
-      * nearest lower rate is chosen.
-      */
-    int setPeriod(int period);
-
-    /**
-      * Reads the currently configured sample rate of the compass.
-      *
-      * @return The time between samples, in milliseconds.
-      */
-    int getPeriod();
-
-    /**
-      * Gets the current heading of the device, relative to magnetic north.
-      *
-      * If the compass is not calibrated, it will raise the MICROBIT_COMPASS_EVT_CALIBRATE event.
-      *
-      * Users wishing to implement their own calibration algorithms should listen for this event,
-      * using MESSAGE_BUS_LISTENER_IMMEDIATE model. This ensures that calibration is complete before
-      * the user program continues.
-      *
-      * @return the current heading, in degrees. Or MICROBIT_CALIBRATION_IN_PROGRESS if the compass is calibrating.
-      *
-      * @code
-      * compass.heading();
-      * @endcode
-      */
-    int heading();
-
-    /**
-      * Attempts to read the 8 bit ID from the magnetometer, this can be used for
-      * validation purposes.
-      *
-      * @return the 8 bit ID returned by the magnetometer, or MICROBIT_I2C_ERROR if the request fails.
-      *
-      * @code
-      * compass.whoAmI();
+      * mrp121.whoAmI();
       * @endcode
       */
     int whoAmI();
 
     /**
-      * Determines the overall magnetic field strength based on the latest update from the magnetometer.
-      *
-      * @return The magnetic force measured across all axis, in nano teslas.
-      *
-      * @code
-      * compass.getFieldStrength();
-      * @endcode
+      * Allow the IC to run and collect user input
       */
-    int getFieldStrength();
-
-    /**
-      * Reads the current die temperature of the compass.
-      *
-      * @return the temperature in degrees celsius, or MICROBIT_I2C_ERROR if the temperature reading could not be retreived
-      *         from the accelerometer.
-      */
-    int readTemperature();
+    void enable(void);
+    
+        /** print the register map and values to the console
+     */
+    void registerDump() const;
 
     /**
-      * Perform a calibration of the compass.
-      *
-      * This method will be called automatically if a user attempts to read a compass value when
-      * the compass is uncalibrated. It can also be called at any time by the user.
-      *
-      * The method will only return once the compass has been calibrated.
-      *
-      * @return MICROBIT_OK, MICROBIT_I2C_ERROR if the magnetometer could not be accessed,
-      * or MICROBIT_CALIBRATION_REQUIRED if the calibration algorithm failed to complete successfully.
-      *
-      * @note THIS MUST BE CALLED TO GAIN RELIABLE VALUES FROM THE COMPASS
+      * Stop the IC and put into low power mode
       */
-    int calibrate();
-
-
-    /**
-      * Updates the local sample, only if the compass indicates that
-      * data is stale.
-      *
-      * @note Can be used to trigger manual updates, if the device is running without a scheduler.
-      *       Also called internally by all get[X,Y,Z]() member functions.
-      */
-    int updateSample();
+    void disable(void);
 
     /**
       * Periodic callback from MicroBit idle thread.
@@ -200,20 +177,6 @@
       */
     virtual void idleTick();
 
-    /**
-      * Returns 0 or 1. 1 indicates that the compass is calibrated, zero means the compass requires calibration.
-      */
-    int isCalibrated();
-
-    /**
-      * Returns 0 or 1. 1 indicates that the compass is calibrating, zero means the compass is not currently calibrating.
-      */
-    int isCalibrating();
-
-    /**
-      * Clears the calibration held in persistent storage, and sets the calibrated flag to zero.
-      */
-    void clearCalibration();
 
     /**
       * Destructor for MicroBitMpr121, where we deregister this instance from the array of fiber components.
@@ -257,7 +220,7 @@
       *
       * @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 magnetometer could not be accessed.
+      * @return The register value, interpreted as a 16 but signed value, or MICROBIT_I2C_ERROR.
       */
     int read16(uint8_t reg);
 
@@ -268,26 +231,18 @@
       *
       * @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.
+      * @return The register value, interpreted as a 8 bit unsigned value, or MICROBIT_I2C_ERROR if the read request failed.
       */
     int read8(uint8_t reg);
 
     /**
-      * Calculates a tilt compensated bearing of the device, using the accelerometer.
-      */
-    int tiltCompensatedBearing();
-
-    /**
-      * Calculates a non-tilt compensated bearing of the device.
-      */
-    int basicBearing();
-
-    /**
-      * An initialisation member function used by the many constructors of MicroBitMpr121.
+      * An initialisation member function.
+      *
+      * @param id the unique identifier for this instance.
       *
-      * @param id the unique identifier for this compass instance.
+      * @param address the base address on the i2c bus.
       *
-      * @param address the base address of the magnetometer on the i2c bus.
+      * @param intPin the pin used for interrupt.
       */
     void init(uint16_t id, uint16_t address);
 };