/* * Copyright 2019 Simon Arlott * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef ARDUINO_H_ #define ARDUINO_H_ #include #include #include #include #include #include // for count_if #define IPAddress std::string #define ICACHE_FLASH_ATTR #define ICACHE_RAM_ATTR #define os_event_t void #define byte uint8_t #define ltoa itoa #define LOW 0 #define HIGH 1 #define NAN 0 #define INPUT 0 #define OUTPUT 1 #define INPUT_PULLUP 2 void pinMode(uint8_t pin, uint8_t mode); void digitalWrite(uint8_t pin, uint8_t value); int digitalRead(uint8_t pin); #define PROGMEM #define PGM_P const char * #define PSTR(s) \ (__extension__({ \ static const char __c[] = (s); \ &__c[0]; \ })) class __FlashStringHelper; #define FPSTR(string_literal) (reinterpret_cast(string_literal)) #define F(string_literal) (FPSTR(PSTR(string_literal))) #define strlen_P strlen #define strncpy_P strncpy #define strcmp_P strcmp int snprintf_P(char * str, size_t size, const char * format, ...); int vsnprintf_P(char * str, size_t size, const char * format, va_list ap); #define pgm_read_byte(addr) (*reinterpret_cast(addr)) class Print; class Printable { public: virtual size_t printTo(Print & print) const = 0; }; class Print { public: virtual size_t write(uint8_t c) = 0; virtual size_t write(const uint8_t * buffer, size_t size) = 0; size_t print(char c) { return write((uint8_t)c); } size_t print(const char * data) { return write(reinterpret_cast(data), strlen(data)); } size_t print(const __FlashStringHelper * data) { return print(reinterpret_cast(data)); } size_t print(const Printable & printable) { return printable.printTo(*this); } size_t print(int value) { return print(std::to_string(value).c_str()); } size_t print(unsigned int value) { return print(std::to_string(value).c_str()); } size_t print(long value) { return print(std::to_string(value).c_str()); } size_t print(unsigned long value) { return print(std::to_string(value).c_str()); } size_t println() { return print("\r\n"); } size_t println(const char * data) { return print(data) + println(); } size_t println(const __FlashStringHelper * data) { return print(reinterpret_cast(data)) + println(); } size_t println(const Printable & printable) { return printable.printTo(*this) + println(); } size_t println(int value) { return print(std::to_string(value).c_str()) + println(); } size_t println(unsigned int value) { return print(std::to_string(value).c_str()) + println(); } size_t println(long value) { return print(std::to_string(value).c_str()) + println(); } size_t println(unsigned long value) { return print(std::to_string(value).c_str()) + println(); } virtual void flush(){}; }; class Stream : public Print { public: virtual int available() = 0; virtual int read() = 0; virtual int peek() = 0; }; class String { public: String(const char * data = "") : data_(data) { } long toInt() const { return std::stol(data_); } const char * c_str() const { return data_.c_str(); } private: std::string data_; }; class NativeConsole : public Stream { public: void begin(unsigned long baud __attribute__((unused))) { } int available() override { if (peek() >= 0) { return 1; } else { return 0; } } int read() override { peek(); if (peek_) { peek_ = false; return peek_data_; } else { return -1; } } int peek() override { if (!peek_) { int ret = ::read(STDIN_FILENO, &peek_data_, 1); peek_ = ret > 0; } if (peek_) { return peek_data_; } else { return -1; } } size_t write(uint8_t c) override { return ::write(STDOUT_FILENO, &c, 1); } size_t write(const uint8_t * buffer, size_t size) override { return ::write(STDOUT_FILENO, buffer, size); } private: bool peek_ = false; unsigned char peek_data_; }; extern NativeConsole Serial; unsigned long millis(); void delay(unsigned long millis); void yield(void); void setup(void); void loop(void); size_t strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize); size_t strlcat(char * dst, const char * src, size_t siz); #endif