Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4867

Bare metal, Assembly language • Accessing GPIO pins from a BASH script on a Raspberry Pi 5

$
0
0
I'm not much good at programming and therefore I am much more comfortable working with bash shell scripts rather than Python - Python is WAY too picky about syntax and indentation. Anyway, I have this rather lengthy script that I wrote in bash that checks the status of doors, using magnetic reed switches as the sensors. So in my bash script running on a Pi 4, I have commands like this:

Code:

for i in {17..18} {22..25}do  echo "$i" > /sys/class/gpio/export  echo "in" > "/sys/class/gpio/gpio$i/direction"  GPIO[$i]=2done

Code:

  for i in {17..18} {22..25}  do    GPIOPREV[$i]=${GPIO[$i]}    GPIO[$i]=$(<"/sys/class/gpio/gpio$i/value")    .....
Mostly the entire thing is built around those commands, just setting up GPIO pins 17, 18, and 22-25 so I can read the current state, and then reading them. But also, I have to run this short Python script to set up the pullup/pulldown resistors:

Code:

!/usr/bin/pythonimport RPi.GPIO as GPIOGPIO.setmode(GPIO.BCM)GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_OFF)GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_OFF)GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_OFF)GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
(In case you are wondering, pins 22-25 have external resistors on them, which is why I disable the Pi's internal ones on those pins). And that is run once from a bash script, like so:

Code:

# Call python script to initialize the GPIO inputs listed above and set pullup resistors, abort this script on error/root/initialize_gpio_inputs.py || { echo "Error initializing GPIO inputs" >> door.log; exit 1; }
I'm guessing none of that is going to work on the Pi 5, since RPi.GPIO probably won't work to set up the internal resistors as needed, and the bash commands I am using to read the pin state probably won't work either? I REALLY would rather not have to try to convert this entire bash script to Python (it took me a few days to write just in bash), so is there any way to do all this in bash on the Pi 5?

Statistics: Posted by berrygood — Wed Mar 20, 2024 11:10 pm



Viewing all articles
Browse latest Browse all 4867

Trending Articles