Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Programming paradigms for physical computing and IoT
Ben Nuttall
April 24, 2018
Programming
0
78
Programming paradigms for physical computing and IoT
Talk given at London Raspberry Pint April 2018 meetup
Ben Nuttall
April 24, 2018
Tweet
Share
More Decks by Ben Nuttall
See All by Ben Nuttall
Running a Python Package Index for Raspberry Pi
bennuttall
0
18
From Acorns to Raspberries
bennuttall
0
8
Innovation in the newsroom
bennuttall
0
15
Innovation in the newsroom - MOS Running Order Manager
bennuttall
0
40
How to market your open source project
bennuttall
0
79
Manage your own Pi Cloud with hostedpi
bennuttall
0
38
Tools for maintaining an open source project
bennuttall
0
37
Tools for maintaining an open source Python project
bennuttall
0
66
piwheels: a Python package repository for Raspberry Pi
bennuttall
0
34
Other Decks in Programming
See All in Programming
How to get satisfaction from ungrateful work: A journey into updating Kotlin
syrinet
0
120
Reinventing the wheel ... as a service
mariofusco
2
150
書籍『良いコード/悪いコードで学ぶ設計入門』でエンジニアリングの当たり前を変える
minodriven
3
1k
How useEvent would change our applications
koba04
1
1.3k
SPA/MPA 議論の俯瞰と 現代における設計のポイント - #tfcon 2022 フロントエンド設計
ahomu
2
1.4k
Groovy Roadmap
paulk
7
13k
Managing gRPC with Wire
oldergod
2
150
Yumemi.apk #6 ~ゆめみのAndroidエンジニア 日頃の成果大発表会!~ Session 2
blendthink
1
200
From Java 11 to 17 and beyond
josepaumard
0
280
New Relicを使った Observabilityの実現方法と活用例 / gocon 2022 spring after talk
budougumi0617
0
860
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
770
microCMS × Shopifyで、ECサイトがリニューアル後急成長した話
microcms
0
440
Featured
See All Featured
A better future with KSS
kneath
225
15k
Music & Morning Musume
bryan
35
4.1k
The Invisible Side of Design
smashingmag
289
48k
Build The Right Thing And Hit Your Dates
maggiecrowley
19
1.1k
Navigating Team Friction
lara
175
11k
The Pragmatic Product Professional
lauravandoore
19
2.9k
Building Applications with DynamoDB
mza
83
4.6k
How to name files
jennybc
39
58k
WebSockets: Embracing the real-time Web
robhawkes
57
5k
Rails Girls Zürich Keynote
gr2m
86
12k
Large-scale JavaScript Application Architecture
addyosmani
499
110k
How To Stay Up To Date on Web Technology
chriscoyier
780
250k
Transcript
Programming paradigms for physical computing and IoT Ben Nuttall Raspberry
Pi Foundation UK Charity 1129409
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 • ben@raspberrypi.org
GPIO Pins – General Purpose Input/Output
GPIO Zero: a friendly API for GPIO devices from gpiozero
import LED led = LED(17) led.on()
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()
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()
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
Multi-paradigm: declarative from gpiozero import LED, Button led = LED(17)
button = Button(4) led.source = button.values
GPIO Zero supports...
GPIO Zero Device Hierarchy!
.value >>> led = PWMLED(17) >>> led.value 0.0 >>> led.on()
>>> led.value 1.0 >>> led.value = 0
.value >>> led = PWMLED(17) >>> pot = MCP3008() >>>
led.value 0.0 >>> pot.value 0.510145879738202 >>> led.value = pot.value
.value >>> while True: ... led.value = pot.value
Source / Values Output Device .value .values .source Input Device
.value .values
Source / Values Output Device .value .values .source Input Device
.value .values
Source / Values from gpiozero import LED, Button led =
LED(17) button = Button(2) led.source = button.values
Processing values Output Device .value .values .source Input Device .value
.values function
Source tools from gpiozero import Button, LED from gpiozero.tools import
negated led = LED(4) btn = Button(17) led.source = negated(btn.values)
Combining values Output Device .value .values .source Input Device .value
.values Source tool Input Device .value .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)
Cross-platform • Raspbian (Raspberry Pi) • Raspberry Pi Desktop x86
(PC) • Linux (PC) • Windows (PC) • Mac OS (Mac)
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
MockPin $ GPIOZERO_PIN_FACTORY=mock python3 >>> from gpiozero import LED >>>
led = LED(22) >>> led.blink() >>> led.value True >>> led.value False
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
pigpio - remote GPIO from Pi or PC
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()
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()
Raspberry Pi Desktop x86
Raspberry Pi Desktop x86 – Remote GPIO
Raspberry Pi Desktop x86 – GPIO expander
Internal devices • TimeOfDay • PingServer • CPUTemperature • More
coming soon • Make your own!
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
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)
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
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())
Blue Dot
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()
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()
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
Multi-paradigm: declarative from gpiozero import LED from bluedot import BlueDot
led = LED(17) bd = BlueDot() led.source = bd.values
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)
guizero
GitHub
pinout command line tool
Read the docs!
The MagPi
Programming paradigms for physical computing and IoT Ben Nuttall Raspberry
Pi Foundation UK Charity 1129409