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

Raspberry-Python-Pi for Hardware Hacking Fun!

Daniel Bader
November 16, 2013

Raspberry-Python-Pi for Hardware Hacking Fun!

Slides from my talk at Vancouver Python Day 2013 (http://www.vanpyday.com)

Github: http://github.com/dbader/piradio
My blog: http://dbader.org

Daniel Bader

November 16, 2013
Tweet

More Decks by Daniel Bader

Other Decks in Programming

Transcript

  1. +

  2. Hi!

  3. • My project and the lessons learned • Python on

    the Raspberry Pi • Inspire you to try this too
  4. Python on the Pi • Python is a first-class citizen

    • CPython & PyPy (ARM) • A great platform for Python on an embedded system
  5. Parts list • Raspberry Pi Model B ($35) • Display

    + buttons (RaspiLCD) ($55) • Aluminum case + custom CNCed front-plate ($70) • 32 GB SD Card ($20) • Wi-Fi USB stick ($20) • USB power supply ($10) • Total: ~ $200 (in theory)
  6. piradio services audio podcast clock weather … panels radio clock

    alarm weather … podcast Architecture graphics lcd fonts commons config ui
  7. 1. Avoid the dots ! ! def bad(): a =

    A() ! for i in xrange(10000): a.b.c.d.e.foo() ! ! def good(): a = A() foo = a.b.c.d.e.foo for i in xrange(10000): foo() ! ! # 3.3x faster!
  8. 1. Avoid the dots (...) ! FOR_ITER STORE_FAST ! LOAD_FAST

    LOAD_ATTR LOAD_ATTR LOAD_ATTR LOAD_ATTR LOAD_ATTR CALL_FUNCTION ! POP_TOP JUMP_ABSOLUTE POP_BLOCK ! (...) (...) ! FOR_ITER STORE_FAST ! LOAD_FAST CALL_FUNCTION ! POP_TOP JUMP_ABSOLUTE POP_BLOCK ! (...)
  9. 2. range() vs xrange() range(): • returns a list •

    needs more memory • support slices • usually slower xrange(): • returns a generator • needs less memory • no slices • usually faster
  10. 3. Access bitmaps row-wise instead of column-wise … = reality

    “memory layout” abstraction “2d bitmap”
  11. 3. Access bitmaps row-wise instead of column-wise ! ! def

    bad(): bitmap = bytearray(w * h) v = 0 for x in xrange(w): for y in xrange(h): v += bitmap[y * w + x] ! ! def good(): bitmap = bytearray(w * h) v = 0 for y in xrange(h): for x in xrange(w): v += bitmap[y * w + x] ! ! # 1.1x faster (Pi)
  12. Fast Python (on the RPi) • Profile and benchmark everything

    • Trade memory for speed: cache things • Use the right data structures
  13. Fun parts • Building a physical object • Building something

    useful and seeing people use it • UI design and programming in a restricted environment • Programming nostalgia – Pixels!
  14. Not-so-fun parts • Soldering • Front plate margins • Raspberry

    Pi audio crackling • Things get expensive quickly
  15. The Future • I'll release this as open-source on GitHub

    • Finish the blog post on it • Users and contributors welcome