Hello,
Indeed the "proper" way of including a script would be to store it in a
file system.
However, when I needed to include a single and small script and I didn't
want to introduce a complete FS just for this, I used xxd.
xxd can convert any file to a C header file.
You can then include the header, and access the whole file as a variable.
Here is an example:
I added this in my app Makefile:
# Setup any special pre-build context
context: header
$(Q) cd path/to/libs/json.lua/ && xxd -i json.lua > json_lua.h &&
echo -n "const " | cat - json_lua.h > temp && mv temp json_lua.h
And then I used the file like this:
#include "lua.h"#include "lauxlib.h"#include <string.h>
#include "json_lua.h"
static int luaopen_json(lua_State * L);
void ExtLibs_load(lua_State * L){
// json.lua#ifdef CONFIG_EXT_LIB_JSON_LUA
luaL_requiref(L, "json", luaopen_json, 1);
lua_pop(L, 1);#endif}
int luaopen_json(lua_State * L){
const char * modname = lua_tostring(L, 1);
if (strcmp(modname, "json") != 0)
return luaL_error(L, "cannot load json module");
if (luaL_loadbufferx(L, (char*)json_lua, json_lua_len, "json", "t") !=
LUA_OK)
return lua_error(L);
lua_call(L, 0, 1);
return 1;}
I hope this helps...
On Sun, Jan 29, 2023 at 7:34 AM Xiang Xiao <[email protected]>
wrote:
> You can use the real file system on the device, there are many choices:
> romfs, littlefs, fatfs, starmtfs and spiffs.
>
> On Sun, Jan 29, 2023 at 12:59 PM Russell Haley <[email protected]>
> wrote:
>
> > On Sat, Jan 28, 2023 at 7:35 PM Xiang Xiao <[email protected]>
> > wrote:
> >
> > > You can enable CONFIG_FS_HOSTFS/CONFIG_SIM_HOSTFS, put your scripts
> into
> > > some PC folder and run mount this folder from nsh:
> > > mount -t hostfs -o fs=/path/to/your/pc/folder. /data
> > >
> > > While I appreciate the answer, I am using the sim as a testing platform
> > and hoping to move to either an STM32F4/7 or a Sony Spresense. I am
> hoping
> > for a solution that is applicable to an embedded project. If I can't just
> > add files to the initial image then I will look at the romfs example and
> > maybe the next best thing?
> >
> >
> > >
> > > On Sun, Jan 29, 2023 at 2:24 AM Russell Haley <[email protected]>
> > > wrote:
> > >
> > > > Hi,
> > > >
> > > > Big thanks to Xiang Xiao for pointing me to the sim:lua
> configuration.
> > I
> > > > was unable to simply include the defconfig file that you linked to,
> > but I
> > > > was able to reconfigure for the sim:lua configuration. I've now got
> an
> > > app
> > > > in the examples folder that includes the Lua interpreter. Is there a
> > > > tutorial on how to include folders and lua scripts or extra files in
> > the
> > > > initial file system?
> > > >
> > > > Much appreciated,
> > > > Russ
> > > >
> > >
> >
>