Merge pull request #630 from Riwan2/lua-fs-ops2
Added LUA file system operations
This commit is contained in:
@@ -202,16 +202,55 @@ if(NOT LUA STREQUAL NO)
|
|||||||
if(LUA STREQUAL YES)
|
if(LUA STREQUAL YES)
|
||||||
add_definitions(-DLUA_MODEL_SCRIPTS)
|
add_definitions(-DLUA_MODEL_SCRIPTS)
|
||||||
endif()
|
endif()
|
||||||
set(SRC ${SRC} lua/interface.cpp lua/api_general.cpp lua/api_model.cpp)
|
|
||||||
|
# TODO: LUA CMAKE
|
||||||
|
set(SRC ${SRC}
|
||||||
|
lua/interface.cpp
|
||||||
|
lua/api_general.cpp
|
||||||
|
lua/api_model.cpp
|
||||||
|
lua/api_filesystem.cpp
|
||||||
|
)
|
||||||
|
|
||||||
if(GUI_DIR STREQUAL colorlcd)
|
if(GUI_DIR STREQUAL colorlcd)
|
||||||
set(SRC ${SRC} lua/api_colorlcd.cpp lua/widgets.cpp)
|
set(SRC ${SRC} lua/api_colorlcd.cpp lua/widgets.cpp)
|
||||||
else()
|
else()
|
||||||
set(SRC ${SRC} lua/api_stdlcd.cpp)
|
set(SRC ${SRC} lua/api_stdlcd.cpp)
|
||||||
endif()
|
endif()
|
||||||
set(LUA_SRC lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c
|
|
||||||
lstate.c lstring.c ltable.c lrotable.c ltm.c lundump.c lvm.c lzio.c linit.c
|
set(LUA_SRC
|
||||||
lbaselib.c lmathlib.c lbitlib.c loadlib.c lauxlib.c ltablib.c lcorolib.c liolib.c lstrlib.c
|
lapi.c
|
||||||
)
|
lcode.c
|
||||||
|
lctype.c
|
||||||
|
ldebug.c
|
||||||
|
ldo.c
|
||||||
|
ldump.c
|
||||||
|
lfunc.c
|
||||||
|
lgc.c
|
||||||
|
llex.c
|
||||||
|
lmem.c
|
||||||
|
lobject.c
|
||||||
|
lopcodes.c
|
||||||
|
lparser.c
|
||||||
|
lstate.c
|
||||||
|
lstring.c
|
||||||
|
ltable.c
|
||||||
|
lrotable.c
|
||||||
|
ltm.c
|
||||||
|
lundump.c
|
||||||
|
lvm.c
|
||||||
|
lzio.c
|
||||||
|
linit.c
|
||||||
|
lbaselib.c
|
||||||
|
lmathlib.c
|
||||||
|
lbitlib.c
|
||||||
|
loadlib.c
|
||||||
|
lauxlib.c
|
||||||
|
ltablib.c
|
||||||
|
lcorolib.c
|
||||||
|
liolib.c
|
||||||
|
lstrlib.c
|
||||||
|
)
|
||||||
|
|
||||||
foreach(FILE ${LUA_SRC})
|
foreach(FILE ${LUA_SRC})
|
||||||
set(SRC ${SRC} ${LUA_DIR}/${FILE})
|
set(SRC ${SRC} ${LUA_DIR}/${FILE})
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|||||||
@@ -21,7 +21,6 @@
|
|||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include "opentx.h"
|
#include "opentx.h"
|
||||||
#include "lua_api.h"
|
|
||||||
#include "libopenui.h"
|
#include "libopenui.h"
|
||||||
|
|
||||||
#include "api_colorlcd.h"
|
#include "api_colorlcd.h"
|
||||||
|
|||||||
@@ -0,0 +1,163 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) EdgeTX
|
||||||
|
*
|
||||||
|
* Based on code named
|
||||||
|
* opentx - https://github.com/opentx/opentx
|
||||||
|
* th9x - http://code.google.com/p/th9x
|
||||||
|
* er9x - http://code.google.com/p/er9x
|
||||||
|
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||||
|
*
|
||||||
|
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License version 2 as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program 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 General Public License for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include "lua_api.h"
|
||||||
|
|
||||||
|
#include "api_filesystem.h"
|
||||||
|
|
||||||
|
// garbage collector for luaDir
|
||||||
|
static int dir_gc(lua_State* L)
|
||||||
|
{
|
||||||
|
DIR* dir = (DIR*)lua_touserdata(L, 1);
|
||||||
|
if (dir) f_closedir(dir);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void registerDirIter(lua_State* L)
|
||||||
|
{
|
||||||
|
luaL_newmetatable(L, DIR_METATABLE);
|
||||||
|
|
||||||
|
/* set the garbage colector field */
|
||||||
|
lua_pushstring(L, "__gc");
|
||||||
|
lua_pushcfunction(L, dir_gc);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
|
||||||
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dir_iter(lua_State* L)
|
||||||
|
{
|
||||||
|
DIR* dir = (DIR*)lua_touserdata(L, lua_upvalueindex(1));
|
||||||
|
|
||||||
|
FILINFO info;
|
||||||
|
FRESULT res = f_readdir(dir, &info);
|
||||||
|
if (res != FR_OK || info.fname[0] == 0) { /* Break on error or end of dir */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushstring(L, info.fname);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*luadoc
|
||||||
|
@function dir(directory)
|
||||||
|
|
||||||
|
Return an iterator listing all the files and directories name in a directory
|
||||||
|
|
||||||
|
@param directory (string) Working directory
|
||||||
|
|
||||||
|
@status current Introduced in 2.5.0
|
||||||
|
|
||||||
|
### Example
|
||||||
|
|
||||||
|
```lua
|
||||||
|
for fname in dir(".") do
|
||||||
|
print(fname)
|
||||||
|
end
|
||||||
|
|
||||||
|
*/
|
||||||
|
int luaDir(lua_State* L)
|
||||||
|
{
|
||||||
|
const char* path = luaL_optstring(L, 1, nullptr);
|
||||||
|
DIR* dir = (DIR*)lua_newuserdata(L, sizeof(DIR));
|
||||||
|
|
||||||
|
luaL_getmetatable(L, DIR_METATABLE);
|
||||||
|
lua_setmetatable(L, -2);
|
||||||
|
|
||||||
|
FRESULT res = f_opendir(dir, path);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
printf("luaDir cannot open %s\n", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushcclosure(L, dir_iter, 1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*luadoc
|
||||||
|
@function fstat(path)
|
||||||
|
|
||||||
|
Checks the existence of file or directory.
|
||||||
|
If not exist, return nil.
|
||||||
|
If exist, return the object information.
|
||||||
|
|
||||||
|
@param path (string) path to the object
|
||||||
|
|
||||||
|
@retval table object info, table elements:
|
||||||
|
* 'size' (number) file size
|
||||||
|
* 'attrib' (number) file attribute flags
|
||||||
|
* 'time' (table) table with last time modified date and times, table elements:
|
||||||
|
* `year` (number) year
|
||||||
|
* `mon` (number) month
|
||||||
|
* `day` (number) day of month
|
||||||
|
* `hour` (number) hours
|
||||||
|
* `hour12` (number) hours in US format
|
||||||
|
* `min` (number) minutes
|
||||||
|
* `sec` (number) seconds
|
||||||
|
* `suffix` (text) am or pm
|
||||||
|
|
||||||
|
### Example
|
||||||
|
|
||||||
|
```lua
|
||||||
|
info = fstat("radio")
|
||||||
|
if info ~= nil then
|
||||||
|
if (info.attrib == AM_DIR) then
|
||||||
|
print("is a directory")
|
||||||
|
end
|
||||||
|
|
||||||
|
size = info.size
|
||||||
|
time = info.time
|
||||||
|
end
|
||||||
|
|
||||||
|
@status current Introduced in 2.5.0
|
||||||
|
|
||||||
|
*/
|
||||||
|
int luaFstat(lua_State* L)
|
||||||
|
{
|
||||||
|
const char * path = luaL_optstring(L, 1, nullptr);
|
||||||
|
|
||||||
|
FRESULT res;
|
||||||
|
FILINFO info;
|
||||||
|
|
||||||
|
res = f_stat(path, &info);
|
||||||
|
if (res != FR_OK) {
|
||||||
|
printf("luaFstat cannot open %s\n", path);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_newtable(L);
|
||||||
|
lua_pushtableinteger(L, "size", info.fsize);
|
||||||
|
lua_pushtableinteger(L, "attrib", info.fattrib);
|
||||||
|
|
||||||
|
uint32_t year = (info.fdate >> 9) + 1980;
|
||||||
|
uint32_t month = info.fdate >> 5 & 15;
|
||||||
|
uint32_t day = info.fdate & 31;
|
||||||
|
uint32_t hour = info.ftime >> 11;
|
||||||
|
uint32_t mn = info.ftime >> 5 & 63;
|
||||||
|
uint32_t sec = (info.ftime & 31) * 2;
|
||||||
|
|
||||||
|
lua_pushstring(L, "time");
|
||||||
|
luaPushDateTime(L, year, month, day, hour, mn, sec);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) EdgeTX
|
||||||
|
*
|
||||||
|
* Based on code named
|
||||||
|
* opentx - https://github.com/opentx/opentx
|
||||||
|
* th9x - http://code.google.com/p/th9x
|
||||||
|
* er9x - http://code.google.com/p/er9x
|
||||||
|
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||||
|
*
|
||||||
|
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License version 2 as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program 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 General Public License for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define DIR_METATABLE "directory metatable"
|
||||||
|
|
||||||
|
void registerDirIter(lua_State* L);
|
||||||
|
int luaDir(lua_State* L);
|
||||||
|
int luaFstat(lua_State* L);
|
||||||
@@ -23,6 +23,7 @@
|
|||||||
#include "opentx.h"
|
#include "opentx.h"
|
||||||
#include "stamp.h"
|
#include "stamp.h"
|
||||||
#include "lua_api.h"
|
#include "lua_api.h"
|
||||||
|
#include "api_filesystem.h"
|
||||||
#include "telemetry/frsky.h"
|
#include "telemetry/frsky.h"
|
||||||
#include "telemetry/multi.h"
|
#include "telemetry/multi.h"
|
||||||
|
|
||||||
@@ -165,7 +166,7 @@ static int luaGetTime(lua_State * L)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void luaPushDateTime(lua_State * L, uint32_t year, uint32_t mon, uint32_t day,
|
void luaPushDateTime(lua_State * L, uint32_t year, uint32_t mon, uint32_t day,
|
||||||
uint32_t hour, uint32_t min, uint32_t sec)
|
uint32_t hour, uint32_t min, uint32_t sec)
|
||||||
{
|
{
|
||||||
uint32_t hour12 = hour;
|
uint32_t hour12 = hour;
|
||||||
@@ -1816,6 +1817,8 @@ const luaL_Reg opentxLib[] = {
|
|||||||
{ "defaultChannel", luaDefaultChannel },
|
{ "defaultChannel", luaDefaultChannel },
|
||||||
{ "getRSSI", luaGetRSSI },
|
{ "getRSSI", luaGetRSSI },
|
||||||
{ "killEvents", luaKillEvents },
|
{ "killEvents", luaKillEvents },
|
||||||
|
{ "dir", luaDir },
|
||||||
|
{ "fstat", luaFstat },
|
||||||
{ "chdir", luaChdir },
|
{ "chdir", luaChdir },
|
||||||
{ "loadScript", luaLoadScript },
|
{ "loadScript", luaLoadScript },
|
||||||
{ "getUsage", luaGetUsage },
|
{ "getUsage", luaGetUsage },
|
||||||
@@ -2175,5 +2178,11 @@ const luaR_value_entry opentxConstants[] = {
|
|||||||
{"UNIT_BITFIELD", UNIT_BITFIELD},
|
{"UNIT_BITFIELD", UNIT_BITFIELD},
|
||||||
{"UNIT_TEXT", UNIT_TEXT},
|
{"UNIT_TEXT", UNIT_TEXT},
|
||||||
|
|
||||||
|
{"AM_RDO", AM_RDO},
|
||||||
|
{"AM_HID", AM_HID},
|
||||||
|
{"AM_SYS", AM_SYS},
|
||||||
|
{"AM_DIR", AM_DIR},
|
||||||
|
{"AM_ARC", AM_ARC},
|
||||||
|
|
||||||
{ nullptr, 0 } /* sentinel */
|
{ nullptr, 0 } /* sentinel */
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
#include "bin_allocator.h"
|
#include "bin_allocator.h"
|
||||||
#include "lua_api.h"
|
#include "lua_api.h"
|
||||||
#include "sdcard.h"
|
#include "sdcard.h"
|
||||||
|
#include "api_filesystem.h"
|
||||||
|
|
||||||
#if defined(LIBOPENUI)
|
#if defined(LIBOPENUI)
|
||||||
#include "api_colorlcd.h"
|
#include "api_colorlcd.h"
|
||||||
@@ -257,6 +258,8 @@ void luaClose(lua_State ** L)
|
|||||||
void luaRegisterLibraries(lua_State * L)
|
void luaRegisterLibraries(lua_State * L)
|
||||||
{
|
{
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
|
registerDirIter(L);
|
||||||
|
|
||||||
#if defined(COLORLCD)
|
#if defined(COLORLCD)
|
||||||
registerBitmapClass(L);
|
registerBitmapClass(L);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -23,6 +23,9 @@
|
|||||||
|
|
||||||
#if defined(LUA)
|
#if defined(LUA)
|
||||||
|
|
||||||
|
// prevent C++ code to be included from lua.h
|
||||||
|
#include "rtos.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
@@ -31,6 +34,9 @@ extern "C" {
|
|||||||
#include <lgc.h>
|
#include <lgc.h>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "dataconstants.h"
|
||||||
|
#include "opentx_types.h"
|
||||||
|
|
||||||
#ifndef LUA_SCRIPT_LOAD_MODE
|
#ifndef LUA_SCRIPT_LOAD_MODE
|
||||||
// Can force loading of binary (.luac) or plain-text (.lua) versions of scripts specifically, and control
|
// Can force loading of binary (.luac) or plain-text (.lua) versions of scripts specifically, and control
|
||||||
// compilation options. See interface.cpp:luaLoadScriptFileToState() <mode> parameter description for details.
|
// compilation options. See interface.cpp:luaLoadScriptFileToState() <mode> parameter description for details.
|
||||||
@@ -209,6 +215,8 @@ void luaRegisterLibraries(lua_State * L);
|
|||||||
void registerBitmapClass(lua_State * L);
|
void registerBitmapClass(lua_State * L);
|
||||||
void luaSetInstructionsLimit(lua_State* L, int count);
|
void luaSetInstructionsLimit(lua_State* L, int count);
|
||||||
int luaLoadScriptFileToState(lua_State * L, const char * filename, const char * mode);
|
int luaLoadScriptFileToState(lua_State * L, const char * filename, const char * mode);
|
||||||
|
void luaPushDateTime(lua_State * L, uint32_t year, uint32_t mon, uint32_t day,
|
||||||
|
uint32_t hour, uint32_t min, uint32_t sec);
|
||||||
|
|
||||||
// Unregister LUA widget factories
|
// Unregister LUA widget factories
|
||||||
void luaUnregisterWidgets();
|
void luaUnregisterWidgets();
|
||||||
|
|||||||
Reference in New Issue
Block a user