Format the file system. Completely erases any existing file system and writes a new one. Depending on the size of the flash chip in the ESP, this may take several seconds.
## file.flush()
####Syntax
`file.format()`
Flushes any pending writes to the file system, ensuring no data is lost on a restart. Closing the open file using [`file.close()`](#fileclose) performs an implicit flush as well.
####Parameters
`nil`
#### Syntax
`file.flush()`
#### Parameters
none
####Returns
####Returns
`nil`
####Example
####Example
```lua
file.format()
-- open 'init.lua' in 'a+' mode
file.open("init.lua","a+")
-- write 'foo bar' to the end of the file
file.write('foo bar')
file.flush()
-- write 'baz' too
file.write('baz')
file.close()
```
#### See also
[`file.close()`](#fileclose)
####See also
-`file.remove()`
## file.format()
___
## file.list()
Format the file system. Completely erases any existing file system and writes a new one. Depending on the size of the flash chip in the ESP, this may take several seconds.
Lists all files in the file system.
#### Syntax
`file.format()`
####Syntax
`file.list()`
#### Parameters
none
####Parameters
#### Returns
`nil`
####Returns
a lua table which contains the {file name: file size} pairs
#### See also
[`file.remove()`](#fileremove)
####Example
```lua
l=file.list();
fork,vinpairs(l)do
print("name:"..k..", size:"..v)
end
```
___
## file.remove()
## file.fsinfo()
Remove a file from the file system. The file must not be currently open.
Return size information for the file system, in bytes.
Renames a file. If a file is currently open, it will be closed first.
## file.list()
####Syntax
`file.rename(oldname, newname)`
Lists all files in the file system.
####Parameters
-`oldname`: old file name
-`newname`: new file name
#### Syntax
`file.list()`
####Returns
`true` on success, `false` on error.
#### Parameters
none
####Example
#### Returns
a lua table which contains the {file name: file size} pairs
#### Example
```lua
-- rename file 'temp.lua' to 'init.lua'.
file.rename("temp.lua","init.lua")
l=file.list();
fork,vinpairs(l)do
print("name:"..k..", size:"..v)
end
```
___
## file.open()
Opens a file for access, potentially creating it (for write modes).
When done with the file, it must be closed using `file.close()`.
####Syntax
####Syntax
`file.open(filename, mode)`
####Parameters
-`filename`: file to be opened, directories are not supported
-`mode`:
- "r": read mode (the default)<br/>
- "w": write mode<br/>
- "a": append mode<br/>
- "r+": update mode, all previous data is preserved<br/>
- "w+": update mode, all previous data is erased<br/>
####Parameters
-`filename` file to be opened, directories are not supported
-`mode`:
- "r": read mode (the default)
- "w": write mode
- "a": append mode
- "r+": update mode, all previous data is preserved
- "w+": update mode, all previous data is erased
- "a+": append update mode, previous data is preserved, writing is only allowed at the end of file
####Returns
-`nil` if file not opened, or not exists (read modes). true` if file opened ok.
####Example
#### Returns
`nil` if file not opened, or not exists (read modes). `true` if file opened ok.
#### Example
```lua
-- open 'init.lua', print the first line.
file.open("init.lua", "r")
print(file.readline())
file.close()
-- open 'init.lua', print the first line.
file.open("init.lua","r")
print(file.readline())
file.close()
```
####See also
- `file.close()`
- `file.readline()`
___
## file.close()
#### See also
-[`file.close()`](#fileclose)
-[`file.readline()`](#filereadline)
Closes the open file, if any.
## file.read()
####Syntax
`file.close()`
Read content from the open file.
####Parameters
`nil`
#### Syntax
`file.read([n_or_str])`
####Returns
`nil`
#### Parameters
-`n_or_str`:
- if nothing passed in, read all byte in file
- if pass a number n, then read n bytes from file, or EOF is reached
- if pass a string "str", then read until 'str' or EOF is reached
####Example
#### Returns
fdile content in string, or nil when EOF
#### Example
```lua
-- open 'init.lua', print the first line.
file.open("init.lua", "r")
print(file.readline())
file.close()
-- print the first line of 'init.lua'
file.open("init.lua","r")
print(file.read('\n'))
file.close()
-- print the first 5 byte of 'init.lua'
file.open("init.lua","r")
print(file.read(5))
file.close()
```
####See also
- `file.open()`
___
#### See also
-[`file.open()`](#fileopen)
-[`file.readline()`](#filereadline)
## file.readline()
Read the next line from the open file.
####Syntax
####Syntax
`file.readline()`
####Parameters
`nil`
####Parameters
none
####Returns
####Returns
File content in string, line by line, include EOL('\n'). Return `nil` when EOF.
####Example
#### Example
```lua
-- print the first line of 'init.lua'
file.open("init.lua", "r")
print(file.readline())
file.close()
-- print the first line of 'init.lua'
file.open("init.lua","r")
print(file.readline())
file.close()
```
####See also
- `file.open()`
- `file.close()`
- `file.read()`
####See also
-[`file.open()`](#fileopen)
-[`file.close()`](#fileclose)
-[`file.read()`](#filereade)
___
## file.writeline()
## file.remove()
Write a string to the open file and append '\n' at the end.
Remove a file from the file system. The file must not be currently open.
####Syntax
`file.writeline(string)`
###Syntax
`file.remove(filename)`
####Parameters
- `string`: content to be write to file
####Parameters
`filename` file to remove
####Returns
`true` if write ok, `nil` on error.
####Returns
`nil`
####Example
####Example
```lua
-- open 'init.lua' in 'a+' mode
file.open("init.lua", "a+")
-- write 'foo bar' to the end of the file
file.writeline('foo bar')
file.close()
-- remove "foo.lua" from file system.
file.remove("foo.lua")
```
#### See also
[`file.open()`](#fileopen)
####See also
- `file.open()`
- `file.readline()`
## file.rename()
___
## file.read()
Renames a file. If a file is currently open, it will be closed first.
Read content from the open file.
####Syntax
`file.read([n_or_str])`
#### Syntax
`file.rename(oldname, newname)`
#### Parameters
-`oldname` old file name
-`newname` new file name
####Parameters
- `n_or_str`:
- if nothing passed in, read all byte in file.
- if pass a number n, then read n bytes from file, or EOF is reached.
- if pass a string "str", then read until 'str' or EOF is reached.
#### Returns
`true` on success, `false` on error.
####Returns
File content in string, or nil when EOF.
#### Example
####Example
```lua
-- print the first line of 'init.lua'
file.open("init.lua", "r")
print(file.read('\n'))
file.close()
-- print the first 5 byte of 'init.lua'
file.open("init.lua", "r")
print(file.read(5))
file.close()
-- rename file 'temp.lua' to 'init.lua'.
file.rename("temp.lua","init.lua")
```
####See also
- `file.open()`
- `file.readline()`
___
## file.write()
Write a string to the open file.
## file.seek()
Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by the string whence.
####Syntax
`file.write(string)`
####Syntax
`file.seek([whence [, offset]])`
####Parameters
`string`: content to be write to file.
#### Parameters
-`whence`
- "set": base is position 0 (beginning of the file)
- "cur": base is current position (default value)
- "end": base is end of file
-`offset` default 0
####Returns
`true` if the write is ok, `nil` on error.
If no parameters are given, the function simply returns the current file offset.
####Example
#### Returns
the resulting file position, or `nil` on error
#### Example
```lua
-- open 'init.lua' in 'a+' mode
file.open("init.lua", "a+")
-- write 'foo bar' to the end of the file
file.write('foo bar')
file.close()
file.open("init.lua","r")
-- skip the first 5 bytes of the file
file.seek("set",5)
print(file.readline())
file.close()
```
#### See also
[`file.open()`](#fileopen)
####See also
- `file.open()`
- `file.writeline()`
___
## file.flush()
## file.write()
Flushes any pending writes to the file system, ensuring no data is lost on a restart. Closing the open file using `file.close()` performs an implicit flush as well.
Write a string to the open file.
####Syntax
`file.flush()`
####Syntax
`file.write(string)`
####Parameters
`nil`
####Parameters
`string` content to be write to file
####Returns
`nil`
####Returns
`true` if the write is ok, `nil` on error
####Example
####Example
```lua
-- open 'init.lua' in 'a+' mode
file.open("init.lua", "a+")
-- write 'foo bar' to the end of the file
file.write('foo bar')
file.flush()
-- write 'baz' too
file.write('baz')
file.close()
-- open 'init.lua' in 'a+' mode
file.open("init.lua","a+")
-- write 'foo bar' to the end of the file
file.write('foo bar')
file.close()
```
####See also
- `file.close()`
___
## file.seek()
Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by the string whence.
#### See also
-[`file.open()`](#fileopen)
-[`file.writeline()`](#filewriteline)
####Syntax
`file.seek([whence [, offset]])`
## file.writeline()
Write a string to the open file and append '\n' at the end.
####Parameters
- `whence`:
- "set": base is position 0 (beginning of the file)
- "cur": base is current position (default value)
- "end": base is end of file
- offset: default 0
#### Syntax
`file.writeline(string)`
If no parameters are given, the function simply returns the current file offset.
@@ -4,266 +4,257 @@ The tmr module allows access to simple timers, the system counter and uptime.
It is aimed at setting up regularly occurring tasks, timing out operations, and provide low-resolution deltas.
What the tmr module is *not* however, is a time keeping module. While most timeouts are expressed in milliseconds or even microseconds, the accuracy is limited and compounding errors would lead to rather inaccurate time keeping. Consider using the `rtctime` module for "wall clock" time.
What the tmr module is *not* however, is a time keeping module. While most timeouts are expressed in milliseconds or even microseconds, the accuracy is limited and compounding errors would lead to rather inaccurate time keeping. Consider using the [rtctime](rtctime.md) module for "wall clock" time.
NodeMCU provides 7 timers, numbered 0-6. It is currently up to the user to keep track of which timers are used for what.
####See also
- rtctime module
## tmr.register()
Configures a timer and registers the callback function to call on expiry.
To free up the resources with this timer when done using it, call `tmr.unregister()` on it. For one-shot timers this is not necessary, unless they were stopped before they expired.
## tmr.alarm()
####Syntax
`tmr.register(id, interval_ms, mode, func)`
This is a convenience function combining [`tmr.register()`](#tmrregister) and [`tmr.start()`](#tmrstart) into a single call.
####Parameters
-`id`: The timer id (0-6).
-`interval_ms`: timer interval in milliseconds. Maximum value is 12884901. In SDKs <=1.5.0values>6871948 result in incorrect behaviour.
-`mode`: timer mode:
-`tmr.ALARM_SINGLE`: a one-shot alarm (and no need to call `tmr.unregister()`)
-`tmr.ALARM_SEMI`: manually repeating alarm (call `tmr.start()` to restart)
-`tmr.ALARM_AUTO`: automatically repeating alarm
To free up the resources with this timer when done using it, call [`tmr.unregister()`](#tmrunregister) on it. For one-shot timers this is not necessary, unless they were stopped before they expired.
Note that registering does *not* start the alarm.
#### Parameters
-`id` timer id (0-6)
-`interval_ms` timer interval in milliseconds. Maximum value is 12884901. In SDKs <=1.5.0values>6871948 result in incorrect behaviour.
-`mode` timer mode:
-`tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`tmr.unregister()`](#tmrunregister))
-`tmr.ALARM_SEMI` manually repeating alarm (call [`tmr.start()`](#tmrstart) to restart)
Stops the timer (if running) and unregisters the associated callback.
Busyloops the processor for a specified number of microseconds.
This isn't necessary for one-shot timers (`tmr.ALARM_SINGLE`), as those automatically unregister themselves when fired.
This is in general a **bad** idea, because nothing else gets to run, and the networking stack (and other things) can fall over as a result. The only time`tmr.delay()` may be appropriate to use is if dealing with a peripheral device which needs a (very) brief delay between commands, or similar. *Use with caution!*
####Syntax
`tmr.unregister(id)`
Also note that the actual amount of time delayed for may be noticeably greater, both as a result of timing inaccuracies as well as interrupts which may run during this time.
#### Syntax
`tmr.delay(us)`
####Parameters
-`id`: The timer id (0-6).
####Parameters
`us` microseconds to busyloop for
####Returns
####Returns
`nil`
####Example
####Example
```lua
tmr.unregister(0)
tmr.delay(100)
```
####See also
-`tmr.register()`
___
## tmr.start()
## tmr.interval()
Starts or restarts a previously configured timer.
Changes a registered timer's expiry interval.
####Syntax
`tmr.start(id)`
####Syntax
`tmr.interval(id, interval_ms)`
####Parameters
-`id`: The timer id (0-6).
#### Parameters
-`id` timer id (0-6)
-`interval_ms` new timer interval in milliseconds. Maximum value is 12884901. In SDKs <=1.5.0values>6871948 result in incorrect behaviour.
tmr.interval(0,3000)-- actually, 3 seconds is better!
```
####See also
-`tmr.register()`
-`tmr.stop()`
-`tmr.unregister()`
___
## tmr.stop()
## tmr.now()
Stops a running timer, but does *not* unregister it. A stopped timer can be restarted with `tmr.start()`.
Returns the system counter, which counts in microseconds. Limited to 31 bits, after that it wraps around back to zero. That is essential if you use this function to [debounce or throttle GPIO input](https://github.com/hackhitchin/esp8266-co-uk/issues/2).
####Syntax
`tmr.stop(id)`
####Syntax
`tmr.now()`
####Parameters
-`id`: The timer id (0-6).
####Parameters
none
####Returns
True if the timer was stopped, false on error.
####Returns
the current value of the system counter
####Example
####Example
```lua
ifnottmr.stop(2)thenprint("timer 2 not stopped, not registered?")end
print(tmr.now())
print(tmr.now())
```
####See also
-`tmr.register()`
-`tmr.stop()`
-`tmr.unregister()`
___
## tmr.interval()
## tmr.register()
Changes a registered timer's expiry interval.
Configures a timer and registers the callback function to call on expiry.
####Syntax
`tmr.interval(id, interval_ms)`
To free up the resources with this timer when done using it, call [`tmr.unregister()`](#tmrunregister) on it. For one-shot timers this is not necessary, unless they were stopped before they expired.
####Parameters
-`id`: The timer id (0-6).
-`interval_ms`: new timer interval in milliseconds.
#### Syntax
`tmr.register(id, interval_ms, mode, func)`
#### Parameters
-`id` timer id (0-6)
-`interval_ms` timer interval in milliseconds. Maximum value is 12884901. In SDKs <=1.5.0values>6871948 result in incorrect behaviour.
-`mode` timer mode:
-`tmr.ALARM_SINGLE` a one-shot alarm (and no need to call [`tmr.unregister()`](#tmrunregister))
-`tmr.ALARM_SEMI` manually repeating alarm (call [`tmr.start()`](#tmrunregister) to restart)
This is a convenience function combining `tmr.register()` and `tmr.start()` into a single call.
## tmr.start()
Starts or restarts a previously configured timer.
To free up the resources with this timer when done using it, call `tmr.unregister()` on it. For one-shot timers this is not necessary, unless they were stopped before they expired.
#### Syntax
`tmr.start(id)`
####Parameters
-`id`: The timer id (0-6).
-`interval_ms`: timer interval in milliseconds.
-`mode`: timer mode:
-`tmr.ALARM_SINGLE`: a one-shot alarm (and no need to call `tmr.unregister()`)
-`tmr.ALARM_SEMI`: manually repeating alarm (call `tmr.start()` to restart)
Busyloops the processor for a specified number of microseconds.
## tmr.unregister()
This is in general a **bad** idea, because nothing else gets to run, and the
networking stack (and other things) can fall over as a result. The only time `tmr.delay()` may be appropriate to use is if dealing with a peripheral device which needs a (very) brief delay between commands, or similar. *Use with caution!*
Stops the timer (if running) and unregisters the associated callback.
Also note that the actual amount of time delayed for may be noticeably greater, both as a result of timing inaccuracies as well as interrupts which may run during this time.
This isn't necessary for one-shot timers (`tmr.ALARM_SINGLE`), as those automatically unregister themselves when fired.
####Syntax
`tmr.delay(us)`
####Syntax
`tmr.unregister(id)`
####Parameters
-`us`: microseconds to busyloop for.
####Parameters
`id` timer id (0-6)
####Returns
####Returns
`nil`
####Example
####Example
```lua
tmr.delay(100)
tmr.unregister(0)
```
___
#### See also
[`tmr.register()`](#tmrregister)
## tmr.wdclr()
Feed the system watchdog.
...
...
@@ -272,17 +263,11 @@ Feed the system watchdog.
The event-driven model of NodeMCU means that there is no need to be sitting in hard loops waiting for things to occur. Rather, simply use the callbacks to get notified when somethings happens. With this approach, there should never be a need to manually feed the system watchdog.
The uart module allows configuration of and communication over the uart serial port.
# UART Module
The [UART](https://en.wikipedia.org/wiki/Universal_asynchronous_receiver/transmitter)(Universal asynchronous receiver/transmitter) module allows configuration of and communication over the UART serial port.
## uart.setup()
(Re-)configures the communication parameters of the UART.
-`method`: "data", data has been received on the uart
-`number/end_char`:
- if pass in a number n<255, the callback will called when n chars are received.
- if n=0, will receive every char in buffer.
- if pass in a one char string "c", the callback will called when "c" is encounterd, or max n=255 received.
-`function`: callback function, event "data" has a callback like this: function(data) end
-`run_input`: 0 or 1; If 0, input from uart will not go into Lua interpreter, can accept binary data. If 1, input from uart will go into Lua interpreter, and run.
####Parameters
-`method` "data", data has been received on the UART
-`number/end_char`
- if pass in a number n<255, the callback will called when n chars are received.
- if n=0, will receive every char in buffer.
- if pass in a one char string "c", the callback will called when "c" is encounterd, or max n=255 received.
-`function` callback function, event "data" has a callback like this: `function(data) end`
-`run_input` 0 or 1. If 0, input from UART will not go into Lua interpreter, can accept binary data. If 1, input from UART will go into Lua interpreter, and run.
To unregister the callback, provide only the "data" parameter.
####Returns
####Returns
`nil`
####Example
####Example
```lua
-- when 4 chars is received.
uart.on("data",4,
...
...
@@ -86,5 +43,47 @@ uart.on("data", "\r",
end
end,0)
```
___
## uart.setup()
(Re-)configures the communication parameters of the UART.