TeensyNanoExoCode
Loading...
Searching...
No Matches
UARTHandler.h
Go to the documentation of this file.
1
10#ifndef UARTHandler_h
11#define UARTHandler_h
12
13//#include "Board.h"
14//#include "ParseIni.h"
15//#include "ExoData.h"
16//#include "Utilities.h"
17#include "UART_msg_t.h"
18
19#include "Arduino.h"
20#include <stdint.h>
21
22#define MAX_NUM_LEGS 2
23#define MAX_NUM_JOINTS_PER_LEG 2 // current PCB can only do 2 motors per leg.
24#define MAX_RAW_BUFFER_SIZE 256
25#define MAX_DATA_SIZE 32
26// if type is changes you will need to comment/uncomment lines in pack_float and unpack_float
27#define UART_DATA_TYPE short int
28#define FIXED_POINT_FACTOR 100
29#define UART_BAUD 256000
30
31#define MAX_RX_LEN 64 // bytes
32#define RX_TIMEOUT_US 1000 // microseconds
33
34/* SLIP special character codes
35*/
36#define END 0300 /* indicates end of packet */
37#define ESC 0333 /* indicates byte stuffing */
38#define ESC_END 0334 /* ESC ESC_END means END data byte */
39#define ESC_ESC 0335 /* ESC ESC_ESC means ESC data byte */
40
41#if defined(ARDUINO_TEENSY36) || defined(ARDUINO_TEENSY41)
42#define MY_SERIAL Serial8
43#elif defined(ARDUINO_ARDUINO_NANO33BLE) | defined(ARDUINO_NANO_RP2040_CONNECT)
44#define MY_SERIAL Serial1
45#else
46#error No Serial Object Found
47#endif
48
54{
55 public:
61 static UARTHandler* get_instance();
62
71 void UART_msg(uint8_t msg_id, uint8_t len, uint8_t joint_id, float *buffer);
72 void UART_msg(UART_msg_t msg);
73
80 UART_msg_t poll(float timeout_us = RX_TIMEOUT_US);
81
87 uint8_t check_for_data();
88
89 private:
95
96 void _pack(uint8_t msg_id, uint8_t len, uint8_t joint_id, float *data, uint8_t *data_to_pack);
97
98 UART_msg_t _unpack(uint8_t* data, uint8_t len);
99
100 uint8_t _get_packed_length(uint8_t msg_id, uint8_t len, uint8_t joint_id, float *data);
101
102 void _send_packet(uint8_t* p, uint8_t len);
103
104 int _recv_packet(uint8_t *p, uint8_t len = MAX_RX_LEN);
105
106 void _send_char(uint8_t val);
107
108 uint8_t _recv_char(void);
109
110 uint8_t _time_left(uint8_t should_latch = 0);
111
112 void _reset_partial_packet();
113
114 /* Data */
115 //circular_buffer<uint8_t, 64> _rx_raw;
116
117 float _timeout_us = RX_TIMEOUT_US;
118
119 uint8_t _partial_packet[MAX_RX_LEN];
120 uint8_t _partial_packet_len = 0;
121 uint8_t _msg_buffer[MAX_RX_LEN];
122 uint8_t _msg_buffer_len = 0;
123
124};
125
126
127
128#endif
#define RX_TIMEOUT_US
Definition UARTHandler.h:32
#define MAX_RX_LEN
Definition UARTHandler.h:31
Singleton Class to handle the UART Work.
Definition UARTHandler.h:54
void UART_msg(uint8_t msg_id, uint8_t len, uint8_t joint_id, float *buffer)
Packs and sends a UART message.
Definition UARTHandler.cpp:35
static UARTHandler * get_instance()
Get the instance object.
Definition UARTHandler.cpp:29
UART_msg_t poll(float timeout_us=RX_TIMEOUT_US)
Check for incoming data. If there is data read the message, timing out if it takes too long.
Definition UARTHandler.cpp:73
uint8_t check_for_data()
See if data is available in the UART buffer.
Definition UARTHandler.cpp:153
Definition UART_msg_t.h:9