Commit 9c83ec0e by John R. Lenton

### Merge remote-tracking branch 'upstream/master' into dict_feats

parent 27d4ca76
 # log the accelerometer values to a file, 1 per second f = open('motion.dat', 'w') # open the file for writing for i in range(60): # loop 60 times time = pyb.time() # get the current time accel = pyb.accel() # get the accelerometer data # write time and x,y,z values to the file f.write('{} {} {} {}\n'.format(time, accel[0], accel[1], accel[2])) pyb.delay(1000) # wait 1000 ms = 1 second f.close() # close the file
 # do 1 iteration of Conway's Game of Life def conway_step(): for x in range(128): # loop over x coordinates for y in range(32): # loop over y coordinates # count number of neigbours num_neighbours = (lcd.get(x - 1, y - 1) + lcd.get(x, y - 1) + lcd.get(x + 1, y - 1) + lcd.get(x - 1, y) + lcd.get(x + 1, y) + lcd.get(x + 1, y + 1) + lcd.get(x, y + 1) + lcd.get(x - 1, y + 1)) # check if the centre cell is alive or not self = lcd.get(x, y) # apply the rules of life if self and not (2 <= num_neighbours <= 3): lcd.reset(x, y) # not enough, or too many neighbours: cell dies elif not self and num_neighbours == 3: lcd.set(x, y) # exactly 3 neigbours around an empty cell: cell is born # randomise the start def conway_rand(): lcd.clear() # clear the LCD for x in range(128): # loop over x coordinates for y in range(32): # loop over y coordinates if pyb.rand() & 1: # get a 1-bit random number lcd.set(x, y) # set the pixel randomly # loop for a certain number of frames, doing iterations of Conway's Game of Life def conway_go(num_frames): for i in range(num_frames): conway_step() # do 1 iteration lcd.show() # update the LCD # PC testing import lcd import pyb lcd = lcd.LCD(128, 32) conway_rand() conway_go(100)
examples/lcd.py 0 → 100644
 # LCD testing object for PC # uses double buffering class LCD: def __init__(self, width, height): self.width = width self.height = height self.buf1 = [[0 for x in range(self.width)] for y in range(self.height)] self.buf2 = [[0 for x in range(self.width)] for y in range(self.height)] def clear(self): for y in range(self.height): for x in range(self.width): self.buf1[y][x] = self.buf2[y][x] = 0 def show(self): print('') # blank line to separate frames for y in range(self.height): for x in range(self.width): self.buf1[y][x] = self.buf2[y][x] for y in range(self.height): row = ''.join(['*' if self.buf1[y][x] else ' ' for x in range(self.width)]) print(row) def get(self, x, y): if 0 <= x < self.width and 0 <= y < self.height: return self.buf1[y][x] else: return 0 def reset(self, x, y): if 0 <= x < self.width and 0 <= y < self.height: self.buf2[y][x] = 0 def set(self, x, y): if 0 <= x < self.width and 0 <= y < self.height: self.buf2[y][x] = 1
 def led_angle(seconds_to_run_for): # make LED objects l1 = pyb.Led(1) l2 = pyb.Led(2) for i in range(20 * seconds_to_run_for): # get x-axis accel = pyb.accel()[0] # turn on LEDs depending on angle if accel < -10: l1.on() l2.off() elif accel > 10: l1.off() l2.on() else: l1.off() l2.off() # delay so that loop runs at at 1/50ms = 20Hz pyb.delay(50)
 @micropython.native def in_set(c): z = 0 for i in range(40): z = z*z + c if abs(z) > 60: return False return True def mandelbrot(): # returns True if c, complex, is in the Mandelbrot set @micropython.native def in_set(c): z = 0 for i in range(40): z = z*z + c if abs(z) > 60: return False return True for v in range(31): line = [] lcd.clear() for u in range(91): line.append('*' if in_set((u / 30 - 2) + (v / 15 - 1) * 1j) else ' ') print(''.join(line)) for v in range(31): if in_set((u / 30 - 2) + (v / 15 - 1) * 1j): lcd.set(u, v) lcd.show() # PC testing import lcd lcd = lcd.LCD(128, 32) mandelbrot()
examples/pyb.py 0 → 100644
 # pyboard testing functions for PC def delay(n): pass rand_seed = 1 def rand(): global rand_seed # for these choice of numbers, see P L'Ecuyer, "Tables of linear congruential generators of different sizes and good lattice structure" rand_seed = (rand_seed * 653276) % 8388593 return rand_seed
This diff is collapsed.

88.7 KB

This diff is collapsed.
 #include #include #include #include #include #include "misc.h" #include "asmx64.h" #include "mpconfig.h" // wrapper around everything in this file #if MICROPY_EMIT_X64 #include #include #include "asmx64.h" #if defined(__OpenBSD__) || defined(__MACH__) #define MAP_ANONYMOUS MAP_ANON #endif ... ...
 ... ... @@ -6,6 +6,8 @@ #include "mpconfig.h" #include "gc.h" #if MICROPY_ENABLE_GC // a machine word is big enough to hold a pointer /* #define BYTES_PER_WORD (8) ... ... @@ -380,3 +382,5 @@ int main(void) { gc_dump_at(); } */ #endif // MICROPY_ENABLE_GC
 ... ... @@ -4,8 +4,11 @@ #include #include "misc.h" #include "mpconfig.h" #include "lexer.h" #if MICROPY_ENABLE_LEXER_UNIX typedef struct _str_buf_t { bool free; // free src_beg when done const char *src_beg; // beginning of source ... ... @@ -78,3 +81,5 @@ mp_lexer_t *mp_import_open_file(qstr mod_name) { vstr_printf(vstr, "%s.py", qstr_str(mod_name)); return mp_lexer_new_from_file(vstr_str(vstr)); // TODO does lexer need to copy the string? can we free it here? } #endif // MICROPY_ENABLE_LEXER_UNIX
 ... ... @@ -5,11 +5,7 @@ /** types *******************************************************/ typedef int bool; enum { false = 0, true = 1 }; #include typedef unsigned char byte; typedef unsigned int uint; ... ...
 ... ... @@ -4,26 +4,67 @@ #include #ifndef INT_FMT // printf format spec to use for machine_int_t and friends #ifdef __LP64__ // Archs where machine_int_t == long, long != int #define UINT_FMT "%lu" #define INT_FMT "%ld" #else // Archs where machine_int_t == int #define UINT_FMT "%u" #define INT_FMT "%d" // Any options not explicitly set in mpconfigport.h will get default // values below. /*****************************************************************************/ /* Micro Python emitters */ // Whether to emit CPython byte codes (for debugging/testing) // Enabling this overrides all other emitters #ifndef MICROPY_EMIT_CPYTHON #define MICROPY_EMIT_CPYTHON (0) #endif #endif //INT_FMT // Whether to emit x64 native code #ifndef MICROPY_EMIT_X64 #define MICROPY_EMIT_X64 (0) #endif // Any options not explicitly set in mpconfigport.h will get default // values below. // Whether to emit thumb native code #ifndef MICROPY_EMIT_THUMB #define MICROPY_EMIT_THUMB (0) #endif // Whether to enable the thumb inline assembler #ifndef MICROPY_EMIT_INLINE_THUMB #define MICROPY_EMIT_INLINE_THUMB (0) #endif /*****************************************************************************/ /* Internal debugging stuff */ // Whether to collect memory allocation stats #ifndef MICROPY_MEM_STATS #define MICROPY_MEM_STATS (1) #define MICROPY_MEM_STATS (0) #endif // Whether to build code to show byte code #ifndef MICROPY_SHOW_BC #define MICROPY_SHOW_BC (0) #endif /*****************************************************************************/ /* Fine control over Python features */ // Whether to include the garbage collector #ifndef MICROPY_ENABLE_GC #define MICROPY_ENABLE_GC (0) #endif // Whether to include REPL helper function #ifndef MICROPY_ENABLE_REPL_HELPERS #define MICROPY_ENABLE_REPL_HELPERS (0) #endif // Whether to include lexer helper function for unix #ifndef MICROPY_ENABLE_LEXER_UNIX #define MICROPY_ENABLE_LEXER_UNIX (0) #endif // Whether to support float and complex types #ifndef MICROPY_ENABLE_FLOAT #define MICROPY_ENABLE_FLOAT (0) #endif // Whether to support slice object and correspondingly ... ... @@ -31,3 +72,19 @@ #ifndef MICROPY_ENABLE_SLICE #define MICROPY_ENABLE_SLICE (1) #endif /*****************************************************************************/ /* Miscellaneous settings */ // printf format spec to use for machine_int_t and friends #ifndef INT_FMT #ifdef __LP64__ // Archs where machine_int_t == long, long != int #define UINT_FMT "%lu" #define INT_FMT "%ld" #else // Archs where machine_int_t == int #define UINT_FMT "%u" #define INT_FMT "%d" #endif #endif //INT_FMT
 ... ... @@ -30,6 +30,7 @@ Q(NameError) Q(SyntaxError) Q(TypeError) Q(ValueError) Q(OSError) Q(abs) Q(all) ... ...
 ... ... @@ -15,15 +15,14 @@ typedef machine_int_t mp_small_int_t; typedef machine_float_t mp_float_t; #endif // Anything that wants to be a Micro Python object must // have mp_obj_base_t as its first member (except NULL and small ints) typedef struct _mp_obj_base_t mp_obj_base_t; typedef struct _mp_obj_type_t mp_obj_type_t; // Anything that wants to be a Micro Python object must have // mp_obj_base_t as its first member (except NULL and small ints) struct _mp_obj_type_t; struct _mp_obj_base_t { const mp_obj_type_t *type; const struct _mp_obj_type_t *type; }; typedef struct _mp_obj_base_t mp_obj_base_t; // The NULL object is used to indicate the absence of an object // It *cannot* be used when an mp_obj_t is expected, except where explicitly allowed ... ... @@ -43,12 +42,17 @@ struct _mp_obj_base_t { #define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_native_t obj_name #define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 0, 0, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 1, 1, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 2, 2, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, 3, 3, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, (~((machine_uint_t)0)), fun_name} #define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, n_args_max, fun_name} #define MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, is_kw, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, is_kw, n_args_min, n_args_max, (void *)fun_name} #define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 0, 0, (mp_fun_0_t)fun_name) #define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 1, 1, (mp_fun_1_t)fun_name) #define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 2, 2, (mp_fun_2_t)fun_name) #define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 3, 3, (mp_fun_3_t)fun_name) #define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, (~((machine_uint_t)0)), (mp_fun_var_t)fun_name) #define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, n_args_max, (mp_fun_var_t)fun_name) #define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, true, 0, (~((machine_uint_t)0)), (mp_fun_var_t)fun_name) // Need to declare this here so we are not dependent on map.h struct _mp_map_t; // Type definitions for methods ... ... @@ -58,10 +62,12 @@ typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t); typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t); typedef mp_obj_t (*mp_fun_t)(void); typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *); typedef mp_obj_t (*mp_fun_kw_t)(mp_obj_t*, struct _mp_map_t*); typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o); typedef mp_obj_t (*mp_make_new_fun_t)(mp_obj_t type_in, int n_args, const mp_obj_t *args); // args are in reverse order in the array typedef mp_obj_t (*mp_call_n_fun_t)(mp_obj_t fun, int n_args, const mp_obj_t *args); // args are in reverse order in the array typedef mp_obj_t (*mp_call_n_kw_fun_t)(mp_obj_t fun, int n_args, int n_kw, const mp_obj_t *args); // args are in reverse order in the array typedef mp_obj_t (*mp_unary_op_fun_t)(int op, mp_obj_t); typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t); ... ... @@ -77,13 +83,14 @@ struct _mp_obj_type_t { mp_make_new_fun_t make_new; // to make an instance of the type mp_call_n_fun_t call_n; mp_call_n_kw_fun_t call_n_kw; mp_unary_op_fun_t unary_op; // can return NULL if op not supported mp_binary_op_fun_t binary_op; // can return NULL if op not supported mp_fun_1_t getiter; mp_fun_1_t iternext; const mp_method_t methods[]; const mp_method_t *methods; /* What we might need to add here: ... ... @@ -108,6 +115,8 @@ struct _mp_obj_type_t { */ }; typedef struct _mp_obj_type_t mp_obj_type_t; // Constant objects, globally accessible extern const mp_obj_type_t mp_const_type; ... ... @@ -118,10 +127,6 @@ extern const mp_obj_t mp_const_empty_tuple; extern const mp_obj_t mp_const_ellipsis; extern const mp_obj_t mp_const_stop_iteration; // special object indicating end of iteration (not StopIteration exception!) // Need to declare this here so we are not dependent on map.h struct _mp_map_t; // General API for objects mp_obj_t mp_obj_new_none(void); ... ... @@ -144,8 +149,8 @@ mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun); mp_obj_t mp_obj_new_gen_wrap(uint n_locals, uint n_stack, mp_obj_t fun); mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_state, int n_args, const mp_obj_t *args); mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple); mp_obj_t mp_obj_new_tuple(uint n, mp_obj_t *items); mp_obj_t mp_obj_new_tuple_reverse(uint n, mp_obj_t *items); mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items); mp_obj_t mp_obj_new_tuple_reverse(uint n, const mp_obj_t *items); mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items); mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items); mp_obj_t mp_obj_new_dict(int n_args); ... ... @@ -234,13 +239,15 @@ void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *sto // functions typedef struct _mp_obj_fun_native_t { // need this so we can define const objects (to go in ROM) mp_obj_base_t base; machine_uint_t n_args_min; // inclusive bool is_kw : 1; machine_uint_t n_args_min : (sizeof(machine_uint_t) - 1); // inclusive machine_uint_t n_args_max; // inclusive void *fun; // TODO add mp_map_t *globals // for const function objects, make an empty, const map // such functions won't be able to access the global scope, but that's probably okay } mp_obj_fun_native_t; extern const mp_obj_type_t fun_native_type; extern const mp_obj_type_t fun_bc_type; void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, uint *n_state, const byte **code); ... ...
 ... ... @@ -34,14 +34,8 @@ static mp_obj_t bool_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args const mp_obj_type_t bool_type = { { &mp_const_type }, "bool", bool_print, // print bool_make_new, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op NULL, // getiter NULL, // iternext .methods = {{NULL, NULL},}, .print = bool_print, .make_new = bool_make_new, }; static const mp_obj_bool_t false_obj = {{&bool_type}, false}; ... ...
 ... ... @@ -36,14 +36,7 @@ mp_obj_t bound_meth_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { const mp_obj_type_t bound_meth_type = { { &mp_const_type }, "bound_method", NULL, // print NULL, // make_new bound_meth_call_n, // call_n NULL, // unary_op NULL, // binary_op NULL, // getiter NULL, // iternext .methods = {{NULL, NULL},}, .call_n = bound_meth_call_n, }; mp_obj_t mp_obj_new_bound_meth(mp_obj_t self, mp_obj_t meth) { ... ...
 ... ... @@ -26,14 +26,6 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) { const mp_obj_type_t cell_type = { { &mp_const_type }, "cell", NULL, // print NULL, // make_new NULL, // call_n NULL, // unary_op NULL, // binary_op NULL, // getiter NULL, // iternext .methods = {{NULL, NULL},}, }; mp_obj_t mp_obj_new_cell(mp_obj_t obj) { ... ...
 ... ... @@ -63,14 +63,7 @@ mp_map_t *mp_obj_class_get_locals(mp_obj_t self_in) { const mp_obj_type_t class_type = { { &mp_const_type }, "class", NULL, // print NULL, // make_new class_call_n, // call_n NULL, // unary_op NULL, // binary_op NULL, // getiter NULL, // iternext .methods = {{NULL, NULL},}, .call_n = class_call_n, }; mp_obj_t mp_obj_new_class(mp_map_t *class_locals) { ... ...
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment