gpio_switch 1013 B

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