Slide 1

Slide 1 text

Raspberry-Python-Pi for Hardware Hacking Fun! Daniel Bader – @dbader_org

Slide 2

Slide 2 text

Hardware-hacking for software people.

Slide 3

Slide 3 text

+

Slide 4

Slide 4 text

piradio

Slide 5

Slide 5 text

Hi!

Slide 6

Slide 6 text

• My project and the lessons learned • Python on the Raspberry Pi • Inspire you to try this too

Slide 7

Slide 7 text

Why?

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

How?

Slide 10

Slide 10 text

Raspberry Pi

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Python on the Pi • Python is a first-class citizen • CPython & PyPy (ARM) • A great platform for Python on an embedded system

Slide 14

Slide 14 text

RaspiLCD kit by Martin Stepphun (emsystech.de)

Slide 15

Slide 15 text

Soldering

Slide 16

Slide 16 text

The case

Slide 17

Slide 17 text

Custom CNCed front plate schaeffer-ag.de/en

Slide 18

Slide 18 text

“Front-plate Designer” schaeffer-ag.de/en

Slide 19

Slide 19 text

Margins

Slide 20

Slide 20 text

Aluminum case Proma 1030 - conrad.de

Slide 21

Slide 21 text

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)

Slide 22

Slide 22 text

Software

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Architecture

Slide 25

Slide 25 text

Philosophy: Do as much as possible in pure Python.

Slide 26

Slide 26 text

Architecture Linux piradio CPython mpd freetype raspilcd

Slide 27

Slide 27 text

piradio services audio podcast clock weather … panels radio clock alarm weather … podcast Architecture graphics lcd fonts commons config ui

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Things I learned

Slide 30

Slide 30 text

Simulate where possible

Slide 31

Slide 31 text

• Implemented as a “fake LCD” driver • pygame + libSDL • Saved me so much time

Slide 32

Slide 32 text

Take advantage of the familiar programming environment

Slide 33

Slide 33 text

Fast Python (on the Raspberry Pi)

Slide 34

Slide 34 text

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!

Slide 35

Slide 35 text

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 ! (...)

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

3. Access bitmaps row-wise instead of column-wise … = reality “memory layout” abstraction “2d bitmap”

Slide 38

Slide 38 text

3. Access bitmaps row-wise instead of column-wise … = nice, sequential memory access

Slide 39

Slide 39 text

3. Access bitmaps row-wise instead of column-wise … = jumping all over the place

Slide 40

Slide 40 text

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)

Slide 41

Slide 41 text

Fast Python (on the RPi) • Profile and benchmark everything • Trade memory for speed: cache things • Use the right data structures

Slide 42

Slide 42 text

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!

Slide 43

Slide 43 text

Not-so-fun parts • Soldering • Front plate margins • Raspberry Pi audio crackling • Things get expensive quickly

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

The Future • I'll release this as open-source on GitHub • Finish the blog post on it • Users and contributors welcome

Slide 46

Slide 46 text

github.com/dbader/piradio dbader.org/blog/tags/python ! @dbader_org Thanks! Questions? Ideas?