1#!/bin/bash
2
3get_gpio()
4{
5    local NET_NAME=$1
6    local RET_VAL
7
8    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
9    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
10        echo "get_gpio: can not find gpio, $NET_NAME" >&2
11        return 1
12    fi
13    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
14        echo "get_gpio: get ${NET_NAME} failed" >&2
15        return 1
16    fi
17    echo "${RET_VAL}"
18    return 0
19}
20
21set_gpio()
22{
23    local NET_NAME=$1
24    local OUT_VAL=$2
25    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
26    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
27        echo "set_gpio: can not find gpio, $NET_NAME"
28        return 1
29    fi
30
31    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
32    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
33        echo " failed"
34        return 1
35    fi
36
37    echo " success"
38    return 0
39}
40
41bind_i2c_device()
42{
43    DRIVER_NAME="$1"
44    DEV_BUS_ADDR="$2"
45    DRIVER_DIR_PATH="/sys/bus/i2c/drivers/${DRIVER_NAME}"
46    DRIVER_BIND_PATH="${DRIVER_DIR_PATH}/bind"
47    if [ ! -L "${DRIVER_DIR_PATH}/${DEV_BUS_ADDR}" ]; then
48        echo "bind_i2c_device: $DRIVER_NAME, $DEV_BUS_ADDR"
49        echo "${DEV_BUS_ADDR}" > "${DRIVER_BIND_PATH}"
50    fi
51}
52