RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login

    Can I do MORE GPIO stuff with Mausberry script?

    Scheduled Pinned Locked Moved General Discussion and Gaming
    mausberryvolume controlscripts
    2 Posts 2 Posters 1.1k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • caver01C
      caver01
      last edited by caver01

      I am curious if anyone is doing more with the Mausberry script. By more, I mean tracking other GPIO inputs. Here's the default script:

      #!/bin/bash
      
      #this is the GPIO pin connected to the lead on switch labeled OUT
      GPIOpin1=23
      
      #this is the GPIO pin connected to the lead on switch labeled IN
      GPIOpin2=24
      
      echo "$GPIOpin1" > /sys/class/gpio/export
      echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction
      echo "$GPIOpin2" > /sys/class/gpio/export
      echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction
      echo "1" > /sys/class/gpio/gpio$GPIOpin2/value
      while [ 1 = 1 ]; do
      power=$(cat /sys/class/gpio/gpio$GPIOpin1/value)
      if [ $power = 0 ]; then
      sleep 1
      else
      sudo poweroff
      fi
      done
      

      I have some Admin buttons on my cabinet that I will wire to GPIO pins to send commands (Volume Up, Volume Down). I already have the commands, but I need to link them to my buttons via GPIO5 and GPIO6. I was going to install one of the GPIO controller solutions that have been documented here, but it struck me that I already have a Mausberry script running that is watching GPIO23 for shutdown. Why not add a couple more commands?

      The problem I am running into is that my attempt to change the script failed, probably because I don't have much experience using GPIO on the Pi , so my modifications are just not correct.

      Has anyone using a Mausberry circuit added additional commands/functions to the script? I proposed this on the Mausberry forum, but it's just crickets over there.

      OR. . . should I leave well enough alone and load some additional GPIO solution for my volume commands?

      By the way, I couldn't create this in the Help and Support area--I am getting an error that the category does not exist.

      My 4-player cocktail style cabinet built as a custom "roadcase"

      1 Reply Last reply Reply Quote 0
      • obsidianspiderO
        obsidianspider
        last edited by obsidianspider

        I modified my Mausberry script to change the display on my TFT to show that the Pi is shutting down. That said, I've seen other python implementations to try to do the same thing. Most seemed to be less "foolproof" than the Mausberry method, so I didn't use python to manage the shutdown. I'm still quite a hack at this, I'm still cleaning things up, and I'm sure my code is messy, but this is my modified switch.sh

        #!/bin/bash
        
        #this is the GPIO pin connected to the lead on switch labeled OUT
        GPIOpin1=18
        
        #this is the GPIO pin connected to the lead on switch labeled IN
        GPIOpin2=17
        
        echo "$GPIOpin1" > /sys/class/gpio/export
        echo "in" > /sys/class/gpio/gpio$GPIOpin1/direction
        echo "$GPIOpin2" > /sys/class/gpio/export
        echo "out" > /sys/class/gpio/gpio$GPIOpin2/direction
        echo "1" > /sys/class/gpio/gpio$GPIOpin2/value
        while [ 1 = 1 ]; do
        power=$(cat /sys/class/gpio/gpio$GPIOpin1/value)
        if [ $power = 0 ]; then
        sleep 1
        else
        #load image
        sudo python /home/pi/Adafruit_Python_ILI9341/examples/imageparam2.py "/home/pi/Adafruit_Python_ILI9341/examples/powering-off.png"
        sudo poweroff
        fi
        done
        

        The Mausberry script is pretty inefficient if you want to monitor buttons as it's constantly polling. From my research the "wait for edge" method is less processor intensive. This is my "reset button" python script that controls the backlight on my secondary TFT. My code may be messy, but I noticed less CPU load when using this method compared to the Mausberry method.

        #!/usr/bin/python
        
        import RPi.GPIO as GPIO
        import time
        
        # set up BCM GPIO numbering
        GPIO.setmode(GPIO.BCM)
        
        # turn off warnings
        GPIO.setwarnings(False)
        
        # tell the script that we're going to output data on GPIO 23
        GPIO.setup(23,GPIO.OUT)
        
        # tell the script we're going to use GPIO 27 for input and act as a pull up resistor
        GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        
        # the default state of the light is on
        light = 1 
        
        # run this unless there's an exception
        try:
        	
        	# Turn ON Backlight by default
        	GPIO.output(23,GPIO.HIGH)
        	
        	# do this forever
        	while True:
        		# use interrupt to wait for button to be pressed
        		GPIO.wait_for_edge(27, GPIO.FALLING)
        		# if the light is on, turn the light off
        		if light == 1:
        			#Turn OFF Backlight
        			GPIO.output(23,GPIO.LOW)
        			#change the state of the light variable so the script knows the light is off
        			light = 0
        			
        		# if the light is off, turn the light on
        		elif light == 0:
        			# Turn ON Backlight
        			GPIO.output(23,GPIO.HIGH)
        			#change the state of the light variable so the script knows the light is on
        			light = 1
        		# wait a little while to account for bounce in the button
        		time.sleep(0.2)
        
        # keep running unless you Ctrl+C to break out of it
        except KeyboardInterrupt:
        	# reset the GPIO pins used in this program
        	GPIO.cleanup()
        

        📷 @obsidianspider

        1 Reply Last reply Reply Quote 0
        • First post
          Last post

        Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

        Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.