Python on a Chip                  PyMite, a Python Virtual Machine                                                      Je...
Microcontrollers• Programming Microcontrollers is  fun!• You can build your own toys / evil  robots• Even getting lights t...
Whats wrong with C?!                                                    #include <stdio.h>                                ...
Introducing: Python-On-A-Chip●    http://code.google.com/p/python-on-a-chip/●    A project developing PyMite, a Python Vir...
FeaturesIt Does:• Support integers, floats, lists, dicts, functions, modules,  classes, generators, decorators and closure...
25 out of 29 Keywords?PyMite supports the following set of Pythons keywords      and          assert      break           ...
No Standard Libraries?Instead of supporting Pythons libraries, PyMite offer its own setof modules. Some of which have the ...
Other Interesting Bits• PyMite only supports 8-bit character strings, so no  Unicode• PyMite supports 32-bit signed intege...
Installing under Ubuntu 11.04The easiest way to get PyMite is to check it out using Mercurial:$ hg clone https://code.goog...
Windows?  AlmostDownload GNU Make, Python 2.6, SetupTools, and VirtualEnvhttp://gnuwin32.sourceforge.net/packages/make.htm...
Interactive PromptTo compile for the desktop and launch the interactive prompt$ make ipm(make sure you have activated the ...
Microcontrollers?mBed Microcontroller• 32-bit ARM Processor• Built-in USB interface• Powered via USB• Implements a Mass St...
Kiwi Pycon 2011   Python on a Chip   jeremy@stott.co.nz
Robots!m3pi from Pololu•Easy to set up•Ideal for getting started•Cost:– m3pi Robot $150 USD– mbed Micro $60 USD•Surprising...
What about Wireless?Nobody likes to follow their robot around with a laptop…The m3pi has a handy slot for a Digi Zigbee mo...
Putting it all togetherCompile PyMite for the mbed target$ make PLATFORM=mbedImport the resulting .zip archive into the mb...
Thank You                  Any Questions?Kiwi Pycon 2011      Python on a Chip   jeremy@stott.co.nz
Upcoming SlideShare
Loading in …5
×

Python on a chip

4,410 views

Published on

Slides for my presentation "Python on a chip" at KiwiPycon 2011.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,410
On SlideShare
0
From Embeds
0
Number of Embeds
70
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Python on a chip

  1. 1. Python on a Chip PyMite, a Python Virtual Machine Jeremy Stott jeremy@stott.co.nzKiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  2. 2. Microcontrollers• Programming Microcontrollers is fun!• You can build your own toys / evil robots• Even getting lights to blink is fun• Usually programmed in C, sometimes C++ http://lighttrees.com/wordpress/archives/category/sculpture Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  3. 3. Whats wrong with C?! #include <stdio.h> int l;int main(int o,char **O,• Nothing is wrong with C int I){char c,*D=O[1];if(o>0){ for(l=0;D[l ];D[l ++]-=10){D [l++]-=120;D[l]-=• Its up to the programmer to write good 110;while (!main(0,O,l))D[l] += 20; putchar((D[l]+1032) code /20 ) ;}putchar(10);}else{ c=o+ (D[I]+82)%10-(I>l/2)* (D[I-l+I]+72)/10-9;D[I]+=I<0?0• Programmers may suffer from object :!(o=main(c/10,O,I-1))*((c+999 )%10-(D[I]+92)%10);}return o;} oriented withdrawal syndrome• Prolonged use of C may cause inappropriate outbursts of semi-colons;• You cant do [Python reference] in C http://www0.us.ioccc.org/years.html#2001 Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  4. 4. Introducing: Python-On-A-Chip● http://code.google.com/p/python-on-a-chip/● A project developing PyMite, a Python Virtual Machine designed to run on Microcontrollers● A small growing group > 256 mailing list members● Uses 55KB of ROM and 5KB of RAM for "Hello World!"● License: ● Tools: GNU LGPL v2.1 (Lesser GPL) ● Virtual Machine: GPL v2 (Non Commercial) Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  5. 5. FeaturesIt Does:• Support integers, floats, lists, dicts, functions, modules, classes, generators, decorators and closures• Support 25 out of 29 keywords and 89 out of 112 byte codes from Python 2.6• Stackless green threadsIt Doesnt:• Have a built in compiler• Support any of Pythons standard libraries Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  6. 6. 25 out of 29 Keywords?PyMite supports the following set of Pythons keywords    and      assert    break     class     continue    def      del       elif      else      for    from     global    if        import    in    is       lambda    not       or        pass    print    raise     return    while     yieldPyMite does not support these keywords    except  exec   finally   tryHowever, they are working on Exceptions Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  7. 7. No Standard Libraries?Instead of supporting Pythons libraries, PyMite offer its own setof modules. Some of which have the same name as theirPython counterparts. PyMite Modules    dict   func   list   string   sysAs well as many built in functions    abs(x), bytearray(seq), bytearray(len), chr(i),    eval(co), globals(), id(o), len(obj), locals(),     map(f,s), ord(c), pow(x,y), range(...), sum(s),    type(obj) Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  8. 8. Other Interesting Bits• PyMite only supports 8-bit character strings, so no Unicode• PyMite supports 32-bit signed integers, floats (if enabled) but not longs• You cant override type operators. Eg __add__() doesnt override + Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  9. 9. Installing under Ubuntu 11.04The easiest way to get PyMite is to check it out using Mercurial:$ hg clone https://code.google.com/p/python-on-a-chip/Install Python 2.6, setuptools and virtualenv$ sudo apt-get install python2.6 python-setuptools$ sudo easy_install virtualenvCreate the virtual environment and activate it$ virtualenv -p python2.6 --no-site-packages ./pymite$ source pymite/bin/activate Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  10. 10. Windows?  AlmostDownload GNU Make, Python 2.6, SetupTools, and VirtualEnvhttp://gnuwin32.sourceforge.net/packages/make.htmhttp://www.python.org/getit/releases/2.6.6/http://pypi.python.org/pypi/setuptoolsInstall and add their paths to your Path Environment Variablec:Python26c:Python26Scriptsc:Program FilesGnuWin32binInstall and activate VirtualEnv> easy_install.exe virtualenv> virtualenv.exe --no-site-packages c:pymite> c:pymiteScriptsactivate.batAnd then you install a C compiler... Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  11. 11. Interactive PromptTo compile for the desktop and launch the interactive prompt$ make ipm(make sure you have activated the Python2.6 VirtualEnv)Test it outipm> class Robot():....     def walk(self, direction):....         print "Walking %s" % (direction,)....ipm> pybot = Robot()ipm> pybot.walk("to the shops")Walking to the shops Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  12. 12. Microcontrollers?mBed Microcontroller• 32-bit ARM Processor• Built-in USB interface• Powered via USB• Implements a Mass Storage Device, so programming is as easy as copying the binary onto the Flash drive• Many peripherals eg Ethernet, USB, Can, SPI, etc…• Online Compiler Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  13. 13. Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  14. 14. Robots!m3pi from Pololu•Easy to set up•Ideal for getting started•Cost:– m3pi Robot $150 USD– mbed Micro $60 USD•Surprisingly Fast!! Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  15. 15. What about Wireless?Nobody likes to follow their robot around with a laptop…The m3pi has a handy slot for a Digi Zigbee module.But these $35 WiFi modules fit in the same slot:RN-XV from Roving Networkshttp://rovingnetworks.com/XVee.php• Have the same pin-out• Support WPA2• Make the project awesome! Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  16. 16. Putting it all togetherCompile PyMite for the mbed target$ make PLATFORM=mbedImport the resulting .zip archive into the mbed IDEImport the m3pi library into the mbed IDECompile and save the binary onto the mbed microcontrollerTurn on the Robot and connect to the ipm using a pythonsocket$ tools/ipm.py –-ipaddress 192.168.1.3 –-port 2000 Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz
  17. 17. Thank You Any Questions?Kiwi Pycon 2011 Python on a Chip jeremy@stott.co.nz

×