Files
picomap/firmware/lib/dispatch.cpp
2026-04-07 07:43:16 +09:00

67 lines
1.6 KiB
C++

#include "dispatch.h"
#include <unordered_map>
#include "pico/stdlib.h"
#include "pico/bootrom.h"
#include "tusb.h"
#include "wire.h"
#include "usb_cdc.h"
#include "timer_queue.h"
#include "net.h"
static timer_queue timers;
void dispatch_init() {
tusb_init();
net_init();
}
void dispatch_schedule_ms(uint32_t ms, std::function<void()> fn) {
timers.schedule_ms(ms, std::move(fn));
}
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers) {
std::unordered_map<int8_t, std::vector<std::vector<uint8_t>> (*)(uint32_t, std::span<const uint8_t>)> handler_map;
for (auto& entry : handlers) {
handler_map[entry.type_id] = entry.handle;
}
static usb_cdc usb;
static static_vector<uint8_t, 256> usb_rx_buf;
while (true) {
tud_task();
usb.drain();
timers.run();
//net_poll();
while (tud_cdc_available()) {
uint8_t byte;
if (tud_cdc_read(&byte, 1) != 1) break;
usb_rx_buf.push_back(byte);
auto msg = try_decode(usb_rx_buf);
if (!msg) {
if (usb_rx_buf.full()) usb_rx_buf.clear();
continue;
}
usb_rx_buf.clear();
auto it = handler_map.find(msg->type_id);
if (it != handler_map.end()) {
for (auto& response : it->second(msg->message_id, msg->payload)) {
usb.send(response);
}
if (msg->type_id == RequestPICOBOOT::ext_id) {
sleep_ms(100);
reset_usb_boot(0, 1);
}
}
}
__wfi();
}
}