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.
SerialBuffered.cpp
00001 #include "SerialBuffered.h" 00002 00003 /** 00004 * Create a buffered serial class. 00005 * 00006 * @param tx A pin for transmit. 00007 * @param rx A pin for receive. 00008 */ 00009 SerialBuffered::SerialBuffered(PinName tx, PinName rx) : Serial(tx, rx) 00010 { 00011 indexContentStart = 0; 00012 indexContentEnd = 0; 00013 timeout = 1; 00014 00015 attach(this, &SerialBuffered::handleInterrupt); 00016 } 00017 00018 /** 00019 * Destroy. 00020 */ 00021 SerialBuffered::~SerialBuffered() 00022 { 00023 } 00024 00025 /** 00026 * Set timeout for getc(). 00027 * 00028 * @param ms milliseconds. (-1:Disable timeout) 00029 */ 00030 void SerialBuffered::setTimeout(int ms) 00031 { 00032 timeout = ms; 00033 } 00034 00035 /** 00036 * Read requested bytes. 00037 * 00038 * @param bytes A pointer to a buffer. 00039 * @param requested Length. 00040 * 00041 * @return Readed byte length. 00042 */ 00043 size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) 00044 { 00045 int i = 0; 00046 00047 while (i < requested) 00048 { 00049 int c = getc(); 00050 00051 if (c < 0) 00052 { 00053 break; 00054 } 00055 00056 bytes[i] = c; 00057 i++; 00058 } 00059 00060 return i; 00061 } 00062 00063 /** 00064 * Get a character. 00065 * 00066 * @return A character. (-1:timeout) 00067 */ 00068 int SerialBuffered::getc() 00069 { 00070 timer.reset(); 00071 timer.start(); 00072 00073 while (indexContentStart == indexContentEnd) 00074 { 00075 wait_ms(1); 00076 00077 if ((timeout > 0) && (timer.read_ms() > timeout)) 00078 { 00079 /* 00080 * Timeout occured. 00081 */ 00082 // printf("Timeout occured.\n"); 00083 return EOF; 00084 } 00085 } 00086 00087 timer.stop(); 00088 00089 uint8_t result = buffer[indexContentStart++]; 00090 indexContentStart = indexContentStart % BUFFERSIZE; 00091 00092 return result; 00093 } 00094 00095 /** 00096 * Returns 1 if there is a character available to read, otherwise. 00097 */ 00098 int SerialBuffered::readable() 00099 { 00100 return indexContentStart != indexContentEnd; 00101 } 00102 00103 void SerialBuffered::handleInterrupt() 00104 { 00105 while (Serial::readable()) 00106 { 00107 if (indexContentStart == ((indexContentEnd + 1) % BUFFERSIZE)) 00108 { 00109 /* 00110 * Buffer overrun occured. 00111 */ 00112 // printf("Buffer overrun occured.\n"); 00113 Serial::getc(); 00114 } 00115 else 00116 { 00117 buffer[indexContentEnd++] = Serial::getc(); 00118 indexContentEnd = indexContentEnd % BUFFERSIZE; 00119 } 00120 } 00121 } 00122 00123 float SerialBuffered::f_readIntTo(char delimiter) 00124 { 00125 char buffer[16]; 00126 int i; 00127 float result; 00128 for (i = 0;; i++) 00129 { 00130 //while (!readable()); 00131 00132 char number = getc(); 00133 00134 if (number == delimiter) 00135 break; 00136 00137 buffer[i] = number; 00138 } 00139 00140 buffer[i-1] = '\0'; 00141 00142 result = atof(buffer); 00143 00144 return result; 00145 } 00146 00147 00148 int SerialBuffered::i_readIntTo(char delimiter) 00149 { 00150 char buffer[16]; 00151 int i; 00152 float result; 00153 for (i = 0;; i++) 00154 { 00155 //while (!readable()); 00156 00157 char number = getc(); 00158 00159 if (number == delimiter) 00160 break; 00161 00162 buffer[i] = number; 00163 } 00164 00165 buffer[i-1] = '\0'; 00166 00167 result = atoi(buffer); 00168 00169 return result; 00170 } 00171 00172 void SerialBuffered::writeText(char* text) 00173 { 00174 for (int i = 0; text[i] != '\0' && i < BUFFER_TEXT_SIZE; i++) 00175 { 00176 while (!Serial::writeable()); 00177 00178 Serial::putc(text[i]); 00179 } 00180 }
Generated on Tue Jul 19 2022 01:26:58 by
1.7.2