Move C++ tree into firmware/
This commit is contained in:
31
firmware/include/usb_cdc.h
Normal file
31
firmware/include/usb_cdc.h
Normal 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();
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user