Chandra Tjhai
/
ENGO333_Lab_tiltmeter
Simple tiltmeter using accelerometer
Diff: ENGO333_I2C.cpp
- Revision:
- 0:7d6134e052e0
diff -r 000000000000 -r 7d6134e052e0 ENGO333_I2C.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ENGO333_I2C.cpp Sat Nov 23 04:58:09 2019 +0000 @@ -0,0 +1,40 @@ +#include "ENGO333_I2C.h" + +ENGO333_I2C::ENGO333_I2C(PinName sda, PinName scl) : i2c(sda, scl) +{ + // Do nothing +} + +ENGO333_I2C::~ENGO333_I2C() +{ + // Do nothing +} + +void ENGO333_I2C::setSpeed(int freq) +{ + this->i2c.frequency(freq); +} + +void ENGO333_I2C::writeOneByte(char deviceAddress, char registerAddress, char value) +{ + char temp[2]; + temp[0] = registerAddress; + temp[1] = value; + this->i2c.write(deviceAddress, temp, 2); +} + +char ENGO333_I2C::readOneByte(char deviceAddress, char registerAddress) +{ + char temp = 0; + this->i2c.write(deviceAddress, ®isterAddress, 1); + this->i2c.read(deviceAddress, &temp, 1); + return (char)temp; +} + +void ENGO333_I2C::readBytes(char deviceAddress, char registerAddress, char* value, int length) +{ + this->i2c.write(deviceAddress, ®isterAddress, 1); + this->i2c.read(deviceAddress, value, length); +} + +