#ifndef LAMP_TX_QUEUE_H
#define LAMP_TX_QUEUE_H

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

#define LAMP_TX_QUEUE_CAPACITY 64u
#define LAMP_TX_QUEUE_MASK (LAMP_TX_QUEUE_CAPACITY - 1u)
#define LAMP_TX_QUEUE_USABLE (LAMP_TX_QUEUE_CAPACITY - 1u)

_Static_assert((LAMP_TX_QUEUE_CAPACITY & LAMP_TX_QUEUE_MASK) == 0u,
               "TX queue capacity must be a power of two");

/* Single-core SPSC contract: main is the only producer, USART2 ISR is the
 * only consumer. Byte index reads/writes are indivisible on the target MCU. */
typedef struct {
    uint8_t bytes[LAMP_TX_QUEUE_CAPACITY];
    volatile uint8_t head;
    volatile uint8_t tail;
} lamp_tx_queue;

void lamp_tx_queue_init(lamp_tx_queue *queue);
/* Enqueue the complete packet or leave the queue unchanged. Not for threads. */
bool lamp_tx_queue_push(lamp_tx_queue *queue, const uint8_t *bytes, size_t length);
bool lamp_tx_queue_pop(lamp_tx_queue *queue, uint8_t *byte);
size_t lamp_tx_queue_count(const lamp_tx_queue *queue);

#endif
