add debugs
This commit is contained in:
parent
010fca9a96
commit
4d11a426af
1 changed files with 67 additions and 8 deletions
|
|
@ -50,24 +50,45 @@ Adafruit_OPT4048::~Adafruit_OPT4048() {
|
|||
* @return true if initialization was successful, false otherwise.
|
||||
*/
|
||||
bool Adafruit_OPT4048::begin(uint8_t addr, TwoWire *wire) {
|
||||
Serial.print(F("DEBUG: Begin with address 0x"));
|
||||
Serial.println(addr, HEX);
|
||||
|
||||
// Clean up old instance if reinitializing
|
||||
if (i2c_dev) {
|
||||
Serial.println(F("DEBUG: Cleaning up old I2C device"));
|
||||
delete i2c_dev;
|
||||
i2c_dev = nullptr;
|
||||
}
|
||||
|
||||
// Create I2C device
|
||||
i2c_dev = new Adafruit_I2CDevice(addr, wire);
|
||||
if (!i2c_dev || !i2c_dev->begin()) {
|
||||
if (!i2c_dev) {
|
||||
Serial.println(F("DEBUG: Failed to create I2C device"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!i2c_dev->begin()) {
|
||||
Serial.println(F("DEBUG: Failed to begin I2C device"));
|
||||
return false;
|
||||
}
|
||||
|
||||
Serial.println(F("DEBUG: I2C device initialized successfully"));
|
||||
|
||||
// Verify device ID to ensure correct chip is connected
|
||||
{
|
||||
Adafruit_BusIO_Register idreg(i2c_dev, OPT4048_REG_DEVICE_ID, 2, MSBFIRST);
|
||||
uint16_t id = idreg.read();
|
||||
|
||||
Serial.print(F("DEBUG: Device ID = 0x"));
|
||||
Serial.println(id, HEX);
|
||||
|
||||
// Default reset device ID is 0x0821
|
||||
if (id != 0x0821) {
|
||||
Serial.println(F("DEBUG: Invalid device ID, expecting 0x0821"));
|
||||
return false;
|
||||
}
|
||||
|
||||
Serial.println(F("DEBUG: Device ID verified correctly"));
|
||||
}
|
||||
|
||||
// Set interrupt direction to default (high threshold active)
|
||||
|
|
@ -94,26 +115,49 @@ bool Adafruit_OPT4048::begin(uint8_t addr, TwoWire *wire) {
|
|||
*/
|
||||
bool Adafruit_OPT4048::getChannelsRaw(uint32_t *ch0, uint32_t *ch1, uint32_t *ch2, uint32_t *ch3) {
|
||||
if (!i2c_dev) {
|
||||
Serial.println(F("DEBUG: i2c_dev is null"));
|
||||
return false;
|
||||
}
|
||||
uint8_t buf[16];
|
||||
uint8_t reg = OPT4048_REG_CH0_MSB;
|
||||
if (!i2c_dev->write_then_read(®, 1, buf, sizeof(buf))) {
|
||||
Serial.println(F("DEBUG: I2C write_then_read failed"));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Debug: print the received data
|
||||
Serial.print(F("DEBUG: Raw data: "));
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
Serial.print(F("0x"));
|
||||
Serial.print(buf[i], HEX);
|
||||
Serial.print(F(", "));
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
for (uint8_t ch = 0; ch < 4; ch++) {
|
||||
uint16_t msb = ((uint16_t)buf[4 * ch] << 8) | buf[4 * ch + 1];
|
||||
uint16_t lsb = ((uint16_t)buf[4 * ch + 2] << 8) | buf[4 * ch + 3];
|
||||
uint8_t exp = (msb >> 12) & 0x0F;
|
||||
uint32_t mant = ((msb & 0x0FFF) << 8) | ((lsb >> 8) & 0xFF);
|
||||
|
||||
// Debug output for each channel
|
||||
Serial.print(F("DEBUG: CH"));
|
||||
Serial.print(ch);
|
||||
Serial.print(F(": MSB=0x"));
|
||||
Serial.print(msb, HEX);
|
||||
Serial.print(F(", LSB=0x"));
|
||||
Serial.print(lsb, HEX);
|
||||
Serial.print(F(", exp="));
|
||||
Serial.print(exp);
|
||||
Serial.print(F(", mant=0x"));
|
||||
Serial.println(mant, HEX);
|
||||
|
||||
// Convert to 20-bit mantissa << exponent format
|
||||
// This is safe because the sensor only uses exponents 0-6 in actual measurements
|
||||
// (even when auto-range mode (12) is enabled in the configuration register)
|
||||
uint32_t code = mant << exp;
|
||||
uint8_t crc = lsb & 0x0F;
|
||||
|
||||
|
||||
// Compute CRC bits
|
||||
uint8_t x0 = 0;
|
||||
for (uint8_t i = 0; i < 4; i++) {
|
||||
|
|
@ -125,28 +169,42 @@ bool Adafruit_OPT4048::getChannelsRaw(uint32_t *ch0, uint32_t *ch1, uint32_t *ch
|
|||
for (uint8_t i = 0; i < 4; i++) {
|
||||
x0 ^= (crc >> i) & 1;
|
||||
}
|
||||
|
||||
|
||||
uint8_t x1 = ((crc >> 1) & 1) ^ ((crc >> 3) & 1);
|
||||
for (uint8_t i = 1; i < 20; i += 2) {
|
||||
x1 ^= (mant >> i) & 1;
|
||||
}
|
||||
x1 ^= (exp >> 1) & 1;
|
||||
x1 ^= (exp >> 3) & 1;
|
||||
|
||||
|
||||
uint8_t x2 = ((crc >> 3) & 1);
|
||||
for (uint8_t i = 3; i < 20; i += 4) {
|
||||
x2 ^= (mant >> i) & 1;
|
||||
}
|
||||
x2 ^= (exp >> 3) & 1;
|
||||
|
||||
|
||||
uint8_t x3 = ((mant >> 3) & 1) ^ ((mant >> 11) & 1) ^ ((mant >> 19) & 1);
|
||||
|
||||
|
||||
// Debug CRC values
|
||||
Serial.print(F("DEBUG: CRC=0x"));
|
||||
Serial.print(crc, HEX);
|
||||
Serial.print(F(", Calculated: x0="));
|
||||
Serial.print(x0);
|
||||
Serial.print(F(", x1="));
|
||||
Serial.print(x1);
|
||||
Serial.print(F(", x2="));
|
||||
Serial.print(x2);
|
||||
Serial.print(F(", x3="));
|
||||
Serial.println(x3);
|
||||
|
||||
// Verify CRC
|
||||
if (((crc & 1) != x0) || (((crc >> 1) & 1) != x1) ||
|
||||
if (((crc & 1) != x0) || (((crc >> 1) & 1) != x1) ||
|
||||
(((crc >> 2) & 1) != x2) || (((crc >> 3) & 1) != x3)) {
|
||||
Serial.print(F("DEBUG: CRC check failed for channel "));
|
||||
Serial.println(ch);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Assign output
|
||||
switch (ch) {
|
||||
case 0: *ch0 = code; break;
|
||||
|
|
@ -155,6 +213,7 @@ bool Adafruit_OPT4048::getChannelsRaw(uint32_t *ch0, uint32_t *ch1, uint32_t *ch
|
|||
case 3: *ch3 = code; break;
|
||||
}
|
||||
}
|
||||
Serial.println(F("DEBUG: All channel reads successful"));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue