From ea52e627afa6be190c4f1022af039567eafe431e Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Sun, 30 Jul 2017 22:18:57 +0200 Subject: [PATCH] [python] Add a (commented-out) example app Change-Id: I3b00052bf7a116b4b6e2a785931e6a265cc6031c --- Makefile | 1 + apps/main.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/Makefile b/Makefile index b88135524..5ccab8c3e 100644 --- a/Makefile +++ b/Makefile @@ -69,6 +69,7 @@ endif include ion/Makefile include kandinsky/Makefile include poincare/Makefile +#include python/Makefile include escher/Makefile # Executable Makefiles include apps/Makefile diff --git a/apps/main.cpp b/apps/main.cpp index 61e58bcfb..ff7f450dd 100644 --- a/apps/main.cpp +++ b/apps/main.cpp @@ -1,7 +1,72 @@ #include "apps_container.h" +#define PYTHON_TEST 0 +extern "C" { +#include +#if PYTHON_TEST +#include "py/compile.h" +#include "py/runtime.h" +#include "py/gc.h" +#include "py/stackctrl.h" +#endif +} AppsContainer container; +#if PYTHON_TEST +mp_obj_t execute_from_str(const char *str) { + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + mp_lexer_t *lex = mp_lexer_new_from_str_len(0/*MP_QSTR_*/, str, strlen(str), false); + mp_parse_tree_t pt = mp_parse(lex, MP_PARSE_FILE_INPUT); + mp_obj_t module_fun = mp_compile(&pt, lex->source_name, MP_EMIT_OPT_NONE, false); + mp_call_function_0(module_fun); + nlr_pop(); + return 0; + } else { + // uncaught exception + return (mp_obj_t)nlr.ret_val; + } +} + +extern "C" +void mp_hal_stdout_tx_strn_cooked(const char * str, size_t len) { + KDContext * ctx = KDIonContext::sharedContext(); + ctx->drawString(str, KDPoint(0, 0)); +} +#endif + void ion_app() { +#if PYTHON_TEST + + // Initialized stack limit + mp_stack_set_limit(40000); + + char * pythonHeap = (char *)malloc(16384); + + gc_init(pythonHeap, pythonHeap + 16384); + + // Initialize interpreter + mp_init(); + + const char str[] = +"import kandinsky\n" +"red = kandinsky.color(255,0,0)\n" +"blue = kandinsky.color(0,0,255)\n" +"for i in range(100):\n" +" for j in range(100):\n" +" if ((i+j)%2 == 0):\n" +" kandinsky.set_pixel(i, j, red)\n" +" else:\n" +" kandinsky.set_pixel(i, j, blue)\n" +; + + if (execute_from_str(str)) { + mp_hal_stdout_tx_strn_cooked("Error", 0); + } + + while (1) { + } +#endif + container.run(); }