- Sun Nov 16, 2014 2:04 pm
#2750
cendev wrote:I've added the scripts as attachment hope they'll help =)
I had a look at your scripts and converted them for (my) use of the serial console.
I saw in your video that you display the content of a file by 2 commands:
filename="filename.lua"
dofile("fileread.lua")
I tried using a function for a 1-liner that allows the filename as a parameter. This work, I like it!
I created the file functions.lua and uploaded it to the ESP
Code: Select all printfile = function (name)
file.open(name)
repeat
line=file.readline()
print(line)
until not line
end
On the ESP I execute the file functions.lua once. After that I can use the command printfile("filename.lua") to display the contents of the file.
Code: Select all> dofile("functions.lua")
> printfile("functions.lua")
printfile = function (name)
file.open(name)
repeat
line=file.readline()
print(line)
until not line
end
nil
>
Please note:
- I do not have to use seek to determine the end of file
- the last printed line = NIL, probably because the last line in the file is blank
- in this way you can extend the supported Lua commands as if they were built-in commands
Using this way of working I intend to:
- code all default functions I need in functions.lua. After dofile all functions are available by just typing the name of the function and the required variables.
- When all testing is done, and I am satisfied with the functions I upload the file functions.lua to init.lua to execute the file at boot time.
- for every additional lua file I code, the functions defined are available for use. As a kind of library.
Neat, huh!