32 lines
756 B
C++
32 lines
756 B
C++
#pragma once
|
|
#include <cstdint>
|
|
#include <span>
|
|
#include <vector>
|
|
#include "tusb.h"
|
|
#include "ring_buffer.h"
|
|
|
|
struct usb_cdc {
|
|
ring_buffer<uint8_t, 8192> 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();
|
|
}
|
|
};
|