#! /bin/bash

# IO system has changed with PI5
# This script uses gpiod
# I'm not sure if this works on older models up to PI4b

REL1a="26"
REL1b="16"
REL2a="20"
REL2b="21"

# swap $RELxa and $RELxb if your relay is miswired (easier than swapping the wires...)
arr=($REL1a $REL1b $REL2a $REL2b)

function rel()
{
sleep 2
#echo 1 >$1
gpioset gpiochip0 $1=1
sleep 0.05
#echo 0 >$1
gpioset gpiochip0 $1=0
}

function usage()
{
echo "Usage: relay <number> <state>"
echo "  number 0 or 1"
echo "  state: on or off"
exit
}

[[ ( "$1" = "0" ) || ( "$1" = "1" ) ]] || usage
[[ ("$2" = "on" ) || ( "$2" = "off" ) ]] || usage
idx=$(($1 * 2));
[ "$2" = "on" ] && idx=$(($idx + 1))
#echo "idx $idx ${arr[$idx]}"

logger "powering $2 relay $1"
rel ${arr[$idx]}



