Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MjLineSerial.cpp
00001 #include "MjLineSerial.h" 00002 #include <cstdarg> 00003 00004 #define STRING_STACK_LIMIT 120 00005 00006 namespace matsujirushi { 00007 00008 MjLineSerial::MjLineSerial(RawSerial *serial) 00009 { 00010 baseSerial = serial; 00011 readLineFunc = NULL; 00012 txDelimiter = "\r\n"; 00013 echo = true; 00014 } 00015 00016 int MjLineSerial::putc(int c) 00017 { 00018 txBuffer.push(c); 00019 return c; 00020 } 00021 00022 int MjLineSerial::puts(const char *str) 00023 { 00024 for (; *str != '\0'; str++) 00025 { 00026 putc(*str); 00027 } 00028 00029 return 0; 00030 } 00031 00032 int MjLineSerial::printf(const char *format, ...) 00033 { 00034 std::va_list arg; 00035 va_start(arg, format); 00036 int len = vsnprintf(NULL, 0, format, arg); 00037 if (len < STRING_STACK_LIMIT) { 00038 char temp[STRING_STACK_LIMIT]; 00039 vsprintf(temp, format, arg); 00040 puts(temp); 00041 } else { 00042 char *temp = new char[len + 1]; 00043 vsprintf(temp, format, arg); 00044 puts(temp); 00045 delete[] temp; 00046 } 00047 va_end(arg); 00048 return len; 00049 } 00050 00051 void MjLineSerial::attachReadLine(void (*func)(const char *str)) 00052 { 00053 readLineFunc = func; 00054 } 00055 00056 void MjLineSerial::task() 00057 { 00058 while (baseSerial->readable()) 00059 { 00060 char c = baseSerial->getc(); 00061 switch (c) 00062 { 00063 case '\r': 00064 if (readLineFunc != NULL) 00065 { 00066 rxBuffer.push_back('\0'); 00067 if (echo) puts(txDelimiter); 00068 readLineFunc(rxBuffer.begin()); 00069 } 00070 rxBuffer.clear(); 00071 break; 00072 case '\b': 00073 if (rxBuffer.size() <= 0) 00074 { 00075 if (echo) putc('\a'); 00076 } 00077 else 00078 { 00079 rxBuffer.pop_back(); 00080 if (echo) puts("\b \b"); 00081 } 00082 break; 00083 default: 00084 rxBuffer.push_back(c); 00085 if (echo) putc(c); 00086 break; 00087 } 00088 } 00089 00090 while (!txBuffer.empty()) 00091 { 00092 if (!baseSerial->writeable()) break; 00093 baseSerial->putc(txBuffer.front()); 00094 txBuffer.pop(); 00095 } 00096 } 00097 00098 } // namespace matsujirushi 00099
Generated on Fri Jul 15 2022 05:02:52 by
1.7.2