32c142f110233b7c85472df7def2cbaeb44b5483
[openwrt/openwrt.git] / package / base-files / files / etc / init.d / gpio_switch
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2015 OpenWrt.org
3
4 START=94
5 STOP=10
6 USE_PROCD=1
7
8
9 load_gpio_switch()
10 {
11 local name
12 local gpio_pin
13 local value
14
15 config_get gpio_pin "$1" gpio_pin
16 config_get name "$1" name
17 config_get value "$1" value 0
18
19 local gpio_path="/sys/class/gpio/gpio${gpio_pin}"
20 # export GPIO pin for access
21 [ -d "$gpio_path" ] || {
22 echo "$gpio_pin" >/sys/class/gpio/export
23 # we need to wait a bit until the GPIO appears
24 [ -d "$gpio_path" ] || sleep 1
25 }
26 # set the pin to output with high or low pin value
27 { [ "$value" = "0" ] && echo "low" || echo "high"; } >"$gpio_path/direction"
28 }
29
30 service_triggers()
31 {
32 procd_add_reload_trigger "system"
33 }
34
35 start_service()
36 {
37 [ -e /sys/class/gpio/ ] && {
38 config_load system
39 config_foreach load_gpio_switch gpio_switch
40 }
41 }