Abstraction 101

Abstraction 101

Making Python code more accessible

A1995c9abe48450ce2f82d93ca0b863f?s=128

Ben Nuttall

March 05, 2016
Tweet

Transcript

  1. Abstraction 101 Making Python code more accessible Ben Nuttall Raspberry

    Pi Foundation
  2. Ben Nuttall • Education Developer Advocate at the Raspberry Pi

    Foundation – Software & project development – Learning resources & teacher training – Community outreach • @ben_nuttall on Twitter
  3. 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
  4. 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)
  5. 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
  6. Reduce the Learning Curve

  7. File Manager • ls • ls ­al • cd •

    pwd • cp • mv • tree
  8. Turtle

  9. Pibrella • Traffic Lights – red, amber, green • Button

    • Buzzer • Labelled Inputs • Labelled Outputs
  10. Pibrella pibrella.light.red.on() pibrella.light.amber.on() pibrella.light.green.on() pibrella.button.pressed(event)

  11. Flotilla

  12. 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'
  13. 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)
  14. PyGame Zero

  15. PyGame & PyGame Zero Sense HAT Apps

  16. 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)
  17. 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()
  18. 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
  19. GPIO Zero LED from gpiozero import LED led = LED(17)

    led.on() led.off() led.toggle() led.blink()
  20. 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)
  21. GPIO Zero Robot from gpiozero import Robot robot = Robot(left=(14,

    15), right=(17, 18)) robot.forward() robot.left() robot.right() robot.stop()
  22. GPIO Zero Pre-defined Robots RyanteckRobot() CamJamKitRobot()

  23. 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()
  24. 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()
  25. 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
  26. 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
  27. PyPI

  28. Import import picamera camera = picamera.PiCamera()

  29. Import from picamera import PiCamera camera = PiCamera()

  30. Import * from gpiozero import * button = Button(2) led

    = LED(3)
  31. Import from file from lesson_1 import forward, backward from time

    import sleep while True: forward() sleep(1) backward() sleep(1)
  32. 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
  33. 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
  34. Abstraction 101 Making Python code more accessible Ben Nuttall Raspberry

    Pi Foundation