Files
MeshCore-Solo/variants/sim/SimSensorManager.h
T
JakubandClaude Sonnet 5 97a86216c6 feat(sim): board feature parity (reset/GPS/sensors/admin/keyboard) + input fixes
Rounds out the browser sim harness with the rest of the physical board's
interactions: a reset button (JS-driven, since board.reboot() is inert
under -sEXIT_RUNTIME=0), GPS input wired into a real LocationProvider via
new SimSensorManager, JS-settable battery/environment telemetry, an
admin/repeater-login test hook (sendRoomLogin against the default
"password"), and full physical-keyboard text entry (printable ASCII
passthrough into the existing KeyboardWidget, Tab->KEY_KB_ENTER submit).

Also fixes three real bugs found while exercising all of this in a real
browser:
- UITask.cpp's native-only stdin poll branch had no __EMSCRIPTEN__
  exclusion, so it also compiled into the wasm build and called a real,
  blocking window.prompt() on nearly every frame -- the actual cause of
  the reported time/controls jumping. Now gated to native only.
- 'n'/'p' were mapped as Next/Prev keyboard shortcuts, colliding with
  typing those literal letters. Removed the shortcuts; added explicit
  Next/Prev buttons to mesh.html (previously relied solely on them).
- Buttons grabbed native browser keyboard focus on click, so a later
  stray Enter/Space could silently re-trigger a previously-clicked button
  (e.g. Reset). mousedown now calls preventDefault() on all buttons.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-03 11:31:42 +02:00

56 lines
2.7 KiB
C++

#pragma once
#include <helpers/SensorManager.h>
// SensorManager subclass wiring the sim's already-existing
// SimLocationProvider (SimLocationProvider.h) into the real
// getLocationProvider() hook -- the base SensorManager (previously used
// as-is in target.cpp) always returns NULL there (see
// SimLocationProvider.h's own comment on this exact gap), so on-device
// screens that check "do I have a GPS fix" (CompassScreen.h,
// NearbyScreen.h) never saw a fix even after sim_location_set() was
// called, even though the *advertised* position (sensors.node_lat/lon)
// already worked. Also adds one representative JS-settable environment
// telemetry channel (temperature) to prove the querySensors() mechanism --
// same channel-numbering convention as the real
// src/helpers/sensors/EnvironmentSensorManager.cpp: GPS on
// TELEM_CHANNEL_SELF, each other active sensor on the next channel.
//
// getLocationProvider()/querySensors() are declared here but DEFINED in
// target.cpp, not inline -- they need SimLocationProvider.h's complete
// type (for the LocationProvider* upcast, and to call
// sim_location_provider()), and SimLocationProvider.h in turn needs THIS
// class's complete type for its own `extern SimSensorManager sensors;`
// declaration (see that file's comment) -- pulling SimLocationProvider.h
// in here too would make the two headers mutually dependent on each
// other's complete type with no valid include order. target.cpp already
// includes target.h, which includes both in the one order that works
// (this file first, then SimLocationProvider.h), so it's the natural
// place for the bodies that need both.
//
// Deliberately doesn't fake any of the ~15 real I2C sensor chip drivers
// (BME280/INA219/etc, EnvironmentSensorManager.cpp) -- those are
// hardware-specific and out of scope; one JS-settable channel is enough to
// prove the mechanism and is trivially extended later if a specific sensor
// type turns out to matter for a demo.
class SimSensorManager : public SensorManager {
static float& envTemperatureRef() { static float t = 21.0f; return t; }
public:
LocationProvider* getLocationProvider() override;
bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) override;
static void setEnvTemperature(float celsius) { envTemperatureRef() = celsius; }
};
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
// C linkage so JS's ccall('sim_env_temperature_set', ...) can find it by
// its exact, unmangled name -- same pattern as SimLocationProvider.h's
// sim_location_set() / SimMainBoard.h's sim_battery_set_mv(). Doesn't touch
// SimLocationProvider.h, so it's safe to define inline here.
extern "C" inline EMSCRIPTEN_KEEPALIVE void sim_env_temperature_set(float celsius) {
SimSensorManager::setEnvTemperature(celsius);
}
#endif