#pragma once #include #include #include #include "tusb.h" #include "ring_buffer.h" struct usb_cdc { ring_buffer tx; bool send(std::span data) { if (!tx.push(data)) return false; drain(); return true; } void send(const std::vector& data) { send(std::span{data}); } void drain() { while (!tx.empty()) { uint32_t avail = tud_cdc_write_available(); if (avail == 0) break; auto chunk = tx.read_contiguous(); if (chunk.size() > avail) chunk = chunk.first(avail); tud_cdc_write(chunk.data(), chunk.size()); tx.consume(chunk.size()); } tud_cdc_write_flush(); } };