buffered_serial.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "mbed.h"
00012 #include "buffered_serial.h"
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 BufferedSerial::BufferedSerial(PinName tx, PinName rx, uint8_t buffer_size) :
00031 Serial(tx, rx),
00032 has_callback_(false)
00033 {
00034 buffer_size_ = 0;
00035 content_start_ = 0;
00036 content_end_ = 0;
00037 available_bytes_ = 0;
00038
00039 attach(this, &BufferedSerial::handleInterrupt);
00040
00041 buffer_ = (uint8_t *) malloc(buffer_size + 1);
00042 if(buffer_ != NULL)
00043 {
00044 buffer_size_ = buffer_size + 1;
00045 }
00046 }
00047
00048 BufferedSerial::~BufferedSerial()
00049 {
00050 if(buffer_)
00051 free(buffer_);
00052 }
00053
00054
00055
00056
00057
00058
00059
00060
00061 bool BufferedSerial::readable()
00062 {
00063 return (content_start_ != content_end_);
00064 }
00065
00066
00067
00068
00069
00070
00071
00072 uint8_t BufferedSerial::availableBytes()
00073 {
00074 return available_bytes_;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 uint8_t BufferedSerial::peek()
00088 {
00089
00090 while(content_start_ == content_end_) { wait_us(1); }
00091
00092 return buffer_[content_start_];
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 uint8_t BufferedSerial::getc()
00106 {
00107
00108 while(content_start_ == content_end_) { wait_us(1); }
00109
00110
00111 uint8_t result = buffer_[content_start_++];
00112 content_start_ = content_start_ % buffer_size_;
00113 available_bytes_--;
00114
00115 return result;
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 long BufferedSerial::readLong()
00130 {
00131 long result = (getc() << 24) |
00132 (getc() << 16) |
00133 (getc() << 8 ) |
00134 (getc() );
00135 return result;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 void BufferedSerial::readBytes(uint8_t *bytes, size_t requested_bytes)
00152 {
00153 for (size_t index = 0; index < requested_bytes; index++)
00154 bytes[index] = getc();
00155 }
00156
00157
00158
00159
00160 void BufferedSerial::flushBuffer()
00161 {
00162 content_end_ = content_start_;
00163 available_bytes_ = 0;
00164 }
00165
00166 void BufferedSerial::writeLong(long data)
00167 {
00168 putc((data >> 24) & 0xFF);
00169 putc((data >> 16) & 0xFF);
00170 putc((data >> 8) & 0xFF);
00171 putc((data ) & 0xFF);
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 void BufferedSerial::handleInterrupt()
00183 {
00184 while(Serial::readable())
00185 {
00186 if (content_start_ == (content_end_ + 1) % buffer_size_)
00187 Serial::getc();
00188 else
00189 {
00190 buffer_[content_end_++] = Serial::getc();
00191 content_end_ = content_end_ % buffer_size_;
00192 available_bytes_++;
00193 }
00194
00195 if (has_callback_ && (content_start_ == (content_end_ + 1) % buffer_size_) )
00196 callback_.call();
00197 }
00198 }