Implemented first Hangar-Service

Dependencies:   CalibrateMagneto QuaternionMath

Fork of SML2 by TobyRich GmbH

I2CPeripheral.cpp

Committer:
pvaibhav
Date:
2015-01-13
Revision:
0:943820483318
Child:
1:c279bc3af90c

File content as of revision 0:943820483318:

#include "I2CPeripheral.h"
#include "mbed.h"
#define DEBUG "I2CPeripheral"
#include "Logger.h"

I2CPeripheral::I2CPeripheral(I2C &i2c, const uint8_t address) : mBus(&i2c), mAddress(address) {
    INFO("Initialised with ADDR=0x%02X", mAddress);
}

void I2CPeripheral::write_reg(const uint8_t reg, const uint8_t val)
{
    char data[2];
    data[0] = reg;
    data[1] = val;
    if (mBus->write(mAddress, data, 2)) {
        ERR("Write failed, addr=0x%02x, reg=%02Xh, data=%02Xh", reg, val);
    }
}

uint8_t I2CPeripheral::read_reg(const uint8_t reg) {
    uint8_t byte;
    if (mBus->write(mAddress, (const char*)&reg, 1, true)) {
        ERR("Can't write reg=0x%02x to read from", reg);
        mBus->stop();
        return 0;
    }
    
    if (mBus->read(mAddress, (char*)&byte, 1)) {
        ERR("Can't read reg=0x%02x", reg);
    }
    return byte;    
}