Scripting Carambola with Lua

A piece of Lua script
Earlier I told you about Carambola, the small and inexpensive embedded Linux board, and how it’s going to be the brains of the Stripe internet-enabled linear clock. To make an interactive product do what you want you need software to tell it what to do. There are many programming and scripting languages around, so how do you pick the right one? In this blog post I’ll show you how I settled for Lua as the scripting language of choice for Stripe.

Programs and scripts

When choosing a language it is important to distinguish between programming languages and scripting languages. With programming languages such as (Arduino) C or C++ you write your program in a text editor or IDE, compile it into native machine code using a compiler and finally run it. If you want to use the program on a different operating system (Mac, Linux or Windows) or on a different platform (a normal pc based on i386, a smartphone with an ARM processor or the Carambola using MIPS) you need to go back to your development computer and recompile the program for the right platform.

Scripts, on the other hand, start out as text files just like with a programming language. However, they do not need to be compiled beforehand. The final platform turns the script into machine code while you run the script. This makes it possible to use the same script on different platforms, and also makes it easy to make small changes to the script without having to go back to your development computer. This makes scripts a lot easier to work with, but it does come at a price: speed. The extra translation step from script to native machine code makes a script run slower than a compiled program.

Picking the right language

If you are already proficient in a language, it’s often smart to just stick with what you know. For me this was not an option. I often work with web-focused languages such as PHP and JavaScript, and both are unsuitable for an embedded device like the Carambola. In order to drive an RGB LED strip from Carambola I wrote a program in C. The program does what it should and works really fast. However, for things that happen at a slow pace like retrieving Google Calendar data every once in a while, writing and compiling a C program would be too cumbersome for someone who is not a full-time C developer (like me). Time to look for a scripting language!

Prime candidate: Python

Python LogoA very popular scripting language is Python. It’s powerful, fast, has a large number of users and therefore a large number of tutorials. The tutorials and documentation are riddled with Monty Python references, which make following them quite entertaining. Libraries for Google Calendar and CalDAV exist, just like numerous others. For an embedded device, however, there is one big gripe. The full Python installation on Carambola is about 5MB, which is a lot on a device that has only 8MB of flash. Adding libraries quickly makes you run out of space – the required libraries for the Google Calendar library would add up to about 14MB!

Lua to the rescue

Lua logoLike Python, Lua is fast and powerful. But it is also lightweight – meaning it doesn’t take much space and resources to run. It’s often used in games such as World of Warcraft and Angry Birds because it’s fast and easily integrates with other programming languages. Moreover, the configuration interface for OpenWrt (the Linux-flavor used by the Carambola) is partly written in Lua which makes it very easy to adjust system settings such as time zone and IP addresses.

(And if you’re curious: all Lua files I need for Google Calendar integration use up 140kB of disk space. That’s 100 times less than with Python.)

On this blog, I am documenting the design of an internet-enabled linear LED clock: Stripe. Want to know more? Have a look at all posts about Stripe, subscribe to the RSS feed or follow me on Twitter

4 Responses to “Scripting Carambola with Lua”

  1. icarus75

    I think you might be confusing bytecode and native machine code.

    Bytecode is an intermediate, portable representation of your code. You can for example pre-compile your Lua code to Lua bytecode and still run it on any CPU architecture supported by the Lua VM.

    In contrast, C-code is typically compiled straight down to non-portable, native machine code. Then again, Emscripten [1] will compile C to Javascript. Go figure!



  2. David

    Thanks for the comment. You’re right on this one – I’ll change the post with the correct wording.

  3. Craig

    Your description of Lua makes it sound like size on disk is the only advantage. It’s also simpler, more regular, more orthogonal, *MUCH* faster (see LuaJIT), much better designed etc. etc.

    Python is fine for amateurs who like to go with the crowd and not think too much but Lua is vastly superior in a technical sense.

  4. Craig

    @icarus75, that’s not true. Lua bytecode is *not* portable across architectures because ANSI C has no sensible way of supporting it. Lua bytecode is dependant on the endianness and integer width of the system that generated it. LuaJITs bytecode is portable to every platform it supports, but it doesn’t support all platforms.

Leave a Reply