Jump to content
The simFlight Network Forums

LUA function calls based on bit values in an offset


Recommended Posts

To Pete or others,

I need some help on how to write a small effective LUA code to send different ipc.control commands based on bit value changes in an user offset.

I have the 2word offset $66E0 sent to FSUIPC whenever there is bit value change of any of the 32 bits ( when turning switches and pushing buttons on my MCP) by SIOC.

with

event.offset(0x66E0, "UD", "MCP")

I call a MCP function. Thereafter I want i.e. to do a specific ipc.control when bit 3 in $66E0 changes from 0 to 1 and another ipc.control when same bit changes from 1 to 0. The same should be valid for all 32 bits in this offset.

What I want is:

if bit "0" changes from 0 to 1 then

ipc.control X

if bit "0" changes from 1 to 0 then

ipc.control Y

and so on for all 32 bits

Any ideas on how to do this? I can't find any ipc.read command that just test a bit, i.e IF_BIT($66E0, 3)

Link to comment
Share on other sites

Any ideas on how to do this? I can't find any ipc.read command that just test a bit, i.e IF_BIT($66E0, 3)

Lua is unfortunately woefully short on logical bit-oriented facilities, so I added them in the logic library. To test a bit you need to logically AND the value with a mask containing only that bit and test the result to be zero or non-zero. For example,


function MCP(off, val)
if logic.And(val, 0x0008) ~= 0 then
-- do what you want for bit 3
end
...
[/CODE]

If you aren't clear on bits, numbers and masks look in the [b]FAQ [/b]subforum for a short tutorial.

Regards

Pete

Link to comment
Share on other sites

To Pete or others,

I have tried to use the logic call as you mentioned but it fails. I am not a programmer but I think it is not finding the logic lib. this is the message I get at debugging.

-- This example of Logic Lib

x = 0;

a = 15;

if logic.And(2, a) == 2 then

x = 4

end

print (x);

...Files\Lua\5.1\SciTE\scite-debug\scite_lua\prompt.lua:55: attempt to call field 'chdir' (a nil value)

Any help would be appriciated.

Ray

Link to comment
Share on other sites

I have tried to use the logic call as you mentioned but it fails. I am not a programmer but I think it is not finding the logic lib. this is the message I get at debugging.

...

...Files\Lua\5.1\SciTE\scite-debug\scite_lua\prompt.lua:55: attempt to call field 'chdir' (a nil value)

Where are you getting that error message from? Wwhat folder is ...Files\Lua\5.1\SciTE\scite-debug\scite_lua? That's nothing like anything I provide in either FSUIPC or WideClient. The logic library is built into FSUIPC and Wideclient for Lua plug-ins running inside those processes. What are you trying to do?

If there's an error in your Lua plug-in it will show up in FSUIPC's log, or its own log file in WideClient or if you've selected Lua logging in FSUIPC. There's never any need to use an external debugger for plug-ins just to locate a syntax error.

Regards

Pete

Link to comment
Share on other sites

Lua is unfortunately woefully short on logical bit-oriented facilities, so I added them in the logic library. To test a bit you need to logically AND the value with a mask containing only that bit and test the result to be zero or non-zero. For example,


function MCP(off, val)
if logic.And(val, 0x0008) ~= 0 then
-- do what you want for bit 3
end
...
[/CODE]

If you aren't clear on bits, numbers and masks look in the [b]FAQ [/b]subforum for a short tutorial.

Regards

Pete

Thanks for your feedback and suggestion. Problem solved.

Do you know of any good debugger for testing LUA script in FSUIPC. I have tried DECODA which looks fine, but I am not able to get it to work with LUA files in FSUIPC.

rgs roarkr

Link to comment
Share on other sites

Do you know of any good debugger for testing LUA script in FSUIPC. I have tried DECODA which looks fine, but I am not able to get it to work with LUA files in FSUIPC.

I don't know "decoda". There's the LuaDebug option in FSUIPC which gives you a trace by line numbers, but which can probably be improved -- the Lua source is included. But I mainly rely on simple logging -- adding ipc.log lines in salient places to tell me where the code is going and what the important values are.getting set to.

Simple syntax errors are logged in any case, as I said.

[LATER]

I juist googled "decoda" and see it is payware, with quite a hefty fee for commercial use!

The reason it doesn't work with FSUIPC is because it needs to hook into the Lua functions compiled into FSUIPC, which it cannot do without a lot more assistance. i read quite a bit through what it offers and I really don't feel it justifies the effort nor the price.

Regards

Pete

Link to comment
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
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.