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

Abstraction 101

Abstraction 101

Making Python code more accessible

Ben Nuttall

March 05, 2016
Tweet

More Decks by Ben Nuttall

Other Decks in Education

Transcript

  1. Ben Nuttall • Education Developer Advocate at the Raspberry Pi

    Foundation – Software & project development – Learning resources & teacher training – Community outreach • @ben_nuttall on Twitter
  2. 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
  3. 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)
  4. 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
  5. File Manager • ls • ls ­al • cd •

    pwd • cp • mv • tree
  6. Pibrella • Traffic Lights – red, amber, green • Button

    • Buzzer • Labelled Inputs • Labelled Outputs
  7. 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'
  8. 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)
  9. 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)
  10. 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()
  11. 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
  12. GPIO Zero LED from gpiozero import LED led = LED(17)

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

    15), right=(17, 18)) robot.forward() robot.left() robot.right() robot.stop()
  15. 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()
  16. 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()
  17. 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
  18. 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
  19. Import from file from lesson_1 import forward, backward from time

    import sleep while True: forward() sleep(1) backward() sleep(1)
  20. 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
  21. 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