avr-classes
|
The Usart interface is meant to be implemented by a hardware specific component like Usart0, but could also be implemented by some kind of software USART. An USART device is configured using an instance of the Usart::Config structure that contains high level identifiers like DATABITS_8 or PARITY_EVEN that keeps away implementation details. There is a static template method Usart::autoFill that assists in creating such a configuration, especially when dealing with baud rate calculation. When this method is called with compile-time constants, the code is heavily optimized and adds only a few bytes to the ROM image.
Example for using autoFill:
The SimpleUsart class makes use of an Usart interface passed to the constructor. It actually is just a thin wrapper around the Usart interface that performs busy waiting to provide synchronous calls for reading and writing (that means a call blocks until reading or writing is possible).
The BufferedUsart class uses buffers for incoming and outgoing data, which allows non-blocking calls to read and write. It is implemented as a template class which allows to specify the buffer sizes when creating an instance. The BufferedUsart makes use of the Data Register Empty and TX Complete interrupts provided by the AVR USART, thus operating sort of independent in the background.
The NotifyingBufferedUsart is an extension of the BufferedUsart that takes a pointer to an EventReceiver instance that is notified when the outgoing data buffer is ready to accept new data, or when the incoming data buffer is non-empty and allows data to be read. When multiple observers need to be notified, an instance of EventDispatcher can be passed (be careful with autoDispatch, as this would trigger dispatching from an ISR context). The event IDs to be used are specified by the user when creating the instance.