microbit-dal/inc/MicroBitSerial.h

118 lines
3 KiB
C
Raw Normal View History

#ifndef MICROBIT_SERIAL_H
#define MICROBIT_SERIAL_H
#include "mbed.h"
microbit: Added configurable concurrency modes for MicroBitMessageBus handlers. MessageBus handlers can now have one of four concurrency modes for the eventuality of an event being raised whilst a previous event is still being processed. An additional (optional) parameter is provided to the listen() functions to allow this to be selected on a per event handler basis. The permissable options are: MESSAGE_BUS_LISTENER_REENTRANT: The event handler is fired with the new event, regardless of whether or not a previous event is still be processed by that handler. MESSAGE_BUS_LISTENER_QUEUE_IF_BUSY: The new event is queued until such a time as the previous event has completed execution. The new event is then processed. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_DROP_IF_BUSY: The new event is dropped, and will never be processed the the event handler. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_NONBLOCKING: The event handler is self-declaring that it never blocks. This flag is used purely for optimisation, as it permits direct execution of the event hadnelr without inducing any overhead from the scheduler. In addition, the following minor revisions were made in this release: * Cleanup of the #include dependencies contained in the microbit-dal .h files * Bugfix to the scheduler block on event matching code. * Introduced a MICROBIT_ID_ALERT MessageBus channel, for general purpose eventing using nonces.
2015-09-11 15:39:38 +00:00
#include "ManagedString.h"
#include "MicroBitImage.h"
#define MICROBIT_SERIAL_DEFAULT_BAUD_RATE 115200
#define MICROBIT_SERIAL_BUFFER_SIZE 20
#define MICROBIT_SERIAL_DEFAULT_EOF '\n'
/**
* Class definition for MicroBitSerial.
*
* Represents an instance of Serial which accepts micro:bit specific data types.
*/
class MicroBitSerial : public Serial
{
ssize_t readChars(void* buffer, size_t length, char eof = MICROBIT_SERIAL_DEFAULT_EOF);
public:
/**
* Constructor.
* Create an instance of MicroBitSerial
* @param sda the Pin to be used for SDA
* @param scl the Pin to be used for SCL
* Example:
* @code
* MicroBitSerial serial(USBTX, USBRX);
* @endcode
*/
MicroBitSerial(PinName tx, PinName rx);
/**
* Sends a managed string over serial.
*
* @param s the ManagedString to send
*
* Example:
* @code
* uBit.serial.printString("abc123");
* @endcode
*/
void sendString(ManagedString s);
/**
* Reads a ManagedString from serial
*
* @param len the buffer size for the string, default is defined by MICROBIT_SERIAL_BUFFER_SIZE
*
* Example:
* @code
* uBit.serial.readString();
* @endcode
*
* @note this member function will wait until either the buffer is full, or a \n is received
*/
ManagedString readString(int len = MICROBIT_SERIAL_BUFFER_SIZE);
/**
* Sends a MicroBitImage over serial in csv format.
*
* @param i the instance of MicroBitImage you would like to send.
*
* Example:
* @code
* const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
* MicroBitImage i(10,5,heart);
* uBit.serial.sendImage(i);
* @endcode
*/
void sendImage(MicroBitImage i);
/**
* Reads a MicroBitImage over serial, in csv format.
*
*
* @return a MicroBitImage with the format described over serial
*
* Example:
* @code
* MicroBitImage i = uBit.serial.readImage(2,2);
* @endcode
*
* Example Serial Format:
* @code
* 0,10x0a0,10x0a // 0x0a is a LF terminal which is used as a delimeter
* @endcode
* @note this will finish once the dimensions are met.
*/
MicroBitImage readImage(int width, int height);
/**
* Sends the current pixel values, byte-per-pixel, over serial
*
* Example:
* @code
* uBit.serial.sendDisplayState();
* @endcode
*/
void sendDisplayState();
/**
* Reads pixel values, byte-per-pixel, from serial, and sets the display.
*
* Example:
* @code
* uBit.serial.readDisplayState();
* @endcode
*/
void readDisplayState();
};
#endif