From daa52dfa73037f561cdccb62bafe87a5763f27c4 Mon Sep 17 00:00:00 2001 From: Michell Gailing <24916947+gailingmic@users.noreply.github.com> Date: Sun, 26 Mar 2023 21:41:22 +0200 Subject: [PATCH] Implement zoom control on a potentiometer --- HORUS/SD/SCRIPTS/TOOLS/Yaapu Maps.lua | 5 +- HORUS/SD/WIDGETS/YaapuMaps/main.lua | 74 +++++++++++++++++++++------ HORUS/SD/WIDGETS/YaapuMaps/menu.lua | 5 +- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/HORUS/SD/SCRIPTS/TOOLS/Yaapu Maps.lua b/HORUS/SD/SCRIPTS/TOOLS/Yaapu Maps.lua index be7f7fd..946a29b 100644 --- a/HORUS/SD/SCRIPTS/TOOLS/Yaapu Maps.lua +++ b/HORUS/SD/SCRIPTS/TOOLS/Yaapu Maps.lua @@ -64,7 +64,8 @@ local menuItems = { {"map type:", 1, "MAPT", 1, { "", "", "", "" }, { "", "", "", "" } }, {"map grid lines:", 1, "MAPG", 1, { "yes", "no" }, { true, false } }, {"map trail dots:", 0, "MAPTD", 10, 5, 50,nil,0,1 }, - {"emulated wheel channel:", 0, "ZTC", 0, 0, 32,nil,0,1 }, + {"zoom channel type:", 1, "ZCT", 1, { "emulated wheel", "potentiometer" }, { 1, 2 } }, + {"zoom channel:", 0, "ZTC", 0, 0, 32,nil,0,1 }, {"emulated wheel delay in seconds:", 0, "ZDMS", 1, 0, 50,"sec",PREC1, 1 }, {"map zoom level default value:", 0, "MAPZ", -2, -2, 17,nil,0,1 }, {"map zoom level min value:", 0, "MAPmZ", -2, -2, 17,nil,0,1 }, @@ -193,6 +194,8 @@ local function applyConfigValues(conf) conf.mapZoomMin = getMenuItemByName(menuItems,"MAPmZ") conf.mapZoomMax = getMenuItemByName(menuItems,"MAPMZ") + conf.zoomChannelType = getMenuItemByName(menuItems,"ZCT") + local chInfo = getFieldInfo("ch"..getMenuItemByName(menuItems,"ZTC")) conf.mapWheelChannelId = (chInfo == nil and -1 or chInfo['id']) diff --git a/HORUS/SD/WIDGETS/YaapuMaps/main.lua b/HORUS/SD/WIDGETS/YaapuMaps/main.lua index 507240c..0fd9d4c 100644 --- a/HORUS/SD/WIDGETS/YaapuMaps/main.lua +++ b/HORUS/SD/WIDGETS/YaapuMaps/main.lua @@ -861,6 +861,30 @@ function utils.checkHomeResetChannel(widget) end end +function filterZoom(t) + local out = {} + local counter = 1 + + for k, v in pairs(t) do + if v == true then + out[counter] = k + counter = counter + 1 + end + end + table.sort(out) + return out +end + +function tableCount(t) + local counter = 1 + for _ in pairs(t) do counter = counter + 1 end + return counter +end + +function math_clamp(x, min, max) + return math.max(math.min(x, max), min) +end + function utils.getMapZoomLevel(widget,conf,status,customSensors) local chValue = getValue(conf.mapWheelChannelId) local newZoom = status.mapZoomLevel == nil and conf.mapZoomLevel or status.mapZoomLevel @@ -868,25 +892,43 @@ function utils.getMapZoomLevel(widget,conf,status,customSensors) if customSensors ~= nil then zoomLevels = customSensors.zoomLevels end - if conf.mapWheelChannelId > -1 then - -- SW up (increase zoom detail) - if chValue < -600 then - if conf.mapProvider == 1 then - return utils.decZoomLevel(conf,status,zoomLevels) - else - return utils.incZoomLevel(conf,status,zoomLevels) - end + + if conf.zoomChannelType == 2 then + -- do magic here + local normChValue = chValue + 1024 + + if (zoomLevels == nil) then + -- no custom zoom levels set using the default -2 to 16 zoom levels + local sizePerChannel = 2048 / 19 + return math_clamp(math.floor((normChValue / sizePerChannel))-2, -2, 16) + else + -- no custom zoom levels set using the default -2 to 16 zoom levels + local filtered = filterZoom(zoomLevels) + local sizePerChannel = 2048 / (tableCount(filtered) - 1) + local tableSize = tableCount(filtered) - 1 + return filtered[math_clamp(math.floor(normChValue / sizePerChannel) + 1, 1, tableSize)] end - -- SW down (decrease zoom detail) - if chValue > 600 then - if conf.mapProvider == 1 then - return utils.incZoomLevel(conf,status,zoomLevels) - else - return utils.decZoomLevel(conf,status,zoomLevels) + else + if conf.mapWheelChannelId > -1 then + -- SW up (increase zoom detail) + if chValue < -600 then + if conf.mapProvider == 1 then + return utils.decZoomLevel(conf,status,zoomLevels) + else + return utils.incZoomLevel(conf,status,zoomLevels) + end + end + -- SW down (decrease zoom detail) + if chValue > 600 then + if conf.mapProvider == 1 then + return utils.incZoomLevel(conf,status,zoomLevels) + else + return utils.decZoomLevel(conf,status,zoomLevels) + end end + -- switch is idle, force timer expire + zoomDelayStart = getTime() - conf.mapWheelChannelDelay*10 end - -- switch is idle, force timer expire - zoomDelayStart = getTime() - conf.mapWheelChannelDelay*10 end return status.mapZoomLevel end diff --git a/HORUS/SD/WIDGETS/YaapuMaps/menu.lua b/HORUS/SD/WIDGETS/YaapuMaps/menu.lua index be7f7fd..946a29b 100644 --- a/HORUS/SD/WIDGETS/YaapuMaps/menu.lua +++ b/HORUS/SD/WIDGETS/YaapuMaps/menu.lua @@ -64,7 +64,8 @@ local menuItems = { {"map type:", 1, "MAPT", 1, { "", "", "", "" }, { "", "", "", "" } }, {"map grid lines:", 1, "MAPG", 1, { "yes", "no" }, { true, false } }, {"map trail dots:", 0, "MAPTD", 10, 5, 50,nil,0,1 }, - {"emulated wheel channel:", 0, "ZTC", 0, 0, 32,nil,0,1 }, + {"zoom channel type:", 1, "ZCT", 1, { "emulated wheel", "potentiometer" }, { 1, 2 } }, + {"zoom channel:", 0, "ZTC", 0, 0, 32,nil,0,1 }, {"emulated wheel delay in seconds:", 0, "ZDMS", 1, 0, 50,"sec",PREC1, 1 }, {"map zoom level default value:", 0, "MAPZ", -2, -2, 17,nil,0,1 }, {"map zoom level min value:", 0, "MAPmZ", -2, -2, 17,nil,0,1 }, @@ -193,6 +194,8 @@ local function applyConfigValues(conf) conf.mapZoomMin = getMenuItemByName(menuItems,"MAPmZ") conf.mapZoomMax = getMenuItemByName(menuItems,"MAPMZ") + conf.zoomChannelType = getMenuItemByName(menuItems,"ZCT") + local chInfo = getFieldInfo("ch"..getMenuItemByName(menuItems,"ZTC")) conf.mapWheelChannelId = (chInfo == nil and -1 or chInfo['id'])