|
| 1 | +#pragma once |
| 2 | +#include <stdint.h> |
| 3 | +#include <chrono> |
| 4 | +#include <cstring> |
| 5 | +#include <cstdio> |
| 6 | +#include <cmath> |
| 7 | +#include <string> |
| 8 | +#include <thread> |
| 9 | + |
| 10 | +// Basic Arduino types and helpers for native screenshot tests |
| 11 | +using std::chrono::steady_clock; |
| 12 | +using std::chrono::milliseconds; |
| 13 | + |
| 14 | +class String { |
| 15 | + public: |
| 16 | + String() = default; |
| 17 | + String(const char* s) : data_(s ? s : "") {} |
| 18 | + bool concat(char c) { data_ += c; return true; } |
| 19 | + bool concat(const char* s) { data_ += s ? s : ""; return true; } |
| 20 | + const char* c_str() const { return data_.c_str(); } |
| 21 | + size_t length() const { return data_.size(); } |
| 22 | + private: |
| 23 | + std::string data_; |
| 24 | +}; |
| 25 | + |
| 26 | +struct __FlashStringHelper; |
| 27 | + |
| 28 | +class Print { |
| 29 | + public: |
| 30 | + virtual ~Print() = default; |
| 31 | + virtual size_t write(uint8_t) { return 1; } |
| 32 | + virtual size_t write(const uint8_t* buffer, size_t size) { (void)buffer; return size; } |
| 33 | + size_t write(const char* s) { return write(reinterpret_cast<const uint8_t*>(s), s ? strlen(s) : 0); } |
| 34 | + template <typename... Args> void printf(const char*, Args...) {} |
| 35 | + template <typename T> void print(const T&) {} |
| 36 | + template <typename T> void println(const T&) {} |
| 37 | + void println() {} |
| 38 | +}; |
| 39 | + |
| 40 | +class Printable { |
| 41 | + public: |
| 42 | + virtual ~Printable() = default; |
| 43 | + virtual size_t printTo(Print&) const { return 0; } |
| 44 | +}; |
| 45 | + |
| 46 | +class Stream : public Print { |
| 47 | + public: |
| 48 | + virtual int available() { return 0; } |
| 49 | + virtual int read() { return -1; } |
| 50 | + virtual size_t readBytes(char* buffer, size_t length) { (void)buffer; (void)length; return 0; } |
| 51 | +}; |
| 52 | + |
| 53 | +struct DummySerial : public Stream { |
| 54 | + inline void begin(unsigned long) {} |
| 55 | +}; |
| 56 | + |
| 57 | +static DummySerial USBSerial; |
| 58 | +static DummySerial Serial; |
| 59 | + |
| 60 | +#ifndef PROGMEM |
| 61 | +#define PROGMEM |
| 62 | +#endif |
| 63 | + |
| 64 | +#ifndef pgm_read_byte |
| 65 | +inline uint8_t pgm_read_byte(const void* p) { return *reinterpret_cast<const uint8_t*>(p); } |
| 66 | +#endif |
| 67 | + |
| 68 | +inline unsigned long millis() { |
| 69 | + static auto start = steady_clock::now(); |
| 70 | + return (unsigned long)std::chrono::duration_cast<milliseconds>(steady_clock::now() - start).count(); |
| 71 | +} |
| 72 | + |
| 73 | +inline void delay(unsigned long ms) { |
| 74 | + std::this_thread::sleep_for(std::chrono::milliseconds(ms)); |
| 75 | +} |
| 76 | + |
| 77 | +typedef uint16_t word; |
| 78 | + |
| 79 | +// FreeRTOS type stubs |
| 80 | +typedef void* QueueHandle_t; |
| 81 | +typedef void* TaskHandle_t; |
| 82 | + |
| 83 | +// GPIO stubs |
| 84 | +#ifndef INPUT |
| 85 | +#define INPUT 0 |
| 86 | +#endif |
| 87 | +#ifndef OUTPUT |
| 88 | +#define OUTPUT 1 |
| 89 | +#endif |
| 90 | +#ifndef LOW |
| 91 | +#define LOW 0 |
| 92 | +#endif |
| 93 | +#ifndef HIGH |
| 94 | +#define HIGH 1 |
| 95 | +#endif |
| 96 | +#ifndef F |
| 97 | +#define F(x) x |
| 98 | +#endif |
| 99 | + |
| 100 | +inline void pinMode(int, int) {} |
| 101 | +inline void digitalWrite(int, int) {} |
| 102 | +inline int digitalRead(int) { return 0; } |
| 103 | +inline int analogRead(int) { return 0; } |
| 104 | +inline void analogReadResolution(int) {} |
| 105 | + |
| 106 | +// Math helpers |
| 107 | +#ifndef constrain |
| 108 | +template <typename T> |
| 109 | +inline T constrain(T x, T a, T b) { return x < a ? a : (x > b ? b : x); } |
| 110 | +#endif |
| 111 | + |
| 112 | +#ifndef min |
| 113 | +template <typename T> |
| 114 | +inline T min(T a, T b) { return a < b ? a : b; } |
| 115 | +#endif |
| 116 | + |
| 117 | +#ifndef max |
| 118 | +template <typename T> |
| 119 | +inline T max(T a, T b) { return a > b ? a : b; } |
| 120 | +#endif |
| 121 | + |
| 122 | +inline long map(long x, long in_min, long in_max, long out_min, long out_max) { |
| 123 | + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
| 124 | +} |
| 125 | + |
| 126 | +// LEDC stubs (ESP32) |
| 127 | +inline void ledcAttach(int, int, int) {} |
| 128 | +inline void ledcWrite(int, int) {} |
| 129 | +inline void ledcDetach(int) {} |
0 commit comments