feat: WASM modules based simulation in Companion (#6435)
This commit is contained in:
@@ -22,6 +22,7 @@ set(${PROJECT_NAME}_NAMES
|
||||
serialportsdialog
|
||||
hostserialconnector
|
||||
simulateduiwidgetGeneric
|
||||
wasmsimulatorinterface
|
||||
)
|
||||
|
||||
if(TARGET SDL2::SDL2)
|
||||
@@ -54,10 +55,13 @@ add_library(${PROJECT_NAME}
|
||||
${${PROJECT_NAME}_SRCS}
|
||||
)
|
||||
|
||||
include(FetchWamr)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
${CPN_COMMON_LIB}
|
||||
Qt::Svg
|
||||
vmlib
|
||||
)
|
||||
|
||||
if(TARGET SDL2::SDL2)
|
||||
|
||||
@@ -118,21 +118,9 @@ class RadioUiAction : public QObject
|
||||
// Note: Qt::KeypadModifier is required for arrow keys as they are considered part of the keypad
|
||||
if (((keyEvent->modifiers() == Qt::NoModifier) || (keyEvent->modifiers() & Qt::ShiftModifier) || (keyEvent->modifiers() & Qt::KeypadModifier)) &&
|
||||
m_keys.contains(keyEvent->key())) {
|
||||
// qDebug() << "Event:" << event->type() << "Key:" << QString("0x%1").arg(keyEvent->key(), 8, 16, QLatin1Char( '0' )) << "Text:" << keyEvent->text() << "found";
|
||||
trigger(event->type() == QEvent::KeyPress);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
// qDebug() << "Key not found";
|
||||
// qDebug() << "Event:" << event->type() <<
|
||||
// "Key:" << QString("0x%1").arg(keyEvent->key(), 8, 16, QLatin1Char( '0' )) <<
|
||||
// "Text:" << keyEvent->text() <<
|
||||
// "Shift:" << (bool)(keyEvent->modifiers() & Qt::ShiftModifier) <<
|
||||
// "Keypad:" << (bool)(keyEvent->modifiers() & Qt::KeypadModifier) <<
|
||||
// "Ctrl:" << (bool)(keyEvent->modifiers() & Qt::ControlModifier) <<
|
||||
// "Alt:" << (bool)(keyEvent->modifiers() & Qt::AltModifier) <<
|
||||
// "Meta:" << (bool)(keyEvent->modifiers() & Qt::MetaModifier);
|
||||
}
|
||||
}
|
||||
|
||||
return QObject::eventFilter(obj, event);
|
||||
|
||||
@@ -174,9 +174,16 @@ void SimulatedUIWidget::wheelEvent(QWheelEvent * event)
|
||||
{
|
||||
if (event->angleDelta().isNull())
|
||||
return;
|
||||
// steps can be negative or positive to determine direction (negative is UP/LEFT scroll)
|
||||
QPoint numSteps = event->angleDelta() / 8 / 15 * -1; // one step per 15deg
|
||||
emit simulatorWheelEvent(numSteps.y());
|
||||
|
||||
// Accumulate sub-step deltas (macOS trackpad sends pixel-level values)
|
||||
static constexpr int STEP_THRESHOLD = 120;
|
||||
m_wheelAccumulator += event->angleDelta().y() * -1;
|
||||
|
||||
int steps = m_wheelAccumulator / STEP_THRESHOLD;
|
||||
if (steps != 0) {
|
||||
m_wheelAccumulator -= steps * STEP_THRESHOLD;
|
||||
emit simulatorWheelEvent(steps);
|
||||
}
|
||||
event->accept();
|
||||
}
|
||||
|
||||
|
||||
@@ -149,6 +149,7 @@ class SimulatedUIWidget : public QWidget
|
||||
unsigned int m_backLight;
|
||||
int m_beepShow;
|
||||
int m_beepVal;
|
||||
int m_wheelAccumulator = 0;
|
||||
|
||||
static int strKeyToInt(std::string key);
|
||||
|
||||
|
||||
@@ -155,7 +155,7 @@ struct SimulatorOptions
|
||||
if (o.m_version >= 3)
|
||||
in >> o.simulatorId;
|
||||
if (o.m_version >= 4)
|
||||
in >> o.simulatorId;
|
||||
in >> o.flags;
|
||||
}
|
||||
else {
|
||||
qWarning() << "Error loading SimulatorOptions, saved version not valid:" << o.m_version << "Expected <=" << SIMULATOR_OPTIONS_VERSION;
|
||||
|
||||
@@ -20,17 +20,15 @@
|
||||
*/
|
||||
|
||||
#include "simulatorinterface.h"
|
||||
#include "wasmsimulatorinterface.h"
|
||||
#include "customdebug.h"
|
||||
#include "version.h"
|
||||
#include "firmwares/eeprominterface.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QLibraryInfo>
|
||||
|
||||
#if defined _MSC_VER || !defined __GNUC__
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
QMap<QString, QPair<QString, QLibrary *>> SimulatorLoader::registeredSimulators;
|
||||
QMap<QString, SimulatorFactory *> SimulatorLoader::registeredSimulators;
|
||||
|
||||
QStringList SimulatorLoader::getAvailableSimulators()
|
||||
{
|
||||
@@ -39,29 +37,40 @@ QStringList SimulatorLoader::getAvailableSimulators()
|
||||
|
||||
int SimulatorLoader::registerSimulators(const QDir & dir)
|
||||
{
|
||||
QStringList filters;
|
||||
#if defined(__APPLE__)
|
||||
filters << "libedgetx-*-simulator.dylib";
|
||||
#elif defined(WIN32) || defined(__CYGWIN__)
|
||||
filters << "libedgetx-*-simulator.dll";
|
||||
#else
|
||||
filters << "libedgetx-*-simulator.so";
|
||||
#endif
|
||||
registeredSimulators.clear();
|
||||
QStringList wasmFilters;
|
||||
wasmFilters << "edgetx-*-simulator.wasm";
|
||||
|
||||
qCDebug(simulatorInterfaceLoader) << "Searching for simulators in" << dir.path() << "matching pattern" << filters;
|
||||
qCDebug(simulatorInterfaceLoader) << "Searching for WASM simulators in"
|
||||
<< dir.path();
|
||||
|
||||
foreach(QString filename, dir.entryList(filters, QDir::Files)) {
|
||||
QString simuName(filename.mid(3, filename.lastIndexOf('-') - 3));
|
||||
QString libPath(dir.path() + "/" + filename);
|
||||
foreach (QString filename, dir.entryList(wasmFilters, QDir::Files)) {
|
||||
// Extract board name: "edgetx-<name>-simulator.wasm"
|
||||
QString simuName = filename;
|
||||
simuName.remove(0, 7); // remove "edgetx-"
|
||||
simuName.truncate(simuName.lastIndexOf("-simulator"));
|
||||
|
||||
if (getAvailableSimulators().contains(simuName))
|
||||
if (registeredSimulators.contains(simuName))
|
||||
continue;
|
||||
|
||||
registeredSimulators.insert(simuName, {libPath, nullptr});
|
||||
QString wasmPath = dir.path() + "/" + filename;
|
||||
|
||||
// Resolve board type from name
|
||||
Board::Type boardType = Board::BOARD_UNKNOWN;
|
||||
Firmware * fw = Firmware::getFirmwareForId(QString("edgetx-") + simuName);
|
||||
if (fw)
|
||||
boardType = fw->getBoard();
|
||||
|
||||
auto * factory = new WasmSimulatorFactory(wasmPath, simuName, boardType);
|
||||
registeredSimulators.insert(simuName, factory);
|
||||
|
||||
qCDebug(simulatorInterfaceLoader) << "Registered WASM simulator:"
|
||||
<< simuName;
|
||||
}
|
||||
|
||||
qCDebug(simulatorInterfaceLoader) << "Found libraries:" << (registeredSimulators.size() ? registeredSimulators.keys() : QStringList() << "none");
|
||||
qCDebug(simulatorInterfaceLoader)
|
||||
<< "Found WASM modules:"
|
||||
<< (registeredSimulators.size() ? registeredSimulators.keys()
|
||||
: QStringList() << "none");
|
||||
return registeredSimulators.size();
|
||||
}
|
||||
|
||||
@@ -78,7 +87,8 @@ void SimulatorLoader::registerSimulators()
|
||||
if (QDir::isAbsolutePath(SIMULATOR_LIB_SEARCH_PATH)) {
|
||||
dir.setPath(SIMULATOR_LIB_SEARCH_PATH);
|
||||
} else {
|
||||
dir.setPath(QCoreApplication::applicationDirPath() + "/" SIMULATOR_LIB_SEARCH_PATH);
|
||||
dir.setPath(QCoreApplication::applicationDirPath() + "/"
|
||||
SIMULATOR_LIB_SEARCH_PATH);
|
||||
}
|
||||
#endif
|
||||
registerSimulators(dir.absolutePath());
|
||||
@@ -86,98 +96,49 @@ void SimulatorLoader::registerSimulators()
|
||||
|
||||
void SimulatorLoader::unregisterSimulators()
|
||||
{
|
||||
for(QPair<QString, QLibrary *> lib : registeredSimulators) {
|
||||
if (lib.second)
|
||||
delete lib.second;
|
||||
}
|
||||
qDeleteAll(registeredSimulators);
|
||||
registeredSimulators.clear();
|
||||
}
|
||||
|
||||
QString SimulatorLoader::findSimulatorByName(const QString & name)
|
||||
{
|
||||
int pos;
|
||||
QString ret;
|
||||
QString simuName = name;
|
||||
|
||||
while(1) {
|
||||
qCDebug(simulatorInterfaceLoader) << "searching" << simuName << "simulator";
|
||||
if (registeredSimulators.contains(simuName)) {
|
||||
ret = simuName;
|
||||
break;
|
||||
}
|
||||
// Strip "edgetx-" prefix if present (registered names don't have it)
|
||||
if (simuName.startsWith("edgetx-"))
|
||||
simuName.remove(0, 7);
|
||||
|
||||
while (1) {
|
||||
qCDebug(simulatorInterfaceLoader) << "searching" << simuName
|
||||
<< "simulator";
|
||||
if (registeredSimulators.contains(simuName))
|
||||
return simuName;
|
||||
if ((pos = simuName.lastIndexOf('-')) <= 0)
|
||||
break;
|
||||
simuName = simuName.mid(0, pos);
|
||||
if (simuName.count('-') == 0)
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
return QString();
|
||||
}
|
||||
|
||||
SimulatorInterface * SimulatorLoader::loadSimulator(const QString & name)
|
||||
{
|
||||
SimulatorInterface * si = nullptr;
|
||||
QString simuName = findSimulatorByName(name);
|
||||
|
||||
if (simuName.isEmpty()) {
|
||||
qWarning() << "Simulator" << name << "not found.";
|
||||
return si;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QPair<QString, QLibrary *> libInfo = registeredSimulators.value(simuName, {QString(), nullptr});
|
||||
QString libPath = libInfo.first;
|
||||
QLibrary *lib = libInfo.second;
|
||||
qCDebug(simulatorInterfaceLoader) << "Trying to load simulator in " << libPath;
|
||||
|
||||
if (!lib) {
|
||||
lib = new QLibrary(libPath);
|
||||
if (lib)
|
||||
registeredSimulators.insert(simuName, {libPath, lib});
|
||||
else {
|
||||
qWarning() << "Unable to load library";
|
||||
return si;
|
||||
}
|
||||
}
|
||||
|
||||
SimulatorFactory * factory;
|
||||
RegisterSimulator registerFunc = (RegisterSimulator)lib->resolve("registerSimu");
|
||||
if (registerFunc && (factory = registerFunc()) && (si = factory->create())) {
|
||||
quint8 instance = lib->property("instances_used").toUInt();
|
||||
lib->setProperty("instances_used", ++instance);
|
||||
qCDebug(simulatorInterfaceLoader) << "Loaded" << factory->name() << "simulator instance" << instance;
|
||||
delete factory;
|
||||
}
|
||||
else {
|
||||
qWarning() << "Library error" << libPath << lib->errorString();
|
||||
}
|
||||
SimulatorInterface * si = registeredSimulators[simuName]->create();
|
||||
if (si)
|
||||
qCDebug(simulatorInterfaceLoader) << "Loaded simulator:" << simuName;
|
||||
|
||||
return si;
|
||||
}
|
||||
|
||||
bool SimulatorLoader::unloadSimulator(const QString & name)
|
||||
{
|
||||
bool ret = false;
|
||||
QString simuName = findSimulatorByName(name);
|
||||
|
||||
if (simuName.isEmpty())
|
||||
return ret;
|
||||
|
||||
QLibrary * lib = registeredSimulators.value(simuName).second;
|
||||
|
||||
if (lib && lib->isLoaded()) {
|
||||
quint8 instance = lib->property("instances_used").toUInt();
|
||||
lib->setProperty("instances_used", --instance);
|
||||
if (!instance) {
|
||||
ret = lib->unload();
|
||||
qCDebug(simulatorInterfaceLoader) << "Unloading" << simuName << "(" << lib->fileName() << ")" << "result:" << ret;
|
||||
}
|
||||
else {
|
||||
ret = true;
|
||||
qCDebug(simulatorInterfaceLoader) << "Simulator" << simuName << "instances remaining:" << instance;
|
||||
}
|
||||
}
|
||||
else {
|
||||
qCDebug(simulatorInterfaceLoader) << "Simulator library for " << simuName << "already unloaded.";
|
||||
}
|
||||
|
||||
return ret;
|
||||
Q_UNUSED(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
#include <QString>
|
||||
#include <QByteArray>
|
||||
#include <QDir>
|
||||
#include <QLibrary>
|
||||
#include <QMap>
|
||||
#include <QSerialPort>
|
||||
|
||||
@@ -177,7 +176,6 @@ class SimulatorInterface : public QObject
|
||||
void started();
|
||||
void stopped();
|
||||
void heartbeat(qint32 loops, qint64 timestamp);
|
||||
void runtimeError(const QString & error);
|
||||
void lcdChange(bool backlightEnable);
|
||||
void phaseChanged(qint8 phase, const QString & name);
|
||||
void channelOutValueChange(quint8 index, qint32 value, qint32 limit);
|
||||
@@ -216,8 +214,6 @@ class SimulatorLoader
|
||||
static bool unloadSimulator(const QString & name);
|
||||
|
||||
protected:
|
||||
typedef SimulatorFactory * (*RegisterSimulator)();
|
||||
|
||||
static int registerSimulators(const QDir & dir);
|
||||
static QMap<QString, QPair<QString, QLibrary *>> registeredSimulators;
|
||||
static QMap<QString, SimulatorFactory *> registeredSimulators;
|
||||
};
|
||||
|
||||
@@ -123,11 +123,16 @@ SimulatorMainWindow::SimulatorMainWindow(QWidget *parent, const QString & simula
|
||||
ui->menuView->insertSeparator(ui->actionToggleMenuBar);
|
||||
ui->menuView->insertAction(ui->actionToggleMenuBar, ui->toolBar->toggleViewAction());
|
||||
|
||||
// Hide some actions based on simulator capabilities.
|
||||
if(!m_simulator->getCapability(SimulatorInterface::CAP_LUA))
|
||||
ui->actionReloadLua->setDisabled(true);
|
||||
if(!m_simulator->getCapability(SimulatorInterface::CAP_TELEM_FRSKY_SPORT))
|
||||
m_telemetryDockWidget->toggleViewAction()->setDisabled(true);
|
||||
// Disable capability-dependent actions until the simulator starts and
|
||||
// the WASM module is loaded (getCapability returns 0 before that).
|
||||
ui->actionReloadLua->setDisabled(true);
|
||||
m_telemetryDockWidget->toggleViewAction()->setDisabled(true);
|
||||
connect(m_simulator, &SimulatorInterface::started, this, [this]() {
|
||||
if (m_simulator->getCapability(SimulatorInterface::CAP_LUA))
|
||||
ui->actionReloadLua->setEnabled(true);
|
||||
if (m_simulator->getCapability(SimulatorInterface::CAP_TELEM_FRSKY_SPORT))
|
||||
m_telemetryDockWidget->toggleViewAction()->setEnabled(true);
|
||||
});
|
||||
#ifndef USE_SDL
|
||||
ui->actionJoystickSettings->setDisabled(true);
|
||||
#endif
|
||||
|
||||
@@ -210,7 +210,7 @@ void SimulatorStartupDialog::loadRadioProfile(int id)
|
||||
|
||||
*m_options = g.profile[id].simulatorOptions();
|
||||
|
||||
if (m_options->firmwareId.isEmpty() && !g.profile[id].fwType().isEmpty())
|
||||
if (!g.profile[id].fwType().isEmpty())
|
||||
m_options->firmwareId = g.profile[id].fwType();
|
||||
|
||||
m_options->firmwareId = findRadioId(m_options->firmwareId);
|
||||
@@ -222,11 +222,11 @@ void SimulatorStartupDialog::loadRadioProfile(int id)
|
||||
setGlobalFirmware(m_options->firmwareId);
|
||||
|
||||
// always refresh as linked simulatorId as could change over time
|
||||
m_options->simulatorId = getCurrentFirmware()->getSimulatorId();
|
||||
m_options->simulatorId = SimulatorLoader::findSimulatorByName(getCurrentFirmware()->getSimulatorId());
|
||||
|
||||
m_simProxy->setFilterFixedString(m_options->simulatorId);
|
||||
|
||||
i = ui->cbSimulator->findText(findRadioId(m_options->simulatorId), Qt::MatchContains);
|
||||
i = ui->cbSimulator->findText(m_options->simulatorId, Qt::MatchContains);
|
||||
if (i > -1)
|
||||
ui->cbSimulator->setCurrentIndex(i);
|
||||
|
||||
@@ -293,11 +293,11 @@ void SimulatorStartupDialog::onRadioTypeChanged(int index)
|
||||
setGlobalFirmware(id);
|
||||
|
||||
// always refresh as linked simulatorId as could change over time
|
||||
id = getCurrentFirmware()->getSimulatorId();
|
||||
id = SimulatorLoader::findSimulatorByName(getCurrentFirmware()->getSimulatorId());
|
||||
|
||||
m_simProxy->setFilterFixedString(id);
|
||||
|
||||
const int i = ui->cbSimulator->findText(findRadioId(id), Qt::MatchContains);
|
||||
const int i = ui->cbSimulator->findText(id, Qt::MatchContains);
|
||||
if (i > -1)
|
||||
ui->cbSimulator->setCurrentIndex(i);
|
||||
}
|
||||
@@ -305,8 +305,8 @@ void SimulatorStartupDialog::onRadioTypeChanged(int index)
|
||||
void SimulatorStartupDialog::onDataFileSelect(bool)
|
||||
{
|
||||
QString filter = SIMU_FILES_FILTER;
|
||||
QString file = QFileDialog::getSaveFileName(this, tr("Select a data file"), ui->dataFile->text(),
|
||||
filter, NULL, QFileDialog::DontConfirmOverwrite);
|
||||
QString file = QFileDialog::getOpenFileName(this, tr("Select a data file"), ui->dataFile->text(),
|
||||
filter);
|
||||
if (!file.isEmpty()) {
|
||||
ui->dataFile->setText(file);
|
||||
ui->optFile->setChecked(true);
|
||||
|
||||
@@ -104,7 +104,6 @@ SimulatorWidget::SimulatorWidget(QWidget * parent, SimulatorInterface * simulato
|
||||
|
||||
connect(simulator, &SimulatorInterface::started, this, &SimulatorWidget::onSimulatorStarted);
|
||||
connect(simulator, &SimulatorInterface::heartbeat, this, &SimulatorWidget::onSimulatorHeartbeat);
|
||||
connect(simulator, &SimulatorInterface::runtimeError, this, &SimulatorWidget::onSimulatorError);
|
||||
connect(simulator, &SimulatorInterface::phaseChanged, this, &SimulatorWidget::onPhaseChanged);
|
||||
|
||||
connect((SimulatorMainWindow *)parent, &SimulatorMainWindow::txBatteryVoltageChanged, this, &SimulatorWidget::onTxBatteryVoltageChanged);
|
||||
@@ -862,8 +861,6 @@ void SimulatorWidget::onjoystickAxisValueChanged(int axis, int value)
|
||||
GeneralSettings radioSettings = GeneralSettings();
|
||||
if (radioSettings.isInputAvailable(stick)) {
|
||||
if (radioSettings.isInputPot(stick)) {
|
||||
if (radioSettings.inputConfig[stick].flexType == Board::FlexType::FLEX_MULTIPOS)
|
||||
stickval += 1024;
|
||||
emit widgetValueChange(RadioWidget::RADIO_WIDGET_KNOB, stick, stickval);
|
||||
} else if (radioSettings.isInputSlider(stick)) {
|
||||
emit widgetValueChange(RadioWidget::RADIO_WIDGET_FADER, stick, stickval);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "simulatorinterface.h"
|
||||
|
||||
#include <QMutex>
|
||||
#include <QTimer>
|
||||
#include <QVector>
|
||||
#include <atomic>
|
||||
|
||||
#include "wasm_export.h"
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
class WasmSimulatorInterface : public SimulatorInterface
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
WasmSimulatorInterface(const QString & wasmPath, const QString & boardName,
|
||||
Board::Type boardType);
|
||||
virtual ~WasmSimulatorInterface();
|
||||
|
||||
QString name() override;
|
||||
bool isRunning() override;
|
||||
void readRadioData(QByteArray & dest) override {}
|
||||
uint8_t * getLcd() override;
|
||||
uint8_t getSensorInstance(uint16_t id, uint8_t defaultValue = 0) override;
|
||||
uint16_t getSensorRatio(uint16_t id) override;
|
||||
const int getCapability(Capability cap) override;
|
||||
|
||||
public slots:
|
||||
void init() override;
|
||||
void start(const char * filename = nullptr, bool tests = true) override;
|
||||
void stop() override;
|
||||
void setSdPath(const QString & sdPath = "",
|
||||
const QString & settingsPath = "") override;
|
||||
void setVolumeGain(const int value) override;
|
||||
void setRadioData(const QByteArray & data) override {}
|
||||
void setAnalogValue(uint8_t index, int16_t value) override;
|
||||
void setKey(uint8_t key, bool state) override;
|
||||
void setSwitch(uint8_t swtch, int8_t state) override;
|
||||
void setTrim(unsigned int idx, int value) override;
|
||||
void setTrimSwitch(uint8_t trim, bool state) override;
|
||||
void setTrainerInput(unsigned int inputNumber, int16_t value) override;
|
||||
void setInputValue(int type, uint8_t index, int16_t value) override;
|
||||
void rotaryEncoderEvent(int steps) override;
|
||||
void touchEvent(int type, int x, int y) override;
|
||||
void lcdFlushed() override;
|
||||
void setTrainerTimeout(uint16_t ms) override;
|
||||
void sendInternalModuleTelemetry(const quint8 protocol,
|
||||
const QByteArray data) override;
|
||||
void sendExternalModuleTelemetry(const quint8 protocol,
|
||||
const QByteArray data) override;
|
||||
void setLuaStateReloadPermanentScripts() override;
|
||||
void addTracebackDevice(QIODevice * device) override;
|
||||
void removeTracebackDevice(QIODevice * device) override;
|
||||
void receiveAuxSerialData(const quint8 port_num,
|
||||
const QByteArray & data) override;
|
||||
|
||||
// Called by WASM import simuGetAnalog
|
||||
int16_t getAnalogValue(uint8_t index);
|
||||
|
||||
// Called by WASM import simuTrace
|
||||
void writeTrace(const char * text);
|
||||
|
||||
// Called by WASM import simuQueueAudio
|
||||
void queueAudio(const uint8_t * buf, uint32_t len);
|
||||
|
||||
// Called by WASM import simuLcdNotify (from WAMR thread)
|
||||
void notifyLcdReady();
|
||||
|
||||
protected slots:
|
||||
void run();
|
||||
void onLcdNotify();
|
||||
|
||||
protected:
|
||||
bool loadModule();
|
||||
void unloadModule();
|
||||
bool resolveExports();
|
||||
void refreshLcd();
|
||||
void checkOutputsChanged();
|
||||
void initAudio();
|
||||
void deinitAudio();
|
||||
|
||||
QString m_wasmPath;
|
||||
QString m_boardName;
|
||||
Board::Type m_boardType;
|
||||
QString m_sdPath;
|
||||
QString m_settingsPath;
|
||||
|
||||
QTimer * m_timer10ms = nullptr;
|
||||
std::atomic<bool> m_lcdNotified{false};
|
||||
QMutex m_mutex;
|
||||
QMutex m_mtxTbDevices;
|
||||
QVector<QIODevice *> m_tracebackDevices;
|
||||
bool m_stopRequested = false;
|
||||
|
||||
// Audio
|
||||
SDL_AudioDeviceID m_audioDevice = 0;
|
||||
int m_volumeGain = SDL_MIX_MAXVOLUME;
|
||||
|
||||
// Host-side analog values (polled by WASM via simuGetAnalog)
|
||||
static constexpr int MAX_ANALOGS = 32;
|
||||
int16_t m_analogValues[MAX_ANALOGS] = {};
|
||||
|
||||
// Cached output values for change detection
|
||||
TxOutputs m_lastOutputs;
|
||||
bool m_resetOutputsData = true;
|
||||
|
||||
// Persistent WASM buffer for bulk copies (allocated once in init)
|
||||
uint32_t m_wasmScratchBuf = 0;
|
||||
uint32_t m_wasmScratchSize = 0;
|
||||
|
||||
// Host-side LCD buffer + persistent WASM-side copy buffer
|
||||
uint8_t * m_lcdBuffer = nullptr;
|
||||
uint32_t m_lcdBufferSize = 0;
|
||||
uint32_t m_wasmLcdBuf = 0;
|
||||
uint32_t m_lcdWidth = 0;
|
||||
uint32_t m_lcdHeight = 0;
|
||||
uint32_t m_lcdDepth = 0;
|
||||
|
||||
// WAMR handles
|
||||
wasm_module_t m_module = nullptr;
|
||||
wasm_module_inst_t m_moduleInst = nullptr;
|
||||
wasm_exec_env_t m_execEnv = nullptr;
|
||||
QByteArray m_wasmBinary;
|
||||
|
||||
// Cached WASM function references
|
||||
wasm_function_inst_t m_fnInit = nullptr;
|
||||
wasm_function_inst_t m_fnStart = nullptr;
|
||||
wasm_function_inst_t m_fnStop = nullptr;
|
||||
wasm_function_inst_t m_fnIsRunning = nullptr;
|
||||
wasm_function_inst_t m_fnSetKey = nullptr;
|
||||
wasm_function_inst_t m_fnSetTrim = nullptr;
|
||||
wasm_function_inst_t m_fnSetSwitch = nullptr;
|
||||
wasm_function_inst_t m_fnLcdChanged = nullptr;
|
||||
wasm_function_inst_t m_fnLcdCopy = nullptr;
|
||||
wasm_function_inst_t m_fnLcdGetWidth = nullptr;
|
||||
wasm_function_inst_t m_fnLcdGetHeight = nullptr;
|
||||
wasm_function_inst_t m_fnLcdGetDepth = nullptr;
|
||||
wasm_function_inst_t m_fnTouchDown = nullptr;
|
||||
wasm_function_inst_t m_fnTouchUp = nullptr;
|
||||
wasm_function_inst_t m_fnFatfsSetPaths = nullptr;
|
||||
wasm_function_inst_t m_fnRotaryEncoderEvent = nullptr;
|
||||
wasm_function_inst_t m_fnGetCapability = nullptr;
|
||||
|
||||
// Output value exports (bulk copy)
|
||||
wasm_function_inst_t m_fnGetNumChannels = nullptr;
|
||||
wasm_function_inst_t m_fnCopyChannelOutputs = nullptr;
|
||||
wasm_function_inst_t m_fnCopyMixOutputs = nullptr;
|
||||
wasm_function_inst_t m_fnGetNumLogicalSwitches = nullptr;
|
||||
wasm_function_inst_t m_fnCopyLogicalSwitches = nullptr;
|
||||
wasm_function_inst_t m_fnGetTrimValue = nullptr;
|
||||
wasm_function_inst_t m_fnGetTrimRange = nullptr;
|
||||
wasm_function_inst_t m_fnGetFlightMode = nullptr;
|
||||
wasm_function_inst_t m_fnGetNumGVars = nullptr;
|
||||
wasm_function_inst_t m_fnGetNumFlightModes = nullptr;
|
||||
wasm_function_inst_t m_fnGetGVar = nullptr;
|
||||
|
||||
// Phase 4: telemetry, trim, lua, lcd, trainer
|
||||
wasm_function_inst_t m_fnSetTrimValue = nullptr;
|
||||
wasm_function_inst_t m_fnSendTelemetry = nullptr;
|
||||
wasm_function_inst_t m_fnLuaReloadPermanentScripts = nullptr;
|
||||
wasm_function_inst_t m_fnLcdFlushed = nullptr;
|
||||
wasm_function_inst_t m_fnGetMaxTrainerChannels = nullptr;
|
||||
wasm_function_inst_t m_fnCopyTrainerInput = nullptr;
|
||||
wasm_function_inst_t m_fnSetTrainerTimeout = nullptr;
|
||||
|
||||
// Backlight and function switch LEDs
|
||||
wasm_function_inst_t m_fnGetBacklightState = nullptr;
|
||||
wasm_function_inst_t m_fnGetNumCustomSwitches = nullptr;
|
||||
wasm_function_inst_t m_fnGetCustomSwitchColor = nullptr;
|
||||
wasm_function_inst_t m_fnGetCustomSwitchIndex = nullptr;
|
||||
|
||||
// Trainer input buffer (flushed in bulk via run())
|
||||
static constexpr int MAX_TRAINER_CH = 16;
|
||||
int16_t m_trainerValues[MAX_TRAINER_CH] = {};
|
||||
bool m_trainerDirty = false;
|
||||
|
||||
// Cached function switch LED colors for change detection
|
||||
static constexpr int MAX_FS_LEDS = 8;
|
||||
uint32_t m_lastFSLedColors[MAX_FS_LEDS] = {};
|
||||
|
||||
wasm_function_inst_t m_fnMalloc = nullptr;
|
||||
wasm_function_inst_t m_fnFree = nullptr;
|
||||
};
|
||||
|
||||
class WasmSimulatorFactory : public SimulatorFactory
|
||||
{
|
||||
public:
|
||||
WasmSimulatorFactory(const QString & wasmPath, const QString & boardName,
|
||||
Board::Type boardType);
|
||||
|
||||
QString name() override;
|
||||
Board::Type type() override;
|
||||
SimulatorInterface * create() override;
|
||||
|
||||
private:
|
||||
QString m_wasmPath;
|
||||
QString m_boardName;
|
||||
Board::Type m_boardType;
|
||||
};
|
||||
@@ -71,18 +71,16 @@ class RadioKnobWidget : public RadioWidget
|
||||
m_dial->setFixedSize(QSize(42, 42));
|
||||
m_dial->setNotchesVisible(true);
|
||||
|
||||
m_dial->setMinimum(-1024);
|
||||
m_dial->setMaximum(1024);
|
||||
|
||||
if (m_stepSize > 1) {
|
||||
m_dial->setMinimum(0);
|
||||
m_dial->setMaximum(2048);
|
||||
// this is a bit of a hack to get the notch markers to display correctly
|
||||
// the actual notches/value are constrained in setValue()
|
||||
// notch markers for multipos — actual values are constrained in setValueFromDial()
|
||||
m_dial->setSingleStep(m_stepSize / 10);
|
||||
m_dial->setPageStep(m_stepSize);
|
||||
m_dial->setNotchTarget(5.7);
|
||||
}
|
||||
else {
|
||||
m_dial->setMinimum(-1024);
|
||||
m_dial->setMaximum(1024);
|
||||
m_dial->setPageStep(128);
|
||||
m_dial->setNotchTarget(64);
|
||||
}
|
||||
@@ -99,14 +97,15 @@ class RadioKnobWidget : public RadioWidget
|
||||
{
|
||||
int v = value;
|
||||
if (m_stepSize > 1) {
|
||||
v = ((v + m_stepSize / 2) / m_stepSize) * m_stepSize;
|
||||
// Fix values to account for lack of precision from using integer step size
|
||||
// This makes the values more symmetrical around the center point
|
||||
// Note: this is specific to the 6 position switch which is currently the only use case here
|
||||
if (v > 1024) v += 3;
|
||||
// Round to nearest step position relative to minimum
|
||||
int offset = v - m_dial->minimum();
|
||||
offset = ((offset + m_stepSize / 2) / m_stepSize) * m_stepSize;
|
||||
v = m_dial->minimum() + offset;
|
||||
// Fix values to account for lack of precision from integer step size
|
||||
// (2048 / 5 = 409 leaves a remainder of 3). Add correction to the
|
||||
// upper half so positions are symmetric around center.
|
||||
if (v > 0) v += 3;
|
||||
if (v != value) {
|
||||
// If the desired value is different then update the slider position
|
||||
// Note: this will trigger another value changed event call back into this function, at which time setValue will be called below
|
||||
m_dial->setValue(v);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user