Another Gauge Widget “Gaugie” for Horus

I have made several gauge widgets in the past, that all have their own functions and purpose.

This time I wanted a renewed version of the built in Horus gauge widget.

The looks of the built in widget is so outdated, and the readability of the gauge, nehhh I dont like it. not clear to me.

So I decided to make my own again.

The function is the same, only the color grading is much better visible.
The gauge is a real gauge, with a frame arround it, and a good background for good contrast readabilty.

An extra option is you can set “LipoGauge” the gauge will now represents itself as in green color changing gauge from 100% to 30% and then goes solid red from 30% to 0%.

The widget automaticly resises to all widget layout sizes. including the topbar widget area.


The script:

Save the code as: main.lua and copy it to your SD card /WIDGETS/Gaugie/main.lua

 
---- ##########################################################################################################
---- # #
---- # Gauge Widget for FrSky Horus #
-----# #
---- # License GPLv3: http://www.gnu.org/licenses/gpl-3.0.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. #
---- # #
---- # Original design by Björn Pasteuning / Hobby4life 2020 #
---- # #
---- ##########################################################################################################

local options = {
{ "Source", SOURCE, 1 },
{ "GaugeColor", COLOR, WHITE },
{ "Min", VALUE, 0, -1024, 1024 },
{ "Max", VALUE, 100, -1024, 1024 },
{ "LipoGauge", VALUE, 1,0,1 },
}

function create(zone, options,counter)
local context = { zone=zone, options=options, counter=0 }
Show_Gauge = 0
return context
end

local function background(context)
context.counter = context.counter + 1
end

-- This function returns green at 100%, red below 30% and graduate in betwwen
local function getPercentColor(cpercent)
if cpercent < 30 then
return lcd.RGB(0xff, 0, 0)
else
g = math.floor(0xdf * cpercent / 100)
r = 0xdf - g
return lcd.RGB(r, g, 0)
end
end

function drawGauge(context)

value = getValue(context.options.Source)

if(value == nil) then
return
end

--Value from source in percentage
percentageValue = value - context.options.Min;
percentageValue = (percentageValue / (context.options.Max - context.options.Min)) * 100

if percentageValue > 100 then
percentageValue = 100
elseif percentageValue < 0 then
percentageValue = 0
end

--Define Source text size and set shadow color manually.
lcd.setColor(CUSTOM_COLOR, lcd.RGB(0,0,0))
FLAGS = CUSTOM_COLOR + SHADOWED
if context.zone.h < 98 then
FLAGS = FLAGS + SMLSIZE
elseif context.zone.h > 170 then
FLAGS = FLAGS + DBLSIZE
else
FLAGS = FLAGS + MIDSIZE
end

lcd.drawSource(context.zone.x, context.zone.y + 1, context.options.Source, FLAGS)

--Define Source and percentage text size and set foreground color manually.
lcd.setColor(CUSTOM_COLOR, lcd.RGB(255,248,255))
FLAGS = CUSTOM_COLOR + SHADOWED
if context.zone.h < 98 then
FLAGS = FLAGS + SMLSIZE
elseif context.zone.h > 170 then
FLAGS = FLAGS + DBLSIZE
else
FLAGS = FLAGS + MIDSIZE
end

--Percentage Text
lcd.drawText(context.zone.x + context.zone.w , context.zone.y, math.floor(percentageValue).."%", FLAGS + RIGHT)
--Source Text
lcd.drawSource(context.zone.x - 1, context.zone.y, context.options.Source, FLAGS)

--Define gauge positions
box_left = context.zone.x
box_top = context.zone.y + context.zone.h /2
box_width = context.zone.w
box_height = context.zone.h - context.zone.h/2

gauge_width = math.floor((((box_width - 2) / 100) * percentageValue) + 2)

--Gauge Back ground
lcd.setColor(CUSTOM_COLOR, lcd.RGB(200,200,200))
lcd.drawFilledRectangle(box_left, box_top, box_width, box_height, CUSTOM_COLOR + SOLID)

--Gauge color
if context.options.LipoGauge > 0 then
lcd.setColor(CUSTOM_COLOR, getPercentColor(percentageValue))
else
lcd.setColor(CUSTOM_COLOR, context.options.GaugeColor)
end

--Gauge bar
lcd.drawFilledRectangle( box_left , box_top , gauge_width , box_height , CUSTOM_COLOR + SOLID)

--Gauge Frame outline
lcd.setColor(CUSTOM_COLOR, lcd.RGB(0,0,0))
lcd.drawRectangle( box_left , box_top , box_width , box_height , CUSTOM_COLOR )
lcd.drawRectangle( box_left - 1, box_top + 1, box_width + 2,box_height - 2, CUSTOM_COLOR )

end

function update(context, options)
context.options = options
context.back = nil
end

function refresh(context)
drawGauge(context)
end

return { name="Gaugie", options=options, create=create, update=update, refresh=refresh, background=background }


You may also like...