1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

use Eclipse auto Format to get rip auf the tab, space and code style inconsistency

This commit is contained in:
Markus Sattler 2015-04-06 19:33:50 +02:00
parent 78ee754677
commit f165a0afcd
37 changed files with 3128 additions and 3329 deletions

View File

@ -144,7 +144,6 @@ volatile uint32_t* portOutputRegister(uint32_t port);
volatile uint32_t* portInputRegister(uint32_t port); volatile uint32_t* portInputRegister(uint32_t port);
volatile uint32_t* portModeRegister(uint32_t port); volatile uint32_t* portModeRegister(uint32_t port);
#define NOT_A_PIN 0 #define NOT_A_PIN 0
#define NOT_A_PORT 0 #define NOT_A_PORT 0
#define NOT_AN_INTERRUPT -1 #define NOT_AN_INTERRUPT -1

View File

@ -39,7 +39,10 @@ public:
virtual uint8_t connected() = 0; virtual uint8_t connected() = 0;
virtual operator bool() = 0; virtual operator bool() = 0;
protected: protected:
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; uint8_t* rawIPAddress(IPAddress& addr) {
return addr.raw_address();
}
;
}; };
#endif #endif

View File

@ -61,11 +61,9 @@ extern "C" {
* *
*/ */
// #################################################################################################### // ####################################################################################################
// #################################################################################################### // ####################################################################################################
// #################################################################################################### // ####################################################################################################
HardwareSerial Serial(UART0); HardwareSerial Serial(UART0);
HardwareSerial Serial1(UART1); HardwareSerial Serial1(UART1);
@ -111,7 +109,8 @@ void ICACHE_FLASH_ATTR uart_interrupt_handler(uart_t* uart) {
if(status & UART_RXFIFO_FULL_INT_ST) { if(status & UART_RXFIFO_FULL_INT_ST) {
while(true) { while(true) {
int rx_count = (READ_PERI_REG(UART_STATUS(0)) >> UART_RXFIFO_CNT_S) & UART_RXFIFO_CNT; int rx_count = (READ_PERI_REG(UART_STATUS(0)) >> UART_RXFIFO_CNT_S) & UART_RXFIFO_CNT;
if(!rx_count) break; if(!rx_count)
break;
while(rx_count--) { while(rx_count--) {
char c = READ_PERI_REG(UART_FIFO(0)) & 0xFF; char c = READ_PERI_REG(UART_FIFO(0)) & 0xFF;
@ -135,7 +134,8 @@ void ICACHE_FLASH_ATTR uart_interrupt_handler(uart_t* uart) {
if(status & UART_RXFIFO_FULL_INT_ST) { if(status & UART_RXFIFO_FULL_INT_ST) {
while(true) { while(true) {
int rx_count = (READ_PERI_REG(UART_STATUS(1)) >> UART_RXFIFO_CNT_S) & UART_RXFIFO_CNT; int rx_count = (READ_PERI_REG(UART_STATUS(1)) >> UART_RXFIFO_CNT_S) & UART_RXFIFO_CNT;
if(!rx_count) break; if(!rx_count)
break;
while(rx_count--) { while(rx_count--) {
char c = READ_PERI_REG(UART_FIFO(1)) & 0xFF; char c = READ_PERI_REG(UART_FIFO(1)) & 0xFF;
@ -160,7 +160,8 @@ void ICACHE_FLASH_ATTR uart_wait_for_tx_fifo(uart_t* uart, size_t size_needed) {
if(uart->txEnabled) { if(uart->txEnabled) {
while(true) { while(true) {
size_t tx_count = (READ_PERI_REG(UART_STATUS(uart->uart_nr)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT; size_t tx_count = (READ_PERI_REG(UART_STATUS(uart->uart_nr)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT;
if(tx_count <= (UART_TX_FIFO_SIZE - size_needed)) break; if(tx_count <= (UART_TX_FIFO_SIZE - size_needed))
break;
} }
} }
} }
@ -327,7 +328,6 @@ void ICACHE_FLASH_ATTR uart_uninit(uart_t* uart) {
break; break;
} }
pinMode(uart->rxPin, INPUT); pinMode(uart->rxPin, INPUT);
pinMode(uart->txPin, INPUT); pinMode(uart->txPin, INPUT);
@ -351,7 +351,6 @@ void ICACHE_FLASH_ATTR uart_swap(uart_t* uart) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_GPIO3);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_GPIO1); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_GPIO1);
pinMode(uart->rxPin, INPUT); pinMode(uart->rxPin, INPUT);
pinMode(uart->txPin, INPUT); pinMode(uart->txPin, INPUT);
@ -370,7 +369,6 @@ void ICACHE_FLASH_ATTR uart_swap(uart_t* uart) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, FUNC_GPIO13);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15);
pinMode(uart->rxPin, INPUT); pinMode(uart->rxPin, INPUT);
pinMode(uart->txPin, INPUT); pinMode(uart->txPin, INPUT);
uart->rxPin = 3; uart->rxPin = 3;
@ -498,12 +496,14 @@ void ICACHE_FLASH_ATTR HardwareSerial::setDebugOutput(bool en) {
} }
bool ICACHE_FLASH_ATTR HardwareSerial::isTxEnabled(void) { bool ICACHE_FLASH_ATTR HardwareSerial::isTxEnabled(void) {
if(_uart == 0) return false; if(_uart == 0)
return false;
return _uart->txEnabled; return _uart->txEnabled;
} }
bool ICACHE_FLASH_ATTR HardwareSerial::isRxEnabled(void) { bool ICACHE_FLASH_ATTR HardwareSerial::isRxEnabled(void) {
if(_uart == 0) return false; if(_uart == 0)
return false;
return _uart->rxEnabled; return _uart->rxEnabled;
} }
@ -540,8 +540,10 @@ int ICACHE_FLASH_ATTR HardwareSerial::availableForWrite(void) {
} }
void ICACHE_FLASH_ATTR HardwareSerial::flush() { void ICACHE_FLASH_ATTR HardwareSerial::flush() {
if(!_uart->txEnabled) return; if(!_uart->txEnabled)
if(!_written) return; return;
if(!_written)
return;
while(_tx_buffer->getSize() || uart_get_tx_fifo_room(_uart) < UART_TX_FIFO_SIZE) while(_tx_buffer->getSize() || uart_get_tx_fifo_room(_uart) < UART_TX_FIFO_SIZE)
yield(); yield();
@ -550,7 +552,8 @@ void ICACHE_FLASH_ATTR HardwareSerial::flush() {
} }
size_t ICACHE_FLASH_ATTR HardwareSerial::write(uint8_t c) { size_t ICACHE_FLASH_ATTR HardwareSerial::write(uint8_t c) {
if(!_uart->txEnabled) return 0; if(!_uart->txEnabled)
return 0;
_written = true; _written = true;
size_t room = uart_get_tx_fifo_room(_uart); size_t room = uart_get_tx_fifo_room(_uart);
if(room > 0 && _tx_buffer->empty()) { if(room > 0 && _tx_buffer->empty()) {

View File

@ -62,9 +62,7 @@
class cbuf; class cbuf;
typedef enum { typedef enum {
UART0 = 0, UART0 = 0, UART1 = 1, UART_NO = 0xFF
UART1 = 1,
UART_NO = 0xFF
} UARTnr_t; } UARTnr_t;
typedef struct { typedef struct {

View File

@ -21,51 +21,42 @@
#include <IPAddress.h> #include <IPAddress.h>
#include <Print.h> #include <Print.h>
IPAddress::IPAddress() IPAddress::IPAddress() {
{
_address.dword = 0; _address.dword = 0;
} }
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) {
{
_address.bytes[0] = first_octet; _address.bytes[0] = first_octet;
_address.bytes[1] = second_octet; _address.bytes[1] = second_octet;
_address.bytes[2] = third_octet; _address.bytes[2] = third_octet;
_address.bytes[3] = fourth_octet; _address.bytes[3] = fourth_octet;
} }
IPAddress::IPAddress(uint32_t address) IPAddress::IPAddress(uint32_t address) {
{
_address.dword = address; _address.dword = address;
} }
IPAddress::IPAddress(const uint8_t *address) IPAddress::IPAddress(const uint8_t *address) {
{
memcpy(_address.bytes, address, sizeof(_address.bytes)); memcpy(_address.bytes, address, sizeof(_address.bytes));
} }
IPAddress& IPAddress::operator=(const uint8_t *address) IPAddress& IPAddress::operator=(const uint8_t *address) {
{
memcpy(_address.bytes, address, sizeof(_address.bytes)); memcpy(_address.bytes, address, sizeof(_address.bytes));
return *this; return *this;
} }
IPAddress& IPAddress::operator=(uint32_t address) IPAddress& IPAddress::operator=(uint32_t address) {
{
_address.dword = address; _address.dword = address;
return *this; return *this;
} }
bool IPAddress::operator==(const uint8_t* addr) const bool IPAddress::operator==(const uint8_t* addr) const {
{
return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
} }
size_t IPAddress::printTo(Print& p) const size_t IPAddress::printTo(Print& p) const {
{
size_t n = 0; size_t n = 0;
for (int i =0; i < 3; i++) for(int i = 0; i < 3; i++) {
{
n += p.print(_address.bytes[i], DEC); n += p.print(_address.bytes[i], DEC);
n += p.print('.'); n += p.print('.');
} }

View File

@ -36,7 +36,9 @@ private:
// to the internal structure rather than a copy of the address this function should only // to the internal structure rather than a copy of the address this function should only
// be used when you know that the usage of the returned uint8_t* will be transient and not // be used when you know that the usage of the returned uint8_t* will be transient and not
// stored. // stored.
uint8_t* raw_address() { return _address.bytes; }; uint8_t* raw_address() {
return _address.bytes;
}
public: public:
// Constructors // Constructors
@ -47,13 +49,21 @@ public:
// Overloaded cast operator to allow IPAddress objects to be used where a pointer // Overloaded cast operator to allow IPAddress objects to be used where a pointer
// to a four-byte uint8_t array is expected // to a four-byte uint8_t array is expected
operator uint32_t() const { return _address.dword; }; operator uint32_t() const {
bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; }; return _address.dword;
}
bool operator==(const IPAddress& addr) const {
return _address.dword == addr._address.dword;
}
bool operator==(const uint8_t* addr) const; bool operator==(const uint8_t* addr) const;
// Overloaded index operator to allow getting and setting individual octets of the address // Overloaded index operator to allow getting and setting individual octets of the address
uint8_t operator[](int index) const { return _address.bytes[index]; }; uint8_t operator[](int index) const {
uint8_t& operator[](int index) { return _address.bytes[index]; }; return _address.bytes[index];
}
uint8_t& operator[](int index) {
return _address.bytes[index];
}
// Overloaded copy operators to allow initialisation of IPAddress objects from other types // Overloaded copy operators to allow initialisation of IPAddress objects from other types
IPAddress& operator=(const uint8_t *address); IPAddress& operator=(const uint8_t *address);
@ -71,5 +81,4 @@ public:
const IPAddress INADDR_NONE(0, 0, 0, 0); const IPAddress INADDR_NONE(0, 0, 0, 0);
#endif #endif

View File

@ -34,8 +34,7 @@ extern "C" {
// Public Methods ////////////////////////////////////////////////////////////// // Public Methods //////////////////////////////////////////////////////////////
/* default implementation: may be overridden */ /* default implementation: may be overridden */
size_t ICACHE_FLASH_ATTR Print::write(const uint8_t *buffer, size_t size) size_t ICACHE_FLASH_ATTR Print::write(const uint8_t *buffer, size_t size) {
{
size_t n = 0; size_t n = 0;
while(size--) { while(size--) {
n += write(*buffer++); n += write(*buffer++);
@ -43,38 +42,31 @@ size_t ICACHE_FLASH_ATTR Print::write(const uint8_t *buffer, size_t size)
return n; return n;
} }
size_t ICACHE_FLASH_ATTR Print::print(const String &s) size_t ICACHE_FLASH_ATTR Print::print(const String &s) {
{
return write(s.c_str(), s.length()); return write(s.c_str(), s.length());
} }
size_t ICACHE_FLASH_ATTR Print::print(const char str[]) size_t ICACHE_FLASH_ATTR Print::print(const char str[]) {
{
return write(str); return write(str);
} }
size_t ICACHE_FLASH_ATTR Print::print(char c) size_t ICACHE_FLASH_ATTR Print::print(char c) {
{
return write(c); return write(c);
} }
size_t ICACHE_FLASH_ATTR Print::print(unsigned char b, int base) size_t ICACHE_FLASH_ATTR Print::print(unsigned char b, int base) {
{
return print((unsigned long) b, base); return print((unsigned long) b, base);
} }
size_t ICACHE_FLASH_ATTR Print::print(int n, int base) size_t ICACHE_FLASH_ATTR Print::print(int n, int base) {
{
return print((long) n, base); return print((long) n, base);
} }
size_t ICACHE_FLASH_ATTR Print::print(unsigned int n, int base) size_t ICACHE_FLASH_ATTR Print::print(unsigned int n, int base) {
{
return print((unsigned long) n, base); return print((unsigned long) n, base);
} }
size_t ICACHE_FLASH_ATTR Print::print(long n, int base) size_t ICACHE_FLASH_ATTR Print::print(long n, int base) {
{
if(base == 0) { if(base == 0) {
return write(n); return write(n);
} else if(base == 10) { } else if(base == 10) {
@ -89,93 +81,81 @@ size_t ICACHE_FLASH_ATTR Print::print(long n, int base)
} }
} }
size_t ICACHE_FLASH_ATTR Print::print(unsigned long n, int base) size_t ICACHE_FLASH_ATTR Print::print(unsigned long n, int base) {
{ if(base == 0)
if (base == 0) return write(n); return write(n);
else return printNumber(n, base); else
return printNumber(n, base);
} }
size_t ICACHE_FLASH_ATTR Print::print(double n, int digits) size_t ICACHE_FLASH_ATTR Print::print(double n, int digits) {
{
return printFloat(n, digits); return printFloat(n, digits);
} }
size_t ICACHE_FLASH_ATTR Print::print(const Printable& x) size_t ICACHE_FLASH_ATTR Print::print(const Printable& x) {
{
return x.printTo(*this); return x.printTo(*this);
} }
size_t ICACHE_FLASH_ATTR Print::println(void) size_t ICACHE_FLASH_ATTR Print::println(void) {
{
size_t n = print("\r\n"); size_t n = print("\r\n");
return n; return n;
} }
size_t ICACHE_FLASH_ATTR Print::println(const String &s) size_t ICACHE_FLASH_ATTR Print::println(const String &s) {
{
size_t n = print(s); size_t n = print(s);
n += println(); n += println();
return n; return n;
} }
size_t ICACHE_FLASH_ATTR Print::println(const char c[]) size_t ICACHE_FLASH_ATTR Print::println(const char c[]) {
{
size_t n = print(c); size_t n = print(c);
n += println(); n += println();
return n; return n;
} }
size_t ICACHE_FLASH_ATTR Print::println(char c) size_t ICACHE_FLASH_ATTR Print::println(char c) {
{
size_t n = print(c); size_t n = print(c);
n += println(); n += println();
return n; return n;
} }
size_t ICACHE_FLASH_ATTR Print::println(unsigned char b, int base) size_t ICACHE_FLASH_ATTR Print::println(unsigned char b, int base) {
{
size_t n = print(b, base); size_t n = print(b, base);
n += println(); n += println();
return n; return n;
} }
size_t ICACHE_FLASH_ATTR Print::println(int num, int base) size_t ICACHE_FLASH_ATTR Print::println(int num, int base) {
{
size_t n = print(num, base); size_t n = print(num, base);
n += println(); n += println();
return n; return n;
} }
size_t ICACHE_FLASH_ATTR Print::println(unsigned int num, int base) size_t ICACHE_FLASH_ATTR Print::println(unsigned int num, int base) {
{
size_t n = print(num, base); size_t n = print(num, base);
n += println(); n += println();
return n; return n;
} }
size_t ICACHE_FLASH_ATTR Print::println(long num, int base) size_t ICACHE_FLASH_ATTR Print::println(long num, int base) {
{
size_t n = print(num, base); size_t n = print(num, base);
n += println(); n += println();
return n; return n;
} }
size_t ICACHE_FLASH_ATTR Print::println(unsigned long num, int base) size_t ICACHE_FLASH_ATTR Print::println(unsigned long num, int base) {
{
size_t n = print(num, base); size_t n = print(num, base);
n += println(); n += println();
return n; return n;
} }
size_t ICACHE_FLASH_ATTR Print::println(double num, int digits) size_t ICACHE_FLASH_ATTR Print::println(double num, int digits) {
{
size_t n = print(num, digits); size_t n = print(num, digits);
n += println(); n += println();
return n; return n;
} }
size_t ICACHE_FLASH_ATTR Print::println(const Printable& x) size_t ICACHE_FLASH_ATTR Print::println(const Printable& x) {
{
size_t n = print(x); size_t n = print(x);
n += println(); n += println();
return n; return n;
@ -190,7 +170,8 @@ size_t ICACHE_FLASH_ATTR Print::printNumber(unsigned long n, uint8_t base) {
*str = '\0'; *str = '\0';
// prevent crash if called with base == 1 // prevent crash if called with base == 1
if (base < 2) base = 10; if(base < 2)
base = 10;
do { do {
unsigned long m = n; unsigned long m = n;
@ -202,18 +183,20 @@ size_t ICACHE_FLASH_ATTR Print::printNumber(unsigned long n, uint8_t base) {
return write(str); return write(str);
} }
size_t ICACHE_FLASH_ATTR Print::printFloat(double number, uint8_t digits) size_t ICACHE_FLASH_ATTR Print::printFloat(double number, uint8_t digits) {
{
size_t n = 0; size_t n = 0;
if (isnan(number)) return print("nan"); if(isnan(number))
if (isinf(number)) return print("inf"); return print("nan");
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically if(isinf(number))
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically return print("inf");
if(number > 4294967040.0)
return print("ovf"); // constant determined empirically
if(number < -4294967040.0)
return print("ovf"); // constant determined empirically
// Handle negative numbers // Handle negative numbers
if (number < 0.0) if(number < 0.0) {
{
n += print('-'); n += print('-');
number = -number; number = -number;
} }
@ -236,8 +219,7 @@ size_t ICACHE_FLASH_ATTR Print::printFloat(double number, uint8_t digits)
} }
// Extract digits from the remainder one at a time // Extract digits from the remainder one at a time
while (digits-- > 0) while(digits-- > 0) {
{
remainder *= 10.0; remainder *= 10.0;
int toPrint = int(remainder); int toPrint = int(remainder);
n += print(toPrint); n += print(toPrint);

View File

@ -31,23 +31,31 @@
#define OCT 8 #define OCT 8
#define BIN 2 #define BIN 2
class Print class Print {
{
private: private:
int write_error; int write_error;
size_t printNumber(unsigned long, uint8_t); size_t printNumber(unsigned long, uint8_t);
size_t printFloat(double, uint8_t); size_t printFloat(double, uint8_t);
protected: protected:
void setWriteError(int err = 1) { write_error = err; } void setWriteError(int err = 1) {
write_error = err;
}
public: public:
Print() : write_error(0) {} Print() :
write_error(0) {
}
int getWriteError() { return write_error; } int getWriteError() {
void clearWriteError() { setWriteError(0); } return write_error;
}
void clearWriteError() {
setWriteError(0);
}
virtual size_t write(uint8_t) = 0; virtual size_t write(uint8_t) = 0;
size_t write(const char *str) { size_t write(const char *str) {
if (str == NULL) return 0; if(str == NULL)
return 0;
return write((const uint8_t *) str, strlen(str)); return write((const uint8_t *) str, strlen(str));
} }
virtual size_t write(const uint8_t *buffer, size_t size); virtual size_t write(const uint8_t *buffer, size_t size);

View File

@ -30,8 +30,7 @@ class Print;
Print::print and Print::println methods. Print::print and Print::println methods.
*/ */
class Printable class Printable {
{
public: public:
virtual size_t printTo(Print& p) const = 0; virtual size_t printTo(Print& p) const = 0;
}; };

View File

@ -29,26 +29,26 @@ extern "C"{
#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field #define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
// private method to read stream with timeout // private method to read stream with timeout
int ICACHE_FLASH_ATTR Stream::timedRead() int ICACHE_FLASH_ATTR Stream::timedRead() {
{
int c; int c;
_startMillis = millis(); _startMillis = millis();
do { do {
c = read(); c = read();
if (c >= 0) return c; if(c >= 0)
return c;
yield(); yield();
} while(millis() - _startMillis < _timeout); } while(millis() - _startMillis < _timeout);
return -1; // -1 indicates timeout return -1; // -1 indicates timeout
} }
// private method to peek stream with timeout // private method to peek stream with timeout
int ICACHE_FLASH_ATTR Stream::timedPeek() int ICACHE_FLASH_ATTR Stream::timedPeek() {
{
int c; int c;
_startMillis = millis(); _startMillis = millis();
do { do {
c = peek(); c = peek();
if (c >= 0) return c; if(c >= 0)
return c;
yield(); yield();
} while(millis() - _startMillis < _timeout); } while(millis() - _startMillis < _timeout);
return -1; // -1 indicates timeout return -1; // -1 indicates timeout
@ -56,14 +56,16 @@ int ICACHE_FLASH_ATTR Stream::timedPeek()
// returns peek of the next digit in the stream or -1 if timeout // returns peek of the next digit in the stream or -1 if timeout
// discards non-numeric characters // discards non-numeric characters
int ICACHE_FLASH_ATTR Stream::peekNextDigit() int ICACHE_FLASH_ATTR Stream::peekNextDigit() {
{
int c; int c;
while(1) { while(1) {
c = timedPeek(); c = timedPeek();
if (c < 0) return c; // timeout if(c < 0)
if (c == '-') return c; return c; // timeout
if (c >= '0' && c <= '9') return c; if(c == '-')
return c;
if(c >= '0' && c <= '9')
return c;
read(); // discard non-numeric read(); // discard non-numeric
} }
} }
@ -77,29 +79,25 @@ void ICACHE_FLASH_ATTR Stream::setTimeout(unsigned long timeout) // sets the ma
} }
// find returns true if the target string is found // find returns true if the target string is found
bool ICACHE_FLASH_ATTR Stream::find(const char *target) bool ICACHE_FLASH_ATTR Stream::find(const char *target) {
{
return findUntil(target, (char*) ""); return findUntil(target, (char*) "");
} }
// reads data from the stream until the target string of given length is found // reads data from the stream until the target string of given length is found
// returns true if target string is found, false if timed out // returns true if target string is found, false if timed out
bool ICACHE_FLASH_ATTR Stream::find(const char *target, size_t length) bool ICACHE_FLASH_ATTR Stream::find(const char *target, size_t length) {
{
return findUntil(target, length, NULL, 0); return findUntil(target, length, NULL, 0);
} }
// as find but search ends if the terminator string is found // as find but search ends if the terminator string is found
bool ICACHE_FLASH_ATTR Stream::findUntil(const char *target, const char *terminator) bool ICACHE_FLASH_ATTR Stream::findUntil(const char *target, const char *terminator) {
{
return findUntil(target, strlen(target), terminator, strlen(terminator)); return findUntil(target, strlen(target), terminator, strlen(terminator));
} }
// reads data from the stream until the target string of the given length is found // reads data from the stream until the target string of the given length is found
// search terminated if the terminator string is found // search terminated if the terminator string is found
// returns true if target string is found, false if terminated or timed out // returns true if target string is found, false if terminated or timed out
bool ICACHE_FLASH_ATTR Stream::findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen) bool ICACHE_FLASH_ATTR Stream::findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen) {
{
size_t index = 0; // maximum target string length is 64k bytes! size_t index = 0; // maximum target string length is 64k bytes!
size_t termIndex = 0; size_t termIndex = 0;
int c; int c;
@ -121,26 +119,22 @@ bool ICACHE_FLASH_ATTR Stream::findUntil(const char *target, size_t targetLen, c
if(termLen > 0 && c == terminator[termIndex]) { if(termLen > 0 && c == terminator[termIndex]) {
if(++termIndex >= termLen) if(++termIndex >= termLen)
return false; // return false if terminate string found before target string return false; // return false if terminate string found before target string
} } else
else
termIndex = 0; termIndex = 0;
} }
return false; return false;
} }
// returns the first valid (long) integer value from the current position. // returns the first valid (long) integer value from the current position.
// initial characters that are not digits (or the minus sign) are skipped // initial characters that are not digits (or the minus sign) are skipped
// function is terminated by the first character that is not a digit. // function is terminated by the first character that is not a digit.
long ICACHE_FLASH_ATTR Stream::parseInt() long ICACHE_FLASH_ATTR Stream::parseInt() {
{
return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout) return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
} }
// as above but a given skipChar is ignored // as above but a given skipChar is ignored
// this allows format characters (typically commas) in values to be ignored // this allows format characters (typically commas) in values to be ignored
long ICACHE_FLASH_ATTR Stream::parseInt(char skipChar) long ICACHE_FLASH_ATTR Stream::parseInt(char skipChar) {
{
boolean isNegative = false; boolean isNegative = false;
long value = 0; long value = 0;
int c; int c;
@ -159,18 +153,15 @@ long ICACHE_FLASH_ATTR Stream::parseInt(char skipChar)
value = value * 10 + c - '0'; value = value * 10 + c - '0';
read(); // consume the character we got with peek read(); // consume the character we got with peek
c = timedPeek(); c = timedPeek();
} } while((c >= '0' && c <= '9') || c == skipChar);
while( (c >= '0' && c <= '9') || c == skipChar );
if(isNegative) if(isNegative)
value = -value; value = -value;
return value; return value;
} }
// as parseInt but returns a floating point value // as parseInt but returns a floating point value
float ICACHE_FLASH_ATTR Stream::parseFloat() float ICACHE_FLASH_ATTR Stream::parseFloat() {
{
return parseFloat(NO_SKIP_CHAR); return parseFloat(NO_SKIP_CHAR);
} }
@ -202,8 +193,7 @@ float ICACHE_FLASH_ATTR Stream::parseFloat(char skipChar){
} }
read(); // consume the character we got with peek read(); // consume the character we got with peek
c = timedPeek(); c = timedPeek();
} } while((c >= '0' && c <= '9') || c == '.' || c == skipChar);
while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
if(isNegative) if(isNegative)
value = -value; value = -value;
@ -218,54 +208,50 @@ float ICACHE_FLASH_ATTR Stream::parseFloat(char skipChar){
// returns the number of characters placed in the buffer // returns the number of characters placed in the buffer
// the buffer is NOT null terminated. // the buffer is NOT null terminated.
// //
size_t ICACHE_FLASH_ATTR Stream::readBytes(char *buffer, size_t length) size_t ICACHE_FLASH_ATTR Stream::readBytes(char *buffer, size_t length) {
{
size_t count = 0; size_t count = 0;
while(count < length) { while(count < length) {
int c = timedRead(); int c = timedRead();
if (c < 0) break; if(c < 0)
break;
*buffer++ = (char) c; *buffer++ = (char) c;
count++; count++;
} }
return count; return count;
} }
// as readBytes with terminator character // as readBytes with terminator character
// terminates if length characters have been read, timeout, or if the terminator character detected // terminates if length characters have been read, timeout, or if the terminator character detected
// returns the number of characters placed in the buffer (0 means no valid data found) // returns the number of characters placed in the buffer (0 means no valid data found)
size_t ICACHE_FLASH_ATTR Stream::readBytesUntil(char terminator, char *buffer, size_t length) size_t ICACHE_FLASH_ATTR Stream::readBytesUntil(char terminator, char *buffer, size_t length) {
{ if(length < 1)
if (length < 1) return 0; return 0;
size_t index = 0; size_t index = 0;
while(index < length) { while(index < length) {
int c = timedRead(); int c = timedRead();
if (c < 0 || c == terminator) break; if(c < 0 || c == terminator)
break;
*buffer++ = (char) c; *buffer++ = (char) c;
index++; index++;
} }
return index; // return number of characters, not including null terminator return index; // return number of characters, not including null terminator
} }
String ICACHE_FLASH_ATTR Stream::readString() String ICACHE_FLASH_ATTR Stream::readString() {
{
String ret; String ret;
int c = timedRead(); int c = timedRead();
while (c >= 0) while(c >= 0) {
{
ret += (char) c; ret += (char) c;
c = timedRead(); c = timedRead();
} }
return ret; return ret;
} }
String ICACHE_FLASH_ATTR Stream::readStringUntil(char terminator) String ICACHE_FLASH_ATTR Stream::readStringUntil(char terminator) {
{
String ret; String ret;
int c = timedRead(); int c = timedRead();
while (c >= 0 && c != terminator) while(c >= 0 && c != terminator) {
{
ret += (char) c; ret += (char) c;
c = timedRead(); c = timedRead();
} }

View File

@ -35,8 +35,7 @@
readBytesBetween( pre_string, terminator, buffer, length) readBytesBetween( pre_string, terminator, buffer, length)
*/ */
class Stream : public Print class Stream: public Print {
{
protected: protected:
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
unsigned long _startMillis; // used for timeout measurement unsigned long _startMillis; // used for timeout measurement
@ -50,26 +49,35 @@ class Stream : public Print
virtual int peek() = 0; virtual int peek() = 0;
virtual void flush() = 0; virtual void flush() = 0;
Stream() {_timeout=1000;} Stream() {
_timeout = 1000;
}
// parsing methods // parsing methods
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
bool find(const char *target); // reads data from the stream until the target string is found bool find(const char *target); // reads data from the stream until the target string is found
bool find(uint8_t *target) { return find ((char *)target); } bool find(uint8_t *target) {
return find((char *) target);
}
// returns true if target string is found, false if timed out (see setTimeout) // returns true if target string is found, false if timed out (see setTimeout)
bool find(const char *target, size_t length); // reads data from the stream until the target string of given length is found bool find(const char *target, size_t length); // reads data from the stream until the target string of given length is found
bool find(const uint8_t *target, size_t length) { return find ((char *)target, length); } bool find(const uint8_t *target, size_t length) {
return find((char *) target, length);
}
// returns true if target string is found, false if timed out // returns true if target string is found, false if timed out
bool findUntil(const char *target, const char *terminator); // as find but search ends if the terminator string is found bool findUntil(const char *target, const char *terminator); // as find but search ends if the terminator string is found
bool findUntil(const uint8_t *target, const char *terminator) { return findUntil((char *)target, terminator); } bool findUntil(const uint8_t *target, const char *terminator) {
return findUntil((char *) target, terminator);
}
bool findUntil(const char *target, size_t targetLen, const char *terminate, size_t termLen); // as above but search ends if the terminate string is found bool findUntil(const char *target, size_t targetLen, const char *terminate, size_t termLen); // as above but search ends if the terminate string is found
bool findUntil(const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); } bool findUntil(const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen) {
return findUntil((char *) target, targetLen, terminate, termLen);
}
long parseInt(); // returns the first valid (long) integer value from the current position. long parseInt(); // returns the first valid (long) integer value from the current position.
// initial characters that are not digits (or the minus sign) are skipped // initial characters that are not digits (or the minus sign) are skipped
@ -78,12 +86,16 @@ class Stream : public Print
float parseFloat(); // float version of parseInt float parseFloat(); // float version of parseInt
size_t readBytes(char *buffer, size_t length); // read chars from stream into buffer size_t readBytes(char *buffer, size_t length); // read chars from stream into buffer
size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); } size_t readBytes(uint8_t *buffer, size_t length) {
return readBytes((char *) buffer, length);
}
// terminates if length characters have been read or timeout (see setTimeout) // terminates if length characters have been read or timeout (see setTimeout)
// returns the number of characters placed in the buffer (0 means no valid data found) // returns the number of characters placed in the buffer (0 means no valid data found)
size_t readBytesUntil(char terminator, char *buffer, size_t length); // as readBytes with terminator character size_t readBytesUntil(char terminator, char *buffer, size_t length); // as readBytes with terminator character
size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); } size_t readBytesUntil(char terminator, uint8_t *buffer, size_t length) {
return readBytesUntil(terminator, (char *) buffer, length);
}
// terminates if length characters have been read, timeout, or if the terminator character detected // terminates if length characters have been read, timeout, or if the terminator character detected
// returns the number of characters placed in the buffer (0 means no valid data found) // returns the number of characters placed in the buffer (0 means no valid data found)

View File

@ -35,15 +35,15 @@ Version Modified By Date Comments
#include "Arduino.h" #include "Arduino.h"
#include "pins_arduino.h" #include "pins_arduino.h"
static int8_t toneBegin(uint8_t _pin) {
static int8_t toneBegin(uint8_t _pin) //TODO implement tone
{ return 0;
} }
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
{ //TODO implement tone
} }
void noTone(uint8_t _pin) void noTone(uint8_t _pin) {
{ //TODO implement tone
} }

View File

@ -82,7 +82,9 @@ public:
// Return the port of the host who sent the current incoming packet // Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort() =0; virtual uint16_t remotePort() =0;
protected: protected:
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; uint8_t* rawIPAddress(IPAddress& addr) {
return addr.raw_address();
}
}; };
#endif #endif

View File

@ -24,7 +24,6 @@
#define isascii(__c) ((unsigned)(__c)<=0177) #define isascii(__c) ((unsigned)(__c)<=0177)
#define toascii(__c) ((__c)&0177) #define toascii(__c) ((__c)&0177)
// WCharacter.h prototypes // WCharacter.h prototypes
inline boolean isAlphaNumeric(int c) __attribute__((always_inline)); inline boolean isAlphaNumeric(int c) __attribute__((always_inline));
inline boolean isAlpha(int c) __attribute__((always_inline)); inline boolean isAlpha(int c) __attribute__((always_inline));
@ -43,128 +42,96 @@ inline int toAscii(int c) __attribute__((always_inline));
inline int toLowerCase(int c) __attribute__((always_inline)); inline int toLowerCase(int c) __attribute__((always_inline));
inline int toUpperCase(int c) __attribute__((always_inline)); inline int toUpperCase(int c) __attribute__((always_inline));
// Checks for an alphanumeric character. // Checks for an alphanumeric character.
// It is equivalent to (isalpha(c) || isdigit(c)). // It is equivalent to (isalpha(c) || isdigit(c)).
inline boolean isAlphaNumeric(int c) inline boolean isAlphaNumeric(int c) {
{
return (isalnum(c) == 0 ? false : true); return (isalnum(c) == 0 ? false : true);
} }
// Checks for an alphabetic character. // Checks for an alphabetic character.
// It is equivalent to (isupper(c) || islower(c)). // It is equivalent to (isupper(c) || islower(c)).
inline boolean isAlpha(int c) inline boolean isAlpha(int c) {
{
return (isalpha(c) == 0 ? false : true); return (isalpha(c) == 0 ? false : true);
} }
// Checks whether c is a 7-bit unsigned char value // Checks whether c is a 7-bit unsigned char value
// that fits into the ASCII character set. // that fits into the ASCII character set.
inline boolean isAscii(int c) inline boolean isAscii(int c) {
{
return ( isascii (c) == 0 ? false : true); return ( isascii (c) == 0 ? false : true);
} }
// Checks for a blank character, that is, a space or a tab. // Checks for a blank character, that is, a space or a tab.
inline boolean isWhitespace(int c) inline boolean isWhitespace(int c) {
{
return (isblank(c) == 0 ? false : true); return (isblank(c) == 0 ? false : true);
} }
// Checks for a control character. // Checks for a control character.
inline boolean isControl(int c) inline boolean isControl(int c) {
{
return (iscntrl(c) == 0 ? false : true); return (iscntrl(c) == 0 ? false : true);
} }
// Checks for a digit (0 through 9). // Checks for a digit (0 through 9).
inline boolean isDigit(int c) inline boolean isDigit(int c) {
{
return (isdigit(c) == 0 ? false : true); return (isdigit(c) == 0 ? false : true);
} }
// Checks for any printable character except space. // Checks for any printable character except space.
inline boolean isGraph(int c) inline boolean isGraph(int c) {
{
return (isgraph(c) == 0 ? false : true); return (isgraph(c) == 0 ? false : true);
} }
// Checks for a lower-case character. // Checks for a lower-case character.
inline boolean isLowerCase(int c) inline boolean isLowerCase(int c) {
{
return (islower(c) == 0 ? false : true); return (islower(c) == 0 ? false : true);
} }
// Checks for any printable character including space. // Checks for any printable character including space.
inline boolean isPrintable(int c) inline boolean isPrintable(int c) {
{
return (isprint(c) == 0 ? false : true); return (isprint(c) == 0 ? false : true);
} }
// Checks for any printable character which is not a space // Checks for any printable character which is not a space
// or an alphanumeric character. // or an alphanumeric character.
inline boolean isPunct(int c) inline boolean isPunct(int c) {
{
return (ispunct(c) == 0 ? false : true); return (ispunct(c) == 0 ? false : true);
} }
// Checks for white-space characters. For the avr-libc library, // Checks for white-space characters. For the avr-libc library,
// these are: space, formfeed ('\f'), newline ('\n'), carriage // these are: space, formfeed ('\f'), newline ('\n'), carriage
// return ('\r'), horizontal tab ('\t'), and vertical tab ('\v'). // return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').
inline boolean isSpace(int c) inline boolean isSpace(int c) {
{
return (isspace(c) == 0 ? false : true); return (isspace(c) == 0 ? false : true);
} }
// Checks for an uppercase letter. // Checks for an uppercase letter.
inline boolean isUpperCase(int c) inline boolean isUpperCase(int c) {
{
return (isupper(c) == 0 ? false : true); return (isupper(c) == 0 ? false : true);
} }
// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7 // Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7
// 8 9 a b c d e f A B C D E F. // 8 9 a b c d e f A B C D E F.
inline boolean isHexadecimalDigit(int c) inline boolean isHexadecimalDigit(int c) {
{
return (isxdigit(c) == 0 ? false : true); return (isxdigit(c) == 0 ? false : true);
} }
// Converts c to a 7-bit unsigned char value that fits into the // Converts c to a 7-bit unsigned char value that fits into the
// ASCII character set, by clearing the high-order bits. // ASCII character set, by clearing the high-order bits.
inline int toAscii(int c) inline int toAscii(int c) {
{
return toascii(c); return toascii(c);
} }
// Warning: // Warning:
// Many people will be unhappy if you use this function. // Many people will be unhappy if you use this function.
// This function will convert accented letters into random // This function will convert accented letters into random
// characters. // characters.
// Converts the letter c to lower case, if possible. // Converts the letter c to lower case, if possible.
inline int toLowerCase(int c) inline int toLowerCase(int c) {
{
return tolower(c); return tolower(c);
} }
// Converts the letter c to upper case, if possible. // Converts the letter c to upper case, if possible.
inline int toUpperCase(int c) inline int toUpperCase(int c) {
{
return toupper(c); return toupper(c);
} }

View File

@ -27,23 +27,20 @@ extern "C" {
#include <stdlib.h> #include <stdlib.h>
} }
void randomSeed(unsigned int seed) void randomSeed(unsigned int seed) {
{
if(seed != 0) { if(seed != 0) {
srand(seed); srand(seed);
} }
} }
long random(long howbig) long random(long howbig) {
{
if(howbig == 0) { if(howbig == 0) {
return 0; return 0;
} }
return rand() % howbig; return rand() % howbig;
} }
long random(long howsmall, long howbig) long random(long howsmall, long howbig) {
{
if(howsmall >= howbig) { if(howsmall >= howbig) {
return howsmall; return howsmall;
} }
@ -51,10 +48,14 @@ long random(long howsmall, long howbig)
return random(diff) + howsmall; return random(diff) + howsmall;
} }
long map(long x, long in_min, long in_max, long out_min, long out_max) long map(long x, long in_min, long in_max, long out_min, long out_max) {
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
} }
unsigned int makeWord(unsigned int w) { return w; } unsigned int makeWord(unsigned int w) {
unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; } return w;
}
unsigned int makeWord(unsigned char h, unsigned char l) {
return (h << 8) | l;
}

View File

@ -33,33 +33,30 @@ extern "C" {
/* Constructors */ /* Constructors */
/*********************************************/ /*********************************************/
ICACHE_FLASH_ATTR String::String(const char *cstr) ICACHE_FLASH_ATTR String::String(const char *cstr) {
{
init(); init();
if (cstr) copy(cstr, strlen(cstr)); if(cstr)
copy(cstr, strlen(cstr));
} }
ICACHE_FLASH_ATTR String::String(const String &value) ICACHE_FLASH_ATTR String::String(const String &value) {
{
init(); init();
*this = value; *this = value;
} }
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
ICACHE_FLASH_ATTR String::String(String &&rval) ICACHE_FLASH_ATTR String::String(String &&rval) {
{
init(); init();
move(rval); move(rval);
} }
ICACHE_FLASH_ATTR String::String(StringSumHelper &&rval)
{ ICACHE_FLASH_ATTR String::String(StringSumHelper &&rval) {
init(); init();
move(rval); move(rval);
} }
#endif #endif
ICACHE_FLASH_ATTR String::String(char c) ICACHE_FLASH_ATTR String::String(char c) {
{
init(); init();
char buf[2]; char buf[2];
buf[0] = c; buf[0] = c;
@ -67,62 +64,54 @@ ICACHE_FLASH_ATTR String::String(char c)
*this = buf; *this = buf;
} }
ICACHE_FLASH_ATTR String::String(unsigned char value, unsigned char base) ICACHE_FLASH_ATTR String::String(unsigned char value, unsigned char base) {
{
init(); init();
char buf[1 + 8 * sizeof(unsigned char)]; char buf[1 + 8 * sizeof(unsigned char)];
utoa(value, buf, base); utoa(value, buf, base);
*this = buf; *this = buf;
} }
ICACHE_FLASH_ATTR String::String(int value, unsigned char base) ICACHE_FLASH_ATTR String::String(int value, unsigned char base) {
{
init(); init();
char buf[2 + 8 * sizeof(int)]; char buf[2 + 8 * sizeof(int)];
itoa(value, buf, base); itoa(value, buf, base);
*this = buf; *this = buf;
} }
ICACHE_FLASH_ATTR String::String(unsigned int value, unsigned char base) ICACHE_FLASH_ATTR String::String(unsigned int value, unsigned char base) {
{
init(); init();
char buf[1 + 8 * sizeof(unsigned int)]; char buf[1 + 8 * sizeof(unsigned int)];
utoa(value, buf, base); utoa(value, buf, base);
*this = buf; *this = buf;
} }
ICACHE_FLASH_ATTR String::String(long value, unsigned char base) ICACHE_FLASH_ATTR String::String(long value, unsigned char base) {
{
init(); init();
char buf[2 + 8 * sizeof(long)]; char buf[2 + 8 * sizeof(long)];
ltoa(value, buf, base); ltoa(value, buf, base);
*this = buf; *this = buf;
} }
ICACHE_FLASH_ATTR String::String(unsigned long value, unsigned char base) ICACHE_FLASH_ATTR String::String(unsigned long value, unsigned char base) {
{
init(); init();
char buf[1 + 8 * sizeof(unsigned long)]; char buf[1 + 8 * sizeof(unsigned long)];
ultoa(value, buf, base); ultoa(value, buf, base);
*this = buf; *this = buf;
} }
ICACHE_FLASH_ATTR String::String(float value, unsigned char decimalPlaces) ICACHE_FLASH_ATTR String::String(float value, unsigned char decimalPlaces) {
{
init(); init();
char buf[33]; char buf[33];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
} }
ICACHE_FLASH_ATTR String::String(double value, unsigned char decimalPlaces) ICACHE_FLASH_ATTR String::String(double value, unsigned char decimalPlaces) {
{
init(); init();
char buf[33]; char buf[33];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
} }
ICACHE_FLASH_ATTR String::~String() ICACHE_FLASH_ATTR String::~String() {
{
os_free(buffer); os_free(buffer);
} }
@ -130,32 +119,31 @@ ICACHE_FLASH_ATTR String::~String()
// /* Memory Management */ // /* Memory Management */
// /*********************************************/ // /*********************************************/
inline void String::init(void) inline void String::init(void) {
{
buffer = NULL; buffer = NULL;
capacity = 0; capacity = 0;
len = 0; len = 0;
} }
void ICACHE_FLASH_ATTR String::invalidate(void) void ICACHE_FLASH_ATTR String::invalidate(void) {
{ if(buffer)
if (buffer) os_free(buffer); os_free(buffer);
buffer = NULL; buffer = NULL;
capacity = len = 0; capacity = len = 0;
} }
unsigned char ICACHE_FLASH_ATTR String::reserve(unsigned int size) unsigned char ICACHE_FLASH_ATTR String::reserve(unsigned int size) {
{ if(buffer && capacity >= size)
if (buffer && capacity >= size) return 1; return 1;
if(changeBuffer(size)) { if(changeBuffer(size)) {
if (len == 0) buffer[0] = 0; if(len == 0)
buffer[0] = 0;
return 1; return 1;
} }
return 0; return 0;
} }
unsigned char ICACHE_FLASH_ATTR String::changeBuffer(unsigned int maxStrLen) unsigned char ICACHE_FLASH_ATTR String::changeBuffer(unsigned int maxStrLen) {
{
char *newbuffer = (char *) os_realloc(buffer, maxStrLen + 1); char *newbuffer = (char *) os_realloc(buffer, maxStrLen + 1);
if(newbuffer) { if(newbuffer) {
buffer = newbuffer; buffer = newbuffer;
@ -169,8 +157,7 @@ unsigned char ICACHE_FLASH_ATTR String::changeBuffer(unsigned int maxStrLen)
// /* Copy and Move */ // /* Copy and Move */
// /*********************************************/ // /*********************************************/
String & ICACHE_FLASH_ATTR String::copy(const char *cstr, unsigned int length) String & ICACHE_FLASH_ATTR String::copy(const char *cstr, unsigned int length) {
{
if(!reserve(length)) { if(!reserve(length)) {
invalidate(); invalidate();
return *this; return *this;
@ -180,10 +167,8 @@ String & ICACHE_FLASH_ATTR String::copy(const char *cstr, unsigned int length)
return *this; return *this;
} }
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
void ICACHE_FLASH_ATTR String::move(String &rhs) void ICACHE_FLASH_ATTR String::move(String &rhs) {
{
if(buffer) { if(buffer) {
if(capacity >= rhs.len) { if(capacity >= rhs.len) {
strcpy(buffer, rhs.buffer); strcpy(buffer, rhs.buffer);
@ -203,117 +188,112 @@ void ICACHE_FLASH_ATTR String::move(String &rhs)
} }
#endif #endif
String & ICACHE_FLASH_ATTR String::operator = (const String &rhs) String & ICACHE_FLASH_ATTR String::operator =(const String &rhs) {
{ if(this == &rhs)
if (this == &rhs) return *this; return *this;
if (rhs.buffer) copy(rhs.buffer, rhs.len); if(rhs.buffer)
else invalidate(); copy(rhs.buffer, rhs.len);
else
invalidate();
return *this; return *this;
} }
#ifdef __GXX_EXPERIMENTAL_CXX0X__ #ifdef __GXX_EXPERIMENTAL_CXX0X__
String & ICACHE_FLASH_ATTR String::operator = (String &&rval) String & ICACHE_FLASH_ATTR String::operator =(String &&rval) {
{ if(this != &rval)
if (this != &rval) move(rval); move(rval);
return *this; return *this;
} }
String & ICACHE_FLASH_ATTR String::operator = (StringSumHelper &&rval) String & ICACHE_FLASH_ATTR String::operator =(StringSumHelper &&rval) {
{ if(this != &rval)
if (this != &rval) move(rval); move(rval);
return *this; return *this;
} }
#endif #endif
String & ICACHE_FLASH_ATTR String::operator = (const char *cstr) String & ICACHE_FLASH_ATTR String::operator =(const char *cstr) {
{ if(cstr)
if (cstr) copy(cstr, strlen(cstr)); copy(cstr, strlen(cstr));
else invalidate(); else
invalidate();
return *this; return *this;
} }
// /*********************************************/ // /*********************************************/
// /* concat */ // /* concat */
// /*********************************************/ // /*********************************************/
unsigned char ICACHE_FLASH_ATTR String::concat(const String &s) unsigned char ICACHE_FLASH_ATTR String::concat(const String &s) {
{
return concat(s.buffer, s.len); return concat(s.buffer, s.len);
} }
unsigned char ICACHE_FLASH_ATTR String::concat(const char *cstr, unsigned int length) unsigned char ICACHE_FLASH_ATTR String::concat(const char *cstr, unsigned int length) {
{
unsigned int newlen = len + length; unsigned int newlen = len + length;
if (!cstr) return 0; if(!cstr)
if (length == 0) return 1; return 0;
if (!reserve(newlen)) return 0; if(length == 0)
return 1;
if(!reserve(newlen))
return 0;
strcpy(buffer + len, cstr); strcpy(buffer + len, cstr);
len = newlen; len = newlen;
return 1; return 1;
} }
unsigned char ICACHE_FLASH_ATTR String::concat(const char *cstr) unsigned char ICACHE_FLASH_ATTR String::concat(const char *cstr) {
{ if(!cstr)
if (!cstr) return 0; return 0;
return concat(cstr, strlen(cstr)); return concat(cstr, strlen(cstr));
} }
unsigned char ICACHE_FLASH_ATTR String::concat(char c) unsigned char ICACHE_FLASH_ATTR String::concat(char c) {
{
char buf[2]; char buf[2];
buf[0] = c; buf[0] = c;
buf[1] = 0; buf[1] = 0;
return concat(buf, 1); return concat(buf, 1);
} }
unsigned char ICACHE_FLASH_ATTR String::concat(unsigned char num) unsigned char ICACHE_FLASH_ATTR String::concat(unsigned char num) {
{
char buf[1 + 3 * sizeof(unsigned char)]; char buf[1 + 3 * sizeof(unsigned char)];
itoa(num, buf, 10); itoa(num, buf, 10);
return concat(buf, strlen(buf)); return concat(buf, strlen(buf));
} }
unsigned char ICACHE_FLASH_ATTR String::concat(int num) unsigned char ICACHE_FLASH_ATTR String::concat(int num) {
{
char buf[2 + 3 * sizeof(int)]; char buf[2 + 3 * sizeof(int)];
itoa(num, buf, 10); itoa(num, buf, 10);
return concat(buf, strlen(buf)); return concat(buf, strlen(buf));
} }
unsigned char ICACHE_FLASH_ATTR String::concat(unsigned int num) unsigned char ICACHE_FLASH_ATTR String::concat(unsigned int num) {
{
char buf[1 + 3 * sizeof(unsigned int)]; char buf[1 + 3 * sizeof(unsigned int)];
utoa(num, buf, 10); utoa(num, buf, 10);
return concat(buf, strlen(buf)); return concat(buf, strlen(buf));
} }
unsigned char ICACHE_FLASH_ATTR String::concat(long num) unsigned char ICACHE_FLASH_ATTR String::concat(long num) {
{
char buf[2 + 3 * sizeof(long)]; char buf[2 + 3 * sizeof(long)];
ltoa(num, buf, 10); ltoa(num, buf, 10);
return concat(buf, strlen(buf)); return concat(buf, strlen(buf));
} }
unsigned char ICACHE_FLASH_ATTR String::concat(unsigned long num) unsigned char ICACHE_FLASH_ATTR String::concat(unsigned long num) {
{
char buf[1 + 3 * sizeof(unsigned long)]; char buf[1 + 3 * sizeof(unsigned long)];
ultoa(num, buf, 10); ultoa(num, buf, 10);
return concat(buf, strlen(buf)); return concat(buf, strlen(buf));
} }
unsigned char ICACHE_FLASH_ATTR String::concat(float num) unsigned char ICACHE_FLASH_ATTR String::concat(float num) {
{
char buf[20]; char buf[20];
char* string = dtostrf(num, 4, 2, buf); char* string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string)); return concat(string, strlen(string));
} }
unsigned char ICACHE_FLASH_ATTR String::concat(double num) unsigned char ICACHE_FLASH_ATTR String::concat(double num) {
{
char buf[20]; char buf[20];
char* string = dtostrf(num, 4, 2, buf); char* string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string)); return concat(string, strlen(string));
@ -323,151 +303,150 @@ unsigned char ICACHE_FLASH_ATTR String::concat(double num)
/* Concatenate */ /* Concatenate */
/*********************************************/ /*********************************************/
StringSumHelper & ICACHE_FLASH_ATTR operator + (const StringSumHelper &lhs, const String &rhs) StringSumHelper & ICACHE_FLASH_ATTR operator +(const StringSumHelper &lhs, const String &rhs) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(rhs.buffer, rhs.len)) a.invalidate(); if(!a.concat(rhs.buffer, rhs.len))
a.invalidate();
return a; return a;
} }
StringSumHelper & ICACHE_FLASH_ATTR operator + (const StringSumHelper &lhs, const char *cstr) StringSumHelper & ICACHE_FLASH_ATTR operator +(const StringSumHelper &lhs, const char *cstr) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate(); if(!cstr || !a.concat(cstr, strlen(cstr)))
a.invalidate();
return a; return a;
} }
StringSumHelper & ICACHE_FLASH_ATTR operator + (const StringSumHelper &lhs, char c) StringSumHelper & ICACHE_FLASH_ATTR operator +(const StringSumHelper &lhs, char c) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(c)) a.invalidate(); if(!a.concat(c))
a.invalidate();
return a; return a;
} }
StringSumHelper & ICACHE_FLASH_ATTR operator + (const StringSumHelper &lhs, unsigned char num) StringSumHelper & ICACHE_FLASH_ATTR operator +(const StringSumHelper &lhs, unsigned char num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate(); if(!a.concat(num))
a.invalidate();
return a; return a;
} }
StringSumHelper & ICACHE_FLASH_ATTR operator + (const StringSumHelper &lhs, int num) StringSumHelper & ICACHE_FLASH_ATTR operator +(const StringSumHelper &lhs, int num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate(); if(!a.concat(num))
a.invalidate();
return a; return a;
} }
StringSumHelper & ICACHE_FLASH_ATTR operator + (const StringSumHelper &lhs, unsigned int num) StringSumHelper & ICACHE_FLASH_ATTR operator +(const StringSumHelper &lhs, unsigned int num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate(); if(!a.concat(num))
a.invalidate();
return a; return a;
} }
StringSumHelper & ICACHE_FLASH_ATTR operator + (const StringSumHelper &lhs, long num) StringSumHelper & ICACHE_FLASH_ATTR operator +(const StringSumHelper &lhs, long num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate(); if(!a.concat(num))
a.invalidate();
return a; return a;
} }
StringSumHelper & ICACHE_FLASH_ATTR operator + (const StringSumHelper &lhs, unsigned long num) StringSumHelper & ICACHE_FLASH_ATTR operator +(const StringSumHelper &lhs, unsigned long num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate(); if(!a.concat(num))
a.invalidate();
return a; return a;
} }
StringSumHelper & ICACHE_FLASH_ATTR operator + (const StringSumHelper &lhs, float num) StringSumHelper & ICACHE_FLASH_ATTR operator +(const StringSumHelper &lhs, float num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate(); if(!a.concat(num))
a.invalidate();
return a; return a;
} }
StringSumHelper & ICACHE_FLASH_ATTR operator + (const StringSumHelper &lhs, double num) StringSumHelper & ICACHE_FLASH_ATTR operator +(const StringSumHelper &lhs, double num) {
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs); StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!a.concat(num)) a.invalidate(); if(!a.concat(num))
a.invalidate();
return a; return a;
} }
// /*********************************************/ // /*********************************************/
// /* Comparison */ // /* Comparison */
// /*********************************************/ // /*********************************************/
int ICACHE_FLASH_ATTR String::compareTo(const String &s) const int ICACHE_FLASH_ATTR String::compareTo(const String &s) const {
{
if(!buffer || !s.buffer) { if(!buffer || !s.buffer) {
if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer; if(s.buffer && s.len > 0)
if (buffer && len > 0) return *(unsigned char *)buffer; return 0 - *(unsigned char *) s.buffer;
if(buffer && len > 0)
return *(unsigned char *) buffer;
return 0; return 0;
} }
return strcmp(buffer, s.buffer); return strcmp(buffer, s.buffer);
} }
unsigned char ICACHE_FLASH_ATTR String::equals(const String &s2) const unsigned char ICACHE_FLASH_ATTR String::equals(const String &s2) const {
{
return (len == s2.len && compareTo(s2) == 0); return (len == s2.len && compareTo(s2) == 0);
} }
unsigned char ICACHE_FLASH_ATTR String::equals(const char *cstr) const unsigned char ICACHE_FLASH_ATTR String::equals(const char *cstr) const {
{ if(len == 0)
if (len == 0) return (cstr == NULL || *cstr == 0); return (cstr == NULL || *cstr == 0);
if (cstr == NULL) return buffer[0] == 0; if(cstr == NULL)
return buffer[0] == 0;
return strcmp(buffer, cstr) == 0; return strcmp(buffer, cstr) == 0;
} }
unsigned char ICACHE_FLASH_ATTR String::operator<(const String &rhs) const unsigned char ICACHE_FLASH_ATTR String::operator<(const String &rhs) const {
{
return compareTo(rhs) < 0; return compareTo(rhs) < 0;
} }
unsigned char ICACHE_FLASH_ATTR String::operator>(const String &rhs) const unsigned char ICACHE_FLASH_ATTR String::operator>(const String &rhs) const {
{
return compareTo(rhs) > 0; return compareTo(rhs) > 0;
} }
unsigned char ICACHE_FLASH_ATTR String::operator<=(const String &rhs) const unsigned char ICACHE_FLASH_ATTR String::operator<=(const String &rhs) const {
{
return compareTo(rhs) <= 0; return compareTo(rhs) <= 0;
} }
unsigned char ICACHE_FLASH_ATTR String::operator>=(const String &rhs) const unsigned char ICACHE_FLASH_ATTR String::operator>=(const String &rhs) const {
{
return compareTo(rhs) >= 0; return compareTo(rhs) >= 0;
} }
unsigned char ICACHE_FLASH_ATTR String::equalsIgnoreCase( const String &s2 ) const unsigned char ICACHE_FLASH_ATTR String::equalsIgnoreCase(const String &s2) const {
{ if(this == &s2)
if (this == &s2) return 1; return 1;
if (len != s2.len) return 0; if(len != s2.len)
if (len == 0) return 1; return 0;
if(len == 0)
return 1;
const char *p1 = buffer; const char *p1 = buffer;
const char *p2 = s2.buffer; const char *p2 = s2.buffer;
while(*p1) { while(*p1) {
if (tolower(*p1++) != tolower(*p2++)) return 0; if(tolower(*p1++) != tolower(*p2++))
return 0;
} }
return 1; return 1;
} }
unsigned char ICACHE_FLASH_ATTR String::startsWith( const String &s2 ) const unsigned char ICACHE_FLASH_ATTR String::startsWith(const String &s2) const {
{ if(len < s2.len)
if (len < s2.len) return 0; return 0;
return startsWith(s2, 0); return startsWith(s2, 0);
} }
unsigned char ICACHE_FLASH_ATTR String::startsWith( const String &s2, unsigned int offset ) const unsigned char ICACHE_FLASH_ATTR String::startsWith(const String &s2, unsigned int offset) const {
{ if(offset > len - s2.len || !buffer || !s2.buffer)
if (offset > len - s2.len || !buffer || !s2.buffer) return 0; return 0;
return strncmp(&buffer[offset], s2.buffer, s2.len) == 0; return strncmp(&buffer[offset], s2.buffer, s2.len) == 0;
} }
unsigned char ICACHE_FLASH_ATTR String::endsWith( const String &s2 ) const unsigned char ICACHE_FLASH_ATTR String::endsWith(const String &s2) const {
{ if(len < s2.len || !buffer || !s2.buffer)
if ( len < s2.len || !buffer || !s2.buffer) return 0; return 0;
return strcmp(&buffer[len - s2.len], s2.buffer) == 0; return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
} }
@ -475,18 +454,16 @@ unsigned char ICACHE_FLASH_ATTR String::endsWith( const String &s2 ) const
// /* Character Access */ // /* Character Access */
// /*********************************************/ // /*********************************************/
char ICACHE_FLASH_ATTR String::charAt(unsigned int loc) const char ICACHE_FLASH_ATTR String::charAt(unsigned int loc) const {
{
return operator[](loc); return operator[](loc);
} }
void ICACHE_FLASH_ATTR String::setCharAt(unsigned int loc, char c) void ICACHE_FLASH_ATTR String::setCharAt(unsigned int loc, char c) {
{ if(loc < len)
if (loc < len) buffer[loc] = c; buffer[loc] = c;
} }
char & ICACHE_FLASH_ATTR String::operator[](unsigned int index) char & ICACHE_FLASH_ATTR String::operator[](unsigned int index) {
{
static char dummy_writable_char; static char dummy_writable_char;
if(index >= len || !buffer) { if(index >= len || !buffer) {
dummy_writable_char = 0; dummy_writable_char = 0;
@ -495,21 +472,22 @@ char & ICACHE_FLASH_ATTR String::operator[](unsigned int index)
return buffer[index]; return buffer[index];
} }
char ICACHE_FLASH_ATTR String::operator[]( unsigned int index ) const char ICACHE_FLASH_ATTR String::operator[](unsigned int index) const {
{ if(index >= len || !buffer)
if (index >= len || !buffer) return 0; return 0;
return buffer[index]; return buffer[index];
} }
void ICACHE_FLASH_ATTR String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const void ICACHE_FLASH_ATTR String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const {
{ if(!bufsize || !buf)
if (!bufsize || !buf) return; return;
if(index >= len) { if(index >= len) {
buf[0] = 0; buf[0] = 0;
return; return;
} }
unsigned int n = bufsize - 1; unsigned int n = bufsize - 1;
if (n > len - index) n = len - index; if(n > len - index)
n = len - index;
strncpy((char *) buf, buffer + index, n); strncpy((char *) buf, buffer + index, n);
buf[n] = 0; buf[n] = 0;
} }
@ -519,76 +497,79 @@ void ICACHE_FLASH_ATTR String::getBytes(unsigned char *buf, unsigned int bufsize
// /*********************************************/ // /*********************************************/
ICACHE_FLASH_ATTR ICACHE_FLASH_ATTR ICACHE_FLASH_ATTR ICACHE_FLASH_ATTR
int ICACHE_FLASH_ATTR String::indexOf(char c) const int ICACHE_FLASH_ATTR String::indexOf(char c) const {
{
return indexOf(c, 0); return indexOf(c, 0);
} }
int ICACHE_FLASH_ATTR String::indexOf( char ch, unsigned int fromIndex ) const int ICACHE_FLASH_ATTR String::indexOf(char ch, unsigned int fromIndex) const {
{ if(fromIndex >= len)
if (fromIndex >= len) return -1; return -1;
const char* temp = strchr(buffer + fromIndex, ch); const char* temp = strchr(buffer + fromIndex, ch);
if (temp == NULL) return -1; if(temp == NULL)
return -1;
return temp - buffer; return temp - buffer;
} }
int ICACHE_FLASH_ATTR String::indexOf(const String &s2) const int ICACHE_FLASH_ATTR String::indexOf(const String &s2) const {
{
return indexOf(s2, 0); return indexOf(s2, 0);
} }
int ICACHE_FLASH_ATTR String::indexOf(const String &s2, unsigned int fromIndex) const int ICACHE_FLASH_ATTR String::indexOf(const String &s2, unsigned int fromIndex) const {
{ if(fromIndex >= len)
if (fromIndex >= len) return -1; return -1;
const char *found = strstr(buffer + fromIndex, s2.buffer); const char *found = strstr(buffer + fromIndex, s2.buffer);
if (found == NULL) return -1; if(found == NULL)
return -1;
return found - buffer; return found - buffer;
} }
int ICACHE_FLASH_ATTR String::lastIndexOf( char theChar ) const int ICACHE_FLASH_ATTR String::lastIndexOf(char theChar) const {
{
return lastIndexOf(theChar, len - 1); return lastIndexOf(theChar, len - 1);
} }
int ICACHE_FLASH_ATTR String::lastIndexOf(char ch, unsigned int fromIndex) const int ICACHE_FLASH_ATTR String::lastIndexOf(char ch, unsigned int fromIndex) const {
{ if(fromIndex >= len)
if (fromIndex >= len) return -1; return -1;
char tempchar = buffer[fromIndex + 1]; char tempchar = buffer[fromIndex + 1];
buffer[fromIndex + 1] = '\0'; buffer[fromIndex + 1] = '\0';
char* temp = strrchr(buffer, ch); char* temp = strrchr(buffer, ch);
buffer[fromIndex + 1] = tempchar; buffer[fromIndex + 1] = tempchar;
if (temp == NULL) return -1; if(temp == NULL)
return -1;
return temp - buffer; return temp - buffer;
} }
int ICACHE_FLASH_ATTR String::lastIndexOf(const String &s2) const int ICACHE_FLASH_ATTR String::lastIndexOf(const String &s2) const {
{
return lastIndexOf(s2, len - s2.len); return lastIndexOf(s2, len - s2.len);
} }
int ICACHE_FLASH_ATTR String::lastIndexOf(const String &s2, unsigned int fromIndex) const int ICACHE_FLASH_ATTR String::lastIndexOf(const String &s2, unsigned int fromIndex) const {
{ if(s2.len == 0 || len == 0 || s2.len > len)
if (s2.len == 0 || len == 0 || s2.len > len) return -1; return -1;
if (fromIndex >= len) fromIndex = len - 1; if(fromIndex >= len)
fromIndex = len - 1;
int found = -1; int found = -1;
for(char *p = buffer; p <= buffer + fromIndex; p++) { for(char *p = buffer; p <= buffer + fromIndex; p++) {
p = strstr(p, s2.buffer); p = strstr(p, s2.buffer);
if (!p) break; if(!p)
if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer; break;
if((unsigned int) (p - buffer) <= fromIndex)
found = p - buffer;
} }
return found; return found;
} }
String ICACHE_FLASH_ATTR String::substring(unsigned int left, unsigned int right) const String ICACHE_FLASH_ATTR String::substring(unsigned int left, unsigned int right) const {
{
if(left > right) { if(left > right) {
unsigned int temp = right; unsigned int temp = right;
right = left; right = left;
left = temp; left = temp;
} }
String out; String out;
if (left >= len) return out; if(left >= len)
if (right > len) right = len; return out;
if(right > len)
right = len;
char temp = buffer[right]; // save the replaced character char temp = buffer[right]; // save the replaced character
buffer[right] = '\0'; buffer[right] = '\0';
out = buffer + left; // pointer arithmetic out = buffer + left; // pointer arithmetic
@ -600,17 +581,18 @@ String ICACHE_FLASH_ATTR String::substring(unsigned int left, unsigned int right
// /* Modification */ // /* Modification */
// /*********************************************/ // /*********************************************/
void ICACHE_FLASH_ATTR String::replace(char find, char replace) void ICACHE_FLASH_ATTR String::replace(char find, char replace) {
{ if(!buffer)
if (!buffer) return; return;
for(char *p = buffer; *p; p++) { for(char *p = buffer; *p; p++) {
if (*p == find) *p = replace; if(*p == find)
*p = replace;
} }
} }
void ICACHE_FLASH_ATTR String::replace(const String& find, const String& replace) void ICACHE_FLASH_ATTR String::replace(const String& find, const String& replace) {
{ if(len == 0 || find.len == 0)
if (len == 0 || find.len == 0) return; return;
int diff = replace.len - find.len; int diff = replace.len - find.len;
char *readFrom = buffer; char *readFrom = buffer;
char *foundAt; char *foundAt;
@ -637,8 +619,10 @@ void ICACHE_FLASH_ATTR String::replace(const String& find, const String& replace
readFrom = foundAt + find.len; readFrom = foundAt + find.len;
size += diff; size += diff;
} }
if (size == len) return; if(size == len)
if (size > capacity && !changeBuffer(size)) return; // XXX: tell user! return;
if(size > capacity && !changeBuffer(size))
return; // XXX: tell user!
int index = len - 1; int index = len - 1;
while(index >= 0 && (index = lastIndexOf(find, index)) >= 0) { while(index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
readFrom = buffer + index + find.len; readFrom = buffer + index + find.len;
@ -659,40 +643,49 @@ void ICACHE_FLASH_ATTR String::remove(unsigned int index){
} }
void ICACHE_FLASH_ATTR String::remove(unsigned int index, unsigned int count) { void ICACHE_FLASH_ATTR String::remove(unsigned int index, unsigned int count) {
if (index >= len) { return; } if(index >= len) {
if (count <= 0) { return; } return;
if (count > len - index) { count = len - index; } }
if(count <= 0) {
return;
}
if(count > len - index) {
count = len - index;
}
char *writeTo = buffer + index; char *writeTo = buffer + index;
len = len - count; len = len - count;
strncpy(writeTo, buffer + index + count, len - index); strncpy(writeTo, buffer + index + count, len - index);
buffer[len] = 0; buffer[len] = 0;
} }
void ICACHE_FLASH_ATTR String::toLowerCase(void) void ICACHE_FLASH_ATTR String::toLowerCase(void) {
{ if(!buffer)
if (!buffer) return; return;
for(char *p = buffer; *p; p++) { for(char *p = buffer; *p; p++) {
*p = tolower(*p); *p = tolower(*p);
} }
} }
void ICACHE_FLASH_ATTR String::toUpperCase(void) void ICACHE_FLASH_ATTR String::toUpperCase(void) {
{ if(!buffer)
if (!buffer) return; return;
for(char *p = buffer; *p; p++) { for(char *p = buffer; *p; p++) {
*p = toupper(*p); *p = toupper(*p);
} }
} }
void ICACHE_FLASH_ATTR String::trim(void) void ICACHE_FLASH_ATTR String::trim(void) {
{ if(!buffer || len == 0)
if (!buffer || len == 0) return; return;
char *begin = buffer; char *begin = buffer;
while (isspace(*begin)) begin++; while(isspace(*begin))
begin++;
char *end = buffer + len - 1; char *end = buffer + len - 1;
while (isspace(*end) && end >= begin) end--; while(isspace(*end) && end >= begin)
end--;
len = end + 1 - begin; len = end + 1 - begin;
if (begin > buffer) memcpy(buffer, begin, len); if(begin > buffer)
memcpy(buffer, begin, len);
buffer[len] = 0; buffer[len] = 0;
} }
@ -700,14 +693,14 @@ void ICACHE_FLASH_ATTR String::trim(void)
// /* Parsing / Conversion */ // /* Parsing / Conversion */
// /*********************************************/ // /*********************************************/
long ICACHE_FLASH_ATTR String::toInt(void) const long ICACHE_FLASH_ATTR String::toInt(void) const {
{ if(buffer)
if (buffer) return atol(buffer); return atol(buffer);
return 0; return 0;
} }
float ICACHE_FLASH_ATTR String::toFloat(void) const float ICACHE_FLASH_ATTR String::toFloat(void) const {
{ if(buffer)
if (buffer) return atof(buffer); return atof(buffer);
return 0; return 0;
} }

View File

@ -37,13 +37,13 @@ typedef char* __FlashStringHelper;
#define F(str) str #define F(str) str
// The string class // The string class
class String class String {
{
// use a function pointer to allow for "if (s)" without the // use a function pointer to allow for "if (s)" without the
// complications of an operator bool(). for more information, see: // complications of an operator bool(). for more information, see:
// http://www.artima.com/cppsource/safebool.html // http://www.artima.com/cppsource/safebool.html
typedef void (String::*StringIfHelperType)() const; typedef void (String::*StringIfHelperType)() const;
void StringIfHelper() const {} void StringIfHelper() const {
}
public: public:
// constructors // constructors
@ -72,7 +72,9 @@ public:
// is left unchanged). reserve(0), if successful, will validate an // is left unchanged). reserve(0), if successful, will validate an
// invalid string (i.e., "if (s)" will be true afterwards) // invalid string (i.e., "if (s)" will be true afterwards)
unsigned char reserve(unsigned int size); unsigned char reserve(unsigned int size);
inline unsigned int length(void) const {return len;} inline unsigned int length(void) const {
return len;
}
// creates a copy of the assigned value. if the value is null or // creates a copy of the assigned value. if the value is null or
// invalid, or if the memory allocation fails, the string will be // invalid, or if the memory allocation fails, the string will be
@ -102,16 +104,46 @@ public:
// if there's not enough memory for the concatenated value, the string // if there's not enough memory for the concatenated value, the string
// will be left unchanged (but this isn't signalled in any way) // will be left unchanged (but this isn't signalled in any way)
String & operator += (const String &rhs) {concat(rhs); return (*this);} String & operator +=(const String &rhs) {
String & operator += (const char *cstr) {concat(cstr); return (*this);} concat(rhs);
String & operator += (char c) {concat(c); return (*this);} return (*this);
String & operator += (unsigned char num) {concat(num); return (*this);} }
String & operator += (int num) {concat(num); return (*this);} String & operator +=(const char *cstr) {
String & operator += (unsigned int num) {concat(num); return (*this);} concat(cstr);
String & operator += (long num) {concat(num); return (*this);} return (*this);
String & operator += (unsigned long num) {concat(num); return (*this);} }
String & operator += (float num) {concat(num); return (*this);} String & operator +=(char c) {
String & operator += (double num) {concat(num); return (*this);} concat(c);
return (*this);
}
String & operator +=(unsigned char num) {
concat(num);
return (*this);
}
String & operator +=(int num) {
concat(num);
return (*this);
}
String & operator +=(unsigned int num) {
concat(num);
return (*this);
}
String & operator +=(long num) {
concat(num);
return (*this);
}
String & operator +=(unsigned long num) {
concat(num);
return (*this);
}
String & operator +=(float num) {
concat(num);
return (*this);
}
String & operator +=(double num) {
concat(num);
return (*this);
}
friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs); friend StringSumHelper & operator +(const StringSumHelper &lhs, const String &rhs);
friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr); friend StringSumHelper & operator +(const StringSumHelper &lhs, const char *cstr);
@ -125,14 +157,24 @@ public:
friend StringSumHelper & operator +(const StringSumHelper &lhs, double num); friend StringSumHelper & operator +(const StringSumHelper &lhs, double num);
// comparison (only works w/ Strings and "strings") // comparison (only works w/ Strings and "strings")
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } operator StringIfHelperType() const {
return buffer ? &String::StringIfHelper : 0;
}
int compareTo(const String &s) const; int compareTo(const String &s) const;
unsigned char equals(const String &s) const; unsigned char equals(const String &s) const;
unsigned char equals(const char *cstr) const; unsigned char equals(const char *cstr) const;
unsigned char operator == (const String &rhs) const {return equals(rhs);} unsigned char operator ==(const String &rhs) const {
unsigned char operator == (const char *cstr) const {return equals(cstr);} return equals(rhs);
unsigned char operator != (const String &rhs) const {return !equals(rhs);} }
unsigned char operator != (const char *cstr) const {return !equals(cstr);} unsigned char operator ==(const char *cstr) const {
return equals(cstr);
}
unsigned char operator !=(const String &rhs) const {
return !equals(rhs);
}
unsigned char operator !=(const char *cstr) const {
return !equals(cstr);
}
unsigned char operator <(const String &rhs) const; unsigned char operator <(const String &rhs) const;
unsigned char operator >(const String &rhs) const; unsigned char operator >(const String &rhs) const;
unsigned char operator <=(const String &rhs) const; unsigned char operator <=(const String &rhs) const;
@ -148,9 +190,12 @@ public:
char operator [](unsigned int index) const; char operator [](unsigned int index) const;
char& operator [](unsigned int index); char& operator [](unsigned int index);
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const; void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index = 0) const;
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const {
{getBytes((unsigned char *)buf, bufsize, index);} getBytes((unsigned char *) buf, bufsize, index);
const char * c_str() const { return buffer; } }
const char * c_str() const {
return buffer;
}
// search // search
int indexOf(char ch) const; int indexOf(char ch) const;
@ -161,7 +206,10 @@ public:
int lastIndexOf(char ch, unsigned int fromIndex) const; int lastIndexOf(char ch, unsigned int fromIndex) const;
int lastIndexOf(const String &str) const; int lastIndexOf(const String &str) const;
int lastIndexOf(const String &str, unsigned int fromIndex) const; int lastIndexOf(const String &str, unsigned int fromIndex) const;
String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); }; String substring(unsigned int beginIndex) const {
return substring(beginIndex, len);
}
;
String substring(unsigned int beginIndex, unsigned int endIndex) const; String substring(unsigned int beginIndex, unsigned int endIndex) const;
// modification // modification
@ -194,19 +242,38 @@ protected:
#endif #endif
}; };
class StringSumHelper : public String class StringSumHelper: public String {
{
public: public:
StringSumHelper(const String &s) : String(s) {} StringSumHelper(const String &s) :
StringSumHelper(const char *p) : String(p) {} String(s) {
StringSumHelper(char c) : String(c) {} }
StringSumHelper(unsigned char num) : String(num) {} StringSumHelper(const char *p) :
StringSumHelper(int num) : String(num) {} String(p) {
StringSumHelper(unsigned int num) : String(num) {} }
StringSumHelper(long num) : String(num) {} StringSumHelper(char c) :
StringSumHelper(unsigned long num) : String(num) {} String(c) {
StringSumHelper(float num) : String(num) {} }
StringSumHelper(double num) : String(num) {} StringSumHelper(unsigned char num) :
String(num) {
}
StringSumHelper(int num) :
String(num) {
}
StringSumHelper(unsigned int num) :
String(num) {
}
StringSumHelper(long num) :
String(num) {
}
StringSumHelper(unsigned long num) :
String(num) {
}
StringSumHelper(float num) :
String(num) {
}
StringSumHelper(double num) :
String(num) {
}
}; };
#endif // __cplusplus #endif // __cplusplus

View File

@ -22,70 +22,51 @@
#define __cbuf_h #define __cbuf_h
#include <stdint.h> #include <stdint.h>
class cbuf class cbuf {
{
public: public:
cbuf(size_t size) cbuf(size_t size) :
: _size(size) _size(size), _buf(new char[size]), _bufend(_buf + size), _begin(_buf), _end(_begin) {
, _buf(new char[size])
, _bufend(_buf + size)
, _begin(_buf)
, _end(_begin)
{
} }
~cbuf() ~cbuf() {
{
delete[] _buf; delete[] _buf;
} }
size_t getSize() const size_t getSize() const {
{ if(_end >= _begin) return _end - _begin;
if (_end >= _begin)
return _end - _begin;
return _size - (_begin - _end); return _size - (_begin - _end);
} }
size_t room() const size_t room() const {
{ if(_end >= _begin) return _size - (_end - _begin) - 1;
if (_end >= _begin)
return _size - (_end - _begin) - 1;
return _begin - _end - 1; return _begin - _end - 1;
} }
bool empty() const bool empty() const {
{
return _begin == _end; return _begin == _end;
} }
int peek() int peek() {
{ if(_end == _begin) return -1;
if (_end == _begin)
return -1;
return static_cast<int>(*_begin); return static_cast<int>(*_begin);
} }
int read() int read() {
{ if(getSize() == 0) return -1;
if (getSize() == 0)
return -1;
char result = *_begin; char result = *_begin;
if (++_begin == _bufend) if(++_begin == _bufend) _begin = _buf;
_begin = _buf;
return static_cast<int>(result); return static_cast<int>(result);
} }
size_t read(char* dst, size_t size) size_t read(char* dst, size_t size) {
{
size_t bytes_available = getSize(); size_t bytes_available = getSize();
size_t size_to_read = (size < bytes_available) ? size : bytes_available; size_t size_to_read = (size < bytes_available) ? size : bytes_available;
size_t size_read = size_to_read; size_t size_read = size_to_read;
if (_end < _begin && size_to_read > _bufend - _begin) if(_end < _begin && size_to_read > _bufend - _begin) {
{
size_t top_size = _bufend - _begin; size_t top_size = _bufend - _begin;
memcpy(dst, _begin, top_size); memcpy(dst, _begin, top_size);
_begin = _buf; _begin = _buf;
@ -94,29 +75,23 @@ public:
} }
memcpy(dst, _begin, size_to_read); memcpy(dst, _begin, size_to_read);
_begin += size_to_read; _begin += size_to_read;
if (_begin == _bufend) if(_begin == _bufend) _begin = _buf;
_begin = _buf;
return size_read; return size_read;
} }
size_t write(char c) size_t write(char c) {
{ if(room() == 0) return 0;
if (room() == 0)
return 0;
*_end = c; *_end = c;
if (++_end == _bufend) if(++_end == _bufend) _end = _buf;
_end = _buf;
return 1; return 1;
} }
size_t write(const char* src, size_t size) size_t write(const char* src, size_t size) {
{
size_t bytes_available = room(); size_t bytes_available = room();
size_t size_to_write = (size < bytes_available) ? size : bytes_available; size_t size_to_write = (size < bytes_available) ? size : bytes_available;
size_t size_written = size_to_write; size_t size_written = size_to_write;
if (_end > _begin && size_to_write > _bufend - _end) if(_end > _begin && size_to_write > _bufend - _end) {
{
size_t top_size = _bufend - _end; size_t top_size = _bufend - _end;
memcpy(_end, src, top_size); memcpy(_end, src, top_size);
_end = _buf; _end = _buf;
@ -125,13 +100,11 @@ public:
} }
memcpy(_end, src, size_to_write); memcpy(_end, src, size_to_write);
_end += size_to_write; _end += size_to_write;
if (_end == _bufend) if(_end == _bufend) _end = _buf;
_end = _buf;
return size_written; return size_written;
} }
void flush() void flush() {
{
_begin = _buf; _begin = _buf;
_end = _buf; _end = _buf;
} }
@ -144,5 +117,4 @@ private:
char* _end; char* _end;
}; };
#endif//__cbuf_h #endif//__cbuf_h

View File

@ -18,7 +18,6 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#ifndef CONT_H_ #ifndef CONT_H_
#define CONT_H_ #define CONT_H_
@ -26,8 +25,7 @@
#define CONT_STACKSIZE 4096 #define CONT_STACKSIZE 4096
#endif #endif
typedef struct cont_ typedef struct cont_ {
{
void (*pc_ret)(void); void (*pc_ret)(void);
unsigned* sp_ret; unsigned* sp_ret;

View File

@ -22,19 +22,15 @@
#define CONT_STACKGUARD 0xfeefeffe #define CONT_STACKGUARD 0xfeefeffe
void cont_init(cont_t* cont) void cont_init(cont_t* cont) {
{
cont->stack_guard1 = CONT_STACKGUARD; cont->stack_guard1 = CONT_STACKGUARD;
cont->stack_guard2 = CONT_STACKGUARD; cont->stack_guard2 = CONT_STACKGUARD;
cont->stack_end = cont->stack + (sizeof(cont->stack) / 4 - 1); cont->stack_end = cont->stack + (sizeof(cont->stack) / 4 - 1);
cont->struct_start = (unsigned*) cont; cont->struct_start = (unsigned*) cont;
} }
int cont_check(cont_t* cont) int cont_check(cont_t* cont) {
{ if(cont->stack_guard1 != CONT_STACKGUARD || cont->stack_guard2 != CONT_STACKGUARD) return 1;
if (cont->stack_guard1 != CONT_STACKGUARD ||
cont->stack_guard2 != CONT_STACKGUARD )
return 1;
return 0; return 0;
} }

View File

@ -22,7 +22,6 @@
//This may be used to change user task stack size: //This may be used to change user task stack size:
//#define CONT_STACKSIZE 4096 //#define CONT_STACKSIZE 4096
#include <Arduino.h> #include <Arduino.h>
extern "C" { extern "C" {
#include "ets_sys.h" #include "ets_sys.h"
@ -35,26 +34,26 @@ extern "C" {
#define LOOP_TASK_PRIORITY 0 #define LOOP_TASK_PRIORITY 0
#define LOOP_QUEUE_SIZE 1 #define LOOP_QUEUE_SIZE 1
int atexit(void (*func)()) { return 0; } int atexit(void (*func)()) {
return 0;
}
extern "C" void ets_update_cpu_frequency(int freqmhz); extern "C" void ets_update_cpu_frequency(int freqmhz);
void initVariant() __attribute__((weak)); void initVariant() __attribute__((weak));
void initVariant() { } void initVariant() {
}
extern void loop(); extern void loop();
extern void setup(); extern void setup();
void preloop_update_frequency() __attribute__((weak)); void preloop_update_frequency() __attribute__((weak));
void preloop_update_frequency() void preloop_update_frequency() {
{
#if defined(F_CPU) && (F_CPU == 16000000L) #if defined(F_CPU) && (F_CPU == 16000000L)
REG_SET_BIT(0x3ff00014, BIT(0)); REG_SET_BIT(0x3ff00014, BIT(0));
ets_update_cpu_frequency(160); ets_update_cpu_frequency(160);
#endif #endif
} }
extern void (*__init_array_start)(void); extern void (*__init_array_start)(void);
extern void (*__init_array_end)(void); extern void (*__init_array_end)(void);
@ -63,39 +62,32 @@ static os_event_t g_loop_queue[LOOP_QUEUE_SIZE];
static uint32_t g_micros_at_task_start; static uint32_t g_micros_at_task_start;
extern "C" uint32_t esp_micros_at_task_start() extern "C" uint32_t esp_micros_at_task_start() {
{
return g_micros_at_task_start; return g_micros_at_task_start;
} }
extern "C" void abort() extern "C" void abort() {
{ while(1) {
while(1){} }
} }
extern "C" void esp_yield() extern "C" void esp_yield() {
{
cont_yield(&g_cont); cont_yield(&g_cont);
} }
extern "C" void esp_schedule() extern "C" void esp_schedule() {
{
system_os_post(LOOP_TASK_PRIORITY, 0, 0); system_os_post(LOOP_TASK_PRIORITY, 0, 0);
} }
extern "C" void __yield() extern "C" void __yield() {
{
esp_schedule(); esp_schedule();
esp_yield(); esp_yield();
} }
extern "C" void yield(void) __attribute__ ((weak, alias("__yield"))); extern "C" void yield(void) __attribute__ ((weak, alias("__yield")));
static void loop_wrapper() {
static void loop_wrapper()
{
static bool setup_done = false; static bool setup_done = false;
if (!setup_done) if(!setup_done) {
{
setup(); setup();
setup_done = true; setup_done = true;
} }
@ -104,33 +96,28 @@ static void loop_wrapper()
esp_schedule(); esp_schedule();
} }
static void loop_task(os_event_t *events) static void loop_task(os_event_t *events) {
{
g_micros_at_task_start = system_get_time(); g_micros_at_task_start = system_get_time();
cont_run(&g_cont, &loop_wrapper); cont_run(&g_cont, &loop_wrapper);
if (cont_check(&g_cont) != 0) if(cont_check(&g_cont) != 0) {
{
ets_printf("\r\nheap collided with sketch stack\r\n"); ets_printf("\r\nheap collided with sketch stack\r\n");
abort(); abort();
} }
} }
static void do_global_ctors(void) static void do_global_ctors(void) {
{
void (**p)(void); void (**p)(void);
for(p = &__init_array_start; p != &__init_array_end; ++p) for(p = &__init_array_start; p != &__init_array_end; ++p)
(*p)(); (*p)();
} }
void init_done() void init_done() {
{
do_global_ctors(); do_global_ctors();
esp_schedule(); esp_schedule();
} }
extern "C" { extern "C" {
void user_init(void) void user_init(void) {
{
uart_div_modify(0, UART_CLK_FREQ / (115200)); uart_div_modify(0, UART_CLK_FREQ / (115200));
init(); init();
@ -140,8 +127,7 @@ void user_init(void)
cont_init(&g_cont); cont_init(&g_cont);
system_os_task(loop_task, system_os_task(loop_task,
LOOP_TASK_PRIORITY, LOOP_TASK_PRIORITY, g_loop_queue,
g_loop_queue,
LOOP_QUEUE_SIZE); LOOP_QUEUE_SIZE);
system_init_done_cb(&init_done); system_init_done_cb(&init_done);

View File

@ -72,7 +72,8 @@ char* itoa(int value, char* result, int base) {
} while(quotient); } while(quotient);
// Apply negative sign // Apply negative sign
if(value < 0) *out++ = '-'; if(value < 0)
*out++ = '-';
reverse(result, out); reverse(result, out);
*out = 0; *out = 0;
@ -96,7 +97,8 @@ char* ltoa(long value, char* result, int base) {
} while(quotient); } while(quotient);
// Apply negative sign // Apply negative sign
if(value < 0) *out++ = '-'; if(value < 0)
*out++ = '-';
reverse(result, out); reverse(result, out);
*out = 0; *out = 0;
@ -194,3 +196,4 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
return s; return s;
} }

View File

@ -19,7 +19,6 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "wiring_private.h" #include "wiring_private.h"
#include "ets_sys.h" #include "ets_sys.h"
#include "osapi.h" #include "osapi.h"
@ -36,57 +35,45 @@ static uint32_t micros_overflow_count = 0;
#define ONCE 0 #define ONCE 0
#define REPEAT 1 #define REPEAT 1
void delay_end(void* arg) {
void delay_end(void* arg)
{
esp_schedule(); esp_schedule();
} }
void delay(unsigned long ms) void delay(unsigned long ms) {
{ if(ms) {
if (ms)
{
os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0); os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0);
os_timer_arm(&delay_timer, ms, ONCE); os_timer_arm(&delay_timer, ms, ONCE);
} } else {
else
{
esp_schedule(); esp_schedule();
} }
esp_yield(); esp_yield();
if (ms) if(ms) {
{
os_timer_disarm(&delay_timer); os_timer_disarm(&delay_timer);
} }
} }
void micros_overflow_tick(void* arg) void micros_overflow_tick(void* arg) {
{
uint32_t m = system_get_time(); uint32_t m = system_get_time();
if(m < micros_at_last_overflow_tick) if(m < micros_at_last_overflow_tick)
++micros_overflow_count; ++micros_overflow_count;
micros_at_last_overflow_tick = m; micros_at_last_overflow_tick = m;
} }
unsigned long millis() unsigned long millis() {
{
uint32_t m = system_get_time(); uint32_t m = system_get_time();
uint32_t c = micros_overflow_count + ((m < micros_at_last_overflow_tick) ? 1 : 0); uint32_t c = micros_overflow_count + ((m < micros_at_last_overflow_tick) ? 1 : 0);
return c * 4294967 + m / 1000; return c * 4294967 + m / 1000;
} }
unsigned long micros() unsigned long micros() {
{
return system_get_time(); return system_get_time();
} }
void delayMicroseconds(unsigned int us) void delayMicroseconds(unsigned int us) {
{
os_delay_us(us); os_delay_us(us);
} }
void init() void init() {
{
initPins(); initPins();
os_timer_setfn(&micros_overflow_timer, (os_timer_func_t*) &micros_overflow_tick, 0); os_timer_setfn(&micros_overflow_timer, (os_timer_func_t*) &micros_overflow_tick, 0);
os_timer_arm(&micros_overflow_timer, 60000, REPEAT); os_timer_arm(&micros_overflow_timer, 60000, REPEAT);

View File

@ -22,18 +22,14 @@
#include "wiring_private.h" #include "wiring_private.h"
#include "pins_arduino.h" #include "pins_arduino.h"
void analogReference(uint8_t mode) {
void analogReference(uint8_t mode)
{
} }
extern int __analogRead(uint8_t pin) extern int __analogRead(uint8_t pin) {
{
if(pin == 0) if(pin == 0)
return system_adc_read(); return system_adc_read();
return 0; return 0;
} }
extern int analogRead(uint8_t pin) __attribute__ ((weak, alias("__analogRead"))); extern int analogRead(uint8_t pin) __attribute__ ((weak, alias("__analogRead")));

View File

@ -31,77 +31,42 @@
#define PINCOUNT 16 #define PINCOUNT 16
static const uint32_t g_pin_muxes[PINCOUNT] = { static const uint32_t g_pin_muxes[PINCOUNT] = { [0] = PERIPHS_IO_MUX_GPIO0_U, [1] = PERIPHS_IO_MUX_U0TXD_U, [2] = PERIPHS_IO_MUX_GPIO2_U, [3] = PERIPHS_IO_MUX_U0RXD_U, [4] = PERIPHS_IO_MUX_GPIO4_U, [5] = PERIPHS_IO_MUX_GPIO5_U,
[0] = PERIPHS_IO_MUX_GPIO0_U,
[1] = PERIPHS_IO_MUX_U0TXD_U,
[2] = PERIPHS_IO_MUX_GPIO2_U,
[3] = PERIPHS_IO_MUX_U0RXD_U,
[4] = PERIPHS_IO_MUX_GPIO4_U,
[5] = PERIPHS_IO_MUX_GPIO5_U,
// These 6 pins are used for SPI flash interface // These 6 pins are used for SPI flash interface
[6] = 0, [6] = 0, [7] = 0, [8] = 0, [9] = 0, [10] = 0, [11] = 0,
[7] = 0,
[8] = 0,
[9] = 0,
[10] = 0,
[11] = 0,
[12] = PERIPHS_IO_MUX_MTDI_U, [12] = PERIPHS_IO_MUX_MTDI_U, [13] = PERIPHS_IO_MUX_MTCK_U, [14] = PERIPHS_IO_MUX_MTMS_U, [15] = PERIPHS_IO_MUX_MTDO_U, };
[13] = PERIPHS_IO_MUX_MTCK_U,
[14] = PERIPHS_IO_MUX_MTMS_U,
[15] = PERIPHS_IO_MUX_MTDO_U,
};
static const uint32_t g_pin_funcs[PINCOUNT] = { static const uint32_t g_pin_funcs[PINCOUNT] = { [0] = FUNC_GPIO0, [1] = FUNC_GPIO1, [2] = FUNC_GPIO2, [3] = FUNC_GPIO3, [4] = FUNC_GPIO4, [5] = FUNC_GPIO5, [12] = FUNC_GPIO12, [13] = FUNC_GPIO13, [14] = FUNC_GPIO14, [15] = FUNC_GPIO15, };
[0] = FUNC_GPIO0,
[1] = FUNC_GPIO1,
[2] = FUNC_GPIO2,
[3] = FUNC_GPIO3,
[4] = FUNC_GPIO4,
[5] = FUNC_GPIO5,
[12] = FUNC_GPIO12,
[13] = FUNC_GPIO13,
[14] = FUNC_GPIO14,
[15] = FUNC_GPIO15,
};
uint32_t digitalPinToPort(uint32_t pin) {
uint32_t digitalPinToPort(uint32_t pin)
{
return 0; return 0;
} }
uint32_t digitalPinToBitMask(uint32_t pin) uint32_t digitalPinToBitMask(uint32_t pin) {
{
return 1 << pin; return 1 << pin;
} }
volatile uint32_t* portOutputRegister(uint32_t port) volatile uint32_t* portOutputRegister(uint32_t port) {
{
return (volatile uint32_t*) (PERIPHS_GPIO_BASEADDR + GPIO_OUT_ADDRESS); return (volatile uint32_t*) (PERIPHS_GPIO_BASEADDR + GPIO_OUT_ADDRESS);
} }
volatile uint32_t* portInputRegister(uint32_t port) volatile uint32_t* portInputRegister(uint32_t port) {
{
return (volatile uint32_t*) (PERIPHS_GPIO_BASEADDR + GPIO_IN_ADDRESS); return (volatile uint32_t*) (PERIPHS_GPIO_BASEADDR + GPIO_IN_ADDRESS);
} }
volatile uint32_t* portModeRegister(uint32_t port) volatile uint32_t* portModeRegister(uint32_t port) {
{
return (volatile uint32_t*) (PERIPHS_GPIO_BASEADDR + GPIO_ENABLE_ADDRESS); return (volatile uint32_t*) (PERIPHS_GPIO_BASEADDR + GPIO_ENABLE_ADDRESS);
} }
enum PinFunction {
enum PinFunction { GPIO, PWM }; GPIO, PWM
static uint32_t g_gpio_function[PINCOUNT] = {
GPIO
}; };
static uint32_t g_gpio_function[PINCOUNT] = { GPIO };
extern void __pinMode(uint8_t pin, uint8_t mode) extern void __pinMode(uint8_t pin, uint8_t mode) {
{ if(pin == 16) {
if (pin == 16)
{
uint32_t val = (mode == OUTPUT) ? 1 : 0; uint32_t val = (mode == OUTPUT) ? 1 : 0;
MODIFY_PERI_REG(PAD_XPD_DCDC_CONF, 0x43, 1); MODIFY_PERI_REG(PAD_XPD_DCDC_CONF, 0x43, 1);
@ -111,36 +76,23 @@ extern void __pinMode(uint8_t pin, uint8_t mode)
} }
uint32_t mux = g_pin_muxes[pin]; uint32_t mux = g_pin_muxes[pin];
if (mode == INPUT) if(mode == INPUT) {
{
gpio_output_set(0, 0, 0, 1 << pin); gpio_output_set(0, 0, 0, 1 << pin);
PIN_PULLUP_DIS(mux); PIN_PULLUP_DIS(mux);
} } else if(mode == INPUT_PULLUP) {
else if (mode == INPUT_PULLUP)
{
gpio_output_set(0, 0, 0, 1 << pin); gpio_output_set(0, 0, 0, 1 << pin);
PIN_PULLUP_EN(mux); PIN_PULLUP_EN(mux);
} } else if(mode == OUTPUT) {
else if (mode == OUTPUT)
{
gpio_output_set(0, 0, 1 << pin, 0); gpio_output_set(0, 0, 1 << pin, 0);
} } else if(mode == OUTPUT_OPEN_DRAIN) {
else if (mode == OUTPUT_OPEN_DRAIN) GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pin)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pin))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE));
{
GPIO_REG_WRITE(
GPIO_PIN_ADDR(GPIO_ID_PIN(pin)),
GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pin))) |
GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)
);
GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << pin)); GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << pin));
} }
} }
extern void __digitalWrite(uint8_t pin, uint8_t val) extern void __digitalWrite(uint8_t pin, uint8_t val) {
{ if(pin == 16) {
if (pin == 16)
{
MODIFY_PERI_REG(RTC_GPIO_OUT, 1, (val & 1)); MODIFY_PERI_REG(RTC_GPIO_OUT, 1, (val & 1));
return; return;
} }
@ -152,74 +104,56 @@ extern void __digitalWrite(uint8_t pin, uint8_t val)
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, mask); GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, mask);
} }
extern int __digitalRead(uint8_t pin) extern int __digitalRead(uint8_t pin) {
{
if(pin == 16) if(pin == 16)
return (READ_PERI_REG(RTC_GPIO_IN_DATA) & 1); return (READ_PERI_REG(RTC_GPIO_IN_DATA) & 1);
else else
return ((gpio_input_get() >> pin) & 1); return ((gpio_input_get() >> pin) & 1);
} }
extern void __analogWrite(uint8_t pin, int val) extern void __analogWrite(uint8_t pin, int val) {
{
} }
typedef void (*voidFuncPtr)(void); typedef void (*voidFuncPtr)(void);
static voidFuncPtr g_handlers[PINCOUNT] = { 0 }; static voidFuncPtr g_handlers[PINCOUNT] = { 0 };
void interrupt_handler(void *arg) {
void interrupt_handler(void *arg)
{
uint32_t intr_mask = GPIO_REG_READ(GPIO_STATUS_ADDRESS); uint32_t intr_mask = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
for (int pin = 0; intr_mask; intr_mask >>= 1, ++pin) for(int pin = 0; intr_mask; intr_mask >>= 1, ++pin) {
{ if((intr_mask & 1) && g_handlers[pin]) {
if ((intr_mask & 1) && g_handlers[pin])
{
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 1 << pin); GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 1 << pin);
(*g_handlers[pin])(); (*g_handlers[pin])();
} }
} }
} }
extern void __attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode) extern void __attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode) {
{
if(pin < 0 || pin > PINCOUNT) if(pin < 0 || pin > PINCOUNT)
return; return;
g_handlers[pin] = handler; g_handlers[pin] = handler;
if (mode == RISING) if(mode == RISING) {
{
gpio_pin_intr_state_set(pin, GPIO_PIN_INTR_POSEDGE); gpio_pin_intr_state_set(pin, GPIO_PIN_INTR_POSEDGE);
} } else if(mode == FALLING) {
else if (mode == FALLING)
{
gpio_pin_intr_state_set(pin, GPIO_PIN_INTR_NEGEDGE); gpio_pin_intr_state_set(pin, GPIO_PIN_INTR_NEGEDGE);
} } else if(mode == CHANGE) {
else if (mode == CHANGE)
{
gpio_pin_intr_state_set(pin, GPIO_PIN_INTR_ANYEDGE); gpio_pin_intr_state_set(pin, GPIO_PIN_INTR_ANYEDGE);
} } else {
else
{
gpio_pin_intr_state_set(pin, GPIO_PIN_INTR_DISABLE); gpio_pin_intr_state_set(pin, GPIO_PIN_INTR_DISABLE);
} }
} }
extern void __detachInterrupt(uint8_t pin) extern void __detachInterrupt(uint8_t pin) {
{
g_handlers[pin] = 0; g_handlers[pin] = 0;
gpio_pin_intr_state_set(pin, GPIO_PIN_INTR_DISABLE); gpio_pin_intr_state_set(pin, GPIO_PIN_INTR_DISABLE);
} }
void initPins() void initPins() {
{
gpio_init(); gpio_init();
for (int i = 0; i < PINCOUNT; ++i) for(int i = 0; i < PINCOUNT; ++i) {
{
uint32_t mux = g_pin_muxes[i]; uint32_t mux = g_pin_muxes[i];
if (mux) if(mux) {
{
uint32_t func = g_pin_funcs[i]; uint32_t func = g_pin_funcs[i];
PIN_FUNC_SELECT(mux, func); PIN_FUNC_SELECT(mux, func);
} }

View File

@ -26,7 +26,6 @@
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
* to 3 minutes in length, but must be called at least a few dozen microseconds * to 3 minutes in length, but must be called at least a few dozen microseconds
* before the start of the pulse. */ * before the start of the pulse. */
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) {
{
return 0; return 0;
} }

View File

@ -42,8 +42,7 @@ uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
return value; return value;
} }
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) {
{
uint8_t i; uint8_t i;
for(i = 0; i < 8; i++) { for(i = 0; i < 8; i++) {

View File

@ -33,8 +33,7 @@ static uint8_t s_sda_pin = 0;
static uint8_t s_scl_pin = 2; static uint8_t s_scl_pin = 2;
static uint32_t s_i2c_delay = 5; static uint32_t s_i2c_delay = 5;
static inline void i2c_digital_write(int pin, int val) static inline void i2c_digital_write(int pin, int val) {
{
uint32_t mask = 1 << pin; uint32_t mask = 1 << pin;
if(val) if(val)
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, mask); GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, mask);
@ -42,47 +41,38 @@ static inline void i2c_digital_write(int pin, int val)
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, mask); GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, mask);
} }
static inline void i2c_set(int sda, int scl) static inline void i2c_set(int sda, int scl) {
{
i2c_digital_write(s_sda_pin, sda); i2c_digital_write(s_sda_pin, sda);
i2c_digital_write(s_scl_pin, scl); i2c_digital_write(s_scl_pin, scl);
} }
static inline void i2c_set_sda(int sda) static inline void i2c_set_sda(int sda) {
{
i2c_digital_write(s_sda_pin, sda); i2c_digital_write(s_sda_pin, sda);
} }
static inline void i2c_set_scl(int scl) static inline void i2c_set_scl(int scl) {
{
i2c_digital_write(s_scl_pin, scl); i2c_digital_write(s_scl_pin, scl);
} }
static inline uint8_t i2c_get_sda() static inline uint8_t i2c_get_sda() {
{
return GPIO_INPUT_GET(GPIO_ID_PIN(s_sda_pin)); return GPIO_INPUT_GET(GPIO_ID_PIN(s_sda_pin));
} }
static inline uint8_t i2c_get_scl() static inline uint8_t i2c_get_scl() {
{
return GPIO_INPUT_GET(GPIO_ID_PIN(s_scl_pin)); return GPIO_INPUT_GET(GPIO_ID_PIN(s_scl_pin));
} }
static inline void i2c_wait() {
static inline void i2c_wait()
{
delayMicroseconds(s_i2c_delay); delayMicroseconds(s_i2c_delay);
} }
void i2c_freq(int freq_hz) void i2c_freq(int freq_hz) {
{
s_i2c_delay = 1000000 / freq_hz / 4 - 1; s_i2c_delay = 1000000 / freq_hz / 4 - 1;
if(s_i2c_delay < 0) if(s_i2c_delay < 0)
s_i2c_delay = 0; s_i2c_delay = 0;
} }
void i2c_init(int sda_pin, int scl_pin) void i2c_init(int sda_pin, int scl_pin) {
{
s_sda_pin = sda_pin; s_sda_pin = sda_pin;
s_scl_pin = scl_pin; s_scl_pin = scl_pin;
pinMode(ESP_PINS_OFFSET + sda_pin, OUTPUT_OPEN_DRAIN); pinMode(ESP_PINS_OFFSET + sda_pin, OUTPUT_OPEN_DRAIN);
@ -91,15 +81,12 @@ void i2c_init(int sda_pin, int scl_pin)
i2c_wait(); i2c_wait();
} }
void i2c_release() {
void i2c_release()
{
pinMode(ESP_PINS_OFFSET + s_sda_pin, INPUT); pinMode(ESP_PINS_OFFSET + s_sda_pin, INPUT);
pinMode(ESP_PINS_OFFSET + s_scl_pin, INPUT); pinMode(ESP_PINS_OFFSET + s_scl_pin, INPUT);
} }
void i2c_start() void i2c_start() {
{
i2c_set(1, 1); i2c_set(1, 1);
i2c_wait(); i2c_wait();
i2c_set_sda(0); i2c_set_sda(0);
@ -109,8 +96,7 @@ void i2c_start()
i2c_wait(); i2c_wait();
} }
void i2c_stop() void i2c_stop() {
{
i2c_wait(); i2c_wait();
i2c_set_scl(1); i2c_set_scl(1);
i2c_wait(); i2c_wait();
@ -118,8 +104,7 @@ void i2c_stop()
i2c_wait(); i2c_wait();
} }
void i2c_set_ack(int ack) void i2c_set_ack(int ack) {
{
i2c_set_sda(!ack); i2c_set_sda(!ack);
i2c_wait(); i2c_wait();
i2c_set_scl(1); i2c_set_scl(1);
@ -130,8 +115,7 @@ void i2c_set_ack(int ack)
i2c_wait(); i2c_wait();
} }
int i2c_get_ack() int i2c_get_ack() {
{
i2c_set_scl(1); i2c_set_scl(1);
i2c_wait(); i2c_wait();
int result = i2c_get_sda(); int result = i2c_get_sda();
@ -142,11 +126,9 @@ int i2c_get_ack()
return result; return result;
} }
uint8_t i2c_read(void) uint8_t i2c_read(void) {
{
uint8_t result = 0; uint8_t result = 0;
for (int i = 7; i >= 0; --i) for(int i = 7; i >= 0; --i) {
{
i2c_wait(); i2c_wait();
i2c_set_scl(1); i2c_set_scl(1);
i2c_wait(); i2c_wait();
@ -157,11 +139,8 @@ uint8_t i2c_read(void)
return result; return result;
} }
void i2c_write(uint8_t val) {
void i2c_write(uint8_t val) for(int i = 7; i >= 0; --i) {
{
for (int i = 7; i >= 0; --i)
{
i2c_set_sda((val >> i) & 1); i2c_set_sda((val >> i) & 1);
i2c_wait(); i2c_wait();
i2c_set_scl(1); i2c_set_scl(1);
@ -172,14 +151,12 @@ void i2c_write(uint8_t val)
i2c_set_sda(1); i2c_set_sda(1);
} }
size_t i2c_master_read_from(int address, uint8_t* data, size_t size, bool sendStop) size_t i2c_master_read_from(int address, uint8_t* data, size_t size, bool sendStop) {
{
i2c_start(); i2c_start();
i2c_write(address << 1 | 1); i2c_write(address << 1 | 1);
int ack = i2c_get_ack(); int ack = i2c_get_ack();
uint8_t* end = data + size; uint8_t* end = data + size;
for (;data != end; ++data ) for(; data != end; ++data) {
{
i2c_set_sda(1); i2c_set_sda(1);
pinMode(ESP_PINS_OFFSET + s_sda_pin, INPUT); pinMode(ESP_PINS_OFFSET + s_sda_pin, INPUT);
*data = i2c_read(); *data = i2c_read();
@ -194,14 +171,12 @@ size_t i2c_master_read_from(int address, uint8_t* data, size_t size, bool sendSt
return size; return size;
} }
size_t i2c_master_write_to(int address, const uint8_t* data, size_t size, bool sendStop) size_t i2c_master_write_to(int address, const uint8_t* data, size_t size, bool sendStop) {
{
i2c_start(); i2c_start();
i2c_write(address << 1); i2c_write(address << 1);
int ack = i2c_get_ack(); int ack = i2c_get_ack();
const uint8_t* end = data + size; const uint8_t* end = data + size;
for (;data != end; ++data ) for(; data != end; ++data) {
{
i2c_write(*data); i2c_write(*data);
ack = i2c_get_ack(); ack = i2c_get_ack();
} }
@ -211,38 +186,36 @@ size_t i2c_master_write_to(int address, const uint8_t* data, size_t size, bool s
} }
// some stubs for libraries that use private twi.h interface // some stubs for libraries that use private twi.h interface
extern "C" extern "C" {
{ void twi_init(void) {
void twi_init(void) { } }
void twi_setAddress(uint8_t) { } void twi_setAddress(uint8_t) {
}
uint8_t twi_readFrom(uint8_t addr, uint8_t* data, uint8_t size, uint8_t sendStop) uint8_t twi_readFrom(uint8_t addr, uint8_t* data, uint8_t size, uint8_t sendStop) {
{
return i2c_master_read_from(addr, data, size, sendStop); return i2c_master_read_from(addr, data, size, sendStop);
} }
uint8_t twi_writeTo(uint8_t addr, uint8_t* data, uint8_t size, uint8_t wait, uint8_t sendStop) uint8_t twi_writeTo(uint8_t addr, uint8_t* data, uint8_t size, uint8_t wait, uint8_t sendStop) {
{
return i2c_master_write_to(addr, data, size, sendStop); return i2c_master_write_to(addr, data, size, sendStop);
} }
uint8_t twi_transmit(const uint8_t* data, uint8_t length) uint8_t twi_transmit(const uint8_t* data, uint8_t length) {
{
} }
void twi_attachSlaveRxEvent(void (*)(uint8_t*, int)) {
void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) ) { } }
void twi_attachSlaveTxEvent( void (*)(void) ) { } void twi_attachSlaveTxEvent(void (*)(void)) {
void twi_reply(uint8_t) { } }
void twi_stop(void) void twi_reply(uint8_t) {
{ }
void twi_stop(void) {
i2c_stop(); i2c_stop();
} }
void twi_releaseBus(void) void twi_releaseBus(void) {
{
i2c_set(1, 1); i2c_set(1, 1);
} }
} }

View File

@ -34,7 +34,6 @@ int i2c_get_ack();
uint8_t i2c_read(void); uint8_t i2c_read(void);
void i2c_write(uint8_t val); void i2c_write(uint8_t val);
size_t i2c_master_read_from(int address, uint8_t* data, size_t size, bool sendStop); size_t i2c_master_read_from(int address, uint8_t* data, size_t size, bool sendStop);
size_t i2c_master_write_to(int address, const uint8_t* data, size_t size, bool sendStop); size_t i2c_master_write_to(int address, const uint8_t* data, size_t size, bool sendStop);

View File

@ -36,7 +36,6 @@
#include "mem.h" #include "mem.h"
#include "user_interface.h" #include "user_interface.h"
void* malloc(size_t size) { void* malloc(size_t size) {
return os_malloc(size); return os_malloc(size);
} }
@ -223,7 +222,6 @@ char * strdup(const char *str) {
return cstr; return cstr;
} }
long int ICACHE_FLASH_ATTR strtol(const char* str, char** endptr, int base) { long int ICACHE_FLASH_ATTR strtol(const char* str, char** endptr, int base) {
long int result = 0; long int result = 0;
int sign = 1; int sign = 1;
@ -275,7 +273,6 @@ long int ICACHE_FLASH_ATTR strtol(const char* str, char** endptr, int base) {
return sign * result; return sign * result;
} }
// based on Source: // based on Source:
// https://github.com/anakod/Sming/blob/master/Sming/system/stringconversion.cpp#L93 // https://github.com/anakod/Sming/blob/master/Sming/system/stringconversion.cpp#L93
double ICACHE_FLASH_ATTR strtod(const char* str, char** endptr) { double ICACHE_FLASH_ATTR strtod(const char* str, char** endptr) {

View File

@ -6,7 +6,6 @@
#define PROGMEM #define PROGMEM
#define PGM_P const char * #define PGM_P const char *
#define PSTR(str) (str) #define PSTR(str) (str)
#define F(str) (str)
#define vsnprintf_P(...) vsnprintf( __VA_ARGS__ ) #define vsnprintf_P(...) vsnprintf( __VA_ARGS__ )
#define snprintf_P(...) snprintf( __VA_ARGS__ ) #define snprintf_P(...) snprintf( __VA_ARGS__ )

View File

@ -29,21 +29,10 @@ typedef void(*discard_cb_t)(void*, ClientContext*);
extern "C" void esp_yield(); extern "C" void esp_yield();
extern "C" void esp_schedule(); extern "C" void esp_schedule();
class ClientContext {
class ClientContext
{
public: public:
ClientContext(tcp_pcb* pcb, ClientContext(tcp_pcb* pcb, discard_cb_t discard_cb, void* discard_cb_arg) :
discard_cb_t discard_cb, void* discard_cb_arg) _pcb(pcb), _rx_buf(0), _rx_buf_offset(0), _discard_cb(discard_cb), _discard_cb_arg(discard_cb_arg), _refcnt(0), _next(0), _send_waiting(false) {
: _pcb(pcb)
, _rx_buf(0)
, _rx_buf_offset(0)
, _discard_cb(discard_cb)
, _discard_cb_arg(discard_cb_arg)
, _refcnt(0)
, _next(0)
, _send_waiting(false)
{
tcp_setprio(pcb, TCP_PRIO_MIN); tcp_setprio(pcb, TCP_PRIO_MIN);
tcp_arg(pcb, this); tcp_arg(pcb, this);
tcp_recv(pcb, &_s_recv); tcp_recv(pcb, &_s_recv);
@ -51,35 +40,28 @@ public:
tcp_err(pcb, &_s_error); tcp_err(pcb, &_s_error);
} }
~ClientContext() ~ClientContext() {
{
} }
ClientContext* next() const ClientContext* next() const {
{
return _next; return _next;
} }
ClientContext* next(ClientContext* new_next) ClientContext* next(ClientContext* new_next) {
{
_next = new_next; _next = new_next;
} }
void ref() void ref() {
{
++_refcnt; ++_refcnt;
DEBUGV(":ref %d\r\n", _refcnt); DEBUGV(":ref %d\r\n", _refcnt);
} }
void unref() void unref() {
{
err_t err; err_t err;
DEBUGV(":ur %d\r\n", _refcnt); DEBUGV(":ur %d\r\n", _refcnt);
if (--_refcnt == 0) if(--_refcnt == 0) {
{
flush(); flush();
if (_pcb) if(_pcb) {
{
tcp_arg(_pcb, NULL); tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL); tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL); tcp_recv(_pcb, NULL);
@ -95,52 +77,41 @@ public:
} }
} }
uint32_t getRemoteAddress() uint32_t getRemoteAddress() {
{ if(!_pcb) return 0;
if (!_pcb)
return 0;
return _pcb->remote_ip.addr; return _pcb->remote_ip.addr;
} }
uint16_t getRemotePort() uint16_t getRemotePort() {
{ if(!_pcb) return 0;
if (!_pcb)
return 0;
return _pcb->remote_port; return _pcb->remote_port;
} }
size_t getSize() const size_t getSize() const {
{ if(!_rx_buf) return 0;
if (!_rx_buf)
return 0;
return _rx_buf->tot_len - _rx_buf_offset; return _rx_buf->tot_len - _rx_buf_offset;
} }
char read() char read() {
{ if(!_rx_buf) return 0;
if (!_rx_buf)
return 0;
char c = reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset]; char c = reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset];
_consume(1); _consume(1);
return c; return c;
} }
size_t read(char* dst, size_t size) size_t read(char* dst, size_t size) {
{ if(!_rx_buf) return 0;
if (!_rx_buf)
return 0;
size_t max_size = _rx_buf->tot_len - _rx_buf_offset; size_t max_size = _rx_buf->tot_len - _rx_buf_offset;
size = (size < max_size) ? size : max_size; size = (size < max_size) ? size : max_size;
DEBUGV(":rd %d, %d, %d\r\n", size, _rx_buf->tot_len, _rx_buf_offset); DEBUGV(":rd %d, %d, %d\r\n", size, _rx_buf->tot_len, _rx_buf_offset);
size_t size_read = 0; size_t size_read = 0;
while(size) while(size) {
{
size_t buf_size = _rx_buf->len - _rx_buf_offset; size_t buf_size = _rx_buf->len - _rx_buf_offset;
size_t copy_size = (size < buf_size) ? size : buf_size; size_t copy_size = (size < buf_size) ? size : buf_size;
DEBUGV(":rdi %d, %d\r\n", buf_size, copy_size); DEBUGV(":rdi %d, %d\r\n", buf_size, copy_size);
@ -153,16 +124,13 @@ public:
return size_read; return size_read;
} }
char peek() char peek() {
{ if(!_rx_buf) return 0;
if (!_rx_buf)
return 0;
return reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset]; return reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset];
} }
void flush() void flush() {
{
if(!_rx_buf) { if(!_rx_buf) {
return; return;
} }
@ -174,16 +142,13 @@ public:
_rx_buf_offset = 0; _rx_buf_offset = 0;
} }
uint8_t state() const uint8_t state() const {
{ if(!_pcb) return CLOSED;
if (!_pcb)
return CLOSED;
return _pcb->state; return _pcb->state;
} }
size_t write(const char* data, size_t size) size_t write(const char* data, size_t size) {
{
if(!_pcb) { if(!_pcb) {
DEBUGV(":wr !_pcb\r\n"); DEBUGV(":wr !_pcb\r\n");
return 0; return 0;
@ -210,40 +175,30 @@ public:
return will_send - _size_sent; return will_send - _size_sent;
} }
private: private:
void _consume(size_t size) void _consume(size_t size) {
{
ptrdiff_t left = _rx_buf->len - _rx_buf_offset - size; ptrdiff_t left = _rx_buf->len - _rx_buf_offset - size;
if (left > 0) if(left > 0) {
{
_rx_buf_offset += size; _rx_buf_offset += size;
} } else if(!_rx_buf->next) {
else if (!_rx_buf->next)
{
DEBUGV(":c0 %d, %d\r\n", size, _rx_buf->tot_len); DEBUGV(":c0 %d, %d\r\n", size, _rx_buf->tot_len);
if (_pcb) if(_pcb) tcp_recved(_pcb, _rx_buf->len);
tcp_recved(_pcb, _rx_buf->len);
pbuf_free(_rx_buf); pbuf_free(_rx_buf);
_rx_buf = 0; _rx_buf = 0;
_rx_buf_offset = 0; _rx_buf_offset = 0;
} } else {
else
{
DEBUGV(":c %d, %d, %d\r\n", size, _rx_buf->len, _rx_buf->tot_len); DEBUGV(":c %d, %d, %d\r\n", size, _rx_buf->len, _rx_buf->tot_len);
auto head = _rx_buf; auto head = _rx_buf;
_rx_buf = _rx_buf->next; _rx_buf = _rx_buf->next;
_rx_buf_offset = 0; _rx_buf_offset = 0;
pbuf_ref(_rx_buf); pbuf_ref(_rx_buf);
if (_pcb) if(_pcb) tcp_recved(_pcb, head->len);
tcp_recved(_pcb, head->len);
pbuf_free(head); pbuf_free(head);
} }
} }
err_t _recv(tcp_pcb* pcb, pbuf* pb, err_t err) err_t _recv(tcp_pcb* pcb, pbuf* pb, err_t err) {
{
if(pb == 0) // connection closed if(pb == 0) // connection closed
{ {
@ -264,15 +219,12 @@ private:
return ERR_OK; return ERR_OK;
} }
if (_rx_buf) if(_rx_buf) {
{
// there is some unread data // there is some unread data
// chain the new pbuf to the existing one // chain the new pbuf to the existing one
DEBUGV(":rch %d, %d\r\n", _rx_buf->tot_len, pb->tot_len); DEBUGV(":rch %d, %d\r\n", _rx_buf->tot_len, pb->tot_len);
pbuf_cat(_rx_buf, pb); pbuf_cat(_rx_buf, pb);
} } else {
else
{
DEBUGV(":rn %d\r\n", pb->tot_len); DEBUGV(":rn %d\r\n", pb->tot_len);
_rx_buf = pb; _rx_buf = pb;
_rx_buf_offset = 0; _rx_buf_offset = 0;
@ -282,8 +234,7 @@ private:
return ERR_OK; return ERR_OK;
} }
void _error(err_t err) void _error(err_t err) {
{
DEBUGV(":er %d\r\n", err); DEBUGV(":er %d\r\n", err);
if(_pcb) { if(_pcb) {
@ -304,38 +255,30 @@ private:
} }
} }
err_t _poll(tcp_pcb* pcb) err_t _poll(tcp_pcb* pcb) {
{
return ERR_OK; return ERR_OK;
} }
err_t _sent(tcp_pcb* pcb, uint16_t len) err_t _sent(tcp_pcb* pcb, uint16_t len) {
{
DEBUGV(":sent %d\r\n", len); DEBUGV(":sent %d\r\n", len);
_size_sent -= len; _size_sent -= len;
if (_size_sent == 0 && _send_waiting) if(_size_sent == 0 && _send_waiting) esp_schedule();
esp_schedule();
return ERR_OK; return ERR_OK;
} }
static err_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err) {
static err_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err)
{
return reinterpret_cast<ClientContext*>(arg)->_recv(tpcb, pb, err); return reinterpret_cast<ClientContext*>(arg)->_recv(tpcb, pb, err);
} }
static void _s_error(void *arg, err_t err) static void _s_error(void *arg, err_t err) {
{
reinterpret_cast<ClientContext*>(arg)->_error(err); reinterpret_cast<ClientContext*>(arg)->_error(err);
} }
static err_t _s_poll(void *arg, struct tcp_pcb *tpcb) static err_t _s_poll(void *arg, struct tcp_pcb *tpcb) {
{
return reinterpret_cast<ClientContext*>(arg)->_poll(tpcb); return reinterpret_cast<ClientContext*>(arg)->_poll(tpcb);
} }
static err_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len) static err_t _s_sent(void *arg, struct tcp_pcb *tpcb, uint16_t len) {
{
return reinterpret_cast<ClientContext*>(arg)->_sent(tpcb, len); return reinterpret_cast<ClientContext*>(arg)->_sent(tpcb, len);
} }
@ -355,6 +298,4 @@ private:
bool _send_waiting; bool _send_waiting;
}; };
#endif//CLIENTCONTEXT_H #endif//CLIENTCONTEXT_H