Jump to content
The simFlight Network Forums

Recommended Posts

Posted

I'm struggling getting to grips with lua I'm afraid. I'm trying to get a code a push button to start the apu if its not already running. If it is then stop it. I'm testing offset 940F bit 5. This is the apu generator offbus light which will be a reasonable test to see if I can shut the apu down safely.

Apart from the 2 lines --// start apu etc. Can you see any problems with this code. It may not be the best way to do it but I'm looking for a simple solution.

ifly2fsuipc will off course be running and this lua file will be assigned to a switch in fsuipc.

function ApuToggle(offset, value)

if Logic.And(value, 0x0101) ~=0

then

--//stop apu

else

--//start apu

end

end

ApuToggle("940F", "UB", "0x0101")

Please don't be tooo brutal in your reply and try to keep it relatively simple as I really am a beginner. I learn best from examples too.

Thanks

Graham

Posted

Apart from the 2 lines --// start apu etc. Can you see any problems with this code. It may not be the best way to do it but I'm looking for a simple solution.

 

You need to test things yourself. You won't do any harm. It either works or doesn't.

 

This function:

 

function ApuToggle(offset, value)

 

has two parameters, "offset" and "value". Why? You never use "offset".

 

This line:

 

ApuToggle("940F", "UB", "0x0101")

 

calls your function, setting offset to the string "940F", and value to "UB". The third parameter (0x0101)  is of course discarded because the function only takes two parameters.

 

No where do you actually read the value in offset 940F, so the plug-in cannot do anything. In fact it will fail on this line:

 

if Logic.And(value, 0x0101) ~=0

 

because you cannot use the And function on the string value (which is sert to "UB", remember?

 

For what you want to do you don't need functions. All you need to do is read the offset (using ipc.readUB)  and test it. But not with 0x0101 which is a 16 bit value with bits 0 and 8 being tested. Why bits 0 and 8? You said it was bit 5, which is 0x20.

 

If you don't understand hexadecimal numbers or binary or bit numbers, please see the FAQ subforum hread on bits and numbers etc.

 

Pete

Posted

Now that was brutal Pete. I'm just trying to make lua do one or 2 things for me so trying to get an understanding for programming, hex and binary and all things technical is like learning do it yourself brain surgery so you can cut your own fingernails. Once I learn one or two tricks like the one I'm trying to achieve I can apply it to many things.

I can't try them at present because I'm away from my computer for a while but trying to get one or two lua scripts written for when I'm next home and able to try them.

I'm not asking people to provide a coding service but one or two pointers with my position in mind would be eally nice.

I'm in the middle east working and would love to pop round to a fellow simmers house to exchange notes but they'd be over 3 and a half thousand miles away.

Graham

Posted

n =ipc.readUB(940F)

Then test n ? There's several lights assigned to 940F so how do I get n to test 940F bit 5.

I'm sorry but I'm just not getting any of it

Graham

Posted

Graham,

 

 maybe this can help, or, seriously screw things up.

function ApuToggle()
   MyData = ipc.readUB(0x940F)
        if Logic.And(MyData, 0x20)  == 1 then   --// Bit 5 = 100000 binary, 32 decimal, 20 hexadecimal
             --//stop apu
        else
             --//start apu
        end
end

Then all you need is some event to call the function ApuToggle()

 

Hope this can help, keep plugging at it :smile:

Roman

Posted

Now that was brutal Pete

 

What's that supposed to mean? You asked me to help you program some Lua plug-in. So I did so, step by step. I could instead have written the program for you -- it's only a few lines -- but the way you asked you seemed to want to learn. If you do not want to learn just ask for the solution instead!

 

I'm not asking people to provide a coding service but one or two pointers with my position in mind would be eally nice.

 

I provided a detailed step by step account of what was wrong and how to make it right! What better could I do? Sorry, but if you don't appreciate the help I give, please don't come and ask for it. I don't appreciate having my detailed assistance thrown back in such a way! :-(

 

Good bye.

 

Pete

Posted

Graham,

 

 maybe this can help, or, seriously screw things up.

function ApuToggle()
   MyData = ipc.readUB(0x940F)
        if Logic.And(MyData, 0x20)  == 1 then   --// Bit 5 = 100000 binary, 32 decimal, 20 hexadecimal
             --//stop apu
        else
             --//start apu
        end
end

 

You simply provided a solution. I could have done that but he stated he didn't want that!

 

Then all you need is some event to call the function ApuToggle()

 

He actually stated the event -- assigned to a button or switch. That isn't an event so much as the thing which runs the plug-in, so all he needs is the content of the function, not the function itself. I did explain that too. But then he threw my help back at me.

 

I don't understand folks like that. Makes no sense!

 

Regards

Pete

Posted

The trouble with words on a page is they don't convey the feeling sometimes when they were written. When I wrote now that was brutal I remember I was actually smiling at the fact I really do know very little and seem to be barking up the wrong tree. No-one is throwing anything back at anyone Pete.

I would have liked the solution but I've read a post were you said you weren't here to provide a coding service ( and why should you? ) so " cap in hand ", I had a go at it myself and came to the ones who know their stuff. Being perplexed at all this stuff, I was even more perplexed at my mistakes you pointed out.

I apologize if my reply to your help came across in a way that it wasn't supposed to.

I'm sorry.

Graham

Posted

I apologize if my reply to your help came across in a way that it wasn't supposed to.

 

Okay. Let me go through the problems I pointed out and explain them a little more. Perhaps they were a bit terse, but the intention was to make you think it through so you could solve things yourself next time.

 

The first point was that "functions" are lumps of code, starting with the "function ..." line which names it and declares what data it should receive (known as "parameters"), and ending with an "end". These lumps of code are executed by referring to them elsewhere, and that reference to them supplies the actual values you want for those 'parameters'.

 

Your function had two parameters, but your call to it had three values. Additionally, the values you supplied were not in fact the types of values your function wanted.

 

That covers the first couple of points I made. The next point relates to data types. There are numbers, like 53 and 0x35 -- the first is decimal, as you'd use every day and the second is hexadecimal, which is much more useful in many computer applications because it relates directly to what hapens in the computer. 53 and 0x35 are the same value, they are just different ways of writing it. The reference I gave to the FAQ subforum was intended to allow you to do things like work out "bit numbers", because you were obviously befuddled by the term "bit 5". It would show you that bit 5 is the bit in binary with value 2^5 (i.e. 2 x 2 x 2 x 2 x 2 = 32 = 0x20). Bits are counted from 0 (2^0 = 1).

 

Another data type which is commonly used is the "string". That term is simply a reference to a string of characters, like "abcDEF1234", and strings are used for messages and passing names and so on. If they look like numbers they can be treated like numbers (this is a facility peculiar to Lua, though, and is not generally true in programming). But not otherwise -- which is why I pointed out that your original proposal would fail on the line

 

if Logic.And(value, 0x0101) ~=0

 

This would fail because the value parameter was set to the string "UB" by the call to the function.

 

In a follow up message, more in line with the questions I thought you'd respond with in the first place, you said:

 

n =ipc.readUB(940F)

Then test n ? There's several lights assigned to 940F so how do I get n to test 940F bit 5.

 

There are three errors or questions there. The first is that "940F" which is not valid. For hexadecimal it must be 0x940F -- the "0x" in front tells the computer it is hex not decimal. There is a relaxation implemented for the ipc functions where an offset can be given as a hex number in a string, thus: "940F", but I think that needs discouraging because it isn't generally applicable.

 

By "test n" I meant "see if the value n tells you to do whatever it is you want to do, or not". You already programmed a "test" in your line

 

if Logic.And(value, 0x0101) ~=0

 

You just needed to read the offset 'value' (my 'n') first.

 

And the third question is how to test bit 5 instead of bits 0 and 8, which in your line the number 0x0101 tests. I did actually give you the direct answer to that before -- 0x20 is the value for bit 5. The explanation for that is in the FAQ you didn't want to look at for some reason.

 

The answer provided by Roman earlier is almost there. There were two errors I just noticed and forgot to point out before, so here is the corrected version.

 

All you need is this bit, the corrections are in red:

 

MyData = ipc.readUB(0x940F)

if logic.And(MyData, 0x20) ~= 0 then -- Bit 5 = 100000 binary, 32 decimal, 20 hexadecimal

-- stop apu

else

-- start apu

end

 

Now all you need to do is fill lin the lines to stop and start the APU, then assign the saved Lua to your button.

 

BTW note that in Lua comments are introduced by --. The // prefix is from C/C++ and doesn't apply in Lua.

 

Pete

Posted

Hi Pete,
Thankyou for your last post. My understanding of things are much clearer now. It was the bit5 I was having trouble with. I did read the FAQ on bits but didn't understand it. It was clearer when I revisited it after your informative post.

Unfortunately it still doesnt work and puzzled as to why that may be. The following is my lua script saved in the modules folder and called by a button press assigned in FSUIPC. The ip.write lines are correct as I saved them as a single line in a lua file and assigned them to a button for testing purposes. They worked as advertised. Ifly2fsuipc documentation says the apu offline status light is 940F bit 5 which is the way I chose to detect whether the apu is running or not so my button operates the correct operation of START or OFF. The line beneath the script is an error relating to my lua script which was picked up by the Linda console window.Im not certain it likes the testing method we chose.


MyData = ipc.readUB(0x940F)
if Logic.And(MyData, 0x20) ~= 0 then -- Bit 5 = 100000 binary, 32 decimal, 20 hexadecimal
      ipc.writeUW("9400",517) -- stop apu
else
      ipc.writeUW("9400",519) --start apu
end



[E] *** LUA Error: ...s\Microsoft Flight Simulator X\Modules\ApuToggle.lua:2: attempt to index global 'Logic' (a nil value)


I think were nearly there but a last hurdle is yet to be jumped.

Hope you can help.

Graham
 

Posted

Unfortunately it still doesnt work and puzzled as to why that may be

[E] *** LUA Error: ...s\Microsoft Flight Simulator X\Modules\ApuToggle.lua:2: attempt to index global 'Logic' (a nil value)

 

The clue is in the error message:

 

*** LUA Error: ...s\Microsoft Flight Simulator X\Modules\ApuToggle.lua:2: attempt to index global 'Logic' (a nil value)

 

You are referring to a Lua library called Logic, which doesn't exist. The one in FSUIPC is called logic. Note the difference is only a capital letter. Programming is like that -- very very fussy. You have to be more careful to write exactrly what it should be.

 

I must admit this is an easy error to make, and I didn't spot it earlier either :-( (I've now gone back and corrected my post so that new readers aren't also misled).  We all make little mistakes like that, which is why it is always important to check for error messages -- which in fact you did.

 

Regards

Pete

Posted

Youre not going to believe this Pete but I actually noticed that capital L and momentarily hovered my mouse over it to change it to lowercase. Then I checked back through this thread and saw that they were all capitals right from the post from SPOKES2012 so I dismissed my own judgment. Maybe we all copied and pasted ( I know I did).

 

It works excellent Pete. I can now release a lot of my button associations as I can now give some of them a dual purpose.

 

Happy man . . . . .for now anyway.

 

Thanks very much

Graham

Posted

Sorry guy's,

 

 Copied/pasted right from post 1 and never really looked at punctuation.  :oops:  

Didn't want to hijack this thread either, just glad it is all working for you now.

 

Roman 

Posted

Thanks for your help. It did help get me to where I got to in the end.

 

Pete youve been a great help so far, can you just do one last thing before we can wrap this thread up. Can you just talk me through this code taken from another thread in another forum.

 

function ApuGenOffline(offset, value)--//Apu Gen Offline Lights
gfd.SetBright(GFP8, 0, 10)--//Sets the brighness level (10) for my P8 FIRST unit(1)
    if logic.And(value,0x0101) ~=0 then
    gfd.SetLight(GFP8, 0, 1)
    gfd.SetLight(GFP8, 0, 3)
    else
    gfd.ClearLight(GFP8, 0, 1)
    gfd.ClearLight(GFP8, 0, 3)
    end
end
event.offset("940F", "UB", "ApuGenOffline")

 

It seems that event.offset("940F", "UB", "ApuGenOffline") has 3 parameters. Is the third the result of the function. Where is value taken from and put into the function being defined.

 

Graham

Posted
It seems that event.offset("940F", "UB", "ApuGenOffline") has 3 parameters. Is the third the result of the function. Where is value taken from and put into the function being defined.Graham

 

Please, please, do look up these functions in the Lua library documentation provided in your FSUIPC Documents folder. It does actually tell you exactly what is going on there.

 

The third parameter is a PARAMETER, not a RESULT. Results are things like X  in X = function(parameters).

 

The third parameter here tells the "event.offset" function which user function to call when the stated offset changes. That's the event which instigates the action. If you look earlier you see the function actually called ApuGenOffline (didn't you notice?), so that function is called when the Unsigned Byte (UB) offset 0x940f changes. The function is called with two parameters, the offset value (0x940F) and its current value.

 

This is all precisely as documented in the Library documentation. Please do refer to it.

 

Pete

Posted

The reason why I chose this to quiz you is the guy that wrote the above is also testing the APU gen status light.

Offset 940F bit 5 but he's using what looks like binary for 5 0x0101 and not hex 0x20 which is what we used in my case. Is that another way to do it or is this code faulty.

I promise I'll read up on things Peter . . . Promise ;-)

Graham

Posted

Offset 940F bit 5 but he's using what looks like binary for 5 0x0101 and not hex 0x20 which is what we used in my case. Is that another way to do it or is this code faulty.

 

What a mix up!

 

The BINARY 0101 is the computer representation of  the number 5.  They are bits, only 0 and 1., each one to the left being worth twice the one to its right (in the same way as in decimal, being based on 10, each digit is 10 times the one to its right. So binary 100 = 2x2 = 4 whilst decimal 100 = 10 x 10.

 

Putting 0x in front says that this is supposed to be hexadecimal, and hex 0101 is decimal 257. In hex each digit it 16 times the one to its right, so 100 = 16 x 16 = 256!

 

All these are numbers, but "bit 5" means "the 5th bit", not a nmuber but a reference. Bits are counted from 0 which is the lowest, the one on the end, so bit 5 is 100000 in binary. Remember? We've been through all of this.

 

Therefore the value to test for bit 5 in a logic operation is binary 100000 which is 2 x 2 x 2 x 2 x 2 = 32 = 0x20.

 

This is all repeating what's gone on before. Using 0x0101 is rubbish by any of these measures!!!

 

Please PLEASE go back to that FAQ subforum thread on numbers. I think you have forgotten it all already.

 

Pete

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.