Jump to content

Recommended Posts

Yes, I am using hardware switches, but unfortunately, the LED-State and Changes are very often not simultaneously with switch manipulating. And some are fully independent!

So my plan is to build a script with a lot of functions in one (as Al described above) and set it to autorun.

Hope it will work, thank you very much for your assistance!

Guenter

Share this post


Link to post
Share on other sites

YESSS, now it runs!!

Let me ask one more question, before I begin to create a lot of single scripts. How must "my script" look like including not one, but several functions.

 

Share this post


Link to post
Share on other sites
19 minutes ago, gunterat said:

Let me ask one more question, before I begin to create a lot of single scripts. How must "my script" look like including not one, but several functions.

Again, it's not clear what you mean when you say 'several functions'. I am assuming by 'functions' you are referring to discrete actions (Lua has a 'function' keyword but this is just a detail of how you write your code). What do your functions (discrete actions) need to do and in what circumstances do you want them to be called? If they are all actually the same kind of thing - such as the one above but just monitoring different offsets - you can do all of those things in the same place:

while true do
    ipc.writeUB(0x66C0, ipc.readLvar("L:AB_OVH_APU_MASTER_LIGHT_ON"))
    ipc.writeUB(0x66C0, ipc.readLvar("L:TOILET_LIGHT_ON"))
    ipc.writeUB(0x66C0, ipc.readLvar("L:LANDING_LIGHT_ON"))
    -- etc.
    ipc.sleep(100)
end

You should think of a single Lua file as a complete program that is invoked either once (auto-start) or a number of discrete times (such as on a key-press event). In the auto-start case your program will generally need to continue running for the lifetime of your sim session and if it needs to do several different things you need to think about how it will decide when to do each of these things. In your simple example from above, it just does its thing over and over again in a loop every tenth of a second.

If you are actually designing actions to bind to individual keystrokes or button clicks, then having one such action per Lua file is the best way to go. Yes, you can reduce the number of Lua files (to one if you really want to) by invoking with a parameter as someone has shown above but this is unnecessarily cryptic.


MarkH

gGzCVFp.jpg
Core i7-7700K / 32Gb DDR4 / Gigabyte GTX1070 / 1080p x 3 x weird / Win7 64 Pro

Share this post


Link to post
Share on other sites
7 hours ago, gunterat said:

So I think I need a script that is:

1. monitoring the state of the Lvar (resp. recognizing changes)

2 and writing to the Offset as soon the state changes

and both permanently !

Once more, the code above works, but it misses the "monitoring function".

Sorry for my simple description, I hope it is understandable.

Best regards

Guenter

Guenter,

From your description above it sounds like you want to use the lua instruction event.Lvar(). Here is how it is described in the lua library file in your FSUIPC documentation:

event.Lvar("lvarname", interval,"function-name")

This monitors the value of the given local gauge variable
("L:Var") at the given interval, and calls the function when it
a change in the value is detected.
The L:var name can be in the form "L:name" or just "name",
but when our declared function is called the name provided is
without it's L: part. It is provided in the call so that the same
function can be used for multiple variables if required.
The interval is in milliseconds and has a minimum value of
100. it is not optional.
The function will never be called if there is no matching
L:Var, so you need not worry about checking the correct
aircraft or gauge is loaded. No error will occur.

I have never used event.Lvar(), but you may want to try using a script with a series of these commands each calling the appropriate function as needed. I would put the script ExampleScript.Lua under [Auto] in the FSUIPC.ini file (and of course also in the modules folder). I would expect the format for the script to be something like:

Script name is ExampleScript.Lua

function funcName1()
   code lines
return

function funcName2()
   code lines
return

function funcName3()
   code lines
return

--*************** Main Program********************

event.Lvar("lvarname1", interval, "funcName1")

event.Lvar("lvarname2", interval, "funcName2")

event.Lvar("lvarname3", interval, "funcName3")

 

I think something like this is worth trying.

Al

Edited by ark

Share this post


Link to post
Share on other sites
15 hours ago, MarkDH said:

If they are all actually the same kind of thing - such as the one above but just monitoring different offsets

I think I used the wrong phrase, you are right - just monitoring different offsets! Thank you for the script example.

 

11 hours ago, ark said:

From your description above it sounds like you want to use the lua instruction event.Lvar(). Here is how it is described in the lua library file in your FSUIPC documentation:

The description sounds very applicable for my project. I will try it out too, thank you.

Happy holidays / Guenter

 

Share this post


Link to post
Share on other sites
On 12/25/2018 at 5:52 PM, MarkDH said:

If you are actually designing actions to bind to individual keystrokes or button clicks, then having one such action per Lua file is the best way to go.

No, the button clicks, rotary switches ..... are absolutly independent of reading out the LED-State. These "Inputs" I have already realized with a macro-file and fsuipc (Mobiflight) control. In the last years I used "Wilco" and "Jeehell". Both have separate Offsets for Inputs and Outputs independently from each other - I think that this concept is very important. Otherwise its very difficult to keep the Hard- and Software simultaneously.

I built a script as you suggested in your last post and it runs very well or in other words: it seems to be the solution that I looked for. Thank you very much!

Al, may I ask for an example "code line" and what is meant with "Main Program"? Thank you!!

  • Like 1

Share this post


Link to post
Share on other sites
7 hours ago, gunterat said:

Al, may I ask for an example "code line" and what is meant with "Main Program"? Thank you!!

A function is a block of code that does something when called. A function is similar to what is called a subroutine in other computer languages. Only you know what each function should do. Functions must be defined before they can be used, so it is common to put all the function definitions at the top of the program, then the Main program, where the code execution actually starts, follows all the function definitions.  So the overall structure of the program is :

All the function definitions at the top, then

The main program code, that among other things, calls the functions when needed

A function definition has the basic form

function function_name()              -- this is the start of the function definition

   Here you write lines of Lua code that carry out whatever the function is supposed to do each time it is called from the main program, or called from another function. This could be just one line, or a 100 or more lines of code, etc

  return       -- this returns the program execution flow back to the point in the code from where the function was called

end            -- this marks the end of the function definition

So, for example,  if everytime the Lvar  AB_OVH_APU_MASTER_LIGHT_ON changes you want to read and store its state you could define the function apu_master_light,  as:

function apu_master_light()                                    -- apu_master_light is your name for this function.

apu_master_light = ipc.readLvar("L: AB_OVH_APU_MASTER_LIGHT_ON")    -- get state of the Lvar

ipc.writeUB( 0x66C0, apu_master_light)                                                              -- save the state of the Lvar

return

end                                                                                                                         -- end of the function definition

Now down in the "main program" you can call (execute) this function with the code line:

apu_master_light()         -- just using the name of the function 'calls' it to execute.

You can call the function apu_master_light as many times as you want in your program using the apu_master_light()  instruction.

However, the point I was making above is FSUIPC has a special instruction called

event.Lvar("lvarname1", interval, "funcName1") that monitors the Lvar called lvarname1

and calls the fucntion funcName1 each time the Lvar changes state.  So in your main program code you could write:

event.Lvar("L: AB_OVH_APU_MASTER_LIGHT_ON",100, "apu_master_light")  which would check the state of the Lvar

L: AB_OVH_APU_MASTER_LIGHT_ON every 100 milliseconds and if it had changed, would call your function apu_master_light(). So by using this special Lua instruction you don't have to write a loop that constantly checks the state of the Lvar AB_OVH_APU_MASTER_LIGHT_ON.

If you Google Lua functions, you will find lots of info that may help you.

Al

 

 

Edited by ark

Share this post


Link to post
Share on other sites

Hi Al, thank you for the detailed instructions. I hope you are patiently 🙂

Please be so kind and look at my example-code, based on your instructions!

-----------------------------

function apu_master_light ()
apu_master_light = ipc.readLvar("L:AB_OVH_APU_MASTER_LIGHT_ON")
ipc.writeUB(0x66C0, apu_master_light)
return
function bat_2_off ()
bat_2_off = ipc.readLvar("L:AB_OVH_ELEC_BAT2_LIGHT_OFF")
ipc.writeUB(0x66C1, bat_2_off)
return
function gen_1_off ()
bat_2_off = ipc.readLvar("L:AB_OVH_ELEC_GEN1_LIGHT_OFF")
ipc.writeUB(0x66C2, gen_1_off)
return
event.Lvar("L:AB_OVH_APU_MASTER_LIGHT_ON",100, "apu_master_light")
event.Lvar("L:AB_OVH_ELEC_BAT2_LIGHT_OFF",100, "bat_2_off")
event.Lvar("L:AB_OVH_ELEC_GEN1_LIGHT_OFF",100, "gen_1_off")
end

______________________________________________

Best regards Guenter

Share this post


Link to post
Share on other sites

apu_master_light

5 hours ago, gunterat said:

Please be so kind and look at my example-code, based on your instructions!

Guenter,

I made some changes below. What is in red should be deleted from your script, and what is in green should be added.

1. I see in my post above I used the same name ( apu_master_light ) both for the function name and the name of the variable being read. That is not good to do, that is my mistake, sorry.  So in your code below, I added ab_ to each function name just to fix this problem and make the function name and variable name different.

2. Each function definition must have an end instruction at the end, so I added that for each function.

3. You seem to have a typo in your gen_1_off function, so I fixed that.

So to use your modified script, you should give it a name like Monitor_AB_Lvars.lua using a max of 16 characters (not including the .lua extension ). Put the script in the sim's modules folder, and also edit FSUIPC to enter the script name under [Auto] in FSUIPC. If you don't have an [Auto] section, add it in. This runs the script automatically when the sim starts:

[Auto]
1=Lua Monitor_AB_Lvars

If you already have other scripts under [Auto] just use the next number in sequence in place of the 1.

Now start the sim and see if it works! Note that each time you change the script for any reason, you will have to restart the sim so the new changed script gets loaded.

BTW, there is a free program you may find useful called LuaEdit 2010 that I use to check the syntax of my scripts (see Check Syntax under the Debug tab). I simply copy the script from Notepad++ and past it into LuaEdit (using File/New/File/Lua Script/ OK) for the syntax check .

Good luck,

Al

Here is the modified script I called Monitor_AB_Lvars.lua

function ab_apu_master_light ()
apu_master_light = ipc.readLvar("L:AB_OVH_APU_MASTER_LIGHT_ON")
ipc.writeUB(0x66C0, apu_master_light)
return
end

function ab_bat_2_off ()
bat_2_off = ipc.readLvar("L:AB_OVH_ELEC_BAT2_LIGHT_OFF")
ipc.writeUB(0x66C1, bat_2_off)
return
end

function ab_gen_1_off ()
bat_2_off gen_1_off = ipc.readLvar("L:AB_OVH_ELEC_GEN1_LIGHT_OFF")
ipc.writeUB(0x66C2, gen_1_off)
return
end

event.Lvar("L:AB_OVH_APU_MASTER_LIGHT_ON",100, "ab_apu_master_light")
event.Lvar("L:AB_OVH_ELEC_BAT2_LIGHT_OFF",100, "ab_bat_2_off")
event.Lvar("L:AB_OVH_ELEC_GEN1_LIGHT_OFF",100, "ab_gen_1_off")
end 

Edited by ark

Share this post


Link to post
Share on other sites

the offsets value remains always at "0", it does not change to"1" when I activate the appropriate LED in the PC-Aircraft (At least MOBIFLIGHT reports "0" as Offset-value)

Any Idea?

Share this post


Link to post
Share on other sites
51 minutes ago, gunterat said:

the offsets value remains always at "0", it does not change to"1" when I activate the appropriate LED in the PC-Aircraft (At least MOBIFLIGHT reports "0" as Offset-value)

Any Idea?

For testing purposes, change the first function to the below. I have added 3 lines of code that should display the apu_master_light value being read by the function on a green bar window that should appear on the screen for about 4 seconds. If nothing is displayed at all (no green bar) when you change the light setting, then the function is not being activated for some reason and there is a problem elsewhere.

Remember:

1. The new script must be put in the modules folder (the entry under [Auto] in the FSUIPC ini file does not have to be changed).

2. You must restart the sim after all changes have been made.

function ab_apu_master_light ()
apu_master_light = ipc.readLvar("L:AB_OVH_APU_MASTER_LIGHT_ON")
ipc.writeUB(0x66C0, apu_master_light)
ipc.writeSTR(0x3380, "apu_master_light value is   "..apu_master_light)      -- display apu_master_light    
ipc.writeSW(0x32FA, 4)                                                                                 -- display for 4 seconds
ipc.sleep(4000)                                               -- program delay to keep message on green bar for 4 sec
return
end

If nothing works, paste your complete script here and I can try checking it for an error.

Al

Edited by ark

Share this post


Link to post
Share on other sites

Hi Al,

I restarted it again and again and now it runs !! Perfect, thank you very much!

I Wish you a Happy New Year!

Guenter

 

Edited by gunterat

Share this post


Link to post
Share on other sites

Guenter -- glad to hear you got it working!   😃

Al

Edited by ark

Share this post


Link to post
Share on other sites

Hi all,

Excuse me if i resume this post but i think that a new one probably creates more confusion.
This discussion has clarify almost all of my doubts but something is not yet clear especially what is the procedure to use mobiflight with this file lua, in the input menù of mobiflight there are many types of inputs, what i have to use: Event ID, Key, Lua Macro.....
I suppose to use key but this means that i've to assign every offset to a keyboard key and than put it in mobiflight? It's correct?
And i suppose to put the offset number for example 0x66C0 in to output/display tab of mobiflight? is it all correct?
Thanks in advance.

maxTurso

Edited by MaxTurso

Share this post


Link to post
Share on other sites
5 hours ago, MaxTurso said:

This discussion has clarify almost all of my doubts but something is not yet clear especially what is the procedure to use mobiflight with this file lua, in the input menù of mobiflight there are many types of inputs, what i have to use: Event ID, Key, Lua Macro.....

I'm sorry, but I have never heard of Mobiflight and have no idea how it works. If Mobiflight has a user or support forum, suggest you try asking your questions there as well as here.

Al

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Tom Allensworth,
    Founder of AVSIM Online


  • Flight Simulation's Premier Resource!

    AVSIM is a free service to the flight simulation community. AVSIM is staffed completely by volunteers and all funds donated to AVSIM go directly back to supporting the community. Your donation here helps to pay our bandwidth costs, emergency funding, and other general costs that crop up from time to time. Thank you for your support!

    Click here for more information and to see all donations year to date.
×
×
  • Create New...