Python On A Chip (Phyton-on-a-Chip)
Python On A Chip (Phyton-on-a-Chip)
PyMite
PyMite is a flyweight Python interpreter. PyMite runs bytecode-compiled PyMite
Python programs or an interactive prompt. This project runs PyMite in Hardware
interactive mode. This project is also a rough draft, so it runs PyMite just fine, Software
but does not have the entire Mbed API wrapped and callable from Python. In Python
time the API will grow to allow you to control all Mbed peripherals from PySerial
Python. PyMite
Mbed Firmware
Hello World!
Hardware Resources
Connect to the Mbed via USB (same port that you use for programming
the Mbed)
The LEDs are hardwired and ready to go.
Optionally, follow the TextLCD recipe, if you want to print to LCD.
Software
http://mbed.co.uk/projects/cookbook/wiki/PyMite Page 1 of 4
PyMite – cookbook – Trac 2009/04/20 07:28
Python
You need the official Python 2.5 (2.6 might work but is untested) on your computer.
http://www.python.org/download/
PySerial
PyMite's interactive mode requires that you install the PySerial package
$ easy_install pyserial
PyMite
You will need to get a copy of the PyMite source tree so you have the tools to run the interactive host
software.
$ cd pymite/src/platform/mbed
$ make zip
Results in pymite_mbed.zip in the current directory. Load pymite_mbed.zip into the on-line Mbed
compiler, compile and download.
Mbed Firmware
PyMite on Mbed is alpha quality: you can get things to work, but there are known and unknown
defects.
Install the pymite_mbed.bin binary built in the previous section, OR download and use this binary
pymite_mbed.bin
Hello World!
Connect to the Mbed via USB (same port that you use for programming the Mbed). Run the PyMite
interactive host software on your desktop or laptop computer (note: your serial port name will vary
depending on your OS):
Now you may type Python source at the prompt, just like you would with Python's interactive
environment. Here is the list of modules that you may import:
chr, dir, eval, globals, len, map, ord, range, sum, type, ismain
All the stuff to control the Mbed is inside the plat module:
The items that start with capital letters are class-like objects (PyMite doesn't have real classes yet)
Here is how to use them:
http://mbed.co.uk/projects/cookbook/wiki/PyMite Page 2 of 4
PyMite – cookbook – Trac 2009/04/20 07:28
# Doing the above will let you use adc16, but adc15 will now refer to adc16.
# This 1 instance limit applies to all other classes.
pm> 40+2
42
ipm> 6 * 7
42
ipm> 2**4
16
ipm> a = 10
ipm> a + 32
42
ipm> foo = range(5)
ipm> foo
[0, 1, 2, 3, 4]
ipm> foo[3] = a
ipm> import list
ipm> list.append(foo, "a string")
ipm> foo
[0, 1, 2, 10, 4, 'a string']
ipm> d = {}
ipm> d["foo"] = foo
ipm> d[0] = "zero"
ipm> d['1'] = 1
ipm> import string; dir(string)
['find', 'count', 'atoi', 'letters', 'hexdigits', 'digits', '__doc__']
ipm> d
{'1':1, 0:'zero', 'foo':[0, 1, 2, 10, 4, 'a string']}
This example defines a function with one default argument and calls the function. Press <Enter>
twice to end a multi-line input:
Resources
PyMite
http://mbed.co.uk/projects/cookbook/wiki/PyMite Page 3 of 4
PyMite – cookbook – Trac 2009/04/20 07:28
Python on a Chip
Python on a Chip
http://mbed.co.uk/projects/cookbook/wiki/PyMite Page 4 of 4