Willkommen, Gast
Benutzername: Passwort: Angemeldet bleiben:
  • Seite:
  • 1

THEMA:

conditional form? 04 Apr 2018 06:41 #1

  • abief
  • abiefs Avatar Autor
  • Offline
  • Expert Boarder
  • Expert Boarder
  • I'm here, there and elsewhere!
  • Beiträge: 95
  • Dank erhalten: 19
Hi,

can somebody provide a sample on how to build a conditional form?

My code:

local function setupForm(formId)
-- load sensors
sensorsAvailable = {}
local available = system.getSensors();
local formSensors={}
local formSensorsIndex=-1
for index,sensor in ipairs(available) do
if(sensor.param == 0 and
(string.sub(sensor.label, 1, 3) == _MUI or
string.sub(sensor.label, 1, 5) == _MEZON)
) then
formSensors[#formSensors+1] = sensor.label
sensorsAvailable[#sensorsAvailable+1] = sensor
if(sensor.id==sensorId ) then
formSensorsIndex=#sensorsAvailable
end
end
end

-- form title
form.setTitle(locale.SetupFormTitle);
-- spacer
form.addSpacer(318,7);
-- sensor (MUI or MEZON)
form.addRow(2);
form.addLabel({label = locale.SetupFormSelectSensorLabel, width = 200});
form. addSelectbox(formSensors, formSensorsIndex, true, sensorChanged);
-- only for MEZON, we add the gearbox ratio
if (sensorMode == _MEZ) then
form.addRow(2);
form.addLabel({label = locale.SetupFormGearBoxValueLabel, width = 200});
form.addIntbox(revolutionsGearboxRatio, 100, 1000, 100, 0, 1, gearboxRatioChanged);
end
-- battery
form.addRow(2);
form.addLabel({label = locale.SetupFormSelectBatteryLabel, width = 200});
form.addIntbox(batteryCapacity, 0, 32000, 0, 0, 10, batteryChanged);
-- spacer
form.addSpacer(318,7);
-- gates
form.addRow(3);
form.addLabel({label = locale.SetupFormRedGateLabel, width = 150});
form.addIntbox(redGate, 0, 100, _REDGATE, 0, 1, redGateChanged);
form.addAudioFilebox(alarmRedGate, alarmRedGateChanged);
form.addRow(3);
form.addLabel({label = locale.SetupFormAmberGateLabel, width = 150});
form.addIntbox(amberGate, 0, 100, _AMBERGATE, 0, 1, amberGateChanged);
form.addAudioFilebox(alarmAmberGate, alarmAmberGateChanged);
form.addRow(3);
form.addLabel({label = locale.SetupFormBlueGateLabel, width = 150});
form.addIntbox(blueGate, 0, 100, _BLUEGATE, 0, 1, blueGateChanged);
form.addAudioFilebox(alarmBlueGate, alarmBlueGateChanged);
collectgarbage();
end

-- print Configuration form
local function printForm(formId)
end

My idea is that if sensorMode is Mezon, then the gearbox ratio parameter shows up, if not, this gearbox ratio is not used.

but I'm not able to make this field to disappear when sensorMode is not mezon.

Suggestions??

Thanks,
Field ready: Taborca (3.4m), Ocelot (1.6m), Alula (0.6m).
Factory line: FVT-3 (1m), A6M Zero (1.6m), Tiger Moth (2.2m), Grumann Goose (2m) and Beechcraft D18 (1.5m).
Design phase: Szd-20x Wampir II (7.5m), Rutan Quickie (1.6m), both parked.
Dreaming: Weird push-pull called Double Trouble (2.4m)

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

conditional form? 07 Apr 2018 11:44 #2

  • TeroS
  • TeroSs Avatar
  • Offline
  • Platinum Boarder
  • Platinum Boarder
  • RC-Thoughts
  • Beiträge: 527
  • Dank erhalten: 370
Please put the code inside code tags so it's more readable.
[code type="" ] [/code ]
(Remove spaces before the "]", or more simply, use the <> button in editors buttonline above)

There have been issues with this forums colors with code tag, I hope they will get it fixed sometime...

But an answer, you should look in reinit function and use some if-statements.

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Letzte Änderung: von TeroS.

conditional form? 07 Apr 2018 11:47 #3

  • TeroS
  • TeroSs Avatar
  • Offline
  • Platinum Boarder
  • Platinum Boarder
  • RC-Thoughts
  • Beiträge: 527
  • Dank erhalten: 370
So here's your code:
local function setupForm(formId)
    -- load sensors
    sensorsAvailable = {}
    local available = system.getSensors();
    local formSensors={}
    local formSensorsIndex=-1
    for index,sensor in ipairs(available) do
        if(sensor.param == 0 and
            (string.sub(sensor.label, 1, 3) == _MUI or
            string.sub(sensor.label, 1, 5) == _MEZON)
        ) then
        formSensors[#formSensors+1] = sensor.label
        sensorsAvailable[#sensorsAvailable+1] = sensor
        if(sensor.id==sensorId ) then
            formSensorsIndex=#sensorsAvailable
        end
        end
    end
 
    -- form title
    form.setTitle(locale.SetupFormTitle);
    -- spacer
    form.addSpacer(318,7);
    -- sensor (MUI or MEZON)
    form.addRow(2);
    form.addLabel({label = locale.SetupFormSelectSensorLabel, width = 200});
    form. addSelectbox(formSensors, formSensorsIndex, true, sensorChanged);
    -- only for MEZON, we add the gearbox ratio
    if (sensorMode == _MEZ) then
        form.addRow(2);
        form.addLabel({label = locale.SetupFormGearBoxValueLabel, width = 200});
        form.addIntbox(revolutionsGearboxRatio, 100, 1000, 100, 0, 1, gearboxRatioChanged);
    end
    -- battery
    form.addRow(2);
    form.addLabel({label = locale.SetupFormSelectBatteryLabel, width = 200});
    form.addIntbox(batteryCapacity, 0, 32000, 0, 0, 10, batteryChanged);
    -- spacer
    form.addSpacer(318,7);
    -- gates
    form.addRow(3);
    form.addLabel({label = locale.SetupFormRedGateLabel, width = 150});
    form.addIntbox(redGate, 0, 100, _REDGATE, 0, 1, redGateChanged);
    form.addAudioFilebox(alarmRedGate, alarmRedGateChanged);
    form.addRow(3);
    form.addLabel({label = locale.SetupFormAmberGateLabel, width = 150});
    form.addIntbox(amberGate, 0, 100, _AMBERGATE, 0, 1, amberGateChanged);
    form.addAudioFilebox(alarmAmberGate, alarmAmberGateChanged);
    form.addRow(3);
    form.addLabel({label = locale.SetupFormBlueGateLabel, width = 150});
    form.addIntbox(blueGate, 0, 100, _BLUEGATE, 0, 1, blueGateChanged);
    form.addAudioFilebox(alarmBlueGate, alarmBlueGateChanged);
    collectgarbage();
end
 
-- print Configuration form
local function printForm(formId)
end

With a quick look I don't see why that would not work with some adjustment. Where and when do you set the "sensorMode" to "_MEZ"? Have a look in LUA API for "form.reinit" :)
Folgende Benutzer bedankten sich: abief

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Letzte Änderung: von TeroS.

conditional form? 07 Apr 2018 12:17 #4

  • abief
  • abiefs Avatar Autor
  • Offline
  • Expert Boarder
  • Expert Boarder
  • I'm here, there and elsewhere!
  • Beiträge: 95
  • Dank erhalten: 19
Ok, I solved it myself.

I'm not sure if this is the right solution, but it works.

In my case, the reason why I need the form to add a parameter when I use a MEZON sensor, to determine the gearbox setting.

Basically, in the setup form I want to be able to switch from



to



I have a variable, called sensorMode, that determines if the sensor is a MUI or a MEZON. If Mezon is active, there is a revolutions parameter that I need to determine and apply the gearbox reduction in order to obtain the actual propeller revolutions.

first, I added a local variable at the start of the lua code:
local previousSensorMode = sensorMode; -- to change the form

second, I slightly change the code in my setup form. so to skip the gearbox field if sensor mode is not MEZON...
    -- only for MEZON, we add the gearbox ratio
    if (sensorMode == _MEZ) then
        form.addRow(2);
        form.addLabel({label = locale.SetupFormGearBoxValueLabel, width = 200});
        form.addIntbox(revolutionsGearboxRatio, 100, 1000, 100, 2, 1, gearboxRatioChanged);
        form.addSpacer(318,7);
    end

now, as this code is present in the setupform function, it's only called once, when we setup the form.

So, I added some code also the the sensorChanged function (that is, the code associated to the change of sensors between MUI and MEZON).
local function sensorChanged(value)
    previousSensorMode = sensorMode;
    -- some more code here
end

this way I know what was the previous value of sensormode.

Lastly, I add a function to my code, and that is the printform

I change my definition of the setup of the form by adding the proper calls
    system.registerForm(1, MENU_APPS, locale.AppName, setupForm, nil, printForm);

and I complete the printform function.

this is called as the loop, every 22ms...
-- print Configuration form
local function printForm()
    if (sensorMode ~= previousSensorMode) then
				 previousSensorMode = sensorMode;
        form.reinit();
    end
end

the important line is
        form.reinit();

this line basically respawns the initform code, so the form is effectively regenerated.

hope it helps
Field ready: Taborca (3.4m), Ocelot (1.6m), Alula (0.6m).
Factory line: FVT-3 (1m), A6M Zero (1.6m), Tiger Moth (2.2m), Grumann Goose (2m) and Beechcraft D18 (1.5m).
Design phase: Szd-20x Wampir II (7.5m), Rutan Quickie (1.6m), both parked.
Dreaming: Weird push-pull called Double Trouble (2.4m)
Anhänge:

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Letzte Änderung: von abief.

conditional form? 07 Apr 2018 12:28 #5

  • abief
  • abiefs Avatar Autor
  • Offline
  • Expert Boarder
  • Expert Boarder
  • I'm here, there and elsewhere!
  • Beiträge: 95
  • Dank erhalten: 19

So here's your code:

--

With a quick look I don't see why that would not work with some adjustment. Where and when do you set the "sensorMode" to "_MEZ"? Have a look in LUA API for "form.reinit" :)


thanks, we arrived to the same conclusion!
Field ready: Taborca (3.4m), Ocelot (1.6m), Alula (0.6m).
Factory line: FVT-3 (1m), A6M Zero (1.6m), Tiger Moth (2.2m), Grumann Goose (2m) and Beechcraft D18 (1.5m).
Design phase: Szd-20x Wampir II (7.5m), Rutan Quickie (1.6m), both parked.
Dreaming: Weird push-pull called Double Trouble (2.4m)

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

  • Seite:
  • 1
Moderatoren: ThornIG-Modellbau
Ladezeit der Seite: 0.270 Sekunden
Powered by Kunena Forum