From 7ccdabb4d481812ff4472833b3331c3528f494fa Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Wed, 5 Jul 2017 17:53:22 +0100 Subject: [PATCH] Implementing dmesg and serial store --- libs/core/linux.cpp | 46 ++++++++++++++++++++++++++++++++------------ libs/core/pxt.h | 1 - libs/core/pxtcore.h | 2 ++ libs/core/screen.cpp | 3 +++ libs/core/screen.ts | 2 +- libs/core/test.ts | 7 +++++-- 6 files changed, 45 insertions(+), 16 deletions(-) diff --git a/libs/core/linux.cpp b/libs/core/linux.cpp index cc7841d3..5808ea83 100644 --- a/libs/core/linux.cpp +++ b/libs/core/linux.cpp @@ -7,20 +7,10 @@ #include #include #include +#include #define DEVICE_EVT_ANY 0 -void dmesg(const char *format, ...) { - char buf[500]; - - va_list arg; - va_start(arg, format); - vsnprintf(buf, sizeof(buf), format, arg); - va_end(arg); - - fprintf(stderr, "DMESG: %s\n", buf); -} - void *operator new(size_t size) { return malloc(size); } @@ -41,6 +31,8 @@ static int startTime; static pthread_mutex_t execMutex; static pthread_mutex_t eventMutex; static pthread_cond_t newEventBroadcast; +static FILE *dmesgFile; +static FILE *serialFile; struct Thread { struct Thread *next; @@ -72,7 +64,15 @@ Event *mkEvent(int source, int value) { } void sendSerial(const char *data, int len) { - fwrite(data, 1, len, stderr); + if (!serialFile) { + serialFile = fopen("/tmp/serial.txt", "w"); + if (!serialFile) + serialFile = stderr; + } + + fwrite(data, 1, len, serialFile); + fflush(serialFile); + fdatasync(fileno(serialFile)); } extern "C" void target_panic(int error_code) { @@ -266,10 +266,32 @@ extern "C" void InitEV3(); void initRuntime() { startTime = currTime(); + DMESG("runtime starting..."); pthread_t disp; pthread_create(&disp, NULL, evtDispatcher, NULL); pthread_detach(disp); InitEV3(); startUser(); + DMESG("runtime started"); } + +void dmesg(const char *format, ...) { + char buf[500]; + + if (!dmesgFile) { + dmesgFile = fopen("/tmp/dmesg.txt", "w"); + if (!dmesgFile) + dmesgFile = stderr; + } + + va_list arg; + va_start(arg, format); + vsnprintf(buf, sizeof(buf), format, arg); + va_end(arg); + + fprintf(dmesgFile, "[%8d] %s\n", current_time_ms(), buf); + fflush(dmesgFile); + fdatasync(fileno(dmesgFile)); +} + } diff --git a/libs/core/pxt.h b/libs/core/pxt.h index 55a14c1b..0b5ddd26 100644 --- a/libs/core/pxt.h +++ b/libs/core/pxt.h @@ -2,7 +2,6 @@ #define __PXT_H #include "pxtbase.h" -#undef DMESG #define ID_BUTTON_BASE 100 diff --git a/libs/core/pxtcore.h b/libs/core/pxtcore.h index 402bdb25..c85c8fde 100644 --- a/libs/core/pxtcore.h +++ b/libs/core/pxtcore.h @@ -1,7 +1,9 @@ #ifndef __PXTCORE_H #define __PXTCORE_H +namespace pxt { void dmesg(const char *fmt, ...); #define DMESG dmesg +} #endif diff --git a/libs/core/screen.cpp b/libs/core/screen.cpp index 06db9837..505a0816 100644 --- a/libs/core/screen.cpp +++ b/libs/core/screen.cpp @@ -25,16 +25,19 @@ enum class ScreenFont { namespace screen { //% void _drawLine(uint32_t p0, uint32_t p1, Draw mode) { + DMESG("line %x %x %x", p0, p1, mode); LineOutEx(XX(p0), YY(p0), XX(p1), YY(p1), (uint32_t)mode); } //% void _drawRect(uint32_t p0, uint32_t p1, Draw mode) { + DMESG("rect %x %x %x", p0, p1, mode); RectOutEx(XX(p0), YY(p0), XX(p1), YY(p1), (uint32_t)mode); } //% void _drawEllipse(uint32_t p0, uint32_t p1, Draw mode) { + DMESG("ellip %x %x %x", p0, p1, mode); EllipseOutEx(XX(p0), YY(p0), XX(p1), YY(p1), (uint32_t)mode); } diff --git a/libs/core/screen.ts b/libs/core/screen.ts index 534e221e..11b89fca 100644 --- a/libs/core/screen.ts +++ b/libs/core/screen.ts @@ -9,7 +9,7 @@ namespace screen { function _drawEllipse(p0: uint32, p1: uint32, mode: Draw): void { } function pack(x: number, y: number) { - return Math.clamp(0, x, 512) | (Math.clamp(0, y, 512) << 16) + return Math.clamp(0, 512, x) | (Math.clamp(0, 512, y) << 16) } export function drawLine(x0: number, y0: number, x1: number, y1: number, mode?: Draw) { diff --git a/libs/core/test.ts b/libs/core/test.ts index 04903fbd..a287dc12 100644 --- a/libs/core/test.ts +++ b/libs/core/test.ts @@ -1,6 +1,9 @@ screen.clear() -screen.drawRect(10, 10, 20, 10) -screen.drawText(10, 30, "Hello PXT!") +screen.setFont(ScreenFont.Large) +//screen.drawText(10, 30, "Hello PXT!") +for (let i = 0; i < 10; ++i) +screen.drawRect(10, 70, 20, 10, Draw.Fill) +//screen.drawEllipse(40, 40, 20, 10) output.setLights(LightsPattern.GreenFlash)