Tackling Graceful Shutdowns on the GBZ

Various user-contributed guides for software-related things
User avatar
Camble
Posts: 885
Joined: Thu May 05, 2016 2:31 am
Location: Scotland
Has thanked: 269 times
Been thanked: 488 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by Camble » Thu Jul 07, 2016 11:14 am

You'll still be able to use a Pololu switch to perform a graceful shutdown. The problem of the supply being on exists with both the Banggood module, as there is no EN pin to shut it off manually. You could always add a switch between your battery and the power supply module though.

SidSilver
Posts: 263
Joined: Sun May 22, 2016 6:22 am
Location: France
Has thanked: 137 times
Been thanked: 37 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by SidSilver » Thu Jul 07, 2016 3:18 pm

@Popcorn : I've followed your tutorial for the pololu graceful shutdown.
When I switch off I have a kernel panic.
Image

SidSilver
Posts: 263
Joined: Sun May 22, 2016 6:22 am
Location: France
Has thanked: 137 times
Been thanked: 37 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by SidSilver » Thu Jul 07, 2016 3:19 pm

Camble wrote:You'll still be able to use a Pololu switch to perform a graceful shutdown. The problem of the supply being on exists with both the Banggood module, as there is no EN pin to shut it off manually. You could always add a switch between your battery and the power supply module though.
Or simply replace the reset tact button by a slide switch ;)

User avatar
tent
Posts: 32
Joined: Thu Jul 07, 2016 4:40 pm
Has thanked: 6 times
Been thanked: 1 time

Re: Tackling Graceful Shutdowns on the GBZ

Post by tent » Thu Jul 07, 2016 5:41 pm

SidSilver wrote:
Camble wrote:You'll still be able to use a Pololu switch to perform a graceful shutdown. The problem of the supply being on exists with both the Banggood module, as there is no EN pin to shut it off manually. You could always add a switch between your battery and the power supply module though.
Or simply replace the reset tact button by a slide switch ;)
I doubt it: how can it work if the boost module has no EN pin or so?? But maybe it would be possible to do an EN pin with a transistor.... I'd be interested in that because I found 1A chargers for very cheap (TP4056) and booster/protectors as well (DLX-UPDC5V)..

tent:wq
tent:wq

SidSilver
Posts: 263
Joined: Sun May 22, 2016 6:22 am
Location: France
Has thanked: 137 times
Been thanked: 37 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by SidSilver » Thu Jul 07, 2016 10:37 pm

tent wrote:
SidSilver wrote:
Camble wrote:You'll still be able to use a Pololu switch to perform a graceful shutdown. The problem of the supply being on exists with both the Banggood module, as there is no EN pin to shut it off manually. You could always add a switch between your battery and the power supply module though.
Or simply replace the reset tact button by a slide switch ;)
I doubt it: how can it work if the boost module has no EN pin or so?? But maybe it would be possible to do an EN pin with a transistor.... I'd be interested in that because I found 1A chargers for very cheap (TP4056) and booster/protectors as well (DLX-UPDC5V)..

tent:wq
Oh sorry, I had not read correctly.
With no EN pin you need to cut directly the battery, indeed.

User avatar
tent
Posts: 32
Joined: Thu Jul 07, 2016 4:40 pm
Has thanked: 6 times
Been thanked: 1 time

Re: Tackling Graceful Shutdowns on the GBZ

Post by tent » Fri Jul 08, 2016 8:41 am

I was thinking about trying to implement kind of an enable pin to be added to the more "Simple" boosters/chargers out there...

or, by the way, did somebody understand the difference between the adafruit 1000 basic and the 1000C? for GBZ purposes I do not quite see evident difference or missing part (enable pin, low battery, etc.)..

tent
tent:wq

User avatar
Fleder
Posts: 849
Joined: Thu May 05, 2016 9:04 am
Location: Germany
Has thanked: 183 times
Been thanked: 258 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by Fleder » Fri Jul 08, 2016 8:59 am

tent wrote:I was thinking about trying to implement kind of an enable pin to be added to the more "Simple" boosters/chargers out there...

or, by the way, did somebody understand the difference between the adafruit 1000 basic and the 1000C? for GBZ purposes I do not quite see evident difference or missing part (enable pin, low battery, etc.)..

tent
The Charger lets you charge the battery while you use it (charge+play). The Basic can't charge during use, as far as i know.

User avatar
tent
Posts: 32
Joined: Thu Jul 07, 2016 4:40 pm
Has thanked: 6 times
Been thanked: 1 time

Re: Tackling Graceful Shutdowns on the GBZ

Post by tent » Fri Jul 08, 2016 10:08 am

Really?? Well I hope then that when you plug in usb it will give precedence to output and not charge!! :)) but in the end not really a vital feature.. and from your calculations what would be the tipical consumptions of those 3.5 lcds? (I suppose the rest is quite low.. Ok hub and dac if you use it and wifi dongle but Ok.
tent:wq

SidSilver
Posts: 263
Joined: Sun May 22, 2016 6:22 am
Location: France
Has thanked: 137 times
Been thanked: 37 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by SidSilver » Fri Jul 08, 2016 12:34 pm

Tackling Graceful Shutdowns on the GBZ [RECALBOX 4.0+ VERSION]

I've managed to make the "Tackling Graceful Shutdowns" works on Recalbox v4.0
Here is how I've done that.

First of all Recalbox has no sudo command (user is root)
It doesn't have omxplayer (will be added in the next 4.1 version), it doesn't have Git and it doesn't have crontab !

So, some little tricks are needed to make the Popcorn TGS works :

Using WinSCP (or equivalent), create a folder "/recalbox/scripts/GBZPowerMonitor" where you put the python script and the mp4 videos from Popcorn's Github.

I've done some little modifications in the python to make it works : remove all sudos and change the video paths
[spoiler="code"]

Code: Select all

#!/usr/bin/env python2.7
# date: 30/05/16
# author: Popcorn <abandonedemails@gmail.com> - Add "Sudomod" in the subject or your message will not be received
# version: 1.0a
# name: GBZ-Power-Monitor - a Power Management utility for the Gameboy Zero project
# description: a GPIO monitor that detects low battery and power switch status which provides a graceful shutdown facility
# source: https://github.com/NullCorn/GBZ-Power-Monitor/
# modified by SidSilver 08/07/16 for Recalbox

import RPi.GPIO as GPIO
import os
import sys
import time

batteryGPIO    = 17  # GPIO 17/pin 0
powerGPIO      = 27  # GPIO 27/pin 2
sampleRate     = 0.1 # tenth of a second
batteryTimeout = 10  # 10 seconds
powerTimeout   = 1   # 1 second
shutdownVideo  = "/recalbox/scripts/GBZPowerMonitor/lowbattshutdown.mp4" # use no space or non-alphanum characters
lowalertVideo  = "/recalbox/scripts/GBZPowerMonitor/lowbattalert.mp4"    # use no space or non-alphanum characters
playerFlag     = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(batteryGPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(powerGPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def lowBattery(channel):
  #Checking for LED bounce for the duration of the battery Timeout
  for bounceSample in range(1, int(round(batteryTimeout / sampleRate))):
    time.sleep(sampleRate)

    if GPIO.input(batteryGPIO) is 1:
       break
  
  global playerFlag   
  while playerFlag is 1:
    time.sleep(1)
     
  #If the LED is a solid condition, there will be no bounce.  Launch shutdown video and then gracefully shutdown
  if bounceSample is int(round(batteryTimeout / sampleRate)) - 1:
    playerFlag = 1
    #videos should be enabled when Recalbox will be in version 4.1 as it doesn't have omxplayer yet
    #os.system("/usr/bin/omxplayer --no-osd --layer 999999 " + shutdownVideo + " --alpha 180;shutdown -h now");
    os.system("shutdown -h +1");
    playerFlag = 0
    sys.exit(0)

  #If the LED is a solid for more than 10% of the timeout, we know that the battery is getting low.  Launch the Low Battery alert. 
  if bounceSample > int(round(batteryTimeout / sampleRate * 0.1)):
    playerFlag = 1
    #videos should be enabled when Recalbox will be in version 4.1 as it doesn't have omxplayer yet
    #os.system("/usr/bin/omxplayer --no-osd --layer 999999 " + lowalertVideo + " --alpha 160;");
    playerFlag = 0
    
    #Discovered a bug with the Python GPIO library and threaded events.  Need to unbind and rebind after a System Call or the program will crash
    GPIO.remove_event_detect(batteryGPIO)
    GPIO.add_event_detect(batteryGPIO, GPIO.BOTH, callback=lowBattery, bouncetime=300)
    
    #If we know the battery is low, we aggresively monitor the level to ensure we shutdown once the Power Timeout is exceeded.
    lowBattery(batteryGPIO)
    
def powerSwitch(channel):
  #Checking for LED bounce for the duration of the Power Timeout
  for bounceSample in range(1, int(round(powerTimeout / sampleRate))):
    time.sleep(sampleRate)

    if GPIO.input(powerGPIO) is 1:
       break

  if bounceSample is int(round(powerTimeout / sampleRate)) - 1:
      #When the Power Switch is placed in the off position with no bounce for the duration of the Power Timeout, we immediately shutdown
      os.system("shutdown -h now")
      try:
         sys.stdout.close()
      except:
         pass
      try:
         sys.stderr.close()
      except:
         pass

      sys.exit(0)

def main():
  #if the Low Battery LED is active when the program launches, handle it 
  if GPIO.input(batteryGPIO) is 0:
    lowBattery(batteryGPIO)

  #if the Power Switch is active when the program launches, handle it
  if GPIO.input(powerGPIO) is 0:
    powerSwitch(powerGPIO)

  #Add threaded event listeners for the Low Battery and Power Switch
  try:
    GPIO.remove_event_detect(batteryGPIO)
    GPIO.add_event_detect(batteryGPIO, GPIO.FALLING, callback=lowBattery, bouncetime=300)

    GPIO.remove_event_detect(powerGPIO)
    GPIO.add_event_detect(powerGPIO, GPIO.FALLING, callback=powerSwitch, bouncetime=300)
  except KeyboardInterrupt:
    GPIO.cleanup()

main()

#We make an endless loop so the threads running the GPIO events will always be listening, in the future we can add Battery Level monitoring here
while True:
  time.sleep(1)

GPIO.cleanup()
[/spoiler]

Then create a script "S99gbzpower" in "/etc/init.d" to launch the python script on startup

Code: Select all

 #! /bin/sh
 
case "$1" in
  start)
    recallog "Starting GBZ Power Management"
    python /recalbox/scripts/GBZPowerMonitor/gbz_power_monitor.py &
    ;;
  stop)
    recallog "Stopping GBZ Power Management"
    killall python
    ;;
  restart|reload)
	;;
  *)
esac

exit $?
To do that you will need to make the folder writable, run the command

Code: Select all

mount -o remount,rw /
Don't worry, on restart Recalbox reset the folder rights to make it read only.

@Popcorn : Thanks again for this great feature

User avatar
RazorX
Posts: 57
Joined: Fri Jun 17, 2016 9:32 am
Has thanked: 3 times
Been thanked: 15 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by RazorX » Sat Jul 09, 2016 7:50 am

there's a more expensive alternative but requires next to no work:

ATXRaspi-R2


https://www.youtube.com/watch?v=w4vSTq2WhN8

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest