7

I am using gpiozero to control devices on the Raspberry Pi. When I create a reference to (for example) an LED device, there is a parameter for creating the object without effecting it's current state: initial_state=None. (The default is initial_state=False, which automatically turns the value off upon reference object creation) The problem is it always seems to reset the hardware pin on script exit (though oddly enough not the internal "state"). What's worse, when I run the script again, it knows the state I left it in, and puts the physical pin back to that state!

Here's my jumpers on/off program, it now has a pausing input, during which the state stays unchanged, but when the program exits, the pins reset. (Though as I mention above, the state is "remembered")

#!/usr/bin/env python
from __future__ import print_function
import sys
import time
from gpiozero import LED
jump1=LED(17,initial_value=None)
jump2=LED(27,initial_value=None)


if len(sys.argv)>1:
    print ("Jumper were: (%s,%s)"%(str(jump1.is_active),str(jump2.is_active)))
    if sys.argv[1].lower() == 'on':
        jump1.on()
        jump2.on()
        print ('turned both on')
    elif sys.argv[1].lower() == 'off':
        jump1.off()
        jump2.off()
        print ('turned both off')

print ("Jumper Currently: (%s,%s)"%(str(jump1.is_active),str(jump2.is_active)))

raw_input("Press enter to exit.")

Does anyone know a way to tell gpiozero to leave the hardware alone after exit? This question details a similar problem, though a different module.

(Edit: it turns out the gpiozero module changes the pin direction to input but doesn't change the output latch, which is how it gets the old state back when the pin direction is changed back to output.)

RufusVS
  • 4,008
  • 3
  • 29
  • 40
  • It was about 2 years ago and I had a similar issue - I can't remember the details but I looked in the source and there was some `atexit()` thing in there that I overrode or cancelled or avoided getting called by calling `sysexit()` or similar. Sorry, it's vague and not a proper answer but look for some `atexit()` call in your library. – Mark Setchell Dec 04 '18 at 20:44

2 Answers2

5

I rewrote using RPi.GPIO module instead of gpiozero. It feels different, but it was easier than researching a way to do exit without cleanup using gpiozero.

Here is the "equivalent" program without pin cleanup.

#!/usr/bin/env python
from __future__ import print_function
import sys
import time
import RPi.GPIO as GPIO

# these pin numbers map have to change
# try the 'pinout' command from the bash prompt

pina = 17
pinb = 27


# set pins up:

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pina, GPIO.OUT)
GPIO.setup(pinb, GPIO.OUT)


if len(sys.argv)>1:
    print ("Jumpers were: (%s,%s)"%  (str(GPIO.input(pina)),str(GPIO.input(pinb))))
    if sys.argv[1].lower() == 'on':
        GPIO.output(pina, GPIO.HIGH)
        GPIO.output(pinb, GPIO.HIGH)
        print ('turned both on')
    elif sys.argv[1].lower() == 'off':
        GPIO.output(pina, GPIO.LOW)
        GPIO.output(pinb, GPIO.LOW)
        print ('turned both off')

print ("Jumpers now: (%s,%s)"%    (str(GPIO.input(pina)),str(GPIO.input(pinb))))

#raw_input("Press enter to exit.")  # optional pause for testing

# Note:  I/O pins will remain at their last state.
RufusVS
  • 4,008
  • 3
  • 29
  • 40
0

Not really supported in gpiozero.

There is an "ongoing" discussion over here: https://github.com/gpiozero/gpiozero/issues/707

There is a hack mentioned by CAM-Gerlach, that seems to work:

import gpiozero.pins.rpigpio

def close(self): pass
gpiozero.pins.rpigpio.RPiGPIOPin.close = close

gpiozero.LED(..., pin_factory=gpiozero.pins.rpigpio.RPiGPIOFactory())

We basically overwrite the close function of the PinFactory and use that one for creating the LED.

Xan-Kun Clark-Davis
  • 2,664
  • 2
  • 27
  • 38