Page 1 of 1

LUA support

Posted: Sun Dec 04, 2011 6:09 pm
by serg
From the wiki page: Lua is a lightweight multi-paradigm programming language designed as a scripting language
with extensible semantics as a primary goal. Lua has a relatively simple C API compared
to other scripting languages. see http://www.lua.org

Below is a simple lua-script which demonstrates using SUB-20 ADC interface in lua script

Code: Select all

-- This lua script demonstrates using SUB-20 ADC interface 
-- The sub20dnc.dll must be located in the same directory

require 'CLRPackage'
import "System"

require 'luanet'

-- Load SUB-20 .NET component
luanet.load_assembly("sub20dnc.dll")

-- Import Sub20 type
local Sub20 = luanet.import_type("Xdimax.Sub20")

-- Create Sub-20 instance
local dev = Sub20()

local SamplesToRead = 16

local Data = Int32[SamplesToRead]
local Mux = Int32[SamplesToRead]

repeat

	-- Open first available SUB-20 device
	success = dev:Open(0)
	if not success 	then break end


    -- Enable the ADC and set Vref=Vcc
	success = dev:ADC_SetConfig( dev.AdcEnable+dev.AdcRefVcc )
	if not success 	then break end

	-- Setup Mux array to CH1
    for i=0, SamplesToRead - 1
	do
		Mux:Set(i,1)
    end

    -- ADC read
	success = dev:ADC_Read(Data, Mux)
	if not success 	then break end

    vref = 5.0;

    Samples = {}

    -- Convert ADC samples to Volts
    for i=1,SamplesToRead
    do
		Samples[i] = ( Data:Get(i - 1) * vref ) / 1023 ;
		print(Samples[i])
    end


    print( "The script completed successfully !" )

until true

if not success then
    print( "ERROR:", dev:GetStrError(dev:GetLastError()) )
end

-- Close the Sub20 instance
dev:Close()