Port of MicroPython to the mbed platform. See micropython-repl for an interactive program.

Dependents:   micropython-repl

This a port of MicroPython to the mbed Classic platform.

This provides an interpreter running on the board's USB serial connection.

Getting Started

Import the micropython-repl program into your IDE workspace on developer.mbed.org. Compile and download to your board. Connect to the USB serial port in your usual manner. You should get a startup message similar to the following:

  MicroPython v1.7-155-gdddcdd8 on 2016-04-23; K64F with ARM
  Type "help()" for more information.
  >>>

Then you can start using micropython. For example:

  >>> from mbed import DigitalOut
  >>> from pins import LED1
  >>> led = DigitalOut(LED1)
  >>> led.write(1)

Requirements

You need approximately 100K of flash memory, so this will be no good for boards with smaller amounts of storage.

Caveats

This can be considered an alpha release of the port; things may not work; APIs may change in later releases. It is NOT an official part part the micropython project, so if anything doesn't work, blame me. If it does work, most of the credit is due to micropython.

  • Only a few of the mbed classes are available in micropython so far, and not all methods of those that are.
  • Only a few boards have their full range of pin names available; for others, only a few standard ones (USBTX, USBRX, LED1) are implemented.
  • The garbage collector is not yet implemented. The interpreter will gradually consume memory and then fail.
  • Exceptions from the mbed classes are not yet handled.
  • Asynchronous processing (e.g. events on inputs) is not supported.

Credits

  • Damien P. George and other contributors who created micropython.
  • Colin Hogben, author of this port.
Committer:
Colin Hogben
Date:
Wed Apr 27 22:11:29 2016 +0100
Revision:
10:33521d742af1
Parent:
0:5868e8752d44
Update README and version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pythontech 0:5868e8752d44 1 /*
pythontech 0:5868e8752d44 2 * This file is part of the Micro Python project, http://micropython.org/
pythontech 0:5868e8752d44 3 *
pythontech 0:5868e8752d44 4 * The MIT License (MIT)
pythontech 0:5868e8752d44 5 *
pythontech 0:5868e8752d44 6 * Copyright (c) 2013, 2014 Damien P. George
pythontech 0:5868e8752d44 7 *
pythontech 0:5868e8752d44 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
pythontech 0:5868e8752d44 9 * of this software and associated documentation files (the "Software"), to deal
pythontech 0:5868e8752d44 10 * in the Software without restriction, including without limitation the rights
pythontech 0:5868e8752d44 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pythontech 0:5868e8752d44 12 * copies of the Software, and to permit persons to whom the Software is
pythontech 0:5868e8752d44 13 * furnished to do so, subject to the following conditions:
pythontech 0:5868e8752d44 14 *
pythontech 0:5868e8752d44 15 * The above copyright notice and this permission notice shall be included in
pythontech 0:5868e8752d44 16 * all copies or substantial portions of the Software.
pythontech 0:5868e8752d44 17 *
pythontech 0:5868e8752d44 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pythontech 0:5868e8752d44 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pythontech 0:5868e8752d44 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pythontech 0:5868e8752d44 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pythontech 0:5868e8752d44 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pythontech 0:5868e8752d44 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pythontech 0:5868e8752d44 24 * THE SOFTWARE.
pythontech 0:5868e8752d44 25 */
pythontech 0:5868e8752d44 26
pythontech 0:5868e8752d44 27 #include "py/builtin.h"
pythontech 0:5868e8752d44 28
pythontech 0:5868e8752d44 29 #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_BUILTINS_COMPLEX && MICROPY_PY_CMATH
pythontech 0:5868e8752d44 30
pythontech 0:5868e8752d44 31 #include <math.h>
pythontech 0:5868e8752d44 32
pythontech 0:5868e8752d44 33 /// \module cmath - mathematical functions for complex numbers
pythontech 0:5868e8752d44 34 ///
pythontech 0:5868e8752d44 35 /// The `cmath` module provides some basic mathematical funtions for
pythontech 0:5868e8752d44 36 /// working with complex numbers.
pythontech 0:5868e8752d44 37
pythontech 0:5868e8752d44 38 /// \function phase(z)
pythontech 0:5868e8752d44 39 /// Returns the phase of the number `z`, in the range (-pi, +pi].
pythontech 0:5868e8752d44 40 STATIC mp_obj_t mp_cmath_phase(mp_obj_t z_obj) {
pythontech 0:5868e8752d44 41 mp_float_t real, imag;
pythontech 0:5868e8752d44 42 mp_obj_get_complex(z_obj, &real, &imag);
pythontech 0:5868e8752d44 43 return mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real));
pythontech 0:5868e8752d44 44 }
pythontech 0:5868e8752d44 45 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_phase_obj, mp_cmath_phase);
pythontech 0:5868e8752d44 46
pythontech 0:5868e8752d44 47 /// \function polar(z)
pythontech 0:5868e8752d44 48 /// Returns, as a tuple, the polar form of `z`.
pythontech 0:5868e8752d44 49 STATIC mp_obj_t mp_cmath_polar(mp_obj_t z_obj) {
pythontech 0:5868e8752d44 50 mp_float_t real, imag;
pythontech 0:5868e8752d44 51 mp_obj_get_complex(z_obj, &real, &imag);
pythontech 0:5868e8752d44 52 mp_obj_t tuple[2] = {
pythontech 0:5868e8752d44 53 mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)),
pythontech 0:5868e8752d44 54 mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real)),
pythontech 0:5868e8752d44 55 };
pythontech 0:5868e8752d44 56 return mp_obj_new_tuple(2, tuple);
pythontech 0:5868e8752d44 57 }
pythontech 0:5868e8752d44 58 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_polar_obj, mp_cmath_polar);
pythontech 0:5868e8752d44 59
pythontech 0:5868e8752d44 60 /// \function rect(r, phi)
pythontech 0:5868e8752d44 61 /// Returns the complex number with modulus `r` and phase `phi`.
pythontech 0:5868e8752d44 62 STATIC mp_obj_t mp_cmath_rect(mp_obj_t r_obj, mp_obj_t phi_obj) {
pythontech 0:5868e8752d44 63 mp_float_t r = mp_obj_get_float(r_obj);
pythontech 0:5868e8752d44 64 mp_float_t phi = mp_obj_get_float(phi_obj);
pythontech 0:5868e8752d44 65 return mp_obj_new_complex(r * MICROPY_FLOAT_C_FUN(cos)(phi), r * MICROPY_FLOAT_C_FUN(sin)(phi));
pythontech 0:5868e8752d44 66 }
pythontech 0:5868e8752d44 67 STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_cmath_rect_obj, mp_cmath_rect);
pythontech 0:5868e8752d44 68
pythontech 0:5868e8752d44 69 /// \function exp(z)
pythontech 0:5868e8752d44 70 /// Return the exponential of `z`.
pythontech 0:5868e8752d44 71 STATIC mp_obj_t mp_cmath_exp(mp_obj_t z_obj) {
pythontech 0:5868e8752d44 72 mp_float_t real, imag;
pythontech 0:5868e8752d44 73 mp_obj_get_complex(z_obj, &real, &imag);
pythontech 0:5868e8752d44 74 mp_float_t exp_real = MICROPY_FLOAT_C_FUN(exp)(real);
pythontech 0:5868e8752d44 75 return mp_obj_new_complex(exp_real * MICROPY_FLOAT_C_FUN(cos)(imag), exp_real * MICROPY_FLOAT_C_FUN(sin)(imag));
pythontech 0:5868e8752d44 76 }
pythontech 0:5868e8752d44 77 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_exp_obj, mp_cmath_exp);
pythontech 0:5868e8752d44 78
pythontech 0:5868e8752d44 79 /// \function log(z)
pythontech 0:5868e8752d44 80 /// Return the natural logarithm of `z`. The branch cut is along the negative real axis.
pythontech 0:5868e8752d44 81 // TODO can take second argument, being the base
pythontech 0:5868e8752d44 82 STATIC mp_obj_t mp_cmath_log(mp_obj_t z_obj) {
pythontech 0:5868e8752d44 83 mp_float_t real, imag;
pythontech 0:5868e8752d44 84 mp_obj_get_complex(z_obj, &real, &imag);
pythontech 0:5868e8752d44 85 return mp_obj_new_complex(0.5 * MICROPY_FLOAT_C_FUN(log)(real*real + imag*imag), MICROPY_FLOAT_C_FUN(atan2)(imag, real));
pythontech 0:5868e8752d44 86 }
pythontech 0:5868e8752d44 87 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log_obj, mp_cmath_log);
pythontech 0:5868e8752d44 88
pythontech 0:5868e8752d44 89 #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
pythontech 0:5868e8752d44 90 /// \function log10(z)
pythontech 0:5868e8752d44 91 /// Return the base-10 logarithm of `z`. The branch cut is along the negative real axis.
pythontech 0:5868e8752d44 92 STATIC mp_obj_t mp_cmath_log10(mp_obj_t z_obj) {
pythontech 0:5868e8752d44 93 mp_float_t real, imag;
pythontech 0:5868e8752d44 94 mp_obj_get_complex(z_obj, &real, &imag);
pythontech 0:5868e8752d44 95 return mp_obj_new_complex(0.5 * MICROPY_FLOAT_C_FUN(log10)(real*real + imag*imag), 0.4342944819032518 * MICROPY_FLOAT_C_FUN(atan2)(imag, real));
pythontech 0:5868e8752d44 96 }
pythontech 0:5868e8752d44 97 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log10_obj, mp_cmath_log10);
pythontech 0:5868e8752d44 98 #endif
pythontech 0:5868e8752d44 99
pythontech 0:5868e8752d44 100 /// \function sqrt(z)
pythontech 0:5868e8752d44 101 /// Return the square-root of `z`.
pythontech 0:5868e8752d44 102 STATIC mp_obj_t mp_cmath_sqrt(mp_obj_t z_obj) {
pythontech 0:5868e8752d44 103 mp_float_t real, imag;
pythontech 0:5868e8752d44 104 mp_obj_get_complex(z_obj, &real, &imag);
pythontech 0:5868e8752d44 105 mp_float_t sqrt_abs = MICROPY_FLOAT_C_FUN(pow)(real*real + imag*imag, 0.25);
pythontech 0:5868e8752d44 106 mp_float_t theta = 0.5 * MICROPY_FLOAT_C_FUN(atan2)(imag, real);
pythontech 0:5868e8752d44 107 return mp_obj_new_complex(sqrt_abs * MICROPY_FLOAT_C_FUN(cos)(theta), sqrt_abs * MICROPY_FLOAT_C_FUN(sin)(theta));
pythontech 0:5868e8752d44 108 }
pythontech 0:5868e8752d44 109 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sqrt_obj, mp_cmath_sqrt);
pythontech 0:5868e8752d44 110
pythontech 0:5868e8752d44 111 /// \function cos(z)
pythontech 0:5868e8752d44 112 /// Return the cosine of `z`.
pythontech 0:5868e8752d44 113 STATIC mp_obj_t mp_cmath_cos(mp_obj_t z_obj) {
pythontech 0:5868e8752d44 114 mp_float_t real, imag;
pythontech 0:5868e8752d44 115 mp_obj_get_complex(z_obj, &real, &imag);
pythontech 0:5868e8752d44 116 return mp_obj_new_complex(MICROPY_FLOAT_C_FUN(cos)(real) * MICROPY_FLOAT_C_FUN(cosh)(imag), -MICROPY_FLOAT_C_FUN(sin)(real) * MICROPY_FLOAT_C_FUN(sinh)(imag));
pythontech 0:5868e8752d44 117 }
pythontech 0:5868e8752d44 118 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_cos_obj, mp_cmath_cos);
pythontech 0:5868e8752d44 119
pythontech 0:5868e8752d44 120 /// \function sin(z)
pythontech 0:5868e8752d44 121 /// Return the sine of `z`.
pythontech 0:5868e8752d44 122 STATIC mp_obj_t mp_cmath_sin(mp_obj_t z_obj) {
pythontech 0:5868e8752d44 123 mp_float_t real, imag;
pythontech 0:5868e8752d44 124 mp_obj_get_complex(z_obj, &real, &imag);
pythontech 0:5868e8752d44 125 return mp_obj_new_complex(MICROPY_FLOAT_C_FUN(sin)(real) * MICROPY_FLOAT_C_FUN(cosh)(imag), MICROPY_FLOAT_C_FUN(cos)(real) * MICROPY_FLOAT_C_FUN(sinh)(imag));
pythontech 0:5868e8752d44 126 }
pythontech 0:5868e8752d44 127 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sin_obj, mp_cmath_sin);
pythontech 0:5868e8752d44 128
pythontech 0:5868e8752d44 129 STATIC const mp_rom_map_elem_t mp_module_cmath_globals_table[] = {
pythontech 0:5868e8752d44 130 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cmath) },
pythontech 0:5868e8752d44 131 { MP_ROM_QSTR(MP_QSTR_e), mp_const_float_e },
pythontech 0:5868e8752d44 132 { MP_ROM_QSTR(MP_QSTR_pi), mp_const_float_pi },
pythontech 0:5868e8752d44 133 { MP_ROM_QSTR(MP_QSTR_phase), MP_ROM_PTR(&mp_cmath_phase_obj) },
pythontech 0:5868e8752d44 134 { MP_ROM_QSTR(MP_QSTR_polar), MP_ROM_PTR(&mp_cmath_polar_obj) },
pythontech 0:5868e8752d44 135 { MP_ROM_QSTR(MP_QSTR_rect), MP_ROM_PTR(&mp_cmath_rect_obj) },
pythontech 0:5868e8752d44 136 { MP_ROM_QSTR(MP_QSTR_exp), MP_ROM_PTR(&mp_cmath_exp_obj) },
pythontech 0:5868e8752d44 137 { MP_ROM_QSTR(MP_QSTR_log), MP_ROM_PTR(&mp_cmath_log_obj) },
pythontech 0:5868e8752d44 138 #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
pythontech 0:5868e8752d44 139 { MP_ROM_QSTR(MP_QSTR_log10), MP_ROM_PTR(&mp_cmath_log10_obj) },
pythontech 0:5868e8752d44 140 #endif
pythontech 0:5868e8752d44 141 { MP_ROM_QSTR(MP_QSTR_sqrt), MP_ROM_PTR(&mp_cmath_sqrt_obj) },
pythontech 0:5868e8752d44 142 //{ MP_ROM_QSTR(MP_QSTR_acos), MP_ROM_PTR(&mp_cmath_acos_obj) },
pythontech 0:5868e8752d44 143 //{ MP_ROM_QSTR(MP_QSTR_asin), MP_ROM_PTR(&mp_cmath_asin_obj) },
pythontech 0:5868e8752d44 144 //{ MP_ROM_QSTR(MP_QSTR_atan), MP_ROM_PTR(&mp_cmath_atan_obj) },
pythontech 0:5868e8752d44 145 { MP_ROM_QSTR(MP_QSTR_cos), MP_ROM_PTR(&mp_cmath_cos_obj) },
pythontech 0:5868e8752d44 146 { MP_ROM_QSTR(MP_QSTR_sin), MP_ROM_PTR(&mp_cmath_sin_obj) },
pythontech 0:5868e8752d44 147 //{ MP_ROM_QSTR(MP_QSTR_tan), MP_ROM_PTR(&mp_cmath_tan_obj) },
pythontech 0:5868e8752d44 148 //{ MP_ROM_QSTR(MP_QSTR_acosh), MP_ROM_PTR(&mp_cmath_acosh_obj) },
pythontech 0:5868e8752d44 149 //{ MP_ROM_QSTR(MP_QSTR_asinh), MP_ROM_PTR(&mp_cmath_asinh_obj) },
pythontech 0:5868e8752d44 150 //{ MP_ROM_QSTR(MP_QSTR_atanh), MP_ROM_PTR(&mp_cmath_atanh_obj) },
pythontech 0:5868e8752d44 151 //{ MP_ROM_QSTR(MP_QSTR_cosh), MP_ROM_PTR(&mp_cmath_cosh_obj) },
pythontech 0:5868e8752d44 152 //{ MP_ROM_QSTR(MP_QSTR_sinh), MP_ROM_PTR(&mp_cmath_sinh_obj) },
pythontech 0:5868e8752d44 153 //{ MP_ROM_QSTR(MP_QSTR_tanh), MP_ROM_PTR(&mp_cmath_tanh_obj) },
pythontech 0:5868e8752d44 154 //{ MP_ROM_QSTR(MP_QSTR_isfinite), MP_ROM_PTR(&mp_cmath_isfinite_obj) },
pythontech 0:5868e8752d44 155 //{ MP_ROM_QSTR(MP_QSTR_isinf), MP_ROM_PTR(&mp_cmath_isinf_obj) },
pythontech 0:5868e8752d44 156 //{ MP_ROM_QSTR(MP_QSTR_isnan), MP_ROM_PTR(&mp_cmath_isnan_obj) },
pythontech 0:5868e8752d44 157 };
pythontech 0:5868e8752d44 158
pythontech 0:5868e8752d44 159 STATIC MP_DEFINE_CONST_DICT(mp_module_cmath_globals, mp_module_cmath_globals_table);
pythontech 0:5868e8752d44 160
pythontech 0:5868e8752d44 161 const mp_obj_module_t mp_module_cmath = {
pythontech 0:5868e8752d44 162 .base = { &mp_type_module },
pythontech 0:5868e8752d44 163 .name = MP_QSTR_cmath,
pythontech 0:5868e8752d44 164 .globals = (mp_obj_dict_t*)&mp_module_cmath_globals,
pythontech 0:5868e8752d44 165 };
pythontech 0:5868e8752d44 166
pythontech 0:5868e8752d44 167 #endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_CMATH