Last active
December 6, 2021 22:31
-
-
Save assimilat/93ce1b6e873f35dd5f2a567080401a83 to your computer and use it in GitHub Desktop.
set and/or check an arduino's pinstate. for use with: https://gist.github.com/assimilat/8bb0921333054be777c6ac16318ad59d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash - | |
#=============================================================================== | |
# | |
# FILE: set_pin.sh | |
# | |
# USAGE: set_pin.sh | |
# | |
# DESCRIPTION: | |
# | |
# OPTIONS: --- | |
# REQUIREMENTS: --- | |
# BUGS: --- | |
# NOTES: --- | |
# AUTHOR: YOUR NAME (), | |
# ORGANIZATION: | |
# CREATED: 11/24/2021 07:39:55 AM | |
# REVISION: --- | |
#=============================================================================== | |
#set -o nounset # Treat unset variables as an error | |
if [ "$1" == "long" ]; | |
then | |
LONG_PRESS="1"; | |
shift | |
else | |
LONG_PRESS="0"; | |
fi | |
LONG_PRESS_DURATION="10"; | |
MAX_PIN="8"; | |
MOMENTARY="1 2 3 4"; | |
DEVICE="/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0"; | |
stty -F ${DEVICE} -hupcl 9600; | |
EXIT_SLEEP="1"; | |
CMD_TYPE=$(echo "${1}" | sed -e "s/[0-9]//g"); | |
TARGET_PIN=$(echo "${1}" | sed -e "s/[a-z]//g"); | |
TARGET_STATE=$(echo "${1}" | sed -e "s/[0-9]//g"); | |
usage() { | |
(echo "Usage: ${0} <read/on/off><pin_number>";) 1>&2; | |
exit; | |
} | |
momentary_check() { | |
for i in ${MOMENTARY}; | |
do | |
if [ "${1}" == "${i}" ]; | |
then | |
echo "m"; | |
break; | |
fi | |
done | |
} | |
if [ -z "$TARGET_PIN" ]; | |
then | |
if [ -n "$2" ]; | |
then | |
if [ -n "$(echo "$2" | grep "[0-9]" | grep -v "[a-z]")" ]; | |
then | |
TARGET_PIN="$2"; | |
fi | |
else | |
echo "No pin specfied!"; | |
usage; | |
fi | |
fi | |
read_pin() { | |
STATE=""; | |
while [ -z "${STATE}" ]; | |
do | |
echo read${1} > ${DEVICE}; | |
sleep .5; | |
STATE=$(head -n1 ${DEVICE} | cat -A | sed -e "s/./\n&/g" | grep "[0-9]" | tee -a /tmp/state); | |
done | |
if [ "$STATE" == "0" ]; | |
then | |
echo "on"; | |
else | |
if [ "${STATE}" == "1" ]; | |
then | |
echo "off"; | |
fi | |
fi; | |
} | |
write_pinstates() { | |
for i in $(seq 1 ${MAX_PIN}); | |
do | |
if [ "${i}" == "1" ]; | |
then | |
rm /tmp/pinstate; | |
fi | |
PIN_STATE=$(read_pin ${i}); | |
if [ "${PIN_STATE}" == "off" ]; | |
then | |
echo "${i}==off" >> /tmp/pinstate; | |
else | |
echo "${i}==on" >> /tmp/pinstate; | |
fi | |
done | |
} | |
if [ -z "$1" ]; | |
then | |
usage; | |
fi | |
if [ "$1" == "write_states" ]; | |
then | |
for i in $(seq 1 ${MAX_PIN}); | |
do | |
echo "Pin${i}=$(read_pin ${i})"; | |
done | |
exit | |
fi | |
if [ -z "$(echo "^${TARGET_PIN}$" | grep "[1-${MAX_PIN}]")" ]; | |
then | |
echo "${PIN} is an invalid pin"; | |
usage; | |
fi | |
if [ "${CMD_TYPE}" == "read" ]; | |
then | |
if [ -z "${TARGET_PIN}" ]; | |
then | |
TARGET_PIN=$(echo $1 | sed -e "s/[a-z]//g"); | |
fi | |
PIN_STATE=""; | |
while [ -z "${PIN_STATE}" ]; | |
do | |
PIN_STATE=$(read_pin ${TARGET_PIN}); | |
echo "${PIN_STATE}" | |
done | |
exit; | |
fi | |
if [ "${CMD_TYPE}" == "write_pinstates" ]; | |
then | |
write_pinstates; | |
exit; | |
fi | |
if [ "$TARGET_STATE" == "on" ]; | |
then | |
if [ "${DEBUG}" == "1" ]; | |
then | |
echo "Setting pin ${TARGET_PIN} to on."; | |
fi | |
echo "off${TARGET_PIN}" > ${DEVICE}; | |
MOMENTARY=$(momentary_check "${TARGET_PIN}"); | |
while [ "$(read_pin ${TARGET_PIN})" == "off" ]; | |
do | |
echo "on${TARGET_PIN}" > ${DEVICE}; | |
done | |
fi | |
if [ "${TARGET_STATE}" == "off" ]; | |
then | |
if [ "${DEBUG}" == "1" ]; | |
then | |
echo "Setting pin ${TARGET_PIN} to off."; | |
fi | |
while [ "$(read_pin ${TARGET_PIN})" == "on" ]; | |
do | |
echo "off${TARGET_PIN}" > ${DEVICE}; | |
done | |
fi | |
if [ "${MOMENTARY}" == "m" ]; | |
then | |
if [ "${LONG_PRESS}" == "1" ]; | |
then | |
sleep "${LONG_PRESS_DURATION}"; | |
fi | |
# sleep 1; | |
if [ "${TARGET_STATE}" == "on" ]; | |
then | |
if [ "DEBUG" == "1" ]; | |
then | |
echo "Setting ${TARGET_PIN}off"; | |
fi | |
while [ "$(read_pin ${TARGET_PIN})" == "on" ]; | |
do | |
echo off${TARGET_PIN} > ${DEVICE}; | |
done | |
fi | |
fi | |
if [ "$EXIT_SLEEP" -gt "0" ]; | |
then | |
sleep ${EXIT_SLEEP}; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment