Skip to content

Commit

Permalink
BatCheck widget, support v2.11
Browse files Browse the repository at this point in the history
  • Loading branch information
offer-shmuely committed Sep 7, 2024
1 parent 108d70b commit 93f7e64
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
103 changes: 103 additions & 0 deletions sdcard/c480x272/WIDGETS/BattCheck/lib_sensors.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
local m_log, app_name = ...

local M = {}
M.m_log = m_log
M.app_name = app_name

--function cache
local math_floor = math.floor
local math_fmod = math.fmod
local string_gmatch = string.gmatch
local string_gsub = string.gsub
local string_len = string.len
local string_sub = string.sub
local string_char = string.char
local string_byte = string.byte


---------------------------------------------------------------------------------------------------
local function log(fmt, ...)
m_log.info(fmt, ...)
end
---------------------------------------------------------------------------------------------------

function M.split(text)
local cnt = 0
local result = {}
for val in string_gmatch(string_gsub(text, ",,", ", ,"), "([^,]+),?") do
cnt = cnt + 1
result[cnt] = val
end
--m_log.info("split: #col: %d (%s)", cnt, text)
--m_log.info("split: #col: %d (1-%s, 2-%s)", cnt, result[1], result[2])
return result, cnt
end

function M.split_pipe(text)
-- m_log.info("split_pipe(%s)", text)
local cnt = 0
local result = {}
for val in string.gmatch(string.gsub(text, "||", "| |"), "([^|]+)|?") do
cnt = cnt + 1
result[cnt] = val
end
m_log.info("split_pipe: #col: %d (%s)", cnt, text)
m_log.info("split_pipe: #col: %d [1-%s, 2-%s, ...]", cnt, result[1], result[2])
return result, cnt
end

-- remove trailing and leading whitespace from string.
-- http://en.wikipedia.org/wiki/Trim_(programming)
function M.trim(s)
if s == nil then
return nil
end
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

function M.trim_safe(s)
if s == nil then
return ""
end
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
--string.gsub(text, ",,", ", ,")
end

function M.findSourceId(sourceNameList)
local interesting_sources = {}
for i = 200, 400 do
local name = getSourceName(i)
if name ~= nil then
-- workaround for bug in getFiledInfo() -- ???? why?
if string.byte(string.sub(name, 1, 1)) > 127 then name = string.sub(name, 2, -1) end
if string.byte(string.sub(name, 1, 1)) > 127 then name = string.sub(name, 2, -1) end

for _, sourceName in ipairs(sourceNameList) do
-- print(string.format("init_compare_source: [%s(%d)][%s] (is =? %s)", name, i, sourceName, (name == sourceName)))
if (string.lower(name) == string.lower(sourceName)) then
print(string.format("init_compare_source (collecting): [%s(%d)] == [%s]", name, i, sourceName))
interesting_sources[#interesting_sources + 1] = {i,name}
end
end
end
end

-- find the source with highest priority
for _, sourceName in ipairs(sourceNameList) do
for _, source in ipairs(interesting_sources) do
local idx = source[1]
local name = source[2]
-- print(string.format("init_compare_source: is_needed? [%s(%d)]", name, idx))
if (string.lower(name) == string.lower(sourceName)) then
print(string.format("init_compare_source: we have: %s", sourceName))
print(string.format("init_compare_source (found): [%s(%d)] == [%s]", name, idx, sourceName))
return idx
end
end
print(string.format("init_compare_source: we do not have: %s", sourceName))
end
return 1
end


return M
9 changes: 6 additions & 3 deletions sdcard/c480x272/WIDGETS/BattCheck/main.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---- #########################################################################
---- # #
---- # Telemetry Widget script for FrSky Horus/RadioMaster TX16s #
---- # Telemetry Widget script for radiomaster TX16s #
---- # Copyright (C) EdgeTX #
-----# #
---- # License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html #
Expand All @@ -20,10 +20,13 @@
-- 3djc & Offer Shmuely
-- Date: 2022
local app_name = "BattCheck"
local app_ver = "0.8"
local app_ver = "0.9"

local lib_sensors = loadScript("/WIDGETS/" .. app_name .. "/lib_sensors.lua", "tcd")(m_log,app_name)
local DEFAULT_SOURCE = lib_sensors.findSourceId( {"Cels"})

local _options = {
{ "Sensor" , SOURCE, 0 }, -- default to 'Cels'
{ "Sensor" , SOURCE, DEFAULT_SOURCE }, -- default to 'Cels'
{ "Color" , COLOR , YELLOW },
{ "Shadow" , BOOL , 0 },
{ "LowestCell" , BOOL , 1 }, -- 0=main voltage display shows all-cell-voltage, 1=main voltage display shows lowest-cell
Expand Down

0 comments on commit 93f7e64

Please sign in to comment.