Python Forum
run different functions each time the same button is pressed?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
run different functions each time the same button is pressed?
#1
I am using python inputs library to detect gamepad button presses.

Purpose: I want to change the listening mode of my Pioneer AV-Receiver when pressing RB on my Xbox Controller. The event is logged as 'BTN_TR' through python inputs library.

Right now when I press the button it loops through all added listening modes when I press RB once. (as expected) but that's not what I want. I want to change from one listening mode to the next each time I press RB.

So when I press RB for the first time it should switch from Auto Surround to Rock/Pop. When I press RB again it should switch to Ext.Stereo. And the third time I press RB it should switch back to Auto Surround.

This is my code right now. What do I need to change?

def main():
	while 1:
		events = get_gamepad()
		for event in events:

			lmd_query = eiscp.eISCP('192.168.0.59').raw('LMDQSTN')

			if lmd_query == 'LMD80': #autosurround 
				if event.code == 'BTN_TR':  
					eiscp.eISCP('192.168.0.59').raw('LMD06') #rockpop  
   
			if lmd_query == 'LMD06':  
				if event.code == 'BTN_TR':
					eiscp.eISCP('192.168.0.59').raw('LMD0C') #ext.stereo

			if lmd_query == 'LMD0C':  
				if event.code == 'BTN_TR':
					eiscp.eISCP('192.168.0.59').raw('LMD80')
				
if __name__ == "__main__":
	main()
Thank you for reading and if you have an idea please let me know!
Reply
#2
Note: Untested code
This should hopefully cycle through each mode and then return to the first and start over cycling through each time RB is pressed.
import itertools


def change_sound(value):
    eiscp.eISCP('192.168.0.59').raw(f'{value}')


cycle_sound = itertools.cycle(('LMD06', 'LMD0C', 'LMD80'))


def main():
    while True:
        events = get_gamepad()
        for event in events:

            if event.code == 'BTN_TR':
                change_sound(next(cycle_sound))


if __name__ == "__main__":
    main()
Reply
#3
Thank you so much for your solution! It is working as you described it. It cycles through each mode.

Is it also possible that it changes to the next mode without cycling?

Because it is going through each mode every time I press the button instead of just switching to the next mode.

Hopefully I was clear to describe the distinction. :)
Reply
#4
It should only switch one mode at a time
import itertools

cycle_sound = itertools.cycle(('LMD06', 'LMD0C', 'LMD80'))

for _ in range(6):
    print('click button')
    print(next(cycle_sound))
Output:
click button LMD06 click button LMD0C click button LMD80 click button LMD06 click button LMD0C click button LMD80
Each time next(cycle_sound) is called it grabs the next item

what happens in for event in events:
is it possible that if event.code == 'BTN_TR': is true more than once

Myabe the while True: loop is too fast and needs a sleep to allow for the release of the button?
Reply
#5
ahh, you're correct. Your code was working all along.

I just realised one button press is divided to the push and releasing of the button:

push:
BTN_TR
SYN_REPORT

release:
BTN_TR
SYN_REPORT

So if I push it down, it switches to the next mode. Upon releasing it switches again.

That was my mistake. Your code works great!
Reply
#6
You may want something like this so it does nothing on the button release
import itertools
 
 
def change_sound(value):
    if value:
        eiscp.eISCP('192.168.0.59').raw(f'{value}')
 
 
cycle_sound = itertools.cycle(('LMD06', None, 'LMD0C', None, 'LMD80', None))
 
 
def main():
    while True:
        events = get_gamepad()
        for event in events:
 
            if event.code == 'BTN_TR':
                change_sound(next(cycle_sound))
 
 
if __name__ == "__main__":
    main()
Reply
#7
Wow, was just about to ask you. Really clever and neat solution.

Now it's exactly as I need it.

Thank you so much for helping me out. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting button pressed number Moris526 4 2,442 Dec-14-2020, 01:41 AM
Last Post: Moris526
  How many times was the button pressed in pyglet rama27 0 1,930 Oct-10-2020, 10:26 AM
Last Post: rama27
  Terminate a process when hotkey is pressed 66Gramms 0 2,254 Dec-24-2019, 06:41 PM
Last Post: 66Gramms
  Advance program when certain keys are pressed? Chrislw324 2 2,348 May-19-2019, 07:13 PM
Last Post: woooee
  Count to movement according to the time pressed button noartist 1 2,523 Feb-27-2019, 01:33 PM
Last Post: noartist
  Time multiple functions within functions Cortessizzz 4 3,138 Jan-09-2019, 04:15 PM
Last Post: Cortessizzz
  How can classes access each other Functions and Variables at the same time PythonOK 4 3,074 Dec-09-2018, 03:46 AM
Last Post: ichabod801
  What key pressed? ian 2 4,991 Jul-29-2018, 02:30 AM
Last Post: snippsat
  Can't edit code after I've pressed enter. xBlackHeartx 2 12,100 Sep-02-2017, 10:04 PM
Last Post: nilamo
  Button Presses in Certain Time Frame MsCheeseyPuff 3 4,022 Jun-30-2017, 08:39 PM
Last Post: MsCheeseyPuff

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020