#include <Arduino.h>
#include <Wire.h>

// IMPORTANT: This is code for study only. Do not assemble an unidentified QMI8658C breakout.
// QMI8658C only. Breakout power/level wiring remains blocked pending identification.
// 0x6B is the address expected by this QMI sensor candidate.
const uint8_t QMI_ADDRESS = 0x6B;
bool sensorReady = false;

// Helper function: write one configuration byte to a sensor register.
bool writeRegister(uint8_t reg, uint8_t value) {
  // Start an I2C message addressed to the QMI8658C.
  Wire.beginTransmission(QMI_ADDRESS);
  Wire.write(reg);
  Wire.write(value);
  // Return true if the write was acknowledged; otherwise false.
  return Wire.endTransmission() == 0;
}

// Helper function: read count bytes, placing them into the data array through a pointer.
bool readRegisters(uint8_t reg, uint8_t *data, uint8_t count) {
  Wire.beginTransmission(QMI_ADDRESS);
  Wire.write(reg);
  // If the register selection fails or the byte count is wrong, stop and return false.
  if (Wire.endTransmission(false) != 0 ||
      Wire.requestFrom(QMI_ADDRESS, count, uint8_t(true)) != count) return false;
  // Read each byte in sequence into the caller-provided array.
  for (uint8_t i = 0; i < count; ++i) data[i] = uint8_t(Wire.read());
  return true;
}

// Start Serial and I2C, then check the chip identity before configuring it.
void setup() {
  Serial.begin(9600);
  Wire.begin();
  Wire.setClock(100000);
  delay(1000);
  uint8_t id = 0;
  // &id passes the address of id; only proceed if the expected chip ID is found.
  if (!readRegisters(0x00, &id, 1) || id != 0x05) return;
  // Chain several register writes with &&; all must succeed to set sensorReady true.
  sensorReady = writeRegister(0x02, 0x40) &&  // Address auto-increment.
                writeRegister(0x03, 0x04) &&  // Accelerometer: +/-2g, 500Hz.
                writeRegister(0x04, 0x64) &&  // Gyroscope: +/-2048dps, 500Hz.
                writeRegister(0x06, 0x11) &&  // Low-pass filters.
                writeRegister(0x08, 0x03);    // Enable accelerometer and gyro.
  delay(2000);
}

// Report failures or read fourteen sensor bytes and interpret them.
void loop() {
  if (!sensorReady) {
    Serial.println("QMI8658C not detected/configured at 0x6B (WHO_AM_I must be 0x05)");
    delay(1000);
    return;
  }
  // Store the raw fourteen bytes received from the sensor.
  uint8_t bytes[14];
  // sizeof(bytes) gets the number of bytes in this fixed array.
  if (!readRegisters(0x33, bytes, sizeof(bytes))) {
    Serial.println("Incomplete QMI8658C read");
    delay(50);
    return;
  }
  int16_t value[7];
  // Pair low and high bytes for each signed value; this sensor is little-endian.
  for (uint8_t i = 0; i < 7; ++i) {
    // Combine the two bytes into one signed 16-bit measurement.
    value[i] = int16_t((uint16_t(bytes[2 * i + 1]) << 8) | bytes[2 * i]);
  }
  // QMI8658C is little-endian; temperature is 256 LSB per degree C.
  Serial.print("AcX="); Serial.print(value[1]);
  Serial.print(" | AcY="); Serial.print(value[2]);
  Serial.print(" | AcZ="); Serial.print(value[3]);
  // Convert raw temperature using 256 counts per degree Celsius.
  Serial.print(" | Tmp="); Serial.print(value[0] / 256.0);
  Serial.print(" | GyX="); Serial.print(value[4]);
  Serial.print(" | GyY="); Serial.print(value[5]);
  Serial.print(" | GyZ="); Serial.println(value[6]);
  delay(50);
}
