arduino-emulator
Loading...
Searching...
No Matches
Esp.h
1/*
2 Esp.h
3 Copyright (c) 2026 Phil Schatzmann. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14*/
15#pragma once
16#include <stdint.h>
17#include <stdlib.h>
18
19#if defined(_WIN32)
20# include <windows.h>
21#elif defined(__APPLE__)
22# include <sys/sysctl.h>
23# include <unistd.h>
24#else
25# include <unistd.h>
26#endif
27
28namespace arduino {
29
35class EspClass {
36 public:
38 uint32_t getHeapSize() { return clamp(physicalMemory(false)); }
40 uint32_t getFreeHeap() { return clamp(physicalMemory(true)); }
41 uint32_t getMinFreeHeap() { return getFreeHeap(); }
42 uint32_t getMaxAllocHeap() { return getFreeHeap(); }
43 uint32_t getPsramSize() { return 0; }
44 uint32_t getFreePsram() { return 0; }
45 uint32_t getMinFreePsram() { return 0; }
46 uint32_t getMaxAllocPsram() { return 0; }
47 uint32_t getCpuFreqMHz() { return 1000; }
48 const char* getChipModel() { return "Desktop"; }
49 uint8_t getChipRevision() { return 0; }
50 uint8_t getChipCores() {
51#if defined(_WIN32)
52 SYSTEM_INFO info;
53 GetSystemInfo(&info);
54 return static_cast<uint8_t>(info.dwNumberOfProcessors);
55#else
56 long n = sysconf(_SC_NPROCESSORS_ONLN);
57 return n > 0 ? static_cast<uint8_t>(n) : 1;
58#endif
59 }
60 const char* getSdkVersion() { return "arduino-emulator"; }
61 uint32_t getFlashChipSize() { return 0; }
62 uint64_t getEfuseMac() { return 0; }
63 void restart() { exit(0); }
64
65 protected:
66 static uint32_t clamp(uint64_t v) { return v > 0xFFFFFFFFull ? 0xFFFFFFFFu : static_cast<uint32_t>(v); }
67
68 static uint64_t physicalMemory(bool available) {
69#if defined(_WIN32)
70 MEMORYSTATUSEX status;
71 status.dwLength = sizeof(status);
72 if (!GlobalMemoryStatusEx(&status)) return 0;
73 return available ? status.ullAvailPhys : status.ullTotalPhys;
74#elif defined(__APPLE__)
75 int64_t size = 0;
76 size_t len = sizeof(size);
77 if (sysctlbyname("hw.memsize", &size, &len, nullptr, 0) != 0) return 0;
78 return static_cast<uint64_t>(size); // no simple "available" on macOS
79#else
80 long pages = sysconf(available ? _SC_AVPHYS_PAGES : _SC_PHYS_PAGES);
81 long page_size = sysconf(_SC_PAGESIZE);
82 if (pages < 0 || page_size < 0) return 0;
83 return static_cast<uint64_t>(pages) * static_cast<uint64_t>(page_size);
84#endif
85 }
86};
87
88inline EspClass ESP;
89
91inline uint32_t getCpuFrequencyMhz() { return ESP.getCpuFreqMHz(); }
92
93// ESP32 (ESP-IDF) deep sleep and GPIO hold: on the desktop the deep sleep
94// ends the program and the GPIO hold does nothing
95typedef int esp_err_t;
96#ifndef ESP_OK
97# define ESP_OK 0
98#endif
99typedef int gpio_num_t;
100
101inline esp_err_t gpio_hold_en(gpio_num_t pin) { return ESP_OK; }
102inline esp_err_t gpio_hold_dis(gpio_num_t pin) { return ESP_OK; }
103inline void gpio_deep_sleep_hold_en() {}
104inline void gpio_deep_sleep_hold_dis() {}
105
107[[noreturn]] inline void esp_deep_sleep_start() { exit(0); }
108
109} // namespace arduino
ESP32 compatible ESP object (ESP.getFreeHeap() ...), so that ESP32 sketches also run on the desktop....
Definition Esp.h:35
uint32_t getFreeHeap()
Available physical memory of the computer.
Definition Esp.h:40
uint32_t getHeapSize()
Physical memory of the computer.
Definition Esp.h:38
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31
uint32_t getCpuFrequencyMhz()
ESP32: cpu frequency in MHz.
Definition Esp.h:91
void esp_deep_sleep_start()
Deep sleep without wake-up source: ends the program.
Definition Esp.h:107