// Simple hall sensor test: Infineon TLE493D-A2B6 on the XIAO RP2040 test board
// Library: "XENSIV 3D Magnetic Sensor TLx493D" by Infineon (Library Manager)
// Open the Serial Monitor at 115200 baud, or the Serial Plotter for graphs.
// Unplug the USB for 10 seconds before testing, so the sensor starts fresh.

#include "TLx493D_inc.hpp"

using namespace ifx::tlx493d;

TLx493D_A2B6 sensor(Wire, TLx493D_IIC_ADDR_A0_e);   // address 0x35, D4 = SDA, D5 = SCL

void setup() {
  Serial.begin(115200);
  while (!Serial) delay(10);   // wait for the Serial Monitor

  if (sensor.begin()) {
    Serial.println("Sensor ready. Bring a magnet close.");
  } else {
    Serial.println("Sensor not found. Check the I2C scan (expect 0x35).");
  }
}

void loop() {
  double x, y, z, temp;
  sensor.getMagneticFieldAndTemperature(&x, &y, &z, &temp);

  // A working sensor reports roughly room temperature.
  // About -258 C means it sent no real data.
  if (temp < -40 || temp > 125) {
    Serial.println("No valid data from the sensor");
  } else {
    Serial.print("X:");  Serial.print(x, 1);
    Serial.print(" Y:"); Serial.print(y, 1);
    Serial.print(" Z:"); Serial.println(z, 1);
  }

  delay(200);   // 5 readings per second
}
