此系列文章参考:https://gpiozero.readthedocs.io/en/stable/#
原文 github 信息:
地址:https://github.com/raspberrypi/documentation
commit:612c6ed3
硬件信息: Raspberrypi 4B
gpiozero 参考: https://pypi.org/project/gpiozero/1.5.1/
gpiozero 是 rasbperry 的 GPIO 接口,由 Raspberry Pi Foundation 维护。
from gpiozero import LED
from time import sleep
led = LED(17)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
from gpiozero import LED, Button
from signal import pause
led = LED(17)
button = Button(3)
button.when_pressed = led.on
button.when_released = led.off
pause()
from gpiozero import LED, MotionSensor, LightSensor
from gpiozero.tools import booleanized, all_values
from signal import pause
garden = LED(17)
motion = MotionSensor(4)
light = LightSensor(5)
garden.source = all_values(booleanized(light, 0, 0.1), motion)
pause()
gpiozero 是基于一些 pin library 生成的,如:RPi.GPIO 和 pigpio。设备可以根据实际情况来选用 pin library 。
Raspbian desktop image 默认安装了 gpiozero。其他系统的安装可以参考本系列文章的安装章节。