OpenWrt Forum Archive

Topic: Read buttons' state?

The content of this topic has been archived on 23 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Is there any way, command or script, to read current buttons' state?
I want to write a script that will do various things according to the slide switch of my MR3020. The script will be placed in /etc/hotplug.d/button/ so every time the switch changes position the script will be executed, then in the script it will read BTN_0 and BTN_1 states and execute commands accordingly.

Thanks!

Do you really need to read button state? See hardware button

But how do I know if the switch is on AP? Because both BTN_0 and BTN_1 will be high.

$BUTTON and $ACTION are passed to /etc/hotplug.d/button
So you just need to e.g. check $BUTTON = BTN_1 & $ACTION =pressed (or released) and execute code.

Your device's wiki says:
http://wiki.openwrt.org/toh/tp-link/tl-mr3020#buttons

Sliding Switch / BTN_0 and BTN_1
WPS Button / WPS

The WPS button is located at the top (illuminated by the WPS LED) and can be easily pressed with a finger. The sliding switch is located at the side and has three positions: 3G, WISP, AP.

That is a bit ambiguous regarding the sliding switch. It might be that the slide set two actions at the same time, e.g. BTN0 pressed and BTN1 released. Or it might really have actions for "3G" etc.

You might write a short script to investigate how the various buttons (and the "slide switch") react to your physical handling, i.e. which actions are produced.

E.g. this script would write the button action message to your system log, if you place the script as the file "/etc/hotplug.d/button/01-log-button-actions" to your live Openwrt system:

#!/bin/sh
logger "Button '$BUTTON' was '$ACTION'"

Thanks for all the help!
On the same wiki page, there's a truth table of BTN_0 and BTN_1 and how the switch is in position. As in the "AP" position, both of the BTN_0 and BTN_1 will be high, and this is what I can't figure out. It's easy to execute codes if only one button is pressed, but two at the same time, it'll be a little tricky.

Edit: I just tested the relationship of position and the two buttons. It's like this:
AP=BTN_0 high, BTN_1 high
WISP=BTN_0 low, BTN_1 high
3G=BTN_0 high, BTN_1 low

(Last edited by applefreak on 22 Feb 2013, 12:54)

if BTN_0 & BTN_1 do AP code
elseif !BTN_0 & BTN_1 do WISP code.....
you get the picture?

I also needed to get initial button state (slider state on MR3020 in my case). Googling gave no results, so after some experiments I came to this:

#!/bin/sh
rmmod gpio_button_hotplug
echo 18 > /sys/class/gpio/export
echo 20 > /sys/class/gpio/export
GPIO_18=$(cat /sys/class/gpio/gpio18/value)
GPIO_20=$(cat /sys/class/gpio/gpio20/value)
echo 18 > /sys/class/gpio/unexport
echo 20 > /sys/class/gpio/unexport
insmod /lib/modules/3.10.32/gpio-button-hotplug.ko
echo Current state is $GPIO_18$GPIO_20 >/tmp/gpio_state
# next step you can use case statement to see if "$GPIO_18$GPIO_20" is "10", "01" or "11" to get actual slider state (left, middle or right).

Here I remove the kernel module that uses that GPIOs (gpio_button_hotplug, it starts automatically during system startup), export the GPIOs, read their state, unexport them, and restart gpio_button_hotplug kernel module (so that hotplug script can be run later).
Hope it'll be useful for someone.

You can read the gpio ports status from /sys/kernel/debug/gpio

For examle:

#!/bin/sh
if (grep -oq "sw1.*hi" /sys/kernel/debug/gpio)
then
  if (grep -oq "sw2.*hi" /sys/kernel/debug/gpio)
  then
    echo sliding switch on AP
  else
    echo sliding switch on 3G 
  fi
else
  echo sliding switch on WISP
fi

Hi,

I have configured the Slide switch on GPIO16 and added the following condition in /etc/hotplug.d/button/50-button file as below:

if [ "$ACTION" = "pressed" -a "$BUTTON" = "BUT_2" ]; then
        echo "255" > /sys/devices/platform/leds-gpio/leds/db120:green:status/brightness       
fi


if [ "$ACTION" = "released" -a "$BUTTON" = "BUT_2" ]; then
        echo "0" > /sys/devices/platform/leds-gpio/leds/db120:green:status/brightness       
fi

It works fine ON/OFF LEDs based on Switch but when i do reboot the system , after that LED will ON only,  even if i set BUT_2 release mode and reboot the system.

If I again switch then it works but i need to check state after reboot without moving the switch button.

I don't know if it's the same on MR3020 but on my wr1043nd I can check the state of buttons with

# cat /sys/kernel/debug/gpio

GPIOs 0-21, ath79:
 gpio-1   (tp-link:green:usb   ) out hi
 gpio-2   (tp-link:green:system) out lo
 gpio-3   (reset               ) in  hi
 gpio-5   (tp-link:green:qss   ) out lo
 gpio-7   (qss                 ) in  hi
 gpio-9   (tp-link:green:wlan  ) out lo
 gpio-18  (rtl8366rb           ) in  hi
 gpio-19  (rtl8366rb           ) in  hi
 gpio-20  (sysfs               ) in  hi

This does not require any rmmod command and hotplug still works normally.

The discussion might have continued from here.