Merge pull request #180 from pfalcon/examples-improve

Improve compatibility of examples with CPython (+ interp compatibility too)
This commit is contained in:
Damien George 2014-01-16 10:56:39 -08:00
commit eea2eb1bb7
6 changed files with 35 additions and 3 deletions

View File

@ -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)

View File

@ -1,3 +1,9 @@
try:
import micropython
except:
pass
def mandelbrot():
# returns True if c, complex, is in the Mandelbrot set
@micropython.native

8
examples/micropython.py Normal file
View File

@ -0,0 +1,8 @@
# micropython module placeholder for CPython
# Dummy function decorators
def nodecor(x):
return x
byte_code = native = viper = nodecor

View File

@ -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():

View File

@ -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 */

View File

@ -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;