Midi And Freepie setButton

Official forum for open source FreePIE discussion and development.
Post Reply
nPulse
One Eyed Hopeful
Posts: 3
Joined: Fri Aug 11, 2017 2:44 pm

Midi And Freepie setButton

Post by nPulse »

Hi all, I've been digging into py and freepie and the use of MIDI controllers.

I'm trying to get a vJoy button to work when I press a button on my midi controller. And I'd like to keep it pressed for as long as I hold the button. I'd also like to have a 'latched' option that will keep the button pressed without keeping the button on the controller pressed, when 'latched == true'

I'm very sorry my knowledge of py is very little.. This is what I come up with, but obviously it doesn't work.

Also I'd like to know if there is a better way, I will be using around 50 buttons..

Code: Select all

def update():

    if (midi[0].data.buffer[0] == 71):
        vJoy[2].x = filters.mapRange(midi[0].data.buffer[1], 127, 0, 17873, -17873)

    if (midi[0].data.buffer[0] == 74):
        vJoy[2].y = filters.mapRange(midi[0].data.buffer[1], 127, 0, 17873, -17873)
        
    if (midi[0].data.buffer[0] == 24) and (midi[0].data.buffer[1] > 120):
        vJoy[2].setButton(0,1)
    
    if latched == False:
        vJoy[2].setButton(0,0)
 
		


diagnostics.watch(midi[0].data.channel)
diagnostics.watch(midi[0].data.status)
diagnostics.watch(midi[0].data.buffer[0])
diagnostics.watch(midi[0].data.buffer[1])

		 
if starting:
	latched = False
	midi[0].update += update
nPulse
One Eyed Hopeful
Posts: 3
Joined: Fri Aug 11, 2017 2:44 pm

Re: Midi And Freepie setButton

Post by nPulse »

Ok its not nice and tidy, but it's a start. Unfortunately I still get some errors now and then. Referring to this line:

Code: Select all

vJoy[Joy1].setButton(Dict[midi[0].data.buffer[0]],True)
Probably has to do something with the timeout loop.. I just found this line on the internet, credits to gabrielqv on GitHub!

Code: Select all

import time

def update():

	if (midi[0].data.buffer[0] == 1) and (midi[0].data.status == MidiStatus.Control):
		vJoy[Joy1].x = filters.mapRange(midi[0].data.buffer[1], 127, 0, 17873, -17873)

	if (midi[0].data.buffer[0] == 2) and (midi[0].data.status == MidiStatus.Control):
		vJoy[Joy1].y = filters.mapRange(midi[0].data.buffer[1], 127, 0, 17873, -17873)

	if (midi[0].data.buffer[0] == 3) and (midi[0].data.status == MidiStatus.Control):
		vJoy[Joy1].z = filters.mapRange(midi[0].data.buffer[1], 127, 0, 17873, -17873)

	if (midi[0].data.buffer[0] == 4) and (midi[0].data.status == MidiStatus.Control):
		vJoy[Joy1].slider = filters.mapRange(midi[0].data.buffer[1], 127, 0, 17873, -17873)

	if (midi[0].data.buffer[0] == 5) and (midi[0].data.status == MidiStatus.Control):
		vJoy[Joy1].dial = filters.mapRange(midi[0].data.buffer[1], 127, 0, 17873, -17873)

	if (midi[0].data.buffer[0] == 6) and (midi[0].data.status == MidiStatus.Control):
		vJoy[Joy1].rz = filters.mapRange(midi[0].data.buffer[1], 127, 0, 17873, -17873)
		
	if (midi[0].data.buffer[0] == 7) and (midi[0].data.status == MidiStatus.Control):
		vJoy[Joy1].rx = filters.mapRange(midi[0].data.buffer[1], 127, 0, 17873, -17873)
		
	if (midi[0].data.buffer[0] == 8) and (midi[0].data.status == MidiStatus.Control):
		vJoy[Joy1].ry = filters.mapRange(midi[0].data.buffer[1], 127, 0, 17873, -17873)
		

	if (midi[0].data.status == MidiStatus.NoteOn) and (latched == False) and (midi[0].data.buffer[0] < (BgnKB+34)):
			vJoy[Joy1].setButton(Dict[midi[0].data.buffer[0]],True)				#Be Sure not to press a note thats not defined by the Dict{} when the diagnostics.watch(Dict[midi[0].data.buffer[0]]) is active..
			timeOut[Dict[midi[0].data.buffer[0]]] = (time.time() + .03)
	
	diagnostics.watch(midi[0].data.channel)
	diagnostics.watch(midi[0].data.status)
	diagnostics.watch(midi[0].data.buffer[0])
	diagnostics.watch(midi[0].data.buffer[1])
#	diagnostics.watch(Dict[midi[0].data.buffer[0]])
	 
if starting:
	timeOut = {}
	latched = False
	BgnKB = 24 # is start of Keyboard, this example begins at C2
	Joy1 = 2
	Dict = {(BgnKB+0):1, (BgnKB+1):2, (BgnKB+2):3, (BgnKB+3):4, (BgnKB+4):5, (BgnKB+5):6, (BgnKB+6):7, (BgnKB+7):8, (BgnKB+8):9, (BgnKB+9):10, (BgnKB+10):11, (BgnKB+11):12, (BgnKB+13):13, (BgnKB+13):14, (BgnKB+14):15, (BgnKB+15):16, (BgnKB+16):17, (BgnKB+17):18, (BgnKB+18):19, (BgnKB+19):20, (BgnKB+20):21, (BgnKB+21):22, (BgnKB+22):23, (BgnKB+23):24, (BgnKB+24):25, (BgnKB+25):26, (BgnKB+26):27, (BgnKB+27):28, (BgnKB+28):29, (BgnKB+29):30, (BgnKB+30):31, (BgnKB+31):32, (BgnKB+32):33} #Maps joystick buttons to each midi note 
	midi[0].update += update
	
buffer = timeOut.copy()

for x in buffer:
    if timeOut.get(x, 0) < time.time():
        vJoy[Joy1].setButton(x,False)
        del(timeOut[x])

diagnostics.watch(str(timeOut))
Edit: It seems to fire the button twice when pressed.. Is there a trick to remove that first press? It has to do something with the timing.
nPulse
One Eyed Hopeful
Posts: 3
Joined: Fri Aug 11, 2017 2:44 pm

Re: Midi And Freepie setButton

Post by nPulse »

I removed the timer and replaced it with a MidiStatus.NoteOff IF statement.. works better now ;)

Code: Select all

	if (midi[0].data.status == MidiStatus.NoteOn) and (latched == False) and (midi[0].data.buffer[0] < (BgnKB+34)) and (midi[0].data.channel == 0):
			vJoy[Joy1].setButton(Dict[midi[0].data.buffer[0]],True)				
#			timeOut[Dict[midi[0].data.buffer[0]]] = (time.time() + 0.03) #default = 0.03

	if (midi[0].data.status == MidiStatus.NoteOff) and (latched == False) and (midi[0].data.buffer[0] < (BgnKB+34)) and (midi[0].data.channel == 0):
			vJoy[Joy1].setButton(Dict[midi[0].data.buffer[0]],False)
#			timeOut[Dict[midi[0].data.buffer[0]]] = (time.time() + 0.03) #default = 0.03
Post Reply

Return to “FreePIE”