You've already forked Core2forAWS-MicroPython
mirror of
https://github.com/m5stack/Core2forAWS-MicroPython.git
synced 2026-05-20 10:30:31 -07:00
Merge remote-tracking branch 'upstream/master' into listsort. Lots of conflict fun.
Conflicts: py/obj.h py/objbool.c py/objboundmeth.c py/objcell.c py/objclass.c py/objclosure.c py/objcomplex.c py/objdict.c py/objexcept.c py/objfun.c py/objgenerator.c py/objinstance.c py/objmodule.c py/objrange.c py/objset.c py/objslice.c
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
# 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
|
||||
@@ -0,0 +1,43 @@
|
||||
# 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)
|
||||
@@ -0,0 +1,36 @@
|
||||
# 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
|
||||
@@ -0,0 +1,22 @@
|
||||
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)
|
||||
+20
-12
@@ -1,14 +1,22 @@
|
||||
@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()
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
# 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
|
||||
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 36 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 89 KiB |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 115 KiB |
+5
-3
@@ -1,16 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/mman.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "misc.h"
|
||||
#include "asmx64.h"
|
||||
#include "mpconfig.h"
|
||||
|
||||
// wrapper around everything in this file
|
||||
#if MICROPY_EMIT_X64
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#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 <fcntl.h>
|
||||
|
||||
#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 <stdbool.h>
|
||||
|
||||
typedef unsigned char byte;
|
||||
typedef unsigned int uint;
|
||||
|
||||
+73
-16
@@ -4,8 +4,80 @@
|
||||
|
||||
#include <mpconfigport.h>
|
||||
|
||||
#ifndef INT_FMT
|
||||
// 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
|
||||
|
||||
// Whether to emit x64 native code
|
||||
#ifndef MICROPY_EMIT_X64
|
||||
#define MICROPY_EMIT_X64 (0)
|
||||
#endif
|
||||
|
||||
// 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 (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
|
||||
// slice subscript operators
|
||||
#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"
|
||||
@@ -16,18 +88,3 @@
|
||||
#define INT_FMT "%d"
|
||||
#endif
|
||||
#endif //INT_FMT
|
||||
|
||||
|
||||
// Any options not explicitly set in mpconfigport.h will get default
|
||||
// values below.
|
||||
|
||||
// Whether to collect memory allocation stats
|
||||
#ifndef MICROPY_MEM_STATS
|
||||
#define MICROPY_MEM_STATS (1)
|
||||
#endif
|
||||
|
||||
// Whether to support slice object and correspondingly
|
||||
// slice subscript operators
|
||||
#ifndef MICROPY_ENABLE_SLICE
|
||||
#define MICROPY_ENABLE_SLICE (1)
|
||||
#endif
|
||||
|
||||
@@ -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,13 +42,14 @@ 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}, false, 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}, false, 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}, false, 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}, false, 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}, false, 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}, false, n_args_min, n_args_max, fun_name}
|
||||
#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, true, 0, (~((machine_uint_t)0)), 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;
|
||||
@@ -90,7 +90,7 @@ struct _mp_obj_type_t {
|
||||
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:
|
||||
@@ -115,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;
|
||||
@@ -245,6 +247,7 @@ typedef struct _mp_obj_fun_native_t { // need this so we can define const object
|
||||
// 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);
|
||||
|
||||
+2
-3
@@ -32,11 +32,10 @@ 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 = {
|
||||
.base = { &mp_const_type },
|
||||
.name = "bool",
|
||||
{ &mp_const_type },
|
||||
"bool",
|
||||
.print = bool_print,
|
||||
.make_new = bool_make_new,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
static const mp_obj_bool_t false_obj = {{&bool_type}, false};
|
||||
|
||||
+2
-3
@@ -34,10 +34,9 @@ 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 = {
|
||||
.base = { &mp_const_type },
|
||||
.name = "bound_method",
|
||||
{ &mp_const_type },
|
||||
"bound_method",
|
||||
.call_n = bound_meth_call_n,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_bound_meth(mp_obj_t self, mp_obj_t meth) {
|
||||
|
||||
+2
-3
@@ -24,9 +24,8 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) {
|
||||
}
|
||||
|
||||
const mp_obj_type_t cell_type = {
|
||||
.base = { &mp_const_type },
|
||||
.name = "cell",
|
||||
.methods = {{NULL, NULL},},
|
||||
{ &mp_const_type },
|
||||
"cell",
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_cell(mp_obj_t obj) {
|
||||
|
||||
+2
-3
@@ -61,10 +61,9 @@ mp_map_t *mp_obj_class_get_locals(mp_obj_t self_in) {
|
||||
}
|
||||
|
||||
const mp_obj_type_t class_type = {
|
||||
.base = { &mp_const_type },
|
||||
.name = "class",
|
||||
{ &mp_const_type },
|
||||
"class",
|
||||
.call_n = class_call_n,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_class(mp_map_t *class_locals) {
|
||||
|
||||
+2
-3
@@ -33,10 +33,9 @@ mp_obj_t closure_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
|
||||
}
|
||||
|
||||
const mp_obj_type_t closure_type = {
|
||||
.base = { &mp_const_type },
|
||||
.name = "closure",
|
||||
{ &mp_const_type },
|
||||
"closure",
|
||||
.call_n = closure_call_n,
|
||||
.methods = {{NULL, NULL},},
|
||||
};
|
||||
|
||||
mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple) {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user