Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite in Lua for FlyWithLua Plugin #13

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft

Conversation

samrq
Copy link

@samrq samrq commented Feb 19, 2025

Removed dependency on libusb.

Implemented the functionality using Lua for better integration with FlyWithLua.

Tested on the default A330-300 model, with the following results:

[Almost] All buttons, LCDs, and LEDs are functioning correctly.

Some LCDs related to autopilot alt ctrl require further testing.

Fix some format problem for spd and hdg

@samrq samrq changed the title Rewrite Script Using Lua for FlyWithLua Plugin Rewrite in Lua for FlyWithLua Plugin Feb 19, 2025
@schenlap
Copy link
Owner

Hi,

thank you for your pr. Everything that make is easier to use for all people is welcome.
On what platform did you test it?

There are a few things in it I already began working:

Why did you drop support for EFIS L/R and Toliss airplanes? Switching to lua should be one pull-request and new features a extra one. Dropping features can not be merged.

@samrq
Copy link
Author

samrq commented Feb 20, 2025

HI
Thanks for your reply.
I tested it on my M-chip Macbook with Xp12 and FlyWithLua NG+ v2.8.9.
no extra dependencies was introducd, so I think it could run on mac/win/linux as long as you installed flywithlua.
I'm not intend to remove toliss airplane and efls module. I just build and test this script with what I have :).
I'm not very farmiliar with pr procedure and i hope it won't bother you

@samrq samrq closed this Feb 20, 2025
@schenlap
Copy link
Owner

I'm not very farmiliar with pr procedure and i hope it won't bother you

No. Maybe flywithlua is a good idea. Do you want wo work on it further or should I add Toliss an EFIS?

@schenlap schenlap reopened this Feb 21, 2025
Comment on lines +78 to +93
cache_data["autopilot_spd"] = 0
cache_data["autopilot_spd_is_mach"] = 0
cache_data["autopilot_hdg_mag"] = 0
cache_data["autopilot_alt"] = 0
cache_data["autopilot_vs"] = 0
cache_data["autopilot_fpa"] = 0
cache_data["autopilot_ap1"] = 0
cache_data["autopilot_ap2"] = 0
cache_data["autopilot_athr"] = 0
cache_data["autopilot_appr"] = 0
cache_data["autopilot_loc"] = 0
cache_data["autopilot_spd_window"] = 0
cache_data["autopilot_fpa_window"] = 0
cache_data["autopilot_hdg_window"] = 0
cache_data["autopilot_trkfpa"] = 0
cache_data["autopilot_alt_mode"] = 0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is cache_data needed in lua? What I had seen it is possible to map variables direct to datarefs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache_data is used to check whether the values of these datarefs have changed. Otherwise, the LCD and LED would be redrawn in every loop

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I should rename this variable to clarify its purpose

fcu_driver.lua Outdated
Comment on lines 413 to 414
set_button_assignment(821, "sim/autopilot/vertical_speed_down")
set_button_assignment(822, "sim/autopilot/vertical_speed_up")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be done during init?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is outdated
I have indeed attempted to directly bind buttons to achieve these functionalities, aiming to minimize the reliance on more low-level interfaces, but it did not take effect. I am still working on resolving this issue. Moreover, I am uncertain whether the button IDs might change across different machines, which could potentially lead to additional development and configuration efforts

fcu_driver.lua Outdated
Comment on lines 419 to 420
set_button_assignment(821, "laminar/A333/autopilot/fpa_decrease")
set_button_assignment(822, "laminar/A333/autopilot/fpa_increase")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be done during init?

fcu_driver.lua Outdated
Comment on lines 156 to 168
local button = {}
for index = 0,32 do
local mask = 2^index
local bit = math.floor(event_bit/mask)
local bit_last = math.floor(last_event_bit/mask)
if (bit ~= bit_last) then
if (bit % 2 == 1) then
local id = index+1
button_press_event_list[index]=1
end
end
end
last_event_bit = event_bit
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this necessary when using hid? Why not work with button assignment instead?

fcu_driver.lua Outdated
function find_fcu()
for i = 1,NUMBER_OF_HID_DEVICES do
local device = ALL_HID_DEVICES[i]
if ((device.vendor_id == 16536) and (device.product_id == 47888) )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please in hex :-)

@samrq
Copy link
Author

samrq commented Feb 21, 2025

I'm not very farmiliar with pr procedure and i hope it won't bother you

No. Maybe flywithlua is a good idea. Do you want wo work on it further or should I add Toliss an EFIS?

If you're interested and have some free time, feel free to add any features you want to this script. After all, my work also relies on what you've done before. Maybe when I get new device and aircraft models, I can work on related stuff too, but I’m not sure about that yet.

@schenlap
Copy link
Owner

I tested you script now on my linux system. It is very slow. After turning a knob it takes 3 seconds to change the value, this is not usable. Does it work better on your setup?

@schenlap schenlap marked this pull request as draft February 21, 2025 12:48
@samrq
Copy link
Author

samrq commented Feb 21, 2025

I tested you script now on my linux system. It is very slow. After turning a knob it takes 3 seconds to change the value, this is not usable. Does it work better on your setup?

This script runs smoothly on my MacBook. I haven't tried it on a Linux platform yet, but I think it might be related to the HID interface. The hid_read in the script is set to a blocking mode with a 10ms timeout., if the device not respond, the loop will be blocked. You could use logMsg in on_button_event to see what slow down the loop. I'll try to fix this issue later

update :
i found a bug in last commit
** do_often(find_fcu())**
it should be
do_often("find_fcu()")

try to use the lasted commit and see if the promble still exist
sorry for this silly mistake
quite confused it still works in my computer!

@schenlap
Copy link
Owner

No, it did not change anything. I added logMsg in line 159 (in function on_button_event) and I see it get's read more often than I get the button event. After more than 20 calls I see that I presses a button.

Another problem is that often it does not connect, I get this output:

[001173] FlyWithLua Info: Start loading script file X-Plane 12/Resources/plugins/FlyWithLua/Scripts/fcu_driver.lua
[001174] FlyWithLua Info: Finished loading script file X-Plane 12/Resources/plugins/FlyWithLua/Scripts/fcu_driver.lua
[001175] FlyWithLua Info: All script files loaded successfully.
[001176] FlyWithLua Info: Loading time for all scripts is 0.012008 sec.
[001177] FlyWithLua Error: Wrong arguments to function hid_read().

@samrq
Copy link
Author

samrq commented Feb 21, 2025

No, it did not change anything. I added logMsg in line 159 (in function on_button_event) and I see it get's read more often than I get the button event. After more than 20 calls I see that I presses a button.

Another problem is that often it does not connect, I get this output:

[001173] FlyWithLua Info: Start loading script file X-Plane 12/Resources/plugins/FlyWithLua/Scripts/fcu_driver.lua
[001174] FlyWithLua Info: Finished loading script file X-Plane 12/Resources/plugins/FlyWithLua/Scripts/fcu_driver.lua
[001175] FlyWithLua Info: All script files loaded successfully.
[001176] FlyWithLua Info: Loading time for all scripts is 0.012008 sec.
[001177] FlyWithLua Error: Wrong arguments to function hid_read().

Can you check which processes are opening the FCU deivce at the same time ?
stop other processes that try to access this device

well. On my Mac, if I don’t run any plugins, X-Plane will recognizes the FCU just fine. When I press buttons, I can see button icon blink in the joystick settings page. But as soon as a plugin invoked hid_open, the device stops being registered in X-Plane, and the icon doesn't blink anymore when I press button.
image

Similarly, if I run sudo python3 ./test_endpoint.py after starting X-Plane, I will see this message in the X-Plane console:
UNREGISTER Joystick device: WINWING FCU-320 - VID:16536 PID:47888.
image

The issue you reported makes me suspect that the plugin and another process are competing for the device, causing intermittent data reception (like packet loss).

To be honest, my knowledge of HID devices is limited, and I don’t have a Linux device on hand. l'm sorry to say that I can't provide too much help. Everything I’ve said so far is just a guess,I hope this can give you some ideas to solve the problem, rather than leading you in the wrong direction

@samrq
Copy link
Author

samrq commented Feb 22, 2025

Try using the latest commit
I just removed hid_read and use builtin button support
hid_write only for led and lcd
and you may need to set the FCU_BUTTON_BEGIN depends on you own computer's setting.
image

you can get this id by click Plugin - FlyWithLua -FlyWithLua Macros - show joystick button numbers and then press "SPD MACH" on fcu(!! stop this plugin before you do this. it will conflict)
image

@schenlap
Copy link
Owner

When starting the macro for joystick button I get:

FlyWithLua Error: Can't execute Lua chunk. The chunk who failed is: DO_EVERY_FRAME_CHUNK
FlyWithLua Debug Info: The Lua stack contains the following elements:
...esources/plugins/FlyWithLua/Internals/button_sniffer.lua:8: attempt to call global 'button' (a table value)

and Lua stopped! at the top rigth corner.

@samrq
Copy link
Author

samrq commented Feb 23, 2025

When starting the macro for joystick button I get:

FlyWithLua Error: Can't execute Lua chunk. The chunk who failed is: DO_EVERY_FRAME_CHUNK
FlyWithLua Debug Info: The Lua stack contains the following elements:
...esources/plugins/FlyWithLua/Internals/button_sniffer.lua:8: attempt to call global 'button' (a table value)

and Lua stopped! at the top rigth corner.

I know what happend
image
it's a global variable button in fcu_driver.lua and it shadows the "button" function in button_sniffer.lua :(

fixed.

sry. I'm a fresher in lua too

@schenlap
Copy link
Owner

Thanks, the macro works now
It ist much besser now, but far from my python script (which is also not optimal). Changing the heading from 0 to 180 takes about a minute with lua.

@samrq
Copy link
Author

samrq commented Feb 23, 2025

Thanks, the macro works now It ist much besser now, but far from my python script (which is also not optimal). Changing the heading from 0 to 180 takes about a minute with lua.

Okay, maybe different platforms require different implementation approaches. If I have time later, I'll see if I can deploy a Linux system on my old laptop to reproduce and resolve this issue. I've uploaded a demo video showing the running effect on a Mac platform. Thank you for your review over these past few days.

@schenlap
Copy link
Owner

Your video looks very good.
I made one on my setup: https://tmpsend.com/mJ96sU7l

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants