Jump to content
The simFlight Network Forums

Different actions for short and hold presses of the button


Georgy

Recommended Posts

Hello!I want to turn on the fuel valve cut off in 737 pmdg with a short press of the button and turn it off by long pressing the same button.

Same with autothrottle disengage (short press) and TOGA (long press)

How to do it?

Link to comment
Share on other sites

Take a look at the TripleUse.lua script which is included in the lua examples (zipped in your FSUIPC7 Documents folder). There is also a more advanced version of this lua script available in the User Contributions sub-forum: 

You should be able to adapt either one of those for your needs.

John

 

Link to comment
Share on other sites

Thank You, John! I thought maybe you did another way)
AT Disengage (67279) and TOGA (65861) I did! Thank you! Code:

joy = 3
btn = 2
interval = 100 -- 1/2 second press, gap, press limits
ignorepress = false

-- Function to time the button being pressed or left released
-- Allow only up to "interval" till decide no change
local function timebutton(test)
  while true do
		time2 = ipc.elapsedtime()
		if (time2 - time1) > interval then
			ignorepress = false
			return false
		end
	 	if ipc.testbutton(joy, btn) == test then
			time1 = time2
			return true
		end
	 	ipc.sleep(20)
	end
end

function buttonpress(j, b, du)
	if ignorepress then
		ignorepress = false
		return
	end
  
	-- Note time button was pressed
	time1 = ipc.elapsedtime()
	time2 = 0

	if timebutton(false) then
		-- First press / release counts: see if there's another
		if timebutton(true) then
			-- got another press in time, look for release
			if timebutton(false) then
				-- this was a double press, send VIEW RIGHT
				ipc.control()
                ignorepress = true --  ignore the next press we receive, i.e. the 2nd in a double-press 
			end
		else	
			-- This was a single press, send VIEW LEFT 
			ipc.control(67279)
			ignorepress = false
		end
	else
		-- This was a longer press, send VIEW FORWARD
		ipc.control(65861)
	end
end

-- Enable event on button being pressed (only)
event.button(joy, btn, 1, "buttonpress")


How do I manage the cut off valves? They work as presets. I don't know what code to enter.

 

Unfortunately, the github page https://github.com/joeherwig/msfs-fsuipc-lua-scripts is not working.

Edited by Georgy
Link to comment
Share on other sites

1 minute ago, Georgy said:

And how I can add another controller to the same script? For example: 

joy = 3
btn = 2
joy = 4
btn = 5

It will be works? Or I must to do 2 tripleuse.lua for my controllers?

No, that won't work, as you are changing the variable value. You can use one script if you know what you are doing (I am not going to explain this to you...!), but it is probably easier to use a separate script for each button.

2 minutes ago, Georgy said:

Sorry, I don't understand(( Can You write example? Thank You!

What don't you understand? Your script contains the following lines to execute the control/event:
          ipc.control(67279)
So that sends the control AUTO_THROTTLE_DISCONNECT. To send a preset instead, change that to:
         ipc.execPreset("presetName")
where presetName is the name of the preset. Or add the parameter if the preset uses one, e.g. ipc.execPreset("presetName", param)

John

Link to comment
Share on other sites

4 minutes ago, John Dowson said:

No, that won't work, as you are changing the variable value. You can use one script if you know what you are doing (I am not going to explain this to you...!), but it is probably easier to use a separate script for each button.

What don't you understand? Your script contains the following lines to execute the control/event:
          ipc.control(67279)
So that sends the control AUTO_THROTTLE_DISCONNECT. To send a preset instead, change that to:
         ipc.execPreset("presetName")
where presetName is the name of the preset. Or add the parameter if the preset uses one, e.g. ipc.execPreset("presetName", param)

John

Please, check this code):

joy = 3
btn = 8
interval = 100 -- 1/2 second press, gap, press limits
ignorepress = false

-- Function to time the button being pressed or left released
-- Allow only up to "interval" till decide no change
local function timebutton(test)
  while true do
		time2 = ipc.elapsedtime()
		if (time2 - time1) > interval then
			ignorepress = false
			return false
		end
	 	if ipc.testbutton(joy, btn) == test then
			time1 = time2
			return true
		end
	 	ipc.sleep(20)
	end
end

function buttonpress(j, b, du)
	if ignorepress then
		ignorepress = false
		return
	end
  
	-- Note time button was pressed
	time1 = ipc.elapsedtime()
	time2 = 0

	if timebutton(false) then
		-- First press / release counts: see if there's another
		if timebutton(true) then
			-- got another press in time, look for release
			if timebutton(false) then
				-- this was a double press, send VIEW RIGHT
				ipc.control()
                ignorepress = true --  ignore the next press we receive, i.e. the 2nd in a double-press 
			end
		else	
			-- This was a single press, send VIEW LEFT 
			ipc.execPreset("PMDG_B737-7_FUEL_CUT_OFF_LEVER1_UP")
			ignorepress = false
		end
	else
		-- This was a longer press, send VIEW FORWARD
		ipc.execPreset("PMDG_B737-7_FUEL_CUT_OFF_LEVER1_DN")
	end
end

-- Enable event on button being pressed (only)
event.button(joy, btn, 1, "buttonpress")

 

Link to comment
Share on other sites

1 minute ago, Georgy said:

Please, check this code):

Can you not do this yourself? Why don't you just try running it, and use the provided debugging facilities if you have any issues.

As you are not using the double press, then you should at least remove the following line (as it will create an error if you do double-press):
    ipc.control()

Or, better still, remove the code that handles a double-press completely if not using this.

John

Link to comment
Share on other sites

16 minutes ago, John Dowson said:

Вы не можете сделать это сами? Почему бы вам просто не попробовать запустить его и использовать предоставленные средства отладки, если у вас возникнут какие-либо проблемы.

 

I meant, did I enter the action correctly in brackets and quotes? Shouldn't there be numbers?

Link to comment
Share on other sites

Just now, Georgy said:

I meant, did I enter the action correctly in brackets and quotes? Shouldn't there be numbers?

Why? The preset is a name, and the documentation for this function is:

Quote
ipc.execPreset(“presetName”, param)[Not WideClient]

This executes the calculator code associated to the argument presetName using the WAPI execute_calculator_code method. Note that the preset name must be given as it appears in the events.txt or myevents.txt file, not as appears un the drop-down menus where underscores are replaced by spaces.

N.B. To use this function, the FSUIPC WASM module must be installed, and the WASM interface active in FSUIPC7.

 

The only number needed is the parameter, if the preset uses one (most don't).

John

Link to comment
Share on other sites

4 minutes ago, John Dowson said:

Why? The preset is a name, and the documentation for this function is:

The only number needed is the parameter, if the preset uses one (most don't).

John

Thank You!

Tell me please how to implement such a function: I raise the chassis crane in 737 (Thrustmaster Boeing), the lever presses the button all the time. After 10 seconds, the second action is activated: the lever itself is transferred to the off position. Sorry for my bad English)

Link to comment
Share on other sites

1 minute ago, Georgy said:

Tell me please how to implement such a function: I raise the chassis crane in 737 (Thrustmaster Boeing), the lever presses the button all the time. After 10 seconds, the second action is activated: the lever itself is transferred to the off position. Sorry for my bad English)

Sorry but I don't understand. What is a 'chassis crane'? What do you want to do?

Link to comment
Share on other sites

7 hours ago, John Dowson said:

Sorry but I don't understand. What is a 'chassis crane'? What do you want to do?

I'm sorry... I used a translator

I would like to do this:

1. In a Boeing 737, I do gear up.

2. Wheels retract

3. After 10 seconds, the gear lever automatically switches to off mode.

Link to comment
Share on other sites

7 hours ago, John Dowson said:

Or, better still, remove the code that handles a double-press completely if not using this.

time1 = ipc.elapsedtime()
	time2 = 0

	if timebutton(false) then
		-- First press / release counts: see if there's another
		if timebutton(true) then
			-- got another press in time, look for release
			if timebutton(false) then
				-- this was a double press, send VIEW RIGHT
				ipc.control()
                ignorepress = true --  ignore the next press we receive, i.e. the 2nd in a double-press 
			end
		else	
			-- This was a single press, send VIEW LEFT 
			ipc.control(67279)
			ignorepress = false
		end
	else
		-- This was a longer press, send VIEW FORWARD
		ipc.control(65861)
	end

What strings (lines) can I delete?

Link to comment
Share on other sites

On 5/31/2023 at 12:58 AM, Georgy said:

I'm sorry... I used a translator

I would like to do this:

1. In a Boeing 737, I do gear up.

2. Wheels retract

3. After 10 seconds, the gear lever automatically switches to off mode.

I was able to do it.
Assigned "gear up" to physical lever without lua script.
In tripleuse.lua assigned only one action (hold press): move virtual lever to position "gear off" after 10 seconds when the physical lever is up.

Link to comment
Share on other sites

On 5/31/2023 at 1:05 AM, Georgy said:
time1 = ipc.elapsedtime()
	time2 = 0

	if timebutton(false) then
		-- First press / release counts: see if there's another
		if timebutton(true) then
			-- got another press in time, look for release
			if timebutton(false) then
				-- this was a double press, send VIEW RIGHT
				ipc.control()
                ignorepress = true --  ignore the next press we receive, i.e. the 2nd in a double-press 
			end
		else	
			-- This was a single press, send VIEW LEFT 
			ipc.control(67279)
			ignorepress = false
		end
	else
		-- This was a longer press, send VIEW FORWARD
		ipc.control(65861)
	end

What strings (lines) can I delete?

I'm sorry. Can You help me? I don't know which lines I can delete, if i need one action (hold press), or 2 actions (hold press and single press) only.

Link to comment
Share on other sites

13 hours ago, John Dowson said:

No, you should use the number.

Thank You.) The letter would be more convenient when changing different joysticks, because it is attached to each device, even when it is disabled.

Link to comment
Share on other sites

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.