#ifndef MINI212G2_H_
#define MINI212G2_H_

#include "ch32v30x.h"

/* ---- Temporary debug wiring option ----
 * Set to 1 to use USART3 (PB10=TX PB11=RX) for sensor communication.
 * This steals the debug printf port — use JTAG debugger for observation.
 * Set to 0 for normal operation (USART2 / PA2+PA3). */
#define SENSOR_USE_USART3    0

/* Set to 1 to enable UART commands to configure sensor at boot.
 * Set to 0 if sensor is pre-configured via USB to auto-output CMOS/DVP.
 * Automatically forced to 1 when SENSOR_USE_USART3 is enabled. */
#if SENSOR_USE_USART3
  #define SENSOR_UART_ENABLE   1
  #define SENSOR_UART          USART3
#else
  #define SENSOR_UART_ENABLE   0
  #define SENSOR_UART          USART2
#endif
#define SENSOR_UART_BAUD     115200

/**
 * Initialize sensor. When SENSOR_UART_ENABLE=1, configures via UART.
 * When SENSOR_UART_ENABLE=0, assumes sensor is pre-configured via USB.
 * Call BEFORE DVP_Init(). Returns 0 on success.
 */
int Mini212G2_Init(void);

/* Debug variables — inspect via JTAG debugger when printf is unavailable.
 * sensor_init_ok:   number of commands that succeeded
 * sensor_init_fail: number of commands that failed
 * sensor_last_resp[]: raw bytes of last failed response
 * sensor_last_resp_len: length of last response */
extern volatile uint8_t  sensor_init_ok;
extern volatile uint8_t  sensor_init_fail;
extern volatile uint8_t  sensor_last_resp[32];
extern volatile uint8_t  sensor_last_resp_len;
/* sensor_comm_test: 0=not run, 1=got response, 0xFF=no response (timeout) */
extern volatile uint8_t  sensor_comm_test;

#if SENSOR_UART_ENABLE
/**
 * Send a raw protocol command and wait for ACK.
 * Returns 0 on ACK received, -1 on timeout/bad response.
 */
int Mini212G2_SendCmd(const uint8_t *cmd, uint8_t len);

/**
 * Query current digital video settings. Prints result to debug console.
 */
void Mini212G2_PrintDigitalVideoStatus(void);

/**
 * Trigger shutter compensation (NUC).
 */
int Mini212G2_ShutterCompensation(void);
#endif /* SENSOR_UART_ENABLE */

#endif
