GPIO Zero

GPIO Zero

Talk given at the Raspberry Pi Big Birthday Weekend

A1995c9abe48450ce2f82d93ca0b863f?s=128

Ben Nuttall

March 04, 2017
Tweet

Transcript

  1. GPIO Zero Ben Nuttall Raspberry Pi Foundation UK Charity 1129409

  2. 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 • ben@raspberrypi.org
  3. GPIO Pins – General Purpose Input/Output

  4. GPIO Zero: a friendly API for GPIO devices from gpiozero

    import LED led = LED(17) led.on()
  5. 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()
  6. 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
  7. Multi-paradigm: declarative from gpiozero import LED, Button led = LED(17)

    button = Button(4) led.source = button.values
  8. GPIO Zero supports...

  9. GPIO Zero Device Hierarchy!

  10. .value >>> led = PWMLED(17) >>> led.value 0.0 >>> led.on()

    >>> led.value 1.0 >>> led.value = 0
  11. .value >>> led = PWMLED(17) >>> pot = MCP3008() >>>

    led.value = pot.value
  12. .value >>> led = PWMLED(17) >>> pot = MCP3008() >>>

    led.value = pot.value >>> while True: ... led.value = pot.value
  13. Source / Values Output Device .value .values .source Input Device

    .value .values
  14. Source / Values Output Device .value .values .source Input Device

    .value .values
  15. Source/Values from gpiozero import LED, Button led = LED(17) button

    = Button(2) led.source = button.values
  16. Processing values Output Device .value .values .source Input Device .value

    .values function
  17. Source tools from gpiozero import Button, LED from gpiozero.tools import

    negated led = LED(4) btn = Button(17) led.source = negated(btn.values)
  18. Combining values Output Device .value .values .source Input Device .value

    .values Source tool Input Device .value .values
  19. 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)
  20. 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
  21. MockPin $ GPIOZERO_PIN_FACTORY=MockPin python3 >>> from gpiozero import LED >>>

    led = LED(22) >>> led.blink() >>> led.value True >>> led.value False
  22. 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
  23. pigpio - remote GPIO from Pi or PC

  24. 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()
  25. 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()
  26. Internal devices • TimeOfDay • PingServer • CPUTemperature • More

    coming soon • Make your own!
  27. 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
  28. 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)
  29. 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
  30. guizero

  31. PIXEL x86

  32. PIXEL x86 – Remote GPIO

  33. GitHub

  34. What's coming?

  35. 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
  36. pinout command line tool

  37. Read the docs!

  38. 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
  39. The MagPi #55

  40. GPIO Zero Book

  41. GPIO Zero Ben Nuttall Raspberry Pi Foundation UK Charity 1129409