diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8fb556f --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +# Root directory of EpoxyFS +epoxyfsdata + +# Root file of EpoxyEepromAvr and EpoxyEepromEsp +epoxyeepromdata diff --git a/EspMock.mk b/EspMock.mk deleted file mode 100644 index 19d74b9..0000000 --- a/EspMock.mk +++ /dev/null @@ -1,22 +0,0 @@ -# vim: set noexpandtab -espmock_path := $(abspath $(lastword $(MAKEFILE_LIST))) -espmock_dir := $(dir $(espmock_path)) - -ESP_MODEL = esp8266 -ESPMOCK_LIBDIRS = $(espmock_dir)/libraries/ - -ARDUINO_LIB_DIRS = $(espmock_dir)/libraries/ - -ESPMOCK_INCLUDES=$(foreach esplib,$(ESP_LIBS), -I$(ESPMOCK_LIBDIRS)/${esplib}/src) - -CPPFLAGS = $(ESPMOCK_INCLUDES) -I$(espmock_dir)/src -I$(espmock_dir)/src/cores/$(ESP_MODEL) - -include ../../../EpoxyDuino/EpoxyDuino.mk - -truc: - @echo "TRAC=$(TRAC)" - @echo "ESP_LIBS=$(ESP_LIBS)" - @echo "ESPMOCK_INCLUDES=$(ESPMOCK_INCLUDES)" - @echo "espmock_dir=$(espmock_dir)" - @echo "$(ARDUINO_LIB_DIRS)" - diff --git a/README.md b/README.md index 4b5d7e1..aae4199 100644 --- a/README.md +++ b/README.md @@ -14,28 +14,48 @@ for any other project that needs mocks for ESP8266 and ESP32 libraries. ## Installation -This project *cannot* be installed through the Arduino Library Manager because -it is compatible only with EpoxyDuino. You need to run the `git clone` command -manually. It is probably best to install at the same location as a normal -Arduino library. In other words, locate the `./libraries/` directory of your -sketchbook directory used by the Arduino IDE: +You must install both of the following projects: + +* https://github.com/bxparks/EpoxyDuino +* https://github.com/hsaturn/EspMock + +These projects *cannot* be installed through the Arduino Library Manager because +they are not normal Arduino libraries. It is probably most convenient to install +them in the same location as other Arduino library directory. In other words, +locate the `./libraries/` directory of your sketchbook directory used by the +Arduino IDE, and run the following commands: ```bash -$ cd {sketchbook_location}/libraries +$ cd {SketchBookDirectory}/libraries +$ git clone https://github.com/bxparks/EpoxyDuino $ git clone https://github.com/hsaturn/EspMock ``` -Then add `EspMock` to the `ARDUINO_LIBS` variable of the EpoxyDuino `Makefile` -located in directory of the Arduino sketch that you want to compile, like this: +## Usage + +For each Arduino program, we need to create a `Makefile` +as described in [EpoxyDuino](https://github.com/bxparks/EpoxyDuino), but with a +few extra parameters: + +* Add the `EPOXY_CORE_PATH` variable (new for EpoxyDuino v0.7), pointing to the + `{EspMock}/cores/esp8266/` directory. +* Add `EspMock` to the `ARDUINO_LIBS` variable. +* Add a `ARDUINO_LIB_DIRS` variable that points to + `{EspMockDirectory}/libraries` directory to pickup additional ESP8266 or ESP32 + mock libraries provided by EspMock. + +The result is a `Makefile` that looks like this: ``` -APP_NAME := network-tests +APP_NAME := {NameOfSketch} ARDUINO_LIBS := {Lib1} {Lib2} ... EspMock -include ../../../EpoxyDuino/EpoxyDuino.mk +ARDUINO_LIB_DIRS := {EspMockDirectory}/libraries +EPOXY_CORE_PATH := {EspMockDirectory}/cores/esp8266 +include {EpoxyDuinoDirectory}/EpoxyDuino.mk ``` -See [EpoxyDuino](https://github.com/bxparks/EpoxyDuino) for information on how -to configure the `Makefile`. +See [tests/network-tests/Makefile](tests/network-tests/Makefile) for a concrete +example. ## License diff --git a/cores/esp8266/Arduino.cpp b/cores/esp8266/Arduino.cpp new file mode 100644 index 0000000..0057a68 --- /dev/null +++ b/cores/esp8266/Arduino.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2019 Brian T. Park + * + * Parts derived from the Arduino SDK + * Copyright (c) 2005-2013 Arduino Team + * + * Parts inspired by [Entering raw + * mode](https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html). + * + * Parts inspired by [ESP8266 Host + * Emulation](https://github.com/esp8266/Arduino/tree/master/tests/host). + * + */ + +#include +#include // usleep() +#include // clock_gettime() +#include "Arduino.h" + +// ----------------------------------------------------------------------- +// Arduino methods emulated in Unix +// ----------------------------------------------------------------------- + +void yield() { + usleep(1000); // prevents program from consuming 100% CPU +} + +void pinMode(uint8_t /*pin*/, uint8_t /*mode*/) {} + +void digitalWrite(uint8_t /*pin*/, uint8_t /*val*/) {} + +int digitalRead(uint8_t /*pin*/) { return 0; } + +int analogRead(uint8_t /*pin*/) { return 0; } + +void analogWrite(uint8_t /*pin*/, int /*val*/) {} + +unsigned long millis() { + struct timespec spec; + clock_gettime(CLOCK_MONOTONIC, &spec); + unsigned long ms = spec.tv_sec * 1000U + spec.tv_nsec / 1000000UL; + return ms; +} + +unsigned long micros() { + struct timespec spec; + clock_gettime(CLOCK_MONOTONIC, &spec); + unsigned long us = spec.tv_sec * 1000000UL + spec.tv_nsec / 1000U; + return us; +} + +void delay(unsigned long ms) { + usleep(ms * 1000); +} + +void delayMicroseconds(unsigned int us) { + usleep(us); +} + +unsigned long pulseIn( + uint8_t /*pin*/, uint8_t /*state*/, unsigned long /*timeout*/) { + return 0; +} + +unsigned long pulseInLong( + uint8_t /*pin*/, uint8_t /*state*/, unsigned long /*timeout*/) { + return 0; +} + +void shiftOut(uint8_t /*dataPin*/, uint8_t /*clockPin*/, uint8_t /*bitOrder*/, + uint8_t /*val*/) {} + +uint8_t shiftIn( + uint8_t /*dataPin*/, uint8_t /*clockPin*/, uint8_t /*bitOrder*/) { + return 0; +} diff --git a/src/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h similarity index 100% rename from src/cores/esp8266/Arduino.h rename to cores/esp8266/Arduino.h diff --git a/src/cores/esp8266/Client.h b/cores/esp8266/Client.h similarity index 100% rename from src/cores/esp8266/Client.h rename to cores/esp8266/Client.h diff --git a/src/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp similarity index 100% rename from src/cores/esp8266/Esp.cpp rename to cores/esp8266/Esp.cpp diff --git a/src/cores/esp8266/Esp.h b/cores/esp8266/Esp.h similarity index 100% rename from src/cores/esp8266/Esp.h rename to cores/esp8266/Esp.h diff --git a/cores/esp8266/IPAddress.cpp b/cores/esp8266/IPAddress.cpp new file mode 100644 index 0000000..a7df382 --- /dev/null +++ b/cores/esp8266/IPAddress.cpp @@ -0,0 +1,116 @@ +/* + IPAddress.cpp - Base class that provides IPAddress + Copyright (c) 2011 Adrian McEwen. All right reserved. + + Copied from Arduino AVR core 1.8.3 by Erik Tideman. Removed + unnecessary 'friend' declarations. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include +#include + +IPAddress::IPAddress() +{ + _address.dword = 0; +} + +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) +{ + _address.dword = address; +} + +IPAddress::IPAddress(const uint8_t *address) +{ + memcpy(_address.bytes, address, sizeof(_address.bytes)); +} + +bool IPAddress::fromString(const char *address) +{ + uint16_t acc = 0; // Accumulator + uint8_t dots = 0; + + while (*address) + { + char c = *address++; + if (c >= '0' && c <= '9') + { + acc = acc * 10 + (c - '0'); + if (acc > 255) { + // Value out of [0..255] range + return false; + } + } + else if (c == '.') + { + if (dots == 3) { + // Too much dots (there must be 3 dots) + return false; + } + _address.bytes[dots++] = acc; + acc = 0; + } + else + { + // Invalid char + return false; + } + } + + if (dots != 3) { + // Too few dots (there must be 3 dots) + return false; + } + _address.bytes[3] = acc; + return true; +} + +IPAddress& IPAddress::operator=(const uint8_t *address) +{ + memcpy(_address.bytes, address, sizeof(_address.bytes)); + return *this; +} + +IPAddress& IPAddress::operator=(uint32_t address) +{ + _address.dword = address; + return *this; +} + +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 n = 0; + for (int i =0; i < 3; i++) + { + n += p.print(_address.bytes[i], DEC); + n += p.print('.'); + } + n += p.print(_address.bytes[3], DEC); + return n; +} diff --git a/cores/esp8266/IPAddress.h b/cores/esp8266/IPAddress.h new file mode 100644 index 0000000..2c0c1b6 --- /dev/null +++ b/cores/esp8266/IPAddress.h @@ -0,0 +1,74 @@ +/* + IPAddress.h - Base class that provides IPAddress + Copyright (c) 2011 Adrian McEwen. All right reserved. + + Copied from Arduino AVR core 1.8.3 by Erik Tideman. Removed + unnecessary 'friend' declarations. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef IPAddress_h +#define IPAddress_h + +#include +#include "Printable.h" +#include "WString.h" + +// A class to make it easier to handle and pass around IP addresses + +class IPAddress : public Printable { +private: + union { + uint8_t bytes[4]; // IPv4 address + uint32_t dword; + } _address; + + // Access the raw byte array containing the address. Because this returns a pointer + // 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; }; + +public: + // Constructors + IPAddress(); + IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); + IPAddress(uint32_t address); + IPAddress(const uint8_t *address); + + bool fromString(const char *address); + bool fromString(const String &address) { return fromString(address.c_str()); } + + // 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; }; + 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]; }; + + // Overloaded copy operators to allow initialisation of IPAddress objects from other types + IPAddress& operator=(const uint8_t *address); + IPAddress& operator=(uint32_t address); + + virtual size_t printTo(Print& p) const; +}; + +const IPAddress INADDR_NONE(0,0,0,0); + +#endif diff --git a/cores/esp8266/Print.cpp b/cores/esp8266/Print.cpp new file mode 100644 index 0000000..ffd3040 --- /dev/null +++ b/cores/esp8266/Print.cpp @@ -0,0 +1,285 @@ +/* + Print.cpp - Base class that provides print() and println() + Copyright (c) 2008 David A. Mellis. All right reserved. + Modified by Brian T. Park 2019. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 23 November 2006 by David A. Mellis + Modified 03 August 2015 by Chuck Todd + Modified 2018 by Brian T. Park +*/ + +#include +#include // vsnprintf +#include // va_list, va_start() +#include // isnan(), isinf() +#include "pgmspace.h" +#include "Print.h" + +// Size of the internal printf() buffer +#define PRINTF_BUFFER_SIZE 250 + +// Public Methods ////////////////////////////////////////////////////////////// + +/* default implementation: may be overridden */ +size_t Print::write(const uint8_t *buffer, size_t size) +{ + size_t n = 0; + while (size--) { + if (write(*buffer++)) n++; + else break; + } + return n; +} + +size_t Print::print(const __FlashStringHelper *ifsh) +{ + PGM_P p = reinterpret_cast(ifsh); + size_t n = 0; + while (1) { + unsigned char c = pgm_read_byte(p++); + if (c == 0) break; + if (write(c)) n++; + else break; + } + return n; +} + +size_t Print::print(const String &s) +{ + return write(s.c_str(), s.length()); +} + +size_t Print::print(const char str[]) +{ + return write(str); +} + +size_t Print::print(char c) +{ + return write(c); +} + +size_t Print::print(unsigned char b, int base) +{ + return print((unsigned long) b, base); +} + +size_t Print::print(int n, int base) +{ + return print((long) n, base); +} + +size_t Print::print(unsigned int n, int base) +{ + return print((unsigned long) n, base); +} + +size_t Print::print(long n, int base) +{ + if (base == 0) { + return write(n); + } else if (base == 10) { + if (n < 0) { + int t = print('-'); + n = -n; + return printNumber(n, 10) + t; + } + return printNumber(n, 10); + } else { + return printNumber(n, base); + } +} + +size_t Print::print(unsigned long n, int base) +{ + if (base == 0) return write(n); + else return printNumber(n, base); +} + +size_t Print::print(double n, int digits) +{ + return printFloat(n, digits); +} + +size_t Print::println(const __FlashStringHelper *ifsh) +{ + size_t n = print(ifsh); + n += println(); + return n; +} + +size_t Print::print(const Printable& x) +{ + return x.printTo(*this); +} + +size_t Print::println(void) +{ + return write("\r\n"); +} + +size_t Print::println(const String &s) +{ + size_t n = print(s); + n += println(); + return n; +} + +size_t Print::println(const char c[]) +{ + size_t n = print(c); + n += println(); + return n; +} + +size_t Print::println(char c) +{ + size_t n = print(c); + n += println(); + return n; +} + +size_t Print::println(unsigned char b, int base) +{ + size_t n = print(b, base); + n += println(); + return n; +} + +size_t Print::println(int num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(unsigned int num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(long num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(unsigned long num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(double num, int digits) +{ + size_t n = print(num, digits); + n += println(); + return n; +} + +size_t Print::println(const Printable& x) +{ + size_t n = print(x); + n += println(); + return n; +} + +size_t Print::printf(const char* fmt, ...) { + char buf[PRINTF_BUFFER_SIZE]; + va_list args; + va_start(args, fmt); + int status = vsnprintf(buf, PRINTF_BUFFER_SIZE, fmt, args); + va_end(args); + if (status >= 0) { + buf[PRINTF_BUFFER_SIZE - 1] = '\0'; + size_t n = print(buf); + return n; + } else { + return 0; + } +} + +// Private Methods ///////////////////////////////////////////////////////////// + +size_t Print::printNumber(unsigned long n, uint8_t base) +{ + char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. + char *str = &buf[sizeof(buf) - 1]; + + *str = '\0'; + + // prevent crash if called with base == 1 + if (base < 2) base = 10; + + do { + char c = n % base; + n /= base; + + *--str = c < 10 ? c + '0' : c + 'A' - 10; + } while(n); + + return write(str); +} + +size_t 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 + + // Handle negative numbers + if (number < 0.0) + { + n += print('-'); + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8_t i=0; i 0) { + n += print('.'); + } + + // Extract digits from the remainder one at a time + while (digits-- > 0) + { + remainder *= 10.0; + unsigned int toPrint = (unsigned int)(remainder); + n += print(toPrint); + remainder -= toPrint; + } + + return n; +} diff --git a/cores/esp8266/Print.h b/cores/esp8266/Print.h new file mode 100644 index 0000000..fba28cb --- /dev/null +++ b/cores/esp8266/Print.h @@ -0,0 +1,99 @@ +/* + Print.h - Base class that provides print() and println() + Copyright (c) 2008 David A. Mellis. All right reserved. + Modified by Brian T. Park 2019. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef EPOXY_DUINO_PRINT_H +#define EPOXY_DUINO_PRINT_H + +#include +#include // strlen() +#include // size_t + +#include "WString.h" +#include "Printable.h" + +#define DEC 10 +#define HEX 16 +#define OCT 8 +#ifdef BIN // Prevent warnings if BIN is previously defined in "iotnx4.h" or similar +#undef BIN +#endif +#define BIN 2 + +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; } + public: + Print() : write_error(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; + return write((const uint8_t *)str, strlen(str)); + } + virtual size_t write(const uint8_t *buffer, size_t size); + size_t write(const char *buffer, size_t size) { + return write((const uint8_t *)buffer, size); + } + + // default to zero, meaning "a single write may block" + // should be overriden by subclasses with buffering + virtual int availableForWrite() { return 0; } + + size_t print(const __FlashStringHelper *); + size_t print(const String &); + size_t print(const char[]); + size_t print(char); + size_t print(unsigned char, int = DEC); + size_t print(int, int = DEC); + size_t print(unsigned int, int = DEC); + size_t print(long, int = DEC); + size_t print(unsigned long, int = DEC); + size_t print(double, int = 2); + size_t print(const Printable&); + + size_t println(const __FlashStringHelper *); + size_t println(const String &s); + size_t println(const char[]); + size_t println(char); + size_t println(unsigned char, int = DEC); + size_t println(int, int = DEC); + size_t println(unsigned int, int = DEC); + size_t println(long, int = DEC); + size_t println(unsigned long, int = DEC); + size_t println(double, int = 2); + size_t println(const Printable&); + size_t println(void); + + // printf() extension supported by many microcontrollers including + // Teensy, ESP8266 and ESP32 (but not AVR). + size_t printf(const char* format, ...); + + virtual void flush() { /* Empty implementation for backward compatibility */ } +}; + +#endif diff --git a/cores/esp8266/Printable.h b/cores/esp8266/Printable.h new file mode 100644 index 0000000..1c5b17b --- /dev/null +++ b/cores/esp8266/Printable.h @@ -0,0 +1,41 @@ +/* + Printable.h - Interface class that allows printing of complex types + Copyright (c) 2011 Adrian McEwen. All right reserved. + Modified by Brian T. Park 2019. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef EPOXY_DUINO_PRINTABLE_H +#define EPOXY_DUINO_PRINTABLE_H + +#include + +class Print; + +/** + * The Printable class provides a way for new classes to allow themselves to be + * printed. By deriving from Printable and implementing the printTo method, it + * will then be possible for users to print out instances of this class by + * passing them into the usual Print::print and Print::println methods. +*/ +class Printable +{ + public: + virtual size_t printTo(Print& p) const = 0; +}; + +#endif + diff --git a/cores/esp8266/StdioSerial.cpp b/cores/esp8266/StdioSerial.cpp new file mode 100644 index 0000000..b1b985c --- /dev/null +++ b/cores/esp8266/StdioSerial.cpp @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2019 Brian T. Park + * MIT License + */ + +#include +#include "StdioSerial.h" + +size_t StdioSerial::write(uint8_t c) { + int result = putchar(c); + return (result == EOF) ? 0 : 1; +} + +void StdioSerial::flush() { + fflush(stdout); +} + +StdioSerial Serial; diff --git a/cores/esp8266/StdioSerial.h b/cores/esp8266/StdioSerial.h new file mode 100644 index 0000000..443637a --- /dev/null +++ b/cores/esp8266/StdioSerial.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2019 Brian T. Park + * MIT License + */ + +#ifndef EPOXY_DUINO_STDIO_SERIAL_H +#define EPOXY_DUINO_STDIO_SERIAL_H + +#include "Print.h" +#include "Stream.h" + +/** + * A version of Serial that reads from STDIN and sends output to STDOUT on + * Linux or MacOS. + */ +class StdioSerial: public Stream { + public: + void begin(unsigned long /*baud*/) { } + + size_t write(uint8_t c) override; + + void flush() override; + + operator bool() { return true; } + + int available() override { return mHead != mTail; } + + int read() override { + if (mHead == mTail) { + return -1; + } else { + char c = mBuffer[mHead]; + mHead = (mHead + 1) % kBufSize; + return c; + } + } + + int peek() override { + return (mHead != mTail) ? mBuffer[mHead] : -1; + } + + /** Insert a character into the ring buffer. */ + void insertChar(char c) { + int newTail = (mTail + 1) % kBufSize; + if (newTail == mHead) { + // Buffer full, drop the character. (Strictly speaking, there's one + // remaining slot in the buffer, but we can't use it because we need to + // distinguish between buffer-empty and buffer-full). + return; + } + mBuffer[mTail] = c; + mTail = newTail; + } + + private: + // Ring buffer size (should be a power of 2 for efficiency). + static const int kBufSize = 128; + + char mBuffer[kBufSize]; + int mHead = 0; + int mTail = 0; +}; + +extern StdioSerial Serial; + +#define SERIAL_PORT_MONITOR Serial + +#endif diff --git a/cores/esp8266/Stream.cpp b/cores/esp8266/Stream.cpp new file mode 100644 index 0000000..2ebea5e --- /dev/null +++ b/cores/esp8266/Stream.cpp @@ -0,0 +1,320 @@ +/* + Stream.cpp - adds parsing methods to Stream class + Copyright (c) 2008 David A. Mellis. All right reserved. + Modified by Brian T. Park 2019. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Created July 2011 + parsing functions based on TextFinder library by Michael Margolis + + findMulti/findUntil routines written by Jim Leonard/Xuth + */ + +#include "Arduino.h" +#include "Stream.h" + +#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait + +// protected method to read stream with timeout +int Stream::timedRead() +{ + int c; + _startMillis = millis(); + do { + c = read(); + if (c >= 0) return c; + } while(millis() - _startMillis < _timeout); + return -1; // -1 indicates timeout +} + +// protected method to peek stream with timeout +int Stream::timedPeek() +{ + int c; + _startMillis = millis(); + do { + c = peek(); + if (c >= 0) return c; + } while(millis() - _startMillis < _timeout); + return -1; // -1 indicates timeout +} + +// returns peek of the next digit in the stream or -1 if timeout +// discards non-numeric characters +int Stream::peekNextDigit(LookaheadMode lookahead, bool detectDecimal) +{ + int c; + while (1) { + c = timedPeek(); + + if( c < 0 || + c == '-' || + (c >= '0' && c <= '9') || + (detectDecimal && c == '.')) return c; + + switch( lookahead ){ + case SKIP_NONE: return -1; // Fail code. + case SKIP_WHITESPACE: + switch( c ){ + case ' ': + case '\t': + case '\r': + case '\n': break; + default: return -1; // Fail code. + } + case SKIP_ALL: + break; + } + read(); // discard non-numeric + } +} + +// Public Methods +////////////////////////////////////////////////////////////// + +void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait +{ + _timeout = timeout; +} + + // find returns true if the target string is found +bool Stream::find(char *target) +{ + return findUntil(target, strlen(target), NULL, 0); +} + +// 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 Stream::find(char *target, size_t length) +{ + return findUntil(target, length, NULL, 0); +} + +// as find but search ends if the terminator string is found +bool Stream::findUntil(char *target, 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 Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen) +{ + if (terminator == NULL) { + MultiTarget t[1] = {{target, targetLen, 0}}; + return findMulti(t, 1) == 0 ? true : false; + } else { + MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}}; + return findMulti(t, 2) == 0 ? true : false; + } +} + +// returns the first valid (long) integer value from the current position. +// lookahead determines how parseInt looks ahead in the stream. +// See LookaheadMode enumeration at the top of the file. +// Lookahead is terminated by the first character that is not a valid part of an integer. +// Once parsing commences, 'ignore' will be skipped in the stream. +long Stream::parseInt(LookaheadMode lookahead, char ignore) +{ + bool isNegative = false; + long value = 0; + int c; + + c = peekNextDigit(lookahead, false); + // ignore non numeric leading characters + if(c < 0) + return 0; // zero returned if timeout + + do{ + if(c == ignore) + ; // ignore this character + else if(c == '-') + isNegative = true; + else if(c >= '0' && c <= '9') // is c a digit? + value = value * 10 + c - '0'; + read(); // consume the character we got with peek + c = timedPeek(); + } + while( (c >= '0' && c <= '9') || c == ignore ); + + if(isNegative) + value = -value; + return value; +} + +// as parseInt but returns a floating point value +float Stream::parseFloat(LookaheadMode lookahead, char ignore) +{ + bool isNegative = false; + bool isFraction = false; + long value = 0; + int c; + float fraction = 1.0; + + c = peekNextDigit(lookahead, true); + // ignore non numeric leading characters + if(c < 0) + return 0; // zero returned if timeout + + do{ + if(c == ignore) + ; // ignore + else if(c == '-') + isNegative = true; + else if (c == '.') + isFraction = true; + else if(c >= '0' && c <= '9') { // is c a digit? + value = value * 10 + c - '0'; + if(isFraction) + fraction *= 0.1; + } + read(); // consume the character we got with peek + c = timedPeek(); + } + while( (c >= '0' && c <= '9') || (c == '.' && !isFraction) || c == ignore ); + + if(isNegative) + value = -value; + if(isFraction) + return value * fraction; + else + return value; +} + +// read characters from stream into buffer +// terminates if length characters have been read, or timeout (see setTimeout) +// returns the number of characters placed in the buffer +// the buffer is NOT null terminated. +// +size_t Stream::readBytes(char *buffer, size_t length) +{ + size_t count = 0; + while (count < length) { + int c = timedRead(); + 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 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; + *buffer++ = (char)c; + index++; + } + return index; // return number of characters, not including null terminator +} + +String Stream::readString() +{ + String ret; + int c = timedRead(); + while (c >= 0) + { + ret += (char)c; + c = timedRead(); + } + return ret; +} + +String Stream::readStringUntil(char terminator) +{ + String ret; + int c = timedRead(); + while (c >= 0 && c != terminator) + { + ret += (char)c; + c = timedRead(); + } + return ret; +} + +int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) { + // any zero length target string automatically matches and would make + // a mess of the rest of the algorithm. + for (struct MultiTarget *t = targets; t < targets+tCount; ++t) { + if (t->len <= 0) + return t - targets; + } + + while (1) { + int c = timedRead(); + if (c < 0) + return -1; + + for (struct MultiTarget *t = targets; t < targets+tCount; ++t) { + // the simple case is if we match, deal with that first. + if (c == t->str[t->index]) { + if (++t->index == t->len) + return t - targets; + else + continue; + } + + // if not we need to walk back and see if we could have matched further + // down the stream (ie '1112' doesn't match the first position in '11112' + // but it will match the second position so we can't just reset the current + // index to 0 when we find a mismatch. + if (t->index == 0) + continue; + + int origIndex = t->index; + do { + --t->index; + // first check if current char works against the new current index + if (c != t->str[t->index]) + continue; + + // if it's the only char then we're good, nothing more to check + if (t->index == 0) { + t->index++; + break; + } + + // otherwise we need to check the rest of the found string + int diff = origIndex - t->index; + size_t i; + for (i = 0; i < t->index; ++i) { + if (t->str[i] != t->str[i + diff]) + break; + } + + // if we successfully got through the previous loop then our current + // index is good. + if (i == t->index) { + t->index++; + break; + } + + // otherwise we just try the next index + } while (t->index); + } + } + // unreachable + return -1; +} diff --git a/cores/esp8266/Stream.h b/cores/esp8266/Stream.h new file mode 100644 index 0000000..f8dda1f --- /dev/null +++ b/cores/esp8266/Stream.h @@ -0,0 +1,139 @@ +/* + Stream.h - base class for character-based streams. + Copyright (c) 2010 David A. Mellis. All right reserved. + Modified by Brian T. Park 2019. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + parsing functions based on TextFinder library by Michael Margolis +*/ + +#ifndef EPOXY_DUINO_STREAM_H +#define EPOXY_DUINO_STREAM_H + +#include +#include "Print.h" + +// compatability macros for testing +/* +#define getInt() parseInt() +#define getInt(ignore) parseInt(ignore) +#define getFloat() parseFloat() +#define getFloat(ignore) parseFloat(ignore) +#define getString( pre_string, post_string, buffer, length) +readBytesBetween( pre_string, terminator, buffer, length) +*/ + +// This enumeration provides the lookahead options for parseInt(), parseFloat() +// The rules set out here are used until either the first valid character is found +// or a time out occurs due to lack of input. +enum LookaheadMode{ + SKIP_ALL, // All invalid characters are ignored. + SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid. + SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped. +}; + +#define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field + +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 + int timedRead(); // read stream with timeout + int timedPeek(); // peek stream with timeout + int peekNextDigit(LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout + + public: + virtual int available() = 0; + virtual int read() = 0; + virtual int peek() = 0; + + Stream() {_timeout=1000;} + +// parsing methods + + void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second + unsigned long getTimeout(void) { return _timeout; } + + bool find(char *target); // reads data from the stream until the target string is found + bool find(uint8_t *target) { return find ((char *)target); } + // returns true if target string is found, false if timed out (see setTimeout) + + bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found + bool find(uint8_t *target, size_t length) { return find ((char *)target, length); } + // returns true if target string is found, false if timed out + + bool find(char target) { return find (&target, 1); } + + bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found + bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); } + + bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found + bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); } + + long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); + // returns the first valid (long) integer value from the current position. + // lookahead determines how parseInt looks ahead in the stream. + // See LookaheadMode enumeration at the top of the file. + // Lookahead is terminated by the first character that is not a valid part of an integer. + // Once parsing commences, 'ignore' will be skipped in the stream. + + float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); + // float version of parseInt + + // Read chars from stream into buffer. + // 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). + virtual size_t readBytes( char *buffer, size_t length); + size_t readBytes( uint8_t *buffer, size_t length) { + return readBytes((char *)buffer, length); + } + + + // Same 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 readBytesUntil( char terminator, char *buffer, size_t length); + size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { + return readBytesUntil(terminator, (char *)buffer, length); + } + + // Arduino String functions to be added here + virtual String readString(); + String readStringUntil(char terminator); + + protected: + long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); } + float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); } + // These overload exists for compatibility with any class that has derived + // Stream and used parseFloat/Int with a custom ignore character. To keep + // the public API simple, these overload remains protected. + + struct MultiTarget { + const char *str; // string you're searching for + size_t len; // length of string you're searching for + size_t index; // index used by the search routine. + }; + + // This allows you to search for an arbitrary number of strings. + // Returns index of the target that is found first or -1 if timeout occurs. + int findMulti(struct MultiTarget *targets, int tCount); +}; + +#undef NO_IGNORE_CHAR +#endif diff --git a/cores/esp8266/WCharacter.h b/cores/esp8266/WCharacter.h new file mode 100644 index 0000000..871c6b2 --- /dev/null +++ b/cores/esp8266/WCharacter.h @@ -0,0 +1,169 @@ +/* + WCharacter.h - Character utility functions for Wiring & Arduino + Copyright (c) 2010 Hernando Barragan. All right reserved. + Copyright (c) 2021 by Erik Tideman (Replaced 'boolean' with 'bool') + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef Character_h +#define Character_h + +#include + +// WCharacter.h prototypes +inline bool isAlphaNumeric(int c) __attribute__((always_inline)); +inline bool isAlpha(int c) __attribute__((always_inline)); +inline bool isAscii(int c) __attribute__((always_inline)); +inline bool isWhitespace(int c) __attribute__((always_inline)); +inline bool isControl(int c) __attribute__((always_inline)); +inline bool isDigit(int c) __attribute__((always_inline)); +inline bool isGraph(int c) __attribute__((always_inline)); +inline bool isLowerCase(int c) __attribute__((always_inline)); +inline bool isPrintable(int c) __attribute__((always_inline)); +inline bool isPunct(int c) __attribute__((always_inline)); +inline bool isSpace(int c) __attribute__((always_inline)); +inline bool isUpperCase(int c) __attribute__((always_inline)); +inline bool isHexadecimalDigit(int c) __attribute__((always_inline)); +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 bool isAlphaNumeric(int c) +{ + return ( isalnum(c) == 0 ? false : true); +} + + +// Checks for an alphabetic character. +// It is equivalent to (isupper(c) || islower(c)). +inline bool 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 bool isAscii(int c) +{ + return ( isascii (c) == 0 ? false : true); +} + + +// Checks for a blank character, that is, a space or a tab. +inline bool isWhitespace(int c) +{ + return ( isblank (c) == 0 ? false : true); +} + + +// Checks for a control character. +inline bool isControl(int c) +{ + return ( iscntrl (c) == 0 ? false : true); +} + + +// Checks for a digit (0 through 9). +inline bool isDigit(int c) +{ + return ( isdigit (c) == 0 ? false : true); +} + + +// Checks for any printable character except space. +inline bool isGraph(int c) +{ + return ( isgraph (c) == 0 ? false : true); +} + + +// Checks for a lower-case character. +inline bool isLowerCase(int c) +{ + return (islower (c) == 0 ? false : true); +} + + +// Checks for any printable character including space. +inline bool isPrintable(int c) +{ + return ( isprint (c) == 0 ? false : true); +} + + +// Checks for any printable character which is not a space +// or an alphanumeric character. +inline bool 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 bool isSpace(int c) +{ + return ( isspace (c) == 0 ? false : true); +} + + +// Checks for an uppercase letter. +inline bool 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 bool 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) +{ + 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) +{ + return tolower (c); +} + + +// Converts the letter c to upper case, if possible. +inline int toUpperCase(int c) +{ + return toupper (c); +} + +#endif diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp new file mode 100644 index 0000000..f38e5f0 --- /dev/null +++ b/cores/esp8266/WString.cpp @@ -0,0 +1,751 @@ +/* + WString.cpp - String library for Wiring & Arduino + ...mostly rewritten by Paul Stoffregen... + Copyright (c) 2009-10 Hernando Barragan. All rights reserved. + Copyright 2011, Paul Stoffregen, paul@pjrc.com + Modified by Brian T. Park 2019. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "WString.h" + +/*********************************************/ +/* Constructors */ +/*********************************************/ + +String::String(const char *cstr) +{ + init(); + if (cstr) copy(cstr, strlen(cstr)); +} + +String::String(const String &value) +{ + init(); + *this = value; +} + +String::String(const __FlashStringHelper *pstr) +{ + init(); + *this = pstr; +} + +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) +String::String(String &&rval) +{ + init(); + move(rval); +} +String::String(StringSumHelper &&rval) +{ + init(); + move(rval); +} +#endif + +String::String(char c) +{ + init(); + char buf[2]; + buf[0] = c; + buf[1] = 0; + *this = buf; +} + +String::String(unsigned char value, unsigned char base) +{ + init(); + char buf[1 + 8 * sizeof(unsigned char)]; + utoa(value, buf, base); + *this = buf; +} + +String::String(int value, unsigned char base) +{ + init(); + char buf[2 + 8 * sizeof(int)]; + itoa(value, buf, base); + *this = buf; +} + +String::String(unsigned int value, unsigned char base) +{ + init(); + char buf[1 + 8 * sizeof(unsigned int)]; + utoa(value, buf, base); + *this = buf; +} + +String::String(long value, unsigned char base) +{ + init(); + char buf[2 + 8 * sizeof(long)]; + ltoa(value, buf, base); + *this = buf; +} + +String::String(unsigned long value, unsigned char base) +{ + init(); + char buf[1 + 8 * sizeof(unsigned long)]; + ultoa(value, buf, base); + *this = buf; +} + +String::String(float value, unsigned char decimalPlaces) +{ + init(); + char buf[33]; + *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); +} + +String::String(double value, unsigned char decimalPlaces) +{ + init(); + char buf[33]; + *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); +} + +String::~String() +{ + free(buffer); +} + +/*********************************************/ +/* Memory Management */ +/*********************************************/ + +inline void String::init(void) +{ + buffer = NULL; + capacity = 0; + len = 0; +} + +void String::invalidate(void) +{ + if (buffer) free(buffer); + buffer = NULL; + capacity = len = 0; +} + +unsigned char String::reserve(unsigned int size) +{ + if (buffer && capacity >= size) return 1; + if (changeBuffer(size)) { + if (len == 0) buffer[0] = 0; + return 1; + } + return 0; +} + +unsigned char String::changeBuffer(unsigned int maxStrLen) +{ + char *newbuffer = (char *)realloc(buffer, maxStrLen + 1); + if (newbuffer) { + buffer = newbuffer; + capacity = maxStrLen; + return 1; + } + return 0; +} + +/*********************************************/ +/* Copy and Move */ +/*********************************************/ + +String & String::copy(const char *cstr, unsigned int length) +{ + if (!reserve(length)) { + invalidate(); + return *this; + } + len = length; + strcpy(buffer, cstr); + return *this; +} + +String & String::copy(const __FlashStringHelper *pstr, unsigned int length) +{ + if (!reserve(length)) { + invalidate(); + return *this; + } + len = length; + strcpy_P(buffer, (PGM_P)pstr); + return *this; +} + +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) +void String::move(String &rhs) +{ + if (buffer) { + if (rhs && capacity >= rhs.len) { + strcpy(buffer, rhs.buffer); + len = rhs.len; + rhs.len = 0; + return; + } else { + free(buffer); + } + } + buffer = rhs.buffer; + capacity = rhs.capacity; + len = rhs.len; + rhs.buffer = NULL; + rhs.capacity = 0; + rhs.len = 0; +} +#endif + +String & String::operator = (const String &rhs) +{ + if (this == &rhs) return *this; + + if (rhs.buffer) copy(rhs.buffer, rhs.len); + else invalidate(); + + return *this; +} + +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) +String & String::operator = (String &&rval) +{ + if (this != &rval) move(rval); + return *this; +} + +String & String::operator = (StringSumHelper &&rval) +{ + if (this != &rval) move(rval); + return *this; +} +#endif + +String & String::operator = (const char *cstr) +{ + if (cstr) copy(cstr, strlen(cstr)); + else invalidate(); + + return *this; +} + +String & String::operator = (const __FlashStringHelper *pstr) +{ + if (pstr) copy(pstr, strlen_P((PGM_P)pstr)); + else invalidate(); + + return *this; +} + +/*********************************************/ +/* concat */ +/*********************************************/ + +unsigned char String::concat(const String &s) +{ + return concat(s.buffer, s.len); +} + +unsigned char 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; + strcpy(buffer + len, cstr); + len = newlen; + return 1; +} + +unsigned char String::concat(const char *cstr) +{ + if (!cstr) return 0; + return concat(cstr, strlen(cstr)); +} + +unsigned char String::concat(char c) +{ + char buf[2]; + buf[0] = c; + buf[1] = 0; + return concat(buf, 1); +} + +unsigned char String::concat(unsigned char num) +{ + char buf[1 + 3 * sizeof(unsigned char)]; + itoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(int num) +{ + char buf[2 + 3 * sizeof(int)]; + itoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(unsigned int num) +{ + char buf[1 + 3 * sizeof(unsigned int)]; + utoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(long num) +{ + char buf[2 + 3 * sizeof(long)]; + ltoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(unsigned long num) +{ + char buf[1 + 3 * sizeof(unsigned long)]; + ultoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(float num) +{ + char buf[20]; + char* string = dtostrf(num, 4, 2, buf); + return concat(string, strlen(string)); +} + +unsigned char String::concat(double num) +{ + char buf[20]; + char* string = dtostrf(num, 4, 2, buf); + return concat(string, strlen(string)); +} + +unsigned char String::concat(const __FlashStringHelper * str) +{ + if (!str) return 0; + int length = strlen_P((const char *) str); + if (length == 0) return 1; + unsigned int newlen = len + length; + if (!reserve(newlen)) return 0; + strcpy_P(buffer + len, (const char *) str); + len = newlen; + return 1; +} + +/*********************************************/ +/* Concatenate */ +/*********************************************/ + +StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(rhs.buffer, rhs.len)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr) +{ + StringSumHelper &a = const_cast(lhs); + if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, char c) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(c)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, int num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, long num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, float num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, double num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(rhs)) a.invalidate(); + return a; +} + +/*********************************************/ +/* Comparison */ +/*********************************************/ + +int 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; + return 0; + } + return strcmp(buffer, s.buffer); +} + +unsigned char String::equals(const String &s2) const +{ + return (len == s2.len && compareTo(s2) == 0); +} + +unsigned char 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 String::operator<(const String &rhs) const +{ + return compareTo(rhs) < 0; +} + +unsigned char String::operator>(const String &rhs) const +{ + return compareTo(rhs) > 0; +} + +unsigned char String::operator<=(const String &rhs) const +{ + return compareTo(rhs) <= 0; +} + +unsigned char String::operator>=(const String &rhs) const +{ + return compareTo(rhs) >= 0; +} + +unsigned char 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; + } + return 1; +} + +unsigned char String::startsWith( const String &s2 ) const +{ + if (len < s2.len) return 0; + return startsWith(s2, 0); +} + +unsigned char 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 String::endsWith( const String &s2 ) const +{ + if ( len < s2.len || !buffer || !s2.buffer) return 0; + return strcmp(&buffer[len - s2.len], s2.buffer) == 0; +} + +/*********************************************/ +/* Character Access */ +/*********************************************/ + +char String::charAt(unsigned int loc) const +{ + return operator[](loc); +} + +void String::setCharAt(unsigned int loc, char c) +{ + if (loc < len) buffer[loc] = c; +} + +char & String::operator[](unsigned int index) +{ + static char dummy_writable_char; + if (index >= len || !buffer) { + dummy_writable_char = 0; + return dummy_writable_char; + } + return buffer[index]; +} + +char String::operator[]( unsigned int index ) const +{ + if (index >= len || !buffer) return 0; + return buffer[index]; +} + +void 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; + strncpy((char *)buf, buffer + index, n); + buf[n] = 0; +} + +/*********************************************/ +/* Search */ +/*********************************************/ + +int String::indexOf(char c) const +{ + return indexOf(c, 0); +} + +int 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; + return temp - buffer; +} + +int String::indexOf(const String &s2) const +{ + return indexOf(s2, 0); +} + +int 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; + return found - buffer; +} + +int String::lastIndexOf( char theChar ) const +{ + return lastIndexOf(theChar, len - 1); +} + +int 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; + return temp - buffer; +} + +int String::lastIndexOf(const String &s2) const +{ + return lastIndexOf(s2, len - s2.len); +} + +int 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; + } + return found; +} + +String 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; + char temp = buffer[right]; // save the replaced character + buffer[right] = '\0'; + out = buffer + left; // pointer arithmetic + buffer[right] = temp; //restore character + return out; +} + +/*********************************************/ +/* Modification */ +/*********************************************/ + +void String::replace(char find, char replace) +{ + if (!buffer) return; + for (char *p = buffer; *p; p++) { + if (*p == find) *p = replace; + } +} + +void 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; + if (diff == 0) { + while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { + memcpy(foundAt, replace.buffer, replace.len); + readFrom = foundAt + replace.len; + } + } else if (diff < 0) { + char *writeTo = buffer; + while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { + unsigned int n = foundAt - readFrom; + memcpy(writeTo, readFrom, n); + writeTo += n; + memcpy(writeTo, replace.buffer, replace.len); + writeTo += replace.len; + readFrom = foundAt + find.len; + len += diff; + } + strcpy(writeTo, readFrom); + } else { + unsigned int size = len; // compute size needed for result + while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { + readFrom = foundAt + find.len; + size += diff; + } + 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; + memmove(readFrom + diff, readFrom, len - (readFrom - buffer)); + len += diff; + buffer[len] = 0; + memcpy(buffer + index, replace.buffer, replace.len); + index--; + } + } +} + +void String::remove(unsigned int index){ + // Pass the biggest integer as the count. The remove method + // below will take care of truncating it at the end of the + // string. + remove(index, (unsigned int)-1); +} + +void String::remove(unsigned int index, unsigned int count){ + 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 String::toLowerCase(void) +{ + if (!buffer) return; + for (char *p = buffer; *p; p++) { + *p = tolower(*p); + } +} + +void String::toUpperCase(void) +{ + if (!buffer) return; + for (char *p = buffer; *p; p++) { + *p = toupper(*p); + } +} + +void String::trim(void) +{ + if (!buffer || len == 0) return; + char *begin = buffer; + while (isspace(*begin)) begin++; + char *end = buffer + len - 1; + while (isspace(*end) && end >= begin) end--; + len = end + 1 - begin; + if (begin > buffer) memcpy(buffer, begin, len); + buffer[len] = 0; +} + +/*********************************************/ +/* Parsing / Conversion */ +/*********************************************/ + +long String::toInt(void) const +{ + if (buffer) return atol(buffer); + return 0; +} + +float String::toFloat(void) const +{ + return float(toDouble()); +} + +double String::toDouble(void) const +{ + if (buffer) return atof(buffer); + return 0; +} diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h new file mode 100644 index 0000000..20d5ab6 --- /dev/null +++ b/cores/esp8266/WString.h @@ -0,0 +1,228 @@ +/* + WString.h - String library for Wiring & Arduino + ...mostly rewritten by Paul Stoffregen... + Copyright (c) 2009-10 Hernando Barragan. All right reserved. + Copyright 2011, Paul Stoffregen, paul@pjrc.com + Modified 2019, Brian T. Park + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef EPOXY_DUINO_STRING_H +#define EPOXY_DUINO_STRING_H +#ifdef __cplusplus + +#include +#include +#include +#include "pgmspace.h" +#include "avr_stdlib.h" + +// Macros for creating and using c-strings in PROGMEM. +// FPSTR() is defined for ESP8266 and ESP32 Cores, but not AVR or SAMD Cores. +class __FlashStringHelper; +#define FPSTR(p) (reinterpret_cast(p)) +#define F(s) FPSTR(PSTR(s)) + +// An inherited class for holding the result of a concatenation. These +// result objects are assumed to be writable by subsequent concatenations. +class StringSumHelper; + +// The string class +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 {} + +public: + // constructors + // creates a copy of the initial value. + // if the initial value is null or invalid, or if memory allocation + // fails, the string will be marked as invalid (i.e. "if (s)" will + // be false). + String(const char *cstr = ""); + String(const String &str); + String(const __FlashStringHelper *str); + #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) + String(String &&rval); + String(StringSumHelper &&rval); + #endif + explicit String(char c); + explicit String(unsigned char, unsigned char base=10); + explicit String(int, unsigned char base=10); + explicit String(unsigned int, unsigned char base=10); + explicit String(long, unsigned char base=10); + explicit String(unsigned long, unsigned char base=10); + explicit String(float, unsigned char decimalPlaces=2); + explicit String(double, unsigned char decimalPlaces=2); + ~String(void); + + // memory management + // return true on success, false on failure (in which case, the string + // 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;} + + // creates a copy of the assigned value. if the value is null or + // invalid, or if the memory allocation fails, the string will be + // marked as invalid ("if (s)" will be false). + String & operator = (const String &rhs); + String & operator = (const char *cstr); + String & operator = (const __FlashStringHelper *str); + #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) + String & operator = (String &&rval); + String & operator = (StringSumHelper &&rval); + #endif + + // concatenate (works w/ built-in types) + + // returns true on success, false on failure (in which case, the string + // is left unchanged). if the argument is null or invalid, the + // concatenation is considered unsucessful. + unsigned char concat(const String &str); + unsigned char concat(const char *cstr); + unsigned char concat(char c); + unsigned char concat(unsigned char c); + unsigned char concat(int num); + unsigned char concat(unsigned int num); + unsigned char concat(long num); + unsigned char concat(unsigned long num); + unsigned char concat(float num); + unsigned char concat(double num); + unsigned char concat(const __FlashStringHelper * str); + + // 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 __FlashStringHelper *str){concat(str); return (*this);} + + friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); + friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); + friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, float num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, double num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs); + + // comparison (only works w/ Strings and "strings") + 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; + unsigned char operator > (const String &rhs) const; + unsigned char operator <= (const String &rhs) const; + unsigned char operator >= (const String &rhs) const; + unsigned char equalsIgnoreCase(const String &s) const; + unsigned char startsWith( const String &prefix) const; + unsigned char startsWith(const String &prefix, unsigned int offset) const; + unsigned char endsWith(const String &suffix) const; + + // character acccess + char charAt(unsigned int index) const; + void setCharAt(unsigned int index, char c); + 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; } + char* begin() { return buffer; } + char* end() { return buffer + length(); } + const char* begin() const { return c_str(); } + const char* end() const { return c_str() + length(); } + + // search + int indexOf( char ch ) const; + int indexOf( char ch, unsigned int fromIndex ) const; + int indexOf( const String &str ) const; + int indexOf( const String &str, unsigned int fromIndex ) const; + int lastIndexOf( char ch ) const; + 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, unsigned int endIndex ) const; + + // modification + void replace(char find, char replace); + void replace(const String& find, const String& replace); + void remove(unsigned int index); + void remove(unsigned int index, unsigned int count); + void toLowerCase(void); + void toUpperCase(void); + void trim(void); + + // parsing/conversion + long toInt(void) const; + float toFloat(void) const; + double toDouble(void) const; + +protected: + char *buffer; // the actual char array + unsigned int capacity; // the array length minus one (for the '\0') + unsigned int len; // the String length (not counting the '\0') +protected: + void init(void); + void invalidate(void); + unsigned char changeBuffer(unsigned int maxStrLen); + unsigned char concat(const char *cstr, unsigned int length); + + // copy and move + String & copy(const char *cstr, unsigned int length); + String & copy(const __FlashStringHelper *pstr, unsigned int length); + #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) + void move(String &rhs); + #endif +}; + +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) {} +}; + +#endif // __cplusplus +#endif // EPOXY_DUINO_STRING_H diff --git a/cores/esp8266/avr_stdlib.cpp b/cores/esp8266/avr_stdlib.cpp new file mode 100644 index 0000000..9f9d8d6 --- /dev/null +++ b/cores/esp8266/avr_stdlib.cpp @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2019 Brian T. Park + * MIT License + */ + +#include +#include +#include +#include "avr_stdlib.h" + +#define BUFSIZE (sizeof(int) * 8 + 1) + +// Copied and modified from https://people.cs.umu.se/isak/snippets/ltoa.c +// Copyright 1988-90 by Robert B. Stout dba MicroFirm +// Released to public domain, 1991. +char *itoa(int n, char *str, int base) { + if (36 < base || 2 > base) { + base = 10; /* can only use 0-9, A-Z */ + } + + char buf[BUFSIZE]; + char *tail = &buf[BUFSIZE - 1]; /* last character position */ + *tail-- = '\0'; + + char *head = str; + unsigned uarg; + if (10 == base && n < 0) { + *head++ = '-'; + uarg = -n; + } else { + uarg = n; + } + + unsigned i = 2; + if (uarg) { + for (i = 1; uarg; ++i) { + div_t r = div(uarg, base); + *tail-- = (char)(r.rem + ((9 < r.rem) ? ('A' - 10) : '0')); + uarg = r.quot; + } + } else { + *tail-- = '0'; + } + + memcpy(head, ++tail, i); + return str; +} + +// Copied and modified from https://people.cs.umu.se/isak/snippets/ltoa.c +// Copyright 1988-90 by Robert B. Stout dba MicroFirm +// Released to public domain, 1991. +char *utoa(unsigned n, char *str, int base) { + if (36 < base || 2 > base) { + base = 10; /* can only use 0-9, A-Z */ + } + + char buf[BUFSIZE]; + char *tail = &buf[BUFSIZE - 1]; /* last character position */ + *tail-- = '\0'; + + unsigned i = 2; + if (n) { + for (i = 1; n; ++i) { + div_t r = div(n, base); + *tail-- = (char)(r.rem + ((9 < r.rem) ? ('A' - 10) : '0')); + n = r.quot; + } + } else { + *tail-- = '0'; + } + + memcpy(str, ++tail, i); + return str; +} + +// Copied and modified from https://people.cs.umu.se/isak/snippets/ltoa.c +// Copyright 1988-90 by Robert B. Stout dba MicroFirm +// Released to public domain, 1991. +char *ltoa(long n, char *str, int base) { + if (36 < base || 2 > base) { + base = 10; /* can only use 0-9, A-Z */ + } + + char buf[BUFSIZE]; + char *tail = &buf[BUFSIZE - 1]; /* last character position */ + *tail-- = '\0'; + + char *head = str; + unsigned long uarg; + if (10 == base && n < 0) { + *head++ = '-'; + uarg = -n; + } else { + uarg = n; + } + + unsigned i = 2; + if (uarg) { + for (i = 1; uarg; ++i) { + ldiv_t r = ldiv(uarg, base); + *tail-- = (char)(r.rem + ((9 < r.rem) ? ('A' - 10) : '0')); + uarg = r.quot; + } + } else { + *tail-- = '0'; + } + + memcpy(head, ++tail, i); + return str; +} + +// Copied and modified from https://people.cs.umu.se/isak/snippets/ltoa.c +// Copyright 1988-90 by Robert B. Stout dba MicroFirm +// Released to public domain, 1991. +char *ultoa(unsigned long n, char *str, int base) { + if (36 < base || 2 > base) { + base = 10; /* can only use 0-9, A-Z */ + } + + char buf[BUFSIZE]; + char *tail = &buf[BUFSIZE - 1]; /* last character position */ + *tail-- = '\0'; + + unsigned i = 2; + if (n) { + for (i = 1; n; ++i) { + ldiv_t r = ldiv(n, base); + *tail-- = (char)(r.rem + ((9 < r.rem) ? ('A' - 10) : '0')); + n = r.quot; + } + } else { + *tail-- = '0'; + } + + memcpy(str, ++tail, i); + return str; +} + +// This is a terrible, hacky implementation of dtostrf() but this will never be +// used in production. It is only used to allow Arduino unit tests using AUnit +// to compile under Linux and MacOS. +char *dtostrf(double val, signed char width, unsigned char prec, char *s) { + char format[13]; + char swidth[5]; + itoa(width, swidth, 10); + char sprec[5]; + utoa(prec, sprec, 10); + + strcpy(format, "%"); + strcat(format, swidth); + strcat(format, "."); + strcat(format, sprec); + strcat(format, "f"); + + sprintf(s, format, val); + return s; +} diff --git a/cores/esp8266/avr_stdlib.h b/cores/esp8266/avr_stdlib.h new file mode 100644 index 0000000..73dd372 --- /dev/null +++ b/cores/esp8266/avr_stdlib.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2019 Brian T. Park + * MIT License + */ + +/** + * @file avr_stdlib.h + * + * Non-standard functions from AVR's library which do not exist on + * Linux or MacOS. This contains only the ones required to compile + * AUnit tests on Linux or MacOS. + */ + +#ifndef EPOXY_DUINO_AVR_STDLIB_H +#define EPOXY_DUINO_AVR_STDLIB_H + +extern "C" { + +char *itoa(int n, char *str, int base); +char *utoa(unsigned n, char *str, int base); +char *ltoa(long n, char *str, int base); +char *ultoa(unsigned long n, char *str, int base); +char *dtostrf(double val, signed char width, unsigned char prec, char *s); + +} + +#endif diff --git a/cores/esp8266/main.cpp b/cores/esp8266/main.cpp new file mode 100644 index 0000000..db64a45 --- /dev/null +++ b/cores/esp8266/main.cpp @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2019 Brian T. Park + * + * Parts derived from the Arduino SDK + * Copyright (c) 2005-2013 Arduino Team + * + * Parts inspired by [Entering raw + * mode](https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html). + * + * Parts inspired by [ESP8266 Host + * Emulation](https://github.com/esp8266/Arduino/tree/master/tests/host). + * + * The 'Serial' object sends output to STDOUT, and receives input from STDIN in + * 'raw' mode. The main() loop checks the STDIN and if it finds a character, + * inserts it into the Serial buffer. + */ + +#ifdef EPOXY_DUINO + +#include "Arduino.h" +#include +#include // SIGINT +#include // exit() +#include // perror() +#include // read() +#include +#include + +// ----------------------------------------------------------------------- +// Unix compatibility. Put STDIN into raw mode and hook it into the 'Serial' +// object. Trap Ctrl-C and perform appropriate clean up. +// ----------------------------------------------------------------------- + +static struct termios orig_termios; +static int orig_stdin_flags; +static bool inRawMode = false; + +static void die(const char* s) { + perror(s); + exit(1); +} + +static void disableRawMode() { + if (!isatty(STDIN_FILENO)) return; + if (!inRawMode) return; + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) { + inRawMode = false; // prevent exit(1) from being called twice + die("disableRawMode(): tcsetattr() failure"); + } + + if (fcntl(STDIN_FILENO, F_SETFL, orig_stdin_flags) == -1) { + die("enableRawMode(): fcntl() failure"); + } +} + +static void enableRawMode() { + // If STDIN is not a real tty, simply return instead of dying so that the + // unit tests can run in a continuous integration framework, e.g. Jenkins. + if (!isatty(STDIN_FILENO)) return; + if (tcgetattr(STDIN_FILENO, &orig_termios) == -1) { + die("enableRawMode(): tcgetattr() failure"); + } + + struct termios raw = orig_termios; + // The 'Enter' key in raw mode is ^M (\r, CR). But internally, we want this + // to be ^J (\n, NL), so ICRNL and INLCR causes the ^M to become a \n. + raw.c_iflag &= ~(/*ICRNL | INLCR |*/ INPCK | ISTRIP | IXON); + // Set the output into cooked mode, to handle NL and CR properly. + // Print.println() sends CR-NL (\r\n). But some code will send just \n. The + // ONLCR translates \n into \r\n. So '\r\n' will become \r\r\n, which is just + // fine. + raw.c_oflag |= (OPOST | ONLCR); + raw.c_cflag |= (CS8); + // Enable ISIG to allow Ctrl-C to kill the program. + raw.c_lflag &= ~(/*ECHO | ISIG |*/ ICANON | IEXTEN); + raw.c_cc[VMIN] = 0; + raw.c_cc[VTIME] = 0; + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) { + die("enableRawMode(): tcsetattr() failure"); + } + + orig_stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); + if (fcntl(STDIN_FILENO, F_SETFL, orig_stdin_flags | O_NONBLOCK) == -1) { + die("enableRawMode(): fcntl() failure"); + } + + inRawMode = true; +} + +static void handleControlC(int /*sig*/) { + if (!isatty(STDIN_FILENO)) return; + if (inRawMode) { + // If this returns an error, don't call die() because it will call exit(), + // which may call this again, causing an infinite recursion. + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1) { + perror("handleControlC(): tcsetattr() failure"); + } + inRawMode = false; + } + exit(1); +} + +// ----------------------------------------------------------------------- +// Main loop. User code will provide setup() and loop(). +// ----------------------------------------------------------------------- + +extern "C" { + +int unixhostduino_main(int /*argc*/, char** /*argv*/) { + signal(SIGINT, handleControlC); + atexit(disableRawMode); + enableRawMode(); + + setup(); + while (true) { + char c = '\0'; + read(STDIN_FILENO, &c, 1); + if (c) Serial.insertChar(c); + loop(); + yield(); + } +} + +// Weak reference so that the calling code can provide its own main(). +int main(int argc, char** argv) +__attribute__((weak)); + +int main(int argc, char** argv) { + return unixhostduino_main(argc, argv); +} + +} + +#endif // #ifdef EPOXY_DUINO diff --git a/cores/esp8266/pgmspace.h b/cores/esp8266/pgmspace.h new file mode 100644 index 0000000..7dc881d --- /dev/null +++ b/cores/esp8266/pgmspace.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 Brian T. Park + * MIT License + */ + +/** + * @file pgmspace.h + * + * A version of the file in the Arduino environment with + * enough mappings to allow AUnit tests to compile and run on Linux or MacOS. + */ + +#ifndef EPOXY_DUINO_PGMSPACE_H +#define EPOXY_DUINO_PGMSPACE_H + +#include + +#define PGM_P const char * +#define PGM_VOID_P const void * +#define PSTR(str) (str) + +#define PROGMEM + +#define pgm_read_byte(p) (* (const uint8_t*) (p)) +#define pgm_read_word(p) (* (const uint16_t*) (p)) +#define pgm_read_dword(p) (* (const uint32_t*) (p)) +#define pgm_read_float(p) (* (const float*) (p)) +#define pgm_read_ptr(p) (* (const void* const*) (p)) + +#define strlen_P strlen +#define strcat_P strcat +#define strcpy_P strcpy +#define strncpy_P strncpy +#define strcmp_P strcmp +#define strncmp_P strncmp +#define strcasecmp_P strcasecmp +#define strchr_P strchr +#define strrchr_P strrchr +#define memcpy_P memcpy +#define vsnprintf_P vsnprintf + +#endif diff --git a/src/pins_arduino.h b/cores/esp8266/pins_arduino.h similarity index 100% rename from src/pins_arduino.h rename to cores/esp8266/pins_arduino.h diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index b0632df..71b5708 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -1,5 +1,6 @@ -// vim: ts=2 sw=2 -#pragma once -#include -#include +#ifndef ESP_MOCK_ESP8266_WIFI_H +#define ESP_MOCK_ESP8266_WIFI_H +#include "WiFiClient.h" + +#endif diff --git a/src/WProgram.h b/src/WProgram.h deleted file mode 100644 index da23d31..0000000 --- a/src/WProgram.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -#include "Esp.h" -#include diff --git a/tests/network-tests/Makefile b/tests/network-tests/Makefile index d2a37ef..e2db8da 100644 --- a/tests/network-tests/Makefile +++ b/tests/network-tests/Makefile @@ -2,6 +2,7 @@ # Makefile to compile and run Arduino programs natively on Linux or MacOS. APP_NAME := network-tests -ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock -ESP_LIBS = ESP8266WiFi -include ../../EspMock.mk +ARDUINO_LIBS := AUnit TinyMqtt EspMock ESP8266WiFi +ARDUINO_LIB_DIRS := ../../libraries +EPOXY_CORE_PATH := ../../cores/esp8266 +include ../../../EpoxyDuino/EpoxyDuino.mk