-- ########################################################################################################################################################## -- # TXBattMan # -- # Lua app for Jeti DS-12, DC-16 II, DS/DC-24 transmitters # -- ########################################################################################################################################################## -- ########################################################################################################################################################## -- # # -- # MIT License # -- # # -- # copyright (c) 2021 Morote │ Jetiforum.de # -- # ------------------------------------------------------------------------------------------------------------------------------------------------------ # -- # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), # -- # I to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, # -- # and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # -- # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # -- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # -- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, # -- # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE # -- # USE OR OTHER DEALINGS IN THE SOFTWARE. # -- ########################################################################################################################################################## -- declarations local appName, appVersion, appAuthor = "TXBattMan", "0.2", "Morote" local voltage, capacity, capacity_relative, capacity_used, capacity_nominal = 0, 0, 0, 0, 9999 local runtime_remaining_full, runtime_remaining_h, runtime_remaining_min, runtime_remaining_string = 0, 0, 0, "" local alarm_audio local current, current_smoothed, current_interval, current_interval_sum, current_interval_length, current_interval_length_max = 0, 0, {}, 0, 0, 50 local counter, data, alarm_state, alarm_threshold, alarm_switch, read, f, value local componentIndex1, componentIndex2 local config, file, json_table, json_text, locale, dic, font_size, pos_x, pos_y -- read translations local function get_translations() locale = system.getLocale() file = io.readall("Apps/TXBattMan/locale.jsn") dic = json.decode(file) if dic[locale] == nil then locale = "en" file = io.readall("Apps/TXBattMan/locale.jsn") dic = json.decode(file) end end -- load settings from config.jsn local function load_config() file = io.readall("Apps/TXBattMan/config.jsn") config = json.decode(file) alarm_switch = config[1]["alarm_switch"] alarm_audio = config[2]["alarm_audio"] alarm_threshold = config[3]["alarm_threshold"] alarm_vibrate = config[4]["alarm_vibrate"] capacity_nominal = config[5]["capacity_nominal"] end -- write settings to config.jsn local function write_config() json_table = {{alarm_switch = tostring(alarm_switch)}, {alarm_audio = tostring(alarm_audio)}, {alarm_threshold = alarm_threshold}, {alarm_vibrate = tostring(alarm_vibrate)}, {capacity_nominal = capacity_nominal} } json_text = json.encode(json_table) f = io.open("Apps/TXBattMan/config.jsn", "w") io.write(f, json_text) io.close(f) end -- define functions of bottom line buttons local function keyPressed(key) -- go to/refresh status page if key == KEY_1 then form.reinit(1) -- go to settings page elseif (key == KEY_2 and formID ~= 2) then form.reinit(2) end end -- returns the sum of a table local function sum_of_table(value) counter = 0 for i,v in ipairs(value) do counter = counter + v end return counter end -- returns a number rounded to a defined amount of decimals local function round(value, decimals) local mult = 10^(decimals or 0) return math.floor(value * mult + 0.5) / mult end -- print dialog box for "low TX battery" warning local function print_alarm_dialogue() system.messageBox(dic[locale]["alarm_text"], 5) end -- activate vibration warning local function vibrate() system.vibration(true, 3) system.vibration(not true, 3); end -- raise audio warning local function play_alarm_audio(value) system.playFile(value, AUDIO_IMMEDIATE) end -- actualize capacity_nominal upon user input: in-script variable, config.jsn local function actualize_capacity_nominal(value) capacity_nominal = value write_config() end -- actualize alarm_threshold upon user input: in-script variable, config.jsn local function actualize_alarm_threshold(value) alarm_threshold = value write_config() end -- actualize alarm_switch upon user input: in-script variable, config.jsn local function actualize_alarm_switch(value) alarm_switch = not value form.setValue(componentIndex1, alarm_switch) write_config() end -- actualize alarm_audio upon user input: in-script variable, config.jsn local function actualize_audio_file(value) alarm_audio = value write_config() end -- actualize alarm_vibrate upon user input: in-script variable, config.jsn local function actualize_alarm_vibrate(value) alarm_vibrate = not value form.setValue(componentIndex2, alarm_vibrate) write_config() end -- draw telemetry window local function printWindow(width, height) -- settings for position beneath battery symbol if height == 30 then if string.sub(runtime_remaining_string, -1) == "h" then pos_x = 30 elseif string.sub(runtime_remaining_string, -1) == "-" then pos_x = 50 else pos_x = 5 end pos_y = 5 font_size = FONT_BOLD -- settings for small telemetry window elseif height == 24 then pos_x = 7 pos_y = 2 font_size = FONT_NORMAL -- settings for large telemetry window else pos_x = 7 pos_y = 12 font_size = FONT_MAXI end lcd.drawText(pos_x, pos_y, runtime_remaining_string, font_size) end -- draw status page local function draw_status_page() form.addLabel({label = dic[locale]["actual_values"], font = FONT_BOLD}) form.addLabel({label = dic[locale]["capacity"] .. tostring((math.floor(capacity))) .. " mAh" .. " (" .. tostring(capacity_relative) .. " %)"}) form.addLabel({label = tostring(voltage) .. " V @ " .. tostring(current) .. " mA"}) if current_smoothed < 0 then form.addLabel({label = dic[locale]["charging"], font = FONT_BOLD}) else form.addLabel({label = dic[locale]["estimated_remaining_runtime"] .. runtime_remaining_string, font = FONT_BOLD}) end end -- draw settings page local function draw_settings_page() form.addLabel({label = dic[locale]["settings"], font = FONT_BOLD}) form.addRow(2) form.addLabel({label = dic[locale]["capacity_in_mAh"]}) form.addIntbox(capacity_nominal, 100, 9900, 6200, 0, 10, actualize_capacity_nominal) form.addRow(2) form.addLabel({label = dic[locale]["runtime_alarm"], width = 280}) if (alarm_switch == "true") then componentIndex1 = form.addCheckbox(true, actualize_alarm_switch) else componentIndex1 = form.addCheckbox(false, actualize_alarm_switch) end form.addRow(2) form.addLabel({label = dic[locale]["time_in_min"]}) form.addIntbox(alarm_threshold, 5, 180, 60, 0, 5, actualize_alarm_threshold) form.addRow(2) form.addLabel({label = dic[locale]["select_audio_file"]}) form.addAudioFilebox(alarm_audio or "", actualize_audio_file) form.addRow(2) form.addLabel({label = dic[locale]["use_vibration"], width = 280}) if (alarm_vibrate == "true") then componentIndex2 = form.addCheckbox(true, actualize_alarm_vibrate) else componentIndex2 = form.addCheckbox(false, actualize_alarm_vibrate) end end local function initForm(subform) form.setButton(1 ,":refresh", ENABLED) form.setButton(2, ":tools", subform == 2 and HIGHLIGHTED or ENABLED) formID = subform -- call draw_status_page function if (subform == 1) then draw_status_page() -- call draw_settings_page function elseif (subform == 2) then draw_settings_page() end end -- initialize app local function init() -- get system language get_translations() -- load configuration from config.jsn load_config() -- reset alarm_state alarm_state = 0 form.setValue(componentIndex1, alarm_switch) form.setValue(componentIndex2, alarm_vibrate) system.registerForm(1, MENU_SYSTEM, appName, initForm, keyPressed, printForm) system.registerTelemetry(2, dic[locale]["remaining_TX_runtime"], 0, printWindow) end -- runtime functions local function loop() -- get TX battery status data = system.getTxTelemetry() voltage = data.txVoltage -- in V capacity_relative = data.txBattPercent -- in % current = data.txCurrent -- in mA --current = current - 500 -- for testing & debugging with the emulator [state "charging"] --current = current + 500 -- for testing & debugging with the emulator [state "draining battery"] -- calculate remaining capacity capacity = capacity_nominal * (capacity_relative / 100) -- in mAh -- add actual current readout to table table.insert(current_interval, current) -- determine number of elements in table current_interval_length = #current_interval -- if number of elements in table above threshold, remove first element and set number of elements to threshold value if current_interval_length > current_interval_length_max then local discard = table.remove(current_interval, 1) current_interval_length = current_interval_length_max end -- calculate sum of table elements current_interval_sum = sum_of_table(current_interval) -- calculate smoothed current as average of table elements current_smoothed = current_interval_sum / current_interval_length -- in mA -- calculate remaining runtime if current_smoothed == 0 then runtime_remaining_string = "--:--" elseif current_smoothed < 0 then runtime_remaining_string = dic[locale]["charge"] else runtime_remaining_full = capacity / current_smoothed -- in h runtime_remaining_h = tostring(math.floor(capacity / current_smoothed)) -- in h runtime_remaining_min = math.floor(((runtime_remaining_full - runtime_remaining_h) * 60) / 1) -- in min if runtime_remaining_min < 10 then runtime_remaining_min = "0" .. tostring(runtime_remaining_min) else runtime_remaining_min = tostring(runtime_remaining_min) end runtime_remaining_string = runtime_remaining_h .. ":" .. runtime_remaining_min .. " h" end -- check if alarms are activated if alarm_switch == "true" then -- check if alarm has already been raised if alarm_state == 0 then -- check if TX draining battery if current > 0 then -- check if remaining runtime below threshold if runtime_remaining_full < (alarm_threshold / 60) then alarm_state = 1 print_alarm_dialogue() -- check/raise vibration alarm if alarm_vibrate == "true" then vibrate() end -- check/raise audio alarm if alarm_audio then play_alarm_audio(alarm_audio) end end end end end end -- return collectgarbage() return {init = init, loop = loop, author = appAuthor, version = appVersion, name = appName}