Command line interface for UART.
UARTでコマンドラインインターフェースを実現するためのライブラリです。
UARTからコマンドが入力されるとReadLine関数をコールバックします。
コマンドのデリミタはCR(0x0d)です。
コマンドはバックスペースで消去できます。
#include "mbed.h" #include "MjLineSerial.h" using namespace matsujirushi; RawSerial pc(USBTX, USBRX); MjLineSerial line(&pc); void readLineFunc(const char *str) { if (strcmp(str, "apple") == 0) { line.puts("RINGO"); } else if (strcmp(str, "orange") == 0) { line.puts("MIKAN"); } else { line.puts("unknown."); } line.puts("\r\n"); line.putc('>'); } int main() { pc.baud(115200); line.attachReadLine(readLineFunc); line.putc('>'); for (;;) { line.task(); } }
MjLineSerial.h@2:84178b3b2824, 2014-11-19 (annotated)
- Committer:
- matsujirushi
- Date:
- Wed Nov 19 11:43:10 2014 +0000
- Revision:
- 2:84178b3b2824
- Parent:
- 1:833745ae8a7e
- Child:
- 3:e730a11160d7
Remove getc() function.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
matsujirushi | 0:40d65405e747 | 1 | #ifndef MJ_LINE_SERIAL_H |
matsujirushi | 0:40d65405e747 | 2 | #define MJ_LINE_SERIAL_H |
matsujirushi | 0:40d65405e747 | 3 | |
matsujirushi | 0:40d65405e747 | 4 | #include "mbed.h" |
matsujirushi | 1:833745ae8a7e | 5 | #include <vector> |
matsujirushi | 0:40d65405e747 | 6 | |
matsujirushi | 0:40d65405e747 | 7 | namespace matsujirushi { |
matsujirushi | 0:40d65405e747 | 8 | |
matsujirushi | 0:40d65405e747 | 9 | class MjLineSerial |
matsujirushi | 0:40d65405e747 | 10 | { |
matsujirushi | 0:40d65405e747 | 11 | public: |
matsujirushi | 0:40d65405e747 | 12 | MjLineSerial(RawSerial *serial); |
matsujirushi | 2:84178b3b2824 | 13 | |
matsujirushi | 0:40d65405e747 | 14 | int putc(int c); |
matsujirushi | 1:833745ae8a7e | 15 | int puts(const char *str); |
matsujirushi | 0:40d65405e747 | 16 | int printf(const char *format, ...); |
matsujirushi | 0:40d65405e747 | 17 | |
matsujirushi | 1:833745ae8a7e | 18 | void attachReadLine(void (*func)(const char *str)); |
matsujirushi | 1:833745ae8a7e | 19 | void task(); |
matsujirushi | 1:833745ae8a7e | 20 | |
matsujirushi | 0:40d65405e747 | 21 | private: |
matsujirushi | 0:40d65405e747 | 22 | RawSerial *baseSerial; |
matsujirushi | 1:833745ae8a7e | 23 | vector<char> rxBuffer; |
matsujirushi | 1:833745ae8a7e | 24 | void (*readLineFunc)(const char *str); |
matsujirushi | 2:84178b3b2824 | 25 | |
matsujirushi | 0:40d65405e747 | 26 | }; |
matsujirushi | 0:40d65405e747 | 27 | |
matsujirushi | 0:40d65405e747 | 28 | } // namespace matsujirushi |
matsujirushi | 0:40d65405e747 | 29 | |
matsujirushi | 0:40d65405e747 | 30 | #endif |