Page 1 of 1

GPIO controls

Posted: Sun Apr 12, 2026 12:20 am
by AZSSG
I'm trying to have a setup where i have 2 buttons from the gpio control volume up and down in hudiy and also show the overlay with the percentage. I have very little knowledge in coding but i am learning slowly. I learned to use gpiozero and setup the buttons to perform simple actions, I just don't know how to incorporate everything in to hudiy. I also tried to modify the volume.py in the gpio examples from the github but I couldn't seem to get the buttons to do what I want. Just wondering if I could get some help with this

Re: GPIO controls

Posted: Sun Apr 12, 2026 12:42 am
by hudiy
To control the volume, you can use the toggle_output_muted, output_volume_down, and output_volume_up actions - a toast with the volume level will appear automatically when using them.
https://github.com/wiboma/hudiy/blob/ma ... md#actions

Here is the modified Volume.py from the examples that uses a Button instead of a RotaryEncoder:

Code: Select all

import common.Api_pb2 as hudiy_api
import time
from common.Client import Client, ClientEventHandler
from gpiozero import Button


class EventHandler(ClientEventHandler):

    def __init__(self):
        self._mute_button = Button(26, bounce_time=0.05)
        self._up_button = Button(16, bounce_time=0.05)
        self._down_button = Button(25, bounce_time=0.05)

    def on_hello_response(self, client, message):
        print(
            "received hello response, result: {}, app version: {}.{}, api version: {}.{}"
            .format(message.result, message.app_version.major,
                    message.app_version.minor, message.api_version.major,
                    message.api_version.minor))

        self._mute_button.when_pressed = lambda: self.send_dispatch_action(
            client, "toggle_output_muted")
        self._down_button.when_pressed = lambda: self.send_dispatch_action(
            client, "output_volume_down")
        self._up_button.when_pressed = lambda: self.send_dispatch_action(
            client, "output_volume_up")

    def send_dispatch_action(self, client, action_name):
        dispatch_action = hudiy_api.DispatchAction()
        dispatch_action.action = action_name
        client.send(hudiy_api.MESSAGE_DISPATCH_ACTION, 0,
                    dispatch_action.SerializeToString())


def main():
    client = Client("volume gpio example")
    event_handler = EventHandler()
    client.set_event_handler(event_handler)

    try:
        while True:
            try:
                client.connect('127.0.0.1', 44405)
                while True:
                    if not client.wait_for_message():
                        break
            except Exception:
                pass
            time.sleep(2)
    except KeyboardInterrupt:
        pass
    finally:
        client.disconnect()


if __name__ == "__main__":
    main()
By default, in gpiozero, buttons are ACTIVE LOW, which means you need to use GND to trigger them.

Re: GPIO controls

Posted: Sun Apr 12, 2026 2:38 am
by AZSSG
That worked perfect! Thank you!