PyMite

PyMite is a flyweight Python virtual machine. This project runs PyMite in the interactive mode that allows you to type Python code at a live prompt and have it execute on the mbed device. PyMite comes with its own small library of modules and this project includes wrappers for some of the classes and methods in the mbed library.

Hardware

  • 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.
source:/TextLCD/doc/TextLCDSchematic.png source:/PyMite/doc/screenshot.jpg

Software

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 firmware for mbed

You will need to get a copy of the PyMite source tree so you have the tools to run the interactive host software.

$ svn co http://python-on-a-chip.googlecode.com/svn/trunk/ pymite

OPTION 1: Use this published program to compile the binary.

OR

OPTION 2: Zip your own project source tree:

$ cd pymite/src/platform/mbed
$ make zip

Results in the zip file, pymite/src/platform/mbed/pymite_mbed.zip, that can be imported to the on-line mbed compiler and compiled to a binary.

Install the pymite_mbed.bin binary that was just built and reset the mbed device to activate it.

ipm, the interactive PyMite prompt

With the binary copied and installed on the mbed device, 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):

$ pymite/src/tools/ipm.py -s /dev/cu.usbmodem1912
This is the interactive PyMite command line.
Just type the code that you want the target device to run.
Type another return if you see no prompt to exit multiline mode.
Type Ctrl+C to interrupt and Ctrl+D to quit.
ipm> print "Hello World!"
Hello World!

If that worked, you may type Python source at the prompt, just like you would with Python's interactive environment. Just be aware that any exception requires you to reset the mbed device. Here is the list of modules that you may import:

sys, list, dict, string, mbed

Here is the list of builtin functions that you may call:

abs, chr, dir, eval, globals, len, locals, map, ord, pow, range, sum, type

The mbed module provides class and method wrappers for most of the basic mbed libraries:

ipm> import mbed
ipm> dir(mbed)
['set_led', 'TextLCD', 'I2C', 'SPI', 'Serial', 'PwmOut', 'DigitalOut', 'DigitalIn', 'AnalogOut', 'AnalogIn', '__doc__']

The items that start with capital letters are class-like objects. Here is how to use them. Notice that you use integers not PinNames to specify the pins to use:

ipm> adc15 = mbed.AnalogIn(15)
ipm> adc15.read_u16()
11595

ipm> dac = mbed.AnalogOut(18)
ipm> dac.write(0x100)

ipm> din = mbed.DigitalIn(5)
ipm> din.read()
0

ipm> dout = mbed.DigitalOut(30)
ipm> dout.write(1)
ipm> dout.read()
1

ipm> pwm21 = mbed.PwmOut(21)
ipm> pwm21.period_us(1000)
ipm> pwm21.pulsewidth_us(500)

# Using the TextLCD requires connect the hardware properly
ipm> lcd = mbed.TextLCD()
ipm> lcd.printf("PyMite on mbed!")
ipm> lcd.cls()

ipm> set_led(1,1) # LED1 on
ipm> set_led(3,1) # LED3 on
ipm> set_led(1,0) # LED1 off

More PyMite Examples

This demonstrates the use of basic datatypes in Python

pm> 40+2
42
ipm> 6 * 7
42
ipm> 2**4
16
ipm> a = 10
ipm> a + 32
42
ipm> 3 + .1415  # floats!
3.141500
ipm> 2.2 ** 1.8
4.133908
ipm> 6.02E23 / 1.21E9
497520690069504.000000
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:

ipm> def bar(n=42):
  return ["bar",] * n

ipm> print bar(4)
['bar', 'bar', 'bar', 'bar']
ipm> ^D

Press Ctrl+D (or Ctrl+Z,<enter> on Windows) to exit the interactive interpreter.


Resources

Python on a Chip

The PyMite VM is part of the Python-on-a-Chip project: