Skip to content

Commit

Permalink
feat(color): Shmuely/gauge rotary v0.6 (EdgeTX#94)
Browse files Browse the repository at this point in the history
* new Rotary Gauge widget

* Rotary Gauge widget: less ticks on small size

* new Rotary Gauge widget

* new Rotary Gauge widget

* widget GaugeRotary: v0.6
better min/max
increase max value (for RPM)
stability changes
log ot file (if needed)

* widget GaugeRotary: v0.6
better min/max
increase max value (for RPM)
stability changes
log ot file (if needed)

* widget GaugeRotary: v0.6
fix bug in highAsGreen option

* widget GaugeRotary: v0.6
update lib file names

* GaugeRotary:
double tap to exit full screen

* GaugeRotary:
mix max layout

---------

Co-authored-by: Shmuely <offer.shmuely@cognyte.com>
Co-authored-by: oshmuely <AbCd321654>
  • Loading branch information
offer-shmuely and offer-shmuely-cognyte authored Feb 23, 2023
1 parent 6373a8f commit a9de667
Show file tree
Hide file tree
Showing 5 changed files with 511 additions and 164 deletions.
84 changes: 53 additions & 31 deletions sdcard/c480x272/WIDGETS/GaugeRotary/gauge_core.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
local HighAsGreen, p2 = ...
local m_log, HighAsGreen = ...

local self = {}
self.HighAsGreen = HighAsGreen
local M = {}
M.m_log = m_log
M.HighAsGreen = HighAsGreen

-- better font names
local FONT_38 = XXLSIZE -- 38px
local FONT_16 = DBLSIZE -- 16px
local FONT_12 = MIDSIZE -- 12px
local FONT_8 = 0 -- Default 8px
local FONT_6 = SMLSIZE -- 6px

--------------------------------------------------------------
local function log(s)
--return;
print("Gauge_core: " .. s)
local function log(fmt, ...)
m_log.info(fmt, ...)
end
--------------------------------------------------------------

function self.drawArm(armX, armY, armR, percentageValue, color, isFull)
function M.drawArm(armX, armY, armR, percentageValue, color, isFull)
--min = 5.54
--max = 0.8

Expand Down Expand Up @@ -38,7 +45,7 @@ function self.drawArm(armX, armY, armR, percentageValue, color, isFull)
end

-- This function returns green at gvalue, red at rvalue and graduate in between
function self.getRangeColor(value, red_value, green_value)
function M.getRangeColor(value, red_value, green_value)
local range = math.abs(green_value - red_value)
if range == 0 then
return lcd.RGB(0, 0xdf, 0)
Expand Down Expand Up @@ -70,7 +77,16 @@ function self.getRangeColor(value, red_value, green_value)
end
end

function self.drawGauge(centerX, centerY, centerR, isFull, percentageValue, percentageValueMin, percentageValueMax, txt1, value_fmt_min, value_fmt_max, txt2)

function M.drawBadge(txtX, txtY, txt1, font_size)
local ts_w, ts_h = lcd.sizeText(txt1, font_size)
local r = ts_h / 2
lcd.drawFilledCircle(txtX , txtY + r, r, GREY)
lcd.drawFilledCircle(txtX + ts_w , txtY + r, r, GREY)
lcd.drawFilledRectangle(txtX, txtY , ts_w, ts_h, GREY)
end

function M.drawGauge(centerX, centerY, centerR, isFull, percentageValue, percentageValueMin, percentageValueMax, txt1, value_fmt_min, value_fmt_max, txt2)
if value_fmt_min == nil then
value_fmt_min = ""
end
Expand All @@ -88,12 +104,12 @@ function self.drawGauge(centerX, centerY, centerR, isFull, percentageValue, perc
local tickWidth = 9
local armCenterR = centerR / 2.5
local armR = centerR - 8
local txtSize = DBLSIZE
local txtSize = FONT_16
if centerR < 65 then
txtSize = MIDSIZE
txtSize = FONT_12
end
if centerR < 30 then
txtSize = SMLSIZE
txtSize = FONT_6
end

-- main gauge background
Expand Down Expand Up @@ -125,14 +141,14 @@ function self.drawGauge(centerX, centerY, centerR, isFull, percentageValue, perc
tick_step = 10 + 0.15 * (100 - centerR)
end
for i = 0, to_tick, tick_step do
--log("HighAsGreen: " .. self.HighAsGreen)
if (self.HighAsGreen == 1) then
local newColor = self.getRangeColor(i, 0, to_tick - 10)
--log("HighAsGreen: " .. M.HighAsGreen)
if (M.HighAsGreen == 1) then
local newColor = M.getRangeColor(i, 0, to_tick - 10)
lcd.setColor(CUSTOM_COLOR, newColor)
--lcd.setColor(CUSTOM_COLOR, self.getRangeColor(i, 0, to_tick - 10))
--lcd.setColor(CUSTOM_COLOR, M.getRangeColor(i, 0, to_tick - 10))
else
lcd.setColor(CUSTOM_COLOR, self.getRangeColor(i, to_tick - 10, 0))
--lcd.setColor(CUSTOM_COLOR, self.getRangeColor(i, 120 , 30))
lcd.setColor(CUSTOM_COLOR, M.getRangeColor(i, to_tick - 10, 0))
--lcd.setColor(CUSTOM_COLOR, M.getRangeColor(i, 120 , 30))
end
lcd.drawAnnulus(centerX, centerY, centerR - fender - 3 - tickWidth, centerR - fender - 3, tick_offset + i, tick_offset + i + 7, CUSTOM_COLOR)
--lcd.drawAnnulus(centerX, centerY, centerR -fender -3 -tickWidth, centerR -fender -3 , 250 +i, 250 +i +7, YELLOW)
Expand All @@ -142,25 +158,25 @@ function self.drawGauge(centerX, centerY, centerR, isFull, percentageValue, perc

local armColor = lcd.RGB(255, 255, 255)
local armColorMin, armColorMax
if (self.HighAsGreen == 1) then
if (M.HighAsGreen == 1) then
armColorMin = lcd.RGB(200, 0, 0)
armColorMax = lcd.RGB(0, 200, 0)
else
armColorMin = lcd.RGB(0, 200, 0)
armColorMax = lcd.RGB(200, 0, 0)
end

--self.drawArm(centerX, centerY, armR, 0, armColorMin, isFull)
--self.drawArm(centerX, centerY, armR, 10, armColorMin, isFull)
--self.drawArm(centerX, centerY, armR, 50, armColorMin, isFull)
--self.drawArm(centerX, centerY, armR, 90, armColorMin, isFull)
--self.drawArm(centerX, centerY, armR, 100, armColorMin, isFull)
--M.drawArm(centerX, centerY, armR, 0, armColorMin, isFull)
--M.drawArm(centerX, centerY, armR, 10, armColorMin, isFull)
--M.drawArm(centerX, centerY, armR, 50, armColorMin, isFull)
--M.drawArm(centerX, centerY, armR, 90, armColorMin, isFull)
--M.drawArm(centerX, centerY, armR, 100, armColorMin, isFull)

if percentageValueMin ~= nil and percentageValueMax ~= nil then
self.drawArm(centerX, centerY, armR, percentageValueMin, armColorMin, isFull)
self.drawArm(centerX, centerY, armR, percentageValueMax, armColorMax, isFull)
M.drawArm(centerX, centerY, armR, percentageValueMin, armColorMin, isFull)
M.drawArm(centerX, centerY, armR, percentageValueMax, armColorMax, isFull)
end
self.drawArm(centerX, centerY, armR, percentageValue, armColor, isFull)
M.drawArm(centerX, centerY, armR, percentageValue, armColor, isFull)

-- hide the base of the arm
lcd.drawFilledCircle(centerX, centerY, armCenterR, BLACK)
Expand All @@ -170,19 +186,25 @@ function self.drawGauge(centerX, centerY, centerR, isFull, percentageValue, perc
)

-- text in center
lcd.drawText(centerX + 0, centerY - 8, txt2, CENTER + SMLSIZE + WHITE) -- XXLSIZE/DBLSIZE/MIDSIZE/SMLSIZE
lcd.drawText(centerX + 0, centerY - 8, txt2, CENTER + FONT_6 + WHITE) -- FONT_38/FONT_16/FONT_12/FONT_6

-- text min
M.drawBadge(centerX - armCenterR - 50, centerY + 20, value_fmt_min, FONT_12)
lcd.drawText(centerX - armCenterR - 20, centerY + 20, value_fmt_min, CENTER + FONT_12 + armColorMin)

lcd.drawText(centerX - armCenterR - 12, centerY + 20, value_fmt_min, CENTER + SMLSIZE + WHITE)
lcd.drawText(centerX + armCenterR + 12, centerY + 20, value_fmt_max, CENTER + SMLSIZE + WHITE)
-- text max
M.drawBadge(centerX + armCenterR - 5, centerY + 20, value_fmt_min, FONT_12)
lcd.drawText(centerX + armCenterR + 20, centerY + 20, value_fmt_max, CENTER + FONT_12 + armColorMax)

-- text below
if isFull then
--lcd.drawText(centerX + 8, centerY + 30, txt1, CENTER + txtSize + WHITE)
M.drawBadge(centerX + 0, centerY + armCenterR +2, txt1, txtSize)
lcd.drawText(centerX + 0, centerY + armCenterR + 2, txt1, CENTER + txtSize + WHITE)
else
-- no text below in flat mode
end

end

return self
return M
110 changes: 110 additions & 0 deletions sdcard/c480x272/WIDGETS/GaugeRotary/lib_log.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
local app_name, script_dir = ...

local ENABLE_LOG_TO_CONSOLE = false
local ENABLE_LOG_TO_FILE = false


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

local log = {
outfile = script_dir .. "/app.log",
enable_file = ENABLE_LOG_TO_FILE,
enable_console = ENABLE_LOG_TO_CONSOLE,
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 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
Loading

0 comments on commit a9de667

Please sign in to comment.