Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update 480x320 BattAnalog, Timer2, GaugeRotary widgets #185

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions sdcard/c480x320/WIDGETS/BattAnalog/lib_log.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
local app_name, script_dir = ...

local ENABLE_LOG_TO_CONSOLE = true
local ENABLE_LOG_TO_FILE = false


local M = {}
M.app_name = app_name
M.script_dir = script_dir

local function is_simulator()
local _, rv = getVersion()
return string.sub(rv, -5) == "-simu"
end

local log = {
outfile = script_dir .. "/app.log",
enable_file = ENABLE_LOG_TO_FILE,
enable_console = ENABLE_LOG_TO_CONSOLE and is_simulator(),
current_level = nil,

-- func
trace = nil,
debug = nil,
info = nil,
warn = nil,
error = nil,
fatal = nil,

levels = {
trace = 1,
debug = 2,
info = 3,
warn = 4,
error = 5,
fatal = 6,
no_logs = 99
}
}
log.current_level = log.levels["info"] -- trace|debug|info|warn|error|fatal


local function round(x, increment)
increment = increment or 1
x = x / increment
return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * increment
end

local _tostring = tostring

local function tostring(...)
local t = {}
for i = 1, select('#', ...) do
local x = select(i, ...)
if type(x) == "number" then
x = round(x, .01)
end
t[#t + 1] = _tostring(x)
end
return table.concat(t, " ")
end

function M.do_log(iLevel, ulevel, fmt, ...)
if log.enable_console == false and log.enable_file == false then
return
end

if iLevel < log.current_level then
--below the log level
return
end

local num_arg = #{ ... }
local msg
if num_arg > 0 then
msg = string.format(fmt, ...)
else
msg = fmt
end

--local lineinfo = "f.lua:0"
--local msg2 = string.format("[%-4s][%-8s] %s: %s", ulevel, M.app_name, lineinfo, msg)
local msg2 = string.format("[%-8s][%-4s] %s", M.app_name, ulevel, msg)

-- output to console
print(msg2)

-- Output to log file
if log.enable_file == true and log.outfile then
local fp = io.open(log.outfile, "a")
io.write(fp, msg2 .. "\n")
io.close(fp)
end
end

function M.trace(fmt, ...)
M.do_log(log.levels.trace, "TRACE", fmt, ...)
end
function M.debug(fmt, ...)
M.do_log(log.levels.debug, "DEBUG", fmt, ...)
end
function M.info(fmt, ...)
M.do_log(log.levels.info, "INFO", fmt, ...)
end
function M.warn(fmt, ...)
M.do_log(log.levels.warn, "WARN", fmt, ...)
end
function M.error(fmt, ...)
M.do_log(log.levels.error, "ERROR", fmt, ...)
end
function M.fatal(fmt, ...)
M.do_log(log.levels.fatal, "FATAL", fmt, ...)
end

return M
103 changes: 103 additions & 0 deletions sdcard/c480x320/WIDGETS/BattAnalog/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
Loading
Loading