Skip to content

Commit eea2eb1

Browse files
committed
Merge pull request adafruit#180 from pfalcon/examples-improve
Improve compatibility of examples with CPython (+ interp compatibility too)
2 parents a671f89 + dcac880 commit eea2eb1

6 files changed

Lines changed: 35 additions & 3 deletions

File tree

examples/conwaylife.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ def conway_go(num_frames):
3434
for i in range(num_frames):
3535
conway_step() # do 1 iteration
3636
lcd.show() # update the LCD
37+
pyb.delay(300)
3738

3839
# PC testing
3940
import lcd
4041
import pyb
4142
lcd = lcd.LCD(128, 32)
4243
conway_rand()
43-
conway_go(100)
44+
conway_go(1000)

examples/mandel.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
try:
2+
import micropython
3+
except:
4+
pass
5+
6+
17
def mandelbrot():
28
# returns True if c, complex, is in the Mandelbrot set
39
@micropython.native

examples/micropython.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# micropython module placeholder for CPython
2+
3+
# Dummy function decorators
4+
5+
def nodecor(x):
6+
return x
7+
8+
byte_code = native = viper = nodecor

examples/pyb.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# pyboard testing functions for PC
1+
# pyboard testing functions for CPython
2+
import time
3+
24

35
def delay(n):
4-
pass
6+
time.sleep(float(n) / 1000)
57

68
rand_seed = 1
79
def rand():

py/mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ typedef long long mp_longint_impl_t;
8585
#define MICROPY_ENABLE_SLICE (1)
8686
#endif
8787

88+
// Enable features which improve CPython compatibility
89+
// but may lead to more code size/memory usage.
90+
// TODO: Originally intended as generic category to not
91+
// add bunch of once-off options. May need refactoring later
92+
#ifndef MICROPY_CPYTHON_COMPAT
93+
#define MICROPY_CPYTHON_COMPAT (1)
94+
#endif
95+
8896
/*****************************************************************************/
8997
/* Miscellaneous settings */
9098

py/runtime.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ void rt_init(void) {
143143
mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj);
144144
mp_map_add_qstr(&map_builtins, MP_QSTR_str, (mp_obj_t)&mp_builtin_str_obj);
145145

146+
#if MICROPY_CPYTHON_COMPAT
147+
// Add (empty) micropython module, so it was possible to "import micropython",
148+
// which can be a placeholder module on CPython.
149+
mp_obj_t m = mp_obj_new_module(qstr_from_str_static("micropython"));
150+
rt_store_name(qstr_from_str_static("micropython"), m);
151+
#endif
152+
146153
next_unique_code_id = 1; // 0 indicates "no code"
147154
unique_codes_alloc = 0;
148155
unique_codes = NULL;

0 commit comments

Comments
 (0)