SBB Micro
Source code for the self-balancing-bike microcontroller unit (Teensy 4.1-based). 🚀️
Loading...
Searching...
No Matches
HostPort Class Reference

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).
 

Detailed Description

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

HostPort hostPort(&Serial, HostPort::NULL_HEADER, HostPort::NULL_TERMINATOR); //with neither header nor terminator
HostPort hostPort(&Serial); //with neither header nor terminator
HostPort hostPort(&Serial, header, HostPort::NULL_TERMINATOR); //with header only
HostPort hostPort(&Serial, header); //with header only
HostPort hostPort(&Serial, HostPort::NULL_HEADER, terminator); //with terminator only
HostPort hostPort(&Serial, header, terminator); //with header and terminator
HostPort(Stream *serial, uint32_t header, uint32_t terminator)
Contructor with header and terminator.
Definition HostPort.cpp:11
static constexpr uint32_t NULL_TERMINATOR
Null terminator.
Definition HostPort.h:121
static constexpr uint32_t NULL_HEADER
Null header.
Definition HostPort.h:120

Objects can be attached to the trasmit buffer using (for a maximum of HostPort::MAX_OBJS objects)

hostPort.attachTx((uint8_t*) &object1, sizeof(object1));
hostPort.attachTx((uint8_t*) &object2, sizeof(object2));

Objects can be attached to the receive buffer using (for a maximum of HostPort::MAX_OBJS objects)

hostPort.attachRx((uint8_t*) &object3, sizeof(object3));
hostPort.attachRx((uint8_t*) &object4, sizeof(object4));

Finally trasfering and receiving is performed using

hostPort.write();
hostPort.read();
Author
Stefano Lovato
Date
2022

Constructor & Destructor Documentation

◆ HostPort() [1/3]

HostPort::HostPort ( Stream * serial,
uint32_t header,
uint32_t terminator )

Contructor with header and terminator.

The constructor with user-defined header and terminator.

Parameters
serialThe pointer to the Stream object used for the serial.
headerThe 4-bytes header of the data packet. Use HostPort::NULL_HEADER for no header.
terminatorThe 4-bytes terminator of the data packet. Use HostPort::NULL_TERMINATOR for no header.

◆ HostPort() [2/3]

HostPort::HostPort ( Stream * serial,
uint32_t header )

Contructor with header only.

The constructor with user-defined header and no terminator.

Parameters
serialThe pointer to the Stream object used for the serial.
headerThe 4-bytes header of the data packet. Use HostPort::NULL_HEADER for no header.

◆ HostPort() [3/3]

HostPort::HostPort ( Stream * serial)

Contructor with no header and terminator.

The constructor with no header and terminator.

Parameters
serialThe pointer to the Stream object used for the serial.

Member Function Documentation

◆ attachRx()

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.

Parameters
pointerThe pointer to the object to attach. Cast to uint8_t is required.
sizeThesize to the object to attach.
Returns
True if success, false if the buffer size or the number of objects exceeds BUF_SIZE or MAX_OBJS.
See also
MAX_OBJS BUF_SIZE

◆ attachTx()

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.

Parameters
pointerThe pointer to the object to attach. Cast to uint8_t is required.
sizeThesize to the object to attach.
Returns
True if success, false if the buffer size or the number of objects exceeds BUF_SIZE or MAX_OBJS.
See also
MAX_OBJS BUF_SIZE

◆ copyrx()

void HostPort::copyrx ( void )
private

Copy receive buffer into receive objects.

The function copies the receive buffer into the receive attached objects when the read is successful.

◆ init()

void HostPort::init ( void )
private

Initialize the host port.

The function performs the inizializaton of the host port.

◆ read()

boolean HostPort::read ( )

Read from serial.

The function reads a data packet from the serial and saves it in the attached receive objects.

Returns
True if success.
Attention
The serial object must be started by the user before reading.

◆ write()

boolean HostPort::write ( )

Write to serial.

The function writes the attached transmit objects to the serial.

Returns
True if success.
Attention
The serial object must be started by the user before writing.

Member Data Documentation

◆ _header

uint32_t HostPort::_header = NULL_HEADER
private

4-bytes header.

The 4-bytes header is null when equal to NULL_HEADER.

See also
NULL_HEADER

◆ _numObj_rx

uint8_t HostPort::_numObj_rx = 0
private

Total number of receive objects.

The total number of receive objects must be lower than MAX_OBJS.

See also
MAX_OBJS

◆ _numObj_tx

uint8_t HostPort::_numObj_tx = 0
private

Total number of transmit objects.

The total number of transmit objects must be lower than MAX_OBJS.

See also
MAX_OBJS

◆ _ptr_rx

uint8_t* HostPort::_ptr_rx[MAX_OBJS] = { nullptr }
private

Pointers to receive objects.

The arrays of pointers to the receive attached objects. It has size MAX_OBJS.

See also
MAX_OBJS attachRx

◆ _ptr_tx

uint8_t* HostPort::_ptr_tx[MAX_OBJS] = { nullptr }
private

Pointers to transmit objects.

The arrays of pointers to the transmit attached objects. It has size MAX_OBJS.

See also
MAX_OBJS attachTx

◆ _rx_buf

uint8_t HostPort::_rx_buf[BUF_SIZE] = { 0 }
private

Receive buffer.

The receive buffer has size BUF_SIZE.

See also
BUF_SIZE attachRx

◆ _serial

Stream* HostPort::_serial = nullptr
private

Pointer to Stream object.

The pointer to a Stream object representing the serial.

Attention
The serial must be started before using this class.

◆ _size_rx

size_t HostPort::_size_rx[MAX_OBJS] = { 0 }
private

Sizes of receive objects.

The arrays of sizes of the receive attached objects. It has size MAX_OBJS.

See also
MAX_OBJS attachRx

◆ _size_tx

size_t HostPort::_size_tx[MAX_OBJS] = { 0 }
private

Sizes of transmit objects.

The arrays of sizes of the transmit attached objects. It has size MAX_OBJS.

See also
MAX_OBJS attachTx

◆ _terminator

uint32_t HostPort::_terminator = NULL_TERMINATOR
private

4-bytes terminator.

The 4-bytes terminator is null when equal to NULL_TERMINATOR.

See also
NULL_TERMINATOR

◆ _totSize_rx

size_t HostPort::_totSize_rx = 0
private

Total size of receive objects.

The total size of receive objects must be lower than BUF_SIZE.

See also
BUF_SIZE

◆ _totSize_tx

size_t HostPort::_totSize_tx = 0
private

Total size of transmit objects.

The total size of transmit objects must be lower than BUF_SIZE.

See also
BUF_SIZE

◆ _tx_buf

uint8_t HostPort::_tx_buf[BUF_SIZE] = { 0 }
private

Transmit buffer.

The transmit buffer has size BUF_SIZE.

See also
BUF_SIZE attachTx

◆ BUF_SIZE

size_t HostPort::BUF_SIZE = 256
staticconstexprprivate

Buffer size.

The size of the transmit and receive buffers.

◆ DEF_HEADER

uint32_t HostPort::DEF_HEADER = 0xFF812345
staticconstexpr

4-bytes default header of data packet.

This value is an ovf in float, and thus it is safe.

◆ DEF_TERMINATOR

uint32_t HostPort::DEF_TERMINATOR = 0xFF8CABDE
staticconstexpr

4-bytes terminator of data packet.

This value is an ovf in float.

◆ MASK

uint32_t HostPort::MASK = 0x000000FF
staticconstexprprivate

A mask for parsing stuff.

◆ MAX_OBJS

size_t HostPort::MAX_OBJS = 4
staticconstexprprivate

Maximum objects.

The number of maximum attached objects to the transmit and receive buffers-

◆ NULL_HEADER

uint32_t HostPort::NULL_HEADER = 0x00000000
staticconstexpr

Null header.

The value used for no header.

◆ NULL_TERMINATOR

uint32_t HostPort::NULL_TERMINATOR = 0x00000000
staticconstexpr

Null terminator.

The value used for no terminator.

◆ TIMEOUT_HOSTPORT

uint32_t HostPort::TIMEOUT_HOSTPORT = 500
staticconstexprprivate

Timeout for the reading (us).