RIT VEXU Core API
Loading...
Searching...
No Matches
cobs_device.h
1#pragma once
2#include "vex.h"
3
4#include <cstddef>
5#include <cstdint>
6#include <vector>
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);
31 int send_cobs_packet_blocking(const uint8_t *data, size_t size, bool leading_delimeter = false);
40 int receive_cobs_packet_blocking(uint8_t *buffer, size_t max_size, uint32_t timeout_us = 0);
41
49 static void cobs_encode(const Packet &in, WirePacket &out, bool add_start_delimeter = false);
55 static void cobs_decode(const WirePacket &in, Packet &out);
60 static void hexdump(const uint8_t *data, size_t len);
61
62 Packet get_last_decoded_packet();
63
64 protected:
69 bool poll_incoming_data_once();
70
74 bool handle_incoming_byte(uint8_t byte);
75
76 private:
77 vex::mutex serial_access_mut;
78 int32_t port;
79 int32_t baud;
80
81 // Buffer to hold data about to be written
82 Packet writing_buffer;
83 // Buffer to hold encoded cobs data about to be written
84 WirePacket encoded_write;
85
86 // buffer used to get data from VEX OS land to userland. Scratch space
87 std::vector<uint8_t> incoming_buffer;
88 // buffer to read bytes in when building up a cobs packet
89 WirePacket incoming_wire_packet;
90 // contains the last packet that was received and decoded
91 Packet last_decoded_packet;
92};