examples: add Lua module example

This commit is contained in:
Michael Mogenson 2022-03-01 18:11:51 -05:00 committed by Xiang Xiao
parent 87df63d367
commit f1a74533f2
5 changed files with 154 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;
}