Slide 1

Slide 1 text

Abstraction 101 Making Python code more accessible Ben Nuttall Raspberry Pi Foundation

Slide 2

Slide 2 text

Ben Nuttall ● Education Developer Advocate at the Raspberry Pi Foundation – Software & project development – Learning resources & teacher training – Community outreach ● @ben_nuttall on Twitter

Slide 3

Slide 3 text

What is abstraction? ● Replacing commonly repeated blocks of code with generic implementations ● Simplifying commonly used software patterns ● Making domain specific interfaces from broad generic ones ● Only focusing on the details important to the user and their intentions

Slide 4

Slide 4 text

Why use abstraction? ● Make programming more accessible ● Stop repeating code ● Simpler, more readable and understandable code ● Don't waste time reinventing the wheel (or googling how wheels work and copying it) ● Reduce friction and opportunities to give up (or not start)

Slide 5

Slide 5 text

Abstraction is for lazy people? ● Yes and no – Yes, and it's good to be a lazy programmer! Learn to be faster and more efficient – No, it's for sensible people who just want to make progress

Slide 6

Slide 6 text

Reduce the Learning Curve

Slide 7

Slide 7 text

File Manager ● ls ● ls ­al ● cd ● pwd ● cp ● mv ● tree

Slide 8

Slide 8 text

Turtle

Slide 9

Slide 9 text

Pibrella ● Traffic Lights – red, amber, green ● Button ● Buzzer ● Labelled Inputs ● Labelled Outputs

Slide 10

Slide 10 text

Pibrella pibrella.light.red.on() pibrella.light.amber.on() pibrella.light.green.on() pibrella.button.pressed(event)

Slide 11

Slide 11 text

Flotilla

Slide 12

Slide 12 text

Energenie #import the required modules import RPi.GPIO as GPIO import time # set the pins numbering mode GPIO.setmode(GPIO.BOARD) # Select the GPIO pins used for the encoder K0­K3 data inputs GPIO.setup(11, GPIO.OUT) GPIO.setup(15, GPIO.OUT) GPIO.setup(16, GPIO.OUT) GPIO.setup(13, GPIO.OUT) # Select the signal to select ASK/FSK GPIO.setup(18, GPIO.OUT) # Select the signal used to enable/disable the modulator GPIO.setup(22, GPIO.OUT) # Disable the modulator by setting CE pin lo GPIO.output (22, False) # Set the modulator to ASK for On Off Keying # by setting MODSEL pin lo GPIO.output (18, False) # Initialise K0­K3 inputs of the encoder to 0000 GPIO.output (11, False) GPIO.output (15, False) GPIO.output (16, False) GPIO.output (13, False) # The On/Off code pairs correspond to the hand controller codes. # True = '1', False ='0'

Slide 13

Slide 13 text

Energenie from energenie import switch_on, switch_off from time import sleep # turn all plug sockets on and off switch_on() switch_off() # turn a plug socket on and off by number switch_on(3) switch_off(3)

Slide 14

Slide 14 text

PyGame Zero

Slide 15

Slide 15 text

PyGame & PyGame Zero Sense HAT Apps

Slide 16

Slide 16 text

RPi.GPIO and GPIO Zero import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(17, GPIO.OUT) while True: GPIO.output(17, GPIO.HIGH) sleep(1) GPIO.output(17, GPIO.LOW) sleep(1) from gpiozero import LED from time import sleep led = LED(17) while True: led.on() sleep(1) led.off() sleep(1)

Slide 17

Slide 17 text

RPi.GPIO and GPIO Zero import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(17, GPIO.OUT) while True: GPIO.output(17, GPIO.HIGH) sleep(1) GPIO.output(17, GPIO.LOW) sleep(1) from gpiozero import LED led = LED(17) led.blink()

Slide 18

Slide 18 text

GPIO Zero LED + Button from gpiozero import LED, Button led = LED(17) button = Button(3) button.when_pressed = led.on button.when_released = led.off

Slide 19

Slide 19 text

GPIO Zero LED from gpiozero import LED led = LED(17) led.on() led.off() led.toggle() led.blink()

Slide 20

Slide 20 text

RPi.GPIO Robot for pin in (14, 15, 17, 18): GPIO.setup(pin, GPIO.OUT) # forward GPIO.output(14, GPIO.LOW) GPIO.output(15, GPIO.HIGH) GPIO.output(17, GPIO.LOW) GPIO.output(18, GPIO.HIGH)

Slide 21

Slide 21 text

GPIO Zero Robot from gpiozero import Robot robot = Robot(left=(14, 15), right=(17, 18)) robot.forward() robot.left() robot.right() robot.stop()

Slide 22

Slide 22 text

GPIO Zero Pre-defined Robots RyanteckRobot() CamJamKitRobot()

Slide 23

Slide 23 text

An implementation of GPIO Zero (simplified) class LED(): def __init__(self, pin): self.pin = pin GPIO.setup(pin, GPIO.OUT) def on(self): GPIO.output(self.pin, True) def off(self): GPIO.output(self.pin, False) >>> led = LED(17) >>> led.on() >>> led.off()

Slide 24

Slide 24 text

GPIO Zero Traffic HAT Interface from gpiozero import TrafficHat hat = TrafficHat() hat.lights.green.on() hat.lights.amber.on() hat.lights.red.blink() hat.buzzer.beep()

Slide 25

Slide 25 text

GPIO Zero Traffic HAT Interface hat.button.when_pressed = hat.on hat.button.when_released = hat.off hat.button.when_pressed = hat.lights.blink hat.button.when_released = hat.off hat.button.when_pressed = sequence

Slide 26

Slide 26 text

Modules ● Standard library (e.g. time, datetime, signal, etc.) ● Third party modules (e.g. GPIO Zero, picamera, mcpi) – Some are pre-installed in Raspbian – Others you can download and install from pypi.python.org – apt­get install or pip install

Slide 27

Slide 27 text

PyPI

Slide 28

Slide 28 text

Import import picamera camera = picamera.PiCamera()

Slide 29

Slide 29 text

Import from picamera import PiCamera camera = PiCamera()

Slide 30

Slide 30 text

Import * from gpiozero import * button = Button(2) led = LED(3)

Slide 31

Slide 31 text

Import from file from lesson_1 import forward, backward from time import sleep while True: forward() sleep(1) backward() sleep(1)

Slide 32

Slide 32 text

Try it yourself! ● Maplin Robot Arm – MoveArm(1,[0,2,0]) #Rotate base clockwise – MoveArm(1,[64,0,0]) #Shoulder up ● Make your own traffic light set ● Make your own Pibrella / Traffic HAT

Slide 33

Slide 33 text

Share your code! ● GitHub! ● Consider publishing as a module – See my talk “Building a Python API for Raspberry Pi Hardware” ● If a module already exists, consider contributing to it

Slide 34

Slide 34 text

Abstraction 101 Making Python code more accessible Ben Nuttall Raspberry Pi Foundation