diff --git a/examples/README.md b/examples/README.md index 1ad7b8d7e..23a3967e6 100644 --- a/examples/README.md +++ b/examples/README.md @@ -643,6 +643,16 @@ maintaining duplicate logic in the NuttX repository. This is a simple test of the board LED driver at `nuttx/drivers/leds/userled_*.c`. +## `luamod_hello` Hello World Lua module + +A Lua C module showing how to add built-in modules to the Lua interpreter. +Usage: + +```lua +> hello.say_hello() +"Hello World!" +``` + ## `lis2csh_reader` `LIS3DSH` Accelerometer A simple reader example for the `LIS3DSH` acceleration sensor as found on diff --git a/examples/lua_module/Kconfig b/examples/lua_module/Kconfig new file mode 100644 index 000000000..18a30eb95 --- /dev/null +++ b/examples/lua_module/Kconfig @@ -0,0 +1,10 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_LUA_MODULE + tristate "Lua module example" + default n + ---help--- + Enable the Lua module example diff --git a/examples/lua_module/Make.defs b/examples/lua_module/Make.defs new file mode 100644 index 000000000..4daf2b8d0 --- /dev/null +++ b/examples/lua_module/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/examples/lua_module/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_EXAMPLES_LUA_MODULE),) +CONFIGURED_APPS += $(APPDIR)/examples/lua_module +endif diff --git a/examples/lua_module/Makefile b/examples/lua_module/Makefile new file mode 100644 index 000000000..c588aacf0 --- /dev/null +++ b/examples/lua_module/Makefile @@ -0,0 +1,35 @@ +############################################################################ +# apps/examples/lua_module/Makefile +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +# A Hello World Lua C module + +CSRCS = luamod_hello.c + +# Set LUAMODNAME and include Module.mk to add this module to the list of +# builtin modules for the Lua interpreter. LUAMODNAME should match the +# module's luaopen function. + +LUAMODNAME = hello + +include $(APPDIR)/interpreters/lua/Module.mk + +include $(APPDIR)/Application.mk diff --git a/examples/lua_module/luamod_hello.c b/examples/lua_module/luamod_hello.c new file mode 100644 index 000000000..dd9fb8b3c --- /dev/null +++ b/examples/lua_module/luamod_hello.c @@ -0,0 +1,76 @@ +/**************************************************************************** + * apps/examples/lua_module/luamod_hello.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int say_hello(lua_State *L); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct luaL_Reg g_hello[] = +{ + {"say_hello", say_hello}, + {NULL, NULL}, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: say_hello + * + * Push a "Hello World!" string to the Lua interpreter. + * + ****************************************************************************/ + +static int say_hello(lua_State *L) +{ + lua_pushstring(L, "Hello World!"); + return 1; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: luaopen_hello + * + * Open the "hello" Lua module. + * + ****************************************************************************/ + +int luaopen_hello(lua_State *L) +{ + luaL_newlib(L, g_hello); + return 1; +}