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(); } }
Revision 5:b7902dd4ab46, committed 2014-11-21
- Comitter:
- matsujirushi
- Date:
- Fri Nov 21 10:48:30 2014 +0000
- Parent:
- 4:14ef86f5de57
- Commit message:
- Add echo member.
Changed in this revision
MjLineSerial.cpp | Show annotated file Show diff for this revision Revisions of this file |
MjLineSerial.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 14ef86f5de57 -r b7902dd4ab46 MjLineSerial.cpp --- a/MjLineSerial.cpp Thu Nov 20 13:21:03 2014 +0000 +++ b/MjLineSerial.cpp Fri Nov 21 10:48:30 2014 +0000 @@ -10,6 +10,7 @@ baseSerial = serial; readLineFunc = NULL; txDelimiter = "\r\n"; + echo = true; } int MjLineSerial::putc(int c) @@ -63,7 +64,7 @@ if (readLineFunc != NULL) { rxBuffer.push_back('\0'); - puts(txDelimiter); + if (echo) puts(txDelimiter); readLineFunc(rxBuffer.begin()); } rxBuffer.clear(); @@ -71,17 +72,17 @@ case '\b': if (rxBuffer.size() <= 0) { - putc('\a'); + if (echo) putc('\a'); } else { rxBuffer.pop_back(); - puts("\b \b"); + if (echo) puts("\b \b"); } break; default: rxBuffer.push_back(c); - putc(c); + if (echo) putc(c); break; } } @@ -95,3 +96,4 @@ } } // namespace matsujirushi +
diff -r 14ef86f5de57 -r b7902dd4ab46 MjLineSerial.h --- a/MjLineSerial.h Thu Nov 20 13:21:03 2014 +0000 +++ b/MjLineSerial.h Fri Nov 21 10:48:30 2014 +0000 @@ -20,6 +20,7 @@ void task(); const char *txDelimiter; + bool echo; private: RawSerial *baseSerial;