Simple I2C test program. open/close/read/write supported.

Dependencies:   mbed vt100

dumb_i2c.cpp

Committer:
Rhyme
Date:
2016-02-09
Revision:
0:1a2637f8e2dd
Child:
1:e105ceaee6ac

File content as of revision 0:1a2637f8e2dd:

#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);
}