Currently we have serialSetErrorCallback and serialSetEventCallback. Both take in a callback function that either gets called on an error or on an event (currently only attached/detached).
The API would be more appealing and useful if we would combine and extend those two functions into one function. For example like this:
MODULE_API auto serialSetEventCallback(
void (*callback_fn)(
cpp_core::SerialEventType event_type,
cpp_core::SerialEventContext *event_context
)
) -> int;
The cpp_core::SerialEventType could be an enum like this:
/**
* Possible event types serialSetEventCallback().
*/
enum class SerialEventType : int
{
kError = 0, ///< An error happened.
kDeviceDisconnected = 1, ///< An existing device was disconnected from the host.
kDeviceConnected = 2, ///< A new device was connected to the host.
kRead = 3, ///< Something was read from the device.
kWrite = 4, ///< Something was written to the device.
// ... can be extended if needed in the future
};
The cpp_core::SerialEventContext could be a union struct that represents all possible structs for every possible cpp_core::SerialEventType.
This whole concept could also eliminate the need for the error callback passed into every function, because we have the cpp_core::SerialEventContext that we could pass any data needed for the specific event fired.
For example the struct for the cpp_core::SerialEventType::kError could look like this:
struct Error
{
int error_code,
const char *message,
}
With this concept the API would be easy to extend with new features aka. events and such.
Currently we have
serialSetErrorCallbackandserialSetEventCallback. Both take in a callback function that either gets called on an error or on an event (currently onlyattached/detached).The API would be more appealing and useful if we would combine and extend those two functions into one function. For example like this:
The
cpp_core::SerialEventTypecould be an enum like this:The
cpp_core::SerialEventContextcould be a union struct that represents all possible structs for every possiblecpp_core::SerialEventType.This whole concept could also eliminate the need for the error callback passed into every function, because we have the
cpp_core::SerialEventContextthat we could pass any data needed for the specific event fired.For example the struct for the
cpp_core::SerialEventType::kErrorcould look like this:With this concept the API would be easy to extend with new features aka. events and such.