Skip to content

Lua RTOS commands

loboris edited this page Mar 15, 2017 · 3 revisions

In Lua you can access some underlying Operation System functions through the os module. For example, if you want to know the system date you do:

/ > os.date()
Wed Dec 14 02:23:39 2016

Lua RTOS extends the standard Lua os module for allow the programmer to access to all Lua RTOS characteristics.

Please, refer to (http://www.lua.org/manual/5.3/manual.html#6.9) to know more about the standard features for this module.

Information commands

os.cpu()

Get the CPU name and CPU revision number, installed in your board.

Arguments: nothing

Returns: a string, with the CPU name installed on your board.

-- Get the CPU model
cpu = os.cpu();

-- Print to screen
print("CPU: "..cpu)
CPU: ESP32 rev 0

os.version()

Get Lua RTOS version installed in your board.

Arguments: nothing

Returns: the operating system name (Lua RTOS), the version number, and the operation system build time.

-- Get operating system name and version
oss, version, build = os.version();

-- Print to screen
print("OS: "..oss..", version: "..version..", build: "..build)
OS: Lua RTOS, version: beta 0.1, build: 1481679957

os.bootcount()

Get the number of boots from the deep sleep state.

Arguments: nothing Returns: the number of boots.

os.resetreason()

Get the reset reason.

Arguments: nothing Returns: reset reason.

Control commands

os.sleep(seconds)

Put the board in sleep mode for a time specified in the seconds argument. Once the sleep time ends the CPU is reset.

Arguments:

  • seconds: the number of seconds to sleep

Returns: nothing

-- Sleep for 10 seconds
os.sleep(10)

File system commands

os.ls([path])

List the path contents.

Arguments:

  • path: directory path. This argument it's optional, and if is not provided the contents of the current directory are listed. Path can be absolute or relative to current working directory.

Returns: nothing

/ > os.ls()
d              -                sd
d              -        Sun Mar 12 12:21:56 2017        examples
d              -        Sun Mar 12 12:21:57 2017        tests
f          24576        Tue Mar 14 15:00:50 2017        newyear_128x96.img
d              -        Sun Mar 12 12:21:56 2017        sys
f         230456        Mon Mar 13 14:14:59 2017        tiger.bmp
f          97543        Tue Mar 14 15:01:30 2017        tiger240.jpg
f              0        Sun Mar 12 12:21:57 2017        autorun.lua
f            468        Sun Mar 12 12:21:57 2017        system.lua
f          14045        Mon Mar 13 14:14:06 2017        tftdemo.lua
f          39360        Mon Mar 13 14:14:37 2017        nature_160x123.img

Directory contents is listed on the screen in columns (separated by tab):

  • first column: entry type (d = directory / f = file)
  • second column: entry size in bytes
  • third column: file timestamp
  • foutth column: entry name

os.list([path])

List the path contents. Similar to os.ls() but with some added features

  • displays free and total drive space
  • displays number of files in directory and used space
  • match files by wildchard

Arguments:

  • path: directory path. This argument it's optional, and if is not provided the contents of the current directory are listed. Path can be absolute or relative to current working directory.
  • wildcard spec: optional, if given only the matching files are shown Returns: nothing
/ > os.list("sd")
T  Size      Date/Time         Name
-----------------------------------
f      1137  10/02/2017 10:29  TEST.TXT
f        13  01/01/1980 00:00  FOO.TXT
f     13523  10/02/2017 10:38  TFTDEMO.LUA
f     13523  10/02/2017 13:43  TEST.LUA
f     13523  10/02/2017 13:44  TEST
f     13523  10/02/2017 14:39  test_tft_demo.lua
d         -  10/02/2017 14:50  PRoba
f   1000000  11/02/2017 18:01  ftest
f    190331  11/02/2017 00:13  tiger_1.jpg
d         -  19/02/2017 17:38  www
f     97543  19/02/2017 15:55  tiger.jpg
-----------------------------------
     1311KB in 9 files
-----------------------------------
FATFS: free 15061 MB of 15063 MB
/ > os.list("sd","*.jpg")
T  Size      Date/Time         Name
-----------------------------------
f    190331  11/02/2017 00:13  tiger_1.jpg
f     97543  19/02/2017 15:55  tiger.jpg
-----------------------------------
     287874 in 2 files
-----------------------------------
FATFS: free 15061 MB of 15063 MB

Directory contents is listed on the screen in columns (separated by tab):

  • first column: entry type (d = directory / f = file)
  • second column: entry size in bytes
  • third column: file timestamp
  • foutth column: entry name

os.cd(path)

Change the current working directory.

Arguments:

  • path: directory path. Path can be absolute or relative to current working directory.

Returns: nothing.

/> os.cd("/examples")
/examples >

os.pwd()

Get the current working directory.

Arguments: nothing

Returns: the current directory

/examples > os.pwd()
/examples

os.mkdir(path)

Make a directory. A file or directory can be removed with Lua [os.remove()] (http://www.lua.org/manual/5.3/manual.html#6.9) standard function.

Arguments:

  • directory path. Path can be absolute or relative to current working directory.

Returns: true if success

-- Make a new directory named test into the current working directory

os.mkdir("test")
true

os.cp(source path,destination path)

Copy a file.

Arguments:

  • source path: file source path
  • destination path: file destination path

Returns: nothing

-- Copy autorun.lua into autorun.old
os.cp("/autorun.lua","/autorun.old")

os.edit(file)

Edits a file.

Arguments:

  • file: file path. Can be absolute or relative to current working directory.

Returns: nothing

/> os.edit("autorun.lua")

Configuration commands

os.shell(enable)

Enable or disable Lua RTOS shell. If shell is enabled the programmer can interact with Lua RTOS in a friendly way:

/ > ls
f	     370		abp.lua
d	       -		examples
d	       -		sys
f	     468		system.lua
f	     388		wifi.lua
f	      40		autorun.lua
/ > cd examples
/examples > ls
d	       -		blocks
d	       -		lua
d	       -		a
f	       0		system.lua
/examples >

Lua RTOS shell is experimental and it's implementation is not finished!!.

Arguments:

  • enable: true for enable / false for disable

Returns: nothing

os.loglevel(level)

Sets the log level. The log level controls the amount of log information that Lua RTOS show in the console, and puts in the /log/messages.log file (is an SD Card is attached).

Arguments:

  • level: the log level, can be either os.LOG_ALL, os.LOG_INFO, os.LOG_EMERG, os.LOG_ALERT, os.LOG_CRIT, os.LOG_ERR, os.LOG_WARNING, os.LOG_NOTICE, os.LOG_DEBUG

Returns: nothing

-- Show only error logs
os.loglevel(os.LOG_ERR)

os.logcons(enable)

Enable or disable Lua RTOS log to the console. If it's enabled log messages are displayed in the console and written to the /log/messages.log file if an SD Card is attached. If it's disabled log mesages are written to the /log/messages.log file if an SD Card is attached.

Arguments:

  • enable: true for enable / false for disable

Returns: nothing

-- Disable logs on the console
os.logcons(false)

os.history(enable)

If enabled Lua RTOS records the commands entered by the programmer. The programmer can access the previous entered commands with the up & down keys. This functions is only available if an SD Card is attached to your board.

Arguments:

  • enable: true for enable / false for disable

Returns: nothing

-- Enable history
os.histrory(true)

Other functions

os.clear()

Clears the console.

Arguments: nothing Returns: nothing