#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

// If you are new to this library, start with QuickStart first.
// This example shows how to customize a few advanced options.
static constexpr bool AUTO_REFRESH_ENABLED = true;   // Keep periodic temp refresh enabled
static constexpr uint32_t REFRESH_PERIOD_MS = 1000;  // Refresh every 1 second
static constexpr uint32_t SPI_CLOCK_HZ = 20000000;   // Target SPI clock
static constexpr uint8_t SAMPLE_INTERVAL = 0x00;     // Continuous sample interval (0..15)

// Build a custom config:
// - Keep backend auto-selection
// - Set a specific SPI clock target
// - Set continuous sample interval
// - Keep temperature auto-refresh on for pressure-focused reads
static WF100DPZ::Config makeAdvancedConfig() {
  WF100DPZ::Config cfg;
  cfg.backend = WF100DPZ::Backend::Auto;
  cfg.spi_hz = SPI_CLOCK_HZ;
  cfg.sample_interval = SAMPLE_INTERVAL;

  // Temperature auto-refresh policy:
  // Periodically run a combined read to keep pressure compensation fresh.
  cfg.auto_refresh_temp.enabled = AUTO_REFRESH_ENABLED;
  cfg.auto_refresh_temp.period_ms = REFRESH_PERIOD_MS;
  return cfg;
}

// Create one sensor object using pins + custom config.
static WF100DPZ sensor(PIN_CSB, PIN_SDA, PIN_CLK, makeAdvancedConfig());
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;
  }

  const uint8_t part_id = sensor.readPartId(&st);
  Serial.println("=== WF100DPZ AdvancedConfig ===");
  Serial.printf("PART_ID=0x%02X\\n", part_id);
}

void loop() {
  // Read one pressure-only sample and print value + raw status byte.
  const WF100DPZ::Sample s = sensor.read(WF100DPZ::Mode::PressureOnly, &st);
  if (st == WF100DPZ::StatusCode::Ok) {
    Serial.printf("P=%.3f kPa (status=0x%02X)\n", s.pressure_kpa, s.status_raw);
  } else {
    Serial.printf("read failed: %s\n", sensor.statusString(st));
  }
  delay(500);
}




