Funtion Functionallity

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
User avatar
skymed
Posts: 46
Joined: Thu Mar 26, 2020 6:29 pm
Location: East Coast, Canada.

Funtion Functionallity

#1 Post by skymed »

Hi Folks
Learning LUA and working my through things, I want to have a max 7219 readout that shows on the left side 4 digits the Xplane rad-alt and on the right side 4 digits a number/height as set by me, so that I can compare them to let me know when I’m close to or at the desired height AGL .
I can make the right side digits appear no problems, and they are controlled by the encoder. For the life of me I have no clue how to get the rad alt from a function into the readout.
Can someone please help me here as I’m stumped....not only the HOW but, and more importantly, the WHY it needs to be done a certain way?
Yes I’ve watched Tony’s videos, yes I’ve read the almost unintelligible LUA manual, so any explanation as what and why and how will be greatly appreciated.

Steve

Code: Select all

desired_height=0

function test_height (actual_height)

       print(actual_height) 

end

--set my own desired altitude with rotary encoder

function dial_change(direction)

          if direction == 1 then

        desired_height=desired_height+10
end

 if
          direction == -1 then
      
             desired_height=desired_height-10

    if desired_height<=0 then desired_height=0
end

end


----this is the only way i can get any number to pretend to be actual_height
 actual_height = 100


--convert to string
local str = string.format("%.0f%4d", actual_height, desired_height)
-- Apply string to character display
print(str)
hw_chr_display_set_text(myheight, 0, 0, str)
end



-- Create a rotary encoder and add max7219 LCD Readout
hw_dial_add("ARDUINO_LEONARDO_A_D2", "ARDUINO_LEONARDO_A_D3", "TYPE_2_DETENT_PER_PULSE", 1, 10, dial_change )
myheight = hw_chr_display_add("MAX7219", 1, "ARDUINO_LEONARDO_A_D5", "ARDUINO_LEONARDO_A_D6", "ARDUINO_LEONARDO_A_D7")

--call backs

xpl_dataref_subscribe("sim/cockpit2/gauges/indicators/radio_altimeter_height_ft_pilot","FLOAT", test_height)

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

Re: Funtion Functionallity

#2 Post by Sling »

Steve,

You are so close. All you need to do is put the function that sets the max7219 data in the subscribe callback. You should also create a variable with the encoder set altitude rather that write that directly to the display. In the subscribe callback you need to either format each of the 2 pieces of data into 4 character strings and then use concatenation to join them or you can use string.format to format both into a single 8 character string. Either way you get an 8 character string ready to use in the display set function.

Have a go at that and let us know how you go.

Tony

User avatar
skymed
Posts: 46
Joined: Thu Mar 26, 2020 6:29 pm
Location: East Coast, Canada.

Re: Funtion Functionallity

#3 Post by skymed »

Cheers Tony......half the battle with this stuff is the terminology: So am off back to your videos to sort some of that out and will ask again when I get lost:-)

User avatar
skymed
Posts: 46
Joined: Thu Mar 26, 2020 6:29 pm
Location: East Coast, Canada.

Re: Funtion Functionallity

#4 Post by skymed »

By George I think I've got it........now I have tofigure a away to shut it up by pushing the encoder button :-)

Code: Select all

---[[initaiate sound and local variables
--Small test gauge that adds teh rad alt to the left 4 digits of a Max7219 
display, and a user defined decision height to the right 4 digits.
Sounds an alarm if rad alt is below user defined alt]]

sound_id = sound_add("DH_Tone.wav") 
local desired_height=0
local my_height=0

--Get rad alt, limit to min o and max 2000 ft agl and print to max7219
function radioaltitude(rad_alt)

    rad_alt=var_cap(rad_alt,0,2000)

local str = string.format("%04.0f%4d", rad_alt, my_height)

--Check with print
print(str)

-- Apply string to character display
hw_chr_display_set_text(myheight, 0, 0, str)

--determine if alarm needed

if rad_alt<= my_height then
 
    print("too low")
    sound_play(sound_id)

 else
    print("OK")
sound_stop(sound_id)
end
    end

--Settings for the rotary encoder
function set_height(direction)

--make into local variable
my_height=desired_height

 if direction == 1 then
        desired_height=desired_height+100
      end
 if  direction == -1 then
 desired_height=desired_height-100
            
    if desired_height<=0 
then desired_height=0 

end
    end
        end

-- Create a rotary encoder 
hw_dial_add("ARDUINO_LEONARDO_A_D2", "ARDUINO_LEONARDO_A_D3", "TYPE_2_DETENT_PER_PULSE", set_height)
myheight = hw_chr_display_add("MAX7219", 1, "ARDUINO_LEONARDO_A_D5", "ARDUINO_LEONARDO_A_D6", "ARDUINO_LEONARDO_A_D7")

--call backs

xpl_dataref_subscribe("sim/cockpit2/gauges/indicators/radio_altimeter_height_ft_pilot","FLOAT",radioaltitude)


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

Re: Funtion Functionallity

#5 Post by Sling »

Well done skymed. Now you have the Air Manager grin. :D

If your encoder has a button on the shaft you can add a hw button to simply stop the sound whenever it’s pressed. Note that the play function only plays the sound file once anyway so it will stop on it’s own or if your altitude goes above your set limit again. If you want the sound to play continuously until one of these events you have to use the loop function. It will play the file over and over until you stop it.

I love your willingness to dig in and work through the solution. If you ever need any future help i’d be only to pleased to help out again.

Tony

User avatar
skymed
Posts: 46
Joined: Thu Mar 26, 2020 6:29 pm
Location: East Coast, Canada.

Re: Funtion Functionallity

#6 Post by skymed »

Cheers Tony and thanks for the encouragement. Actually as set right now, the sound plays continously whilst I'm below desired height and stops after I go above again...................I inadvertantly reinvented a GPWS :-)

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

Re: Funtion Functionallity

#7 Post by Sling »

Ah yes I see that now in your code. It’s because the radio altitude is a float value that will be continually updating and calling the callback function. There’s no way you would be able to hold altitude to multiple decimal places so the callback will just keep getting called many many times every second. The solution is to add a variable into the if else that you set to true when the if first becomes true. You then use this variable in an if that you wrap around the sound play function. You use this to check if the new variable is false and if it is the sound is played. All future checks will return true because the variable is now set. You use the altitude going back above the set altitude as the means to reset the variable back to false.
This only plays the file once on first detection and only again if it gets reset. It’s probably better this way to save the continual playing of a wav file and necessary if you want that acknowledge button to work.

A bit like this.

Code: Select all

trigger = false

function callback(rad_alt)
    If rad_alt < set_alt then
        If not trigger then sound_play(sound_id) end
        trigger = true
    else
        trigger = false
    end
end

User avatar
skymed
Posts: 46
Joined: Thu Mar 26, 2020 6:29 pm
Location: East Coast, Canada.

Re: Funtion Functionallity

#8 Post by skymed »

Clever Stuff Tony cheers for that I'll gve it a wing.

Post Reply