You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/03/21 05:49:51 UTC

[incubator-nuttx-apps] 02/02: examples: add Lua module example

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit f1a74533f2a637b276d898a367f365e792aa0567
Author: Michael Mogenson <mi...@gmail.com>
AuthorDate: Tue Mar 1 18:11:51 2022 -0500

    examples: add Lua module example
---
 examples/README.md                 | 10 +++++
 examples/lua_module/Kconfig        | 10 +++++
 examples/lua_module/Make.defs      | 23 ++++++++++++
 examples/lua_module/Makefile       | 35 ++++++++++++++++++
 examples/lua_module/luamod_hello.c | 76 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 154 insertions(+)

diff --git a/examples/README.md b/examples/README.md
index 1ad7b8d..23a3967 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 0000000..18a30eb
--- /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 0000000..4daf2b8
--- /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 0000000..c588aac
--- /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 0000000..dd9fb8b
--- /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 <lua.h>
+#include <lauxlib.h>
+
+/****************************************************************************
+ * 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;
+}