Files

153 lines
5.5 KiB
C++

/**
* Minimal emulator shim — provides only the subset of Arduino API needed
* for graphics/engine prototypes running on the SDL3 emulator.
*
* Include this single header in your sketch:
* #include "shim.h"
*
* The template's main.cpp includes this file and sets up the emulated state.
* This header defines: millis(), random(), randomSeed(), String, and basic
* type definitions. No GPIO, no interrupts, no peripheral dependencies.
*/
#pragma once
#include <cstdint>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <string>
#include <deque>
#include <thread> // for std::this_thread::sleep_for
#include <chrono> // for std::chrono::milliseconds, microseconds
// ---------------------------------------------------------------------------
// Platform marker: the sketch can #ifdef EMULATOR for emulator-only code
// ---------------------------------------------------------------------------
#define EMULATOR 1
// ---------------------------------------------------------------------------
// Flash-resident data macros (no-op on emulator; flash-mapped memory)
// ---------------------------------------------------------------------------
#define PROGMEM
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#define pgm_read_word(addr) (*(const unsigned short *)(addr))
#define pgm_read_dword(addr) (*(const unsigned long *)(addr))
// ---------------------------------------------------------------------------
// Time
// ---------------------------------------------------------------------------
inline unsigned long millis()
{
using namespace std::chrono;
static const auto start = steady_clock::now();
return (unsigned long)duration_cast<milliseconds>(steady_clock::now() - start).count();
}
inline unsigned long micros()
{
using namespace std::chrono;
static const auto start = steady_clock::now();
return (unsigned long)duration_cast<microseconds>(steady_clock::now() - start).count();
}
inline void delay(unsigned long ms)
{
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
inline void delayMicroseconds(unsigned int us)
{
std::this_thread::sleep_for(std::chrono::microseconds(us));
}
// ---------------------------------------------------------------------------
// Random (Arduino-style)
// ---------------------------------------------------------------------------
inline long random(long maxv) { return maxv > 0 ? std::rand() % maxv : 0; }
inline long random(long lo, long hi) { return hi > lo ? lo + std::rand() % (hi - lo) : lo; }
inline void randomSeed(unsigned long seed) { std::srand((unsigned)seed); }
// Emulated esp_random (used by some sketches)
inline uint32_t esp_random()
{
return ((uint32_t)std::rand() << 17) ^ (uint32_t)std::rand() ^ millis();
}
// ---------------------------------------------------------------------------
// Minimal String class (Arduino-style)
// ---------------------------------------------------------------------------
class String {
public:
std::string s;
String() {}
String(const char *c) : s(c) {}
String(const std::string &x) : s(x) {}
String(char c) : s(1, c) {}
String(int v) : s(std::to_string(v)) {}
String(long v) : s(std::to_string(v)) {}
String(unsigned v) : s(std::to_string(v)) {}
String(unsigned long v) : s(std::to_string(v)) {}
String(float v) : s(std::to_string(v)) {}
const char *c_str() const { return s.c_str(); }
unsigned length() const { return (unsigned)s.length(); }
};
inline String operator+(const String &a, const String &b) { return String(a.s + b.s); }
inline String operator+(const String &a, const char *b) { return String(a.s + b); }
inline String operator+(const char *a, const String &b) { return String(std::string(a) + b.s); }
// -------------------------------------------------------------------------//
// Minimal GPIO — emulated pin levels (idle HIGH). Access via
// emu::pinLevels in the main.cpp; this shim provides a safe read API.
// For the template's purposes, users should use the emu::keyboard queue
// for input instead of raw GPIO reads.
// -------------------------------------------------------------------------//
#define INPUT 0
#define OUTPUT 1
#define INPUT_PULLUP 2
#define HIGH 1
#define LOW 0
inline void pinMode(int, int) {} // no-op in emulator
inline int digitalRead(int pin)
{
(void)pin;
return 1; // idle HIGH
}
inline void digitalWrite(int, int) {}
// Pin edge structure — used by the emulator's ISR dispatch logic.
struct PinEdge { int pin, from, to; };
// -------------------------------------------------------------------------//
// Serial simulation (minimal)
// -------------------------------------------------------------------------//
struct SerialSim {
std::deque<std::string> *sink = nullptr;
void capture(const char *s)
{
if (!sink) return;
sink->push_back(s);
while (sink->size() > 512) sink->pop_front();
}
template <typename... A> void printf(const char *fmt, A... a)
{
char buf[512];
std::snprintf(buf, sizeof(buf), fmt, a...);
std::fputs(buf, stdout);
capture(buf);
}
void println() { std::printf("\n"); capture("\n"); }
void println(const char *s) { std::printf("%s\n", s); capture(s); capture("\n"); }
void println(const String &s) { std::printf("%s\n", s.c_str()); capture(s.c_str()); capture("\n"); }
void println(int v) { std::printf("%d\n", v); capture(std::to_string(v).c_str()); capture("\n"); }
};
// External instance — the emulator main.cpp defines this.
extern SerialSim Serial;