#!/bin/bash

PORT_NUM=$1
PORT_LEVEL=$2

# export gpio if needed
if [ ! -e /sys/class/gpio/gpio${PORT_NUM} ]; then
    echo ${PORT_NUM} > /sys/class/gpio/export
fi

# set gpio as out if needed
direction=$(cat /sys/class/gpio/gpio${PORT_NUM}/direction)
if [ "$direction" != "out" ]; then
    echo "GPIO ${PORT_NUM} is not out mode, but $direction mode"
    echo "Set GPIO ${PORT_NUM} as out mode..."
    echo out > /sys/class/gpio/gpio${PORT_NUM}/direction
    echo "GPIO ${PORT_NUM} has been set as out mode."
else
    echo "GPIO ${PORT_NUM} is out mode now."
fi

echo ${PORT_LEVEL} > /sys/class/gpio/gpio${PORT_NUM}/value
