Possible to cycle button events in Touch Phat?


#1

So I’m loving the TouchPhat and have attached to my fridge. I have one button to switch my amp on and off, two buttons for volume, leaving me with three buttons for Kodi (B is currently PartyMode music, C and D are both Radio stations). But I wonder is there a way so that a button cycles through Radio stations? eg first press Radio 1, second press Radio 4 etc. I’m very new to Python and would appreciate any ideas. Currently it looks like this…

@touchphat.on_release([‘C’])
def handle_touch(event):
print(“Radio 1”)
my_kodi.Player.Open({“item”:{“channelid”:21}})

@touchphat.on_release([‘D’])
def handle_touch(event):
print(“Radio 4”)
my_kodi.Player.Open({“item”:{“channelid”:20}})


#2

What you would need is a List of stations, with one station per item. Then, rather than triggering a specific radio station with a button press, your button press should increment a number.

You could use a tuple (x, y) to store the station name and channel ID in your list, such as: ("Radio 1", 21)

You will need to create a variable to keep track of your station index outside of the button press functions, and use global station_index to ensure that, when you increment it, you actually increment the global variable and not just a new variable inside the function.

You will also need to be mindful of the length of your list, and reset your index back to 0 when it’s >= len(station_list).

Hopefully this enough for you to experiment with, without making it too easy ;)


#3

Thank you so much for your detailed reply. It’s very much appreciated. As I mentioned my knowledge of python is very limited but I will look into your ideas and see if I can work out how to do it. Can you recommend an example script of what you suggest? I really am a beginner I’m afraid.


#4
station_index = 0
station_list = [
    ("Radio 1", 21),
    ("Radio 4", 20)
]

@touchphat.on_release([‘C’])
def handle_touch(event):
    global station_index # Declare global, so we modify the variable defined above

    station_index += 1 # Advance to next station

    station_index %= len(station_list) # Loop station_index  back to zero if it's greater than the length of the list

    channel_name, channel_id = station_list[station_index] # Grab the name/id from the tuple to variables for convenience

    print("Now playing: {}".format(channel_name)) # Display the channel name

    my_kodi.Player.Open({“item”:{“channelid”:channel_id}})

station_index % len(station_list) is a bit of a mysert line if you’re not familiar with modulo %. Basically the operator % returns the remainder of station_index divided by len(station_list).

So if your list is two items long, len(station_list) will return 2.

Since a list with 2 items has indexes 0 and 1 (they are zero indexed) the modulo operator is super great at turning a constantly incriminating number into a number that steps through the items in the list, like so:

0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
4 % 2 = 0
5 % 2 = 1
6 % 2 = 0
7 % 2 = 1
8 % 2 = 0
9 % 2 = 1

These tutorials might be useful to you:

https://learn.pimoroni.com/tutorial/python/python-introduction
https://learn.pimoroni.com/tutorial/python/python-arguments
https://learn.pimoroni.com/tutorial/python/python-list
https://learn.pimoroni.com/tutorial/python/python-range

Edit: Note I’ve not got a Pi & Touch pHAT set up, so I can’t test this :D


#5

Thank you ever so much!! That’s great, I’ll give that a go. Brilliant :)


#6

Just tried it and it works brilliantly!! Thank you ever so much! Now to check out the tutorials… :)