Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GPIO Zero - AmsterJam

Ben Nuttall
September 09, 2017

GPIO Zero - AmsterJam

GPIO Zero talk given at AmsterJam

Ben Nuttall

September 09, 2017
Tweet

More Decks by Ben Nuttall

Other Decks in Programming

Transcript

  1. 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]
  2. 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()
  3. 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
  4. .value >>> led = PWMLED(17) >>> pot = MCP3008() >>>

    led.value = pot.value >>> while True: ... led.value = pot.value
  5. Source / Values from gpiozero import LED, Button led =

    LED(17) button = Button(2) led.source = button.values
  6. Source tools from gpiozero import Button, LED from gpiozero.tools import

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

    .values Source tool Input Device .value .values
  8. 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)
  9. 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
  10. MockPin $ GPIOZERO_PIN_FACTORY=mock python3 >>> from gpiozero import LED >>>

    led = LED(22) >>> led.blink() >>> led.value True >>> led.value False
  11. MockPin >>> from gpiozero import LED >>> led = LED(22)

    >>> button = Button(23) >>> led.source = button.values >>> led.value False >>> button.pin.drive_low() >>> led.value True
  12. pigpio - remote GPIO from Pi or PC from gpiozero

    import LED from gpiozero.pins.pigpio import PiGPIOFactory factory = PiGPIOFactory('192.168.0.2') led = LED(22, pin_factory=factory) led.blink()
  13. 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()
  14. Energenie tortoise lamp from gpiozero import Energenie, TimeOfDay from datetime

    import time lamp = Energenie(1) daytime = TimeOfDay(time(9), time(18)) lamp.source = daytime.values
  15. Is the internet working? from gpiozero import LED, PingServer from

    gpiozero.tools import negated green = LED(17) red = LED(18) google = PingServer('google.com') green.source = google.values green.source_delay = 60 red.source = negated(green.values)
  16. 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