Skip to content

Commit

Permalink
LuaLuaLua
Browse files Browse the repository at this point in the history
  • Loading branch information
zwim committed Jun 2, 2024
1 parent 6e1d31d commit fdecd17
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
url = https://github.com/BoschSensortec/BMA400-API.git
[submodule "lib/LUA"]
path = lib/LUA
url = https://github.com/zwim/lua-5.4-osw.git
url = git@github.com:zwim/lua-5.4-osw.git
branch = ModificationsToUseWithOSW
[submodule "emulator/lib/ArduinoJson"]
path = emulator/lib/ArduinoJson
Expand Down
13 changes: 11 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,14 @@ add_custom_target(
COMMENT "Generating OSW assets..."
)

execute_process(
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/build/build_luac.sh
COMMENT "Generating luac and compile src.lua/* ..."
)

# 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}
Expand All @@ -118,6 +124,7 @@ add_executable(emulator.run
add_dependencies(emulator.run
osw_script_prebuild_assets
)

target_include_directories(emulator.run PUBLIC
./emulator/include
./include
Expand Down Expand Up @@ -158,6 +165,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
Expand All @@ -178,7 +187,7 @@ target_compile_options(emulator.run PUBLIC
-Wno-deprecated-declarations
>
$<$<CONFIG:Release>:
-O4
-O2
>
)

Expand Down
3 changes: 3 additions & 0 deletions emulator/include/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Serial_t {
void println();

void flush() {}

void printf(const char *format, ...);

private:
std::list<char> inputBuffer;
int bauds = 0;
Expand Down
14 changes: 13 additions & 1 deletion emulator/src/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -53,4 +65,4 @@ int Serial_t::read() {
char c = this->inputBuffer.front();
this->inputBuffer.pop_front();
return c;
}
}
2 changes: 1 addition & 1 deletion lib/LUA
Submodule LUA updated from a3a987 to 7e5651
10 changes: 7 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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

Expand All @@ -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 =
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -124,3 +127,4 @@ build_flags =
-D OSW_FEATURE_WIFI_APST
-D OSW_FEATURE_WIFI_ONBOOT
build_type = debug
lib_ignore = LUA
41 changes: 41 additions & 0 deletions scripts/build/build_luac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#/bin/bash

echo "Generate osw_wrap.cxx"

DEFINES="-DEXAMPLE_LUA -DGAME_BRICK_BREAKER=1 -DGAME_SNAKE=1 \
-DIMGUI_ENABLE_TEST_ENGINE=1 -DIMGUI_TEST_ENGINE_ENABLE_COROUTINE_STDTHREAD_IMPL=1 \
-DOSW_APPS_EXAMPLES -DOSW_EMULATOR=1 -DOSW_FEATURE_LUA -DOSW_FEATURE_STATS_STEPS \
-DOSW_FEATURE_WEATHER -DOSW_SERVICE_CONSOLE -DOSW_TARGET_PLATFORM_HEADER=\"platform/EMULATOR.h\" \
-DPIO_ENV_NAME=\"VIRTUAL\" -DTOOL_CALCULATOR=1 -DTOOL_CLOCK=1 -DTOOL_FLASHLIGHT=1 \
-DTOOL_STOPWATCH=1 -DTOOL_WATERLEVEL=1"



swig "-c++" "-lua" "-I./include" "-I./lib/Arduino_GFX" ${DEFINES} ./src/swig/osw.i
mv ./src/swig/osw_wrap.cxx ./src/swig/osw_wrap.tmp
echo "#ifdef OSW_FEATURE_LUA" > ./src/swig/osw_wrap.cxx
cat ./src/swig/osw_wrap.tmp >> ./src/swig/osw_wrap.cxx
echo "" >> ./src/swig/osw_wrap.cxx
echo "#endif" >> ./src/swig/osw_wrap.cxx
rm ./src/swig/osw_wrap.tmp

# And compile luac for compressing lua chunks
echo "Building luac ..."
make -C ./lib/LUA/ luac

if [ "$?" != "0" ]; then
echo "Building luac failed with return code: $?"
exit 1
fi

echo "Building luac successful."


echo "Compiling lua bytecode with luac ..."

cd ./src.lua
for i in $(find . -iname '*.lua'); do
echo compiling $i
../lib/LUA/luac -s -o ../data/lua/$i $i
done
echo "Compiling lua bytecode successful."
13 changes: 7 additions & 6 deletions scripts/build/prebuild_lua.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Import("env")

import sys
Import("env")

# Only build the swig file when the feature is enabled
buildWantsLua = False
Expand All @@ -22,12 +23,12 @@ def getPlatformCore(env):
framework = env['PIOFRAMEWORK'][0]
platform = env['PIOPLATFORM']
board = env['BOARD_MCU']

return os.path.join(os.path.join(os.path.join(packagesDir, f"framework-{framework}{platform}"), "cores"), board)

def getSwigPath():
path = "swig"

#Attempt to use swig.exe from install.py
windowsPath = os.path.join("bin", "swig")
exe = "swig.exe"
Expand All @@ -37,7 +38,7 @@ def getSwigPath():
for root, subdirs, files in os.walk(windowsPath):
if (exe in files):
return os.path.join(root, exe)

return path

# Try to pass on the build flags to swig
Expand Down Expand Up @@ -70,7 +71,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("make -C ./lib/LUA/ luac")
if return_code != 0:
print("Building lua compiler (luac) failed with return code:")
print(return_code)
Expand All @@ -79,7 +80,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")
13 changes: 6 additions & 7 deletions src/apps/watchfaces/OswAppWatchfaceZwilight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -117,7 +117,7 @@ bool OswAppWatchfaceZwilight::getTwilights() {
}

#else
#error "LUA not defined"
#warning "LUA not defined"
#endif

return true;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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();

Expand Down
21 changes: 14 additions & 7 deletions src/osw_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@

#include "lprefix.h"


#include <stddef.h>
#ifdef OSW_EMULATOR
#include <Serial.h>
#endif

extern "C" {
#include "lua.h"
Expand All @@ -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},
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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;
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/swig/osw.i
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,11 @@ void halToLua(lua_State *L) {

%include <std_string.i>
%include "stdint.i"
%include "Print.h"
%{
#ifdef OSW_EMULATOR
#include "FakePrint.h"
#else
#include "Print.h"
#endif
%}
%include "osw_hal.h"

0 comments on commit fdecd17

Please sign in to comment.