#!/usr/bin/env sh

#############################################
# Script for setting Profinet LEDs on Linux #
#############################################
#
# Adapt this script to your particular hardware

if [ $# -ne 2 ]; then
   echo "Usage: ${0} led_number led_state"
   echo "   where:"
   echo "       led_number:      LED number. Number 1 for sample app data LED"
   echo "                                    Number 2 for mandatory Profinet signal LED"
   echo "       led_state:       1 for on, 0 for off"
   echo "   Exit code:           0 on success, 1 on error"
   exit 1
fi

LED_NUMBER=$1
LED_STATE=$2

# Uncomment next line for debugging
#echo "Script for Profinet LEDs on RPi. LED number ${LED_NUMBER}, new state ${LED_STATE}"

if ! [ "${LED_STATE}" = "0" -o "${LED_STATE}" = "1" ]; then
   echo "Wrong LED state: ${LED_STATE}"
   exit 1
fi
if  [ "${LED_NUMBER}" = "1" ]; then
   GPIO=17
elif [ "${LED_NUMBER}" = "2" ]; then
   GPIO=23
else
   echo "Wrong LED number: ${LED_NUMBER}"
   exit 1
fi

echo ${GPIO} > /sys/class/gpio/export 2> /dev/null
echo out > /sys/class/gpio/gpio${GPIO}/direction 2> /dev/null
echo ${LED_STATE} > /sys/class/gpio/gpio${GPIO}/value

exit 0
