[apps/code] Repare print() in micropython

Change-Id: I43c46748b15d83e071c7b7f1e8448384ce5ad77f
This commit is contained in:
Émilie Feral
2017-08-17 15:33:53 +02:00
parent 3184bc2276
commit b50296d9c2
5 changed files with 48 additions and 7 deletions

View File

@@ -6,4 +6,7 @@ app_objs += $(addprefix apps/code/,\
program.o\
)
tests += $(addprefix apps/code/test/,\
mpprint.cpp\
)
app_images += apps/code/code_icon.png

View File

@@ -25,21 +25,47 @@ mp_obj_t execute_from_str(const char *str) {
}
}
/* mp_hal_stdout_tx_strn_cooked symbol required by micropython at printing
* needs to access information about where to print (depending on the strings
* printed before). This 'context' is provided by the global sCurrentView that
* points to the content view only within the drawRect method (as runPython is
* called within drawRect). */
static const ExecutorController::ContentView * sCurrentView = nullptr;
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));
assert(sCurrentView != nullptr);
sCurrentView->print(str);
}
ExecutorController::ContentView::ContentView(Program * program) :
View(),
m_program(program)
m_program(program),
m_printLocation(KDPointZero)
{
}
void ExecutorController::ContentView::drawRect(KDContext * ctx, KDRect rect) const {
ctx->fillRect(bounds(), KDColorWhite);
assert(ctx == KDIonContext::sharedContext());
clearScreen(ctx);
assert(sCurrentView == nullptr);
sCurrentView = this;
// Reinitialize the print location
m_printLocation = KDPointZero;
runPython();
sCurrentView = nullptr;
}
void ExecutorController::ContentView::print(const char * str) const {
KDContext * ctx = KDIonContext::sharedContext();
m_printLocation = ctx->drawString(str, m_printLocation);
if (bounds().height() < m_printLocation.y()) {
clearScreen(ctx);
m_printLocation = KDPoint(m_printLocation.x(), 0);
}
}
void ExecutorController::ContentView::runPython() const {
@@ -60,6 +86,10 @@ void ExecutorController::ContentView::runPython() const {
free(pythonHeap);
}
void ExecutorController::ContentView::clearScreen(KDContext * ctx) const {
ctx->fillRect(bounds(), KDColorWhite);
}
ExecutorController::ExecutorController(Program * program) :
ViewController(nullptr),
m_view(program)

View File

@@ -11,15 +11,18 @@ public:
ExecutorController(Program * program);
View * view() override;
bool handleEvent(Ion::Events::Event event) override;
private:
class ContentView : public View {
public:
ContentView(Program * program);
void drawRect(KDContext * ctx, KDRect rect) const override;
void print(const char * str) const;
private:
void runPython() const;
void clearScreen(KDContext * ctx) const;
Program * m_program;
mutable KDPoint m_printLocation;
};
private:
ContentView m_view;
};

View File

@@ -0,0 +1,6 @@
#include <stdlib.h>
extern "C"
void mp_hal_stdout_tx_strn_cooked(const char * str, size_t len) {
}

View File

@@ -72,8 +72,7 @@ typedef long mp_off_t;
#define SEEK_CUR 1
//#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)
#define MP_PLAT_PRINT_STRN(str, len) ((void)0)
#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)
// extra built in names to add to the global namespace
#define MICROPY_PORT_BUILTINS \