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:user-guide:hardware:hardware.button [2018/02/17 20:55] – ↷ Links adapted because of a move operation | 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. | ||
| * [[# | * [[# | ||
| - | * [[# | + | * [[# |
| * [[# | * [[# | ||
| - | |||
| - | |||
| | {{: | | {{: | ||
| ===== procd buttons ===== | ===== procd buttons ===== | ||
| - | native | + | Native |
| - | These scripts receive the **same** environment as older style hotplug buttons received. | + | These scripts receive the **same** environment as older style [[docs: |
| - | ^ Button Action ^ Script Environment ^ Script return value ^ | + | To get the button name you can try: |
| - | | Press | + | * '' |
| + | * or use hotplug compatible scripts with procd | ||
| + | |||
| + | |||
| + | ^ Button Action ^ Script Environment ^ Script return value ^ | ||
| + | | Press | ACTION=" | ||
| | Held " | | Held " | ||
| - | | Release< | + | | Release | ACTION=" |
| + | |||
| + | :!: " | ||
| + | |||
| + | <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 | | ||
| + | |||
| + | </ | ||
| - | 1 - " | ||
| ===== Hotplug Buttons ===== | ===== Hotplug Buttons ===== | ||
| - | | {{: | + | | {{: |
| - | | FIXME | Please read the articles [[docs:user-guide:services: | + | | FIXME | Please read the articles [[docs: |
| ==== 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> | ||
| - | # mkdir -p / | + | mkdir -p / |
| - | </ | + | |
| - | Create the file '' | + | cat << " |
| - | + | logger | |
| - | <code bash> | + | EOF |
| - | #!/bin/sh | + | |
| - | logger the button was $BUTTON and the action was $ACTION | + | |
| </ | </ | ||
| - | Save and exit. Now press the button you want to use, then run '' | + | Now press the button you want to use, then run '' |
| <code bash> | <code bash> | ||
| - | Jan 1 00:01:15 OpenWrt user.notice root: BTN_1 | + | Jan 1 00:01:15 OpenWrt user.notice root: BTN_1 |
| - | Jan 1 00:01:15 OpenWrt user.notice root: pressed | + | Jan 1 00:01:15 OpenWrt user.notice root: pressed |
| - | Jan 1 00:01:16 OpenWrt user.notice root: BTN_1 | + | Jan 1 00:01:16 OpenWrt user.notice root: BTN_1 |
| - | Jan 1 00:01:16 OpenWrt user.notice root: released | + | Jan 1 00:01:16 OpenWrt user.notice root: released |
| </ | </ | ||
| - | '' | + | '' |
| + | 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 '' | ||
| - | **notice** | + | If you want to run programs from hotplug' |
| - | + | Especially if you install stuff into nonstandard locations like / | |
| - | If you want to run programs from hotplug' | + | |
| <code bash> | <code bash> | ||
| - | #!/bin/sh | + | source |
| - | . / | + | |
| </ | </ | ||
| ==== Using Atheros' | ==== Using Atheros' | ||
| - | If you've installed the full version of '' | ||
| - | |||
| <code bash> | <code bash> | ||
| - | # wget -O / | + | cat << " |
| - | </ | + | source |
| - | If you only have '' | ||
| - | |||
| - | <code bash> | ||
| - | #!/bin/sh | ||
| - | . / | ||
| do_button () { | do_button () { | ||
| - | | + | |
| - | local action | + | local action |
| - | local handler | + | local handler |
| - | local min | + | local min |
| - | local max | + | local max |
| - | | + | |
| - | 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}" |
| - | | + | |
| - | [ -z " | + | [ -z "${min}" -o -z "${max}" ] && eval ${handler} |
| - | [ -n " | + | [ -n "${min}" -a -n "${max}" ] && { |
| - | [ $min -le $SEEN -a $max -ge $SEEN ] && eval $handler | + | [ "${min}" |
| - | | + | |
| } | } | ||
| + | } | ||
| } | } | ||
| config_load system | config_load system | ||
| config_foreach do_button button | config_foreach do_button button | ||
| - | </ | + | EOF |
| - | Please note that after r34793 / | + | uci add system button |
| - | + | uci set system.@button[-1].button="BTN_1" | |
| - | Save and exit, then issue these commands: | + | uci set system.@button[-1].action="pressed" |
| - | + | uci set system.@button[-1].handler="logger BTN_1 pressed" | |
| - | <code bash> | + | |
| - | uci add system button | + | |
| - | uci set system.@button[-1].button=BTN_1 | + | |
| - | uci set system.@button[-1].action=pressed | + | |
| - | uci set system.@button[-1].handler='logger BTN_1 pressed' | + | |
| uci commit system | uci commit system | ||
| </ | </ | ||
| Line 110: | 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// | ||
| <code bash> | <code bash> | ||
| - | uci add system button | + | uci add system button |
| - | uci set system.@button[-1].button=wps | + | uci set system.@button[-1].button="wps" |
| - | uci set system.@button[-1].action=pressed | + | uci set system.@button[-1].action="pressed" |
| - | uci set system.@button[-1].handler='uci set wireless.@wifi-device[0].disabled=1 && wifi' | + | uci set system.@button[-1].handler="uci set wireless.@wifi-device[0].disabled='1' |
| - | 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> | ||
| uci add system button | uci add system button | ||
| - | uci set system.@button[-1].button=BTN_1 | + | uci set system.@button[-1].button="BTN_1" |
| - | uci set system.@button[-1].action=released | + | uci set system.@button[-1].action="released" |
| - | uci set system.@button[-1].handler='logger timed pressed: 0-3s' | + | uci set system.@button[-1].handler="logger timed pressed: 0-3s" |
| - | uci set system.@button[-1].min=0 | + | uci set system.@button[-1].min="0" |
| - | uci set system.@button[-1].max=3 | + | uci set system.@button[-1].max="3" |
| uci add system button | uci add system button | ||
| - | uci set system.@button[-1].button=BTN_1 | + | uci set system.@button[-1].button="BTN_1" |
| - | uci set system.@button[-1].action=released | + | uci set system.@button[-1].action="released" |
| - | uci set system.@button[-1].handler='logger timed pressed: 8-10s' | + | uci set system.@button[-1].handler="logger timed pressed: 8-10s" |
| - | uci set system.@button[-1].min=8 | + | uci set system.@button[-1].min="8" |
| - | uci set system.@button[-1].max=10 | + | uci set system.@button[-1].max="10" |
| - | uci commit system | + | uci commit system |
| </ | </ | ||
| Line 145: | Line 168: | ||
| <code bash> | <code bash> | ||
| uci add system button | uci add system button | ||
| - | uci set system.@button[-1].button=BTN_1 | + | uci set system.@button[-1].button="BTN_1" |
| - | uci set system.@button[-1].action=released | + | uci set system.@button[-1].action="released" |
| - | uci set system.@button[-1].handler=" | + | uci set system.@button[-1].handler=" |
| - | uci set system.@button[-1].min=5 | + | uci set system.@button[-1].min="5" |
| - | uci set system.@button[-1].max=10 | + | uci set system.@button[-1].max="10" |
| - | uci commit system | + | uci commit system |
| </ | </ | ||
| **Example 4:** //Restore defaults// | **Example 4:** //Restore defaults// | ||
| <code bash> | <code bash> | ||
| - | config | + | uci add system |
| - | | + | uci set system.@button[-1].button=" |
| - | | + | uci set system.@button[-1].action="released" |
| - | | + | uci set system.@button[-1].handler=" |
| - | | + | uci set system.@button[-1].min="5" |
| - | | + | uci set system.@button[-1].max="30" |
| + | uci commit system | ||
| </ | </ | ||
| **Example 5:** //Toggle Wi-Fi using a script// | **Example 5:** //Toggle Wi-Fi using a script// | ||
| <code bash> | <code bash> | ||
| - | config | + | uci add system |
| - | | + | uci set system.@button[-1].button=" |
| - | | + | uci set system.@button[-1].action="released" |
| - | | + | uci set system.@button[-1].handler="/ |
| - | | + | uci set system.@button[-1].min="0" |
| - | | + | uci set system.@button[-1].max="3" |
| - | </ | + | uci commit system |
| - | You'll have to create the file '' | + | cat << " |
| - | <code bash> | + | |
| #!/bin/sh | #!/bin/sh | ||
| - | SW=$(uci -q get wireless.@wifi-device[0].disabled) | + | [ " |
| - | [ " | + | |
| - | [ " | + | [ "${SW}" = " |
| - | wifi | + | |
| + | | ||
| + | wifi | ||
| + | } | ||
| + | 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> | ||
| + | cat << " | ||
| #!/bin/sh | #!/bin/sh | ||
| 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 |
| - | echo " | + | chmod u+x / |
| </ | </ | ||
| - | **Example | + | **Example |
| + | |||
| + | 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 " | ||
| + | |||
| + | One way to find out your Wireless Network configuration name in LuCi is to navigate to the " | ||
| - | Edit your alt-speed limits from transmission-daemon , // | ||
| <code bash> | <code bash> | ||
| - | uci add system button | ||
| - | uci set system.@button[-1].button=BTN_1 | ||
| - | uci set system.@button[-1].action=pressed | ||
| - | uci set system.@button[-1].handler=' | ||
| - | uci add system button | ||
| - | uci set system.@button[-1].button=BTN_1 | ||
| - | uci set system.@button[-1].action=pressed | ||
| - | uci set system.@button[-1].handler=' | ||
| uci add system button | uci add system button | ||
| - | uci set system.@button[-1].button=BTN_1 | + | uci set system.@button[-1].button=" |
| - | uci set system.@button[-1].action=released | + | uci set system.@button[-1].action="released" |
| - | uci set system.@button[-1].handler=' | + | uci set system.@button[-1].handler="/usr/bin/wifinetonoff" |
| - | uci set system.@button[-1].min=1 | + | uci set system.@button[-1].min=" |
| - | uci set system.@button[-1].max=4 | + | uci set system.@button[-1].max=" |
| - | uci add system button | + | |
| - | uci set system.@button[-1].button=BTN_1 | + | |
| - | uci set system.@button[-1].action=released | + | |
| - | uci set system.@button[-1].handler=' | + | |
| - | uci set system.@button[-1].min=1 | + | |
| - | uci set system.@button[-1].max=4 | + | |
| uci commit system | uci commit system | ||
| + | |||
| + | cat << " | ||
| + | #!/bin/sh | ||
| + | { | ||
| + | SW=" | ||
| + | [ " | ||
| + | && uci del wireless.default_radio0.disabled \ | ||
| + | || uci set wireless.default_radio0.disabled=' | ||
| + | wifi | ||
| + | } | ||
| + | EOF | ||
| + | chmod u+x / | ||
| </ | </ | ||
| - | ==== Leftovers from a previous version ==== | + | **Example 6:** //Set transmission-daemon alt-speed, enable or disable.Short press will activate alt-speed or longer press will deactivate alt-speed and also turns on qss led about speed status on tl-wr1043nd// |
| - | + | ||
| - | FIXME | + | |
| + | Edit your alt-speed limits from transmission-daemon , // | ||
| <code bash> | <code bash> | ||
| - | mkdir -p /etc/hotplug.d/button | + | uci add system button |
| - | touch /etc/hotplug.d/button/00-button | + | uci set system.@button[-1].button=" |
| + | uci set system.@button[-1].action=" | ||
| + | uci set system.@button[-1].handler=" | ||
| + | uci add system button | ||
| + | uci set system.@button[-1].button=" | ||
| + | uci set system.@button[-1].action=" | ||
| + | uci set system.@button[-1].handler=" | ||
| + | uci add system | ||
| + | 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 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 | ||
| </ | </ | ||
| + | ==== WR1043ND ==== | ||
| + | If you decide to use the '' | ||
| + | The following will work and make the QSS led blink " | ||
| <code bash> | <code bash> | ||
| - | if [ "$ACTION" = "pressed" | + | uci add wifitoggle wifitoggle |
| - | | + | uci set wifitoggle.@wifitoggle[0].led_enable_trigger="timer" |
| - | | + | uci set wifitoggle.@wifitoggle[0].persistent="1" |
| - | | + | uci set wifitoggle.@wifitoggle[0].button="BTN_1" |
| - | fi | + | uci set wifitoggle.@wifitoggle[0].led_sysfs="tl-wr1043nd: |
| + | uci set wifitoggle.@wifitoggle[0].led_enable_delayon="2000" | ||
| + | uci set wifitoggle.@wifitoggle[0].led_disable_default="1" | ||
| + | uci set wifitoggle.@wifitoggle[0].led_enable_delayoff=" | ||
| + | uci set wifitoggle.@wifitoggle[0].timer=" | ||
| + | uci commit wifitoggle | ||
| </ | </ | ||
| - | <code bash> | + | :!: //You can probably get similar behaviour with [[docs: |
| - | mkdir -p /etc/hotplug.d/ | + | |
| - | wget -O / | + | |
| - | wget -O http://dev.openwrt.org/export/ | + | |
| - | </code> | + | |
| - | <code bash> | + | ===== HID buttons ===== |
| - | #!/bin/sh | + | ==== triggerhappy |
| - | [ " | + | To manage |
| - | SW=$(uci get wireless.@wifi-device[0].disabled) | + | |
| - | [ $SW == ' | + | |
| - | [ $SW == ' | + | |
| - | wifi | + | |
| - | } | + | |
| - | </ | + | |
| - | + | ||
| - | ==== WR1043ND | + | |
| - | If you decide to use the '' | + | |
| <code bash> | <code bash> | ||
| - | uci show wifitoggle | + | # Install packages |
| - | uci set wifitoggle.@wifitoggle[0]=wifitoggle | + | opkg update |
| - | uci set wifitoggle.@wifitoggle[0].led_enable_trigger=timer | + | opkg install triggerhappy kmod-hid |
| - | uci set wifitoggle.@wifitoggle[0].persistent=1 | + | |
| - | uci set wifitoggle.@wifitoggle[0].button=BTN_1 | + | |
| - | uci set wifitoggle.@wifitoggle[0].led_sysfs=tl-wr1043nd: | + | |
| - | uci set wifitoggle.@wifitoggle[0].led_enable_delayon=2000 | + | |
| - | uci set wifitoggle.@wifitoggle[0].led_disable_default=1 | + | |
| - | uci set wifitoggle.@wifitoggle[0].led_enable_delayoff=3000 | + | |
| - | uci set wifitoggle.@wifitoggle[0].timer=0 | + | |
| - | </ | + | |
| - | :!: //You can probably get similar behaviour with [[doc: | + | # List your available buttons |
| - | ===== HID buttons ===== | + | thd --dump |
| - | ==== triggerhappy ==== | + | # Press your buttons |
| - | To manage the router | + | EV_KEY |
| - | === Installation === | + | |
| - | - Install the **triggerhappy** package and the **kmod-hid** kernel module | + | |
| - | - list your available buttons: execute< | + | |
| # KEY_WPS_BUTTON | # KEY_WPS_BUTTON | ||
| EV_KEY | EV_KEY | ||
| Line 309: | Line 337: | ||
| # KEY_VOLUMEDOWN | # KEY_VOLUMEDOWN | ||
| EV_KEY | EV_KEY | ||
| - | # KEY_VOLUMEDOWN | + | # KEY_VOLUMEDOWN |
| - | - Now associate | + | |
| + | # Associate | ||
| + | cat << " | ||
| + | KEY_WPS_BUTTON 1 / | ||
| KEY_VOLUMEUP 1 amixer -q set Speaker 3%+ | KEY_VOLUMEUP 1 amixer -q set Speaker 3%+ | ||
| - | KEY_VOLUMEDOWN 1 amixer -q set Speaker 3%-</ | + | KEY_VOLUMEDOWN 1 amixer -q set Speaker 3%- |
| - | - run triggerhappy \\ <code bash>/ | + | EOF |
| - | - enable triggerhappy permanently\\ <code bash>/ | + | |
| - | ==== Notes ==== | + | # Restart services |
| + | service triggerhappy restart | ||
| + | </ | ||
| + | |||
| + | Notes: | ||
| * triggerhappy repeats commands twice: see bug https:// | * triggerhappy repeats commands twice: see bug https:// | ||
| - | * kernel modules: | + | * kernel modules: **kmod-hid** and **kmod-hid-generic** both should be installed\\ The **kmod-hid-generic** and supposedly also **kmod-usb-hid** |
| + | |||
| + | < | ||
| [ | [ | ||
| [ | [ | ||
| [ | [ | ||
| [ | [ | ||
| - | </ | + | </ |
| + | |||
| + | This is also noted in https:// | ||
| ==== cmdpad ==== | ==== cmdpad ==== | ||
| Another simpler application to manage buttons. | Another simpler application to manage buttons. | ||
| + | |||
| + | ===== Sliding switches ===== | ||
| + | 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 '' | ||
| + | Buttons scripts, in either procd or hotplug format, can be written for switch positions directly. | ||
| + | See the package' | ||