You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.2 KiB

#include "i2c_ch1_pImpL.hpp"
// some curious things https://www.john.geek.nz/2012/12/update-reading-data-from-a-bosch-bmp085-with-a-raspberry-pi/
i2c_ch1_pImpL::i2c_ch1_pImpL(const uint8_t& mode, ErrorHandler* err)
{
errorHandling = err;
char filename[20];
errorHandling->addNewError(-1,__FILE__,"Unable to open device",KILL);
errorHandling->addNewError(-2,__FILE__,"Unable to reach the device",KILL);
errorHandling->addNewError(-3,__FILE__,"Unable to write to device",KILL);
errorHandling->addNewError(-4,__FILE__,"Unable to read from device",KILL);
snprintf(filename, 19, "/dev/i2c-%d", 1);
deviceDescriptor = open(filename, O_RDWR);
if (deviceDescriptor < 0) {
errorHandling->handleError(-1,__FILE__);
}
}
uint8_t i2c_ch1_pImpL::writeByte(const uint8_t& address, const uint8_t& data) // retuns 0 when a sucsessful transation ocures
{
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
{
errorHandling->handleError(-2,__FILE__);
}
send_buffer[0] = data;
if ((write(deviceDescriptor, send_buffer, 1)) != 1)
{
errorHandling->handleError(-3,__FILE__);
}
return 0;
}
uint8_t i2c_ch1_pImpL::readByte(const uint8_t& address, const uint8_t& reg)
{
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
{
errorHandling->handleError(-2,__FILE__);
}
writeByte(address,reg); //Initiate a write to indicate the desired register to read
if (read(deviceDescriptor, recieve_buffer, 1) != 1) // An then initare a read request of 1 byte
{
errorHandling->handleError(-4,__FILE__);
}
return recieve_buffer[0] ;
}
uint8_t i2c_ch1_pImpL::writeWord(const uint8_t& address, const uint8_t& reg, const uint8_t& data) // retuns 0 when a sucsessful transation ocures
{
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
{
errorHandling->handleError(-2,__FILE__);
}
send_buffer[0] = reg;
send_buffer[1] = data;
if ((write(deviceDescriptor, send_buffer, 2)) != 2)
{
errorHandling->handleError(-3,__FILE__);
}
return 0;
}
uint8_t i2c_ch1_pImpL::writeWordWithReg(const uint8_t& address, const uint8_t& reg, const uint16_t& data) // retuns 0 when a sucsessful transation ocures
{
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
{
errorHandling->handleError(-2,__FILE__);
}
send_buffer[0] = reg;
send_buffer[1] = data >> 8;
send_buffer[2] = data;
if ((write(deviceDescriptor, send_buffer, 3)) != 3)
{
errorHandling->handleError(-3,__FILE__);
}
return 0;
}
uint16_t i2c_ch1_pImpL::readWord(const uint8_t& address, const uint8_t& reg)
{
uint16_t result = 0 ;
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
{
errorHandling->handleError(-2,__FILE__);
}
writeByte(address,reg); //Initiate a write to indicate the desired register to read
if (read(deviceDescriptor, recieve_buffer, 2) != 2) // An then initare a read request of 2 bytes
{
errorHandling->handleError(-4,__FILE__);
}
result = (recieve_buffer[0] << 8) + recieve_buffer[1] ;
return result ;
}
void i2c_ch1_pImpL::writeBuffer(const uint8_t& address, const uint8_t* buff, const uint8_t& len)
{
if(len > send_buffer_size){
// TODO: implement error handling here!
}
if ((write(deviceDescriptor, send_buffer, len)) != len)
{
// TODO: implement error handling here!
}
}