Skip to content
View in the app

A better way to browse. Learn more.

The AVSIM Community

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Simple LUA question (toggle function)

Featured Replies

I am trying to find a work around for a rotary button that only has a push-function but not a pull-function. I want to use it in an Airbus home cockpit for the Baro encoder that in the real airplane is push/pull. My plan is that if a push sends a push command (ROTOR_BRAKE plus parameter) to FSUIPC, the next push will send a pull command. So while the button can only be pushed, it will send alternating commands. I have experince with the EPIC programming language that is similar to C++, but I cannot find information how Lua handles flags. Anyway, this is what I came up with, and it does not work:

local i = n            -- defining the local variable

    if i > 1 then
        ipc.control(66587,71008) -- command for pushing the Baro knob
        ipc.control(66587,71011) -- command for releasing the Baro knob
    i = 1            -- setting the local variable to 1 in order to continue with "else" for the next push
    else
        ipc.control(66587,71007) -- command for pulling the Baro knob
        ipc.control(66587,71010) -- command for releasing the Baro knob
    i = 2            -- setting the local variable to 2 in order to continue with the first command for the next push
    end

I would be happy for any advice. If this is not the right forum for these questions, please let me know.

Best regards,

Holger

Holger TillmannSIM: FS9/2004 // FSX<p>CPU: Intel Core i5-4670K 4x3.40GHz (for FSX OCed to 4.2), GPU: ASUS GTX 760 Direct CU II OC, GeForce® 9800 GTX +MB: ASRock Z87 Extreme6 Z87 DDR3RAM: Crucial 4GB PC3-12800 DDR3-1600 CL8 Ballistix Tactical

I'd do it by putting the state flag into a user offset, which persists across lua script executions.  For your example to work, you'd have to keep the script running in a loop (to keep the state variable "i" alive) rather than just executing it once each time the button is pressed.

e.g. (using user-area offset 66C0 as an example)

btnstate = ipc.readUB(0x66C0)
if btnstate == 2
  then
    ipc.control(66587,71008) -- command for pushing the Baro knob
    ipc.control(66587,71011) -- command for releasing the Baro knob
    ipc.writeUB(0x66C0,1)
  else

   ...etc

That said, I think it would be better to code your "pull" action for something like a press-and-hold > 1 sec rather than alternating the press-pull actions.

Bob Scott | President and CEO, AVSIM Inc
ATP Gulfstream II-III-IV-V

Sys1 (MSFS20+24/XPlane12+11): AMD 9800X3D, water 2x240mm, MSI MPG X670E Carbon, 64GB GSkill 6000/30, nVidia RTX4090FE
Alienware AW3821DW 38" 21:9 GSync, 2x4TB Crucial T705 PCIe5 + 2x2TB Samsung 990 SSD, EVGA 1000P2 PSU, 12.9" iPad Pro
Thrustmaster TCA Boeing Yoke, TCA Airbus Sidestick, Twin TCA Airbus Throttle quads, PFC Cirrus Pedals, Coolermaster HAF932 case

Sys2 (P3Dv5/v4): i9-13900KS, water 2x360mm, ASUS Z790 Hero, 32GB GSkill 7800MHz CAS36, ASUS RTX4090
Samsung 55" JS8500 4K TV@60Hz,
3x 2TB WD SN850X 1x 4TB Crucial P3 M.2 NVME SSD, EVGA 1600T2 PSU
Fiber link to Yamaha RX-V467 Home Theater Receiver, Polk/Klipsch 6" bookshelf speakers, Polk 12" subwoofer, 12.9" iPad Pro
PFC yoke/throttle quad/pedals with custom Hall sensor retrofit, Thermaltake View 71 case, Stream Deck XL button box

Sys3 (DCS/P3Dv4/ATS/ETS): AMD 7800X3D, MSI MPG X870E Carbon, Noctua NH-D15S, 64GB GSkill 6000/30, EVGA RTX3090
Alienware AW3420DW 34" 21:9 GSync, Corsair HX1000i PSU, 4TB Crucial T705 PCIe5 + 2TB Samsung 970Evo Plus,
TM TCA Officer Pack
, Saitek combat pedals, TM Warthog, TM RS300 FF wheel/pedals, Coolermaster HAF XB case

  • Author

Dear Bob,

Thank you so much for your advice. This is what the script looks like now, and it works, even without running in an infinite loop. I think this is because the value is stored in the offset. Thank you again so much!
Best regards,
Holger

P.S.: For the record: This is for the FSLabs Airbus (FSX) EFIS Baro knob. The display alternates between "Std" and the selected QNH with every push of a button now. But the command for releasing the button comes too quickly, so I have lost the animation. I understand that there is no wait function in Lua, so I will have to carry on tinkering until I find a solution.

btnstate = ipc.readUB(0x66C0)
if btnstate == 2
  then
    ipc.control(66587,71008) -- command for pushing the Baro knob
    ipc.control(66587,71011) -- command for releasing the Baro knob
    ipc.writeUB(0x66C0,1)
  else
    ipc.control(66587,71007) -- command for pulling the Baro knob
    ipc.control(66587,71010) -- command for releasing the Baro knob
    ipc.writeUB(0x66C0,2)
end

 

Holger TillmannSIM: FS9/2004 // FSX<p>CPU: Intel Core i5-4670K 4x3.40GHz (for FSX OCed to 4.2), GPU: ASUS GTX 760 Direct CU II OC, GeForce® 9800 GTX +MB: ASRock Z87 Extreme6 Z87 DDR3RAM: Crucial 4GB PC3-12800 DDR3-1600 CL8 Ballistix Tactical

7 minutes ago, EPICfan said:

P.S.: For the record: This is for the FSLabs Airbus (FSX) EFIS Baro knob. The display alternates between "Std" and the selected QNH with every push of a button now. But the command for releasing the button comes too quickly, so I have lost the animation. I understand that there is no wait function in Lua, so I will have to carry on tinkering until I find a solution.

I'd think adding a 1/4 sec sleep delay between the controls should fix that:

...

    ipc.control(66587,71008) -- command for pushing the Baro knob
    ipc.sleep(250)
    ipc.control(66587,71011) -- command for releasing the Baro knob

...

 

Bob Scott | President and CEO, AVSIM Inc
ATP Gulfstream II-III-IV-V

Sys1 (MSFS20+24/XPlane12+11): AMD 9800X3D, water 2x240mm, MSI MPG X670E Carbon, 64GB GSkill 6000/30, nVidia RTX4090FE
Alienware AW3821DW 38" 21:9 GSync, 2x4TB Crucial T705 PCIe5 + 2x2TB Samsung 990 SSD, EVGA 1000P2 PSU, 12.9" iPad Pro
Thrustmaster TCA Boeing Yoke, TCA Airbus Sidestick, Twin TCA Airbus Throttle quads, PFC Cirrus Pedals, Coolermaster HAF932 case

Sys2 (P3Dv5/v4): i9-13900KS, water 2x360mm, ASUS Z790 Hero, 32GB GSkill 7800MHz CAS36, ASUS RTX4090
Samsung 55" JS8500 4K TV@60Hz,
3x 2TB WD SN850X 1x 4TB Crucial P3 M.2 NVME SSD, EVGA 1600T2 PSU
Fiber link to Yamaha RX-V467 Home Theater Receiver, Polk/Klipsch 6" bookshelf speakers, Polk 12" subwoofer, 12.9" iPad Pro
PFC yoke/throttle quad/pedals with custom Hall sensor retrofit, Thermaltake View 71 case, Stream Deck XL button box

Sys3 (DCS/P3Dv4/ATS/ETS): AMD 7800X3D, MSI MPG X870E Carbon, Noctua NH-D15S, 64GB GSkill 6000/30, EVGA RTX3090
Alienware AW3420DW 34" 21:9 GSync, Corsair HX1000i PSU, 4TB Crucial T705 PCIe5 + 2TB Samsung 970Evo Plus,
TM TCA Officer Pack
, Saitek combat pedals, TM Warthog, TM RS300 FF wheel/pedals, Coolermaster HAF XB case

  • Author

I just tried this (below) and it worked. But your solution works, too, and it is much more elegant. Thank you very much! This is my first day with Lua, I haven't coded for years, and its like coming back home.

local clock = os.clock
function sleep(n)-- seconds
local t0 = clock()
while clock() - t0 <= n do end
end

btnstate = ipc.readUB(0x66C0)
if btnstate == 2
  then
    ipc.control(66587,71008) -- command for pushing the Baro knob
sleep(0.2)
    ipc.control(66587,71011) -- command for releasing the Baro knob
    ipc.writeUB(0x66C0,1)
  else
    ipc.control(66587,71007) -- command for pushing the Baro knob
sleep(0.2)
    ipc.control(66587,71010) -- command for releasing the Baro knob
    ipc.writeUB(0x66C0,2)
end

 

Holger TillmannSIM: FS9/2004 // FSX<p>CPU: Intel Core i5-4670K 4x3.40GHz (for FSX OCed to 4.2), GPU: ASUS GTX 760 Direct CU II OC, GeForce® 9800 GTX +MB: ASRock Z87 Extreme6 Z87 DDR3RAM: Crucial 4GB PC3-12800 DDR3-1600 CL8 Ballistix Tactical

Most importantly, the ipc.sleep function suspends execution of the script rather than going into a tight timing loop, which is a potentially important consideration when coding real-time processes.

There's a lot of useful functionality built into the Lua library for FSUIPC.  I never cease to be surprised at how much one can get done with simple Lua scripting using the FSUIPC library.

Bob Scott | President and CEO, AVSIM Inc
ATP Gulfstream II-III-IV-V

Sys1 (MSFS20+24/XPlane12+11): AMD 9800X3D, water 2x240mm, MSI MPG X670E Carbon, 64GB GSkill 6000/30, nVidia RTX4090FE
Alienware AW3821DW 38" 21:9 GSync, 2x4TB Crucial T705 PCIe5 + 2x2TB Samsung 990 SSD, EVGA 1000P2 PSU, 12.9" iPad Pro
Thrustmaster TCA Boeing Yoke, TCA Airbus Sidestick, Twin TCA Airbus Throttle quads, PFC Cirrus Pedals, Coolermaster HAF932 case

Sys2 (P3Dv5/v4): i9-13900KS, water 2x360mm, ASUS Z790 Hero, 32GB GSkill 7800MHz CAS36, ASUS RTX4090
Samsung 55" JS8500 4K TV@60Hz,
3x 2TB WD SN850X 1x 4TB Crucial P3 M.2 NVME SSD, EVGA 1600T2 PSU
Fiber link to Yamaha RX-V467 Home Theater Receiver, Polk/Klipsch 6" bookshelf speakers, Polk 12" subwoofer, 12.9" iPad Pro
PFC yoke/throttle quad/pedals with custom Hall sensor retrofit, Thermaltake View 71 case, Stream Deck XL button box

Sys3 (DCS/P3Dv4/ATS/ETS): AMD 7800X3D, MSI MPG X870E Carbon, Noctua NH-D15S, 64GB GSkill 6000/30, EVGA RTX3090
Alienware AW3420DW 34" 21:9 GSync, Corsair HX1000i PSU, 4TB Crucial T705 PCIe5 + 2TB Samsung 970Evo Plus,
TM TCA Officer Pack
, Saitek combat pedals, TM Warthog, TM RS300 FF wheel/pedals, Coolermaster HAF XB case

Archived

This topic is now archived and is closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.