Skip to content
Merged
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
186 changes: 89 additions & 97 deletions sdcard/color/WIDGETS/CellsValues/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

local name = "Cells Values"
local cellsT = {}
local T
local T = {}
-- When the difference between the lowest and highest cell exceeds 0.1,
-- the delta value is then displayed in red
local deltawarning = 0
Expand All @@ -36,7 +36,6 @@ local options = {
{ "circletext", COLOR, COLOR_THEME_PRIMARY1},
{ "textcolor", COLOR, COLOR_THEME_PRIMARY1},
{ "textalarmcolor", COLOR, COLOR_THEME_WARNING},

}

local function translate(nam)
Expand All @@ -52,22 +51,19 @@ local function translate(nam)
end

local function getCellsSensor(sensor)
T = getValue(sensor)

-- Safety check: ensure T is a valid table
if T == nil or type(T) ~= "table" then
T = {
-- 3.99, 4.0, 4.01, 4.02, 3.98, 3.97 -- uncomment to test in simulator
local val = getValue(sensor)
if val == nil or type(val) ~= "table" then
val = {
-- 3.99, 4.0, 4.01, 4.02, 3.98, 3.97 -- uncomment to test in simulator
}
end

T = val
return T
end

local function getColorGradient(value)
-- Clamp input to 0-100 range
if value < 0 then value = 0 end
if value > 100 then value = 100 end
-- Clamp input to 0-100 range, guard against nil
value = math.max(0, math.min(100, value or 0))

local r, g, b

Expand All @@ -85,47 +81,46 @@ local function getColorGradient(value)
b = 0
end

return lcd.RGB(r,g,b)
return lcd.RGB(r, g, b)
end

local function getCellCount(T)
if type(T) == "table" then
return math.max(#T, 0)
else
return 0
local function getCellCount(tbl)
if type(tbl) == "table" then
return math.max(#tbl, 0)
end
return 0
end

local function getCellMinMax(T)
if type(T) == "table" and #T > 0 then
local min, max = T[1], T[1]
for i=1, #T, 1 do
local v = T[i]
if v and v < min then min = v end
if v and v > max then max = v end
local function getCellMinMax(tbl)
if type(tbl) == "table" and #tbl > 0 then
local min, max = tbl[1], tbl[1]
for i = 1, #tbl do
local v = tbl[i]
if v then
if v < min then min = v end
if v > max then max = v end
end
end
return min, max
else
return 0, 0
end
return 0, 0
end

local function getCellTotal(T)
if type(T) == "table" then
local function getCellTotal(tbl)
if type(tbl) == "table" then
local cellsum = 0
for i=1, #T, 1 do
if T[i] then
cellsum = cellsum + T[i]
for i = 1, #tbl do
if tbl[i] then
cellsum = cellsum + tbl[i]
end
end
return cellsum
else
return 0
end
return 0
end

local function getCellPercent(cellvalue)
if not cellvalue or cellvalue <= 0 then
if not cellvalue or cellvalue <= 0 or #cellsT == 0 then
return 0
end

Expand All @@ -143,15 +138,15 @@ local function getCellPercent(cellvalue)
return 100
end

local function getCellTotalPercent(T)
local count = getCellCount(T)
local function getCellTotalPercent(tbl)
local count = getCellCount(tbl)
if count == 0 then return 0 end

local cellaverage = getCellTotal(T) / count
local cellaverage = getCellTotal(tbl) / count
local lastpercentage = 0

for i=1, #cellsT do
for j=1, #cellsT[i] do
for i = 1, #cellsT do
for j = 1, #cellsT[i] do
if cellaverage >= cellsT[i][j][1] then
lastpercentage = cellsT[i][j][2]
else
Expand All @@ -162,51 +157,45 @@ local function getCellTotalPercent(T)
return 100
end

local function getCellText(T, index)
if type(T) == "table" and T[index] and index <= #T then
return string.format("%1.2f", T[index])
else
return "??"
local function getCellText(tbl, index)
if type(tbl) == "table" and index and index <= #tbl and tbl[index] then
return string.format("%1.2f", tbl[index])
end
return "??"
end

local function getTotalText(T)
return string.format("T:%2.2f V", getCellTotal(T))
local function getTotalText(tbl)
return string.format("T:%2.2f V", getCellTotal(tbl))
end

local function getDeltaText(T)
local min, max = getCellMinMax(T)
if max - min > 0.1 then
deltawarning = 1
else
deltawarning = 0
end
return string.format("D:%0.2f V", max - min)
local function getDeltaText(tbl)
local min, max = getCellMinMax(tbl)
local delta = max - min
deltawarning = (delta > 0.1) and 1 or 0
return string.format("D:%0.2f V", delta)
end

local function getSingleCellPercentage(T, index)
if type(T) == "table" and T[index] then
local value = T[index]
return getCellPercent(value)
else
return 0
local function getSingleCellPercentage(tbl, index)
if type(tbl) == "table" and tbl[index] then
return getCellPercent(tbl[index])
end
return 0
end

local function cellLayout(c, zw, th, rd, bw, bh, bx, by, widget)
local w = zw // 2
local x = (c + 1) % 2 * (zw + 1) // 2
local y = (c - 1) // 2 * th
return ({type="box", x=x, y=y, w=w, h=th, visible=function() return getCellCount(T) >= c end,
children={
{type="circle", x=rd, y=rd, radius=rd, filled=true, color=widget.options.circlecolor, children={{type="label", text=c, x=rd//2, y=0, color=widget.options.circletext}}},
{type="label", x=2*rd+lvgl.PAD_TINY, text=function() return getCellText(T, c) end, color=widget.options.textcolor},
{type="rectangle", x=bx, y=by, h=bh, w=bw, color=GREY, children={
{type="rectangle", filled=true,
size=function() return (bw-2)*getSingleCellPercentage(T, c)//100, bh-2 end,
color=function() return getColorGradient(getSingleCellPercentage(T, c)) end },
}},
}})
local w = zw // 2
local x = (c + 1) % 2 * (zw + 1) // 2
local y = (c - 1) // 2 * th
return ({type="box", x=x, y=y, w=w, h=th, visible=function() return getCellCount(T) >= c end,
children={
{type="circle", x=rd, y=rd, radius=rd, filled=true, color=widget.options.circlecolor, children={{type="label", text=c, x=rd//2, y=0, color=widget.options.circletext}}},
{type="label", x=2*rd+lvgl.PAD_TINY, text=function() return getCellText(T, c) end, color=widget.options.textcolor},
{type="rectangle", x=bx, y=by, h=bh, w=bw, color=GREY, children={
{type="rectangle", filled=true,
size=function() return (bw-2)*getSingleCellPercentage(T, c)//100, bh-2 end,
color=function() return getColorGradient(getSingleCellPercentage(T, c)) end },
}},
}})
end

local function create(zone, options)
Expand All @@ -227,9 +216,12 @@ local function update(widget, options)

if lvgl == nil then return end

-- Guard: zone must be valid and sized before building layout
if not widget.zone or (widget.zone.w or 0) == 0 then return end

if options.battchemistry == 2 then
-- Lipo HV
cellsT = {
cellsT = {
{ {3.000, 0}},
{ {3.093, 1}, {3.196, 2}, {3.301, 3}, {3.401, 4}, {3.477, 5}, {3.544, 6}, {3.601, 7}, {3.637, 8}, {3.664, 9}, {3.679, 10} },
{ {3.683, 11}, {3.689, 12}, {3.692, 13}, {3.705, 14}, {3.710, 15}, {3.713, 16}, {3.715, 17}, {3.720, 18}, {3.731, 19}, {3.735, 20} },
Expand Down Expand Up @@ -265,32 +257,32 @@ local function update(widget, options)

local zw = widget.zone.w
local tw, th = lcd.sizeText("3.99", STDSIZE)
local rd = th//2
tw = tw+rd*2+lvgl.PAD_TINY*2
local bw = zw // 2 - tw - lvgl.PAD_TINY
local bh = th-lvgl.PAD_SMALL
local rd = th // 2
tw = tw + rd * 2 + lvgl.PAD_TINY * 2
local bw = math.max(1, zw // 2 - tw - lvgl.PAD_TINY)
local bh = math.max(1, th - lvgl.PAD_SMALL)
local bx = zw // 2 - bw - 1
local by = (th - bh) // 2

local lyt = {
{type="label", text="No Cells sensor", color=COLOR_THEME_WARNING, visible=function() return getCellCount(T) == 0 end, w=zw, align=CENTER|VCENTER},
{type="box", w=zw, h=widget.zone.h, visible=function() return getCellCount(T) > 0 end,
children={
cellLayout(1, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(2, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(3, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(4, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(5, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(6, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(7, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(8, zw, th, rd, bw, bh, bx, by, widget),
{type="box", pos=function() return 0, ((getCellCount(T) + 1) // 2) * th end, children={
{type="label", x=0, text=function() return string.format("%d %%", getCellTotalPercent(T)) end, font=SMALLSIZE, color=widget.options.textcolor},
{type="label", x=zw/4, text=function() return getTotalText(T) end, font=SMALLSIZE, color=widget.options.textcolor},
{type="label", x=2*zw/3, text=function() return getDeltaText(T) end, font=SMALLSIZE, color= function() return (deltawarning == 0) and widget.options.textcolor or widget.options.textalarmcolor end},
}},
{type="label", text="No Cells sensor", color=COLOR_THEME_WARNING, visible=function() return getCellCount(T) == 0 end, w=zw, align=CENTER|VCENTER},
{type="box", w=zw, h=widget.zone.h, visible=function() return getCellCount(T) > 0 end,
children={
cellLayout(1, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(2, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(3, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(4, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(5, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(6, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(7, zw, th, rd, bw, bh, bx, by, widget),
cellLayout(8, zw, th, rd, bw, bh, bx, by, widget),
{type="box", pos=function() return 0, ((getCellCount(T) + 1) // 2) * th end, children={
{type="label", x=0, text=function() return string.format("%d %%", getCellTotalPercent(T)) end, font=SMALLSIZE, color=widget.options.textcolor},
{type="label", x=zw/4, text=function() return getTotalText(T) end, font=SMALLSIZE, color=widget.options.textcolor},
{type="label", x=2*zw/3, text=function() return getDeltaText(T) end, font=SMALLSIZE, color= function() return (deltawarning == 0) and widget.options.textcolor or widget.options.textalarmcolor end},
}},
}
}
}
}

lvgl.build(lyt)
Expand Down Expand Up @@ -326,4 +318,4 @@ return {
background = background,
translate = translate,
useLvgl = true
}
}