Toggle LED with push button

I'm currently working on device support for the TP-Link EAP225-Wall, which has a GPIO push button that can be used to toggle the status LED with the stock firmware.

The gpio led trigger can be used to link an input GPIO to an led, but that's a one-to-one translation of the input state. Having to hold the button down all the time to keep the LED off doesn't sound like a good user experience to me.

Does anybody know of a way to use button/GPIO events to toggle an LED?

Last time I worked with buttons and leds, I wrote my own daemon to monitor button holding times (long press means a factory reset, short press changes a config, for instance). I followed the suggestions at https://elinux.org/GPIO#GPIO_interrupts_from_user_space

At that time, using trigger-happy was not possible as suggested at https://openwrt.org/docs/guide-user/hardware/hardware.button#hid_buttons

1 Like

If the the LED is currently uncontrolled, you can simply write something to /sys/class/ledx/.../brightness that has 0 for dark and 1-255 for lit LED (if it is just on/off LED).
echo "255" > /sys/class/leds/r7800:amber:power/brightness

Examples e.g. in [Fixed] Wps to trigger power-led:orange to blink

Put that command to your button's hotplug script in /etc/rc.button

root@router1:~# ls /etc/rc.button/
failsafe  power     reboot    reset     rfkill    wps
1 Like

The hardware.button wiki page lists KEY_LIGHTS_TOGGLE as one of the supported keys, for which I can add a script in /etc/rc.button/lights_toggle.

To disable the LED(s), I could probably just loop over all buttons and set trigger to 'none' and default to '0'. That would mean I lost all user config though, so I'll have a look at how to avoid that.

Edit: Found a similar use case for KEY_LIGHTS_TOGGLE

Edit:
DTS entry:

led {
	label = "LED button";
	linux,code = <KEY_LIGHTS_TOGGLE>;
	gpios = <&gpio 21 GPIO_ACTIVE_LOW>;
	debounce-interval = <60>;
};

Button handler script for the TP-Link EAP225-Wall:

#!/bin/sh

[ "${ACTION}" = "released" ] || exit 0

LED_PATH="/sys/class/leds/tp-link:white:status/brightness"
CURRENT=$(cat $LED_PATH)
if [[ $CURRENT == 0 ]]; then
	echo 255 > $LED_PATH
else
	echo 0 > $LED_PATH
fi
return 0

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.