Programming paradigms for physical computing and IoT

A1995c9abe48450ce2f82d93ca0b863f?s=47 Ben Nuttall
September 18, 2018

Programming paradigms for physical computing and IoT

Talk given at PyCon UK 2018

A1995c9abe48450ce2f82d93ca0b863f?s=128

Ben Nuttall

September 18, 2018
Tweet

Transcript

  1. Programming paradigms for physical computing and IoT Ben Nuttall Raspberry

    Pi Foundation UK Charity 1129409
  2. GPIO Pins – General Purpose Input/Output

  3. GPIO Zero – a friendly API for GPIO devices from

    gpiozero import LED led = LED(2) led.blink()
  4. GPIO Zero – a friendly API for GPIO devices •

    Zero-boilerplate Pythonic library • Intended for use in education • Simple, guessable API with commonly used names and sensible default values • Simple introduction, smooth learning curve • Multi-paradigm • Extendable
  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()
  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()
  7. Multi-paradigm: event-driven (callbacks) from gpiozero import LED, Button led =

    LED(17) button = Button(4) button.when_pressed = led.on button.when_released = led.off
  8. Multi-paradigm: declarative from gpiozero import LED, Button led = LED(17)

    button = Button(4) led.source = button.values
  9. .value >>> led = PWMLED(17) >>> led.value 0.0 >>> led.on()

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

    led.value 0.0 >>> pot.value 0.008130081300813009 >>> led.value = pot.value
  11. .value >>> while True: ... led.value = pot.value

  12. .values >>> for v in pot.values: ... print(v) 0.008130081300813009 0.008130081300675679

    0.008130081300700108 0.008130081300414001
  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 LED Button

  16. Source / Values from gpiozero import LED, Button led =

    LED(17) button = Button(2) led.source = button.values
  17. Source / Values PWM LED Potentiometer

  18. Processing values Output Device .value .values .source Input Device .value

    .values function
  19. Processing values LED negated Button

  20. Source tools from gpiozero import Button, LED from gpiozero.tools import

    negated led = LED(4) btn = Button(17) led.source = negated(btn.values)
  21. Source tools – single source conversions • absoluted • booleanized

    • clamped • inverted • negated • post_delayed • post_periodic_fltered • pre_delayed • pre_periodic_fltered • quantized • queued • smoothed • scaled
  22. Combining values Output Device .value .values .source Input Device .value

    .values Source tool Input Device .value .values
  23. Combining values LED Button all_values Button

  24. Source tools – combining sources • all_values • any_values •

    averaged • multiplied • summed
  25. Chaining combined values LED Motion sensor all_values Light sensor booleanized

  26. Chaining combined values – garden light prototype from gpiozero import

    LED, LightSensor, MotionSensor from gpiozero.tools import all_values, booleanized garden = LED() light = LightSensor() motion = MotionSensor() garden.source = all_values(booleanized(light.values, 0, 0.1), motion.values)
  27. Use source/values in your library from somelib import GardenLight, LightSensor,

    MotionSensor from gpiozero.tools import all_values, booleanized garden = GardenLight() light = LightSensor() motion = MotionSensor() garden.source = all_values(booleanized(light.values, 0, 0.1), motion.values)
  28. Combining paradigms while True: light.wait_for_dark() garden.source = motion.values light.wait_for_light() garden.source

    = None
  29. Artifcial values Output Device .value .values .source function

  30. Source tools – artifcial sources • alternating_values • cos_values •

    ramping_values • random_values • sin_values
  31. 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
  32. Blue Dot

  33. 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()
  34. 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()
  35. Multi-paradigm: event-driven (callbacks) from gpiozero import LED from bluedot import

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

    led = LED(17) bd = BlueDot() led.source = bd.values
  37. BlueDot Robot def move(pos): if pos.top: robot.forward(pos.distance) elif pos.bottom: robot.backward(pos.distance)

    elif pos.left: robot.left(pos.distance) elif pos.right: robot.right(pos.distance) bd.when_pressed = move bd.when_moved = move bd.when_released = robot.stop
  38. Z-Wave devices & asyncio

  39. GPIO Zero on GitHub & ReadTheDocs

  40. piwheels • Python package repository providing Arm platform wheels for

    Raspberry Pi • Builds automated from PyPI releases, plus manual builds e.g. opencv & tensorfow • Raspbian is pre-confgured to use piwheels.org as an additional index to PyPI • Massively reduces pip install time for Raspberry Pi users (saved 23 years of build time in 10 months) • Natively compiled on Raspberry Pi 3 hardware (Mythic Beasts Pi cloud) • Repo hosted on single Raspberry Pi serving 400-500k packages per month
  41. Raspberry Jam • Independently organised community events around the world

    • Family-friendly • Mix of meetup / conference / workshop styles • Makers, hackers, programmers & beginners come together • Find one near you – or start your own! • raspberrypi.org/jam
  42. CoderDojo • Free coding clubs for young people • Find

    one near you and volunteer as a mentor – or start a new Dojo in your area • coderdojo.com
  43. Programming paradigms for physical computing and IoT Ben Nuttall Raspberry

    Pi Foundation UK Charity 1129409