From 56491b7ab0e54a26ac88f58ce08af2479e118891 Mon Sep 17 00:00:00 2001 From: Dave Date: Thu, 11 Oct 2018 18:57:23 +0100 Subject: [PATCH] added in GPIO led functionality --- scripts/blink | 25 +++++++++++++++++++ scripts/card-backup.sh | 32 +++++++++++++++++++----- scripts/gpio | 55 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 scripts/blink create mode 100644 scripts/gpio diff --git a/scripts/blink b/scripts/blink new file mode 100644 index 0000000..1488a3e --- /dev/null +++ b/scripts/blink @@ -0,0 +1,25 @@ +#!/bin/bash + +source ./gpio + +function blink() +{ + local pin=$1 + local speed=$2 + + while : + do + gpio write $pin 1 + sleep $speed + gpio write $pin 0 + sleep $speed + done + +} + + +# Just invoke our function if the script is called directly +if [ "$BASH_SOURCE" == "$0" ]; then + gpio mode $1 out + blink $@ +fi diff --git a/scripts/card-backup.sh b/scripts/card-backup.sh index 9c04fb0..1beb180 100755 --- a/scripts/card-backup.sh +++ b/scripts/card-backup.sh @@ -25,6 +25,22 @@ CARD_DEV="sd[ab]1" # Name of the storage card CARD_MOUNT_POINT="/media/card" # Mount point of the storage card SHUTD="5" # Minutes to wait before shutdown due to inactivity +source ./gpio +source ./blink + +gpio mode 5 out +gpio mode 6 out +gpio mode 13 out +gpio mode 19 out +gpio mode 26 out + +gpio wirte 5 0 +gpio write 6 0 +gpio write 13 0 +gpio write 19 0 +gpio write 26 0 + + # Set the ACT LED to heartbeat sudo sh -c "echo heartbeat > /sys/class/leds/led0/trigger" @@ -90,12 +106,16 @@ if [ ! -z "${CARD_READER[0]}" ]; then PERCENT=$(expr 100 \* $STORAGE_COUNT / $CARD_COUNT) sudo sh -c "echo $PERCENT" #IF STATEMENTS HERE FOR LEDS - if [ $PERCENT -gt 25 ] && [ $PERCENT -lt 49 ]; then - sudo sh -c "echo 300 > /sys/class/leds/led0/delay_on" - elif [ $PERCENT -gt 50 ] && [ $PERCENT -lt 74 ]; then - sudo sh -c "echo 200 > /sys/class/leds/led0/delay_on" - elif [ $PERCENT -gt 75 ] && [ $PERCENT -lt 100 ]; then - sudo sh -c "echo 100 > /sys/class/leds/led0/delay_on" + if [ $PERCENT -lt 19 ]; then + gpio write 26 1 + elif [ $PERCENT -gt 20 ] && [ $PERCENT -lt 39 ]; then + gpio write 19 1 + elif [ $PERCENT -gt 40 ] && [ $PERCENT -lt 59 ]; then + gpio write 13 1 + elif [ $PERCENT -gt 60 ] && [ $PERCENT -lt 79 ]; then + gpio write 6 1 + elif [ $PERCENT -gt 80 ] && [ $PERCENT -lt 100 ]; then + gpio write 5 1 fi # then #LEDS diff --git a/scripts/gpio b/scripts/gpio new file mode 100644 index 0000000..67a26cf --- /dev/null +++ b/scripts/gpio @@ -0,0 +1,55 @@ +#!/bin/bash + +# Utility to control the GPIO pins of the Raspberry Pi +# Can be called as a script or sourced so that the gpio +# function can be called directly + +function gpio() +{ + local verb=$1 + local pin=$2 + local value=$3 + + local pins=($GPIO_PINS) + if [[ "$pin" -lt ${#pins[@]} ]]; then + local pin=${pins[$pin]} + fi + + local gpio_path=/sys/class/gpio + local pin_path=$gpio_path/gpio$pin + + case $verb in + read) + cat $pin_path/value + ;; + + write) + sudo sh -c "echo $value > $pin_path/value" + ;; + + mode) + if [ ! -e $pin_path ]; then + sudo sh -c "echo $pin > $gpio_path/export" + fi + sudo sh -c "echo $value > $pin_path/direction" + ;; + + state) + if [ -e $pin_path ]; then + local dir=$(cat $pin_path/direction) + local val=$(cat $pin_path/value) + echo "$dir $val" + fi + ;; + clean) + sudo sh -c "echo 0 > $pin_path/value" + sudo sh -c "echo $pin > $gpio_path/unexport" + ;; + + esac +} + +# Just invoke our function if the script is called directly +if [ "$BASH_SOURCE" == "$0" ]; then + gpio $@ +fi