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

Programming paradigms for physical computing and IoT

Programming paradigms for physical computing and IoT

Talk given at London Raspberry Pint April 2018 meetup

Ben Nuttall

April 24, 2018
Tweet

More Decks by Ben Nuttall

Other Decks in Programming

Transcript

  1. Programming paradigms for
    physical computing and IoT
    Ben Nuttall
    Raspberry Pi Foundation
    UK Charity 1129409

    View full-size slide

  2. Ben Nuttall

    Raspberry Pi Community Manager

    Based in Cambridge

    Creator of gpiozero python library and
    piwheels project

    Columnist on opensource.com

    github.com/bennuttall

    twitter.com/ben_nuttall

    [email protected]

    View full-size slide

  3. GPIO Pins – General Purpose
    Input/Output

    View full-size slide

  4. GPIO Zero: a friendly API for GPIO devices
    from gpiozero import LED
    led = LED(17)
    led.on()

    View full-size slide

  5. Multi-paradigm: procedural (polling)
    from gpiozero import LED, Button
    led = LED(17)
    button = Button(4)
    while True:
    if button.is_pressed:
    led.on()
    else:
    led.off()

    View full-size slide

  6. Multi-paradigm: procedural (blocking)
    from gpiozero import LED, Button
    led = LED(17)
    button = Button(4)
    while True:
    button.wait_for_press()
    led.on()
    button.wait_for_release()
    led.off()

    View full-size slide

  7. 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

    View full-size slide

  8. Multi-paradigm: declarative
    from gpiozero import LED, Button
    led = LED(17)
    button = Button(4)
    led.source = button.values

    View full-size slide

  9. GPIO Zero supports...

    View full-size slide

  10. GPIO Zero Device Hierarchy!

    View full-size slide

  11. .value
    >>> led = PWMLED(17)
    >>> led.value
    0.0
    >>> led.on()
    >>> led.value
    1.0
    >>> led.value = 0

    View full-size slide

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

    View full-size slide

  13. .value
    >>> while True:
    ... led.value = pot.value

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  16. Source / Values
    from gpiozero import LED, Button
    led = LED(17)
    button = Button(2)
    led.source = button.values

    View full-size slide

  17. Processing values
    Output Device
    .value
    .values
    .source
    Input Device
    .value
    .values
    function

    View full-size slide

  18. Source tools
    from gpiozero import Button, LED
    from gpiozero.tools import negated
    led = LED(4)
    btn = Button(17)
    led.source = negated(btn.values)

    View full-size slide

  19. Combining values
    Output Device
    .value
    .values
    .source
    Input Device
    .value
    .values
    Source tool
    Input Device
    .value
    .values

    View full-size slide

  20. 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)

    View full-size slide

  21. Cross-platform

    Raspbian (Raspberry Pi)

    Raspberry Pi Desktop x86 (PC)

    Linux (PC)

    Windows (PC)

    Mac OS (Mac)

    View full-size slide

  22. 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

    View full-size slide

  23. MockPin
    $ GPIOZERO_PIN_FACTORY=mock python3
    >>> from gpiozero import LED
    >>> led = LED(22)
    >>> led.blink()
    >>> led.value
    True
    >>> led.value
    False

    View full-size slide

  24. MockPin
    >>> from gpiozero import LED, Button
    >>> led = LED(22)
    >>> button = Button(23)
    >>> led.source = button.values
    >>> led.value
    False
    >>> button.pin.drive_low()
    >>> led.value
    True

    View full-size slide

  25. pigpio - remote GPIO from Pi or PC

    View full-size slide

  26. 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()

    View full-size slide

  27. 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()

    View full-size slide

  28. Raspberry Pi Desktop x86

    View full-size slide

  29. Raspberry Pi Desktop x86 – Remote GPIO

    View full-size slide

  30. Raspberry Pi Desktop x86 – GPIO
    expander

    View full-size slide

  31. Internal devices

    TimeOfDay

    PingServer

    CPUTemperature

    More coming soon

    Make your own!

    View full-size slide

  32. 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

    View full-size slide

  33. 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)

    View full-size slide

  34. 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

    View full-size slide

  35. Custom internal devices
    from gpiozero import InternalDevice
    class FileReader(InternalDevice):
    @property
    def value(self):
    with open('value.txt') as f:
    return int(f.read().strip())

    View full-size slide

  36. Multi-paradigm: procedural (polling)
    from gpiozero import LED
    from bluedot import BlueDot
    led = LED(17)
    bd = BlueDot()
    while True:
    if bd.is_pressed:
    led.on()
    else:
    led.off()

    View full-size slide

  37. Multi-paradigm: procedural (blocking)
    from gpiozero import LED
    from bluedot import BlueDot
    led = LED(17)
    bd = BlueDot()
    while True:
    bd.wait_for_press()
    led.on()
    bd.wait_for_release()
    led.off()

    View full-size slide

  38. Multi-paradigm: event-driven
    from gpiozero import LED
    from bluedot import BlueDot
    led = LED(17)
    bd = BlueDot()
    bd.when_pressed = led.on
    bd.when_released = led.off

    View full-size slide

  39. Multi-paradigm: declarative
    from gpiozero import LED
    from bluedot import BlueDot
    led = LED(17)
    bd = BlueDot()
    led.source = bd.values

    View full-size slide

  40. IoT devices?
    from somelib import GardenLight, LightSensor, MotionSensor
    from gpiozero.tools import all_values, negated
    garden = GardenLight()
    light = LightSensor()
    motion = MotionSensor()
    garden.source = all_values(negated(light.values), motion.values)

    View full-size slide

  41. pinout command line tool

    View full-size slide

  42. Read the docs!

    View full-size slide

  43. Programming paradigms for
    physical computing and IoT
    Ben Nuttall
    Raspberry Pi Foundation
    UK Charity 1129409

    View full-size slide