This is a decent description of C++ classes, and the EndpointData class in LineInterface.h (in the Formatter code) is a simple implementation of a class. Note how definition/implementation are broken between the .h and .cpp files. Some things to know about C++ classes:
There are methods (functions associated with the class) and fields (variables associated with the class).
The class constructor is a special method with the same name as the class. It is used to create a new instance of the class (same thing as __init__ in Python). Calling the constructor reserves all the memory needed to store the object.
Class methods and fields may be declared public, private, or protected like this:
class MyClass {
public:
int a_public_field;
MyClass(); // a public constructor
private:
int a_private_field;
};
Public members are accessible from outside the class. Private members are not accessible from outside the class. Protected members are only accessible from methods of classes that inherit from the base class.
Like in Python, people sometimes use the underscore prefix/suffix to indicate that a member is private.
For UART, would it work to wrap a class with an interface like this?
class UARTPort {
public:
UARTPort(unsigned int baud_rate, unsigned short parity_bits, unsigned short data_bits, unsigned short stop_bits …);
int send(std::vector<uint8_t> msg, size_t msg_len);
int recv(std::vector<uint8_t> buff);
int fd; // the thing you get back when you open() a port
};
Inside the constructor you would create the termios configuration struct from the constructor arguments, and open the tty file (storing the return of open() in UARTPort::fd).
Then you can expose send() and recv() interface methods that shift buffers in and out. I return int in the pseudocode example above to indicate success/failure of the transmission, but you could return nothing (void) or flags as well.
You could define a deconstructor as well (~UARTPort()) that closes the port when the file is destroyed. This may just happen automatically though.
You could initialize your class with a boost::asio::io_context and try also exposing asynchronous async_send(...) and async_recv(...) methods.
A follow on from #32.
This is a decent description of C++ classes, and the EndpointData class in LineInterface.h (in the Formatter code) is a simple implementation of a class. Note how definition/implementation are broken between the .h and .cpp files. Some things to know about C++ classes:
__init__
in Python). Calling the constructor reserves all the memory needed to store the object.Class methods and fields may be declared public, private, or protected like this:
For UART, would it work to wrap a class with an interface like this?
termios
configuration struct from the constructor arguments, and open the tty file (storing the return ofopen()
inUARTPort::fd
).send()
andrecv()
interface methods that shift buffers in and out. I return int in the pseudocode example above to indicate success/failure of the transmission, but you could return nothing (void) or flags as well.~UARTPort()
) that closes the port when the file is destroyed. This may just happen automatically though.boost::asio::io_context
and try also exposing asynchronousasync_send(...)
andasync_recv(...)
methods.From Thanasi's Google Doc