micropython/micropython — pull request #227 by dhylands (2014-01-26 07:31:32+00:00)
Title: Added mbed hal support for gpio and adc

Please do not merge.

I'm putting this up for revew.

To compile, you'll need to clone https://github.com/mbedmicro/mbed so that mbed directory is in the same directory as the micropython repo (so mbed and micropython are in the same directory).

This adds gpio and adc.

Functions implemented:

g = pyb.Gpio(pin, dir)

pin is an integer such that PA0 = 0, PA1 = 1, ... PA15 = 15, PB0 = 16, PB1 = 17, ...
dir is an integer. 0 = input 1 = output

g.mode(m)

m == 0 no pullup
m == 1 pull up
m == 2 pull down
m == 3 open drain

g.dir(d)

d == 0 input
d == 1 output

g.read() returns 0 or 1 whether the pin is low or high
g.write(v) sets the pin to low or high (dir needs to be output)

the modes and direction integers take on different meanings for different platforms, so we may wish to take a string and map the string to a corresponding integer.

a = pyb.Adc(pin)

a.read() returns a float in the range 0.0 to 1.0
a.read_u16() returns an integer (in the range 0 - 4095

There is a C constant called ADC_RANGE, set to 4095 which we should probably expose.

Passing in an pin which doesn't correspond to an adc pin (for example calling pyb.Adc(8) winds up calling error() in mbed_hal which currently calls mbed_die and then __fatal_error

I think that it should probably throw an exception instead.

And how do we call a python function from C?