--=============================================================== -- Top-Speed Horus widget. Displays fastest speed during the -- current flight. The 'SH' momentary switch will call-out -- the current top speed. -- -- Defaults to Imperial (MPH). If you want to change to metric -- (KPH) changed the Units variable on line 28. To change the -- switch that is used for the call-out, change line 69 -- -- T3chDadĀ® August-2018 ****NON-COMMERCIAL USE ONLY**** -- Comments and suggestion by l Shems --=============================================================== local function round(num, numDecimalPlaces) --l Shems: moved forward. Needs to be declared before first use local mult = 10^(numDecimalPlaces or 0) return math.floor(num * mult + 0.5) / mult end local defaultOptions = { --l Shems: make some widget options to set it up {"switch",SOURCE,117,109,117}, --l Shems limits don't seem to work ??? {"units",VALUE,0,0,1}, --l Shems } local function getTelemetryId(name) local field = getFieldInfo(name) --l Shems: local declaration was missing, so it was global as default if field then return field.id else return -1 end end --local TopSpeed = 0 --l Shems: declared here it will be part of the WIDGET, not the widget INSTANCE --================================== -- Units: use either "mph" or "kph" --local Units = "mph" --l Shems: declared here it will be part of the WIDGET, not the widget INSTANCE --================================== --local block = 0 --l Shems: declared here it will be part of the WIDGET, not the widget INSTANCE local function createWidget(zone, options) local TopSpeed = 0 --l Shems: local declaration was missing, so it was global as default local TelID = getTelemetryId("GSpd") --l Shems: make it an instance thing (later an option ??) local block = 0 --l Shems: make it an instance thing return { zone=zone, options=options, TopSpeed=TopSpeed,TelID=TelID,block = block } --l Shems: need to pass the widget instance data end local function updateWidget(widgetToUpdate, newOptions) widgetToUpdate.options = newOptions -- updateSpeed(widgetToUpdate) l Shems: this function is to update the widget options through the options screen in opentx, and is never called during use. end local function backgroundProcessWidget(widgetToProcessInBackground) -- updateSpeed(widgetToProcessInBackground) --l Shems: do the processing in this function, see rest of this function if widgetToProcessInBackground.TelID == -1 then -- do nothing else local spd = getValue(widgetToProcessInBackground.TelID) --conversion from KNOTS to MPH if widgetToProcessInBackground.options.units == 0 then spd = round(spd * 1.150, 1) else --conversion from KNOTS to KPH spd = round(spd * 1.852, 1) end if spd > widgetToProcessInBackground.TopSpeed then widgetToProcessInBackground.TopSpeed = spd end -- Change the 'sh' on the next line to change the switch used for call-out -- if getValue('sh') > 800 then if getValue(widgetToProcessInBackground.options.switch) > 800 then --l shems: used the option if getTime() > widgetToProcessInBackground.block + 100 then widgetToProcessInBackground.TopSpeed = 0 end if not widgetToProcessInBackground.announced then if widgetToProcessInBackground.options.units == 0 then playNumber(widgetToProcessInBackground.TopSpeed * 10, UNIT_MPH, PREC1) else playNumber(widgetToProcessInBackground.TopSpeed * 10, UNIT_KMH, PREC1) end widgetToProcessInBackground.announced = true end else widgetToProcessInBackground.announced = nil widgetToProcessInBackground.block = getTime() end end end --[[ l Shems: split over the widget functions function updateSpeed(widget) local TelID = getTelemetryId("GSpd") x = widget.zone.x y = widget.zone.y if TelID == -1 then lcd.drawText(x, y, "No Speed Sensor",MIDSIZE) else local spd = getValue(TelID) --conversion from KNOTS to MPH if Units == "mph" then spd = round(spd * 1.150, 1) else --conversion from KNOTS to KPH spd = round(spd * 1.852, 1) end if spd > TopSpeed then TopSpeed = spd end -- Change the 'sh' on the next line to change the switch used for call-out if getValue('sh') > 800 then if getTime() > block + 300 then block = getTime() if Units == "mph" then playNumber(TopSpeed * 10, UNIT_MPH, PREC1) else playNumber(TopSpeed * 10, UNIT_KMH, PREC1) end end end lcd.drawText(x,y,"Top Speed", SMLSIZE) lcd.drawText(x,y+13,TopSpeed..Units,MIDSIZE) end end --]] local function refreshWidget(widgetToRefresh) -- updateSpeed(widgetToRefresh) if widgetToRefresh.TelID == -1 then lcd.drawText(widgetToRefresh.zone.x, widgetToRefresh.zone.y, "No Speed Sensor",MIDSIZE) else lcd.drawText(widgetToRefresh.zone.x, widgetToRefresh.zone.y,"Top Speed", SMLSIZE) if widgetToRefresh.options.units == 0 then lcd.drawText(widgetToRefresh.zone.x, widgetToRefresh.zone.y+13,widgetToRefresh.TopSpeed.."MPH",MIDSIZE) else lcd.drawText(widgetToRefresh.zone.x, widgetToRefresh.zone.y+13,widgetToRefresh.TopSpeed.."KPH",MIDSIZE) end end backgroundProcessWidget(widgetToRefresh) end return { name="Top-Speed", options=defaultOptions, create=createWidget, update=updateWidget, refresh=refreshWidget, background=backgroundProcessWidget }