uTerminal is a simple AT-command style terminal processor, simplifying the work to send and parse COMMAND=value pairs to a microcontroller.

Dependencies:   USBDevice

Dependents:   uTerminal_Example

Committer:
fbcosentino
Date:
Thu Feb 21 14:51:43 2019 +0000
Revision:
3:39fb12e8a2a3
Parent:
1:4e304b58f597
bug fixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fbcosentino 0:1655adb3a46e 1 /**
fbcosentino 0:1655adb3a46e 2 * @file uTerminal.h
fbcosentino 0:1655adb3a46e 3 * @brief uTerminal is a simple AT-command style terminal processor,
fbcosentino 0:1655adb3a46e 4 * simplifying the work to send and parse COMMAND=value pairs to a
fbcosentino 0:1655adb3a46e 5 * microcontroller.
fbcosentino 0:1655adb3a46e 6 *
fbcosentino 0:1655adb3a46e 7 * You can use COMMAND=value syntax, as well as
fbcosentino 0:1655adb3a46e 8 * COMMAND=value1,value2,value3,... to sendo multiple parameters.
fbcosentino 0:1655adb3a46e 9 * Commands and values can be any string, as long as they do not contain
fbcosentino 0:1655adb3a46e 10 * the following chars: '=', '\n', '\r' or ','.
fbcosentino 0:1655adb3a46e 11 *
fbcosentino 0:1655adb3a46e 12 * @author Fernando Cosentino
fbcosentino 0:1655adb3a46e 13 */
fbcosentino 0:1655adb3a46e 14
fbcosentino 1:4e304b58f597 15 #ifndef _UTERMINAL
fbcosentino 1:4e304b58f597 16 #define _UTERMINAL
fbcosentino 0:1655adb3a46e 17
fbcosentino 0:1655adb3a46e 18 #include "mbed.h"
fbcosentino 1:4e304b58f597 19 #include "USBSerial.h"
fbcosentino 0:1655adb3a46e 20
fbcosentino 0:1655adb3a46e 21 #define UTERMINAL_BUF_SIZE 512
fbcosentino 0:1655adb3a46e 22
fbcosentino 0:1655adb3a46e 23 /** uTerminal processes messages from a serial port,
fbcosentino 0:1655adb3a46e 24 * parsing as a "COMMAND=value" pair,
fbcosentino 0:1655adb3a46e 25 * or "COMMAND=value1,value2,value3,..." set.
fbcosentino 0:1655adb3a46e 26 */
fbcosentino 0:1655adb3a46e 27 class uTerminal {
fbcosentino 0:1655adb3a46e 28 public:
fbcosentino 0:1655adb3a46e 29
fbcosentino 0:1655adb3a46e 30 /** Instances an uTerminal object.
fbcosentino 0:1655adb3a46e 31 * @param PinTX TX pin for the serial port.
fbcosentino 0:1655adb3a46e 32 * @param PinRX RX pin for the serial port.
fbcosentino 0:1655adb3a46e 33 */
fbcosentino 0:1655adb3a46e 34 uTerminal(PinName PinTX, PinName PinRX);
fbcosentino 1:4e304b58f597 35 /** Instances an uTerminal object.
fbcosentino 1:4e304b58f597 36 * @param serial_object An instance of a Serial object
fbcosentino 1:4e304b58f597 37 */
fbcosentino 1:4e304b58f597 38 uTerminal(Serial * serial_object);
fbcosentino 1:4e304b58f597 39 /** Instances an uTerminal object.
fbcosentino 1:4e304b58f597 40 * @param usbserial_object An instance of a USBSerial object
fbcosentino 1:4e304b58f597 41 */
fbcosentino 1:4e304b58f597 42 uTerminal(USBSerial * usbserial_object);
fbcosentino 1:4e304b58f597 43
fbcosentino 0:1655adb3a46e 44
fbcosentino 0:1655adb3a46e 45 /** Attaches an external function to be called whenever a new command
fbcosentino 0:1655adb3a46e 46 * is received. Format for the callback is: void function_name()
fbcosentino 0:1655adb3a46e 47 * @param callback The callback function to attach to
fbcosentino 0:1655adb3a46e 48 */
fbcosentino 0:1655adb3a46e 49 void attach( void (*callback)() );
fbcosentino 0:1655adb3a46e 50
fbcosentino 0:1655adb3a46e 51 /** Process the internal buffer and check for complete messages.
fbcosentino 0:1655adb3a46e 52 * Invokes the callback as appropriate.
fbcosentino 0:1655adb3a46e 53 * @returns
fbcosentino 0:1655adb3a46e 54 * 0 if command not yet present
fbcosentino 0:1655adb3a46e 55 * 1 if a command is present ended by a new line
fbcosentino 0:1655adb3a46e 56 * 2 if there is content filling the entire buffer
fbcosentino 0:1655adb3a46e 57 */
fbcosentino 0:1655adb3a46e 58 int Process();
fbcosentino 0:1655adb3a46e 59
fbcosentino 0:1655adb3a46e 60 /** Sets automatic mode: whenever a message is ready, Process() is
fbcosentino 0:1655adb3a46e 61 * automatically called. Use in conjunction with your own interrupt
fbcosentino 0:1655adb3a46e 62 * service routine, attached as callback via attach().
fbcosentino 0:1655adb3a46e 63 */
fbcosentino 0:1655adb3a46e 64 void ModeAuto();
fbcosentino 0:1655adb3a46e 65
fbcosentino 0:1655adb3a46e 66 /** Sets manual mode: whenever a message is ready, you have to call
fbcosentino 0:1655adb3a46e 67 * Process() to prepare the message before acting on it. This way
fbcosentino 0:1655adb3a46e 68 * you can decide the best time to poll Process() (useful in
fbcosentino 0:1655adb3a46e 69 * time-consuming code).
fbcosentino 0:1655adb3a46e 70 * This is the default mode.
fbcosentino 0:1655adb3a46e 71 */
fbcosentino 0:1655adb3a46e 72 void ModeManual();
fbcosentino 0:1655adb3a46e 73
fbcosentino 0:1655adb3a46e 74 /** Retrieves the first or next parameter from the received message.
fbcosentino 0:1655adb3a46e 75 * In the first call, fetches the first parameter. All subsequent calls
fbcosentino 0:1655adb3a46e 76 * will return the next parameter.
fbcosentino 0:1655adb3a46e 77 * @returns pointer to first char (that is, a C string)
fbcosentino 0:1655adb3a46e 78 */
fbcosentino 0:1655adb3a46e 79 char* GetParam();
fbcosentino 0:1655adb3a46e 80
fbcosentino 0:1655adb3a46e 81 /** When a new message is received, NumParams contains the
fbcosentino 0:1655adb3a46e 82 * number of parameters available to be read. If it is zero,
fbcosentino 0:1655adb3a46e 83 * the received message is just a command
fbcosentino 0:1655adb3a46e 84 */
fbcosentino 0:1655adb3a46e 85 int NumParams;
fbcosentino 0:1655adb3a46e 86
fbcosentino 0:1655adb3a46e 87 /** String buffer (array of char) containing the received command
fbcosentino 0:1655adb3a46e 88 * (in a message like "BAUD=9600,1" the command would be "BAUD")
fbcosentino 0:1655adb3a46e 89 */
fbcosentino 0:1655adb3a46e 90 char Command[UTERMINAL_BUF_SIZE+1];
fbcosentino 0:1655adb3a46e 91
fbcosentino 0:1655adb3a46e 92 /** String buffer (array of char) containing the received value
fbcosentino 0:1655adb3a46e 93 * (in a message like "BAUD=9600,1" the value would be "9600,1")
fbcosentino 0:1655adb3a46e 94 */
fbcosentino 0:1655adb3a46e 95 char Value[UTERMINAL_BUF_SIZE+1];
fbcosentino 0:1655adb3a46e 96
fbcosentino 0:1655adb3a46e 97 /** String buffer (array of char) containing the current parameter taken
fbcosentino 0:1655adb3a46e 98 * from the value. (In a message like "BAUD=9600,1", after the first call to
fbcosentino 0:1655adb3a46e 99 * GetParam() Param would be "9600", and after the second call Param
fbcosentino 0:1655adb3a46e 100 * would be "1".)
fbcosentino 0:1655adb3a46e 101 */
fbcosentino 0:1655adb3a46e 102 char Param[UTERMINAL_BUF_SIZE+1];
fbcosentino 0:1655adb3a46e 103
fbcosentino 0:1655adb3a46e 104 /** Length of useful data in Param.
fbcosentino 0:1655adb3a46e 105 * Param is actually safe to be used as a null-terminated string,
fbcosentino 0:1655adb3a46e 106 * but if the length of actual data in it is required this is it.
fbcosentino 0:1655adb3a46e 107 */
fbcosentino 0:1655adb3a46e 108 int ParamLen;
fbcosentino 0:1655adb3a46e 109
fbcosentino 0:1655adb3a46e 110 /** Prints a string to serial port.
fbcosentino 0:1655adb3a46e 111 * If you want to use printf formatting functionalities, use sprintf
fbcosentino 0:1655adb3a46e 112 * to output your formatted data into a string buffer and then call
fbcosentino 0:1655adb3a46e 113 * this method passing your buffer as parameter.
fbcosentino 0:1655adb3a46e 114 * @param str String to be sent to serial port
fbcosentino 0:1655adb3a46e 115 */
fbcosentino 0:1655adb3a46e 116 void print(char* str);
fbcosentino 0:1655adb3a46e 117 void print(const char* str);
fbcosentino 0:1655adb3a46e 118
fbcosentino 0:1655adb3a46e 119 private:
fbcosentino 1:4e304b58f597 120 Serial * _serial;
fbcosentino 1:4e304b58f597 121 USBSerial * _usbserial;
fbcosentino 1:4e304b58f597 122 void init();
fbcosentino 1:4e304b58f597 123 bool readable();
fbcosentino 1:4e304b58f597 124 unsigned char getc();
fbcosentino 0:1655adb3a46e 125 void (*_callback)();
fbcosentino 0:1655adb3a46e 126 void _rx_irq();
fbcosentino 0:1655adb3a46e 127 void transfer_buffers();
fbcosentino 0:1655adb3a46e 128
fbcosentino 0:1655adb3a46e 129 char _buffer[UTERMINAL_BUF_SIZE+1];
fbcosentino 0:1655adb3a46e 130 int buffer_len;
fbcosentino 0:1655adb3a46e 131 int params_len;
fbcosentino 0:1655adb3a46e 132 int cursor;
fbcosentino 0:1655adb3a46e 133 int param_cursor;
fbcosentino 0:1655adb3a46e 134 int has_command;
fbcosentino 0:1655adb3a46e 135 int auto_mode;
fbcosentino 1:4e304b58f597 136 int is_usb;
fbcosentino 0:1655adb3a46e 137 };
fbcosentino 1:4e304b58f597 138
fbcosentino 1:4e304b58f597 139 #endif