Você está na página 1de 4

PyMite – cookbook – Trac

PyMite – cookbook – Trac PyMite PyMite is a flyweight Python interpreter. PyMite runs bytecode-compiled Python

PyMite is a flyweight Python interpreter. PyMite runs bytecode-compiled Python programs or an interactive prompt. This project runs PyMite in interactive mode. This project is also a rough draft, so it runs PyMite just fine, but does not have the entire Mbed API wrapped and callable from Python. In time the API will grow to allow you to control all Mbed peripherals from Python.

Hardware

Connect to the Mbed via USB (same port that you use for programming the Mbed)PyMite Mbed Firmware Hello World! Resources The LEDs are hardwired and ready to go. Optionally, follow

The LEDs are hardwired and ready to go.via USB (same port that you use for programming the Mbed) Optionally, follow the TextLCD recipe,

Optionally, follow the TextLCD recipe, if you want to print to LCD. TextLCD recipe, if you want to print to LCD.

2009/04/20 07:28

recipe, if you want to print to LCD. 2009/04/20 07:28 Software http://mbed.co.uk/projects/cookbook/wiki/PyMite

Software

PyMite – cookbook – Trac

Python

2009/04/20 07:28

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

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://svn.pymite.python-hosting.com/trunk pymite

OPTION: If you want to zip your own project source tree:

$ 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):

$ pymite/src/tools/ipm.py --serial=/dev/cu.usbmodem1912 --baud=19200 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!

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:

sys, list, dict, string, plat

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

chr, dir, eval, globals, len, map, ord, range, sum, type, ismain

All the stuff to control the Mbed is inside the plat module:

ipm> from plat import * ipm> dir()

 

['

doc

',

'AnalogIn', 'AnalogOut', 'DigitalIn', 'DigitalOut', 'PwmOut', 'Text

 
 
 
 

The items that start with capital letters are class-like objects (PyMite doesn't have real classes yet) Here is how to use them:

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

11595

ipm> adc16 = AnalogIn(16)

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.

 

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

# This method will be renamed to write_u16() in the fut

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

0

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

 

1

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

 

ipm> lcd = 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
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:

ipm> def bar(n=42):

return ["bar",] * n

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

Press Ctrl+D to exit the interactive interpreter.

Resources

More about PyMite PyMite

PyMite – cookbook – Trac

Python on a Chip

PyMite – cookbook – Trac Python on a Chip Python on a Chip 2009/04/20 07:28

2009/04/20 07:28