#include <Arduino.h>
#include <WF100DPZ.h>

// Wire these pins to your sensor module.
static constexpr int PIN_CSB = 10;   // Chip select
static constexpr int PIN_SDA = 11;   // Sensor "SDA" pin (shared SPI data line)
static constexpr int PIN_CLK = 12;  // SPI clock

// Create one sensor object using those pins.
static WF100DPZ sensor(PIN_CSB, PIN_SDA, PIN_CLK);
static bool is_ready = false;
static WF100DPZ::StatusCode st = WF100DPZ::StatusCode::Ok;

void setup() {
  Serial.begin(115200);
  delay(200);

  // Initialize the sensor once at startup.
  if (!sensor.begin(&st)) {
    Serial.printf("begin failed: %s\n", sensor.statusString(st));
    return;
  }

  Serial.println("=== WF100DPZ ContinuousRead ===");
  Serial.println("Flow: startContinuous -> continuousReady -> getContinuousResult");

  // Start one continuous stream.
  if (!sensor.startContinuous(WF100DPZ::Mode::Combined, &st)) {
    Serial.printf("startContinuous failed: %s\n", sensor.statusString(st));
    delay(1000);
    return;
  }
}

void loop() {
  // Poll until the next sample is ready.
  is_ready = sensor.continuousReady(&st);
  if (st != WF100DPZ::StatusCode::Ok) {
    Serial.printf("continuousReady failed: %s\n", sensor.statusString(st));
    delay(200);
    return;
  }

  if (!is_ready) {
    delay(2);
    return;
  }

  // Read one completed sample from the stream.
  const WF100DPZ::Sample s = sensor.getContinuousResult(&st);
  if (st != WF100DPZ::StatusCode::Ok) {
    Serial.printf("getContinuousResult failed: %s\n", sensor.statusString(st));
    delay(200);
    return;
  }

  // Print sample values and measured sample period.
  Serial.printf("T=%.2f C P=%.3f kPa period=%lu us\n", s.temp_c, s.pressure_kpa, (unsigned long)s.elapsed_us);

  delay(250);
}
