Split into main/net/tusb_config, extract ring_buffer and usb_cdc headers

This commit is contained in:
Ian Gulliver
2026-04-05 21:04:56 +09:00
parent 9fc79408dc
commit 421cb5840e
8 changed files with 164 additions and 122 deletions

31
include/usb_cdc.h Normal file
View File

@@ -0,0 +1,31 @@
#pragma once
#include <cstdint>
#include <span>
#include <vector>
#include "tusb.h"
#include "ring_buffer.h"
struct usb_cdc {
ring_buffer<512> tx;
void send(std::span<const uint8_t> data) {
tx.push(data);
drain();
}
void send(const std::vector<uint8_t>& data) {
send(std::span<const uint8_t>{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();
}
};