In a couple previous blogs I looked at using Lua to read/write with Raspberry GPIO pin, and creating simple text based (ncurses) Lua interfaces.
The Lua LÖVE Graphic engine is very popular for 2D gaming.
In this blog I wanted to document how I used Lua LÖVE Graphics with Raspberry Pi data, specifically I’ll look at:
- Writing Text with different colours and Font sizes
- Use a timer to periodically update the screen with Raspberry Pi temperatures
- User keys to toggle Pi GPIO
- Use button images to toggle logic
Getting Started with LÖVE Graphics
Rather than LÖVE being a graphic library it’s a replacement Lua interpreter, so you run your code directly through LÖVE. To install LÖVE:
sudo apt-get update sudo apt-get install love
You can check that love is running by:
pi@raspberrypi:~/ $ love --version LOVE 11.1 (Mysterious Mysteries)
There are a number of IDE’s that are available, however any text editor will work. I used leafpad on the Pi.
First Example with Text and Keypresses
The LÖVE expects a project to be in it’s own directory with a main.lua file.
My first example was to launch a window with some text and then catch keystrokes. A “q” key will exit the program.
This program is call main.lua . An example of this program running with output:
pi@raspberrypi:~/lua1 $ love ./ a b q Quiting Now
Add a Timer Loop
There are a number of timer loop options. I found that some of these options however would take a lot of background CPU resources. One solution that worked well for me was to add a cron function from : https://github.com/kikito/cron.lua
I downloaded this code (cron.lua) and put it into my working project directory.
For this example I wanted to show the Raspberry Pi’s GPU temperatures. For the GPU temperature, a Pi command line utility exists:
pi@raspberrypi:~ $ /opt/vc/bin/vcgencmd measure_temp temp=44.0'C
This code example includes:
- A reference to a cron file, and a timer variable is setup with a getGPU function
- a shell to the command line : /opt/vc/bin/vcgencmd measure_temp , and then the output was read into a variable (GPU).
- the love.draw function is refreshed with the love.update function.
The output will look something like:
After I got the basic timer function working my next step will be to look at creating dynamic bars for Pi Sensors.
GPIO Reads and Writes
There are couple of choices for doing Lua connections to GPIO pins.
The lua-periphery library works well but it required sudo rights. For a command line program this isn’t a problem but by default the Raspberry Pi will not allow X-windows to run under super-user. A way to run X-Windows with sudo is:
sudo XAUTHORITY=$HOME/.Xauthority love /mypath_dir
Perhaps a simpler approach is to run the gpio command line program (this is pre-install on the PI’s). Below is some code that uses keystrokes to toggle a GPIO pin.
The output will be:
Button images can be defined in the load function, then in the draw function they can be drawn based on some logic. Below is a example with two buttons (red/green), the mousepressed function will catch mouse clicks on the x-y sizing of the image. Then some logic was added to toggle between the two red/green buttons and text feedback.
The button image and the “Button State” text will toggle each time the button is clicked.
I found that I was able to do all the things that I needed for my Raspberry Pi project, however I struggled with the documentation and examples.
For me this was a chance to learn Love, but I would pick Python over Lua/Love for Raspberry Pi projects. I found that Python with Tkinter was considerably faster with a lot more examples.