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.
LineSerial.cpp
00001 #include "LineSerial.h" 00002 00003 LineSerial::LineSerial(PinName tx, PinName rx, const char *name) : Serial(tx, rx, name), 00004 readBuffer(NULL), 00005 readBufferSize(0), 00006 readBufferCount(0), 00007 readLineFunc(NULL) 00008 { 00009 } 00010 00011 LineSerial::~LineSerial() 00012 { 00013 if (this->readBuffer != NULL) 00014 { 00015 delete this->readBuffer; 00016 this->readBuffer = NULL; 00017 } 00018 } 00019 00020 void LineSerial::allocateReadBuffer(size_t size) 00021 { 00022 // free memory. 00023 if (this->readBuffer != NULL) 00024 { 00025 delete this->readBuffer; 00026 } 00027 00028 // allocate memory. 00029 if (size <= 0) 00030 { 00031 this->readBuffer = NULL; 00032 } 00033 else 00034 { 00035 this->readBuffer = new char[size]; 00036 } 00037 00038 // initialize related values. 00039 this->readBufferSize = size <= 0 ? 0 : size; 00040 this->readBufferCount = 0; 00041 } 00042 00043 void LineSerial::attachReadLine(void (*func)(const char* str)) 00044 { 00045 this->readLineFunc = func; 00046 } 00047 00048 void LineSerial::task() 00049 { 00050 if (this->readable()) 00051 { 00052 char c = this->getc(); 00053 switch (c) 00054 { 00055 case '\r': 00056 if (this->readLineFunc != NULL) 00057 { 00058 this->readBuffer[this->readBufferCount] = '\0'; 00059 this->readLineFunc(this->readBuffer); 00060 this->readBufferCount = 0; 00061 } 00062 break; 00063 case '\b': 00064 if (!this->removeReadBuffer()) 00065 { 00066 this->putc('\a'); 00067 } 00068 else 00069 { 00070 this->puts("\b \b"); 00071 00072 } 00073 break; 00074 default: 00075 if (!this->appendReadBuffer(c)) 00076 { 00077 this->putc('\a'); 00078 } 00079 else 00080 { 00081 this->putc(c); 00082 } 00083 break; 00084 } 00085 } 00086 } 00087 00088 bool LineSerial::appendReadBuffer(char data) 00089 { 00090 if (this->readBuffer == NULL) 00091 { 00092 return false; 00093 } 00094 if (this->readBufferCount >= this->readBufferSize - 1) 00095 { 00096 return false; 00097 } 00098 00099 this->readBuffer[this->readBufferCount++] = data; 00100 return true; 00101 } 00102 00103 bool LineSerial::removeReadBuffer() 00104 { 00105 if (this->readBuffer == NULL) 00106 { 00107 return false; 00108 } 00109 if (this->readBufferCount <= 0) 00110 { 00111 return false; 00112 } 00113 00114 this->readBufferCount--; 00115 return true; 00116 }
Generated on Mon Jul 25 2022 17:23:07 by
1.7.2