1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-24 16:55:20 +03:00

3djc/batt check widget (#3519)

* Completely rewrite drawing sections to allow MUCHO easier size or position change.

* First take at large widget, missing fine tuning of displayed item, and background data collection

* Comparing lower cell with their live counterparts is so much better

* Make use of newly introduced update() function

* Tidy up
This commit is contained in:
3djc 2016-05-21 05:15:39 -07:00 committed by Bertrand Songis
parent cfbec2cad9
commit d1e7b66f1a

View file

@ -18,18 +18,52 @@
local options = {
{ "Sensor", SOURCE, 1 },
{ "Sensor", SOURCE, 1 },
{ "Color", COLOR, WHITE }
}
-- This function is runned once at the creation of the widget
local function create(zone, options)
local myZone = { zone=zone, options=options }
local myZone = { zone=zone, options=options, counter=0 }
histCellData = {}
return myZone
end
-- This function allow updates when you change widgets settings
local function update(myZone, options)
myZone.options = options
end
-- A quick and dirty check for empty table
local function isEmpty(self)
for _, _ in pairs(self) do
return false
end
return true
end
--- This function returns a table with cels values
local function getCels(sensor)
return getValue(sensor)
liveCellData = getValue(sensor)
if type(liveCellData) == "table" then
-- initialize historical table if not done yet
if isEmpty(histCellData) then
for k, v in pairs(liveCellData) do
histCellData[k] = v
end
end
-- this is necessary for simu where cellcount can change
if #histCellData ~= #liveCellData then
for k, v in pairs(liveCellData) do
histCellData[k] = v
end
end
-- stores the lowest cell values in historical table
for k, v in pairs(liveCellData) do
if v < histCellData[k] then histCellData[k] = v end
end
end
return liveCellData
end
--- This function returns the number of cels
@ -93,11 +127,11 @@ end
-- This function returns green at 100%, red bellow 30% and graduate in betwwen
local function getPercentColor(cpercent)
if cpercent < 30 then
return lcd.RGB(0xff,0,0)
return lcd.RGB(0xff, 0, 0)
else
g = math.floor(0xdf * cpercent / 100)
r = 0xdf - g
return lcd.RGB(r,g,0)
return lcd.RGB(r, g, 0)
end
end
@ -105,82 +139,84 @@ end
local function getRangeColor(value, gvalue, rvalue)
if gvalue > rvalue and not range==0 then
range = gvalue - rvalue
if value > gvalue then return lcd.RGB(0,0xdf,0) end
if value < rvalue then return lcd.RGB(0xdf,0,0) end
if value > gvalue then return lcd.RGB(0, 0xdf, 0) end
if value < rvalue then return lcd.RGB(0xdf, 0, 0) end
g = math.floor(0xdf * (value-rvalue) / range)
r = 0xdf - g
return lcd.RGB(r,g,0)
return lcd.RGB(r, g, 0)
else
range = rvalue - gvalue
if value > gvalue then return lcd.RGB(0,0xdf,0) end
if value < rvalue then return lcd.RGB(0xdf,0,0) end
if value > gvalue then return lcd.RGB(0, 0xdf, 0) end
if value < rvalue then return lcd.RGB(0xdf, 0, 0) end
r = math.floor(0xdf * (value-gvalue) / range)
g = 0xdf - r
return lcd.RGB(r,g,0)
return lcd.RGB(r, g, 0)
end
end
--- Size is 160x30
local function zoneSmall(zone)
local myBatt = {["x"]=0, ["y"]=0, ["w"]=75, ["h"]=28, ["segments_w"]=15, ["color"]=WHITE, ["cath_w"]=6, ["cath_h"]=20}
local mySensor = getCels(zone.options.Sensor)
lcd.setColor(TEXT_COLOR, zone.options.Color)
if type(mySensor) == "table" then
local myString = tostring(getCellSum(mySensor)).."V ("..getCellCount(mySensor).."S)"
local percent = getCellPercent(getCellAvg(mySensor))
lcd.drawText(zone.zone.x+90, zone.zone.y+22, myString, LEFT + SMLSIZE + TEXT_COLOR)
lcd.drawText(zone.zone.x+95, zone.zone.y,percent.."%", LEFT + MIDSIZE + TEXT_COLOR)
lcd.drawText(zone.zone.x + zone.zone.w, zone.zone.y + 22, myString, RIGHT + SMLSIZE + TEXT_COLOR)
lcd.drawText(zone.zone.x + zone.zone.w, zone.zone.y, percent.."%", RIGHT + MIDSIZE + TEXT_COLOR)
-- fils batt
lcd.setColor(CUSTOM_COLOR, getPercentColor(percent))
lcd.drawGauge(zone.zone.x+2, zone.zone.y+2, 75, zone.zone.h - 4,percent,100, CUSTOM_COLOR)
lcd.drawGauge(zone.zone.x+2, zone.zone.y+2, 75, zone.zone.h - 4, percent, 100, CUSTOM_COLOR)
-- draws bat
lcd.setColor(CUSTOM_COLOR, WHITE)
for i=2,75,15 do
lcd.drawRectangle(zone.zone.x+i, zone.zone.y+2, 15, zone.zone.h - 4, SOLID + CUSTOM_COLOR,1)
lcd.drawRectangle(zone.zone.x + myBatt.x, zone.zone.y + myBatt.y, myBatt.w, myBatt.h, CUSTOM_COLOR, 2)
lcd.drawFilledRectangle(zone.zone.x + myBatt.x + myBatt.w, zone.zone.y + myBatt.h/2 - myBatt.cath_h/2, myBatt.cath_w, myBatt.cath_h, CUSTOM_COLOR)
for i=1, myBatt.w - myBatt.segments_w, myBatt.segments_w do
lcd.drawRectangle(zone.zone.x + myBatt.x + i, zone.zone.y + myBatt.y, myBatt.segments_w, myBatt.h, CUSTOM_COLOR, 1)
end
lcd.drawRectangle(zone.zone.x+2, zone.zone.y+2, 75, zone.zone.h - 4, SOLID + CUSTOM_COLOR,2)
lcd.drawFilledRectangle(zone.zone.x+77, zone.zone.y+8, 6, zone.zone.h - 16, SOLID + CUSTOM_COLOR)
else
lcd.drawText(zone.zone.x, zone.zone.y+10, "No Cels sensor data", LEFT + SMLSIZE + INVERS + TEXT_COLOR)
end
lcd.setColor(CUSTOM_COLOR, BLACK)
lcd.setColor(TEXT_COLOR, BLACK)
return
end
--- Size is 180x70
local function zoneMedium(zone)
local myBatt = {["x"]=0, ["y"]=0, ["w"]=75, ["h"]=32, ["segments_w"]=15, ["color"]=WHITE, ["cath_w"]=6, ["cath_h"]=20}
local mySensor = getCels(zone.options.Sensor)
lcd.setColor(TEXT_COLOR, zone.options.Color)
if type(mySensor) == "table" then
local percent = getCellPercent(getCellAvg(mySensor))
lcd.drawText(zone.zone.x+102, zone.zone.y, percent.."%", LEFT + DBLSIZE + TEXT_COLOR)
-- fils batt
lcd.setColor(CUSTOM_COLOR, getPercentColor(percent))
lcd.drawGauge(zone.zone.x+2, zone.zone.y+2, 75, 36 - 4,percent,100, CUSTOM_COLOR)
mystring = ""
pos = {{x=2,y=38}, {x=60,y=38}, {x=118,y=38}, {x=2,y=57}, {x=60,y=57}, {x=118,y=57}}
for i=1,getCellCount(mySensor),1 do
lcd.drawGauge(zone.zone.x + myBatt.x +myBatt.cath_w, zone.zone.y + myBatt.y, myBatt.w, myBatt.h, percent, 100, CUSTOM_COLOR)
-- draw cells
local pos = {{x=2, y=38}, {x=60, y=38}, {x=118, y=38}, {x=2, y=57}, {x=60, y=57}, {x=118, y=57}}
for i=1, getCellCount(mySensor), 1 do
lcd.setColor(CUSTOM_COLOR, getRangeColor(mySensor[i], getCellMax(mySensor), getCellMax(mySensor) - 0.2))
lcd.drawFilledRectangle(zone.zone.x + pos[i].x,zone.zone.y + pos[i].y,58,20, CUSTOM_COLOR)
lcd.drawText(zone.zone.x + pos[i].x+10,zone.zone.y + pos[i].y,string.format("%.2f",mySensor[i]))
lcd.drawRectangle(zone.zone.x + pos[i].x,zone.zone.y + pos[i].y,59,20)
lcd.drawFilledRectangle(zone.zone.x + pos[i].x, zone.zone.y + pos[i].y, 58, 20, CUSTOM_COLOR)
lcd.drawText(zone.zone.x + pos[i].x+10, zone.zone.y + pos[i].y, string.format("%.2f", mySensor[i]))
lcd.drawRectangle(zone.zone.x + pos[i].x, zone.zone.y + pos[i].y, 59, 20)
end
lcd.drawText(zone.zone.x + 2, zone.zone.y + 40, mystring)
else
lcd.drawText(zone.zone.x, zone.zone.y+35, "No Cels sensor data", LEFT + SMLSIZE + INVERS + TEXT_COLOR)
end
-- draws bat
lcd.setColor(CUSTOM_COLOR, WHITE)
for i=2,75,15 do
lcd.drawRectangle(zone.zone.x+i, zone.zone.y+2, 15, 32, SOLID + CUSTOM_COLOR,1)
lcd.drawRectangle(zone.zone.x + myBatt.x , zone.zone.y + myBatt.y, myBatt.w, myBatt.h, CUSTOM_COLOR, 2)
lcd.drawFilledRectangle(zone.zone.x + myBatt.x + myBatt.w, zone.zone.y + myBatt.h/2 - myBatt.cath_h/2, myBatt.cath_w, myBatt.cath_h, CUSTOM_COLOR)
for i=1, myBatt.w - myBatt.segments_w, myBatt.segments_w do
lcd.drawRectangle(zone.zone.x + myBatt.x + i, zone.zone.y + myBatt.y, myBatt.segments_w, myBatt.h, CUSTOM_COLOR, 1)
end
lcd.drawRectangle(zone.zone.x+2, zone.zone.y+2, 75, 32, SOLID + CUSTOM_COLOR,2)
lcd.drawFilledRectangle(zone.zone.x+77, zone.zone.y+8, 6, 20, SOLID + CUSTOM_COLOR)
lcd.setColor(CUSTOM_COLOR, BLACK)
lcd.setColor(TEXT_COLOR, BLACK)
return
end
--- Size is 190x150
local function zoneLarge(zone)
local myBatt = {["x"]=0, ["y"]=18, ["w"]=76, ["h"]=121, ["segments_h"]=30, ["color"]=WHITE, ["cath_w"]=30, ["cath_h"]=10}
local mySensor = getCels(zone.options.Sensor)
lcd.setColor(TEXT_COLOR, zone.options.Color)
@ -190,34 +226,84 @@ local function zoneLarge(zone)
lcd.drawText(zone.zone.x+zone.zone.w, zone.zone.y+44, tostring(getCellSum(mySensor)).."V", RIGHT + MIDSIZE + TEXT_COLOR)
lcd.drawText(zone.zone.x+zone.zone.w, zone.zone.y+65, getCellCount(mySensor).."S", RIGHT + MIDSIZE + TEXT_COLOR)
lcd.setColor(CUSTOM_COLOR, getPercentColor(percent))
lcd.drawFilledRectangle(zone.zone.x+2,zone.zone.y+(147-math.floor(percent*1.2)),74,math.floor(percent*1.2), CUSTOM_COLOR)
pos = {{x=80,y=90}, {x=138,y=90}, {x=80,y=109}, {x=138,y=109}, {x=80,y=128}, {x=138,y=128}}
for i=1,getCellCount(mySensor),1 do
-- fils batt
lcd.setColor(CUSTOM_COLOR, getPercentColor(percent))
lcd.drawFilledRectangle(zone.zone.x + myBatt.x, zone.zone.y + myBatt.y + myBatt.h + myBatt.cath_h - math.floor(percent/100 * myBatt.h), myBatt.w, math.floor(percent/100 * myBatt.h), CUSTOM_COLOR)
-- draw cells
local pos = {{x=80, y=90}, {x=138, y=90}, {x=80, y=109}, {x=138, y=109}, {x=80, y=128}, {x=138, y=128}}
for i=1, getCellCount(mySensor), 1 do
lcd.setColor(CUSTOM_COLOR, getRangeColor(mySensor[i], getCellMax(mySensor), getCellMax(mySensor) - 0.2))
lcd.drawFilledRectangle(zone.zone.x + pos[i].x,zone.zone.y + pos[i].y,58,20, CUSTOM_COLOR)
lcd.drawText(zone.zone.x + pos[i].x+10,zone.zone.y + pos[i].y,string.format("%.2f",mySensor[i]))
lcd.drawRectangle(zone.zone.x + pos[i].x,zone.zone.y + pos[i].y,59,20)
lcd.drawFilledRectangle(zone.zone.x + pos[i].x, zone.zone.y + pos[i].y, 58, 20, CUSTOM_COLOR)
lcd.drawText(zone.zone.x + pos[i].x+10, zone.zone.y + pos[i].y, string.format("%.2f", mySensor[i]))
lcd.drawRectangle(zone.zone.x + pos[i].x, zone.zone.y + pos[i].y, 59, 20)
end
else
lcd.drawText(zone.zone.x+5, zone.zone.y, "No Cels sensor data", LEFT + SMLSIZE + INVERS + TEXT_COLOR)
end
-- draws bat
lcd.setColor(CUSTOM_COLOR, WHITE)
lcd.drawRectangle(zone.zone.x, zone.zone.y+28, 76, 121, SOLID + CUSTOM_COLOR,2)
lcd.drawFilledRectangle(zone.zone.x+22, zone.zone.y+20, 30, 10, SOLID + CUSTOM_COLOR,2)
for i=1,120,30 do
lcd.drawRectangle(zone.zone.x, zone.zone.y+28+i, 76, 30, SOLID + CUSTOM_COLOR,1)
lcd.drawRectangle(zone.zone.x + myBatt.x, zone.zone.y + myBatt.y + myBatt.cath_h , myBatt.w, myBatt.h, CUSTOM_COLOR, 2)
lcd.drawFilledRectangle(zone.zone.x + myBatt.x + myBatt.w/2 - myBatt.cath_w/2, zone.zone.y + myBatt.y, myBatt.cath_w, myBatt.cath_h, CUSTOM_COLOR)
for i=1, myBatt.h - myBatt.segments_h, myBatt.segments_h do
lcd.drawRectangle(zone.zone.x + myBatt.x, zone.zone.y + myBatt.y + myBatt.cath_h + i, myBatt.w, myBatt.segments_h, CUSTOM_COLOR, 1)
end
return
end
--- Size is 390x170
local function zoneXLarge(zone)
local myBatt = {["x"]=10, ["y"]=20, ["w"]=80, ["h"]=121, ["segments_h"]=30, ["color"]=WHITE, ["cath_w"]=30, ["cath_h"]=10}
local mySensor = getCels(zone.options.Sensor)
lcd.setColor(TEXT_COLOR, zone.options.Color)
if type(mySensor) == "table" then
local percent = getCellPercent(getCellAvg(mySensor))
-- fils batt
lcd.setColor(CUSTOM_COLOR, getPercentColor(percent))
lcd.drawFilledRectangle(zone.zone.x + myBatt.x, zone.zone.y + myBatt.y + myBatt.h + myBatt.cath_h - math.floor(percent/100 * myBatt.h), myBatt.w, math.floor(percent/100 * myBatt.h), CUSTOM_COLOR)
local percent = getCellPercent(getCellAvg(mySensor))
-- draw right text section
lcd.drawText(zone.zone.x+zone.zone.w, zone.zone.y + myBatt.y, percent.."%", RIGHT + XXLSIZE + TEXT_COLOR)
lcd.drawText(zone.zone.x+zone.zone.w, zone.zone.y + myBatt.y + 67, tostring(getCellSum(mySensor)).."V", RIGHT + MIDSIZE + TEXT_COLOR)
lcd.drawText(zone.zone.x+zone.zone.w, zone.zone.y + myBatt.y + 110, getCellCount(mySensor).."S", RIGHT + MIDSIZE + TEXT_COLOR)
-- draw cells
local pos = {{x=110, y=38}, {x=160, y=38}, {x=210, y=38}, {x=110, y=57}, {x=160, y=57}, {x=210, y=57}}
for i=1, getCellCount(mySensor), 1 do
lcd.setColor(CUSTOM_COLOR, getRangeColor(mySensor[i], getCellMax(mySensor), getCellMax(mySensor) - 0.2))
lcd.drawFilledRectangle(zone.zone.x + pos[i].x, zone.zone.y + pos[i].y, 58, 20, CUSTOM_COLOR)
lcd.drawText(zone.zone.x + pos[i].x+10, zone.zone.y + pos[i].y, string.format("%.2f", mySensor[i]))
lcd.drawRectangle(zone.zone.x + pos[i].x, zone.zone.y + pos[i].y, 59, 20)
end
-- draw cells for lowest cells
local pos = {{x=110, y=110}, {x=160, y=110}, {x=210, y=110}, {x=110, y=129}, {x=160, y=129}, {x=210, y=129}}
for i=1, getCellCount(mySensor), 1 do
lcd.setColor(CUSTOM_COLOR, getRangeColor(histCellData[i], mySensor[i], mySensor[i] - 0.3))
lcd.drawFilledRectangle(zone.zone.x + pos[i].x, zone.zone.y + pos[i].y, 58, 20, CUSTOM_COLOR)
lcd.drawText(zone.zone.x + pos[i].x+10, zone.zone.y + pos[i].y, string.format("%.2f", histCellData[i]))
lcd.drawRectangle(zone.zone.x + pos[i].x, zone.zone.y + pos[i].y, 59, 20)
end
else
lcd.drawText(zone.zone.x+5, zone.zone.y, "No Cels sensor data", LEFT + SMLSIZE + INVERS + TEXT_COLOR)
end
-- draws bat
lcd.setColor(CUSTOM_COLOR, WHITE)
lcd.drawRectangle(zone.zone.x + myBatt.x, zone.zone.y + myBatt.y + myBatt.cath_h , myBatt.w, myBatt.h, CUSTOM_COLOR, 2)
lcd.drawFilledRectangle(zone.zone.x + myBatt.x + myBatt.w/2 - myBatt.cath_w/2, zone.zone.y + myBatt.y, myBatt.cath_w, myBatt.cath_h, CUSTOM_COLOR)
for i=1, myBatt.h - myBatt.segments_h, myBatt.segments_h do
lcd.drawRectangle(zone.zone.x + myBatt.x, zone.zone.y + myBatt.y + myBatt.cath_h + i, myBatt.w, myBatt.segments_h, CUSTOM_COLOR, 1)
end
-- draw middle rectangles
lcd.drawRectangle(zone.zone.x + 110, zone.zone.y + 38, 160, 40, CUSTOM_COLOR, 1)
lcd.drawText(zone.zone.x + 270, zone.zone.y + 21, "Live data", RIGHT + SMLSIZE + INVERS + TEXT_COLOR)
lcd.drawRectangle(zone.zone.x + 110, zone.zone.y + 110, 160, 40, CUSTOM_COLOR, 1)
lcd.drawText(zone.zone.x + 270, zone.zone.y + 93, "Lowest data", RIGHT + SMLSIZE + INVERS + TEXT_COLOR)
return
end
function refresh(myZone)
if myZone.options.Sensor == 1 then
lcd.drawText(myZone.zone.x+2, myZone.zone.y+2, "Widget not configured", LEFT + SMLSIZE + INVERS + TEXT_COLOR)
lcd.drawText(myZone.zone.x+2, myZone.zone.y+2, "BattCheck not configured", LEFT + SMLSIZE + INVERS + TEXT_COLOR)
return
end
if myZone.zone.w > 380 and myZone.zone.h > 165 then zoneXLarge(myZone)
@ -227,4 +313,4 @@ function refresh(myZone)
end
end
return { name="BattCheck", options=options, create=create, refresh=refresh }
return { name="BattCheck", options=options, create=create, update=update, refresh=refresh }