1

Hi was just wondering if there was a way of making a GPIO INPUT (connected to a momentary switch) stay high (for a certain amount of time), no matter how long the switch has been pressed for?

The problem I've got is, I'm using a geekwork X728 board (does battery charging, UPS, battery power and safe shutdown, reboot and power off). If you press the switch for less than 3 seconds it reboots, and longer than that does a shutdown and cuts the power. I really need to lose the reboot part to make it easier for the user to use my design.

It's straightforward enough to stop the Pi rebooting, but there is clearly firmware on the x728 board that's counting the time the switch is pushed for, so if it does get pressed for less than 3 seconds it does a shutdown but then a 'bootok' LED just flashes and the power isn't fully cut from the pi. But pressing more than 3 seconds does everything I need it to do.

So I was thinking can I trick the X728 into thinking the switch has been pressed for more than 3 seconds whether it really has or not?

I've tried quite a few things already.

I tried turning the input GPIO input into an output as soon as switch is pressed, but the pin must go low in-between which isn't enough to trick the x728 but that causes the flashing LED, not cutting power scenario. I also don't think its possible for the switch to start as an input and turn into an output whilst it's in use.

Example code (that doesn't work):

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

GPIO.setmode(GPIO.BCM)

SHUTDOWN = 5
BOOT = 12

GPIO.setup(BOOT, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(SHUTDOWN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

while True:
    GPIO.wait_for_edge(SHUTDOWN, GPIO.RISING)
    shutdownSignal = GPIO.input(SHUTDOWN)
    while shutdownSignal:
        GPIO.setup(5, GPIO.OUT)
        GPIO.output(5, GPIO.HIGH)         
        sleep(0.5)                  
        GPIO.output(5, GPIO.LOW)         

I've tried the obvious things of modifying the x728.sh script to do shutdown rather than reboot if pressed for less than 3 seconds, but that also causes the flashing LED, not cutting power scenario

I've tried modifying the reboot time in the x728.sh script, but that also causes the flashing LED, not cutting power scenario.

Not sure what else to do. I just literally want it to shutdown and cut power with one button and boot up with the same button, which it nearly does just this horrible reboot/press for less than 3 second problem.

Anyone got any ideas please?

X728pwr.sh code:

 echo '#!/bin/bash
SHUTDOWN=5
REBOOTPULSEMINIMUM=200
REBOOTPULSEMAXIMUM=600
echo "$SHUTDOWN" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$SHUTDOWN/direction
BOOT=12
echo "$BOOT" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$BOOT/direction
echo "1" > /sys/class/gpio/gpio$BOOT/value
echo "X728 Shutting down..."
while [ 1 ]; do
  shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
  if [ $shutdownSignal = 0 ]; then
    /bin/sleep 0.2
  else  
    pulseStart=$(date +%s%N | cut -b1-13)
    while [ $shutdownSignal = 1 ]; do
      /bin/sleep 0.02
      if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMAXIMUM ]; then
        echo "X728 Shutting down", SHUTDOWN, ", halting Rpi ..."
        sudo poweroff
        exit
      fi
      shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
    done
    if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMINIMUM ]; then 
      echo "X728 Rebooting", SHUTDOWN, ", recycling Rpi ..."
      sudo reboot
      exit
    fi
  fi
done' > /etc/x728pwr.sh

the full github code is here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.