#pragma once
/* vesc_can.h — VESC CAN TWAI driver for ESP32-S3 BALANCE (bd-66hx)
 *
 * VESC extended CAN ID: (packet_type << 8) | vesc_node_id
 * Physical layer: TWAI peripheral → SN65HVD230 → 500 kbps shared bus
 */

#include <stdint.h>
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"

/* ── VESC packet types ── */
#define VESC_PKT_SET_RPM    3u
#define VESC_PKT_STATUS     9u    /* int32 erpm, int16 I×10, int16 duty×1000 */
#define VESC_PKT_STATUS_4   16u   /* int16 T_fet×10, T_mot×10, I_in×10 */
#define VESC_PKT_STATUS_5   27u   /* int32 tacho, int16 V_in×10 */

/* ── VESC telemetry snapshot ── */
typedef struct {
    int32_t  erpm;          /* electrical RPM (STATUS) */
    int16_t  current_x10;   /* phase current A×10 (STATUS) */
    int16_t  voltage_x10;   /* bus voltage V×10 (STATUS_5) */
    int16_t  temp_mot_x10;  /* motor temp °C×10 (STATUS_4) */
    uint32_t last_rx_ms;    /* esp_timer ms of last STATUS frame */
} vesc_state_t;

/* ── Globals (two VESC nodes: index 0 = VESC_ID_A=56, 1 = VESC_ID_B=68) ── */
extern vesc_state_t g_vesc[2];

/* ── API ── */
void vesc_can_init(void);
void vesc_can_send_rpm(uint8_t vesc_id, int32_t erpm);

/* RX task — pass tx_queue as arg; forwards STATUS frames to Orin over serial */
void vesc_can_rx_task(void *arg);   /* arg = QueueHandle_t orin_tx_queue */
