mogenson opened a new pull request #1075:
URL: https://github.com/apache/incubator-nuttx-apps/pull/1075


   ## Summary
   
   Lua is a popular scripting language that is tailored towards embedding in 
applications and embedded devices. This PR builds Lua as an NSH app. The 
`apps/interpreters/lua/Makefile` does a few notable things:
   - fetches and unpacks a source tarball from the Lua website
   - sets defines to use the `readline` library for the Lua REPL
   - adopts logic from `apps/builtin/Makefile` to generate headers for 
including builtin Lua modules
   
   To expand on the last point: Nuttx apps can include 
`apps/interpreters/lua/LuaModule.mk` to register a C module with the Lua 
interpreter. This makefile uses the `register` target to write entries to the 
`apps/interpreters/lua/registry` directory. See `apps/examples/luamod_hello` 
for an example of how to register a builtin Lua C module. The Lua source file 
`linit.c` is excluded from the build in favor of `nuttx_linit.c`, which 
optionally opens core Lua modules in addition to registered builtin modules.
   
   Here's an example of a Lua script to blink an LED using the `userleds` 
driver:
   
   ```lua
   -- concatenate strings and print to NSH console
   print("hello from " .. _VERSION)
   
   -- open device driver, file is closed when leds is garbage collected
   leds = io.open("/dev/userleds", "wb")
   
   -- from 1 to 10, inclusive
   for i = 1, 10 do  -- lua convention is to start at 1
       -- pack value into uint32_t and write to file
       leds:write(string.pack("I", i % 2))
       leds:flush()
   
       -- run sleep command from NSH
       os.execute("sleep 1")
   end
   ```
   
   Required defconfig:
   ```
   CONFIG_INTERPRETERS_LUA=y
   CONFIG_NSH_READLINE=y
   CONFIG_READLINE_CMD_HISTORY=y
   CONFIG_READLINE_TABCOMPLETION=y
   CONFIG_SYSTEM_NSH=y
   CONFIG_USERLED=y
   CONFIG_USERLED_LOWER=y
   ```
   
   For Lua bindings to the `userleds` `ioctl` interface, checkout this external 
Lua C module: https://github.com/mogenson/lua_userleds
   
   ## Impact
   
   No modifications to Nuttx are required and no changes outside of 
`apps/interpreters` and `apps/examples` were made.
   
   ## Testing
   
   Lua versions 5.2.0 through 5.4.4 have been tested successfully on a Teensy 
4.1 board and MacOS/Linux with the NSH simulator config. Lua version 5.1 and 
lower have a slightly different build setup that is incompatible with these 
Makefiles.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to