Search code examples
c#luanvidia

Getting Dynamic Data in Lua


I have built an Instrumentation Cluster with the NVIDIA UI Composer Studio. To control animations it uses the Lua Scripting Language. Now I'm really new to Lua and my question is the following:

I've got the following Code sequence in Lua which controls my Speedometer:

self.vehicleSpeedData = {} -- A lot of Values are put in here

function self.speedSim( inFrame, theController )

local timeInSec, dummy
timeInSec, dummy = getElapsedTime()
-- data based on 60fps
actualFrameNumber = math.floor(timeInSec * 60)
local theSample = ((actualFrameNumber-1) % #self.vehicleSpeedData) + 1
theController.value = self.vehicleSpeedData[theSample] *0.06    

end

The array in this example is empty. So as you can see, the function reads out the Values of the Array.

But what I need is, that I can get this Data from an external Source (Such as RS232 or CAN Simulation) ... What I want to try is, if I can put Data from C# for example to that Lua Script.

It is hard do explain what I exactly want. My idea is that this Lua Script above listens and reads Data which I dynamically read in C# from my Data Source.

Thanks a lot for your help. This work is for my Bachelors Degree and I'm stuck at this point a long time and I'm nearly out of ideas.


Solution

  • It all kind of depends on what that Nvidia thing exposes to the user (in terms of API, and Lua base libraries).

    Assuming it is available, you can use io.read to read in a datafile (say a csv) from another source, then parse it yourself into your table. If you can preprocess the file to have a valid Lua syntax (eg prepend a return {, have values separated by , , and end with a }, you could directly load the string with loadstring).

    If they allow you to, you could use external libraries for interfacing with RS232, Excel, sockets, whatever.

    PS: it's Lua not LUA (not an acronym, but the Portuguese noun for Moon ;))

    Edit: example mkfifo

    so in Linux it goes like this: make a fifo with mkfifo fff and feed it something echo ' '> fff to prevent Lua from blocking.

    In Lua:

    fh=io.open('fff','rb')
    while true do
        res = fh:read()
        if res then
            print(res)
        end
    end
    

    Whatever cat into fff (eg cat 10 > fff) will come out in Lua. this way you can read out any available values and use them at each run of your function.

    Another option is using standard input, but I'm not sure whether this composer thing lets you.