1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-06 05:21:22 +03:00

Merge pull request #60 from Links2004/esp8266

add more libc functions, fix possible null prt, force close of tcp on error, fix printf / serial.print mix problems, code style
This commit is contained in:
Ivan Grokhotkov 2015-04-11 09:43:43 +08:00
commit 66633e742f
39 changed files with 3251 additions and 3250 deletions

View File

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

View File

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

View File

@ -61,11 +61,9 @@ extern "C" {
*
*/
// ####################################################################################################
// ####################################################################################################
// ####################################################################################################
HardwareSerial Serial(UART0);
HardwareSerial Serial1(UART1);
@ -111,7 +109,8 @@ void ICACHE_FLASH_ATTR uart_interrupt_handler(uart_t* uart) {
if(status & UART_RXFIFO_FULL_INT_ST) {
while(true) {
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--) {
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) {
while(true) {
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--) {
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) {
while(true) {
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;
}
pinMode(uart->rxPin, 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_U0TXD_U, FUNC_GPIO1);
pinMode(uart->rxPin, 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_MTDO_U, FUNC_GPIO15);
pinMode(uart->rxPin, INPUT);
pinMode(uart->txPin, INPUT);
uart->rxPin = 3;
@ -394,18 +392,32 @@ void ICACHE_FLASH_ATTR uart_ignore_char(char c) {
}
void ICACHE_FLASH_ATTR uart0_write_char(char c) {
if(&Serial != NULL && Serial.isTxEnabled()) {
if(c == '\n') {
Serial.write('\r');
}
Serial.write(c);
} else {
if(c == '\n') {
WRITE_PERI_REG(UART_FIFO(0), '\r');
}
WRITE_PERI_REG(UART_FIFO(0), c);
}
}
void ICACHE_FLASH_ATTR uart1_write_char(char c) {
if(&Serial1 != NULL && Serial1.isTxEnabled()) {
if(c == '\n') {
Serial1.write('\r');
}
Serial1.write(c);
} else {
if(c == '\n') {
WRITE_PERI_REG(UART_FIFO(1), '\r');
}
WRITE_PERI_REG(UART_FIFO(1), c);
}
}
static UARTnr_t s_uart_debug_nr = UART_NO;
void ICACHE_FLASH_ATTR uart_set_debug(UARTnr_t uart_nr) {
@ -484,12 +496,14 @@ void ICACHE_FLASH_ATTR HardwareSerial::setDebugOutput(bool en) {
}
bool ICACHE_FLASH_ATTR HardwareSerial::isTxEnabled(void) {
if(_uart == 0) return false;
if(_uart == 0)
return false;
return _uart->txEnabled;
}
bool ICACHE_FLASH_ATTR HardwareSerial::isRxEnabled(void) {
if(_uart == 0) return false;
if(_uart == 0)
return false;
return _uart->rxEnabled;
}
@ -526,8 +540,10 @@ int ICACHE_FLASH_ATTR HardwareSerial::availableForWrite(void) {
}
void ICACHE_FLASH_ATTR HardwareSerial::flush() {
if(!_uart->txEnabled) return;
if(!_written) return;
if(!_uart->txEnabled)
return;
if(!_written)
return;
while(_tx_buffer->getSize() || uart_get_tx_fifo_room(_uart) < UART_TX_FIFO_SIZE)
yield();
@ -536,7 +552,8 @@ void ICACHE_FLASH_ATTR HardwareSerial::flush() {
}
size_t ICACHE_FLASH_ATTR HardwareSerial::write(uint8_t c) {
if(!_uart->txEnabled) return 0;
if(!_uart->txEnabled)
return 0;
_written = true;
size_t room = uart_get_tx_fifo_room(_uart);
if(room > 0 && _tx_buffer->empty()) {

View File

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

View File

@ -21,51 +21,42 @@
#include <IPAddress.h>
#include <Print.h>
IPAddress::IPAddress()
{
IPAddress::IPAddress() {
_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[1] = second_octet;
_address.bytes[2] = third_octet;
_address.bytes[3] = fourth_octet;
}
IPAddress::IPAddress(uint32_t address)
{
IPAddress::IPAddress(uint32_t address) {
_address.dword = address;
}
IPAddress::IPAddress(const uint8_t *address)
{
IPAddress::IPAddress(const uint8_t *address) {
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));
return *this;
}
IPAddress& IPAddress::operator=(uint32_t address)
{
IPAddress& IPAddress::operator=(uint32_t address) {
_address.dword = address;
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;
}
size_t IPAddress::printTo(Print& p) const
{
size_t IPAddress::printTo(Print& p) const {
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('.');
}

View File

@ -36,7 +36,9 @@ private:
// 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
// stored.
uint8_t* raw_address() { return _address.bytes; };
uint8_t* raw_address() {
return _address.bytes;
}
public:
// Constructors
@ -47,13 +49,21 @@ public:
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
// to a four-byte uint8_t array is expected
operator uint32_t() const { return _address.dword; };
bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; };
operator uint32_t() const {
return _address.dword;
}
bool operator==(const IPAddress& addr) const {
return _address.dword == addr._address.dword;
}
bool operator==(const uint8_t* addr) const;
// 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) { return _address.bytes[index]; };
uint8_t operator[](int index) const {
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
IPAddress& operator=(const uint8_t *address);
@ -71,5 +81,4 @@ public:
const IPAddress INADDR_NONE(0, 0, 0, 0);
#endif

View File

@ -34,8 +34,7 @@ extern "C" {
// Public Methods //////////////////////////////////////////////////////////////
/* 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;
while(size--) {
n += write(*buffer++);
@ -43,38 +42,31 @@ size_t ICACHE_FLASH_ATTR Print::write(const uint8_t *buffer, size_t size)
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());
}
size_t ICACHE_FLASH_ATTR Print::print(const char str[])
{
size_t ICACHE_FLASH_ATTR Print::print(const char 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);
}
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);
}
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);
}
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);
}
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) {
return write(n);
} 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)
{
if (base == 0) return write(n);
else return printNumber(n, base);
size_t ICACHE_FLASH_ATTR Print::print(unsigned long n, int base) {
if(base == 0)
return write(n);
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);
}
size_t ICACHE_FLASH_ATTR Print::print(const Printable& x)
{
size_t ICACHE_FLASH_ATTR Print::print(const Printable& x) {
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");
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);
n += println();
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);
n += println();
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);
n += println();
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);
n += println();
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);
n += println();
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);
n += println();
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);
n += println();
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);
n += println();
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);
n += println();
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);
n += println();
return n;
@ -190,7 +170,8 @@ size_t ICACHE_FLASH_ATTR Print::printNumber(unsigned long n, uint8_t base) {
*str = '\0';
// prevent crash if called with base == 1
if (base < 2) base = 10;
if(base < 2)
base = 10;
do {
unsigned long m = n;
@ -202,18 +183,20 @@ size_t ICACHE_FLASH_ATTR Print::printNumber(unsigned long n, uint8_t base) {
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;
if (isnan(number)) return print("nan");
if (isinf(number)) return print("inf");
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
if(isnan(number))
return print("nan");
if(isinf(number))
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
if (number < 0.0)
{
if(number < 0.0) {
n += print('-');
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
while (digits-- > 0)
{
while(digits-- > 0) {
remainder *= 10.0;
int toPrint = int(remainder);
n += print(toPrint);

View File

@ -31,23 +31,31 @@
#define OCT 8
#define BIN 2
class Print
{
class Print {
private:
int write_error;
size_t printNumber(unsigned long, uint8_t);
size_t printFloat(double, uint8_t);
protected:
void setWriteError(int err = 1) { write_error = err; }
void setWriteError(int err = 1) {
write_error = err;
}
public:
Print() : write_error(0) {}
Print() :
write_error(0) {
}
int getWriteError() { return write_error; }
void clearWriteError() { setWriteError(0); }
int getWriteError() {
return write_error;
}
void clearWriteError() {
setWriteError(0);
}
virtual size_t write(uint8_t) = 0;
size_t write(const char *str) {
if (str == NULL) return 0;
if(str == NULL)
return 0;
return write((const uint8_t *) str, strlen(str));
}
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.
*/
class Printable
{
class Printable {
public:
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
// private method to read stream with timeout
int ICACHE_FLASH_ATTR Stream::timedRead()
{
int ICACHE_FLASH_ATTR Stream::timedRead() {
int c;
_startMillis = millis();
do {
c = read();
if (c >= 0) return c;
if(c >= 0)
return c;
yield();
} while(millis() - _startMillis < _timeout);
return -1; // -1 indicates timeout
}
// private method to peek stream with timeout
int ICACHE_FLASH_ATTR Stream::timedPeek()
{
int ICACHE_FLASH_ATTR Stream::timedPeek() {
int c;
_startMillis = millis();
do {
c = peek();
if (c >= 0) return c;
if(c >= 0)
return c;
yield();
} while(millis() - _startMillis < _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
// discards non-numeric characters
int ICACHE_FLASH_ATTR Stream::peekNextDigit()
{
int ICACHE_FLASH_ATTR Stream::peekNextDigit() {
int c;
while(1) {
c = timedPeek();
if (c < 0) return c; // timeout
if (c == '-') return c;
if (c >= '0' && c <= '9') return c;
if(c < 0)
return c; // timeout
if(c == '-')
return c;
if(c >= '0' && c <= '9')
return c;
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
bool ICACHE_FLASH_ATTR Stream::find(const char *target)
{
bool ICACHE_FLASH_ATTR Stream::find(const char *target) {
return findUntil(target, (char*) "");
}
// 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
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);
}
// 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));
}
// reads data from the stream until the target string of the given length is found
// search terminated if the terminator string is found
// 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 termIndex = 0;
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(++termIndex >= termLen)
return false; // return false if terminate string found before target string
}
else
} else
termIndex = 0;
}
return false;
}
// returns the first valid (long) integer value from the current position.
// initial characters that are not digits (or the minus sign) are skipped
// 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)
}
// as above but a given skipChar is 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;
long value = 0;
int c;
@ -159,18 +153,15 @@ long ICACHE_FLASH_ATTR Stream::parseInt(char skipChar)
value = value * 10 + c - '0';
read(); // consume the character we got with peek
c = timedPeek();
}
while( (c >= '0' && c <= '9') || c == skipChar );
} while((c >= '0' && c <= '9') || c == skipChar);
if(isNegative)
value = -value;
return 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);
}
@ -202,8 +193,7 @@ float ICACHE_FLASH_ATTR Stream::parseFloat(char skipChar){
}
read(); // consume the character we got with peek
c = timedPeek();
}
while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
} while((c >= '0' && c <= '9') || c == '.' || c == skipChar);
if(isNegative)
value = -value;
@ -218,54 +208,50 @@ float ICACHE_FLASH_ATTR Stream::parseFloat(char skipChar){
// returns the number of characters placed in the buffer
// 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;
while(count < length) {
int c = timedRead();
if (c < 0) break;
if(c < 0)
break;
*buffer++ = (char) c;
count++;
}
return count;
}
// as readBytes with terminator character
// 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)
size_t ICACHE_FLASH_ATTR Stream::readBytesUntil(char terminator, char *buffer, size_t length)
{
if (length < 1) return 0;
size_t ICACHE_FLASH_ATTR Stream::readBytesUntil(char terminator, char *buffer, size_t length) {
if(length < 1)
return 0;
size_t index = 0;
while(index < length) {
int c = timedRead();
if (c < 0 || c == terminator) break;
if(c < 0 || c == terminator)
break;
*buffer++ = (char) c;
index++;
}
return index; // return number of characters, not including null terminator
}
String ICACHE_FLASH_ATTR Stream::readString()
{
String ICACHE_FLASH_ATTR Stream::readString() {
String ret;
int c = timedRead();
while (c >= 0)
{
while(c >= 0) {
ret += (char) c;
c = timedRead();
}
return ret;
}
String ICACHE_FLASH_ATTR Stream::readStringUntil(char terminator)
{
String ICACHE_FLASH_ATTR Stream::readStringUntil(char terminator) {
String ret;
int c = timedRead();
while (c >= 0 && c != terminator)
{
while(c >= 0 && c != terminator) {
ret += (char) c;
c = timedRead();
}

View File

@ -35,8 +35,7 @@
readBytesBetween( pre_string, terminator, buffer, length)
*/
class Stream : public Print
{
class Stream: public Print {
protected:
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
unsigned long _startMillis; // used for timeout measurement
@ -50,26 +49,35 @@ class Stream : public Print
virtual int peek() = 0;
virtual void flush() = 0;
Stream() {_timeout=1000;}
Stream() {
_timeout = 1000;
}
// parsing methods
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(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)
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
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 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.
// 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
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)
// 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, 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
// 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 "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
virtual uint16_t remotePort() =0;
protected:
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
uint8_t* rawIPAddress(IPAddress& addr) {
return addr.raw_address();
}
};
#endif

View File

@ -24,7 +24,6 @@
#define isascii(__c) ((unsigned)(__c)<=0177)
#define toascii(__c) ((__c)&0177)
// WCharacter.h prototypes
inline boolean isAlphaNumeric(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 toUpperCase(int c) __attribute__((always_inline));
// Checks for an alphanumeric character.
// It is equivalent to (isalpha(c) || isdigit(c)).
inline boolean isAlphaNumeric(int c)
{
inline boolean isAlphaNumeric(int c) {
return (isalnum(c) == 0 ? false : true);
}
// Checks for an alphabetic character.
// It is equivalent to (isupper(c) || islower(c)).
inline boolean isAlpha(int c)
{
inline boolean isAlpha(int c) {
return (isalpha(c) == 0 ? false : true);
}
// Checks whether c is a 7-bit unsigned char value
// that fits into the ASCII character set.
inline boolean isAscii(int c)
{
inline boolean isAscii(int c) {
return ( isascii (c) == 0 ? false : true);
}
// 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);
}
// Checks for a control character.
inline boolean isControl(int c)
{
inline boolean isControl(int c) {
return (iscntrl(c) == 0 ? false : true);
}
// Checks for a digit (0 through 9).
inline boolean isDigit(int c)
{
inline boolean isDigit(int c) {
return (isdigit(c) == 0 ? false : true);
}
// Checks for any printable character except space.
inline boolean isGraph(int c)
{
inline boolean isGraph(int c) {
return (isgraph(c) == 0 ? false : true);
}
// Checks for a lower-case character.
inline boolean isLowerCase(int c)
{
inline boolean isLowerCase(int c) {
return (islower(c) == 0 ? false : true);
}
// Checks for any printable character including space.
inline boolean isPrintable(int c)
{
inline boolean isPrintable(int c) {
return (isprint(c) == 0 ? false : true);
}
// Checks for any printable character which is not a space
// or an alphanumeric character.
inline boolean isPunct(int c)
{
inline boolean isPunct(int c) {
return (ispunct(c) == 0 ? false : true);
}
// Checks for white-space characters. For the avr-libc library,
// these are: space, formfeed ('\f'), newline ('\n'), carriage
// 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);
}
// Checks for an uppercase letter.
inline boolean isUpperCase(int c)
{
inline boolean isUpperCase(int c) {
return (isupper(c) == 0 ? false : true);
}
// 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.
inline boolean isHexadecimalDigit(int c)
{
inline boolean isHexadecimalDigit(int c) {
return (isxdigit(c) == 0 ? false : true);
}
// Converts c to a 7-bit unsigned char value that fits into the
// ASCII character set, by clearing the high-order bits.
inline int toAscii(int c)
{
inline int toAscii(int c) {
return toascii(c);
}
// Warning:
// Many people will be unhappy if you use this function.
// This function will convert accented letters into random
// characters.
// Converts the letter c to lower case, if possible.
inline int toLowerCase(int c)
{
inline int toLowerCase(int c) {
return tolower(c);
}
// Converts the letter c to upper case, if possible.
inline int toUpperCase(int c)
{
inline int toUpperCase(int c) {
return toupper(c);
}

View File

@ -27,23 +27,20 @@ extern "C" {
#include <stdlib.h>
}
void randomSeed(unsigned int seed)
{
void randomSeed(unsigned int seed) {
if(seed != 0) {
srand(seed);
}
}
long random(long howbig)
{
long random(long howbig) {
if(howbig == 0) {
return 0;
}
return rand() % howbig;
}
long random(long howsmall, long howbig)
{
long random(long howsmall, long howbig) {
if(howsmall >= howbig) {
return howsmall;
}
@ -51,10 +48,14 @@ long random(long howsmall, long howbig)
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;
}
unsigned int makeWord(unsigned int w) { return w; }
unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; }
unsigned int makeWord(unsigned int w) {
return w;
}
unsigned int makeWord(unsigned char h, unsigned char l) {
return (h << 8) | l;
}

View File

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

View File

@ -33,16 +33,17 @@
class StringSumHelper;
typedef char* __FlashStringHelper;
//#define F(str) []() -> const char * { static const char tmp[] ICACHE_RODATA_ATTR = str; return &tmp[0]; }()
#define F(str) str
// The string class
class String
{
class String {
// use a function pointer to allow for "if (s)" without the
// complications of an operator bool(). for more information, see:
// http://www.artima.com/cppsource/safebool.html
typedef void (String::*StringIfHelperType)() const;
void StringIfHelper() const {}
void StringIfHelper() const {
}
public:
// constructors
@ -71,7 +72,9 @@ public:
// is left unchanged). reserve(0), if successful, will validate an
// invalid string (i.e., "if (s)" will be true afterwards)
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
// invalid, or if the memory allocation fails, the string will be
@ -101,16 +104,46 @@ public:
// if there's not enough memory for the concatenated value, the string
// will be left unchanged (but this isn't signalled in any way)
String & operator += (const String &rhs) {concat(rhs); return (*this);}
String & operator += (const char *cstr) {concat(cstr); return (*this);}
String & operator += (char c) {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);}
String & operator +=(const String &rhs) {
concat(rhs);
return (*this);
}
String & operator +=(const char *cstr) {
concat(cstr);
return (*this);
}
String & operator +=(char c) {
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 char *cstr);
@ -124,14 +157,24 @@ public:
friend StringSumHelper & operator +(const StringSumHelper &lhs, double num);
// 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;
unsigned char equals(const String &s) const;
unsigned char equals(const char *cstr) const;
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 {return !equals(rhs);}
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 {
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;
@ -147,9 +190,12 @@ public:
char operator [](unsigned int index) const;
char& operator [](unsigned int index);
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
{getBytes((unsigned char *)buf, bufsize, index);}
const char * c_str() const { return buffer; }
void toCharArray(char *buf, unsigned int bufsize, unsigned int index = 0) const {
getBytes((unsigned char *) buf, bufsize, index);
}
const char * c_str() const {
return buffer;
}
// search
int indexOf(char ch) const;
@ -160,7 +206,10 @@ public:
int lastIndexOf(char ch, unsigned int fromIndex) const;
int lastIndexOf(const String &str) 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;
// modification
@ -193,19 +242,38 @@ protected:
#endif
};
class StringSumHelper : public String
{
class StringSumHelper: public String {
public:
StringSumHelper(const String &s) : String(s) {}
StringSumHelper(const char *p) : String(p) {}
StringSumHelper(char c) : String(c) {}
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) {}
StringSumHelper(const String &s) :
String(s) {
}
StringSumHelper(const char *p) :
String(p) {
}
StringSumHelper(char c) :
String(c) {
}
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

View File

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

View File

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

View File

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

View File

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

View File

@ -34,56 +34,13 @@ int atoi(const char* s) {
}
long atol(const char* s) {
long int result = 0;
int i;
const char* b = s;
int sign = 1;
for(i = 0; *b; ++i, ++b) {
if(i == 0 && *b == '-') sign = -1;
int x = *b - '0';
if(x < 0 || x > 9) break;
result = result * 10 + x;
}
return sign * result;
char * tmp;
return strtol(s, &tmp, 10);
}
// Source:
// https://github.com/anakod/Sming/blob/master/Sming/system/stringconversion.cpp#L93
double atof(const char* s) {
double result = 0;
double factor = 1.0;
while(*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n')
++s;
if(*s == 0) return 0;
if(*s == '-') {
factor = -1.0;
++s;
}
if(*s == '+') {
++s;
}
bool decimals = false;
char c;
while((c = *s)) {
if(c == '.') {
decimals = true;
++s;
continue;
}
int d = c - '0';
if(d < 0 || d > 9) break;
result = 10.0 * result + d;
if(decimals) factor *= 0.1;
++s;
}
return result * factor;
char * tmp;
return strtod(s, &tmp);
}
void reverse(char* begin, char* end) {
@ -115,7 +72,8 @@ char* itoa(int value, char* result, int base) {
} while(quotient);
// Apply negative sign
if(value < 0) *out++ = '-';
if(value < 0)
*out++ = '-';
reverse(result, out);
*out = 0;
@ -139,7 +97,8 @@ char* ltoa(long value, char* result, int base) {
} while(quotient);
// Apply negative sign
if(value < 0) *out++ = '-';
if(value < 0)
*out++ = '-';
reverse(result, out);
*out = 0;
@ -189,7 +148,6 @@ char* ultoa(unsigned long value, char* result, int base) {
}
char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
size_t n = 0;
if(isnan(number)) {
strcpy(s, "nan");
@ -237,3 +195,4 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
return s;
}

View File

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

View File

@ -22,18 +22,14 @@
#include "wiring_private.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)
return system_adc_read();
return 0;
}
extern int analogRead(uint8_t pin) __attribute__ ((weak, alias("__analogRead")));

View File

@ -31,77 +31,42 @@
#define PINCOUNT 16
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,
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,
// These 6 pins are used for SPI flash interface
[6] = 0,
[7] = 0,
[8] = 0,
[9] = 0,
[10] = 0,
[11] = 0,
[6] = 0, [7] = 0, [8] = 0, [9] = 0, [10] = 0, [11] = 0,
[12] = PERIPHS_IO_MUX_MTDI_U,
[13] = PERIPHS_IO_MUX_MTCK_U,
[14] = PERIPHS_IO_MUX_MTMS_U,
[15] = PERIPHS_IO_MUX_MTDO_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, };
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,
};
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, };
uint32_t digitalPinToPort(uint32_t pin)
{
uint32_t digitalPinToPort(uint32_t pin) {
return 0;
}
uint32_t digitalPinToBitMask(uint32_t pin)
{
uint32_t digitalPinToBitMask(uint32_t 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);
}
volatile uint32_t* portInputRegister(uint32_t port)
{
volatile uint32_t* portInputRegister(uint32_t port) {
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);
}
enum PinFunction { GPIO, PWM };
static uint32_t g_gpio_function[PINCOUNT] = {
GPIO
enum PinFunction {
GPIO, PWM
};
static uint32_t g_gpio_function[PINCOUNT] = { GPIO };
extern void __pinMode(uint8_t pin, uint8_t mode)
{
if (pin == 16)
{
extern void __pinMode(uint8_t pin, uint8_t mode) {
if(pin == 16) {
uint32_t val = (mode == OUTPUT) ? 1 : 0;
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];
if (mode == INPUT)
{
if(mode == INPUT) {
gpio_output_set(0, 0, 0, 1 << pin);
PIN_PULLUP_DIS(mux);
}
else if (mode == INPUT_PULLUP)
{
} else if(mode == INPUT_PULLUP) {
gpio_output_set(0, 0, 0, 1 << pin);
PIN_PULLUP_EN(mux);
}
else if (mode == OUTPUT)
{
} else if(mode == OUTPUT) {
gpio_output_set(0, 0, 1 << pin, 0);
}
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)
);
} 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_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << pin));
}
}
extern void __digitalWrite(uint8_t pin, uint8_t val)
{
if (pin == 16)
{
extern void __digitalWrite(uint8_t pin, uint8_t val) {
if(pin == 16) {
MODIFY_PERI_REG(RTC_GPIO_OUT, 1, (val & 1));
return;
}
@ -152,74 +104,56 @@ extern void __digitalWrite(uint8_t pin, uint8_t val)
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, mask);
}
extern int __digitalRead(uint8_t pin)
{
extern int __digitalRead(uint8_t pin) {
if(pin == 16)
return (READ_PERI_REG(RTC_GPIO_IN_DATA) & 1);
else
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);
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);
for (int pin = 0; intr_mask; intr_mask >>= 1, ++pin)
{
if ((intr_mask & 1) && g_handlers[pin])
{
for(int pin = 0; intr_mask; intr_mask >>= 1, ++pin) {
if((intr_mask & 1) && g_handlers[pin]) {
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 1 << 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)
return;
g_handlers[pin] = handler;
if (mode == RISING)
{
if(mode == RISING) {
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);
}
else if (mode == CHANGE)
{
} else if(mode == CHANGE) {
gpio_pin_intr_state_set(pin, GPIO_PIN_INTR_ANYEDGE);
}
else
{
} else {
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;
gpio_pin_intr_state_set(pin, GPIO_PIN_INTR_DISABLE);
}
void initPins()
{
void initPins() {
gpio_init();
for (int i = 0; i < PINCOUNT; ++i)
{
for(int i = 0; i < PINCOUNT; ++i) {
uint32_t mux = g_pin_muxes[i];
if (mux)
{
if(mux) {
uint32_t func = g_pin_funcs[i];
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
* to 3 minutes in length, but must be called at least a few dozen microseconds
* 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;
}

View File

@ -42,8 +42,7 @@ uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
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;
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 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;
if(val)
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);
}
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_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);
}
static inline void i2c_set_scl(int scl)
{
static inline void i2c_set_scl(int 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));
}
static inline uint8_t i2c_get_scl()
{
static inline uint8_t i2c_get_scl() {
return GPIO_INPUT_GET(GPIO_ID_PIN(s_scl_pin));
}
static inline void i2c_wait()
{
static inline void i2c_wait() {
delayMicroseconds(s_i2c_delay);
}
void i2c_freq(int freq_hz)
{
void i2c_freq(int freq_hz) {
s_i2c_delay = 1000000 / freq_hz / 4 - 1;
if(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_scl_pin = scl_pin;
pinMode(ESP_PINS_OFFSET + sda_pin, OUTPUT_OPEN_DRAIN);
@ -91,15 +81,12 @@ void i2c_init(int sda_pin, int scl_pin)
i2c_wait();
}
void i2c_release()
{
void i2c_release() {
pinMode(ESP_PINS_OFFSET + s_sda_pin, INPUT);
pinMode(ESP_PINS_OFFSET + s_scl_pin, INPUT);
}
void i2c_start()
{
void i2c_start() {
i2c_set(1, 1);
i2c_wait();
i2c_set_sda(0);
@ -109,8 +96,7 @@ void i2c_start()
i2c_wait();
}
void i2c_stop()
{
void i2c_stop() {
i2c_wait();
i2c_set_scl(1);
i2c_wait();
@ -118,8 +104,7 @@ void i2c_stop()
i2c_wait();
}
void i2c_set_ack(int ack)
{
void i2c_set_ack(int ack) {
i2c_set_sda(!ack);
i2c_wait();
i2c_set_scl(1);
@ -130,8 +115,7 @@ void i2c_set_ack(int ack)
i2c_wait();
}
int i2c_get_ack()
{
int i2c_get_ack() {
i2c_set_scl(1);
i2c_wait();
int result = i2c_get_sda();
@ -142,11 +126,9 @@ int i2c_get_ack()
return result;
}
uint8_t i2c_read(void)
{
uint8_t i2c_read(void) {
uint8_t result = 0;
for (int i = 7; i >= 0; --i)
{
for(int i = 7; i >= 0; --i) {
i2c_wait();
i2c_set_scl(1);
i2c_wait();
@ -157,11 +139,8 @@ uint8_t i2c_read(void)
return result;
}
void i2c_write(uint8_t val)
{
for (int i = 7; i >= 0; --i)
{
void i2c_write(uint8_t val) {
for(int i = 7; i >= 0; --i) {
i2c_set_sda((val >> i) & 1);
i2c_wait();
i2c_set_scl(1);
@ -172,14 +151,12 @@ void i2c_write(uint8_t val)
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_write(address << 1 | 1);
int ack = i2c_get_ack();
uint8_t* end = data + size;
for (;data != end; ++data )
{
for(; data != end; ++data) {
i2c_set_sda(1);
pinMode(ESP_PINS_OFFSET + s_sda_pin, INPUT);
*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;
}
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_write(address << 1);
int ack = i2c_get_ack();
const uint8_t* end = data + size;
for (;data != end; ++data )
{
for(; data != end; ++data) {
i2c_write(*data);
ack = i2c_get_ack();
}
@ -211,38 +186,37 @@ 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
extern "C"
{
void twi_init(void) { }
extern "C" {
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);
}
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);
}
uint8_t twi_transmit(const uint8_t* data, uint8_t length)
{
uint8_t twi_transmit(const uint8_t* data, uint8_t length) {
//TODO implement twi_transmit
return 0;
}
void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) ) { }
void twi_attachSlaveTxEvent( void (*)(void) ) { }
void twi_reply(uint8_t) { }
void twi_stop(void)
{
void twi_attachSlaveRxEvent(void (*)(uint8_t*, int)) {
}
void twi_attachSlaveTxEvent(void (*)(void)) {
}
void twi_reply(uint8_t) {
}
void twi_stop(void) {
i2c_stop();
}
void twi_releaseBus(void)
{
void twi_releaseBus(void) {
i2c_set(1, 1);
}
}

View File

@ -34,7 +34,6 @@ int i2c_get_ack();
uint8_t i2c_read(void);
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_write_to(int address, const uint8_t* data, size_t size, bool sendStop);

View File

@ -25,6 +25,10 @@
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
#include "ets_sys.h"
#include "os_type.h"
@ -32,7 +36,6 @@
#include "mem.h"
#include "user_interface.h"
void* malloc(size_t size) {
return os_malloc(size);
}
@ -45,6 +48,10 @@ void* realloc(void* ptr, size_t size) {
return os_realloc(ptr, size);
}
int puts(const char * str) {
return os_printf("%s", str);
}
int printf(const char* format, ...) {
int ret;
va_list arglist;
@ -194,6 +201,126 @@ char *strtok_r(char * str, const char * delimiters, char ** temp) {
return ret;
}
int strcasecmp(const char * str1, const char * str2) {
int d = 0;
while(1) {
int c1 = tolower(*str1++);
int c2 = tolower(*str2++);
if(((d = c1 - c2) != 0) || (c2 == '\0')) {
break;
}
}
return d;
}
char * strdup(const char *str) {
size_t len = strlen(str) + 1;
char *cstr = malloc(len);
if(cstr) {
memcpy(cstr, str, len);
}
return cstr;
}
long int ICACHE_FLASH_ATTR strtol(const char* str, char** endptr, int base) {
long int result = 0;
int sign = 1;
while(isspace(*str)) {
str++;
}
if(*str == 0x00) {
// only space in str?
*endptr = (char*) str;
return result;
}
switch(base) {
case 10:
if(*str == '-') {
sign = -1;
str++;
} else if(*str == '+') {
str++;
}
for(uint8_t i = 0; *str; i++, str++) {
int x = *str - '0';
if(x < 0 || x > 9) {
break;
}
result = result * 10 + x;
}
break;
case 2:
for(uint8_t i = 0; *str; i++, str++) {
int x = *str - '0';
if(x < 0 || x > 1) {
break;
}
result = result * 2 + x;
}
break;
case 16:
default:
os_printf("fnk: strtol() only supports base 10 and 2 ATM!\n");
break;
}
*endptr = (char*) str;
return sign * result;
}
// based on Source:
// https://github.com/anakod/Sming/blob/master/Sming/system/stringconversion.cpp#L93
double ICACHE_FLASH_ATTR strtod(const char* str, char** endptr) {
double result = 0.0;
double factor = 1.0;
bool decimals = false;
char c;
while(isspace(*str)) {
str++;
}
if(*str == 0x00) {
// only space in str?
*endptr = (char*) str;
return result;
}
if(*str == '-') {
factor = -1;
str++;
} else if(*str == '+') {
str++;
}
while((c = *str)) {
if(c == '.') {
decimals = true;
str++;
continue;
}
int d = c - '0';
if(d < 0 || d > 9) {
break;
}
result = 10.0 * result + d;
if(decimals) {
factor *= 0.1;
}
str++;
}
*endptr = (char*) str;
return result * factor;
}
// ##########################################################################
// ctype functions
// ##########################################################################
@ -225,6 +352,7 @@ int isdigit(int c) {
}
return 0;
}
int isgraph(int c) {
if(isprint(c) && c != ' ') {
return 1;
@ -311,3 +439,78 @@ int isblank(int c) {
// ##########################################################################
static int errno_var = 0;
int * __errno(void) {
os_printf("__errno is called last error: %d (not current)\n", errno_var);
return &errno_var;
}
// ##########################################################################
// __ieee754 functions
// ##########################################################################
double __ieee754_sinh(double x) {
return sinh(x);
}
double __ieee754_hypot(double x, double y) {
return hypot(x, y);
}
float __ieee754_hypotf(float x, float y) {
return hypotf(x, y);
}
float __ieee754_logf(float x) {
return logf(x);
}
double __ieee754_log10(double x) {
return log10(x);
}
double __ieee754_exp(double x) {
return exp(x);
}
double __ieee754_cosh(double x) {
return cosh(x);
}
float __ieee754_expf(float x) {
return expf(x);
}
float __ieee754_log10f(float x) {
return log10f(x);
}
double __ieee754_atan2(double x, double y) {
return atan2(x, y);
}
float __ieee754_sqrtf(float x) {
return sqrtf(x);
}
float __ieee754_sinhf(float x) {
return sinhf(x);
}
double __ieee754_log(double x) {
return log(x);
}
double __ieee754_sqrt(double x) {
return sqrt(x);
}
float __ieee754_coshf(float x) {
return coshf(x);
}
float __ieee754_atan2f(float x, float y) {
return atan2f(x, y);
}

View File

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

View File

@ -157,7 +157,6 @@ int ICACHE_FLASH_ATTR WiFiClient::available()
int ICACHE_FLASH_ATTR WiFiClient::read()
{
uint8_t b;
if (!available())
return -1;

View File

@ -54,6 +54,7 @@ WiFiUDP& WiFiUDP::operator=(const WiFiUDP& rhs)
_ctx = rhs._ctx;
if (_ctx)
_ctx->ref();
return *this;
}
WiFiUDP::~WiFiUDP()

View File

@ -29,21 +29,10 @@ typedef void(*discard_cb_t)(void*, ClientContext*);
extern "C" void esp_yield();
extern "C" void esp_schedule();
class ClientContext
{
class ClientContext {
public:
ClientContext(tcp_pcb* pcb,
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)
{
ClientContext(tcp_pcb* pcb, 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) {
tcp_setprio(pcb, TCP_PRIO_MIN);
tcp_arg(pcb, this);
tcp_recv(pcb, &_s_recv);
@ -51,34 +40,29 @@ public:
tcp_err(pcb, &_s_error);
}
~ClientContext()
{
~ClientContext() {
}
ClientContext* next() const
{
ClientContext* next() const {
return _next;
}
ClientContext* next(ClientContext* new_next)
{
ClientContext* next(ClientContext* new_next) {
_next = new_next;
return _next;
}
void ref()
{
void ref() {
++_refcnt;
DEBUGV(":ref %d\r\n", _refcnt);
}
void unref()
{
void unref() {
err_t err;
DEBUGV(":ur %d\r\n", _refcnt);
if (--_refcnt == 0)
{
if (_pcb)
{
if(--_refcnt == 0) {
flush();
if(_pcb) {
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
@ -94,52 +78,41 @@ public:
}
}
uint32_t getRemoteAddress()
{
if (!_pcb)
return 0;
uint32_t getRemoteAddress() {
if(!_pcb) return 0;
return _pcb->remote_ip.addr;
}
uint16_t getRemotePort()
{
if (!_pcb)
return 0;
uint16_t getRemotePort() {
if(!_pcb) return 0;
return _pcb->remote_port;
}
size_t getSize() const
{
if (!_rx_buf)
return 0;
size_t getSize() const {
if(!_rx_buf) return 0;
return _rx_buf->tot_len - _rx_buf_offset;
}
char read()
{
if (!_rx_buf)
return 0;
char read() {
if(!_rx_buf) return 0;
char c = reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset];
_consume(1);
return c;
}
size_t read(char* dst, size_t size)
{
if (!_rx_buf)
return 0;
size_t read(char* dst, size_t size) {
if(!_rx_buf) return 0;
size_t max_size = _rx_buf->tot_len - _rx_buf_offset;
size = (size < max_size) ? size : max_size;
DEBUGV(":rd %d, %d, %d\r\n", size, _rx_buf->tot_len, _rx_buf_offset);
size_t size_read = 0;
while(size)
{
while(size) {
size_t buf_size = _rx_buf->len - _rx_buf_offset;
size_t copy_size = (size < buf_size) ? size : buf_size;
DEBUGV(":rdi %d, %d\r\n", buf_size, copy_size);
@ -152,44 +125,47 @@ public:
return size_read;
}
char peek()
{
if (!_rx_buf)
return 0;
char peek() {
if(!_rx_buf) return 0;
return reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset];
}
void flush()
{
if (!_rx_buf)
void flush() {
if(!_rx_buf) {
return;
size_t len = _rx_buf->tot_len;
tcp_recved(_pcb, len);
}
if(_pcb) {
tcp_recved(_pcb, (size_t) _rx_buf->tot_len);
}
pbuf_free(_rx_buf);
_rx_buf = 0;
_rx_buf_offset = 0;
}
uint8_t state() const
{
if (!_pcb)
return CLOSED;
uint8_t state() const {
if(!_pcb) return CLOSED;
return _pcb->state;
}
size_t write(const char* data, size_t size)
{
if (!_pcb)
size_t write(const char* data, size_t size) {
if(!_pcb) {
DEBUGV(":wr !_pcb\r\n");
return 0;
}
if(size == 0) {
return 0;
}
size_t room = tcp_sndbuf(_pcb);
size_t will_send = (room < size) ? room : size;
err_t err = tcp_write(_pcb, data, will_send, 0);
if (err != ERR_OK)
if(err != ERR_OK) {
DEBUGV(":wr !ERR_OK\r\n");
return 0;
}
_size_sent = will_send;
DEBUGV(":wr\r\n");
@ -200,40 +176,30 @@ public:
return will_send - _size_sent;
}
private:
void _consume(size_t size)
{
void _consume(size_t size) {
ptrdiff_t left = _rx_buf->len - _rx_buf_offset - size;
if (left > 0)
{
if(left > 0) {
_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);
if (_pcb)
tcp_recved(_pcb, _rx_buf->len);
if(_pcb) tcp_recved(_pcb, _rx_buf->len);
pbuf_free(_rx_buf);
_rx_buf = 0;
_rx_buf_offset = 0;
}
else
{
} else {
DEBUGV(":c %d, %d, %d\r\n", size, _rx_buf->len, _rx_buf->tot_len);
auto head = _rx_buf;
_rx_buf = _rx_buf->next;
_rx_buf_offset = 0;
pbuf_ref(_rx_buf);
if (_pcb)
tcp_recved(_pcb, head->len);
if(_pcb) tcp_recved(_pcb, head->len);
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
{
@ -254,15 +220,12 @@ private:
return ERR_OK;
}
if (_rx_buf)
{
if(_rx_buf) {
// there is some unread data
// chain the new pbuf to the existing one
DEBUGV(":rch %d, %d\r\n", _rx_buf->tot_len, pb->tot_len);
pbuf_cat(_rx_buf, pb);
}
else
{
} else {
DEBUGV(":rn %d\r\n", pb->tot_len);
_rx_buf = pb;
_rx_buf_offset = 0;
@ -272,47 +235,51 @@ private:
return ERR_OK;
}
void _error(err_t err)
{
void _error(err_t err) {
DEBUGV(":er %d\r\n", err);
_pcb = 0;
if (_size_sent && _send_waiting)
esp_schedule();
if(_pcb) {
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
err = tcp_close(_pcb);
if(err != ERR_OK) {
DEBUGV(":tc err %d\r\n", err);
tcp_abort(_pcb);
}
}
_pcb = 0;
if(_size_sent && _send_waiting) {
esp_schedule();
}
}
err_t _poll(tcp_pcb* pcb)
{
err_t _poll(tcp_pcb* pcb) {
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);
_size_sent -= len;
if (_size_sent == 0 && _send_waiting)
esp_schedule();
if(_size_sent == 0 && _send_waiting) esp_schedule();
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);
}
static void _s_error(void *arg, err_t err)
{
static void _s_error(void *arg, err_t 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);
}
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);
}
@ -332,6 +299,4 @@ private:
bool _send_waiting;
};
#endif//CLIENTCONTEXT_H