RIT VEXU Core API
Loading...
Searching...
No Matches
cobs_device.h
1#pragma once
2#include <cstddef>
3#include <cstdint>
4#include <vector>
5
6#include "vex/vex.h"
7
8class COBSSerialDevice {
9 public:
10 // Decoded packet containing the data one wishes to send
11 using Packet = std::vector<uint8_t>;
12 // Cobs Encoded packet containing 0 delimeters ready to be sent over the wire
13 using WirePacket = std::vector<uint8_t>;
14
20 COBSSerialDevice(int32_t port, int32_t baud);
21
33 int send_cobs_packet_blocking(const uint8_t* data, size_t size, bool leading_delimeter = false);
34
45 int receive_cobs_packet_blocking(uint8_t* buffer, size_t max_size, uint32_t timeout_us = 0);
46
54 static void cobs_encode(const Packet& in, WirePacket& out, bool add_start_delimeter = false);
55
61 static void cobs_decode(const WirePacket& in, Packet& out);
62
67 static void hexdump(const uint8_t* data, size_t len);
68
70 Packet get_last_decoded_packet();
71
72 protected:
78 bool poll_incoming_data_once();
79
85 bool handle_incoming_byte(uint8_t byte);
86
87 private:
88 vex::mutex serial_access_mut;
89 int32_t port;
90 int32_t baud;
91
92 // Buffer to hold data about to be written
93 Packet writing_buffer;
94 // Buffer to hold encoded cobs data about to be written
95 WirePacket encoded_write;
96
97 // buffer used to get data from VEX OS land to userland. Scratch space
98 std::vector<uint8_t> incoming_buffer;
99 // buffer to read bytes in when building up a cobs packet
100 WirePacket incoming_wire_packet;
101 // contains the last packet that was received and decoded
102 Packet last_decoded_packet;
103};