edgetx icon indicating copy to clipboard operation
edgetx copied to clipboard

PoC: SDL simu and WASM target

Open raphaelcoeffic opened this issue 10 months ago • 7 comments

Description

This PR rewrites simu based only SDL2 and ImGui. Additionally, it adds a WASM target based on emscripten, thus allowing to run simu in a browser.

Features so far:

  • dynamic controls for switches and pots (based on radio configuration)
  • keyboard controls (see handleKeyEvents())
  • touch controls with mouse/trackpad input

TODOs:

  • generic:
    • [ ] handle empty storage better: currently, starting simu with empty storage leads to corrupted control layout.
    • [ ] better application layout.
  • WASM:
    • [ ] Storage API support: so far there is local storage supported at all, so that simu always starts with empty storage.
Screenshot Screenshot 2025-02-23 at 08 12 48

Requirements

The WASM target requires emscripten to be installed. I'm using the version installed by brew, which is fairly new:

brew install emscripten

Usage

simu can be used as usual:

make -j16 simu && ./native/simu

The WASM target requires cmake to be run with -DENABLE_WASM=y. For example:

cmake -DPCB=X10 -DPCBREV=TX16S -DENABLE_WASM=y ..

It is then compiled and run with:

make -j16 wasm-simu && emrun --no-browser wasm/simu.html

raphaelcoeffic avatar Feb 23 '25 07:02 raphaelcoeffic

I don't know if my testing this on openSUSE Leap 15.6 is of any value to you, but I have been compiling openTX/edgeTX since 2013 and everything has always worked. In compiling and testing simu (will need to install Emscripten to test wasm) I get the following:

Compile error using G++ 11:

[ 93%] Building CXX object radio/src/targets/simu/CMakeFiles/simu_drivers.dir/simuaudio.cpp.o
/home/jim/edgetx/edgetx/radio/src/targets/simu/simuaudio.cpp: In function ‘bool simuAudioInit()’:
/home/jim/edgetx/edgetx/radio/src/targets/simu/simuaudio.cpp:83:3: sorry, unimplemented: non-trivial designated initializers not supported
   };
   ^
make[3]: **

According to https://stackoverflow.com/questions/31215971/non-trivial-designated-initializers-not-supported With g++ the order of initialization needs to be in the exact order of declaration. So changing simuaudio.cpp to the following allows it to compile.

diff --git a/radio/src/targets/simu/simuaudio.cpp b/radio/src/targets/simu/simuaudio.cpp
index b22b30adf..0d8ca0b03 100644
--- a/radio/src/targets/simu/simuaudio.cpp
+++ b/radio/src/targets/simu/simuaudio.cpp
@@ -79,6 +79,7 @@ bool simuAudioInit()
     .freq = AUDIO_SAMPLE_RATE,
     .format = SIMU_AUDIO_FMT,
     .channels = 1,
+    .silence = NULL,
     .samples = 1024,
   };

After that change it compiles, but when running the simu comes up (see screen shot) with the current model but the audio is very noisy and scratchy and the program hangs and has to be killed (see screen shot running in debuger). dbScreenshot

jtaylor2 avatar Feb 23 '25 15:02 jtaylor2

Compiled WASM on openSUSE 15.6. Had to use emsdk 4.0.2 as Leap 15.6 has python 3.6 and emsdk main requires python 3.8.

Compiled and seems to work fine but I can't figure out how to point it to my SD card image. It comes up with a Storage Warning and initializes to a new radio. Don't know how to make it see my SD image. wasm

jtaylor2 avatar Feb 23 '25 18:02 jtaylor2

Filesystem access is not yet possible.

gagarinlg avatar Feb 23 '25 20:02 gagarinlg

Of course it's not.....says that in the TODOs. I need to learn to read.

jtaylor2 avatar Feb 23 '25 22:02 jtaylor2

@jtaylor2 thx for trying it out! I've just fixed the audio, which was somehow still using unsigned 16 bit, instead of signed 16 bit, as we do now across all targets. It seems I forgot that while rebasing the code on main.

raphaelcoeffic avatar Feb 24 '25 08:02 raphaelcoeffic

Simu audio good now but I have to comment out radio/src/targets/simu/sdl_simu.cpp:706 to get it to actually run. Otherwise it just comes up and hangs in the delay loop:

//SDL_Delay(floor(16.666f - elapsedMS));

jtaylor2 avatar Feb 24 '25 13:02 jtaylor2

I believe this is obsoleted by #6233 and #6435.

raphaelcoeffic avatar Jul 13 '25 12:07 raphaelcoeffic