Jump to content
The simFlight Network Forums

Different actions for short and hold presses of the button


Recommended Posts

Posted

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?

Posted

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

 

Posted (edited)

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
Posted

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

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

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

Posted
  On 5/30/2023 at 1:50 PM, 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?

Expand  

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.

  On 5/30/2023 at 1:51 PM, Georgy said:

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

Expand  

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

Posted
  On 5/30/2023 at 1:59 PM, 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

Expand  

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")

 

Posted
  On 5/30/2023 at 2:05 PM, Georgy said:

Please, check this code):

Expand  

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

Posted
  On 5/30/2023 at 2:14 PM, John Dowson said:

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

 

Expand  

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

Posted
  On 5/30/2023 at 2:30 PM, Georgy said:

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

Expand  

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.

 

Expand  

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

John

Posted
  On 5/30/2023 at 2:34 PM, 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

Expand  

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)

Posted
  On 5/30/2023 at 2:39 PM, 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)

Expand  

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

Posted
  On 5/30/2023 at 2:44 PM, John Dowson said:

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

Expand  

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.

Posted
  On 5/30/2023 at 2:14 PM, John Dowson said:

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

Expand  
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?

Posted

B=TCA YOKE BOEING
B.GUID={E05CFE20-D530-11EC-8003-444553540000}

Can I use letter "B" at "joy' line of Tripleuse.lua? Or only number?

Posted
  On 5/30/2023 at 9:58 PM, 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.

Expand  

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.

Posted
  On 5/30/2023 at 10:05 PM, 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?

Expand  

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.

Posted
  On 5/31/2023 at 9:27 AM, John Dowson said:

No, you should use the number.

Expand  

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

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.