#ifndef BARO_H
#define BARO_H

#include <stdint.h>
#include <stdbool.h>

/*
 * baro — BME280/BMP280 barometric pressure & ambient temperature module
 *        (Issue #672).
 *
 * Reads pressure and temperature from the BME280 at BARO_READ_HZ (1 Hz),
 * computes pressure altitude using the ISA barometric formula, and publishes
 * JLINK_TLM_BARO (0x8D) telemetry to the Orin at BARO_TLM_HZ (1 Hz).
 *
 * Runs entirely on the Mamba F722S — no Orin dependency.
 * Altitude is exposed via baro_get_alt_cm() for use by slope compensation
 * in the balance PID (Issue #672 requirement).
 *
 * Usage:
 *   1. Call i2c1_init() then bmp280_init() and pass the chip_id result.
 *   2. Call baro_tick(now_ms) every ms from the main loop.
 *   3. Call baro_get_alt_cm() to read the latest altitude.
 */

/* ---- Configuration ---- */
#define BARO_READ_HZ   1u   /* sensor poll rate (Hz)      */
#define BARO_TLM_HZ    1u   /* JLink telemetry rate (Hz)  */

/* ---- Data ---- */
typedef struct {
    int32_t  pressure_pa;       /* barometric pressure (Pa)                          */
    int16_t  temp_x10;          /* ambient temperature (°C × 10; e.g. 235 = 23.5 °C) */
    int32_t  alt_cm;            /* pressure altitude above ISA sea level (cm)         */
    int16_t  humidity_pct_x10;  /* %RH × 10 (BME280 only); -1 if BMP280/absent        */
    bool     valid;             /* true once at least one reading has been obtained   */
} baro_data_t;

/* ---- API ---- */

/*
 * baro_init(chip_id) — register chip type from bmp280_init() result.
 *   chip_id : 0x58 = BMP280, 0x60 = BME280, 0 = absent/not found.
 * Call after i2c1_init() and bmp280_init(); no-op if chip_id == 0.
 */
void baro_init(int chip_id);

/*
 * baro_tick(now_ms) — rate-limited sensor read + JLink telemetry publish.
 * Call every ms from the main loop. No-op if chip absent.
 * Reads at BARO_READ_HZ; sends JLINK_TLM_BARO at BARO_TLM_HZ.
 */
void baro_tick(uint32_t now_ms);

/*
 * baro_get(out) — copy latest baro data into *out.
 * Returns true on success; false if no valid reading yet.
 */
bool baro_get(baro_data_t *out);

/*
 * baro_get_alt_cm() — latest pressure altitude (cm above ISA sea level).
 * Returns 0 if no valid reading. Used by slope compensation in balance PID.
 */
int32_t baro_get_alt_cm(void);

#endif /* BARO_H */
