#!/bin/bash

# This is Craig's modifications to the file the file  
#    /home/irlp/custom/init_IRLP_GPIO  
# in the Raspbery Pi
# with the mods below the pi does not need any jumpers
# to function


# In order: D0, D1, D2, D3, COS
INGPIO="4 25 9 24 10"
# In order: PTT, AUX1, AUX2, AUX3
OUTGPIO="17 18 22 23"

#Export all pins and set input/ouput
for i in $INGPIO ; do
  if [ ! -d /sys/class/gpio/gpio${i} ] ; then
    EXPORTS="$EXPORTS $i"
    echo "$i" > /sys/class/gpio/export
  fi
  echo "in" > /sys/class/gpio/gpio${i}/direction
  # Changes permissions so non-root can read/write to GPIO
  chmod -R 666 /sys/class/gpio/gpio${i}/*
done

for i in $OUTGPIO ; do
  if [ ! -d /sys/class/gpio/gpio${i} ] ; then 
    echo "$i" > /sys/class/gpio/export
    EXPORTS="$EXPORTS $i"
  fi
  echo "out" > /sys/class/gpio/gpio${i}/direction
  # Set outputs low

####################################################
####################################################
  # echo "0" > /sys/class/gpio/gpio${i}/value

echo "1" > /sys/class/gpio/gpio10/active_low
echo "1" > /sys/class/gpio/gpio4/active_low

####################################################
####################################################


  # Changes permissions so non-root can read/write to GPIO
  chmod -R 666 /sys/class/gpio/gpio${i}/*
done

chmod 777 /sys/class/gpio/

#Added Feb 12, 2015 to fix GPIO permission error injected by the code changes for the Pi2
if [ -d /sys/devices/soc/*.gpio ] ; then
  chmod -R +x /sys/devices/soc/*.gpio/
fi

#Added Jul 7, 2015 to fix GPIO permission error injected by the code changes for the Pi bootloder (AGAIN)
if [ -d /sys/devices/platform/soc/*.gpio ] ; then
  chmod -R +x /sys/devices/platform/soc/*.gpio/
fi

if [ ! -z "$EXPORTS" ] ; then
  echo "Activating IRLP GPIO pins$EXPORTS"
fi

