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
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.
`file.format()`
####Parameters
#### Syntax
`nil`
`file.flush()`
#### Parameters
none
####Returns
####Returns
`nil`
`nil`
####Example
####Example
```lua
```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.format()
-`file.remove()`
___
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.list()
Lists all files in the file system.
#### Syntax
`file.format()`
####Syntax
#### Parameters
`file.list()`
none
####Parameters
#### Returns
`nil`
`nil`
####Returns
#### See also
a lua table which contains the {file name: file size} pairs
[`file.remove()`](#fileremove)
####Example
## file.fsinfo()
```lua
l=file.list();
fork,vinpairs(l)do
print("name:"..k..", size:"..v)
end
```
___
## file.remove()
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
Lists all files in the file system.
`file.rename(oldname, newname)`
####Parameters
#### Syntax
-`oldname`: old file name
`file.list()`
-`newname`: new file name
####Returns
#### Parameters
`true` on success, `false` on error.
none
####Example
#### Returns
a lua table which contains the {file name: file size} pairs
#### Example
```lua
```lua
-- rename file 'temp.lua' to 'init.lua'.
l=file.list();
file.rename("temp.lua","init.lua")
fork,vinpairs(l)do
print("name:"..k..", size:"..v)
end
```
```
___
## file.open()
## file.open()
Opens a file for access, potentially creating it (for write modes).
Opens a file for access, potentially creating it (for write modes).
When done with the file, it must be closed using `file.close()`.
When done with the file, it must be closed using `file.close()`.
####Syntax
####Syntax
`file.open(filename, mode)`
`file.open(filename, mode)`
####Parameters
####Parameters
-`filename`: file to be opened, directories are not supported
-`filename` file to be opened, directories are not supported
-`mode`:
-`mode`:
- "r": read mode (the default)<br/>
- "r": read mode (the default)
- "w": write mode<br/>
- "w": write mode
- "a": append mode<br/>
- "a": append mode
- "r+": update mode, all previous data is preserved<br/>
- "r+": update mode, all previous data is preserved
- "w+": update mode, all previous data is erased<br/>
- "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
- "a+": append update mode, previous data is preserved, writing is only allowed at the end of file
####Returns
#### Returns
-`nil` if file not opened, or not exists (read modes). true` if file opened ok.
`nil` if file not opened, or not exists (read modes). `true` if file opened ok.
####Example
#### Example
```lua
```lua
-- open 'init.lua', print the first line.
-- open 'init.lua', print the first line.
file.open("init.lua", "r")
file.open("init.lua","r")
print(file.readline())
print(file.readline())
file.close()
file.close()
```
```
####See also
#### See also
- `file.close()`
-[`file.close()`](#fileclose)
- `file.readline()`
-[`file.readline()`](#filereadline)
___
## file.close()
Closes the open file, if any.
## file.read()
####Syntax
Read content from the open file.
`file.close()`
####Parameters
#### Syntax
`nil`
`file.read([n_or_str])`
####Returns
#### Parameters
`nil`
-`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
```lua
-- open 'init.lua', print the first line.
-- print the first line of 'init.lua'
file.open("init.lua", "r")
file.open("init.lua","r")
print(file.readline())
print(file.read('\n'))
file.close()
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()
## file.readline()
Read the next line from the open file.
Read the next line from the open file.
####Syntax
####Syntax
`file.readline()`
`file.readline()`
####Parameters
####Parameters
`nil`
none
####Returns
####Returns
File content in string, line by line, include EOL('\n'). Return `nil` when EOF.
File content in string, line by line, include EOL('\n'). Return `nil` when EOF.
####Example
#### Example
```lua
```lua
-- print the first line of 'init.lua'
-- print the first line of 'init.lua'
file.open("init.lua", "r")
file.open("init.lua","r")
print(file.readline())
print(file.readline())
file.close()
file.close()
```
```
####See also
####See also
- `file.open()`
-[`file.open()`](#fileopen)
- `file.close()`
-[`file.close()`](#fileclose)
- `file.read()`
-[`file.read()`](#filereade)
___
## file.remove()
## file.writeline()
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
###Syntax
`file.writeline(string)`
`file.remove(filename)`
####Parameters
####Parameters
- `string`: content to be write to file
`filename` file to remove
####Returns
####Returns
`true` if write ok, `nil` on error.
`nil`
####Example
####Example
```lua
```lua
-- open 'init.lua' in 'a+' mode
-- remove "foo.lua" from file system.
file.open("init.lua", "a+")
file.remove("foo.lua")
-- write 'foo bar' to the end of the file
file.writeline('foo bar')
file.close()
```
```
#### See also
[`file.open()`](#fileopen)
####See also
## file.rename()
- `file.open()`
- `file.readline()`
___
Renames a file. If a file is currently open, it will be closed first.
## file.read()
Read content from the open file.
#### Syntax
####Syntax
`file.rename(oldname, newname)`
`file.read([n_or_str])`
#### Parameters
-`oldname` old file name
-`newname` new file name
####Parameters
#### Returns
- `n_or_str`:
`true` on success, `false` on error.
- 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
#### Example
File content in string, or nil when EOF.
####Example
```lua
```lua
-- print the first line of 'init.lua'
-- rename file 'temp.lua' to 'init.lua'.
file.open("init.lua", "r")
file.rename("temp.lua","init.lua")
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.seek()
- `file.open()`
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.
- `file.readline()`
___
## file.write()
Write a string to the open file.
####Syntax
####Syntax
`file.write(string)`
`file.seek([whence [, offset]])`
####Parameters
#### Parameters
`string`: content to be write to file.
-`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
If no parameters are given, the function simply returns the current file offset.
`true` if the write is ok, `nil` on error.
####Example
#### Returns
the resulting file position, or `nil` on error
#### Example
```lua
```lua
-- open 'init.lua' in 'a+' mode
file.open("init.lua","r")
file.open("init.lua", "a+")
-- skip the first 5 bytes of the file
-- write 'foo bar' to the end of the file
file.seek("set",5)
file.write('foo bar')
print(file.readline())
file.close()
file.close()
```
```
#### See also
[`file.open()`](#fileopen)
####See also
## file.write()
- `file.open()`
- `file.writeline()`
___
## file.flush()
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
####Syntax
`file.flush()`
`file.write(string)`
####Parameters
####Parameters
`nil`
`string` content to be write to file
####Returns
####Returns
`nil`
`true` if the write is ok, `nil` on error
####Example
####Example
```lua
```lua
-- open 'init.lua' in 'a+' mode
-- open 'init.lua' in 'a+' mode
file.open("init.lua", "a+")
file.open("init.lua","a+")
-- write 'foo bar' to the end of the file
-- write 'foo bar' to the end of the file
file.write('foo bar')
file.write('foo bar')
file.flush()
file.close()
-- write 'baz' too
file.write('baz')
file.close()
```
```
####See also
- `file.close()`
___
#### See also
## file.seek()
-[`file.open()`](#fileopen)
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.
-[`file.writeline()`](#filewriteline)
####Syntax
## file.writeline()
`file.seek([whence [, offset]])`
Write a string to the open file and append '\n' at the end.
####Parameters
#### Syntax
- `whence`:
`file.writeline(string)`
- "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
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.
...
@@ -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.
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.
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
## tmr.alarm()
- 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.
####Syntax
This is a convenience function combining [`tmr.register()`](#tmrregister) and [`tmr.start()`](#tmrstart) into a single call.
`tmr.register(id, interval_ms, mode, func)`
####Parameters
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.
-`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
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
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.
`tmr.unregister(id)`
#### Syntax
`tmr.delay(us)`
####Parameters
####Parameters
-`id`: The timer id (0-6).
`us` microseconds to busyloop for
####Returns
####Returns
`nil`
`nil`
####Example
####Example
```lua
```lua
tmr.unregister(0)
tmr.delay(100)
```
```
####See also
-`tmr.register()`
___
## tmr.interval()
## tmr.start()
Starts or restarts a previously configured timer.
Changes a registered timer's expiry interval.
####Syntax
####Syntax
`tmr.start(id)`
`tmr.interval(id, interval_ms)`
####Parameters
#### Parameters
-`id`: The timer id (0-6).
-`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.now()
## tmr.stop()
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
####Syntax
`tmr.stop(id)`
`tmr.now()`
####Parameters
####Parameters
-`id`: The timer id (0-6).
none
####Returns
####Returns
True if the timer was stopped, false on error.
the current value of the system counter
####Example
####Example
```lua
```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.register()
## tmr.interval()
Changes a registered timer's expiry interval.
Configures a timer and registers the callback function to call on expiry.
####Syntax
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.
`tmr.interval(id, interval_ms)`
####Parameters
#### Syntax
-`id`: The timer id (0-6).
`tmr.register(id, interval_ms, mode, func)`
-`interval_ms`: new timer interval in milliseconds.
#### 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
#### Parameters
-`id`: The timer id (0-6).
`id` 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
Stops the timer (if running) and unregisters the associated callback.
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!*
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
####Syntax
`tmr.delay(us)`
`tmr.unregister(id)`
####Parameters
####Parameters
-`us`: microseconds to busyloop for.
`id` timer id (0-6)
####Returns
####Returns
`nil`
`nil`
####Example
####Example
```lua
```lua
tmr.delay(100)
tmr.unregister(0)
```
```
___
#### See also
[`tmr.register()`](#tmrregister)
## tmr.wdclr()
## tmr.wdclr()
Feed the system watchdog.
Feed the system watchdog.
...
@@ -272,17 +263,11 @@ 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 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.
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
-`method` "data", data has been received on the UART
-`number/end_char`:
-`number/end_char`
- if pass in a number n<255, the callback will called when n chars are received.
- 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 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.
- 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
-`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.
-`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.
To unregister the callback, provide only the "data" parameter.
####Returns
####Returns
`nil`
`nil`
####Example
####Example
```lua
```lua
-- when 4 chars is received.
-- when 4 chars is received.
uart.on("data",4,
uart.on("data",4,
...
@@ -86,5 +43,47 @@ uart.on("data", "\r",
...
@@ -86,5 +43,47 @@ uart.on("data", "\r",
end
end
end,0)
end,0)
```
```
___
## uart.setup()
(Re-)configures the communication parameters of the UART.