Willkommen, Gast
Benutzername: Passwort: Angemeldet bleiben:

THEMA:

Default Wert für Schalterbelegung in LUA 19 Dez 2022 16:22 #13

  • davidmcq137
  • davidmcq137s Avatar
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Beiträge: 75
  • Dank erhalten: 80
thanks. I will write up a little test program that hopefully will isolate the interesting parts and make this clear.
Folgende Benutzer bedankten sich: sierra_uniform

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Default Wert für Schalterbelegung in LUA 19 Dez 2022 17:00 #14

  • dit71
  • dit71s Avatar
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Beiträge: 157
  • Dank erhalten: 79
You can use use the createswitch command also for default switchitems, you know the name and you just have to figure out once which is the activeone value and the modifiers value, then if you like you can define the switches hard coded in the app. Otherwise I don't unterstand the problem.

The difficult problem was to find the correct activeon value during selecting the switch.

Dieter

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Default Wert für Schalterbelegung in LUA 19 Dez 2022 17:20 #15

  • davidmcq137
  • davidmcq137s Avatar
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Beiträge: 75
  • Dank erhalten: 80
OK .. let me summarize what is going on as I see it .. let's see if we agree or not...

1) as far as any of us knows, it is not possible to set a default for an _actual_ switchitem (the type returned by form.addInputbox())

2) You cannot serialize an actual switchitem .. any switchitem userdata in a call to json.encode() is an error (I checked again). Of course, Jeti does do a special case for pLoad and pSave for handling the userdata "real" switchitems.

So, if you want to solve problem 1 or problem 2 you need another approach. The approach I believe you are using, and I am also using in my maps app, is the create some "soft switchitem" data .. basically the table needed for system.createSwitch(). This works fine and solves problem 1 and 2.

But .. it means you cannot ALSO use form.addInputbox which is the familiar process for pilots. If you try to mix the two approaches, and let the pilot define a switch with form.addInputbox, then extract the "soft switchitem" data using system.getSwitchInfo() you run into the +/-1 problem. In summary ... addInputbox returns the +1 direction as whatever direction the pilot flipped the switch . .which could be up or down or middle .. and there is no way to know what that direction was. So while you can call getSwitchInfo, you won't know the active direction.

I've tried very hard to solve that and I cannot. If you are solving this in your app please show me where .. I did not see it .. I tried!

So .. you can go "native Jeti" and just use pSave and pCall as the persistence model and have no way to default .. and a weak persistence model .. or .. you can use a json file as the persistence model, and create and serialize your own "soft switchitems" and solve both .. but you will end up with some menuapproach to assign the switches which will work fine but won't be the familiar addInputbox user interface.

What do you think?

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Default Wert für Schalterbelegung in LUA 19 Dez 2022 18:30 #16

  • dit71
  • dit71s Avatar
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Beiträge: 157
  • Dank erhalten: 79


using system.getSwitchInfo() you run into the +/-1 problem. In summary ... addInputbox returns the +1 direction as whatever direction the pilot flipped the switch . .which could be up or down or middle .. and there is no way to know what that direction was. So while you can call getSwitchInfo, you won't know the active direction.

Here is the solution:

local function switchChanged(switchName, value)
local Invert = 1.0
local swInfo = system.getSwitchInfo(value)
local swTyp = string.sub(swInfo.label,1,1)
if swInfo.assigned then
if string.sub(swInfo.mode,-1,-1) == "I" then Invert = -1.0 end
if swInfo.value == Invert or swTyp == "L" or swTyp =="M" then
vars.switches[switchName] = value
system.pSave(switchName, value)
form.setButton(1, "save",ENABLED)
vars.changedConfig = 1
vars.switchInfo[switchName] = {}
vars.switchInfo[switchName].name = swInfo.label
vars.switchInfo[switchName].mode = swInfo.mode
if swTyp == "L" or swTyp =="M" then
vars.switchInfo[switchName].activeOn = 0
else
vars.switchInfo[switchName].activeOn = system.getInputs(string.upper(swInfo.label))
end
else
system.messageBox(vars.trans.switchError, 3)
if vars.switches[switchName] then
form.setValue(switch[switchName],vars.switches[switchName])
else
form.setValue(switch[switchName],nil)
end
end
else
if vars.switchInfo[switchName] then
vars.switches[switchName] = nil
vars.switchInfo[switchName] = nil
form.setButton(1, "save",ENABLED)
system.pSave(switchName, nil)
vars.changedConfig = 1
end
end
collectgarbage()
end

It is not true that you always get +1 from the addinputbox, you get -1 if he moved the switch two times or he choosed inverted. If he choose "inverted" everything is okay, you got the information. If you get -1 and he didn't invert then he moved the stick twice, I don't allow that, because if you have a 3-position switch you don't know in which position the switch is now, with a 2-position switch you woulld be fine - of course it can only be on the other side.
With getswitchinfo you get if he has moved the stick or not, and you can be sure if not it is now on the position he has selected. Finally all you have to do is system.getinputs to get if it is -1, 0, or +1 Then you have everything you need and can restore the switch.
I hope it is unterstandible how it works.
Dieter

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Default Wert für Schalterbelegung in LUA 19 Dez 2022 18:34 #17

  • dit71
  • dit71s Avatar
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Beiträge: 157
  • Dank erhalten: 79
Sorry the format of the code is gone, thats hard to read, but you will find it in the form.lua

Dieter

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Default Wert für Schalterbelegung in LUA 19 Dez 2022 18:59 #18

  • davidmcq137
  • davidmcq137s Avatar
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Beiträge: 75
  • Dank erhalten: 80
I'll give it a try, thanks! I guess you had the solution all along and I did not see it.
On the "moved the switch twice" .. how do you "not allow it"? You just document it as "don't do that"?

Bitte Anmelden oder Registrieren um der Konversation beizutreten.

Moderatoren: ThornIG-Modellbau
Ladezeit der Seite: 0.238 Sekunden
Powered by Kunena Forum