vJoy toggle MIDI button

Official forum for open source FreePIE discussion and development.
Post Reply
RogerR
One Eyed Hopeful
Posts: 3
Joined: Tue Sep 01, 2020 10:42 pm

vJoy toggle MIDI button

Post by RogerR »

Hello, I have a MIDI controller with several toggle CC buttons (0 = off; 127 = on) and I would like my vJoy button to be pressed but not held like an on/off switch when vJoy get 127/0 respectively.

What I figured out so far is that this code will make the vJoy button to be held:

Code: Select all

if (midi[0].data.buffer[0] == 4) and (midi[0].data.buffer[1] == 127): 
	vJoy[0].setButton(4,True)
else:
	vJoy[0].setButton(4,False)
How can I make the vJoy button to be pressed momentarily when values 127 and 0 are received?

Thanks in advance!
Jabberwock
Cross Eyed!
Posts: 197
Joined: Mon Mar 02, 2015 3:58 pm

Re: vJoy toggle MIDI button

Post by Jabberwock »

Try this:

Code: Select all

def update():
	
	global toggle

	if (midi[0].data.buffer[0] == 4) and (midi[0].data.buffer[1] == 127):
		toggle = 1

if starting:
	midi[0].update += update
	
	toggle = 0
	

if (toggle == 1):
	vJoy[0].setButton(4,True)
	toggle = 0
else
	vJoy[0].setButton(4,False)

Note that this makes the button press rather short (one frame), which might not be sufficient for all applications. You can solve it using timers or just make the toggle value higher and decrease it each frame until it is zero.
Last edited by Jabberwock on Thu Sep 03, 2020 2:35 am, edited 1 time in total.
RogerR
One Eyed Hopeful
Posts: 3
Joined: Tue Sep 01, 2020 10:42 pm

Re: vJoy toggle MIDI button

Post by RogerR »

Jabberwock wrote: Wed Sep 02, 2020 5:39 am Try this:

Code: Select all

def update():
	
	global toggle

	if (midi[0].data.buffer[0] == 4) and (midi[0].data.buffer[1] == 127):
		toggle = 1

if starting:
	midi[0].update += update
	
	toggle = 0
	

if (toggle == 1):
	vJoy[0].setButton(4,True)
	toggle == 0
else
	vJoy[0].setButton(4,False)

Note that this makes the button press rather short (one frame), which might not be sufficient for all applications. You can solve it using timers or just make the toggle value higher and decrease it each frame until it is zero.
Thank you Jabberwock but unfortunately that didn't work and made the button stuck, I can't turn it off.
RogerR
One Eyed Hopeful
Posts: 3
Joined: Tue Sep 01, 2020 10:42 pm

Re: vJoy toggle MIDI button

Post by RogerR »

I've changed setButton to setPressed and now I can get a short button press as desired but it only works for value 127 and just the first time, I can't make it activate again. Any ideas?
Jabberwock
Cross Eyed!
Posts: 197
Joined: Mon Mar 02, 2015 3:58 pm

Re: vJoy toggle MIDI button

Post by Jabberwock »

Sorry, the code has an error: it should be toggle = 0 and not toggle == 0 in the conditional.

As for setPressed - try to debug the MIDI values and see how they change, to make sure that they are not stuck.
Post Reply

Return to “FreePIE”