![](/media/cache/group/IMG_1289.JPG.50x50_q85.jpg)
Simple I2C test program. open/close/read/write supported.
Diff: dumb_i2c.cpp
- Revision:
- 0:1a2637f8e2dd
- Child:
- 1:e105ceaee6ac
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dumb_i2c.cpp Tue Feb 09 00:23:22 2016 +0000 @@ -0,0 +1,40 @@ +#include "mbed.h" +#include "dumb_i2c.h" + +DUMB_I2C::DUMB_I2C(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) { + // activate the peripheral +} + +DUMB_I2C::~DUMB_I2C() { } + +uint8_t DUMB_I2C::address(void) +{ + return( m_addr >> 1 ) ; +} + +void DUMB_I2C::read(int addr, uint8_t *data, int len) +{ + readRegs(addr, data, len) ; +} + +void DUMB_I2C::write(int addr, uint8_t *data, int len) +{ + uint8_t *buf ; + buf = new uint8_t[len+1] ; + buf[0] = addr ; + for (int i = 0 ; i < len ; i++ ) { + buf[i+1] = data[i] ; + } + writeRegs(buf, len+1) ; + delete buf ; +} + +void DUMB_I2C::readRegs(int addr, uint8_t * data, int len) { + char t[1] = {addr}; + m_i2c.write(m_addr, t, 1, true); + m_i2c.read(m_addr, (char *)data, len); +} + +void DUMB_I2C::writeRegs(uint8_t * data, int len) { + m_i2c.write(m_addr, (char *)data, len); +} \ No newline at end of file