Skip to content

Commit

Permalink
Merge pull request esp8266#1504 from Links2004/master
Browse files Browse the repository at this point in the history
rename cbuf getSize to available, and add size function
  • Loading branch information
Links2004 committed Jan 26, 2016
2 parents 85973cd + 531d748 commit aeb9597
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
6 changes: 3 additions & 3 deletions cores/esp8266/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ int HardwareSerial::available(void) {

if (_uart != NULL && _uart->rxEnabled) {
InterruptLock il;
result = static_cast<int>(_rx_buffer->getSize());
result = static_cast<int>(_rx_buffer->available());
}

if (!result) {
Expand Down Expand Up @@ -696,7 +696,7 @@ void HardwareSerial::flush() {
while(true) {
{
InterruptLock il;
if(_tx_buffer->getSize() == 0 &&
if(_tx_buffer->available() == 0 &&
UART_GET_TX_FIFO_ROOM(uart_nr) >= UART_TX_FIFO_SIZE) {
break;
} else if(il.savedInterruptLevel() > 0) {
Expand Down Expand Up @@ -750,7 +750,7 @@ void ICACHE_RAM_ATTR HardwareSerial::_rx_complete_irq(char c) {

void ICACHE_RAM_ATTR HardwareSerial::_tx_empty_irq(void) {
const int uart_nr = _uart->uart_nr;
size_t queued = _tx_buffer->getSize();
size_t queued = _tx_buffer->available();
if(!queued) {
UART_DISARM_TX_INTERRUPT(uart_nr);
return;
Expand Down
26 changes: 15 additions & 11 deletions cores/esp8266/cbuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "c_types.h"

cbuf::cbuf(size_t size) :
_size(size), _buf(new char[size]), _bufend(_buf + size), _begin(_buf), _end(_begin) {
next(NULL), _size(size), _buf(new char[size]), _bufend(_buf + size), _begin(_buf), _end(_begin) {
}

cbuf::~cbuf() {
Expand All @@ -35,11 +35,11 @@ size_t cbuf::resizeAdd(size_t addSize) {

size_t cbuf::resize(size_t newSize) {

size_t available = getSize();
size_t bytes_available = available();

// not lose any data
// if data can be lost use remove or flush before resize
if((newSize < available) || (newSize == _size)) {
if((newSize < bytes_available) || (newSize == _size)) {
return _size;
}

Expand All @@ -51,12 +51,12 @@ size_t cbuf::resize(size_t newSize) {
}

if(_buf) {
read(newbuf, available);
memset((newbuf + available), 0x00, (newSize - available));
read(newbuf, bytes_available);
memset((newbuf + bytes_available), 0x00, (newSize - bytes_available));
}

_begin = newbuf;
_end = newbuf + available;
_end = newbuf + bytes_available;
_bufend = newbuf + newSize;
_size = newSize;

Expand All @@ -66,13 +66,17 @@ size_t cbuf::resize(size_t newSize) {
return _size;
}

size_t ICACHE_RAM_ATTR cbuf::getSize() const {
size_t ICACHE_RAM_ATTR cbuf::available() const {
if(_end >= _begin) {
return _end - _begin;
}
return _size - (_begin - _end);
}

size_t cbuf::size() {
return _size;
}

size_t cbuf::room() const {
if(_end >= _begin) {
return _size - (_end - _begin) - 1;
Expand All @@ -88,7 +92,7 @@ int cbuf::peek() {
}

size_t cbuf::peek(char *dst, size_t size) {
size_t bytes_available = getSize();
size_t bytes_available = available();
size_t size_to_read = (size < bytes_available) ? size : bytes_available;
size_t size_read = size_to_read;
char * begin = _begin;
Expand All @@ -113,7 +117,7 @@ int ICACHE_RAM_ATTR cbuf::read() {
}

size_t cbuf::read(char* dst, size_t size) {
size_t bytes_available = getSize();
size_t bytes_available = available();
size_t size_to_read = (size < bytes_available) ? size : bytes_available;
size_t size_read = size_to_read;
if(_end < _begin && size_to_read > (size_t) (_bufend - _begin)) {
Expand Down Expand Up @@ -159,7 +163,7 @@ void cbuf::flush() {
}

size_t cbuf::remove(size_t size) {
size_t bytes_available = getSize();
size_t bytes_available = available();
if(size >= bytes_available) {
flush();
return 0;
Expand All @@ -171,5 +175,5 @@ size_t cbuf::remove(size_t size) {
size_to_remove -= top_size;
}
_begin = wrap_if_bufend(_begin + size_to_remove);
return getSize();
return available();
}
6 changes: 5 additions & 1 deletion cores/esp8266/cbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class cbuf {

size_t resizeAdd(size_t addSize);
size_t resize(size_t newSize);
size_t getSize() const;
size_t available() const;
size_t size();

size_t room() const;

Expand All @@ -56,6 +57,8 @@ class cbuf {
void flush();
size_t remove(size_t size);

cbuf *next;

private:
inline char* wrap_if_bufend(char* ptr) const {
return (ptr == _bufend) ? _buf : ptr;
Expand All @@ -66,6 +69,7 @@ class cbuf {
const char* _bufend;
char* _begin;
char* _end;

};

#endif//__cbuf_h
2 changes: 1 addition & 1 deletion platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5---3rd-party-Hardware-specification

name=ESP8266 Modules
version=2.0.0
version=2.1.0

runtime.tools.xtensa-lx106-elf-gcc.path={runtime.platform.path}/tools/xtensa-lx106-elf
runtime.tools.esptool.path={runtime.platform.path}/tools/esptool
Expand Down

0 comments on commit aeb9597

Please sign in to comment.