diff --git a/CMakeLists.txt b/CMakeLists.txt index d7b375118..3fc6b45da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -109,7 +109,7 @@ add_custom_target( ) # Emulator -file(GLOB_RECURSE SOURCES_OSW ./src/*.cpp) +file(GLOB_RECURSE SOURCES_OSW ./src/*.cpp ./src/swig/*.cxx) file(GLOB_RECURSE SOURCES_OSW_EMULATOR ./emulator/src/*.cpp) add_executable(emulator.run ${SOURCES_OSW} @@ -158,6 +158,8 @@ target_compile_definitions(emulator.run PUBLIC # Comment these as you wish... OSW_FEATURE_STATS_STEPS OSW_FEATURE_WEATHER + OSW_FEATURE_LUA + EXAMPLE_LUA OSW_SERVICE_CONSOLE OSW_APPS_EXAMPLES GAME_SNAKE=1 @@ -178,7 +180,7 @@ target_compile_options(emulator.run PUBLIC -Wno-deprecated-declarations > $<$: - -O4 + -O2 > ) diff --git a/emulator/include/Serial.h b/emulator/include/Serial.h index 20c88810c..63db504b9 100644 --- a/emulator/include/Serial.h +++ b/emulator/include/Serial.h @@ -46,6 +46,9 @@ class Serial_t { void println(); void flush() {} + + void printf(const char *format, ...); + private: std::list inputBuffer; int bauds = 0; diff --git a/emulator/src/Serial.cpp b/emulator/src/Serial.cpp index fae25e22d..fd04d98a3 100644 --- a/emulator/src/Serial.cpp +++ b/emulator/src/Serial.cpp @@ -38,6 +38,18 @@ void Serial_t::println() { std::cout << std::endl; } +// programmer misses printf... +void Serial_t::printf(const char *format, ...) +{ + char buffer[256]; // or smaller or static &c. + va_list args; + va_start(args, format); + vsprintf(buffer, format, args); + va_end(args); + Serial.print(buffer); +} + + int Serial_t::available() { char c; if(::read(STDIN_FILENO, &c, 1) > 0) { @@ -53,4 +65,4 @@ int Serial_t::read() { char c = this->inputBuffer.front(); this->inputBuffer.pop_front(); return c; -} \ No newline at end of file +} diff --git a/platformio.ini b/platformio.ini index ecbd86d84..fba792147 100755 --- a/platformio.ini +++ b/platformio.ini @@ -39,6 +39,7 @@ extra_scripts = pre:scripts/build/prebuild_cppflags.py pre:scripts/build/prebuild_lua.py ; Needed to generate the .cxx file(s), enabled via "OSW_FEATURE_LUA" build flag build_unflags = -std=gnu++11 # The correct flag will be set by the cppflags python script... +; [env:LIGHT_EDITION_V3_3_LUA] @@ -48,10 +49,9 @@ build_flags = -D OSW_TARGET_PLATFORM_HEADER='"platform/LIGHT_EDITION_V3_3.h"' -D OSW_FEATURE_STATS_STEPS -D OSW_FEATURE_WIFI -; -D OSW_FEATURE_LUA + -D OSW_FEATURE_LUA + -D MAKE_LIB -Wdouble-promotion - -ffunction-sections - -fdata-sections -Wno-deprecated-declarations -fpermissive @@ -66,6 +66,7 @@ build_flags = -D OSW_SERVICE_CONSOLE -D OSW_FEATURE_WIFI build_type = debug +lib_ignore = LUA [env:LIGHT_EDITION_V4_0] build_flags = @@ -75,6 +76,7 @@ build_flags = -D OSW_FEATURE_WIFI -D OSW_FEATURE_WIFI_ONBOOT build_type = debug +lib_ignore = LUA ; Light edition other stuff [env:LIGHT_EDITION_DEV_LUA] @@ -109,6 +111,7 @@ build_flags = -D OSW_FEATURE_WIFI_APST -D OSW_FEATURE_WIFI_ONBOOT build_type = debug +lib_ignore = LUA ; GPS edition other stuff [env:GPS_EDITION_DEV_ROTATED] @@ -124,3 +127,4 @@ build_flags = -D OSW_FEATURE_WIFI_APST -D OSW_FEATURE_WIFI_ONBOOT build_type = debug +lib_ignore = LUA diff --git a/scripts/build/prebuild_lua.py b/scripts/build/prebuild_lua.py index 036b5fc8d..39852e70c 100644 --- a/scripts/build/prebuild_lua.py +++ b/scripts/build/prebuild_lua.py @@ -70,7 +70,7 @@ def getSwigPath(): # And compile luac for compressing lua chunks print("Building luac ...") ## return_code = os.system("cd ./lib/LUA/; gcc -O2 -std=c89 -DLUA_USE_C89 -DMAKE_LUAC -o ../../build/luac onelua.c -lm") - return_code = os.system("cd ./lib/LUA/; make luac; cp luac ../../build/luac") + return_code = os.system("cd ./lib/LUA/; make luac") if return_code != 0: print("Building lua compiler (luac) failed with return code:") print(return_code) @@ -79,7 +79,7 @@ def getSwigPath(): print("Building lua compiler (luac) successful") print("Compiling lua bytecode with luac ...") - os.system("cd src.lua; for i in $(find . -iname '*.lua'); do echo compiling $i; ../build/luac -s -o ../data/lua/$i $i; done") + os.system("cd src.lua; for i in $(find . -iname '*.lua'); do echo compiling $i; ./lib/LUA/luac -s -o ../data/lua/$i $i; done") else: print("Skipping building osw_wrap.cxx with swig because OSW_FEATURE_LUA is not defined") diff --git a/src/apps/watchfaces/OswAppWatchfaceZwilight.cpp b/src/apps/watchfaces/OswAppWatchfaceZwilight.cpp index b8277529e..829d23a23 100644 --- a/src/apps/watchfaces/OswAppWatchfaceZwilight.cpp +++ b/src/apps/watchfaces/OswAppWatchfaceZwilight.cpp @@ -78,7 +78,7 @@ bool OswAppWatchfaceZwilight::getTwilights() { lua_pushboolean(luaState, false); // true if (lua_pcall(luaState, 8, 0, 0)) { - printf("xxxxx8\n"); +// Serial.printf("xxxxx8\n"); printLuaError(); cleanupLuaState(); return false; @@ -88,7 +88,7 @@ bool OswAppWatchfaceZwilight::getTwilights() { lua_getglobal(luaState, "getTimes"); lua_pushnumber(luaState, i); if (lua_pcall(luaState, 1, 1, 0)) { - printf("xxxx33333333 knock out\n"); +// Serial.printf("xxxx33333333 knock out\n"); printLuaError(); cleanupLuaState(); return false; @@ -103,7 +103,7 @@ bool OswAppWatchfaceZwilight::getTwilights() { for (int i = 1; i <=11; ++i) { if ( lua_geti(luaState, -1, i) == LUA_TNUMBER) { // Times sunTimes[i] = lua_tonumber(luaState, -1); - printf("xxxxx SunTime.times[%d]=%f\n", i, sunTimes[i]); + Serial.printf("xxxxx SunTime.times[%d]=%f\n", i, sunTimes[i]); } else { sunTimes[i] = -10000; } @@ -117,7 +117,7 @@ bool OswAppWatchfaceZwilight::getTwilights() { } #else - #error "LUA not defined" + #warning "LUA not defined" #endif return true; @@ -186,7 +186,7 @@ void OswAppWatchfaceZwilight::drawWatch(int32_t year, int32_t month, int32_t day day_indicator_angle -= 360; while (day_indicator_angle < 0) day_indicator_angle += 360; - hal->gfx()->drawRing(CENTER_X + 115*cosf_tlu(day_indicator_angle), CENTER_Y - 115*sinf_tlu(day_indicator_angle), + hal->gfx()->drawRing(CENTER_X + 114*cosf_tlu(day_indicator_angle), CENTER_Y - 114*sinf_tlu(day_indicator_angle), 5, 2, sun_color, true); // sunTimes @@ -200,7 +200,7 @@ void OswAppWatchfaceZwilight::drawWatch(int32_t year, int32_t month, int32_t day day_indicator_angle += 360; int16_t x0, y0, x1, y1; - int16_t r1 = 120; + int16_t r1 = 119; int16_t r2 = 105; x0 = CENTER_X + r1*cosf_tlu(day_indicator_angle); y0 = CENTER_Y - r1*sinf_tlu(day_indicator_angle); @@ -351,7 +351,6 @@ void OswAppWatchfaceZwilight::onButton(Button id, bool up, OswAppV2::ButtonState return; // if the button was handled by the defaults, we are done here } - void OswAppWatchfaceZwilight::onStop() { OswAppV2::onStop(); diff --git a/src/osw_lua.cpp b/src/osw_lua.cpp index af4a5bac9..bc6cedb56 100644 --- a/src/osw_lua.cpp +++ b/src/osw_lua.cpp @@ -29,8 +29,10 @@ #include "lprefix.h" - #include +#ifdef OSW_EMULATOR +#include +#endif extern "C" { #include "lua.h" @@ -51,7 +53,7 @@ static const luaL_Reg loadedlibs[] = { {LUA_COLIBNAME, luaopen_coroutine}, {LUA_TABLIBNAME, luaopen_table}, {LUA_IOLIBNAME, luaopen_io}, -// {LUA_OSLIBNAME, luaopen_os}, + {LUA_OSLIBNAME, luaopen_os}, {LUA_STRLIBNAME, luaopen_string}, {LUA_MATHLIBNAME, luaopen_math}, {LUA_UTF8LIBNAME, luaopen_utf8}, @@ -63,7 +65,6 @@ static const luaL_Reg loadedlibs[] = { {NULL, NULL} }; -/* LUALIB_API void luaL_openlibs (lua_State* L) { const luaL_Reg* lib; // "require" functions from 'loadedlibs' and set results to global table @@ -72,7 +73,6 @@ LUALIB_API void luaL_openlibs (lua_State* L) { lua_pop(L, 1); // remove lib } } -*/ void printLuaStack(lua_State *L) { int top = lua_gettop(L); @@ -85,22 +85,29 @@ void printLuaStack(lua_State *L) { { // booleans case LUA_TBOOLEAN: + if (lua_toboolean(L, index)) + Serial.println("true"); + else + Serial.println("false"); Serial.printf("%s\n", lua_toboolean(L, index) ? "true" : "false"); break; // numbers case LUA_TNUMBER: - Serial.printf("%f\n", lua_tonumber(L, index)); + Serial.println(lua_tonumber(L, index)); +// Serial.printf("%f\n", lua_tonumber(L, index)); break; // strings case LUA_TSTRING: - Serial.printf("%s\n", lua_tostring(L, index)); + Serial.println(lua_tostring(L, index)); +// Serial.printf("%s\n", lua_tostring(L, index)); break; // other default: - Serial.printf("%s\n", lua_typename(L, type)); + Serial.println(lua_typename(L, type)); +// Serial.printf("%s\n", lua_typename(L, type)); break; } } diff --git a/src/swig/osw.i b/src/swig/osw.i index df512d740..904240346 100644 --- a/src/swig/osw.i +++ b/src/swig/osw.i @@ -58,5 +58,11 @@ void halToLua(lua_State *L) { %include %include "stdint.i" -%include "Print.h" +%{ +#ifdef OSW_EMULATOR +#include "FakePrint.h" +#else +#include "Print.h" +#endif +%} %include "osw_hal.h"