LUA support

All about Sub-20 Multi Interface USB Adapter USB to I2C, SPI, GPIO, RS232, RS485, Ir, LCD

Moderator: serg

Post Reply
serg
Posts: 143
Joined: Mon Aug 31, 2009 9:17 pm

LUA support

Post 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()
Attachments
adc.zip
(846 Bytes) Downloaded 972 times

Post Reply