Merge branch 'master' into python_turtle

This commit is contained in:
Romain Goyet
2018-11-16 09:46:11 +01:00
75 changed files with 628 additions and 249 deletions

View File

@@ -129,10 +129,12 @@ port_objs += $(addprefix python/port/,\
port.o \
builtins.o\
helpers.o \
modkandinsky.o \
modkandinsky_impl.o \
modturtle.o \
modturtle_impl.o \
mod/kandinsky/modkandinsky.o \
mod/kandinsky/modkandinsky_table.o \
mod/time/modtime.o \
mod/time/modtime_table.o \
mphalport.o \
)

View File

@@ -65,6 +65,11 @@ Q(isvisible)
Q(pencolor)
Q(reset)
// utime QSTRs
Q(time)
Q(sleep)
Q(monotonic)
// MicroPython QSTRs
Q()
Q(*)

View File

@@ -4,16 +4,35 @@ extern "C" {
#include "mphalport.h"
}
void micropython_port_should_interrupt() {
void micropython_port_vm_hook_loop() {
/* This function is called very frequently by the MicroPython engine. We grab
* this opportunity to interrupt execution and/or refresh the display on
* platforms that need it. */
/* Doing too many things here slows down Python execution quite a lot. So we
* only do things once in a while and return as soon as possible otherwise. */
static int c = 0;
c++;
if (c%20000 != 0) {
c = (c + 1) % 20000;
if (c != 0) {
return;
}
c = 0;
Ion::Keyboard::State scan = Ion::Keyboard::scan();
if (scan.keyDown((Ion::Keyboard::Key)mp_interrupt_char)) {
/* Check if the user asked for an interruption from the keyboard */
if (micropython_port_should_interrupt()) {
mp_keyboard_interrupt();
}
}
bool micropython_port_should_interrupt() {
Ion::Keyboard::State scan = Ion::Keyboard::scan();
Ion::Keyboard::Key interruptKey = static_cast<Ion::Keyboard::Key>(mp_interrupt_char);
return scan.keyDown(interruptKey);
}
void micropython_port_interruptible_msleep(uint32_t delay) {
uint32_t start = Ion::Timing::millis();
while (Ion::Timing::millis() - start < delay && !micropython_port_should_interrupt()) {
Ion::Timing::msleep(1);
}
}

View File

@@ -5,9 +5,12 @@
extern "C" {
#endif
/* should_interrupt effectively does something once every 20000 calls. It checks
* if a key is down to raise an interruption flag. */
void micropython_port_should_interrupt();
#include <stdbool.h>
#include <stdint.h>
void micropython_port_vm_hook_loop();
bool micropython_port_should_interrupt();
void micropython_port_interruptible_msleep(uint32_t delay);
#ifdef __cplusplus
}

View File

@@ -10,7 +10,7 @@ extern "C" {
* the stackViewController and forces the window to redraw itself.
* KDIonContext::sharedContext is set to the frame of the last object drawn. */
mp_obj_t kandinsky_color(mp_obj_t red, mp_obj_t green, mp_obj_t blue) {
mp_obj_t modkandinsky_color(mp_obj_t red, mp_obj_t green, mp_obj_t blue) {
return
MP_OBJ_NEW_SMALL_INT(
KDColor::RGB888(
@@ -21,14 +21,14 @@ mp_obj_t kandinsky_color(mp_obj_t red, mp_obj_t green, mp_obj_t blue) {
);
}
mp_obj_t kandinsky_get_pixel(mp_obj_t x, mp_obj_t y) {
mp_obj_t modkandinsky_get_pixel(mp_obj_t x, mp_obj_t y) {
KDColor c = KDIonContext::sharedContext()->getPixel(
KDPoint(mp_obj_get_int(x), mp_obj_get_int(y))
);
return MP_OBJ_NEW_SMALL_INT(c);
}
mp_obj_t kandinsky_set_pixel(mp_obj_t x, mp_obj_t y, mp_obj_t color) {
mp_obj_t modkandinsky_set_pixel(mp_obj_t x, mp_obj_t y, mp_obj_t color) {
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();
KDIonContext::sharedContext()->setPixel(
KDPoint(mp_obj_get_int(x), mp_obj_get_int(y)),
@@ -37,7 +37,7 @@ mp_obj_t kandinsky_set_pixel(mp_obj_t x, mp_obj_t y, mp_obj_t color) {
return mp_const_none;
}
mp_obj_t kandinsky_draw_string(mp_obj_t text, mp_obj_t x, mp_obj_t y) {
mp_obj_t modkandinsky_draw_string(mp_obj_t text, mp_obj_t x, mp_obj_t y) {
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();
KDIonContext::sharedContext()->drawString(
mp_obj_str_get_str(text),

View File

@@ -0,0 +1,6 @@
#include <py/obj.h>
mp_obj_t modkandinsky_color(mp_obj_t red, mp_obj_t green, mp_obj_t blue);
mp_obj_t modkandinsky_get_pixel(mp_obj_t x, mp_obj_t y);
mp_obj_t modkandinsky_set_pixel(mp_obj_t x, mp_obj_t y, mp_obj_t color);
mp_obj_t modkandinsky_draw_string(mp_obj_t text, mp_obj_t x, mp_obj_t y);

View File

@@ -0,0 +1,21 @@
#include "modkandinsky.h"
STATIC MP_DEFINE_CONST_FUN_OBJ_3(modkandinsky_color_obj, modkandinsky_color);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(modkandinsky_get_pixel_obj, modkandinsky_get_pixel);
STATIC MP_DEFINE_CONST_FUN_OBJ_3(modkandinsky_set_pixel_obj, modkandinsky_set_pixel);
STATIC MP_DEFINE_CONST_FUN_OBJ_3(modkandinsky_draw_string_obj, modkandinsky_draw_string);
STATIC const mp_rom_map_elem_t modkandinsky_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_kandinsky) },
{ MP_ROM_QSTR(MP_QSTR_color), (mp_obj_t)&modkandinsky_color_obj },
{ MP_ROM_QSTR(MP_QSTR_get_pixel), (mp_obj_t)&modkandinsky_get_pixel_obj },
{ MP_ROM_QSTR(MP_QSTR_set_pixel), (mp_obj_t)&modkandinsky_set_pixel_obj },
{ MP_ROM_QSTR(MP_QSTR_draw_string), (mp_obj_t)&modkandinsky_draw_string_obj },
};
STATIC MP_DEFINE_CONST_DICT(modkandinsky_module_globals, modkandinsky_module_globals_table);
const mp_obj_module_t modkandinsky_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&modkandinsky_module_globals,
};

View File

@@ -0,0 +1,20 @@
extern "C" {
#include "modtime.h"
}
#include <ion/timing.h>
#include "../../helpers.h"
#include <py/smallint.h>
#include <py/runtime.h>
mp_obj_t modtime_sleep(mp_obj_t seconds_o) {
#if MICROPY_PY_BUILTINS_FLOAT
micropython_port_interruptible_msleep(1000 * mp_obj_get_float(seconds_o));
#else
micropython_port_interruptible_msleep(1000 * mp_obj_get_int(seconds_o));
#endif
return mp_const_none;
}
mp_obj_t modtime_monotonic() {
return mp_obj_new_float(Ion::Timing::millis() / 1000.0);
}

View File

@@ -0,0 +1,4 @@
#include <py/obj.h>
mp_obj_t modtime_sleep(mp_obj_t seconds_o);
mp_obj_t modtime_monotonic();

View File

@@ -0,0 +1,17 @@
#include "modtime.h"
MP_DEFINE_CONST_FUN_OBJ_1(modtime_sleep_obj, modtime_sleep);
MP_DEFINE_CONST_FUN_OBJ_0(modtime_monotonic_obj, modtime_monotonic);
STATIC const mp_rom_map_elem_t modtime_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) },
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&modtime_sleep_obj) },
{ MP_ROM_QSTR(MP_QSTR_monotonic), MP_ROM_PTR(&modtime_monotonic_obj) },
};
STATIC MP_DEFINE_CONST_DICT(modtime_module_globals, modtime_module_globals_table);
const mp_obj_module_t modtime_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&modtime_module_globals,
};

View File

@@ -1,23 +0,0 @@
#include "py/obj.h"
#include "py/mphal.h"
#include "modkandinsky.h"
STATIC MP_DEFINE_CONST_FUN_OBJ_3(kandinsky_color_obj, kandinsky_color);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(kandinsky_get_pixel_obj, kandinsky_get_pixel);
STATIC MP_DEFINE_CONST_FUN_OBJ_3(kandinsky_set_pixel_obj, kandinsky_set_pixel);
STATIC MP_DEFINE_CONST_FUN_OBJ_3(kandinsky_draw_string_obj, kandinsky_draw_string);
STATIC const mp_rom_map_elem_t kandinsky_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_kandinsky) },
{ MP_ROM_QSTR(MP_QSTR_color), (mp_obj_t)&kandinsky_color_obj },
{ MP_ROM_QSTR(MP_QSTR_get_pixel), (mp_obj_t)&kandinsky_get_pixel_obj },
{ MP_ROM_QSTR(MP_QSTR_set_pixel), (mp_obj_t)&kandinsky_set_pixel_obj },
{ MP_ROM_QSTR(MP_QSTR_draw_string), (mp_obj_t)&kandinsky_draw_string_obj },
};
STATIC MP_DEFINE_CONST_DICT(kandinsky_module_globals, kandinsky_module_globals_table);
const mp_obj_module_t kandinsky_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&kandinsky_module_globals,
};

View File

@@ -1,13 +0,0 @@
#include "py/obj.h"
/*
* kandinsky.color(12,0,233);
* kandinsky.getPixel(x, y);
* kandinsky.setPixel(x, y, color);
* kandinsky.drawString(text, x, y);
*/
mp_obj_t kandinsky_color(mp_obj_t red, mp_obj_t green, mp_obj_t blue);
mp_obj_t kandinsky_get_pixel(mp_obj_t x, mp_obj_t y);
mp_obj_t kandinsky_set_pixel(mp_obj_t x, mp_obj_t y, mp_obj_t color);
mp_obj_t kandinsky_draw_string(mp_obj_t text, mp_obj_t x, mp_obj_t y);

View File

@@ -55,7 +55,7 @@ void draw_turtle() {
if (t_mileage > 1000) {
if (t_speed > 0) {
Ion::msleep(8 * (8 - t_speed));
Ion::Timing::msleep(8 * (8 - t_speed));
t_mileage -= 1000;
}
else {

View File

@@ -90,7 +90,7 @@
// (This scheme won't work if we want to mix Thumb and normal ARM code.)
#define MICROPY_MAKE_POINTER_CALLABLE(p) (p)
#define MICROPY_VM_HOOK_LOOP micropython_port_should_interrupt();
#define MICROPY_VM_HOOK_LOOP micropython_port_vm_hook_loop();
typedef intptr_t mp_int_t; // must be pointer size
typedef uintptr_t mp_uint_t; // must be pointer size
@@ -106,9 +106,12 @@ typedef long mp_off_t;
#define MP_STATE_PORT MP_STATE_VM
extern const struct _mp_obj_module_t kandinsky_module;
extern const struct _mp_obj_module_t modkandinsky_module;
extern const struct _mp_obj_module_t modtime_module;
extern const struct _mp_obj_module_t turtle_module;
#define MICROPY_PORT_BUILTIN_MODULES \
{ MP_ROM_QSTR(MP_QSTR_kandinsky), MP_ROM_PTR(&kandinsky_module) }, \
{ MP_ROM_QSTR(MP_QSTR_turtle), MP_ROM_PTR(&turtle_module) }
{ MP_ROM_QSTR(MP_QSTR_kandinsky), MP_ROM_PTR(&modkandinsky_module) }, \
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&modtime_module) }, \
{ MP_ROM_QSTR(MP_QSTR_turtle), MP_ROM_PTR(&turtle_module) }, \