Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Next revisionBoth sides next revision | ||
| docs:guide-user:hardware:hardware.button [2019/07/16 13:20] – [triggerhappy] vgaetera | docs:guide-user:hardware:hardware.button [2022/04/27 15:48] – [Attach functions to a push button] some1 | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| There several ways for controlling buttons in OpenWrt. | There several ways for controlling buttons in OpenWrt. | ||
| * [[# | * [[# | ||
| - | * [[# | + | * [[# |
| * [[# | * [[# | ||
| Line 8: | Line 8: | ||
| ===== procd buttons ===== | ===== procd buttons ===== | ||
| - | Native button handling in procd is handled by scripts in '''/ | + | Native button handling in procd is handled by scripts in ''/ |
| + | |||
| + | These scripts receive the **same** environment as older style [[docs: | ||
| + | |||
| + | To get the button name you can try: | ||
| + | * '' | ||
| + | * or use hotplug compatible scripts with procd | ||
| - | These scripts receive the **same** environment as older style [[docs: | ||
| - | However, the script files have to be named after the button. | ||
| - | I am unaware of a way of getting the button name. | ||
| - | (Other than using hotplug compatible scripts with procd ;) | ||
| ^ Button Action ^ Script Environment ^ Script return value ^ | ^ Button Action ^ Script Environment ^ Script return value ^ | ||
| Line 20: | Line 22: | ||
| | Release | ACTION=" | | Release | ACTION=" | ||
| - | Note that " | + | :!: " |
| + | |||
| + | <WRAP round box 60%> | ||
| + | Please consider using these kernel codes when adding support for new devices, they' | ||
| + | ^ Kernel code ^ event ^ | ||
| + | | BTN_0 | BTN_0 | | ||
| + | | BTN_1 | BTN_1 | | ||
| + | | BTN_2 | BTN_2 | | ||
| + | | BTN_3 | BTN_3 | | ||
| + | | BTN_4 | BTN_4 | | ||
| + | | BTN_5 | BTN_5 | | ||
| + | | BTN_6 | BTN_6 | | ||
| + | | BTN_7 | BTN_7 | | ||
| + | | BTN_8 | BTN_8 | | ||
| + | | BTN_9 | BTN_9 | | ||
| + | | KEY_BRIGHTNESS_ZERO | brightness_zero | | ||
| + | | KEY_CONFIG | config | | ||
| + | | KEY_COPY | copy | | ||
| + | | KEY_EJECTCD | eject | | ||
| + | | KEY_HELP | help | | ||
| + | | KEY_LIGHTS_TOGGLE | lights_toggle | | ||
| + | | KEY_PHONE | phone | | ||
| + | | **KEY_POWER** | **power** | | ||
| + | | **KEY_POWER2** | **reboot** | | ||
| + | | **KEY_RESTART** | **reset** | | ||
| + | | **KEY_RFKILL** | **rfkill** | | ||
| + | | KEY_VIDEO | video | | ||
| + | | KEY_VOLUMEDOWN | volume_down | | ||
| + | | KEY_VOLUMEUP | volume_up | | ||
| + | | KEY_WIMAX | wwan | | ||
| + | | KEY_WLAN | wlan | | ||
| + | | KEY_WPS_BUTTON | wps | | ||
| + | |||
| + | </ | ||
| ===== Hotplug Buttons ===== | ===== Hotplug Buttons ===== | ||
| Line 27: | Line 62: | ||
| ==== Preliminary steps ==== | ==== Preliminary steps ==== | ||
| - | The first step is to find out the internal name of the button you want to use: some images use generic names such as '' | + | The first step is to find out the internal name of the button you want to use: some images use generic names such as '' |
| + | Run the following: | ||
| <code bash> | <code bash> | ||
| Line 33: | Line 69: | ||
| cat << " | cat << " | ||
| - | logger "the button was $BUTTON and the action was $ACTION" | + | logger "the button was ${BUTTON} and the action was ${ACTION}" |
| EOF | EOF | ||
| </ | </ | ||
| Line 46: | Line 82: | ||
| </ | </ | ||
| - | '' | + | '' |
| + | If you want or need to use another button, replace every instance of '' | ||
| + | From now on, there are several possible approaches: the first uses the '' | ||
| If you want to run programs from hotplug' | If you want to run programs from hotplug' | ||
| Line 56: | Line 94: | ||
| ==== Using Atheros' | ==== Using Atheros' | ||
| - | If you've installed the full version of '' | ||
| - | |||
| - | <code bash> | ||
| - | wget -O / | ||
| - | </ | ||
| - | |||
| - | If you only have '' | ||
| - | |||
| <code bash> | <code bash> | ||
| cat << " | cat << " | ||
| - | #!/bin/sh | ||
| source / | source / | ||
| + | |||
| do_button () { | do_button () { | ||
| local button | local button | ||
| Line 75: | Line 105: | ||
| local max | local max | ||
| - | config_get button $1 button | + | config_get button |
| - | config_get action $1 action | + | config_get action |
| - | config_get handler $1 handler | + | config_get handler |
| - | config_get min $1 min | + | config_get min "${1}" |
| - | config_get max $1 max | + | config_get max "${1}" |
| - | [ " | + | [ "${ACTION}" = "${action}" -a "${BUTTON}" = "${button}" -a -n "${handler}" ] && { |
| - | [ -z " | + | [ -z "${min}" -o -z "${max}" ] && eval ${handler} |
| - | [ -n " | + | [ -n "${min}" -a -n "${max}" ] && { |
| - | [ $min -le $SEEN -a $max -ge $SEEN ] && eval $handler | + | [ "${min}" |
| } | } | ||
| } | } | ||
| Line 92: | Line 122: | ||
| config_foreach do_button button | config_foreach do_button button | ||
| EOF | EOF | ||
| - | </ | ||
| - | Please note that after r34793 / | ||
| - | |||
| - | Save and exit, then issue these commands: | ||
| - | |||
| - | <code bash> | ||
| uci add system button | uci add system button | ||
| uci set system.@button[-1].button=" | uci set system.@button[-1].button=" | ||
| Line 108: | Line 132: | ||
| '' | '' | ||
| - | You may need to reboot the router the make the change effective (mine would work with the simple shell script just fine but wouldn' | + | You may need to reboot the router the make the change effective (mine would work with the simple shell script just fine but wouldn' |
| - | + | If this works, you can change the handler to something more useful, and add more button handlers. | |
| - | === Examples === | + | |
| + | ==== Examples ==== | ||
| **Example 1:** //Toggle Wi-Fi radio with a button press// | **Example 1:** //Toggle Wi-Fi radio with a button press// | ||
| Line 119: | Line 143: | ||
| uci set system.@button[-1].action=" | uci set system.@button[-1].action=" | ||
| uci set system.@button[-1].handler=" | uci set system.@button[-1].handler=" | ||
| - | uci commit system | + | uci commit system |
| </ | </ | ||
| - | **Example 2:** //Assign two different functions to the same button: short press VS long press. This relies on tracking the // | + | **Example 2:** //Assign two different functions to the same button: short press VS long press. |
| + | This relies on tracking the // | ||
| <code bash> | <code bash> | ||
| Line 137: | Line 162: | ||
| uci set system.@button[-1].min=" | uci set system.@button[-1].min=" | ||
| uci set system.@button[-1].max=" | uci set system.@button[-1].max=" | ||
| - | uci commit system | + | uci commit system |
| </ | </ | ||
| Line 145: | Line 170: | ||
| uci set system.@button[-1].button=" | uci set system.@button[-1].button=" | ||
| uci set system.@button[-1].action=" | uci set system.@button[-1].action=" | ||
| - | uci set system.@button[-1].handler=" | + | uci set system.@button[-1].handler=" |
| uci set system.@button[-1].min=" | uci set system.@button[-1].min=" | ||
| uci set system.@button[-1].max=" | uci set system.@button[-1].max=" | ||
| - | uci commit system | + | uci commit system |
| </ | </ | ||
| Line 159: | Line 184: | ||
| uci set system.@button[-1].min=" | uci set system.@button[-1].min=" | ||
| uci set system.@button[-1].max=" | uci set system.@button[-1].max=" | ||
| - | uci commit system | + | uci commit system |
| </ | </ | ||
| Line 174: | Line 199: | ||
| cat << " | cat << " | ||
| #!/bin/sh | #!/bin/sh | ||
| - | SW=" | + | [ " |
| - | [ " | + | |
| - | [ " | + | [ "${SW}" = " |
| - | wifi | + | |
| + | || uci set wireless.@wifi-device[0].disabled=" | ||
| + | wifi | ||
| + | } | ||
| EOF | EOF | ||
| + | chmod u+x / | ||
| </ | </ | ||
| Another option for wifionoff is this script (doesn' | Another option for wifionoff is this script (doesn' | ||
| - | You can also call this script | + | You can also call this script |
| <code bash> | <code bash> | ||
| Line 189: | Line 218: | ||
| STATEFILE="/ | STATEFILE="/ | ||
| - | if [ $# -eq 1 ]; then | + | if [ "${#}" |
| - | case $1 in | + | case "${1}" |
| - | " | + | " |
| - | STATE=off | + | " |
| - | | + | esac |
| - | " | + | |
| - | STATE=on | + | |
| - | | + | |
| - | esac | + | |
| else | else | ||
| - | | + | |
| - | STATE=on | + | GOAL="on" |
| - | else | + | else |
| - | | + | # if the statefile doesn' |
| - | | + | GOAL=" |
| + | fi | ||
| fi | fi | ||
| - | if [ -z ${STATE} ]; then | + | |
| - | STATE=on | + | if [ "${GOAL}" |
| - | fi | + | /sbin/wifi down |
| - | + | touch " | |
| - | if [ ${STATE} == "on" ]; then | + | |
| - | /sbin/wifi down | + | |
| - | | + | |
| else | else | ||
| - | | + | |
| - | | + | # file may not exist if we're given args |
| + | rm " | ||
| fi | fi | ||
| + | EOF | ||
| + | chmod u+x / | ||
| + | </ | ||
| - | echo "STATE=${STATE}" > ${STATEFILE} | + | **Example 5-bis:** //Toggle only a Wireless Network using a script without disabling the entire Wi-Fi module// |
| + | |||
| + | This solution is heavily based on example 5. You need to figure out the name of your Wi-Fi Network configuration to make it work and replace the 3 occurrences of "default_radio0" | ||
| + | |||
| + | One way to find out your Wireless Network configuration name in LuCi is to navigate to the " | ||
| + | |||
| + | <code bash> | ||
| + | uci add system button | ||
| + | uci set system.@button[-1].button=" | ||
| + | uci set system.@button[-1].action=" | ||
| + | uci set system.@button[-1].handler="/ | ||
| + | uci set system.@button[-1].min=" | ||
| + | uci set system.@button[-1].max=" | ||
| + | uci commit system | ||
| + | |||
| + | cat << " | ||
| + | #!/bin/sh | ||
| + | { | ||
| + | SW=" | ||
| + | [ "${SW}" = " | ||
| + | && uci del wireless.default_radio0.disabled \ | ||
| + | || uci set wireless.default_radio0.disabled=' | ||
| + | wifi | ||
| + | } | ||
| EOF | EOF | ||
| + | chmod u+x / | ||
| </ | </ | ||
| Line 247: | Line 298: | ||
| uci commit system | uci commit system | ||
| </ | </ | ||
| - | |||
| - | ==== Leftovers from a previous version ==== | ||
| - | |||
| - | FIXME | ||
| - | |||
| - | <code bash> | ||
| - | if [ " | ||
| - | if [ " | ||
| - | elif [ " | ||
| - | fi | ||
| - | fi | ||
| - | </ | ||
| - | |||
| - | <code bash> | ||
| - | mkdir -p / | ||
| - | wget -O / | ||
| - | wget -O http:// | ||
| - | </ | ||
| - | |||
| - | <code bash> | ||
| - | #!/bin/sh | ||
| - | [ " | ||
| - | SW=$(uci get wireless.@wifi-device[0].disabled) | ||
| - | [ $SW == ' | ||
| - | [ $SW == ' | ||
| - | wifi | ||
| - | } | ||
| - | </ | ||
| - | |||
| ==== WR1043ND ==== | ==== WR1043ND ==== | ||
| - | If you decide to use the '' | + | If you decide to use the '' |
| + | The following will work and make the QSS led blink " | ||
| <code bash> | <code bash> | ||
| Line 327: | Line 350: | ||
| </ | </ | ||
| - | ==== Notes ==== | + | Notes: |
| * triggerhappy repeats commands twice: see bug https:// | * triggerhappy repeats commands twice: see bug https:// | ||
| - | * kernel modules: **kmod-hid** and **kmod-hid-generic** both should be installed\\ The kmod-hid-generic kernel module must be installed for buttons on USB devices such as USB sound cards to work in OpenWrt trunk. Only then the / | + | * kernel modules: **kmod-hid** and **kmod-hid-generic** both should be installed\\ The **kmod-hid-generic** and supposedly also **kmod-usb-hid** |
| < | < | ||
| Line 344: | Line 367: | ||
| ===== Sliding switches ===== | ===== Sliding switches ===== | ||
| - | Some routers, for example the [[toh: | + | Some routers, for example the [[toh: |
| + | These are usually implemented using two GPIOs, meaning OpenWrt interprets a switch like this as two separate push buttons. | ||
| - | The '' | + | The '' |
| + | Buttons scripts, in either procd or hotplug format, can be written for switch positions directly. | ||
| + | See the package' | ||