#ifndef APP_VARIABLES_H
#define APP_VARIABLES_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

extern volatile uint8_t pin_value;
extern volatile uint16_t adc_value;

extern volatile uint8_t UART1[2];
extern volatile uint8_t receive[4];
extern volatile uint8_t motor_ctrl_buffer[2];

extern volatile float rpm_left;
extern volatile float rpm_right;
extern volatile float pitch_deg;
extern volatile float pitch_offset;
extern volatile uint16_t adc_raw;
extern volatile float adc_voltage;
extern volatile float battery_voltage;

extern volatile uint8_t uart_frame_valid;
extern volatile uint32_t uart_frame_ok_count;
extern volatile uint32_t uart_frame_err_count;
extern volatile uint8_t mpu_online;
extern volatile uint8_t adc_ready;

extern volatile uint8_t esp_link_ready;
extern volatile uint8_t esp_link_stage;
extern volatile uint32_t esp_link_ok_count;
extern volatile uint32_t esp_link_retry_count;
extern volatile uint32_t esp_rx_byte_count;
extern volatile uint32_t esp_tx_byte_count;
extern uint8_t esp_wifi_connected;

/* ========================== Network control state ==========================
 * Terminal -> Server -> ESP8266(TCP transparent) -> STM32
 *
 * net_* variables are updated when a valid control frame is received.
 */
typedef struct
{
    uint8_t present;
    uint8_t gas_type;
    uint16_t conc_raw;
    uint16_t fmt_reg;
    uint8_t state;
    uint8_t miss_cnt;
    uint32_t last_ok_tick;
    uint32_t ok_count;
    uint32_t err_count;
} GasSlotState_t;

extern volatile uint8_t net_seq;
extern volatile uint8_t net_move_cmd;
extern volatile uint8_t net_led_cmd;
extern volatile uint8_t net_yun_cmd;
extern volatile uint32_t net_last_cmd_tick;
extern volatile uint32_t net_frame_ok_count;
extern volatile uint32_t net_feedback_ok_count;
extern volatile uint32_t net_feedback_fail_count;

extern volatile GasSlotState_t gas_slot1;
extern volatile GasSlotState_t gas_slot2;

#ifdef __cplusplus
}
#endif

#endif /* APP_VARIABLES_H */
