Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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:47] – code optimized vgaeteradocs: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| buttons using procd]]   * [[#procd_buttons| buttons using procd]]
-  * [[#Hotplug_Buttons|Hotplug buttons]], using the hotplug daemon or procd in compatibility mode (hotplug itself was phased out with r36003).+  * [[#Hotplug_Buttons|Hotplug buttons]], using the hotplug daemon or procd in compatibility mode (hotplug itself was phased out with r36003, circa 2013).
   * [[#HID_buttons|HID buttons]], using ///dev/input/event// with an application like triggerhappy.   * [[#HID_buttons|HID buttons]], using ///dev/input/event// with an application like triggerhappy.
  
Line 10: Line 10:
 Native button handling in procd is handled by scripts in ''/etc/rc.button/*''. Native button handling in procd is handled by scripts in ''/etc/rc.button/*''.
  
-These scripts receive the **same** environment as older style [[docs:guide-user:base-system:hotplug|hotplug]] buttons received. +These scripts receive the **same** environment as older style [[docs:guide-user:base-system:hotplug|hotplug]] buttons received.   
-However, the script files have to be named after the button. + 
-I am unaware of a way of getting the button name. +To get the button name you can try: 
-(Other than using hotplug compatible scripts with procd ;)+  * ''cat /sys/kernel/debug/gpio'' 
 +  * or use 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="released" SEEN="<seconds held>" | n/a | | Release | ACTION="released" SEEN="<seconds held>" | n/a |
  
-Note that "released" action is sent on release even if "timeout" has been sent.+:!: "released" action is sent on release even if "timeout" has been sent. 
 + 
 +<WRAP round box 60%> 
 +Please consider using these kernel codes when adding support for new devices, they're mapped by the gpio-button-hotplug kernel module: 
 +^ 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 | 
 + 
 +</WRAP>
  
 ===== Hotplug Buttons ===== ===== Hotplug Buttons =====
Line 34: Line 69:
  
 cat << "EOF" > /etc/hotplug.d/button/buttons cat << "EOF" > /etc/hotplug.d/button/buttons
-logger "the button was $BUTTON and the action was $ACTION"+logger "the button was ${BUTTONand the action was ${ACTION}"
 EOF EOF
 </code> </code>
Line 59: Line 94:
  
 ==== Using Atheros' 00-button + UCI ==== ==== Using Atheros' 00-button + UCI ====
-If you've installed the full version of ''wget'', run the following: 
- 
-<code bash> 
-wget -O /etc/hotplug.d/button/00-button https://dev.openwrt.org/export/36332/trunk/target/linux/atheros/base-files/etc/hotplug.d/button/00-button 
-</code> 
- 
-If you only have ''wget-nossl'' and don't want to or can't upgrade, copy-paste the following: 
- 
 <code bash> <code bash>
 cat << "EOF" > /etc/hotplug.d/button/00-button cat << "EOF" > /etc/hotplug.d/button/00-button
 source /lib/functions.sh source /lib/functions.sh
 +
 do_button () { do_button () {
     local button     local button
Line 77: Line 105:
     local max     local max
  
-    config_get button $1 button +    config_get button "${1}" button 
-    config_get action $1 action +    config_get action "${1}" action 
-    config_get handler $1 handler +    config_get handler "${1}" handler 
-    config_get min $1 min +    config_get min "${1}" min 
-    config_get max $1 max+    config_get max "${1}" max
  
-    [ "$ACTION" = "$action" -a "$BUTTON" = "$button" -a -n "$handler" ] && { +    [ "${ACTION}" = "${action}" -a "${BUTTON}" = "${button}" -a -n "${handler}" ] && { 
-        [ -z "$min" -o -z "$max" ] && eval $handler +        [ -z "${min}" -o -z "${max}" ] && eval ${handler} 
-        [ -n "$min" -a -n "$max" ] && { +        [ -n "${min}" -a -n "${max}" ] && { 
-            [ $min -le $SEEN -a $max -ge $SEEN ] && eval $handler+            [ "${min}" -le "${SEEN}" -a "${max}" -ge "${SEEN}" ] && eval ${handler}
         }         }
     }     }
Line 94: Line 122:
 config_foreach do_button button config_foreach do_button button
 EOF EOF
-</code> 
  
-Please note that after r34793 /etc/functions.sh -> /lib/functions.sh so if you are using an old version change it! 
- 
-Save and exit, then issue these commands: 
- 
-<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"
Line 121: Line 143:
 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' && wifi"
-uci commit system +uci commit system
 </code> </code>
  
Line 140: Line 162:
 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
 </code> </code>
  
Line 148: Line 170:
 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="for i in \$(mount | awk '/dev\/sd[b-z]/{print \$1}'); do umount \$i; done"+uci set system.@button[-1].handler="for i in \$(mount | awk '/dev\/sd[b-z]/{print \$1}'); do umount \${i}; done"
 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
 </code> </code>
  
Line 162: Line 184:
 uci set system.@button[-1].min="5" uci set system.@button[-1].min="5"
 uci set system.@button[-1].max="30" uci set system.@button[-1].max="30"
-uci commit system +uci commit system
 </code> </code>
  
Line 177: Line 199:
 cat << "EOF" > /usr/bin/wifionoff cat << "EOF" > /usr/bin/wifionoff
 #!/bin/sh #!/bin/sh
-[ "$BUTTON" = "BTN_1" ] && [ "$ACTION" = "pressed" ] && {+[ "${BUTTON}" = "BTN_1" ] && [ "${ACTION}" = "pressed" ] && {
     SW="$(uci -q get wireless.@wifi-device[0].disabled)"     SW="$(uci -q get wireless.@wifi-device[0].disabled)"
     [ "${SW}" = "1" ] \     [ "${SW}" = "1" ] \
Line 185: Line 207:
 } }
 EOF EOF
 +chmod u+x /usr/bin/wifionoff
 </code> </code>
  
Line 195: Line 218:
 STATEFILE="/tmp/wifionoff.state" STATEFILE="/tmp/wifionoff.state"
  
-if [ "${#}" -eq "1]; then+if [ "${#}" -eq 1 ]; then
     case "${1}" in     case "${1}" in
-        "up"|"on"STATE="off" ;; +        "up"|"on"GOAL="on" ;; 
-        "down"|"off"STATE="on" ;;+        "down"|"off"GOAL="off" ;;
     esac     esac
 else else
-    if [ -e "${STATEFILE}" ]; then +    if [ -e "${STATEFILE}" ]; then 
-        STATE="on"+ GOAL="on"
     else     else
-        source "${STATEFILE}"+        # if the statefile doesn't exist, turn wifi off 
 +        GOAL="off"
     fi     fi
 fi fi
-if [ -z "${STATE}" ]; then +  
-    STATE="on" +if [ "${GOAL}" = "off" ]; then
-fi +
- +
-if [ "${STATE}" = "on" ]; then+
     /sbin/wifi down     /sbin/wifi down
-    STATE="off"+    touch "${STATEFILE}"
 else else
     /sbin/wifi up     /sbin/wifi up
-    STATE="on"+    # file may not exist if we're given args 
 +    rm "${STATEFILE}2> /dev/null || true
 fi fi
 +EOF
 +chmod u+x /usr/bin/wifionoff
 +</code>
  
-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" in the script by the name of your wireless network configuration (eg. "cfg033579"). "default_radio0" is the configuration name of the initial default wireless network that existed "out of the box" (on the DIR-610-a1 at least). 
 + 
 +One way to find out your Wireless Network configuration name in LuCi is to navigate to the "Wireless Overview" page (Network > Wireless) and edit the wireless network configuration you need to toggle. For example, change temporarily the ESSID value and then click "SAVE" (and __NOT__ "SAVE & APPLY"). You will then see the button "UNSAVED CHANGES" in the upper right corner of the interface. Click on it and you should be able to find an entry such as "uci set wireless.cfg033579.ssid='My-Own-Wi-Fi-test'where "cfg033579" stands for your configuration name that you need to change in the following script. 
 + 
 +<code bash> 
 +uci add system button 
 +uci set system.@button[-1].button="wps" 
 +uci set system.@button[-1].action="released" 
 +uci set system.@button[-1].handler="/usr/bin/wifinetonoff" 
 +uci set system.@button[-1].min="0" 
 +uci set system.@button[-1].max="3" 
 +uci commit system 
 + 
 +cat << "EOF" > /usr/bin/wifinetonoff 
 +#!/bin/sh 
 +
 +    SW="$(uci -q get wireless.default_radio0.disabled)" 
 +    [ "${SW}" = "1" ] \ 
 +        && uci del wireless.default_radio0.disabled \ 
 +        || uci set wireless.default_radio0.disabled='1' 
 +    wifi 
 +}
 EOF EOF
 +chmod u+x /usr/bin/wifinetonoff
 </code> </code>
  
Line 249: Line 298:
 uci commit system uci commit system
 </code> </code>
- 
 ==== WR1043ND ==== ==== WR1043ND ====
 If you decide to use the ''wifitoggle'' package, you will need to change a few things on the default configuration. If you decide to use the ''wifitoggle'' package, you will need to change a few things on the default configuration.
Line 304: Line 352:
 Notes: Notes:
   * triggerhappy repeats commands twice: see bug https://dev.openwrt.org/ticket/14995   * triggerhappy repeats commands twice: see bug https://dev.openwrt.org/ticket/14995
-  * 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 /dev/input/event0 node for the buttons was created on the DIR-505 router with attached USB sound card.+  * kernel modules: **kmod-hid** and **kmod-hid-generic** both should be installed\\ The **kmod-hid-generic** and supposedly also **kmod-usb-hid** kernel module must be installed for buttons on USB devices such as USB sound cards to work in OpenWrt trunk. Only then the /dev/input/event0 node for the buttons was created on the DIR-505 router with attached USB sound card.
  
 <code> <code>
  • Last modified: 2022/09/25 20:08
  • by kirelagin