mirror of
https://git.mirrors.martin98.com/https://github.com/luc-github/ESP3D.git
synced 2025-08-03 12:40:37 +08:00

* Update WebSocket library * Update SSDP library * Update TFT_eSPI library * Update EspLuaEngine library * Update SDFat library * Change to pioarduino * Make ESP3DMessageFIFO and ESP3DMessage more thread safe * Fix sanity checks for BT * Add some C6 support * Refactor ethernet code * Split Ethernet Sta / WiFi sta ESP Commands and settings * Simplify wait and wdtFeed code * Set C3 with 4MB by default in platformio.ini * Apply Disable brown out only on ESP32 to avoid crash e.g:ESP32S3 * Add missing entries in platformio.ini
90 lines
2.5 KiB
C++
90 lines
2.5 KiB
C++
/*
|
|
EspLuaEngine project - Hello world example
|
|
|
|
Copyright (c) 2024 Luc Lebosse. All rights reserved.
|
|
|
|
This code is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
This code is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with this library; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
#include "EspLuaEngine.h"
|
|
#include <Arduino.h>
|
|
|
|
class Counter {
|
|
public:
|
|
Counter() : count(0) {}
|
|
void increment() { count++; }
|
|
int getCount() const { return count; }
|
|
private:
|
|
int count;
|
|
};
|
|
|
|
Counter globalCounter;
|
|
EspLuaEngine* luaEsp;
|
|
|
|
// Lua function to increment the counter
|
|
int l_incrementCounter(lua_State* L) {
|
|
Counter* counter = (Counter*)lua_touserdata(L, lua_upvalueindex(1));
|
|
counter->increment();
|
|
return 0;
|
|
}
|
|
|
|
// Lua function to get the counter value
|
|
int l_getCounterValue(lua_State* L) {
|
|
Counter* counter = (Counter*)lua_touserdata(L, lua_upvalueindex(1));
|
|
lua_pushinteger(L, counter->getCount());
|
|
return 1;
|
|
}
|
|
|
|
// Print function for Lua
|
|
int l_print(lua_State* L) {
|
|
int nargs = lua_gettop(L);
|
|
for (int i = 1; i <= nargs; i++) {
|
|
if (lua_isstring(L, i)) {
|
|
const char* s = lua_tostring(L, i);
|
|
Serial.print(s);
|
|
if (i < nargs) Serial.print(" ");
|
|
}
|
|
}
|
|
Serial.println();
|
|
return 0;
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
while (!Serial) {
|
|
; // Wait for serial connection
|
|
}
|
|
|
|
luaEsp = new EspLuaEngine();
|
|
|
|
// Register functions with userdata
|
|
luaEsp->registerFunction("incrementCounter", l_incrementCounter, &globalCounter);
|
|
luaEsp->registerFunction("getCounterValue", l_getCounterValue, &globalCounter);
|
|
luaEsp->registerFunction("print", l_print);
|
|
|
|
// Execute a Lua script that uses the counter
|
|
const char* script = R"(
|
|
print("Initial counter value: " .. getCounterValue())
|
|
for i = 1, 5 do
|
|
incrementCounter()
|
|
print("Counter value after increment: " .. getCounterValue())
|
|
end
|
|
)";
|
|
luaEsp->executeScript(script);
|
|
}
|
|
|
|
void loop() {
|
|
delay(1000);
|
|
} |