Skip to content

Commit 95d95c6

Browse files
committed
lua
1 parent 8b41cbc commit 95d95c6

File tree

8 files changed

+55
-28
lines changed

8 files changed

+55
-28
lines changed

examples/00.HelloWorld/madflight_config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,6 @@ const char madflight_config[] = R""(
155155

156156
// Uncomment to print additional debug information and reduce startup delay
157157
//#define MF_DEBUG
158+
159+
// Uncomment to enable Lua Scripting
160+
//#define MF_LUA_ENABLE 1

examples/01.Quadcopter/madflight_config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,6 @@ const char madflight_config[] = R""(
155155

156156
// Uncomment to print additional debug information and reduce startup delay
157157
//#define MF_DEBUG
158+
159+
// Uncomment to enable Lua Scripting
160+
//#define MF_LUA_ENABLE 1

examples/02.QuadcopterAdvanced/madflight_config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,6 @@ const char madflight_config[] = R""(
155155

156156
// Uncomment to print additional debug information and reduce startup delay
157157
//#define MF_DEBUG
158+
159+
// Uncomment to enable Lua Scripting
160+
//#define MF_LUA_ENABLE 1

examples/03.Plane/madflight_config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,6 @@ const char madflight_config[] = R""(
155155

156156
// Uncomment to print additional debug information and reduce startup delay
157157
//#define MF_DEBUG
158+
159+
// Uncomment to enable Lua Scripting
160+
//#define MF_LUA_ENABLE 1

src/madflight.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void madflight_setup() {
250250
#endif
251251
}
252252

253-
// LUA
253+
// LUA - Start Lua script /madflight.lua from SDCARD (when #define MF_LUA_ENABLE 1)
254254
lua.begin();
255255

256256
// CLI - Command Line Interface

src/madflight/hal/RP2040/Serial/uart_rx.pio

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*==========================================================================================
2+
MIT License
3+
4+
Copyright (c) 2025 https://github.com/qqqlab
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
==========================================================================================*/
24+
125
.pio_version 0 // only requires PIO version 0
226

327
.program uart_rx

src/madflight/lua/lua.h

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,18 @@
66
#include "../hal/hal.h" //xTaskCreate, vTaskDelete
77
#include "../bbx/bbx.h"
88

9-
/*
10-
11-
const char * code = R""(
12-
13-
while( true )
14-
do
15-
print('Hello from lua',mf.millis())
16-
mf.delay(1000);
17-
end
18-
19-
--result = result / 0 --ok
20-
--for --LUA ERROR: [string "..."]:12: <name> expected near <eof>
21-
troela() --LUA ERROR: [string "..."]:12: attempt to call a nil value (global 'troela')
22-
)"";
23-
*/
24-
25-
269
class LuaClass {
2710
private:
2811
TaskHandle_t xHandle;
29-
static uint8_t* code;
12+
static uint8_t* code;
13+
3014
static void lua_task(void *pvParameters) {
3115
(void)pvParameters;
3216
luawrap_run((const char*)LuaClass::code); //should not return
3317
free(LuaClass::code);
3418
vTaskDelete(NULL);
35-
}
19+
}
20+
3621
public:
3722
void begin() {
3823
int len = bbx.read("/madflight.lua", &LuaClass::code);

src/madflight/lua/luawrap.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ static void mf_register(lua_State *L) {
112112

113113
static jmp_buf luawrap_panic_jump;
114114

115-
bool luawrap_overtime = false;
116-
uint32_t luawrap_start_ts = 0;
115+
117116

118117
/* custom panic handler */
119118
static int luawrap_panic(lua_State *L)
@@ -123,11 +122,16 @@ static int luawrap_panic(lua_State *L)
123122
return 0;
124123
}
125124

125+
/*
126+
//------------------------
127+
// lua CPU time watchdog
128+
//------------------------
129+
uint32_t luawrap_start_ts = 0;
130+
bool luawrap_overtime = false;
131+
126132
//debug handler, called every time 1000 lines are executed
127133
static void luawrap_hook(lua_State *L, lua_Debug *ar) {
128-
return;
129-
130-
Serial.printf("luawrap_hookcnt dt=%u\n", micros() - luawrap_start_ts);
134+
//Serial.printf("luawrap_hookcnt dt=%d\n", (int)(micros() - luawrap_start_ts));
131135
if(micros() - luawrap_start_ts < 1000000) return;
132136
133137
luawrap_overtime = true;
@@ -139,7 +143,6 @@ static void luawrap_hook(lua_State *L, lua_Debug *ar) {
139143
luaL_error(L, "Exceeded CPU time");
140144
}
141145
142-
/*
143146
static void luawrap_reset_loop_overtime(lua_State *L) {
144147
luawrap_overtime = false;
145148
// reset the hook to clear the counter
@@ -194,13 +197,16 @@ void luawrap_run(const char* code) {
194197
//register mf table and global lua functions
195198
mf_register(L);
196199

197-
//execute
198-
//luawrap_reset_loop_overtime(L); //optional
200+
//optional cpu time watchdog
201+
//luawrap_reset_loop_overtime(L);
202+
203+
//execute lua script
199204
if (luaL_dostring(L, code) == LUA_OK) {
200205
lua_pop(L, lua_gettop(L)); // Pop the return value
201206
}else{
202207
const char *err = luaL_checkstring(L, -1);
203208
Serial.printf("LUA: ERROR: %s\n", err);
204209
}
210+
205211
lua_close(L);
206212
}

0 commit comments

Comments
 (0)