-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathremote_gpiozero.py
44 lines (29 loc) · 1.22 KB
/
remote_gpiozero.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/python3
# Example of using remote GPIO access with gpiozero, installing picraftzero on the target Pi is not required.
# For setup and more info see: https://gpiozero.readthedocs.io/en/docs-updates/remote_gpio.html
# Run using:
# GPIOZERO_PIN_FACTORY=PiGPIOPin PIGPIO_ADDR=raspberry.local python3 remote_gpiozero.py
# If env var not present then default them:
from os import environ
if 'GPIOZERO_PIN_FACTORY' not in environ:
environ['GPIOZERO_PIN_FACTORY'] = 'PiGPIOPin'
environ['PIGPIO_ADDR'] = 'raspberrypi.local' # or IP address '192.168.1.108'
# ------------------------------------------------------------------------
# Carry on as normal...
from gpiozero import LED
from picraftzero import Joystick, Button, start
joystick = Joystick() # use the first available controller's rightmost joystick (required)
button0 = Button(0) # attach some buttons by id
led = LED(17)
def button0_pressed():
print("on")
led.on()
def button0_released():
print("off")
led.off()
# Use the gpiozero callback style:
button0.when_pressed = button0_pressed
button0.when_released= button0_released
# Or use the spiozero source/value style:
#led.source = button0.values
start()