OpenRGB/i2c_tools/i2c_tools.cpp
2019-12-20 12:22:14 -06:00

72 lines
2.6 KiB
C++

#include "i2c_tools.h"
/******************************************************************************************\
* *
* i2c_detect *
* *
* Prints a list of all detected I2C addresses on the given bus *
* *
* bus - pointer to i2c_smbus_interface to scan *
* mode - one of AUTO, QUICK, READ, FUNC - method of access *
* *
* Code adapted from i2cdetect.c from i2c-tools Linux package *
* *
\******************************************************************************************/
std::string i2c_detect(i2c_smbus_interface * bus, int mode)
{
int i, j;
int res;
int slave_addr;
char line[128];
std::string text;
sprintf(line, " 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
text.append(line);
for (i = 0; i < 128; i += 16)
{
sprintf(line, "%02x: ", i);
text.append(line);
for (j = 0; j < 16; j++)
{
/* Set slave address */
slave_addr = i + j;
/* Probe this address */
switch (mode)
{
case MODE_QUICK:
res = bus->i2c_smbus_write_quick(slave_addr, I2C_SMBUS_WRITE);
break;
case MODE_READ:
res = bus->i2c_smbus_read_byte(slave_addr);
break;
default:
if ((i + j >= 0x30 && i + j <= 0x37)
|| (i + j >= 0x50 && i + j <= 0x5F))
res = bus->i2c_smbus_read_byte(slave_addr);
else
res = bus->i2c_smbus_write_quick(slave_addr, I2C_SMBUS_WRITE);
break;
}
if (res < 0)
{
sprintf(line, "-- ");
text.append(line);
}
else
{
sprintf(line, "%02x ", i + j);
text.append(line);
}
}
sprintf(line, "\r\n");
text.append(line);
}
return text;
} /* i2c_detect() */