简体   繁体   English

在 `raspberry pi` 上使用 `gpiozero` 来控制引脚,但输出引脚会在脚本退出时重置,即使在运行之间记住状态

[英]Using `gpiozero` on `raspberry pi` to control pins, but output pins are reset upon script exit, even though state is remembered between runs

I am using gpiozero to control devices on the Raspberry Pi.我正在使用gpiozero来控制 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 .当我创建对(例如)LED 设备的引用时,有一个用于创建对象而不影响其当前状态的参数: 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"). (默认值是initial_state=False ,它会在创建引用对象时自动关闭该值)问题是它似乎总是在脚本退出时重置硬件引脚(尽管很奇怪不是内部“状态”)。 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?有没有人知道告诉gpiozero在退出后不要gpiozero硬件的方法? 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.) (编辑:事实证明 gpiozero 模块将引脚方向更改为输入但不更改输出锁存器,这就是当引脚方向更改回输出时它如何恢复旧状态。)

I rewrote using RPi.GPIO module instead of gpiozero.我使用 RPi.GPIO 模块而不是 gpiozero 重写。 It feels different, but it was easier than researching a way to do exit without cleanup using gpiozero.感觉不同,但它比研究一种使用 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.

Not really supported in gpiozero. gpiozero 并不真正支持。

There is an "ongoing" discussion over here: https://github.com/gpiozero/gpiozero/issues/707这里有一个“正在进行的”讨论: https : //github.com/gpiozero/gpiozero/issues/707

There is a hack mentioned by CAM-Gerlach, that seems to work: 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())

We basically overwrite the close function of the PinFactory and use that one for creating the LED.我们基本上覆盖了 PinFactory 的 close 函数并使用该函数来创建 LED。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM