Start of a microbit mpr121 library
Dependents: microbitmpr121-example
MicroBitMpr121.h@1:f6fed00a3ff2, 2017-01-16 (annotated)
- Committer:
- owenbrotherwood
- Date:
- Mon Jan 16 11:38:45 2017 +0000
- Revision:
- 1:f6fed00a3ff2
- Parent:
- 0:fb4572fc4901
- Child:
- 2:4e130924a398
wip
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
owenbrotherwood | 1:f6fed00a3ff2 | 1 | |
owenbrotherwood | 0:fb4572fc4901 | 2 | #ifndef MICROBITMPR121_H |
owenbrotherwood | 0:fb4572fc4901 | 3 | #define MICROBITMPR121_H |
owenbrotherwood | 0:fb4572fc4901 | 4 | #include "MicroBit.h" |
owenbrotherwood | 0:fb4572fc4901 | 5 | |
owenbrotherwood | 0:fb4572fc4901 | 6 | class MicroBitMpr121 |
owenbrotherwood | 0:fb4572fc4901 | 7 | { |
owenbrotherwood | 0:fb4572fc4901 | 8 | private: |
owenbrotherwood | 1:f6fed00a3ff2 | 9 | |
owenbrotherwood | 1:f6fed00a3ff2 | 10 | MicroBitI2C _i2c; |
owenbrotherwood | 1:f6fed00a3ff2 | 11 | PinName _irq; |
owenbrotherwood | 0:fb4572fc4901 | 12 | uint8_t _i2c_addr; |
owenbrotherwood | 0:fb4572fc4901 | 13 | volatile uint16_t _button; |
owenbrotherwood | 0:fb4572fc4901 | 14 | volatile uint32_t _button_has_changed; |
owenbrotherwood | 1:f6fed00a3ff2 | 15 | |
owenbrotherwood | 0:fb4572fc4901 | 16 | /** The interrupt handler for the IRQ pin |
owenbrotherwood | 0:fb4572fc4901 | 17 | */ |
owenbrotherwood | 0:fb4572fc4901 | 18 | void handler(void); |
owenbrotherwood | 0:fb4572fc4901 | 19 | |
owenbrotherwood | 0:fb4572fc4901 | 20 | public: |
owenbrotherwood | 1:f6fed00a3ff2 | 21 | |
owenbrotherwood | 0:fb4572fc4901 | 22 | /** |
owenbrotherwood | 0:fb4572fc4901 | 23 | * @enum MPR121_ADDR |
owenbrotherwood | 0:fb4572fc4901 | 24 | * @brief Possible terminations for the ADDR pin |
owenbrotherwood | 1:f6fed00a3ff2 | 25 | */ |
owenbrotherwood | 1:f6fed00a3ff2 | 26 | enum MPR121_ADDR { |
owenbrotherwood | 0:fb4572fc4901 | 27 | ADDR_VSS = 0x5A, /*!< ADDR connected to VSS */ |
owenbrotherwood | 0:fb4572fc4901 | 28 | ADDR_VDD, /*!< ADDR connected to VDD */ |
owenbrotherwood | 0:fb4572fc4901 | 29 | ADDR_SCL, /*!< ADDR connected to SDA */ |
owenbrotherwood | 0:fb4572fc4901 | 30 | ADDR_SDA /*!< ADDR connected to SCL */ |
owenbrotherwood | 0:fb4572fc4901 | 31 | }; |
owenbrotherwood | 1:f6fed00a3ff2 | 32 | |
owenbrotherwood | 0:fb4572fc4901 | 33 | /** |
owenbrotherwood | 0:fb4572fc4901 | 34 | * @enum MPR121_REGISTER |
owenbrotherwood | 0:fb4572fc4901 | 35 | * @brief The device register map |
owenbrotherwood | 0:fb4572fc4901 | 36 | */ |
owenbrotherwood | 1:f6fed00a3ff2 | 37 | enum MPR121_REGISTER { |
owenbrotherwood | 0:fb4572fc4901 | 38 | ELE0_7_STAT = 0x00, |
owenbrotherwood | 1:f6fed00a3ff2 | 39 | ELE8_11_STAT, ELE0_7_OOR_STAT, ELE8_11_OOR_STAT, EFD0LB, EFD0HB, |
owenbrotherwood | 0:fb4572fc4901 | 40 | EFD1LB, EFD1HB, EFD2LB, EFD2HB, EFD3LB, EFD3HB, EFD4LB, EFD4HB, EFD5LB, EFD5HB, |
owenbrotherwood | 1:f6fed00a3ff2 | 41 | |
owenbrotherwood | 0:fb4572fc4901 | 42 | EFD6LB = 0x10, |
owenbrotherwood | 0:fb4572fc4901 | 43 | EFD6HB, EFD7LB, EFD7HB, EFD8LB, EFD8HB, EFD9LB, EFD9HB, EFD10LB, |
owenbrotherwood | 0:fb4572fc4901 | 44 | EFD10HB, EFD11LB, EFD11HB, EFDPROXLB, EFDPROXHB, E0BV, E1BV, |
owenbrotherwood | 1:f6fed00a3ff2 | 45 | |
owenbrotherwood | 0:fb4572fc4901 | 46 | E2BV = 0x20, |
owenbrotherwood | 0:fb4572fc4901 | 47 | E3BV, E4BV, E5BV, E6BV, E7BV, E8BV, E9BV, E10BV, E11BV, EPROXBV, |
owenbrotherwood | 1:f6fed00a3ff2 | 48 | MHDR, NHDR, NCLR, FDLR, MHDF, |
owenbrotherwood | 1:f6fed00a3ff2 | 49 | |
owenbrotherwood | 0:fb4572fc4901 | 50 | NHDF = 0x30, |
owenbrotherwood | 0:fb4572fc4901 | 51 | NCLF, FDLF, NHDT, NCLT, FDLT, MHDPROXR, NHDPROXR, NCLPROXR, |
owenbrotherwood | 0:fb4572fc4901 | 52 | FDLPROXR, MHDPROXF, NHDPROXF, NCLPROXF, FDLPROXF, NHDPROXT, NCLPROXT, |
owenbrotherwood | 1:f6fed00a3ff2 | 53 | |
owenbrotherwood | 0:fb4572fc4901 | 54 | FDLPROXT = 0x40, |
owenbrotherwood | 0:fb4572fc4901 | 55 | E0TTH, E0RTH, E1TTH, E1RTH, E2TTH, E2RTH, E3TTH, E3RTH, |
owenbrotherwood | 0:fb4572fc4901 | 56 | E4TTH, E4RTH, E5TTH, E5RTH, E6TTH, E6RTH, E7TTH, |
owenbrotherwood | 1:f6fed00a3ff2 | 57 | |
owenbrotherwood | 0:fb4572fc4901 | 58 | E7RTH = 0x50, |
owenbrotherwood | 0:fb4572fc4901 | 59 | E8TTH, E8RTH, E9TTH, E9RTH, E10TTH, E10RTH, E11TTH, E11RTH, |
owenbrotherwood | 0:fb4572fc4901 | 60 | EPROXTTH, EPROXRTH, DT_DR, CDC_CONFIG, CDT_CONFIG, ECR, CDC0, |
owenbrotherwood | 1:f6fed00a3ff2 | 61 | |
owenbrotherwood | 0:fb4572fc4901 | 62 | CDC1 = 0x60, |
owenbrotherwood | 0:fb4572fc4901 | 63 | CDC2, CDC3, CDC4, CDC5, CDC6, CDC7, CDC8, CDC9, CDC10, CDC11, CDCPROX, CDT0_CDT1, |
owenbrotherwood | 1:f6fed00a3ff2 | 64 | CDT2_CDT3, CDT4_CDT5, CDT6_CDT7, |
owenbrotherwood | 1:f6fed00a3ff2 | 65 | |
owenbrotherwood | 0:fb4572fc4901 | 66 | CDT8_CDT9 = 0x70, |
owenbrotherwood | 0:fb4572fc4901 | 67 | CDT10_CDT11, CDTPROX, GPIO_CTRL0, GPIO_CTRL1, GPIO_DATA, GPIO_DIR, GPIO_EN, GPIO_SET, |
owenbrotherwood | 0:fb4572fc4901 | 68 | GPIO_CLR, GPIO_TOGGLE, AUTO_CFG0, AUTO_CFG1, USL, LSL, TL, |
owenbrotherwood | 1:f6fed00a3ff2 | 69 | |
owenbrotherwood | 0:fb4572fc4901 | 70 | SRST = 0x80 |
owenbrotherwood | 0:fb4572fc4901 | 71 | }; |
owenbrotherwood | 1:f6fed00a3ff2 | 72 | |
owenbrotherwood | 1:f6fed00a3ff2 | 73 | /** Create the MPR121 object with defaults for microbit, pi-cap and int P16 |
owenbrotherwood | 1:f6fed00a3ff2 | 74 | */ |
owenbrotherwood | 1:f6fed00a3ff2 | 75 | MicroBitMpr121(); |
owenbrotherwood | 0:fb4572fc4901 | 76 | |
owenbrotherwood | 0:fb4572fc4901 | 77 | /** Create the MPR121 object |
owenbrotherwood | 1:f6fed00a3ff2 | 78 | * @param sda - PinName for sda |
owenbrotherwood | 1:f6fed00a3ff2 | 79 | * @param sda - PinName for scl |
owenbrotherwood | 1:f6fed00a3ff2 | 80 | * @param sda - PinName for irq |
owenbrotherwood | 1:f6fed00a3ff2 | 81 | * @param addr - Connection of the address line |
owenbrotherwood | 1:f6fed00a3ff2 | 82 | */ |
owenbrotherwood | 1:f6fed00a3ff2 | 83 | |
owenbrotherwood | 1:f6fed00a3ff2 | 84 | //MicroBitMpr121(PinName sda, PinName scl, PinName irq, MPR121_ADDR addr); |
owenbrotherwood | 0:fb4572fc4901 | 85 | /** Clear state variables and initilize the dependant objects |
owenbrotherwood | 0:fb4572fc4901 | 86 | */ |
owenbrotherwood | 1:f6fed00a3ff2 | 87 | |
owenbrotherwood | 0:fb4572fc4901 | 88 | void init(void); |
owenbrotherwood | 1:f6fed00a3ff2 | 89 | |
owenbrotherwood | 0:fb4572fc4901 | 90 | /** Allow the IC to run and collect user input |
owenbrotherwood | 0:fb4572fc4901 | 91 | */ |
owenbrotherwood | 0:fb4572fc4901 | 92 | void enable(void); |
owenbrotherwood | 1:f6fed00a3ff2 | 93 | |
owenbrotherwood | 0:fb4572fc4901 | 94 | /** Stop the IC and put into low power mode |
owenbrotherwood | 0:fb4572fc4901 | 95 | */ |
owenbrotherwood | 0:fb4572fc4901 | 96 | void disable(void); |
owenbrotherwood | 1:f6fed00a3ff2 | 97 | |
owenbrotherwood | 0:fb4572fc4901 | 98 | /** Determine if a new button press event occured |
owenbrotherwood | 0:fb4572fc4901 | 99 | * Upon calling the state is cleared until another press is detected |
owenbrotherwood | 0:fb4572fc4901 | 100 | * @return 1 if a press has been detected since the last call, 0 otherwise |
owenbrotherwood | 0:fb4572fc4901 | 101 | */ |
owenbrotherwood | 0:fb4572fc4901 | 102 | uint32_t isPressed(void); |
owenbrotherwood | 1:f6fed00a3ff2 | 103 | |
owenbrotherwood | 0:fb4572fc4901 | 104 | /** Get the electrode status (ELE12 ... ELE0 -> b15 xxx b11 ... b0 |
owenbrotherwood | 0:fb4572fc4901 | 105 | * The buttons are bit mapped. ELE0 = b0 ... ELE11 = b11 b12 ... b15 undefined |
owenbrotherwood | 0:fb4572fc4901 | 106 | * @return The state of all buttons |
owenbrotherwood | 0:fb4572fc4901 | 107 | */ |
owenbrotherwood | 0:fb4572fc4901 | 108 | uint16_t buttonPressed(void); |
owenbrotherwood | 1:f6fed00a3ff2 | 109 | |
owenbrotherwood | 0:fb4572fc4901 | 110 | /** print the register map and values to the console |
owenbrotherwood | 0:fb4572fc4901 | 111 | */ |
owenbrotherwood | 1:f6fed00a3ff2 | 112 | void registerDump() const; |
owenbrotherwood | 1:f6fed00a3ff2 | 113 | |
owenbrotherwood | 0:fb4572fc4901 | 114 | /** Write to a register (exposed for debugging reasons) |
owenbrotherwood | 0:fb4572fc4901 | 115 | * Note: most writes are only valid in stop mode |
owenbrotherwood | 0:fb4572fc4901 | 116 | * @param reg - The register to be written |
owenbrotherwood | 0:fb4572fc4901 | 117 | * @param data - The data to be written |
owenbrotherwood | 0:fb4572fc4901 | 118 | */ |
owenbrotherwood | 0:fb4572fc4901 | 119 | void writeRegister(MPR121_REGISTER const reg, uint8_t const data) const; |
owenbrotherwood | 1:f6fed00a3ff2 | 120 | |
owenbrotherwood | 0:fb4572fc4901 | 121 | /** Read from a register (exposed for debugging reasons) |
owenbrotherwood | 0:fb4572fc4901 | 122 | * @param reg - The register to read from |
owenbrotherwood | 0:fb4572fc4901 | 123 | * @return The register contents |
owenbrotherwood | 0:fb4572fc4901 | 124 | */ |
owenbrotherwood | 0:fb4572fc4901 | 125 | uint8_t readRegister(MPR121_REGISTER const reg) const; |
owenbrotherwood | 1:f6fed00a3ff2 | 126 | |
owenbrotherwood | 0:fb4572fc4901 | 127 | }; |
owenbrotherwood | 0:fb4572fc4901 | 128 | |
owenbrotherwood | 0:fb4572fc4901 | 129 | #endif |
owenbrotherwood | 0:fb4572fc4901 | 130 |