collectgarbage() local model = " " local selected_sensor = 0 local sensorsAvailable local measure = false local measure_switch = 0 local integral_switch = 0; local app_name = "Eigensinken" local sensorId, vario_param, altitude_param local sensor_label = "" local timestamp, diff_time local vario, min_vario, max_vario, vario_integral = 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_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 = sensorsAvailable[value].id system.pSave("sensorId", sensorId) 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 available = system.getSensors() local sensor_label_list = {} local parameter_list = {} local sensorIndex=-1 local paramIndex=-1 local multisensors = {} sensorsAvailable = {} -- scope: this script for index,sensor in ipairs(available) 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 sensorsAvailable[#sensorsAvailable + 1] = sensor -- to get id from this header table, it has same index as list if(sensor.id==sensorId ) then -- mem index of sensorIndex=#sensorsAvailable -- already selected entry for preselect in selectbox end --multisensorIndex = multisensorIndex + 1 multisensors[#multisensors + 1] = {} else multisensors[#multisensors][sensor.param] = sensor.label .. " " .. sensor.unit -- list presented in select box end end end form.setTitle("Telemetrieeinstellungen") form.addRow(1) form.addLabel({label="Select Sensor and Parameter",font=FONT_BOLD}) form.addRow(2) form.addLabel({label = "Sensor", width=200}) form.addSelectbox(sensor_label_list, sensorIndex, true, sensorChanged) --form.addSpacer(318,7) if ( sensorsAvailable and sensorIndex > 0 ) then form.addRow(2) form.addLabel({label = "Parameter Vario", width=200}) form.addSelectbox(multisensors[sensorIndex], vario_param, true, paramVarioChanged) form.addRow(2) form.addLabel({label = "Parameter Höhe", width=200}) form.addSelectbox(multisensors[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 2 Sek. integriert An", width=220}) form.addInputbox(integral_switch,true,integral_switchChanged) form.addSpacer(318,7) form.addLabel({label="Eigensinken " .. Version .. " ", font=FONT_MINI, visible=true, alignRight=true}) collectgarbage() end function average(value) -- integral vario helper local sum_varios = 0 local vario local i if ( #vario_list == 200 ) then table.remove(vario_list, 1) end vario_list[#vario_list + 1] = value for i,vario in ipairs(vario_list) do sum_varios = sum_varios + vario end collectgarbage() return sum_varios / #vario_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 = 100 max_vario = -100 vario_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 = sensor.value --vario = 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 = average(vario) -- average integral vario over 200 samples with 1/100 sencond period ( ie. 2 Sec ) last_integral_time = avr_timestamp end if (vario_integral > max_vario ) then max_vario = vario_integral end if (vario_integral < min_vario ) then min_vario = vario_integral end else if (vario > max_vario ) then max_vario = vario end if (vario < min_vario ) then min_vario = vario 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 = (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 flight[flight_index]["max"] = max_vario flight[flight_index]["average"] = average_vario 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") --selected_sensor = system.pLoad("selected_sensor", 0) sensor_label = system.pLoad("sensor_label", "") system.registerForm(1, MENU_APPS, app_name, setupForm) system.registerTelemetry(1, app_name .. " " .. model .. " " .. sensor_label, 4, Page1) collectgarbage() end Version = "1.1" collectgarbage() return {init=init, loop=loop, author="nichtgedacht", version=Version, name=app_name}