SBB Micro
Source code for the self-balancing-bike microcontroller unit (Teensy 4.1-based). 🚀️
Loading...
Searching...
No Matches
HostPort.h
Go to the documentation of this file.
1
2#ifndef _HOSTPORT_H
3#define _HOSTPORT_H
4
5#if ARDUINO >= 100
6#include "Arduino.h"
7#else
8#include "WProgram.h"
9#endif
10
53
54//using constexpr instead
55//#define BUF_SIZE 256 //size of buffers
56//#define MAX_OBJS 3 //max num of attached objects
57//#define MASK 0x000000FF //mask for bitwise operations
58//#define TIMEOUT_HOSTPORT 500 //timeout (us)
59
60class HostPort {
61public:
62 //constructors
69 HostPort(Stream* serial, uint32_t header, uint32_t terminator); //constructor with both header and terminator
70
76 HostPort(Stream* serial, uint32_t header); //constructor with header only
77
82 HostPort(Stream* serial); //constructor w/o header and terminator
83
84 //funs
93 boolean attachRx(uint8_t* pointer, size_t size); //attach object for rx
94
103 boolean attachTx(uint8_t* pointer, size_t size); //attach object for tx
104
110 boolean write(); //write attached objects, return true if data written
111
117 boolean read(); //read attached object, return true is data read
118
119 //static constexpr
120 static constexpr uint32_t NULL_HEADER = 0x00000000;
121 static constexpr uint32_t NULL_TERMINATOR = 0x00000000;
122 static constexpr uint32_t DEF_HEADER = 0xFF812345;
123 static constexpr uint32_t DEF_TERMINATOR = 0xFF8CABDE;
124
125private:
126
127 //static constexpr
128 static constexpr size_t BUF_SIZE = 256;
129 static constexpr size_t MAX_OBJS = 4;
130 static constexpr uint32_t MASK = 0x000000FF;
131 static constexpr uint32_t TIMEOUT_HOSTPORT = 500;
132
133 //funs
137 void init(void); //general init in costructors
138
142 void copyrx(void); //copy _rx_buf in _ptr_rx when reading was ok
143
144 //vars
145 uint8_t _rx_buf[BUF_SIZE] = { 0 };
146 uint8_t _tx_buf[BUF_SIZE] = { 0 };
147 uint32_t _header = NULL_HEADER;
149 uint8_t* _ptr_rx[MAX_OBJS] = { nullptr };
150 uint8_t* _ptr_tx[MAX_OBJS] = { nullptr };
151 size_t _size_rx[MAX_OBJS] = { 0 };
152 size_t _size_tx[MAX_OBJS] = { 0 };
153 size_t _totSize_rx = 0;
154 size_t _totSize_tx = 0;
155 uint8_t _numObj_rx = 0;
156 uint8_t _numObj_tx = 0;
157 Stream* _serial = nullptr;
158};
159
160#endif
boolean attachRx(uint8_t *pointer, size_t size)
Attach object for receiving.
Definition HostPort.cpp:40
size_t _size_rx[MAX_OBJS]
Sizes of receive objects.
Definition HostPort.h:151
size_t _totSize_tx
Total size of transmit objects.
Definition HostPort.h:154
uint32_t _header
4-bytes header.
Definition HostPort.h:147
static constexpr uint32_t TIMEOUT_HOSTPORT
Timeout for the reading (us).
Definition HostPort.h:131
uint8_t _numObj_tx
Total number of transmit objects.
Definition HostPort.h:156
uint8_t * _ptr_rx[MAX_OBJS]
Pointers to receive objects.
Definition HostPort.h:149
uint32_t _terminator
4-bytes terminator.
Definition HostPort.h:148
static constexpr size_t MAX_OBJS
Maximum objects.
Definition HostPort.h:129
HostPort(Stream *serial, uint32_t header, uint32_t terminator)
Contructor with header and terminator.
Definition HostPort.cpp:11
uint8_t _rx_buf[BUF_SIZE]
Receive buffer.
Definition HostPort.h:145
static constexpr uint32_t MASK
A mask for parsing stuff.
Definition HostPort.h:130
static constexpr uint32_t DEF_HEADER
4-bytes default header of data packet.
Definition HostPort.h:122
void copyrx(void)
Copy receive buffer into receive objects.
Definition HostPort.cpp:173
size_t _totSize_rx
Total size of receive objects.
Definition HostPort.h:153
boolean attachTx(uint8_t *pointer, size_t size)
Attach object for tranmission.
Definition HostPort.cpp:54
static constexpr uint32_t NULL_TERMINATOR
Null terminator.
Definition HostPort.h:121
boolean write()
Write to serial.
Definition HostPort.cpp:69
boolean read()
Read from serial.
Definition HostPort.cpp:110
uint8_t _numObj_rx
Total number of receive objects.
Definition HostPort.h:155
Stream * _serial
Pointer to Stream object.
Definition HostPort.h:157
static constexpr size_t BUF_SIZE
Buffer size.
Definition HostPort.h:128
uint8_t * _ptr_tx[MAX_OBJS]
Pointers to transmit objects.
Definition HostPort.h:150
size_t _size_tx[MAX_OBJS]
Sizes of transmit objects.
Definition HostPort.h:152
static constexpr uint32_t NULL_HEADER
Null header.
Definition HostPort.h:120
void init(void)
Initialize the host port.
Definition HostPort.cpp:30
uint8_t _tx_buf[BUF_SIZE]
Transmit buffer.
Definition HostPort.h:146
static constexpr uint32_t DEF_TERMINATOR
4-bytes terminator of data packet.
Definition HostPort.h:123