Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 11 of 11

Thread: Lua interpreter example?

  1. #1

    Lua interpreter example?

    I'd like to be able to run some scripts from an SD card to control application behavior. Has anyone tried this using Lua on the Teensy 3.6?

    I'm aware of eLua but don't want to run the whole Teensy in Lua, just application specific code. To be more specific I'd like to write little games or apps in Lua to control a display connected to the Teensy. I'd also consider using Forth but figured Lua would be more accessible for others.

  2. #2
    Junior Member
    Join Date
    Feb 2018
    Posts
    9
    I am also interested in extending teensy sketches with lua.

    I have tried creating Arduino libs out of lua 5.3.4 and 5.3.2 but keep running into compile errors on some newer C++ libraries and types (signals and locale, for example). I will try again later with an older version of lua.

  3. #3
    I think the best option may be to use the Lua src that is part of eLua. https://github.com/elua/elua/tree/master/src/lua I wasn't able to fully get it compiled. Most of the errors were for other elua src files not in that folder.

  4. #4
    Junior Member
    Join Date
    Feb 2018
    Posts
    9
    Thanks for this. I had not heard of eLua. I took a look and started trying to compile it and made some progress. I will have to get back to this later.

  5. #5
    Senior Member Projectitis's Avatar
    Join Date
    Feb 2018
    Location
    New Zealand
    Posts
    114
    I'm interested in what you discover Particularly the size it adds to your program, it's memory usage and performance. i.e. everything Is it viable for extending teensy sketches?

  6. #6
    Junior Member
    Join Date
    Feb 2018
    Posts
    9
    I was able to build and execute lua 5.3.4. I haven't figured out how to incorporate lua 'files' into my project yet though, so I am just executing strings.

    Once I figure out how to add files to my project I will clean it up and do some more investigation. Unfortunately I am away and will be back next week.

  7. #7
    @ohthepain do you have a repo or any examples of getting it to compile?

  8. #8
    Senior Member Projectitis's Avatar
    Join Date
    Feb 2018
    Location
    New Zealand
    Posts
    114
    ^ I would be interested too

  9. #9
    Just noticed this: https://github.com/BleuLlama/TinyBasicPlus/

    It's not Lua but is written with arduinos in mind. Might work out of the box on the Teensy.

  10. #10
    For anyone interested in this I've got an example of running Lua 5.1.5 on a Teensy 3.6 here: https://github.com/AnthonyDiGirolamo/lua-teensyduino

    It contains a simple repl and examples of how to run lua code from strings or the sdcard (using SdFat).

    An example of making your own c++ functions available to Lua can be seen in the sdwrapper files.

    It uses Lua 5.1.5 with the following patches:

    Note: this isn't a port of eLua or nodemcu. It's just an example of how to add Lua scripting to your own Teensy 3.6 project.

    Please let me know if you have any feedback or make something with it!

  11. #11
    Junior Member
    Join Date
    Jan 2019
    Posts
    19
    For loading lua scripts, you'll probably want to use a bundler script like this one, then turn the result into a char array in a header file that you'll #include in your C source.

    That bundler is a little crude. You'd ideally want something like Browserify in the JS world, but that entails properly parsing the source files to look for require calls, which was overkill for my purposes.

    Note that the bundler here relies on require to avoid polluting the global namespace, because lulpeg is a library which should be encapsulated. For your own scripts, you could rely on globals to share values between files, and wrap each file in a do ... end block to keep the locals scoped per file. Just make sure the files are bundled in the right order according to their dependencies.
    Last edited by Vam; 01-08-2019 at 09:03 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •