GPIO Zero
Ben Nuttall
Raspberry Pi Foundation
UK Charity 1129409
Slide 2
Slide 2 text
Ben Nuttall
●
Raspberry Pi Community Manager
●
Based in Cambridge
●
Creator of gpiozero python library
●
Columnist on opensource.com
●
github.com/bennuttall
●
twitter.com/ben_nuttall
●
[email protected]
Slide 3
Slide 3 text
GPIO Pins – General Purpose
Input/Output
Slide 4
Slide 4 text
GPIO Zero: a friendly API for GPIO devices
from gpiozero import LED
led = LED(17)
led.on()
Slide 5
Slide 5 text
Multi-paradigm: procedural
from gpiozero import LED, Button
led = LED(17)
button = Button(4)
while True:
if button.is_pressed:
led.on()
else:
led.off()
Slide 6
Slide 6 text
Multi-paradigm: event-driven
from gpiozero import LED, Button
led = LED(17)
button = Button(4)
button.when_pressed = led.on
button.when_released = led.off
Slide 7
Slide 7 text
Multi-paradigm: declarative
from gpiozero import LED, Button
led = LED(17)
button = Button(4)
led.source = button.values
Source tools
from gpiozero import Button, LED
from gpiozero.tools import all_values
button_a = Button(2)
button_b = Button(3)
led = LED(17)
led.source = all_values(button_a.values, button_b.values)
Slide 20
Slide 20 text
Supporting multiple back-ends
●
RPi.GPIO
●
Implemented in C, current default
●
RPIO
●
Implemented in C
●
pigpio
●
Python wrapper for C library, runs as daemon, remote pins
●
Native
●
Pure Python, limited functionality, experimental
●
MockPin & MockPWMPin
●
Pure Python, used in test suite
Slide 21
Slide 21 text
MockPin
$ GPIOZERO_PIN_FACTORY=MockPin python3
>>> from gpiozero import LED
>>> led = LED(22)
>>> led.blink()
>>> led.value
True
>>> led.value
False
Slide 22
Slide 22 text
MockPin
>>> from gpiozero import LED
>>> from gpiozero.pins.mock import MockPin
>>> led = LED(22)
>>> button = Button(23)
>>> led.source = button.values
>>> pin = MockPin(23)
>>> led.value
False
>>> pin.drive_low()
>>> led.value
True
Slide 23
Slide 23 text
pigpio - remote GPIO from Pi or PC
Slide 24
Slide 24 text
pigpio - remote GPIO from Pi or PC
from gpiozero import LED
from gpiozero.pins.pigpiod import PiGPIOPin
led = LED(PiGPIOPin(22, host='192.168.0.2'))
led.blink()
Slide 25
Slide 25 text
pigpio - remote GPIO from Pi or PC
$ PIGPIO_ADDR=192.168.0.2 python3 led.py
from gpiozero import LED
led = LED(22)
led.blink()
Slide 26
Slide 26 text
Internal devices
●
TimeOfDay
●
PingServer
●
CPUTemperature
●
More coming soon
●
Make your own!
Slide 27
Slide 27 text
Energenie tortoise lamp
from gpiozero import Energenie, TimeOfDay
from datetime import time
lamp = Energenie(1)
daytime = TimeOfDay(time(8), time(20))
lamp.source = daytime.values
Slide 28
Slide 28 text
Is the internet working?
from gpiozero import LED, PingServer
from gpiozero.tools import negated
green = LED(17)
red = LED(18)
google = PingServer('google.com')
google.source_delay = 60
green.source = google.values
red.source = negated(green.values)
Slide 29
Slide 29 text
CPU Temperature
from gpiozero import LEDBarGraph, CPUTemperature
cpu = CPUTemperature(min_temp=50, max_temp=90)
leds = LEDBarGraph(2, 3, 4, 5, 6, 7, 8, pwm=True)
leds.source = cpu.values
Slide 30
Slide 30 text
guizero
Slide 31
Slide 31 text
PIXEL x86
Slide 32
Slide 32 text
PIXEL x86 – Remote GPIO
Slide 33
Slide 33 text
GitHub
Slide 34
Slide 34 text
What's coming?
Slide 35
Slide 35 text
What's coming?
●
More GPIO devices, more internal devices
●
More protocols (I2C, one wire, etc.)
●
Improved documentation
●
Improved Remote GPIO support including Pi Zero OTG
●
Improved advanced features
●
GUI representation for real and mock pins
●
pinout command line tool
Slide 36
Slide 36 text
pinout command line tool
Slide 37
Slide 37 text
Read the docs!
Slide 38
Slide 38 text
Contributors
●
Core team:
– Ben Nuttall
– Dave Jones
– Andrew Scheller
●
Contributors:
– Martin O'Hanlon
– Steve Amor
– David Glaude
– Edward Betts
– Alex Chan
– Thijs Triemstra
– Schelto vanDoorn
– Alex Eames
– Barry Byford
– Clare Macrae
– Tim Golden
– Phil Howard
Slide 39
Slide 39 text
The MagPi #55
Slide 40
Slide 40 text
GPIO Zero Book
Slide 41
Slide 41 text
GPIO Zero
Ben Nuttall
Raspberry Pi Foundation
UK Charity 1129409