Interfacing with Lua

From Pinguino-Wiki
Jump to: navigation, search

Contents

LUA Installation

First, LUA 5.1 should be installed.

On Linux:

sudo apt-get install lua5.1
sudo apt-get install liblua5.1-0-dev
sudo apt-get install libusb-1.0-0-dev

On Windows:

?

On Mac OS X (with Homebrew):

brew install lua

Launch it in a terminal, then type

print("LUA is ready !!") and hit Enter.

The result should be:

LUA is ready !!

Congratulations, LUA is installed !!

Now to communicate with USB, you need a libusb binding for LUA.

Download it here

Extract it :

tar xvzf lualibusb-0.4.tar.gz 

To compile this package you need the liblua5.1-0-dev package of LUA and libusb-1.0-0-dev.

That's why we installed it before.

Open the Makefile in the lualibusb folder and change the first 2 lines to point to the good folders, the lines for me were:

LIBUSB_SRC = /usr/include/libusb-1.0 #change with your libusb sources folder
LUA_SRC = /usr/include/lua5.1    #change with your lua sources folder

Then compile this libusb binding.

make

Now you should find a lualibusb.so file in the folder. Rename this file to libusb.so and copy it in /usr/local/lib/lua/5.1/

If this folder doesn't exist, create it.

mv lualibusb.so libusb.so
sudo mkdir -p /usr/local/lib/lua/5.1/
sudo cp libusb.so /usr/local/lib/lua/5.1/

Now your libusb binding is installed. Try it:

lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
>require('libusb')
>

Everythig is OK, now you can try LUA with your Pinguino.

Example - ds18b20

On Pinguino

/*
	Pinguino example to read ds18b20 1wire temperature sensor
	Result is sent on USB bus and can be read with temp18b20.py
	author		Régis Blanchot
	first release	14/09/2010
	last update	06/10/2010
	IDE		Pinguino b9.2 or sup.
 
	DS18B20 Connection
	------------------
	pin 1: GND
	pin 2: DQ (Data in/out) must be connected to the PIC
	pin 3: VDD (+5V)
 
	NB : 1-wire bus (DQ line) must have 4K7 pull-up resistor (connected to +5V)
*/
 
#define ONEWIREBUS	0	// 1-wire bus is on pin 0 (RB0), just change it to suit you 
#define RUNLED PORTAbits.RA4	// for visual debug purposes
 
void setup()
{
	//
	//	find all sensors on the bus and get their Rom Code
	//
 
	//DS18B20.find(ONEWIREBUS);
}
 
void loop()
{
	TEMPERATURE t;
	u8 temp[4];
	// if you want to read temperature from sensor #1 with max. resolution :
	// if (DS18B20.read(ONEWIREBUS, 1, RES12BIT, &t))
	// if you have only one sensor on the bus, just skip rom detection
	if (DS18B20.read(ONEWIREBUS, SKIPROM, RES12BIT, &t))
	{
		// send temperature on usb bus
		temp[0] = t.sign;		// t.sign is unsigned  char (1 byte)
		temp[1] = t.integer;		// t.integer  is unsigned  char (1 byte)
		temp[2] = high8(t.fraction);	// t.fraction is unsigned int (2 bytes)
		temp[3] = low8(t.fraction);
		USB.send(temp, 4);		// send 4-bit temp on usb bus
		RUNLED = RUNLED ^ 1;		// blinked led for visual debug
		delay(1000);			// wait for 1 sec. before next reading
	}
}

On Computer Side

Save these lines as temp18b20.lua

#!/usr/bin/lua
 
-- Read temperature from Pinguino connected with 18b20 sensor
-- Régis Blanchot 14/09/2010
 
require('libusb')
 
--
-- global variables
--
 
VENDOR = 0x04D8
PRODUCT = 0xFEAA
CONFIGURATION = 3
INTERFACE = 0
ENDPOINT_IN = 0x82
ENDPOINT_OUT = 0x01
 
--
-- read keyboard function in non blocking mode
--
 
function read_keyboard()
	charactere=io.popen("sh testkbd.sh","r")	-- open pipe and run command
	valeur=charactere:read()			-- read output of command
	charactere:close()				-- close pipe
	if valeur~=nil then
		return valeur
	end
end
 
--
-- try to find Pinguino
--
 
local buses=libusb.get_busses()    -- locate USB buses on the computer
 
function find_pinguino()
	for dirname, bus in pairs(buses) do
		local devices=libusb.get_devices(bus)
		for filename, device in pairs(devices) do
			local descriptor=libusb.device_descriptor(device)
			if ((descriptor.idVendor==VENDOR) and (descriptor.idProduct==PRODUCT)) then
				local handle=libusb.open(device)
				if ((libusb.set_configuration(handle, CONFIGURATION)) and (libusb.claim_interface(handle, INTERFACE))) then
					return handle
				else return nil
				end   
			end
		end
	end
end
 
--
-- main
--
 
pinguino=find_pinguino()	-- try to find a Pinguino
if (pinguino~=nil) then
	print('Pinguino initialised with success !!')
	print("Press a key to exit")
	ts=""
	ti=""
	tf=""
	while (read_keyboard()==nil) do
		-- int libusb.bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout);
		-- libusb.bulk_read performs a bulk read request to the endpoint specified by ep.
		-- Returns number of bytes read on success or < 0 on error.
		libusb.bulk_read(pinguino, ENDPOINT_IN, ts, 1)	-- read sign (1 byte)
		libusb.bulk_read(pinguino, ENDPOINT_IN, ti, 1)	-- read integer part (1 byte)
		libusb.bulk_read(pinguino, ENDPOINT_IN, tf, 2)	-- read fractional part (2 bytes)
		print(string.format("Temperature is %s%s.%s°C ", ts, ti, tf))
	end
	libusb.close(pinguino)	-- close Pinguino
else
	print('Pinguino initialisation failed !!')
	os.exit()
end

You will also need this little script to use the keyboard.

Type the following lines in your editor and save it as testkbd.sh

stty -echo -icanon time 0 min 0
  key=$( dd bs=1 count=1 2> /dev/null; printf "." )
  key=${key%.}
  case $key in
	?) printf "%d\n" "'$key";; 
  esac
stty sane

Launch the program in a Terminal with :

lua temp18b20.lua
Personal tools