local poweredClimb = 0 local glideClimb = 0 local lastAltitude local playedAlarm = false local engineSwitch local resetSwitch local alarmAlt local altSensorIndex local sensorIDs local sensorParams local sensorLabels local function onEngineSwitchChanged(value) engineSwitch = system.getInputsVal(value) ~= 0 and value or nil system.pSave("enginesw", engineSwitch) end local function onAlarmAltChanged(value) alarmAlt = value system.pSave("alarmAlt", alarmAlt) end local function onResetSwitchChanged(value) resetSwitch = system.getInputsVal(value) ~= 0 and value or nil system.pSave("resetsw", resetSwitch) end local function onAltSensorIndexChanged(value) altSensorIndex = value - 1 system.pSave("altidx", altSensorIndex) end local function initForm() form.addRow(2) form.addLabel({ label = "Engine" }) form.addInputbox(engineSwitch, false, onEngineSwitchChanged) form.addRow(2) form.addLabel({ label = "Alarm" }) form.addIntbox(alarmAlt, 1, 20000, 100, 0, 1, onAlarmAltChanged) form.addRow(2) form.addLabel({ label = "Altitude sensor" }) form.addSelectbox(sensorLabels, altSensorIndex + 1, true, onAltSensorIndexChanged) form.addRow(2) form.addLabel({ label = "Reset" }) form.addInputbox(resetSwitch, false, onResetSwitchChanged) end local function init() sensorIDs = {} sensorParams = {} sensorLabels = {"..."} for _,sensor in ipairs(system.getSensors()) do if sensor.param ~= 0 and sensor.type ~= 5 and sensor.type ~= 9 then sensorIDs[#sensorIDs+1] = sensor.id sensorParams[#sensorParams+1] = sensor.param sensorLabels[#sensorLabels+1] = string.format("%s: %s [%s]", sensor.sensorName, sensor.label, sensor.unit) end end engineSwitch = system.pLoad("enginesw") resetSwitch = system.pLoad("resetsw") alarmAlt = system.pLoad("alarmAlt", 100) altSensorIndex = system.pLoad("altidx", 0) if altSensorIndex > #sensorIDs then altSensorIndex = 0 end system.registerForm(1, MENU_APPS, "Calculated Capacity", initForm) end local function loop() if system.getInputsVal(resetSwitch) == 1 then poweredClimb = 0 glideClimb = 0 playedAlarm = false end local altitude = altSensorIndex ~= 0 and system.getSensorValueByID(sensorIDs[altSensorIndex], sensorParams[altSensorIndex]) or nil if altitude and altitude.valid and lastAltitude then if system.getInputsVal(engineSwitch) == 1 and altitude.value > lastAltitude then poweredClimb = poweredClimb + altitude.value - lastAltitude if poweredClimb >= alarmAlt and not playedAlarm then -- ALARM system.playBeep(3, 1000, 600) playedAlarm = true end elseif system.getInputsVal(engineSwitch) == -1 and altitude.value > lastAltitude then glideClimb = glideClimb + altitude.value - lastAltitude end end lastAltitude = (altitude and altitude.valid) and altitude.value or nil end return { init = init, loop = loop, author = "LeonAir RC", version = "1.0.0", name = "Calculated Capacity" }