New in version 8.1.3.
Various functions to do io
New in version 8.1.5.
Allows interaction with files
pilight.io.file
(string file)¶Creates a new file object
exists
()¶Returns true if the file exists and false if not
open
(string mode)¶Opens the file with a specific mode
Mode Description r Opens a file for reading. The file must exist. w Creates an empty file for writing.If a file with the same name already exists,its content is erased and the file isconsidered as a new empty file.a Appends to a file. Writing operations,append data at the end of the file. The fileis created if it does not exist.r+ Opens a file to update both readingand writing. The file must exist.w+ Creates an empty file for both readingand writing.a+ Opens a file for reading and appending.
read
(number bytes)¶Reads maximum number of bytes from the opened file at a time
readline
()¶Returns an iterator to read the lines of the file
for line in file.readline() do
print(line);
end
write
(string data)¶Writes date to the opened file
seek
(number location [, string whence])¶Sets the position of the file to the given offset, relative to the optional whence
Whence Description set Beginning of file (default) cur Current position of the file pointer end End of file
close
()¶Closes the file object
function M.run()
local file = pilight.io.file("/var/log/syslog");
file.open("r");
local content = '';
for line in file.readline() do
content = content .. line;
end
file.close();
return 1;
end
return M;
Allows interaction with directories
pilight.io.dir
(string directory)¶Creates a new directory object
exists
()Returns true if the directory exists and false if not
close
()Closes the directory object
function M.run()
local dir = pilight.io.dir("/tmp");
if dir.exists() == false then
dir.close();
error("/tmp does not exists")
dir.close();
return 1;
end
return M;
Allows interaction with serial devices
pilight.io.serial
(string port)¶Creates a new serial object or return a previously created object for the same port
setBaudrate
()¶Sets or changes the baudrate of the serial connection Supported baudrates are:
Supported baudrates 50, 75, 110, 134, 150, 200, 600, 1200, 1800, 2400, 48009600, 19200, 38400, 57600, 115200, 230400
setParity
()¶Sets or changes the parity of the serial connection
Letter Function n, s No parity o Disable parity checking e Enable parity checking
open
()Open the serial device
close
()Close the serial device
write
(string line)Write the line to the serial device
read
()Tell the serial device we are (still) reading
setCallback
(string callback)¶The name of the callback being triggered when io occured. This callback will be called when data was read, written or when an error occured.
getUserdata
()¶Returns a persistent userdata table for the lifetime of the serial object.
setUserdata
(userdata table)¶Set a new persistent userdata table for the lifetime of the serial object. The userdata table cannot be of another type as returned from the getUserdata functions.
function M.callback(rw, serial, line)
if rw == 'write' then
print(line); -- success or fail
elseif rw == 'read' then
print(line);
serial.read();
elseif rw == 'disconnect' then
serial.close();
end
end
function M.run()
local serial = pilight.io.serial("/dev/ttyUSB0");
serial.setBaudrate(57600);
serial.setParity('n');
serial.setCallback("callback");
if serial.open() == false then
error("could not connect to device /dev/ttyUSB0");
end
serial.write("foo");
serial.read();
end
return M;
Allows interaction with Serial Peripheral Interfaces
pilight.io.spi
(number channel[, number frequency])¶Creates a new SPI object or return a previously created object for the same channel.
rw
(table value)¶Sets or gets the value of a certain register. The table required should have the following format:
{ adr = adr, val = val }
read
The adr
key should contain the register addres to be read, the val
key is ignored. A number is returned when valid data was read.
write
The adr
key should contain the register addres to be written, the val
should contain the value to be set. No data is returned.
function M.run()
local spi = pilight.io.spi(0, 2500000);
spi.rw({ adr = 0x01, val = 0x02 });
print(spi.rw({ adr = 0xAB, val = 0x00 }));
end
return M;