#!/bin/bash
#
# pintest
#	Test the Pi's GPIO port
# Copyright (c) 2013-2015 Gordon Henderson
#################################################################################
# This file is part of wiringPi:
#	A "wiring" library for the Raspberry Pi
#
#    wiringPi is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    wiringPi is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with wiringPi.  If not, see .
#################################################################################
# logErr pin, expected got
################################################################################
logErr ()
{
  if [ $errs = 0 ]; then
    echo ""
  fi
  echo " --> Pin $1 failure. Expected $2, got $3"
  let errs+=1
}
# printErrorCount
################################################################################
printErrCount()
{
  if [ $errs = 0 ]; then
    echo "No faults detected."
  elif [ $errs = 1 ]; then
    echo "One fault detected."
  else
    echo "$errs faults detected"
  fi
}
# testPins start end
################################################################################
testPins()
{
  start=$1
    end=$2
   errs=0
  printf "%30s %2d:%2d: "  "$3" $1 $2
# Set range to inputs
  for i in `seq $start $end`; do
    gpio mode $i in
  done
# Enable internal pull-ups and expect to read high
  for i in `seq $start $end`; do
    gpio mode $i up
    if [ `gpio read $i` = 0 ]; then
      logErr $i 1 0
    fi
  done
# Enable internal pull-downs and expect to read low
  for i in `seq $start $end`; do
    gpio mode $i down
    if [ `gpio read $i` = 1 ]; then
      echo "Pin $i failure - expected 0, got 1"
      let errs+=1
    fi
  done
# Remove the internal pull up/downs
  for i in `seq $start $end`; do
    gpio mode $i tri
  done
  if [ $errs = 0 ]; then
    echo " OK"
  else
    printErrCount
  fi
  let totErrs+=errs
}
intro()
{
  cat <