It’s kinda like baby-steps, small and shaky… :)
Hello world?
I’ve always hated to write anything containing “Hello World” so I wanted to make something at least some way useful instead.
I always wanted a screensaver but (at the moment) this is not possible due Lua limitations in formatting of LCD-screen, we are not allowed to use the whole screen.
Basic principles with Lua
Well, the Lua API documentation from here says pretty much all what comes to naming, placement and usage of scripts so no need for me to duplicate all the text here so I’ll just make small how-to instead.
What did I want?
I decided to make a selectable screen with a logo of my choice and date + time. So far it sounds easy but it took some time, I’m totally sure the fact that I’m not a programmer and this is my first crack at Lua had nothing to do with it :)
Since I wanted this to be selectable in main screen and not in menu (like Lua forms usually are) I made it as a telemetry-screen. Since we have a few options for telemetry screens/boxes (small, double, whole screen) this was the choice of the day.
The result is very simple yet almost exactly what I was after:
The Script
The script itself is short and easy:
local appName="DateTime"
local function printDate()
lcd.drawImage((310-imagepng.width)/2, -70, imagepng)
local dt = system.getDateTime()
lcd.setColor(255,255,255)
local dtDate = string.format("%02d.%02d.%d", dt.day, dt.mon, dt.year)
local dtTime = string.format("%d:%02d:%02d", dt.hour, dt.min, dt.sec)
lcd.drawText((310 - lcd.getTextWidth(FONT_MAXI,dtDate))/2,75,dtDate,FONT_MAXI)
lcd.drawText((310 - lcd.getTextWidth(FONT_MAXI,dtTime))/2,110,dtTime,FONT_MAXI)
end
local function init()
system.registerTelemetry(2,"Jeti DC-24 powered by RC-Thoughts.com",4,printDate);
imagepng = lcd.loadImage("Apps/Images/thoughts.png")
end
--------------------------------------------------------------------
return { init=init, loop=loop, author="Tero & RC-Thoughts", version="1.00",name=appName}
There is a few pointers here. the label in “system.registerTelemetry” is also the name we find this screen in telemetry-menus.
So, what happens in the script? Fairly simple, when loading the function “printDate” we are first drawing the background-image with horizontally centered and vertically lifted with 70 pixels. The path to image-file is stated in initialization function. (The -70 is not needed if image is purpose-made to right position, I was lazy…)
After that we are getting current time from the unit with “system.getDateTime()”, after that we need to change foreground color to white since the image has black background. Apparently Lua does not respect global colors defined by theme.
Now it’s time to make local values dtDate and dtTime and format them with values we get from “system.getDateTime()”.
Lastly time to draw the text, centered and with biggest font there is.
Activating Lua script
First, move the script to SD-card in “Apps” folder. Since I used an image “Apps/Images/thoughts.png” I also made the folder “Images” inside “Apps” and put the desired image there.
After that, dive in to setting:
Go to Menu -> Applications -> User Applications:
Press the “+” and look for the file you just moved to your SD-card’s “Apps”-folder:
Now the file is loaded. Now, if your code sucks (like mine did, many times…) you would not see the “Status: OK” on the right side. If you do you can use the F1-button and go to debug-screen and at least find the line-number in code where it failed. This looks ok to us:
So, the app is ok and it’s running. Next we need to activate it to our main screen via telemetry options.
Go to Displayed telemetry:
And press “Add”:
As you see the name of the Telemetry screen is now the same as the label we wrote in the Lua script.
Now we need to decide where to put the screen. Since we have the format of telemetry screen as “full screen” we have no possibility to use the usual normal size or double size, also the page divider is seen in the order selection screen:
That was it, pretty simple, huh?
I have no doubt that someone with better programming skills will point some errors to me but hey, this is my first Lua script ever, and it works :)
What’s next?
I do have an idea how to explore this further. One idea is to make triggers that would activate a bit like inactivity alarm and then use the “next screen” and “previous screen” choices to make this screen automatic. But that’s for some other day, there’s a ton of other interesting stuff going on with Lua…