Start of a microbit mpr121 library
Dependents: microbitmpr121-example
Diff: MicroBitMpr121.cpp
- Revision:
- 3:a91b1bb396ca
- Parent:
- 2:4e130924a398
- Child:
- 4:f63476855239
--- a/MicroBitMpr121.cpp Mon Jan 16 14:00:36 2017 +0000 +++ b/MicroBitMpr121.cpp Mon Jan 16 14:31:36 2017 +0000 @@ -7,7 +7,7 @@ //MicroBitMpr121::MicroBitMpr121(PinName sda, PinName scl, PinName irq, MPR121_ADDR addr) MicroBitMpr121::MicroBitMpr121() { - I2C _i2c = MicroBitI2C(I2C_SDA0, I2C_SCL0); + MicroBitI2C::I2C _i2c(I2C_SDA0, I2C_SCL0); uint8_t addr = ADDR_SDA; // PinName _irq = irq; //TODO :: use uint8_t _i2c_addr = (addr << 1); // To get 8bit address ...? @@ -16,31 +16,31 @@ void MicroBitMpr121::init(void) { - P16.setPull(PullUp); //TODO use _irq and not hard code - MicroBitI2C::write(SRST, 0x63); - MicroBitI2C::write(MHDR, 0x1); - MicroBitI2C::write(NHDR, 0x1); - MicroBitI2C::write(NCLR, 0x0); - MicroBitI2C::write(FDLR, 0x0); - MicroBitI2C::write(MHDF, 0x1); - MicroBitI2C::write(NHDF, 0x1); - MicroBitI2C::write(NCLF, 0xFF); - MicroBitI2C::write(FDLF, 0x2); + // P16.setPull(PullUp); //TODO use _irq and not hard code + MicroBitMpr121::write(SRST, 0x63); + MicroBitMpr121::write(MHDR, 0x1); + MicroBitMpr121::write(NHDR, 0x1); + MicroBitMpr121::write(NCLR, 0x0); + MicroBitMpr121::write(FDLR, 0x0); + MicroBitMpr121::write(MHDF, 0x1); + MicroBitMpr121::write(NHDF, 0x1); + MicroBitMpr121::write(NCLF, 0xFF); + MicroBitMpr121::write(FDLF, 0x2); for(int i=0; i<(12*2); i+=2) { - MicroBitI2C::write(static_cast<MPR121_REGISTER>(E0TTH+i), 0x20); + MicroBitMpr121::write(static_cast<MPR121_REGISTER>(E0TTH+i), 0x20); } for(int i=0; i<(12*2); i+=2) { - MicroBitI2C::write(static_cast<MPR121_REGISTER>(E0RTH+i), 0x10); + MicroBitMpr121::write(static_cast<MPR121_REGISTER>(E0RTH+i), 0x10); } - MicroBitI2C::write(DT_DR, 0x11); - MicroBitI2C::write(CDC_CONFIG, 0x10); - MicroBitI2C::write(CDT_CONFIG, 0x20); - MicroBitI2C::write(AUTO_CFG0, 0x33); - MicroBitI2C::write(AUTO_CFG1, 0x07); - MicroBitI2C::write(USL, 0xc9); - MicroBitI2C::write(LSL, 0x83); - MicroBitI2C::write(TL, 0xb5); - MicroBitI2C::write(ECR, 0x8f); + MicroBitMpr121::write(DT_DR, 0x11); + MicroBitMpr121::write(CDC_CONFIG, 0x10); + MicroBitMpr121::write(CDT_CONFIG, 0x20); + MicroBitMpr121::write(AUTO_CFG0, 0x33); + MicroBitMpr121::write(AUTO_CFG1, 0x07); + MicroBitMpr121::write(USL, 0xc9); + MicroBitMpr121::write(LSL, 0x83); + MicroBitMpr121::write(TL, 0xb5); + MicroBitMpr121::write(ECR, 0x8f); return; } @@ -48,19 +48,19 @@ { _button = 0; _button_has_changed = 0; - MicroBitI2C::write(ECR, 0x8f); - _irq->fall(this, &MicroBitMpr121::handler); + MicroBitMpr121::write(ECR, 0x8f); + //TODO _irq->fall(this, &MicroBitMpr121::handler); return; } void MicroBitMpr121::disable(void) { - _irq->fall(NULL); + //TODO _irq->fall(NULL); _button = 0; _button_has_changed = 0; - MicroBitI2C::write(ECR, 0x0); - MicroBitI2C::write(AUTO_CFG0, 0x0); - MicroBitI2C::write(AUTO_CFG1, 0x0); + MicroBitMpr121::write(ECR, 0x0); + MicroBitMpr121::write(AUTO_CFG0, 0x0); + MicroBitMpr121::write(AUTO_CFG1, 0x0); return; } @@ -104,11 +104,11 @@ return; } -void MicroBitI2C::write(MPR121_REGISTER const reg, uint8_t const data) const +void MicroBitMpr121::write(MPR121_REGISTER const reg, uint8_t const data) const { char buf[2] = {reg, data}; uint8_t result = 0; - result = _i2c->write(_i2c_addr, buf, 2); + result = _i2c.write(_i2c_addr, buf, 2); if(result && DEBUG) { printf("I2c write failed\n"); } @@ -119,8 +119,8 @@ { char buf[1] = {reg}, data = 0; uint8_t result = 1; - result &= _i2c->write(_i2c_addr, buf, 1, true); //TODO: Correct with true? - result &= _i2c->read(_i2c_addr, &data, 1); + result &= _i2c.write(_i2c_addr, buf, 1, true); //TODO: Correct with true? + result &= _i2c.read(_i2c_addr, &data, 1); if(result && DEBUG) { printf("I2c read failed\n"); }