collectgarbage() local model = " " local sensor_label_rows local measure = false local measure_switch = 0 local integral_switch = 0; local app_name = "Eigensinken" local sensorId, vario_param, altitude_param local timestamp, diff_time local vario_value, min_vario_value, max_vario_value, vario_integral_value = 0.0, 0.0, 0.0, 0.0 local altitude_start, altitude_end = 0, 0 local flight = {} local flight_num, flight_index = 0, 0 local last_integral_time = 0 local vario_value_list = {} local function Page1(width, height) if( system.getTime() % 2 == 0 and measure ) then lcd.drawText(1, 1, " Flugnr. Zeit MinVar MaxVar ØSteigen", FONT_BOLD|FONT_REVERSED ) else lcd.drawText(1, 1, " Flugnr. Zeit MinVar MaxVar ØSteigen", FONT_BOLD ) end if ( #flight ) then for i = 1, #flight do lcd.drawText (40 - lcd.getTextWidth (FONT_NORMAL, string.format("%3d", flight[i]["num"] )), flight[i]["line_offs"], string.format("%3d",flight[i]["num"]), FONT_NORMAL ) lcd.drawText (105 - lcd.getTextWidth (FONT_NORMAL, string.format("%4.2f", flight[i]["time"] )), flight[i]["line_offs"], string.format("%4.2f",flight[i]["time"]), FONT_NORMAL ) lcd.drawText (170 - lcd.getTextWidth (FONT_NORMAL, string.format("%3.2f", flight[i]["min"] )), flight[i]["line_offs"], string.format("%3.2f",flight[i]["min"]), FONT_NORMAL ) lcd.drawText (235 - lcd.getTextWidth (FONT_NORMAL, string.format("%3.2f", flight[i]["max"] )), flight[i]["line_offs"], string.format("%3.2f",flight[i]["max"]), FONT_NORMAL ) lcd.drawText (300 - lcd.getTextWidth (FONT_NORMAL, string.format("%3.2f", flight[i]["average"] )), flight[i]["line_offs"], string.format("%3.2f",flight[i]["average"]), FONT_NORMAL ) end end end local function sensorChanged(value) sensorId = sensor_label_rows[value].id system.pSave("sensorId", sensorId) vario_param = 1 -- prevent error if previous index was higher than possible in this new sensor altitude_param = 1 -- prevent error if previous index was higher than possible in this new sensor form.reinit() end local function paramVarioChanged(value) vario_param = value system.pSave("vario_param", vario_param) end local function paramAltitudeChanged(value) altitude_param = value system.pSave("altitude_param", altitude_param) end local function measure_switchChanged(value) measure_switch = value system.pSave("measure_switch", measure_switch) end local function integral_switchChanged(value) integral_switch = value system.pSave("integral_switch", integral_switch) end local function setupForm(formID) local sensor_label_list = {} local sensorIndex=-1 local sensor_parm_lists = {} local all_sensor_rows = system.getSensors() sensor_label_rows = {} -- scope: this script for index,sensor in ipairs(all_sensor_rows) do if ( not ( sensor.type == 5 or sensor.type == 9 ) ) then -- no GPS no Date Time used if ( sensor.param == 0) then sensor_label_list[#sensor_label_list + 1] = sensor.label -- list presented in select box sensor_label_rows[#sensor_label_rows + 1] = sensor -- to get id from this header table, it has same index as list if(sensor.id==sensorId ) then -- mem index of sensorIndex=#sensor_label_rows -- already selected entry for preselect in selectbox end sensor_parm_lists[#sensor_parm_lists + 1] = {} -- start new param list only containing label and unit as string else -- subscript is number of param for current multisensor sensor_parm_lists[#sensor_parm_lists][sensor.param] = sensor.label .. " " .. sensor.unit -- list presented in select box end end end form.setTitle("Telemetrieeinstellungen") form.addRow(1) form.addLabel({label="Sensor- und Parameterwahl",font=FONT_BOLD}) form.addRow(2) form.addLabel({label = "Sensor", width=200}) form.addSelectbox(sensor_label_list, sensorIndex, true, sensorChanged) if ( sensor_label_rows and sensorIndex > 0 ) then form.addRow(2) form.addLabel({label = "Parameter Vario", width=200}) form.addSelectbox(sensor_parm_lists[sensorIndex], vario_param, true, paramVarioChanged) form.addRow(2) form.addLabel({label = "Parameter Höhe", width=200}) form.addSelectbox(sensor_parm_lists[sensorIndex], altitude_param, true, paramAltitudeChanged) end form.addSpacer(318,7) form.addRow(1) form.addLabel({label="Schalterwahl",font=FONT_BOLD}) form.addRow(2) form.addLabel({label="Messung An", width=220}) form.addInputbox(measure_switch,true,measure_switchChanged) form.addRow(2) form.addLabel({label="Min/Max 1 Sek. integriert An", width=220}) form.addInputbox(integral_switch,true,integral_switchChanged) form.addSpacer(318,7) form.addRow(1) form.addLabel({label="Eigensinken " .. Version .. " ", font=FONT_MINI, visible=true, alignRight=true}) collectgarbage() end function average(value) -- integral vario helper local sum_vario_values = 0 local vario local i if ( #vario_value_list == 100 ) then table.remove(vario_value_list, 1) end vario_value_list[#vario_value_list + 1] = value for i,vario_value in ipairs(vario_value_list) do sum_vario_values = sum_vario_values + vario_value end collectgarbage() return sum_vario_values / #vario_value_list end local function loop() local sensor local avr_timestamp if ( 1 == system.getInputsVal(measure_switch) ) then -- transition to measure state if ( measure == false ) then measure = true timestamp = system.getTimeCounter() sensor = system.getSensorValueByID(sensorId, altitude_param) if ( sensor and sensor.valid ) then --if ( sensor ) then --fake valid sensor altitude_start = sensor.value --altitude_start = system.getInputs ("P6") * 100 + 100 --fake altitude else altitude_start = 0.0 end min_vario_value = 100 max_vario_value = -100 vario_value_list = {} system.playBeep( 0, 1200, 500 ) end sensor = system.getSensorValueByID(sensorId, vario_param) if ( sensor and sensor.valid ) then --if ( sensor) then --fake valid sensor vario_value = sensor.value --vario_value = system.getInputs ("P8") * 10 --fake vario if ( 1 == system.getInputsVal(integral_switch) ) then avr_timestamp = system.getTimeCounter() if avr_timestamp >= (last_integral_time + 10) then -- one 1/100 second period vario_integral_value = average(vario_value) -- average integral vario over 100 samples with 1/100 sencond period ( ie. 1 Sec ) last_integral_time = avr_timestamp end if (vario_integral_value > max_vario_value ) then max_vario_value = vario_integral_value end if (vario_integral_value < min_vario_value ) then min_vario_value = vario_integral_value end else if (vario_value > max_vario_value ) then max_vario_value = vario_value end if (vario_value < min_vario_value ) then min_vario_value = vario_value end end else vario = 0.0 end else -- transition to non measure state if ( measure == true ) then measure = false diff_time = system.getTimeCounter() - timestamp sensor = system.getSensorValueByID(sensorId, altitude_param) if ( sensor and sensor.valid) then --if ( sensor ) then --fake valid sensor altitude_stop = sensor.value --altitude_stop = system.getInputs ("P6") * 100 + 100 --fake altitude else altitude_stop = 0.0 end average_vario_value = (altitude_stop - altitude_start) / ( diff_time / 1000 ) if ( flight_index == 8 ) then flight_index = 0 end flight_num = flight_num + 1 flight_index = flight_index + 1 flight[flight_index] = {} flight[flight_index]["num"] = flight_num flight[flight_index]["line_offs"] = flight_index * 17 + 4 flight[flight_index]["time"] = diff_time / 1000 flight[flight_index]["min"] = min_vario_value flight[flight_index]["max"] = max_vario_value flight[flight_index]["average"] = average_vario_value system.playBeep( 1, 1200, 500 ) end end collectgarbage() end local function init(code1) model = system.getProperty("Model") measure_switch = system.pLoad("measure_switch") integral_switch = system.pLoad("integral_switch") vario_param = system.pLoad("vario_param", 2) altitude_param = system.pLoad("altitude_param", 2) sensorId = system.pLoad("sensorId") system.registerForm(1, MENU_APPS, app_name, setupForm) system.registerTelemetry(1, app_name .. " " .. model, 4, Page1) collectgarbage() end Version = "1.2" collectgarbage() return {init=init, loop=loop, author="nichtgedacht", version=Version, name=app_name}