diff --git a/.vscode/scripts/__pycache__/subprocess_conout.cpython-312.pyc b/.vscode/scripts/__pycache__/subprocess_conout.cpython-312.pyc index cae0deb..77c3035 100644 Binary files a/.vscode/scripts/__pycache__/subprocess_conout.cpython-312.pyc and b/.vscode/scripts/__pycache__/subprocess_conout.cpython-312.pyc differ diff --git a/bin/sensors/sensors.exe b/bin/sensors/sensors.exe index 88b779a..5f1fcd0 100644 Binary files a/bin/sensors/sensors.exe and b/bin/sensors/sensors.exe differ diff --git a/scripts/dashx/app/app.lua b/scripts/dashx/app/app.lua index 22ecec0..89476b1 100644 --- a/scripts/dashx/app/app.lua +++ b/scripts/dashx/app/app.lua @@ -771,9 +771,6 @@ function app.close() dashx.config.useCompiler = true -- Reset page and navigation state - pageLoaded = 100 - pageTitle = nil - pageFile = nil app.Page = {} app.formFields = {} app.formNavigationFields = {} @@ -802,9 +799,6 @@ function app.close() -- Reset audio app.audio = {} - -- Reset telemetry and protocol state - ELRS_PAUSE_TELEMETRY = false - CRSF_PAUSE_TELEMETRY = false -- Reset profile/rate state dashx.app.triggers.profileswitchLast = nil diff --git a/scripts/dashx/app/modules/logs/logs_logs.lua b/scripts/dashx/app/modules/logs/logs_logs.lua index 9cd6f90..d877b98 100644 --- a/scripts/dashx/app/modules/logs/logs_logs.lua +++ b/scripts/dashx/app/modules/logs/logs_logs.lua @@ -65,7 +65,6 @@ local function openPage(pidx, title, script, displaymode) local logs = utils.getLogs(logDir) - print(logDir) local name = utils.resolveModelName(dashx.session.mcu_id or dashx.session.activeLogDir) @@ -129,7 +128,7 @@ local function openPage(pidx, title, script, displaymode) if #dates == 0 then - LCD_W, LCD_H = dashx.utils.getWindowSize() + local LCD_W, LCD_H = dashx.utils.getWindowSize() local str = "@i18n(app.modules.logs.msg_no_logs_found)@" local ew = LCD_W local eh = LCD_H @@ -137,9 +136,9 @@ local function openPage(pidx, title, script, displaymode) local eposX = ew / 2 - etsizeW / 2 local eposY = eh / 2 - etsizeH / 2 - local posErr = {w = etsizeW, h = dashx.app.radio.navbuttonHeight, x = eposX, y = ePosY} + local posErr = {w = etsizeW, h = dashx.app.radio.navbuttonHeight, x = eposX, y = eposY} - line = form.addLine("", nil, false) + local line = form.addLine("", nil, false) form.addStaticText(line, posErr, str) else diff --git a/scripts/dashx/app/modules/logs/logs_view.lua b/scripts/dashx/app/modules/logs/logs_view.lua index 2f06d03..79376d9 100644 --- a/scripts/dashx/app/modules/logs/logs_view.lua +++ b/scripts/dashx/app/modules/logs/logs_view.lua @@ -75,7 +75,7 @@ local function secondsToSamples(sec) return math.floor(sec * SAMPLE_RATE) end -function readNextChunk() +local function readNextChunk() if logDataRawReadComplete then return end @@ -102,7 +102,7 @@ function readNextChunk() end end -function format_time(seconds) +local function format_time(seconds) -- Calculate minutes and remaining seconds local minutes = math.floor(seconds / 60) local seconds_remainder = seconds % 60 @@ -132,7 +132,7 @@ end -function calculateSeconds(totalSeconds, sliderValue) +local function calculateSeconds(totalSeconds, sliderValue) -- Ensure sliderValue is within the range 1-100 if sliderValue < 1 or sliderValue > 100 then error("Slider value must be between 1 and 100") end @@ -141,7 +141,7 @@ function calculateSeconds(totalSeconds, sliderValue) end -- Enhanced paginate_table() to support decimation -function paginate_table(data, step_size, position, decimationFactor) +local function paginate_table(data, step_size, position, decimationFactor) decimationFactor = decimationFactor or 1 local start_index = math.max(1, position) @@ -155,7 +155,7 @@ function paginate_table(data, step_size, position, decimationFactor) return page end -function padTable(tbl, padCount) +local function padTable(tbl, padCount) -- Get the first and last values of the table local first = tbl[1] local last = tbl[#tbl] @@ -176,13 +176,13 @@ function padTable(tbl, padCount) end -function map(x, in_min, in_max, out_min, out_max) +local function map(x, in_min, in_max, out_min, out_max) return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min end -- Efficient function to get a specific column from CSV -function getColumn(csvData, colIndex) +local function getColumn(csvData, colIndex) local column = {} local start = 1 local len = #csvData @@ -232,7 +232,7 @@ local function cleanColumn(data) return out end -function getValueAtPercentage(array, percentage) +local function getValueAtPercentage(array, percentage) -- Ensure percentage is between 0 and 100 if percentage < 0 or percentage > 100 then error("Percentage must be between 0 and 100") end @@ -479,7 +479,7 @@ local function drawCurrentIndex(points, position, totalPoints, keyindex, keyunit end end -function findMaxNumber(numbers) +local function findMaxNumber(numbers) local max = numbers[1] -- Assume the first number is the largest initially for i = 2, #numbers do -- Iterate through the table starting from the second element if numbers[i] > max then max = numbers[i] end @@ -487,7 +487,7 @@ function findMaxNumber(numbers) return max end -function findMinNumber(numbers) +local function findMinNumber(numbers) local min = numbers[1] -- Assume the first number is the smallest initially for i = 2, #numbers do -- Iterate through the table starting from the second element if numbers[i] < min then min = numbers[i] end @@ -495,7 +495,7 @@ function findMinNumber(numbers) return min end -function findAverage(numbers) +local function findAverage(numbers) local sum = 0 for i = 1, #numbers do -- Iterate through the table sum = sum + numbers[i] @@ -506,7 +506,7 @@ end local function openPage(pidx, title, script, logfile, displaymode,dirname) - + local err dashx.app.triggers.isReady = false dashx.app.uiState = dashx.app.uiStatus.pages diff --git a/scripts/dashx/app/modules/model/model.lua b/scripts/dashx/app/modules/model/model.lua index 8e86c5f..0fef06d 100644 --- a/scripts/dashx/app/modules/model/model.lua +++ b/scripts/dashx/app/modules/model/model.lua @@ -18,7 +18,6 @@ local function openPage(pidx, title, script) dashx.app.lastTitle = title dashx.app.lastScript = script - ESC = {} -- size of buttons if dashx.preferences.general.iconsize == nil or dashx.preferences.general.iconsize == "" then @@ -37,7 +36,7 @@ local function openPage(pidx, title, script) form.addLine(title) - buttonW = 100 + local buttonW = 100 local x = windowWidth - buttonW - 10 dashx.app.formNavigationFields['menu'] = form.addButton(line, {x = x, y = dashx.app.radio.linePaddingTop, w = buttonW, h = dashx.app.radio.navbuttonHeight}, { @@ -96,6 +95,7 @@ local function openPage(pidx, title, script) local pages = S_PAGES local lc = 0 local bx = 0 + local y = 0 diff --git a/scripts/dashx/app/modules/settings/settings.lua b/scripts/dashx/app/modules/settings/settings.lua index 8277b50..0f3d537 100644 --- a/scripts/dashx/app/modules/settings/settings.lua +++ b/scripts/dashx/app/modules/settings/settings.lua @@ -21,7 +21,7 @@ local function openPage(pidx, title, script) dashx.app.lastTitle = title dashx.app.lastScript = script - ESC = {} + -- size of buttons if dashx.preferences.general.iconsize == nil or dashx.preferences.general.iconsize == "" then @@ -40,7 +40,7 @@ local function openPage(pidx, title, script) form.addLine(title) - buttonW = 100 + local buttonW = 100 local x = windowWidth - buttonW - 10 dashx.app.formNavigationFields['menu'] = form.addButton(line, {x = x, y = dashx.app.radio.linePaddingTop, w = buttonW, h = dashx.app.radio.navbuttonHeight}, { @@ -99,6 +99,7 @@ local function openPage(pidx, title, script) local pages = S_PAGES local lc = 0 local bx = 0 + local y = 0 diff --git a/scripts/dashx/app/modules/settings/tools/audio.lua b/scripts/dashx/app/modules/settings/tools/audio.lua index 118700d..0cd0444 100644 --- a/scripts/dashx/app/modules/settings/tools/audio.lua +++ b/scripts/dashx/app/modules/settings/tools/audio.lua @@ -19,7 +19,6 @@ local function openPage(pidx, title, script) dashx.app.lastTitle = title dashx.app.lastScript = script - ESC = {} -- size of buttons if dashx.preferences.general.iconsize == nil or dashx.preferences.general.iconsize == "" then @@ -38,7 +37,7 @@ local function openPage(pidx, title, script) - buttonW = 100 + local buttonW = 100 local x = windowWidth - buttonW - 10 dashx.app.ui.fieldHeader( @@ -84,6 +83,7 @@ local function openPage(pidx, title, script) local pages = S_PAGES local lc = 0 local bx = 0 + local y = 0 diff --git a/scripts/dashx/app/modules/settings/tools/audio_events.lua b/scripts/dashx/app/modules/settings/tools/audio_events.lua index f096d18..776c502 100644 --- a/scripts/dashx/app/modules/settings/tools/audio_events.lua +++ b/scripts/dashx/app/modules/settings/tools/audio_events.lua @@ -1,5 +1,5 @@ local settings = {} - +local enableWakeup = false local function sensorNameMap(sensorList) local nameMap = {} diff --git a/scripts/dashx/app/modules/settings/tools/audio_switches.lua b/scripts/dashx/app/modules/settings/tools/audio_switches.lua index fd97df7..7fa5e6e 100644 --- a/scripts/dashx/app/modules/settings/tools/audio_switches.lua +++ b/scripts/dashx/app/modules/settings/tools/audio_switches.lua @@ -1,5 +1,5 @@ local settings = {} - +local enableWakeup = false local function sensorNameMap(sensorList) local nameMap = {} diff --git a/scripts/dashx/app/modules/settings/tools/dashboard.lua b/scripts/dashx/app/modules/settings/tools/dashboard.lua index a735176..99d7b7f 100644 --- a/scripts/dashx/app/modules/settings/tools/dashboard.lua +++ b/scripts/dashx/app/modules/settings/tools/dashboard.lua @@ -19,7 +19,6 @@ local function openPage(pidx, title, script) dashx.app.lastTitle = title dashx.app.lastScript = script - ESC = {} -- size of buttons if dashx.preferences.general.iconsize == nil or dashx.preferences.general.iconsize == "" then @@ -37,7 +36,7 @@ local function openPage(pidx, title, script) local panel - buttonW = 100 + local buttonW = 100 local x = windowWidth - buttonW - 10 dashx.app.ui.fieldHeader( @@ -84,6 +83,7 @@ local function openPage(pidx, title, script) local pages = S_PAGES local lc = 0 local bx = 0 + local y = 0 diff --git a/scripts/dashx/app/modules/settings/tools/dashboard_settings.lua b/scripts/dashx/app/modules/settings/tools/dashboard_settings.lua index d2274b0..a0c850e 100644 --- a/scripts/dashx/app/modules/settings/tools/dashboard_settings.lua +++ b/scripts/dashx/app/modules/settings/tools/dashboard_settings.lua @@ -2,6 +2,7 @@ local themesBasePath = "SCRIPTS:/" .. dashx.config.baseDir .. "/widgets/dashboard/themes/" local themesUserPath = "SCRIPTS:/" .. dashx.config.preferences .. "/dashboard/" +local enableWakeup = false local function openPage(pidx, title, script) -- Get the installed themes diff --git a/scripts/dashx/app/modules/settings/tools/development.lua b/scripts/dashx/app/modules/settings/tools/development.lua index 916d8c7..0341abd 100644 --- a/scripts/dashx/app/modules/settings/tools/development.lua +++ b/scripts/dashx/app/modules/settings/tools/development.lua @@ -1,4 +1,5 @@ local settings = {} +local enableWakeup = false local function openPage(pageIdx, title, script) enableWakeup = true diff --git a/scripts/dashx/app/modules/settings/tools/general.lua b/scripts/dashx/app/modules/settings/tools/general.lua index d980a0c..fe251fa 100644 --- a/scripts/dashx/app/modules/settings/tools/general.lua +++ b/scripts/dashx/app/modules/settings/tools/general.lua @@ -1,4 +1,5 @@ local settings = {} +local enableWakeup = false local function openPage(pageIdx, title, script) enableWakeup = true diff --git a/scripts/dashx/app/modules/settings/tools/localizations.lua b/scripts/dashx/app/modules/settings/tools/localizations.lua index 28a67fa..aac87e9 100644 --- a/scripts/dashx/app/modules/settings/tools/localizations.lua +++ b/scripts/dashx/app/modules/settings/tools/localizations.lua @@ -1,4 +1,6 @@ local settings = {} +local enableWakeup = false + local function openPage(pageIdx, title, script) enableWakeup = true diff --git a/scripts/dashx/lib/compile.lua b/scripts/dashx/lib/compile.lua index 8f8530a..0b78c19 100644 --- a/scripts/dashx/lib/compile.lua +++ b/scripts/dashx/lib/compile.lua @@ -1,37 +1,31 @@ --[[ - - * Copyright (C) dashx Project - * - * License GPLv3: https://www.gnu.org/licenses/gpl-3.0.en.html - * - * compile.lua - Deferred/Throttled Lua Script Compilation and Caching for dashx Suite (Ethos) - - * Usage: - * local compile = require("dashx.lib.compile") - * local chunk = compile.loadfile("myscript.lua") - * chunk() -- executes the loaded script - * -- Or use compile.dofile / compile.require as drop-in replacements - -]] -- ---[[ - * Copyright (C) dashx Project + * dashx - Deferred/Throttled Lua Script Compilation and Caching (ENV-aware) * License GPLv3: https://www.gnu.org/licenses/gpl-3.0.en.html - * compile.lua - Deferred/Throttled Lua Script Compilation and Caching for dashx Suite (Ethos) - - * Usage: - * local compile = require("dashx.lib.compile") - * local chunk = compile.loadfile("myscript.lua") - * chunk() -- executes the loaded script - * -- Or use compile.dofile / compile.require as drop-in replacements ]] local compile = {} local arg = {...} +-- Capture the environment we were loaded with so all subsequent loads +-- run with the same `_ENV` (which includes `dashx` from main.lua). +local ENV = _ENV or _G + +-- Helper to load files inside ENV (Lua 5.2+/5.3) with 5.1 fallback +local function loadfile_in_env(path) + if setfenv and _VERSION == "Lua 5.1" then + local chunk, err = loadfile(path) + if not chunk then return nil, err end + setfenv(chunk, ENV) + return chunk + else + return loadfile(path, "t", ENV) + end +end + compile._startTime = os.clock() compile._startupDelay = 15 -- seconds before starting any compiles --- Configuration: expects dashx.config to be globally available +-- Configuration: expects dashx.config to be available via ENV local logTimings = true if dashx and dashx.config then if type(dashx.preferences.developer.compilerTiming) == "boolean" then @@ -76,7 +70,7 @@ end -------------------------------------------------- -- Adaptive LRU Cache (in-memory loaders, interval-based eviction) -------------------------------------------------- -local LUA_RAM_THRESHOLD = 32 * 1024 -- 32 KB free (adjust as needed) +local LUA_RAM_THRESHOLD = 32 * 1024 -- 32 KB free (bytes) local LRU_HARD_LIMIT = 50 -- absolute maximum (safety) local EVICT_INTERVAL = 5 -- seconds between eviction checks @@ -180,11 +174,10 @@ function compile.wakeup() end end +-- ENV-aware loadfile function compile.loadfile(script) local startTime - if logTimings then - startTime = os.clock() - end + if logTimings then startTime = os.clock() end local loader, which local cache_fname = cachename(script) .. "c" @@ -195,16 +188,16 @@ function compile.loadfile(script) which = "in-memory" else if not dashx.preferences.developer.compile then - loader = loadfile(script) + loader = loadfile_in_env(script) which = "raw" else local cache_path = compiledDir .. cache_fname if disk_cache[cache_fname] then - loader = loadfile(cache_path) + loader = loadfile_in_env(cache_path) which = "compiled" else compile._enqueue(script, cache_path, cache_fname) - loader = loadfile(script) + loader = loadfile_in_env(script) which = "raw (queued for deferred compile)" end end @@ -235,7 +228,7 @@ function compile.require(modname) local chunk if not dashx.preferences.developer.compile then - chunk = assert(loadfile(path)) + chunk = assert(loadfile_in_env(path)) else chunk = compile.loadfile(path) end diff --git a/scripts/dashx/lib/ini.lua b/scripts/dashx/lib/ini.lua index ec661bd..186a24f 100644 --- a/scripts/dashx/lib/ini.lua +++ b/scripts/dashx/lib/ini.lua @@ -1,4 +1,4 @@ -ini = {} +local ini = {} -- Reads a file's full contents into a string, compatible with limited Lua function ini.load_file_as_string(path) diff --git a/scripts/dashx/main.lua b/scripts/dashx/main.lua index 1a4dad2..2e39ff7 100644 --- a/scripts/dashx/main.lua +++ b/scripts/dashx/main.lua @@ -1,103 +1,83 @@ --[[ - * Copyright (C) dashx Project * - * * License GPLv3: https://www.gnu.org/licenses/gpl-3.0.en.html - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 3 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. - - * Note. Some icons have been sourced from https://www.flaticon.com/ - * +]] -]] -- -dashx = {} +-- Keep the namespace local (no globals) +local dashx = {} dashx.session = {} +-- Shared environment so every chunk sees `dashx` without touching _G +local ENV = setmetatable({ dashx = dashx }, { + __index = _G, + -- Uncomment to forbid accidental globals from modules: + __newindex = function(_, k) error("attempt to create global '"..tostring(k).."'", 2) end +}) + -- initialise legacy font if not already set (ethos 1.6 vs 1.7) if not FONT_M then FONT_M = FONT_STD end - -- RotorFlight + ETHOS LUA configuration local config = {} -- Configuration settings for the dashx Lua Ethos Suite -config.toolName = "DashX" -- name of the tool -config.icon = lcd.loadMask("app/gfx/icon.png") -- icon -config.icon_logtool = lcd.loadMask("app/gfx/icon_logtool.png") -- icon -config.icon_unsupported = lcd.loadMask("app/gfx/unsupported.png") -- icon -config.version = {major = 2, minor = 3, revision = 0, suffix = "DEV"} -- version of the script -config.ethosVersion = {1, 6, 2} -- min version of ethos supported by this script -config.supportedMspApiVersion = {"12.07","12.08","12.09"} -- supported msp versions -config.baseDir = "dashx" -- base directory for the suite. This is only used by msp api to ensure correct path -config.preferences = config.baseDir .. ".user" -- user preferences folder location -config.defaultRateProfile = 4 -- ACTUAL -- default rate table [default = 4] -config.watchdogParam = 10 -- watchdog timeout for progress boxes [default = 10] +config.toolName = "DashX" +config.icon = lcd.loadMask("app/gfx/icon.png") +config.icon_logtool = lcd.loadMask("app/gfx/icon_logtool.png") +config.icon_unsupported = lcd.loadMask("app/gfx/unsupported.png") +config.version = {major = 2, minor = 3, revision = 0, suffix = "DEV"} +config.ethosVersion = {1, 6, 2} +config.supportedMspApiVersion = {"12.07","12.08","12.09"} +config.baseDir = "dashx" +config.preferences = config.baseDir .. ".user" +config.defaultRateProfile = 4 +config.watchdogParam = 10 dashx.config = config ---[[ - Loads and updates user preferences from an INI file. - - Steps: - 1. Retrieves the user preferences file path from the configuration. - 2. Sets `slave_ini` to the default user preferences. - 3. Initializes `master_ini` as an empty table. - 4. If the user preferences file exists, loads its contents into `master_ini`. - 5. Merges `master_ini` (existing preferences) with `slave_ini` (defaults) to ensure all default values are present. - 6. Assigns the merged preferences to `dashx.preferences`. - 7. If the loaded preferences differ from the defaults, saves the updated preferences back to the file and logs the update. - - This ensures that any missing default preferences are added to the user's preferences file without overwriting existing values. -]] -dashx.ini = assert(loadfile("lib/ini.lua"))(config) -- self contantained and never compiled +-- INI utilities (never compiled) loaded inside ENV +dashx.ini = assert(loadfile("lib/ini.lua", "t", ENV))(config) -- set defaults for user preferences local userpref_defaults ={ - general ={ - iconsize = 2, - syncname = false, - gimbalsupression = 0.85 - }, - localizations = { - temperature_unit = 0, -- 0 = Celsius, 1 = Fahrenheit - altitude_unit = 0, -- 0 = meters, 1 = feet - }, - dashboard = { - theme_preflight = "system/default", - theme_inflight = "system/default", - theme_postflight = "system/default", - }, - events = { - armed = true, - voltage = true, - fuel = true, - profile = true, - inflight = true, - }, - switches = { - }, - developer = { - compile = true, -- compile the script - devtools = false, -- show dev tools menu - logtofile = false, -- log to file - loglevel = "off", -- off, info, debug - logmsp = false, -- print msp byte stream to log - logmspQueue = false, -- periodic print the msp queue size - memstats = false, -- perioid print memory usage - mspexpbytes = 8, - apiversion = 2, -- msp api version to use for simulator - }, - menulastselected = {} + general ={ + iconsize = 2, + syncname = false, + gimbalsupression = 0.85 + }, + localizations = { + temperature_unit = 0, -- 0 = Celsius, 1 = Fahrenheit + altitude_unit = 0, -- 0 = meters, 1 = feet + }, + dashboard = { + theme_preflight = "system/default", + theme_inflight = "system/default", + theme_postflight = "system/default", + }, + events = { + armed = true, + voltage = true, + fuel = true, + profile = true, + inflight = true, + }, + switches = {}, + developer = { + compile = true, + devtools = false, + logtofile = false, + loglevel = "off", + logmsp = false, + logmspQueue = false, + memstats = false, + mspexpbytes = 8, + apiversion = 2, + }, + menulastselected = {} } +-- Preferences path os.mkdir("SCRIPTS:/" .. dashx.config.preferences) local userpref_file = "SCRIPTS:/" .. dashx.config.preferences .. "/preferences.ini" local slave_ini = userpref_defaults @@ -107,261 +87,145 @@ local updated_ini = dashx.ini.merge_ini_tables(master_ini, slave_ini) dashx.preferences = updated_ini if not dashx.ini.ini_tables_equal(master_ini, slave_ini) then - dashx.ini.save_ini_file(userpref_file, updated_ini) -end + dashx.ini.save_ini_file(userpref_file, updated_ini) +end -- tasks -dashx.config.bgTaskName = dashx.config.toolName .. " [Background]" -- background task name for msp services etc -dashx.config.bgTaskKey = "dshxbg" -- key id used for msp services +dashx.config.bgTaskName = dashx.config.toolName .. " [Background]" +dashx.config.bgTaskKey = "dshxbg" --- main --- dashx: Main table for the dashx script. --- dashx.config: Configuration table for the suite. --- dashx.session: Session table for the suite. --- dashx.app: Application module loaded from "app/app.lua" with the provided configuration. -dashx.compiler = assert(loadfile("lib/compile.lua"))(dashx.config) +-- compiler (loaded into ENV so it can load the rest into the same ENV) +dashx.compiler = assert(loadfile("lib/compile.lua","t",ENV))(dashx.config) - --- library with utility functions used throughou the suite +-- library with utility functions used throughout the suite dashx.utils = assert(dashx.compiler.loadfile("lib/utils.lua"))(dashx.config) +-- main app dashx.app = assert(dashx.compiler.loadfile("app/app.lua"))(dashx.config) - --- --- This script initializes the `dashx` tasks and background task. --- --- The `dashx.tasks` table is created to hold various tasks. --- The `dashx.tasks` is assigned the result of executing the "tasks/tasks.lua" file with the `config` parameter. --- The `dashx.compiler.loadfile` function is used to load the "tasks/tasks.lua" file, and `assert` ensures that the file is loaded successfully. --- The loaded file is then executed with the `config` parameter, and its return value is assigned to `dashx.tasks`. -- tasks dashx.tasks = assert(dashx.compiler.loadfile("tasks/tasks.lua"))(dashx.config) --- LuaFormatter off - -- configure the flight mode -dashx.flightmode = {} -dashx.flightmode.current = "preflight" +dashx.flightmode = { current = "preflight" } -- Reset the session state dashx.utils.session() --- when running in sim mode we can trigger events in tasks/simevent/simevent.lua --- this is used to trigger events in the simulator that are hard to do without a physical radio -dashx.simevent = {} -dashx.simevent.telemetry_state = true +-- simulator hooks +dashx.simevent = { telemetry_state = true } --- Retrieves the version information of the dashx module. ---- ---- This function constructs a version string and returns a table containing ---- detailed version information, including the major, minor, revision, and suffix ---- components. ---- ---- @return table A table containing the following fields: ---- - `version` (string): The full version string in the format "X.Y.Z-SUFFIX". ---- - `major` (number): The major version number. ---- - `minor` (number): The minor version number. ---- - `revision` (number): The revision version number. ---- - `suffix` (string): The version suffix (e.g., "alpha", "beta"). function dashx.version() - local version = dashx.config.version.major .. "." .. dashx.config.version.minor .. "." .. dashx.config.version.revision .. "-" .. dashx.config.version.suffix - return { - version = version, - major = dashx.config.version.major, - minor = dashx.config.version.minor, - revision = dashx.config.version.revision, - suffix = dashx.config.version.suffix - } + local v = dashx.config.version + return { + version = string.format("%d.%d.%d-%s", v.major, v.minor, v.revision, v.suffix), + major = v.major, + minor = v.minor, + revision = v.revision, + suffix = v.suffix + } end - ---[[ - Initializes the main script for the dashx. - - This function performs the following tasks: - 1. Checks if the Ethos version is supported using `dashx.utils.ethosVersionAtLeast()`. - If the version is not supported, it raises an error and stops execution. - 2. Registers system tools using `system.registerSystemTool()` with configurations from `config`. - 3. Registers a background task using `system.registerTask()` with configurations from `config`. - 4. Dynamically loads and registers widgets: - - Finds widget scripts using `dashx.utils.findWidgets()`. - - Loads each widget script dynamically using `dashx.compiler.loadfile()`. - - Assigns the loaded script to a variable inside the `dashx` table. - - Registers each widget with `system.registerWidget()` using the dynamically assigned module. - - Note: - - Assumes `v.name` is a valid Lua identifier-like string (without spaces or special characters). - - Each widget script is expected to have functions like `event`, `create`, `paint`, `wakeup`, `close`, `configure`, `read`, `write`, and optionally `persistent` and `menu`. - - Throws: - - Error if the Ethos version is not supported. - - Dependencies: - - `dashx.utils.ethosVersionAtLeast()` - - `system.registerSystemTool()` - - `system.registerTask()` - - `dashx.utils.findWidgets()` - - `dashx.compiler.loadfile()` - - `system.registerWidget()` -]] local function init() - - -- prevent this even getting close to running if version is not good - if not dashx.utils.ethosVersionAtLeast() then - - system.registerSystemTool({ - name = dashx.config.toolName, - icon = dashx.config.icon_unsupported , - create = function () end, - wakeup = function () - lcd.invalidate() - return - end, - paint = function () - local w, h = lcd.getWindowSize() - local textColor = lcd.RGB(255, 255, 255, 1) - lcd.color(textColor) - lcd.font(FONT_M) - local badVersionMsg = string.format("ETHOS < V%d.%d.%d", table.unpack(config.ethosVersion)) - local textWidth, textHeight = lcd.getTextSize(badVersionMsg) - local x = (w - textWidth) / 2 - local y = (h - textHeight) / 2 - lcd.drawText(x, y, badVersionMsg) - return - end, - close = function () end, - }) - return - end - - -- Registers the main system tool with the specified configuration. - -- This tool handles events, creation, wakeup, painting, and closing. + -- prevent this running if version is not supported + if not dashx.utils.ethosVersionAtLeast() then system.registerSystemTool({ - event = dashx.app.event, - name = dashx.config.toolName, - icon = dashx.config.icon, - create = dashx.app.create, - wakeup = dashx.app.wakeup, - paint = dashx.app.paint, - close = dashx.app.close - }) - - -- Registers the log tool with the specified configuration. - -- This tool handles events, creation, wakeup, painting, and closing. - -- disabled for now as can be reached via main tool - tbd - -- system.registerSystemTool({ - -- event = dashx.app.event, - -- name = config.toolName, - -- icon = config.icon_logtool, - -- create = dashx.app.create_logtool, - -- wakeup = dashx.app.wakeup, - -- paint = dashx.app.paint, - -- close = dashx.app.close - -- }) - - -- Registers a background task with the specified configuration. - -- This task handles wakeup and event processing. - system.registerTask({ - name = dashx.config.bgTaskName, - key = dashx.config.bgTaskKey, - wakeup = dashx.tasks.wakeup, - event = dashx.tasks.event, - init = dashx.tasks.init + name = dashx.config.toolName, + icon = dashx.config.icon_unsupported , + create = function () end, + wakeup = function () lcd.invalidate(); return end, + paint = function () + local w, h = lcd.getWindowSize() + local textColor = lcd.RGB(255, 255, 255, 1) + lcd.color(textColor) + lcd.font(FONT_M) + local badVersionMsg = string.format("ETHOS < V%d.%d.%d", table.unpack(config.ethosVersion)) + local textWidth, textHeight = lcd.getTextSize(badVersionMsg) + local x = (w - textWidth) / 2 + local y = (h - textHeight) / 2 + lcd.drawText(x, y, badVersionMsg) + return + end, + close = function () end, }) - - -- widgets are loaded dynamically - local cacheFile = "widgets.lua" - local cachePath = "cache/" .. cacheFile - local widgetList - - -- Try loading cache if it exists - local loadf, loadErr = dashx.compiler.loadfile(cachePath) - if loadf then - local ok, cached = pcall(loadf) - if ok and type(cached) == "table" then - widgetList = cached - dashx.utils.log("[cache] Loaded widget list from cache","info") - else - dashx.utils.log("[cache] Bad cache, rebuilding: "..tostring(cached),"info") - end + return + end + + -- main system tool + system.registerSystemTool({ + event = dashx.app.event, + name = dashx.config.toolName, + icon = dashx.config.icon, + create = dashx.app.create, + wakeup = dashx.app.wakeup, + paint = dashx.app.paint, + close = dashx.app.close + }) + + -- background task + system.registerTask({ + name = dashx.config.bgTaskName, + key = dashx.config.bgTaskKey, + wakeup = dashx.tasks.wakeup, + event = dashx.tasks.event, + init = dashx.tasks.init + }) + + -- widgets: use cache if valid; else rebuild + local cacheFile = "widgets.lua" + local cachePath = "cache/" .. cacheFile + local widgetList + + local loadf, loadErr = dashx.compiler.loadfile(cachePath) + if loadf then + local ok, cached = pcall(loadf) + if ok and type(cached) == "table" then + widgetList = cached + dashx.utils.log("[cache] Loaded widget list from cache","info") + else + dashx.utils.log("[cache] Bad cache, rebuilding: "..tostring(cached),"info") end - - -- If no valid cache, build and write new one - if not widgetList then - widgetList = dashx.utils.findWidgets() - dashx.utils.createCacheFile(widgetList, cacheFile, true) - dashx.utils.log("[cache] Created new widgets cache file","info") + end + + if not widgetList then + widgetList = dashx.utils.findWidgets() + dashx.utils.createCacheFile(widgetList, cacheFile, true) + dashx.utils.log("[cache] Created new widgets cache file","info") + end + + -- load and register widgets + dashx.widgets = {} + for _, v in ipairs(widgetList) do + if v.script then + local scriptModule = assert(dashx.compiler.loadfile("widgets/" .. v.folder .. "/" .. v.script))(config) + local varname = v.varname or v.script:gsub("%.lua$", "") + if dashx.widgets[varname] then + math.randomseed(os.time()) + local rand = math.random() + dashx.widgets[varname .. rand] = scriptModule + else + dashx.widgets[varname] = scriptModule + end + + system.registerWidget({ + name = v.name, + key = v.key, + event = scriptModule.event, + create = scriptModule.create, + paint = scriptModule.paint, + wakeup = scriptModule.wakeup, + build = scriptModule.build, + close = scriptModule.close, + configure = scriptModule.configure, + read = scriptModule.read, + write = scriptModule.write, + persistent = scriptModule.persistent or false, + menu = scriptModule.menu, + title = scriptModule.title + }) end + end +end - -- Iterates over the widgetList table and dynamically loads and registers widget scripts. - -- For each widget in the list: - -- 1. Checks if the widget has a script defined. - -- 2. Loads the script file from the specified folder and assigns it to a variable inside the dashx table. - -- 3. Uses the script name (or a provided variable name) as a key to store the loaded script module in the dashx table. - -- 4. Registers the widget with the system using the dynamically assigned module's functions and properties. - -- - -- Parameters: - -- widgetList - A table containing widget definitions. Each widget should have the following fields: - -- - script: The filename of the widget script to load. - -- - folder: The folder where the widget script is located. - -- - name: The name of the widget. - -- - key: A unique key for the widget. - -- - varname (optional): A custom variable name to use for storing the script module in the dashx table. - -- - -- The loaded script module should define the following functions and properties (if applicable): - -- - event: Function to handle events. - -- - create: Function to create the widget. - -- - paint: Function to paint the widget. - -- - wakeup: Function to handle wakeup events. - -- - close: Function to handle widget closure. - -- - configure: Function to configure the widget. - -- - read: Function to read data. - -- - write: Function to write data. - -- - persistent: Boolean indicating if the widget is persistent (default is false). - -- - menu: Menu definition for the widget. - -- - title: Title of the widget. - dashx.widgets = {} - - for i, v in ipairs(widgetList) do - if v.script then - -- Load the script dynamically - local scriptModule = assert(dashx.compiler.loadfile("widgets/" .. v.folder .. "/" .. v.script))(config) - - -- Use the script filename (without .lua) as the key, or v.varname if provided - local varname = v.varname or v.script:gsub("%.lua$", "") - - -- Store the module inside dashx.widgets - if dashx.widgets[varname] then - math.randomseed(os.time()) - local rand = math.random() - dashx.widgets[varname .. rand] = scriptModule - else - dashx.widgets[varname] = scriptModule - end - - -- Register the widget with the system - system.registerWidget({ - name = v.name, - key = v.key, - event = scriptModule.event, - create = scriptModule.create, - paint = scriptModule.paint, - wakeup = scriptModule.wakeup, - build = scriptModule.build, - close = scriptModule.close, - configure = scriptModule.configure, - read = scriptModule.read, - write = scriptModule.write, - persistent = scriptModule.persistent or false, - menu = scriptModule.menu, - title = scriptModule.title - }) - end - end - -end - --- LuaFormatter on - -return {init = init} +return { init = init } diff --git a/scripts/dashx/tasks/tasks.lua b/scripts/dashx/tasks/tasks.lua index 2ed319a..243722d 100644 --- a/scripts/dashx/tasks/tasks.lua +++ b/scripts/dashx/tasks/tasks.lua @@ -760,9 +760,6 @@ function tasks.init() schedulerTick = 0 ethosVersionGood = nil - lastTelemetrySensorName = nil - sportSensor, elrsSensor = nil, nil - lastModuleId = 0 lastSensorName = nil lastCheckAt = nil