Start of a microbit mpr121 library
Dependents: microbitmpr121-example
MicroBitMpr121.cpp@3:a91b1bb396ca, 2017-01-16 (annotated)
- Committer:
- owenbrotherwood
- Date:
- Mon Jan 16 14:31:36 2017 +0000
- Revision:
- 3:a91b1bb396ca
- Parent:
- 2:4e130924a398
- Child:
- 4:f63476855239
error No default constructor exists for class "MicroBitI2C";
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
owenbrotherwood | 0:fb4572fc4901 | 1 | #include "MicroBit.h" |
owenbrotherwood | 1:f6fed00a3ff2 | 2 | #include "MicroBitI2C.h" |
owenbrotherwood | 1:f6fed00a3ff2 | 3 | #include "MicroBitMpr121.h" |
owenbrotherwood | 0:fb4572fc4901 | 4 | |
owenbrotherwood | 0:fb4572fc4901 | 5 | #define DEBUG 1 |
owenbrotherwood | 1:f6fed00a3ff2 | 6 | |
owenbrotherwood | 1:f6fed00a3ff2 | 7 | //MicroBitMpr121::MicroBitMpr121(PinName sda, PinName scl, PinName irq, MPR121_ADDR addr) |
owenbrotherwood | 1:f6fed00a3ff2 | 8 | MicroBitMpr121::MicroBitMpr121() |
owenbrotherwood | 0:fb4572fc4901 | 9 | { |
owenbrotherwood | 3:a91b1bb396ca | 10 | MicroBitI2C::I2C _i2c(I2C_SDA0, I2C_SCL0); |
owenbrotherwood | 2:4e130924a398 | 11 | uint8_t addr = ADDR_SDA; |
owenbrotherwood | 1:f6fed00a3ff2 | 12 | // PinName _irq = irq; //TODO :: use |
owenbrotherwood | 1:f6fed00a3ff2 | 13 | uint8_t _i2c_addr = (addr << 1); // To get 8bit address ...? |
owenbrotherwood | 0:fb4572fc4901 | 14 | return; |
owenbrotherwood | 0:fb4572fc4901 | 15 | } |
owenbrotherwood | 0:fb4572fc4901 | 16 | |
owenbrotherwood | 1:f6fed00a3ff2 | 17 | void MicroBitMpr121::init(void) |
owenbrotherwood | 0:fb4572fc4901 | 18 | { |
owenbrotherwood | 3:a91b1bb396ca | 19 | // P16.setPull(PullUp); //TODO use _irq and not hard code |
owenbrotherwood | 3:a91b1bb396ca | 20 | MicroBitMpr121::write(SRST, 0x63); |
owenbrotherwood | 3:a91b1bb396ca | 21 | MicroBitMpr121::write(MHDR, 0x1); |
owenbrotherwood | 3:a91b1bb396ca | 22 | MicroBitMpr121::write(NHDR, 0x1); |
owenbrotherwood | 3:a91b1bb396ca | 23 | MicroBitMpr121::write(NCLR, 0x0); |
owenbrotherwood | 3:a91b1bb396ca | 24 | MicroBitMpr121::write(FDLR, 0x0); |
owenbrotherwood | 3:a91b1bb396ca | 25 | MicroBitMpr121::write(MHDF, 0x1); |
owenbrotherwood | 3:a91b1bb396ca | 26 | MicroBitMpr121::write(NHDF, 0x1); |
owenbrotherwood | 3:a91b1bb396ca | 27 | MicroBitMpr121::write(NCLF, 0xFF); |
owenbrotherwood | 3:a91b1bb396ca | 28 | MicroBitMpr121::write(FDLF, 0x2); |
owenbrotherwood | 1:f6fed00a3ff2 | 29 | for(int i=0; i<(12*2); i+=2) { |
owenbrotherwood | 3:a91b1bb396ca | 30 | MicroBitMpr121::write(static_cast<MPR121_REGISTER>(E0TTH+i), 0x20); |
owenbrotherwood | 0:fb4572fc4901 | 31 | } |
owenbrotherwood | 1:f6fed00a3ff2 | 32 | for(int i=0; i<(12*2); i+=2) { |
owenbrotherwood | 3:a91b1bb396ca | 33 | MicroBitMpr121::write(static_cast<MPR121_REGISTER>(E0RTH+i), 0x10); |
owenbrotherwood | 0:fb4572fc4901 | 34 | } |
owenbrotherwood | 3:a91b1bb396ca | 35 | MicroBitMpr121::write(DT_DR, 0x11); |
owenbrotherwood | 3:a91b1bb396ca | 36 | MicroBitMpr121::write(CDC_CONFIG, 0x10); |
owenbrotherwood | 3:a91b1bb396ca | 37 | MicroBitMpr121::write(CDT_CONFIG, 0x20); |
owenbrotherwood | 3:a91b1bb396ca | 38 | MicroBitMpr121::write(AUTO_CFG0, 0x33); |
owenbrotherwood | 3:a91b1bb396ca | 39 | MicroBitMpr121::write(AUTO_CFG1, 0x07); |
owenbrotherwood | 3:a91b1bb396ca | 40 | MicroBitMpr121::write(USL, 0xc9); |
owenbrotherwood | 3:a91b1bb396ca | 41 | MicroBitMpr121::write(LSL, 0x83); |
owenbrotherwood | 3:a91b1bb396ca | 42 | MicroBitMpr121::write(TL, 0xb5); |
owenbrotherwood | 3:a91b1bb396ca | 43 | MicroBitMpr121::write(ECR, 0x8f); |
owenbrotherwood | 0:fb4572fc4901 | 44 | return; |
owenbrotherwood | 0:fb4572fc4901 | 45 | } |
owenbrotherwood | 0:fb4572fc4901 | 46 | |
owenbrotherwood | 1:f6fed00a3ff2 | 47 | void MicroBitMpr121::enable(void) |
owenbrotherwood | 0:fb4572fc4901 | 48 | { |
owenbrotherwood | 0:fb4572fc4901 | 49 | _button = 0; |
owenbrotherwood | 0:fb4572fc4901 | 50 | _button_has_changed = 0; |
owenbrotherwood | 3:a91b1bb396ca | 51 | MicroBitMpr121::write(ECR, 0x8f); |
owenbrotherwood | 3:a91b1bb396ca | 52 | //TODO _irq->fall(this, &MicroBitMpr121::handler); |
owenbrotherwood | 0:fb4572fc4901 | 53 | return; |
owenbrotherwood | 0:fb4572fc4901 | 54 | } |
owenbrotherwood | 0:fb4572fc4901 | 55 | |
owenbrotherwood | 1:f6fed00a3ff2 | 56 | void MicroBitMpr121::disable(void) |
owenbrotherwood | 0:fb4572fc4901 | 57 | { |
owenbrotherwood | 3:a91b1bb396ca | 58 | //TODO _irq->fall(NULL); |
owenbrotherwood | 0:fb4572fc4901 | 59 | _button = 0; |
owenbrotherwood | 0:fb4572fc4901 | 60 | _button_has_changed = 0; |
owenbrotherwood | 3:a91b1bb396ca | 61 | MicroBitMpr121::write(ECR, 0x0); |
owenbrotherwood | 3:a91b1bb396ca | 62 | MicroBitMpr121::write(AUTO_CFG0, 0x0); |
owenbrotherwood | 3:a91b1bb396ca | 63 | MicroBitMpr121::write(AUTO_CFG1, 0x0); |
owenbrotherwood | 0:fb4572fc4901 | 64 | return; |
owenbrotherwood | 0:fb4572fc4901 | 65 | } |
owenbrotherwood | 0:fb4572fc4901 | 66 | |
owenbrotherwood | 1:f6fed00a3ff2 | 67 | uint32_t MicroBitMpr121::isPressed(void) |
owenbrotherwood | 0:fb4572fc4901 | 68 | { |
owenbrotherwood | 0:fb4572fc4901 | 69 | return _button_has_changed; |
owenbrotherwood | 0:fb4572fc4901 | 70 | } |
owenbrotherwood | 0:fb4572fc4901 | 71 | |
owenbrotherwood | 1:f6fed00a3ff2 | 72 | uint16_t MicroBitMpr121::buttonPressed(void) |
owenbrotherwood | 0:fb4572fc4901 | 73 | { |
owenbrotherwood | 0:fb4572fc4901 | 74 | _button_has_changed = 0; |
owenbrotherwood | 0:fb4572fc4901 | 75 | return _button; |
owenbrotherwood | 0:fb4572fc4901 | 76 | } |
owenbrotherwood | 0:fb4572fc4901 | 77 | |
owenbrotherwood | 1:f6fed00a3ff2 | 78 | void MicroBitMpr121::registerDump() const |
owenbrotherwood | 0:fb4572fc4901 | 79 | { |
owenbrotherwood | 0:fb4572fc4901 | 80 | uint8_t reg_val = 0; |
owenbrotherwood | 1:f6fed00a3ff2 | 81 | |
owenbrotherwood | 1:f6fed00a3ff2 | 82 | for(int i=0; i<0x80; i++) { |
owenbrotherwood | 1:f6fed00a3ff2 | 83 | reg_val = MicroBitMpr121::read(static_cast<MPR121_REGISTER>(i)); |
owenbrotherwood | 1:f6fed00a3ff2 | 84 | printf("Reg 0x%02x: 0x%02x \n", i, reg_val); |
owenbrotherwood | 0:fb4572fc4901 | 85 | } |
owenbrotherwood | 1:f6fed00a3ff2 | 86 | |
owenbrotherwood | 0:fb4572fc4901 | 87 | return; |
owenbrotherwood | 0:fb4572fc4901 | 88 | } |
owenbrotherwood | 0:fb4572fc4901 | 89 | |
owenbrotherwood | 1:f6fed00a3ff2 | 90 | void MicroBitMpr121::handler(void) |
owenbrotherwood | 0:fb4572fc4901 | 91 | { |
owenbrotherwood | 0:fb4572fc4901 | 92 | uint16_t reg_val = 0, oor_val = 0; |
owenbrotherwood | 1:f6fed00a3ff2 | 93 | reg_val = MicroBitMpr121::read(ELE0_7_STAT); |
owenbrotherwood | 1:f6fed00a3ff2 | 94 | reg_val |= MicroBitMpr121::read(ELE8_11_STAT) << 8; |
owenbrotherwood | 1:f6fed00a3ff2 | 95 | oor_val = MicroBitMpr121::read(ELE0_7_OOR_STAT); |
owenbrotherwood | 1:f6fed00a3ff2 | 96 | oor_val |= MicroBitMpr121::read(ELE8_11_OOR_STAT) << 8; |
owenbrotherwood | 1:f6fed00a3ff2 | 97 | if((0 != oor_val) && DEBUG) { |
owenbrotherwood | 1:f6fed00a3ff2 | 98 | printf("MPR121 OOR failure - 0x%04x\n", oor_val); |
owenbrotherwood | 0:fb4572fc4901 | 99 | wait(0.1f); |
owenbrotherwood | 0:fb4572fc4901 | 100 | NVIC_SystemReset(); |
owenbrotherwood | 0:fb4572fc4901 | 101 | } |
owenbrotherwood | 0:fb4572fc4901 | 102 | _button = reg_val; |
owenbrotherwood | 0:fb4572fc4901 | 103 | _button_has_changed = 1; |
owenbrotherwood | 0:fb4572fc4901 | 104 | return; |
owenbrotherwood | 0:fb4572fc4901 | 105 | } |
owenbrotherwood | 0:fb4572fc4901 | 106 | |
owenbrotherwood | 3:a91b1bb396ca | 107 | void MicroBitMpr121::write(MPR121_REGISTER const reg, uint8_t const data) const |
owenbrotherwood | 0:fb4572fc4901 | 108 | { |
owenbrotherwood | 0:fb4572fc4901 | 109 | char buf[2] = {reg, data}; |
owenbrotherwood | 0:fb4572fc4901 | 110 | uint8_t result = 0; |
owenbrotherwood | 3:a91b1bb396ca | 111 | result = _i2c.write(_i2c_addr, buf, 2); |
owenbrotherwood | 1:f6fed00a3ff2 | 112 | if(result && DEBUG) { |
owenbrotherwood | 1:f6fed00a3ff2 | 113 | printf("I2c write failed\n"); |
owenbrotherwood | 0:fb4572fc4901 | 114 | } |
owenbrotherwood | 0:fb4572fc4901 | 115 | return; |
owenbrotherwood | 0:fb4572fc4901 | 116 | } |
owenbrotherwood | 0:fb4572fc4901 | 117 | |
owenbrotherwood | 1:f6fed00a3ff2 | 118 | uint8_t MicroBitMpr121::read(MPR121_REGISTER const reg) const |
owenbrotherwood | 0:fb4572fc4901 | 119 | { |
owenbrotherwood | 0:fb4572fc4901 | 120 | char buf[1] = {reg}, data = 0; |
owenbrotherwood | 0:fb4572fc4901 | 121 | uint8_t result = 1; |
owenbrotherwood | 3:a91b1bb396ca | 122 | result &= _i2c.write(_i2c_addr, buf, 1, true); //TODO: Correct with true? |
owenbrotherwood | 3:a91b1bb396ca | 123 | result &= _i2c.read(_i2c_addr, &data, 1); |
owenbrotherwood | 1:f6fed00a3ff2 | 124 | if(result && DEBUG) { |
owenbrotherwood | 1:f6fed00a3ff2 | 125 | printf("I2c read failed\n"); |
owenbrotherwood | 0:fb4572fc4901 | 126 | } |
owenbrotherwood | 0:fb4572fc4901 | 127 | return data; |
owenbrotherwood | 0:fb4572fc4901 | 128 | } |