SBB Micro
Source code for the self-balancing-bike microcontroller unit (Teensy 4.1-based). 🚀️
|
A class for communication with PC via USB/serial. More...
#include <HostPort.h>
Public Member Functions | |
HostPort (Stream *serial, uint32_t header, uint32_t terminator) | |
Contructor with header and terminator. | |
HostPort (Stream *serial, uint32_t header) | |
Contructor with header only. | |
HostPort (Stream *serial) | |
Contructor with no header and terminator. | |
boolean | attachRx (uint8_t *pointer, size_t size) |
Attach object for receiving. | |
boolean | attachTx (uint8_t *pointer, size_t size) |
Attach object for tranmission. | |
boolean | write () |
Write to serial. | |
boolean | read () |
Read from serial. | |
Static Public Attributes | |
static constexpr uint32_t | NULL_HEADER = 0x00000000 |
Null header. | |
static constexpr uint32_t | NULL_TERMINATOR = 0x00000000 |
Null terminator. | |
static constexpr uint32_t | DEF_HEADER = 0xFF812345 |
4-bytes default header of data packet. | |
static constexpr uint32_t | DEF_TERMINATOR = 0xFF8CABDE |
4-bytes terminator of data packet. | |
Private Member Functions | |
void | init (void) |
Initialize the host port. | |
void | copyrx (void) |
Copy receive buffer into receive objects. | |
Private Attributes | |
uint8_t | _rx_buf [BUF_SIZE] = { 0 } |
Receive buffer. | |
uint8_t | _tx_buf [BUF_SIZE] = { 0 } |
Transmit buffer. | |
uint32_t | _header = NULL_HEADER |
4-bytes header. | |
uint32_t | _terminator = NULL_TERMINATOR |
4-bytes terminator. | |
uint8_t * | _ptr_rx [MAX_OBJS] = { nullptr } |
Pointers to receive objects. | |
uint8_t * | _ptr_tx [MAX_OBJS] = { nullptr } |
Pointers to transmit objects. | |
size_t | _size_rx [MAX_OBJS] = { 0 } |
Sizes of receive objects. | |
size_t | _size_tx [MAX_OBJS] = { 0 } |
Sizes of transmit objects. | |
size_t | _totSize_rx = 0 |
Total size of receive objects. | |
size_t | _totSize_tx = 0 |
Total size of transmit objects. | |
uint8_t | _numObj_rx = 0 |
Total number of receive objects. | |
uint8_t | _numObj_tx = 0 |
Total number of transmit objects. | |
Stream * | _serial = nullptr |
Pointer to Stream object. | |
Static Private Attributes | |
static constexpr size_t | BUF_SIZE = 256 |
Buffer size. | |
static constexpr size_t | MAX_OBJS = 4 |
Maximum objects. | |
static constexpr uint32_t | MASK = 0x000000FF |
A mask for parsing stuff. | |
static constexpr uint32_t | TIMEOUT_HOSTPORT = 500 |
Timeout for the reading (us). | |
A class for communication with PC via USB/serial.
The class manages the communication between a microcontroller and a host PC via USB/serial. The trasmitted or received data packets consists of:
The HostPort object is created using
Objects can be attached to the trasmit buffer using (for a maximum of HostPort::MAX_OBJS objects)
Objects can be attached to the receive buffer using (for a maximum of HostPort::MAX_OBJS objects)
Finally trasfering and receiving is performed using
HostPort::HostPort | ( | Stream * | serial, |
uint32_t | header, | ||
uint32_t | terminator ) |
Contructor with header and terminator.
The constructor with user-defined header and terminator.
serial | The pointer to the Stream object used for the serial. |
header | The 4-bytes header of the data packet. Use HostPort::NULL_HEADER for no header. |
terminator | The 4-bytes terminator of the data packet. Use HostPort::NULL_TERMINATOR for no header. |
HostPort::HostPort | ( | Stream * | serial, |
uint32_t | header ) |
Contructor with header only.
The constructor with user-defined header and no terminator.
serial | The pointer to the Stream object used for the serial. |
header | The 4-bytes header of the data packet. Use HostPort::NULL_HEADER for no header. |
HostPort::HostPort | ( | Stream * | serial | ) |
Contructor with no header and terminator.
The constructor with no header and terminator.
serial | The pointer to the Stream object used for the serial. |
boolean HostPort::attachRx | ( | uint8_t * | pointer, |
size_t | size ) |
Attach object for receiving.
The function attaches an object to the receive buffer. A number of MAX_OBJS objects can be attached, for a total of maximum BUF_SIZE bytes.
pointer | The pointer to the object to attach. Cast to uint8_t is required. |
size | Thesize to the object to attach. |
boolean HostPort::attachTx | ( | uint8_t * | pointer, |
size_t | size ) |
Attach object for tranmission.
The function attaches an object to the trasmit buffer. A number of MAX_OBJS objects can be attached, for a total of maximum BUF_SIZE bytes.
pointer | The pointer to the object to attach. Cast to uint8_t is required. |
size | Thesize to the object to attach. |
|
private |
Copy receive buffer into receive objects.
The function copies the receive buffer into the receive attached objects when the read is successful.
|
private |
Initialize the host port.
The function performs the inizializaton of the host port.
boolean HostPort::read | ( | ) |
Read from serial.
The function reads a data packet from the serial and saves it in the attached receive objects.
boolean HostPort::write | ( | ) |
Write to serial.
The function writes the attached transmit objects to the serial.
|
private |
|
private |
Total number of receive objects.
The total number of receive objects must be lower than MAX_OBJS.
|
private |
Total number of transmit objects.
The total number of transmit objects must be lower than MAX_OBJS.
|
private |
|
private |
|
private |
|
private |
Pointer to Stream object.
The pointer to a Stream object representing the serial.
|
private |
|
private |
|
private |
4-bytes terminator.
The 4-bytes terminator is null when equal to NULL_TERMINATOR.
|
private |
Total size of receive objects.
The total size of receive objects must be lower than BUF_SIZE.
|
private |
Total size of transmit objects.
The total size of transmit objects must be lower than BUF_SIZE.
|
private |
|
staticconstexprprivate |
Buffer size.
The size of the transmit and receive buffers.
|
staticconstexpr |
4-bytes default header of data packet.
This value is an ovf in float, and thus it is safe.
|
staticconstexpr |
4-bytes terminator of data packet.
This value is an ovf in float.
|
staticconstexprprivate |
A mask for parsing stuff.
|
staticconstexprprivate |
Maximum objects.
The number of maximum attached objects to the transmit and receive buffers-
|
staticconstexpr |
Null header.
The value used for no header.
|
staticconstexpr |
Null terminator.
The value used for no terminator.
|
staticconstexprprivate |
Timeout for the reading (us).