-
Notifications
You must be signed in to change notification settings - Fork 42
/
xbee-init
88 lines (76 loc) · 1.75 KB
/
xbee-init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh
# Linux GPIOs on XBee lines
XBEE_RESET_N_GPIO="##XBEE_RESET_N_GPIO##"
XBEE_SLEEP_RQ_GPIO="##XBEE_SLEEP_RQ_GPIO##"
# request_gpio <gpio_nr>
request_gpio_out() {
local SG_GPIONR="${1}"
local SG_GPIOPATH="/sys/class/gpio/gpio${SG_GPIONR}"
[ -d "${SG_GPIOPATH}" ] || printf "%s" "${SG_GPIONR}" > /sys/class/gpio/export
printf out > "${SG_GPIOPATH}/direction" && sleep .2
}
# free_gpio <gpio_nr>
free_gpio() {
local SG_GPIONR="${1}"
local SG_GPIOPATH="/sys/class/gpio/gpio${SG_GPIONR}"
[ -d "${SG_GPIOPATH}" ] && printf "%s" "${SG_GPIONR}" > /sys/class/gpio/unexport
}
# set_gpio_value <gpio_nr> <value>
set_gpio_value() {
local SG_GPIONR="${1}"
local SG_GPIOVAL="${2}"
local SG_GPIOPATH="/sys/class/gpio/gpio${SG_GPIONR}"
printf out > "${SG_GPIOPATH}/direction" && sleep .2
printf "${SG_GPIOVAL}" > "${SG_GPIOPATH}/value" && sleep .2
}
xbee_init() {
# Power cycle XBEE_RESET_N
request_gpio_out ${1}
set_gpio_value ${1} 0
set_gpio_value ${1} 1
# Set low XBEE_SLEEP_RQ
request_gpio_out ${2}
set_gpio_value ${2} 0
}
xbee_stop() {
free_gpio ${1}
free_gpio ${2}
}
xbee_iterate_list() {
i=0
for RESET in $(echo ${XBEE_RESET_N_GPIO} | sed "s/,/ /g"); do
j=0
for SLEEP in $(echo ${XBEE_SLEEP_RQ_GPIO} | sed "s/,/ /g"); do
if [ "${i}" = "${j}" ]; then
if [ "${1}" = "start" ]; then
xbee_init ${RESET} ${SLEEP}
elif [ "${1}" = "stop" ]; then
xbee_stop ${RESET} ${SLEEP}
fi
fi
j="$((j + 1))"
done
i="$((i + 1))"
done
}
case "$1" in
start)
echo -n "Starting XBee hardware: "
xbee_iterate_list start
echo "done."
;;
stop)
echo -n "Stopping XBee hardware: "
xbee_iterate_list stop
echo "done."
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac