6

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.)

4

2 回答 2

4

我使用 RPi.GPIO 模块而不是 gpiozero 重写了。感觉不同,但它比使用 gpiozero 研究一种无需清理即可退出的方法更容易。

这是没有引脚清理的“等效”程序。

#!/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.
于 2018-12-05T19:05:03.630 回答
0

gpiozero 并不真正支持。

这里有一个“正在进行的”讨论: https ://github.com/gpiozero/gpiozero/issues/707

CAM-Gerlach 提到了一个 hack,它似乎有效:

import gpiozero.pins.rpigpio

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

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

我们基本上覆盖了 PinFactory 的关闭函数并使用它来创建 LED。

于 2020-11-07T17:16:27.520 回答