[code/python] Fixed the Kandinsky module in Python.

User can draw on the 320*220 drawing screen.

Change-Id: I25034b05f21aacc35608358fdb7a4d9924dd22e8
This commit is contained in:
Léa Saviot
2017-11-20 11:14:08 +01:00
parent 2aa1cb0256
commit a9f4da92c0
11 changed files with 93 additions and 14 deletions

View File

@@ -21,4 +21,3 @@ const mp_obj_module_t kandinsky_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&kandinsky_module_globals,
};

View File

@@ -1,7 +1,9 @@
extern "C" {
#include "modkandinsky.h"
}
#include <escher/metric.h>
#include <kandinsky.h>
#include "port.h"
mp_obj_t kandinsky_color(mp_obj_t red, mp_obj_t green, mp_obj_t blue) {
return
@@ -16,22 +18,25 @@ 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) {
KDColor c = KDIonContext::sharedContext()->getPixel(
KDPoint(mp_obj_get_int(x), mp_obj_get_int(y))
KDPoint(mp_obj_get_int(x), mp_obj_get_int(y) + Metric::TitleBarHeight)
);
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) {
KDIonContext::sharedContext()->setPixel(
KDPoint(mp_obj_get_int(x), mp_obj_get_int(y)),
KDPoint(mp_obj_get_int(x), mp_obj_get_int(y) + Metric::TitleBarHeight),
KDColor::RGB16(mp_obj_get_int(color))
);
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->didModifyFramebuffer();
return mp_const_none;
}
mp_obj_t kandinsky_draw_string(mp_obj_t text, mp_obj_t x, mp_obj_t y) {
KDIonContext::sharedContext()->drawString(
mp_obj_str_get_str(text),
KDPoint(mp_obj_get_int(x), mp_obj_get_int(y))
KDPoint(mp_obj_get_int(x), mp_obj_get_int(y) + Metric::TitleBarHeight)
);
MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->didModifyFramebuffer();
return mp_const_none;
}

View File

@@ -1,3 +1,10 @@
#include <apps/i18n.h>
#include <escher/metric.h>
#include <ion/display.h>
#include <ion/events.h>
#include <ion/keyboard.h>
#include "port.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
@@ -16,19 +23,31 @@ extern "C" {
#include "py/stackctrl.h"
}
#include <apps/i18n.h>
#include <ion/keyboard.h>
#include "port.h"
static char * python_stack_top = NULL;
static MicroPython::ScriptProvider * sScriptProvider = nullptr;
static MicroPython::ExecutionEnvironment * sCurrentExecutionEnvironment = nullptr;
MicroPython::ExecutionEnvironment::ExecutionEnvironment() :
m_framebufferHasBeenModified(false)
{
}
MicroPython::ExecutionEnvironment * MicroPython::ExecutionEnvironment::currentExecutionEnvironment() {
return sCurrentExecutionEnvironment;
}
void MicroPython::ExecutionEnvironment::didModifyFramebuffer() {
m_framebufferHasBeenModified = true;
}
void MicroPython::ExecutionEnvironment::runCode(const char * str) {
assert(sCurrentExecutionEnvironment == nullptr);
sCurrentExecutionEnvironment = this;
KDIonContext::sharedContext()->setOrigin(KDPointZero);
KDIonContext::sharedContext()->setClippingRect(KDRect(0, Metric::TitleBarHeight, Ion::Display::Width, Ion::Display::Height - Metric::TitleBarHeight));
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
mp_lexer_t *lex = mp_lexer_new_from_str_len(0, str, strlen(str), false);
@@ -74,6 +93,15 @@ void MicroPython::ExecutionEnvironment::runCode(const char * str) {
/* End of mp_obj_print_exception. */
}
while (m_framebufferHasBeenModified) {
int timeout = 3000;
Ion::Events::Event event = Ion::Events::getEvent(&timeout);
if (event == Ion::Events::OK || event == Ion::Events::Back) {
m_framebufferHasBeenModified = false;
redraw();
}
}
assert(sCurrentExecutionEnvironment == this);
sCurrentExecutionEnvironment = nullptr;
}

View File

@@ -10,9 +10,16 @@ public:
class ExecutionEnvironment {
public:
ExecutionEnvironment();
static ExecutionEnvironment * currentExecutionEnvironment();
void didModifyFramebuffer();
void runCode(const char * );
virtual void printText(const char * text, size_t length) {
}
virtual void redraw() {
}
private:
bool m_framebufferHasBeenModified;
};
void init(void * heapStart, void * heapEnd);