Main()

Let Sim Innovations know about your Air Manager experience and let us know about your dream feature addition

Moderators: russ, Ralph

Post Reply
Message
Author
evanssa1963
Posts: 6
Joined: Sat Sep 26, 2020 1:38 am

Main()

#1 Post by evanssa1963 »

Just started writing lua script in Air Manager for Arduino support of my sim. One of the things I've noticed is that there is no default function that runs unless its associated with a hardware event. For example, I want to get a bunch of sim data and then use it later in my code. The way I got around this is to add a hardware switch so that the switch event is recognized in the lua loop and place a bunch of code there. It works but uses up a pin on the Arduino even though I don't have a physical switch connected.

Is there a way to run code that is not directly associated with a hardware event?

Thanks,
Scott

User avatar
Sling
Posts: 5244
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Main()

#2 Post by Sling »

Hi Scott and welcome to the forum,

The answer to your question is Yes its called subscribe. There are also other ways but this is the primary way to do something when sim data changes. You don't say which sim but for xplane this would be xpl_dataref_subscribe() and its associated callback. If you want to use the data returned to this callback function outside of this function you either call the other function from this one and pass it the data as arguments or you make the data you want to use elsewhere a global variable by declaring the variable at the head of your code block. Just like this.

Code: Select all

--1st option. Data is passed to another function
function another_func(arg1)

	--data is now available in this function as arg1

end

function subscribe_callback(data)

	another_func(data)

end
xpl_dataref_subscribe("required dataref", "type", subscribe_callback)

--2nd option. You can use my_var anywhere in this lua file.
local my_var = 0

function subscribe_callback2(data)

	my_var= data

end
xpl_dataref_subscribe("required dataref", "type", subscribe_callback2)

Let me know if you need futher clarification.

Tony

evanssa1963
Posts: 6
Joined: Sat Sep 26, 2020 1:38 am

Re: Main()

#3 Post by evanssa1963 »

Thanks for the response Tony - I really appreciate the help :D . Here's what I'm running into:

-----------------------------------------------------------------------------------------------------------------------------------------------------
Your example:

xpl_dataref_subscribe("sim/cockpit2/electrical/battery_amps", "float[8]", GetChargingCurrent_callback)
function GetChargingCurrent_callback(amps)
-code goes here
end

I get an error: ERROR - logic.lua:173: Argument 'callback(3)' in function 'xpl_dataref_subscribe' is nil, which is not allowed

-----------------------------------------------------------------------------------------------------------------------------------------------------
When I fake a switch event:

hw_switch_add("ARDUINO_MEGA_2560_A_D53", 1, FakeSwitchFunction)

function FakeSwitchFunction(position)
xpl_dataref_subscribe("sim/cockpit2/electrical/battery_amps", "float[8]", GetChargingCurrent_callback)
end

function GetChargingCurrent_callback(amps)
-code goes here
end

This works
---------------------------------------------------------------------------------------------------------------------------------------------------

EDIT: Hang On! If I place the subscribe line BELOW the callback function, it WORKS! :roll: I guess lua runs from bottom to top?

User avatar
Ralph
Posts: 7933
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Main()

#4 Post by Ralph »

It does not, but the callback function has to be 'known' first. Or else the dataref subscribe is calling a function that isn't there.

evanssa1963
Posts: 6
Joined: Sat Sep 26, 2020 1:38 am

Re: Main()

#5 Post by evanssa1963 »

Thanks Ralph - that makes perfect sense.

Post Reply