Start of a microbit mpr121 library

Dependents:   microbitmpr121-example

MicroBitMpr121.h

Committer:
owenbrotherwood
Date:
2017-01-16
Revision:
1:f6fed00a3ff2
Parent:
0:fb4572fc4901
Child:
2:4e130924a398

File content as of revision 1:f6fed00a3ff2:


#ifndef MICROBITMPR121_H
#define MICROBITMPR121_H
#include "MicroBit.h"

class MicroBitMpr121
{
private:

    MicroBitI2C _i2c;
    PinName     _irq;
    uint8_t      _i2c_addr;
    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
    };

    /** Create the MPR121 object with defaults for microbit, pi-cap and int P16
    */
    MicroBitMpr121();
    
    /** Create the MPR121 object
     *  @param sda - PinName for sda
     *  @param sda - PinName for scl
     *  @param sda - PinName for irq
     *  @param addr - Connection of the address line
     */
     
    //MicroBitMpr121(PinName sda, PinName scl, PinName irq, MPR121_ADDR addr);
    /** Clear state variables and initilize the dependant objects
     */
    
    void init(void);

    /** Allow the IC to run and collect user input
     */
    void enable(void);

    /** Stop the IC and put into low power mode
     */
    void disable(void);

    /** Determine if a new button press event occured
     *  Upon calling the state is cleared until another press is detected
     *  @return 1 if a press has been detected since the last call, 0 otherwise
     */
    uint32_t isPressed(void);

    /** Get the electrode status (ELE12 ... ELE0 -> b15 xxx b11 ... b0
     *  The buttons are bit mapped. ELE0 = b0 ... ELE11 = b11 b12 ... b15 undefined
     *  @return The state of all buttons
     */
    uint16_t buttonPressed(void);

    /** print the register map and values to the console
     */
    void registerDump() const;

    /** Write to a register (exposed for debugging reasons)
     *  Note: most writes are only valid in stop mode
     *  @param reg - The register to be written
     *  @param data - The data to be written
     */
    void writeRegister(MPR121_REGISTER const reg, uint8_t const data) const;

    /** Read from a register (exposed for debugging reasons)
     *  @param reg - The register to read from
     *  @return The register contents
     */
    uint8_t readRegister(MPR121_REGISTER const reg) const;

};

#endif