Here is a Shell version, no dependency:
这是一个 Shell 版本,没有依赖关系:
#!/bin/bash
# Convert argument to uppercase
arg=${1^^}
if [[ "$arg" =~ ^[0-9]+$ ]]; then
# Argument is numeric, each port is 32-bit wide
let port_base=$arg/32
# Convert port number to ASCII code ('A' is 65)
let port=$port_base+65
let bit=$arg%32
printf "P%b%d\n" $(printf '\x%x\n' $port 2>/dev/null) $bit
elif [[ "$arg" =~ ^P[A-Z][0-9]+$ ]]; then
# Argument is a pin name, convert port name to number ('A' is 65)
let port=$(printf "%d" "'${arg:1:1}")-65
# Each port is 32-bit wide
let port_base=$port*32
let gpio=port_base+${arg:2}
echo $gpio
else
>&2 echo "wrong argument"
fi