2
0

gpio_switch 805 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2015 OpenWrt.org
  3. START=98
  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. echo out >"$gpio_path/direction"
  21. }
  22. # write 0 or 1 to the "value" field
  23. { [ "$value" = "0" ] && echo "0" || echo "1"; } >"$gpio_path/value"
  24. }
  25. service_triggers()
  26. {
  27. procd_add_reload_trigger "system"
  28. }
  29. start_service()
  30. {
  31. [ -e /sys/class/gpio/ ] && {
  32. config_load system
  33. config_foreach load_gpio_switch gpio_switch
  34. }
  35. }