Merge branch 'version-11' into f7

This commit is contained in:
Émilie Feral
2019-05-03 17:25:53 +02:00
120 changed files with 910 additions and 890 deletions

View File

@@ -21,6 +21,7 @@ QCFG(BYTES_IN_HASH, (2))
Q(kandinsky)
Q(color)
Q(draw_string)
Q(fill_rect)
Q(get_pixel)
Q(set_pixel)

View File

@@ -1,9 +1,35 @@
extern "C" {
#include "modkandinsky.h"
#include <py/objtuple.h>
#include <py/runtime.h>
}
#include <kandinsky.h>
#include "port.h"
static KDColor ColorForTuple(mp_obj_t tuple) {
size_t len;
mp_obj_t * elem;
mp_obj_get_array(tuple, &len, &elem);
if (len != 3) {
mp_raise_TypeError("color needs 3 components");
}
return KDColor::RGB888(
mp_obj_get_int(elem[0]),
mp_obj_get_int(elem[1]),
mp_obj_get_int(elem[2])
);
}
static mp_obj_t TupleForRGB(uint8_t r, uint8_t g, uint8_t b) {
mp_obj_tuple_t * t = static_cast<mp_obj_tuple_t *>(MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)));
t->items[0] = MP_OBJ_NEW_SMALL_INT(r);
t->items[1] = MP_OBJ_NEW_SMALL_INT(g);
t->items[2] = MP_OBJ_NEW_SMALL_INT(b);
return MP_OBJ_FROM_PTR(t);
}
/* KDIonContext::sharedContext needs to be set to the wanted Rect before
* calling kandinsky_get_pixel, kandinsky_set_pixel and kandinsky_draw_string.
* We do this here with displaySandbox(), which pushes the SandboxController on
@@ -11,38 +37,51 @@ extern "C" {
* KDIonContext::sharedContext is set to the frame of the last object drawn. */
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(
mp_obj_get_int(red),
mp_obj_get_int(green),
mp_obj_get_int(blue)
)
);
return TupleForRGB(
mp_obj_get_int(red),
mp_obj_get_int(green),
mp_obj_get_int(blue)
);
}
/* Calling ExecutionEnvironment::displaySandbox() hides the console and switches
* to another mode. So it's a good idea to retrieve and handle input parameters
* before calling displaySandbox, otherwise error messages (such as TypeError)
* won't be visible until the user comes back to the console screen. */
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);
KDPoint point(mp_obj_get_int(x), mp_obj_get_int(y));
KDColor c = KDIonContext::sharedContext()->getPixel(point);
return TupleForRGB(c.red(), c.green(), c.blue());
}
mp_obj_t modkandinsky_set_pixel(mp_obj_t x, mp_obj_t y, mp_obj_t color) {
KDPoint point(mp_obj_get_int(x), mp_obj_get_int(y));
KDColor kdColor = ColorForTuple(color);
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();
KDIonContext::sharedContext()->setPixel(
KDPoint(mp_obj_get_int(x), mp_obj_get_int(y)),
KDColor::RGB16(mp_obj_get_int(color))
);
KDIonContext::sharedContext()->setPixel(point, kdColor);
return mp_const_none;
}
mp_obj_t modkandinsky_draw_string(mp_obj_t text, mp_obj_t x, mp_obj_t y) {
mp_obj_t modkandinsky_draw_string(size_t n_args, const mp_obj_t * args) {
const char * text = mp_obj_str_get_str(args[0]);
KDPoint point(mp_obj_get_int(args[1]), mp_obj_get_int(args[2]));
KDColor textColor = (n_args >= 4) ? ColorForTuple(args[3]) : KDColorBlack;
KDColor backgroundColor = (n_args >= 5) ? ColorForTuple(args[4]) : KDColorWhite;
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();
KDIonContext::sharedContext()->drawString(
mp_obj_str_get_str(text),
KDPoint(mp_obj_get_int(x), mp_obj_get_int(y))
);
KDIonContext::sharedContext()->drawString(text, point, KDFont::LargeFont, textColor, backgroundColor);
return mp_const_none;
}
mp_obj_t modkandinsky_fill_rect(size_t n_args, const mp_obj_t * args) {
KDRect rect(
mp_obj_get_int(args[0]),
mp_obj_get_int(args[1]),
mp_obj_get_int(args[2]),
mp_obj_get_int(args[3])
);
KDColor color = ColorForTuple(args[4]);
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();
KDIonContext::sharedContext()->fillRect(rect, color);
return mp_const_none;
}

View File

@@ -3,4 +3,5 @@
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);
mp_obj_t modkandinsky_draw_string(size_t n_args, const mp_obj_t *args);
mp_obj_t modkandinsky_fill_rect(size_t n_args, const mp_obj_t *args);

View File

@@ -3,7 +3,8 @@
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 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_draw_string_obj, 3, 5, modkandinsky_draw_string);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_fill_rect_obj, 5, 5, modkandinsky_fill_rect);
STATIC const mp_rom_map_elem_t modkandinsky_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_kandinsky) },
@@ -11,6 +12,7 @@ STATIC const mp_rom_map_elem_t modkandinsky_module_globals_table[] = {
{ 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 },
{ MP_ROM_QSTR(MP_QSTR_fill_rect), (mp_obj_t)&modkandinsky_fill_rect_obj },
};
STATIC MP_DEFINE_CONST_DICT(modkandinsky_module_globals, modkandinsky_module_globals_table);