diff options
author | Damien George <damien.p.george@gmail.com> | 2014-01-16 10:56:39 -0800 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-01-16 10:56:39 -0800 |
commit | eea2eb1bb74c59e80deb14872a0aa10d6a69a304 (patch) | |
tree | 77c4ec4cbede304afffe0f7a86f8e9886e8b900d | |
parent | a671f891ddb42496accb889e19a0eb14b50caba2 (diff) | |
parent | dcac88095b3a67e75204f6b245d62d481ce2d906 (diff) | |
download | micropython-eea2eb1bb74c59e80deb14872a0aa10d6a69a304.tar.gz micropython-eea2eb1bb74c59e80deb14872a0aa10d6a69a304.zip |
Merge pull request #180 from pfalcon/examples-improve
Improve compatibility of examples with CPython (+ interp compatibility too)
-rw-r--r-- | examples/conwaylife.py | 3 | ||||
-rw-r--r-- | examples/mandel.py | 6 | ||||
-rw-r--r-- | examples/micropython.py | 8 | ||||
-rw-r--r-- | examples/pyb.py | 6 | ||||
-rw-r--r-- | py/mpconfig.h | 8 | ||||
-rw-r--r-- | py/runtime.c | 7 |
6 files changed, 35 insertions, 3 deletions
diff --git a/examples/conwaylife.py b/examples/conwaylife.py index fb62ce69e8..7c6ac4d548 100644 --- a/examples/conwaylife.py +++ b/examples/conwaylife.py @@ -34,10 +34,11 @@ def conway_go(num_frames): for i in range(num_frames): conway_step() # do 1 iteration lcd.show() # update the LCD + pyb.delay(300) # PC testing import lcd import pyb lcd = lcd.LCD(128, 32) conway_rand() -conway_go(100) +conway_go(1000) diff --git a/examples/mandel.py b/examples/mandel.py index 996132a915..d2b34fff83 100644 --- a/examples/mandel.py +++ b/examples/mandel.py @@ -1,3 +1,9 @@ +try: + import micropython +except: + pass + + def mandelbrot(): # returns True if c, complex, is in the Mandelbrot set @micropython.native diff --git a/examples/micropython.py b/examples/micropython.py new file mode 100644 index 0000000000..091c92deb3 --- /dev/null +++ b/examples/micropython.py @@ -0,0 +1,8 @@ +# micropython module placeholder for CPython + +# Dummy function decorators + +def nodecor(x): + return x + +byte_code = native = viper = nodecor diff --git a/examples/pyb.py b/examples/pyb.py index 5e24f40e4a..f7b52a2a1e 100644 --- a/examples/pyb.py +++ b/examples/pyb.py @@ -1,7 +1,9 @@ -# pyboard testing functions for PC +# pyboard testing functions for CPython +import time + def delay(n): - pass + time.sleep(float(n) / 1000) rand_seed = 1 def rand(): diff --git a/py/mpconfig.h b/py/mpconfig.h index 5d8c57692e..505b1b2d18 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -85,6 +85,14 @@ typedef long long mp_longint_impl_t; #define MICROPY_ENABLE_SLICE (1) #endif +// Enable features which improve CPython compatibility +// but may lead to more code size/memory usage. +// TODO: Originally intended as generic category to not +// add bunch of once-off options. May need refactoring later +#ifndef MICROPY_CPYTHON_COMPAT +#define MICROPY_CPYTHON_COMPAT (1) +#endif + /*****************************************************************************/ /* Miscellaneous settings */ diff --git a/py/runtime.c b/py/runtime.c index e1e9e31cc7..0da3ced058 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -143,6 +143,13 @@ void rt_init(void) { mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj); mp_map_add_qstr(&map_builtins, MP_QSTR_str, (mp_obj_t)&mp_builtin_str_obj); +#if MICROPY_CPYTHON_COMPAT + // Add (empty) micropython module, so it was possible to "import micropython", + // which can be a placeholder module on CPython. + mp_obj_t m = mp_obj_new_module(qstr_from_str_static("micropython")); + rt_store_name(qstr_from_str_static("micropython"), m); +#endif + next_unique_code_id = 1; // 0 indicates "no code" unique_codes_alloc = 0; unique_codes = NULL; |