RIT VEXU Core API
Loading...
Searching...
No Matches
cobs_device.h
1#include "vex.h"
2
3#include <cstddef>
4#include <cstdint>
5#include <vector>
6
7class COBSSerialDevice {
8 // Decoded packet containing the data one wishes to send
9 using Packet = std::vector<uint8_t>;
10 // Cobs Encoded packet containing 0 delimeters ready to be sent over the wire
11 using WirePacket = std::vector<uint8_t>;
12
13 public:
19 COBSSerialDevice(int32_t port, int32_t baud);
30 int send_cobs_packet_blocking(const uint8_t *data, size_t size, bool leading_delimeter = false);
39 int receive_cobs_packet_blocking(uint8_t *buffer, size_t max_size, uint32_t timeout_us = 0);
40
48 static void cobs_encode(const Packet &in, WirePacket &out, bool add_start_delimeter = false);
54 static void cobs_decode(const WirePacket &in, Packet &out);
59 static void hexdump(const uint8_t *data, size_t len);
60
61 protected:
66 bool poll_incoming_data_once();
67
68 private:
72 bool handle_incoming_byte(uint8_t byte);
73
74 vex::mutex serial_access_mut;
75 int32_t port;
76 int32_t baud;
77
78 // Buffer to hold data about to be written
79 Packet writing_buffer;
80 // Buffer to hold encoded cobs data about to be written
81 WirePacket encoded_write;
82
83 // buffer used to get data from VEX OS land to userland. Scratch space
84 std::vector<uint8_t> incoming_buffer;
85 // buffer to read bytes in when building up a cobs packet
86 WirePacket incoming_wire_packet;
87 // contains the last packet that was received and decoded
88 Packet last_decoded_packet;
89};