Commit 7eba3cbc authored by Marcel Stör's avatar Marcel Stör
Browse files

Update for consistency with the other docs

parent 8d38cef0
...@@ -5,347 +5,327 @@ The file system is a flat file system, with no notion of directories/folders. ...@@ -5,347 +5,327 @@ The file system is a flat file system, with no notion of directories/folders.
Only one file can be open at any given time. Only one file can be open at any given time.
## file.fsinfo() ## file.close()
Return size information for the file system, in bytes.
####Syntax Closes the open file, if any.
`file.fsinfo()`
####Parameters #### Syntax
`nil` `file.close()`
####Returns #### Parameters
- `remaining` (number) none
- `used` (number)
- `total` (number)
####Example #### Returns
`nil`
#### Example
```lua ```lua
-- get file system info -- open 'init.lua', print the first line.
remaining, used, total=file.fsinfo() file.open("init.lua", "r")
print("\nFile system info:\nTotal : "..total.." Bytes\nUsed : "..used.." Bytes\nRemain: "..remaining.." Bytes\n") print(file.readline())
``` file.close()
``` ```
___ #### See also
## file.format() [`file.open()`](#fileopen)
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();
for k,v in pairs(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.
###Syntax #### Syntax
`file.remove(filename)` `file.fsinfo()`
####Parameters #### Parameters
- `filename`: file to remove none
####Returns #### Returns
`nil` - `remaining` (number)
- `used` (number)
- `total` (number)
####Example #### Example
```lua ```lua
-- remove "foo.lua" from file system. -- get file system info
file.remove("foo.lua") remaining, used, total=file.fsinfo()
print("\nFile system info:\nTotal : "..total.." Bytes\nUsed : "..used.." Bytes\nRemain: "..remaining.." Bytes\n")
``` ```
####See also
- `file.open()`
___
## file.rename()
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") for k,v in pairs(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. #### Parameters
`string` content to be write to file
####Returns #### Returns
The resulting file position, or `nil` on error. `true` if write ok, `nil` on error
####Example #### Example
```lua ```lua
file.open("init.lua", "r") -- open 'init.lua' in 'a+' mode
-- skip the first 5 bytes of the file file.open("init.lua", "a+")
file.seek("set", 5) -- write 'foo bar' to the end of the file
print(file.readline()) file.writeline('foo bar')
file.close() file.close()
``` ```
####See also
- `file.open()`
___ #### See also
- [`file.open()`](#fileopen)
- [`file.readline()`](#filereadline)
\ No newline at end of file
...@@ -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.0 values >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.0 values >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)
- `tmr.ALARM_AUTO` automatically repeating alarm
####Returns #### Returns
`nil` `true` if the timer was started, `false` on error
####Example #### Example
```lua ```lua
tmr.register(0, 5000, tmr.ALARM_SINGLE, function() print("hey there") end) if not tmr.alarm(0, 5000, tmr.ALARM_SINGLE, function() print("hey there") end) then print("whoopsie") end
tmr.start(0)
``` ```
####See also #### See also
- `tmr.alarm()` - [`tmr.register()`](#tmrregister)
- [`tmr.start()`](#tmrstart)
- [`tmr.unregister()`](#tmrunregister)
___ ## tmr.delay()
## tmr.unregister()
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.0 values >6871948 result in incorrect behaviour.
####Returns #### Returns
True if the timer was started, false on error. `nil`
####Example #### Example
```lua ```lua
tmr.register(0, 5000, tmr.ALARM_SINGLE, function() print("hey there") end) tmr.register(0, 5000, tmr.ALARM_SINGLE, function() print("hey there") end)
if not tmr.start(0) then print("uh oh") end 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
if not tmr.stop(2) then print("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.0 values >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)
- `tmr.ALARM_AUTO` automatically repeating alarm
Note that registering does *not* start the alarm.
####Returns #### Returns
`nil` `nil`
####Example #### Example
```lua ```lua
tmr.register(0, 5000, tmr.ALARM_SINGLE, function() print("hey there") end) tmr.register(0, 5000, tmr.ALARM_SINGLE, function() print("hey there") end)
tmr.interval(0, 3000) -- actually, 3 seconds is better! tmr.start(0)
``` ```
___ #### See also
## tmr.state() [`tmr.alarm()`](#tmralarm)
Checks the state of a timer. ## tmr.softwd()
####Syntax Provides a simple software watchdog, which needs to be re-armed or disabled before it expires, or the system will be restarted.
`tmr.state(id)`
####Parameters #### Syntax
- `id`: The timer id (0-6). `tmr.softwd(timeout_s)`
####Returns #### Parameters
(bool, int) or nil `timeout_s` watchdog timeout, in seconds. To disable the watchdog, use -1 (or any other negative value).
If the specified timer is registered, returns whether it is currently started and its mode. If the timer is not registered, `nil` is returned. #### Returns
`nil`
####Example #### Example
```lua ```lua
running, mode = tmr.state(0) function on_success_callback()
tmr.softwd(-1)
print("Complex task done, soft watchdog disabled!")
end
tmr.softwd(5)
-- go off and attempt to do whatever might need a restart to recover from
complex_stuff_which_might_never_call_the_callback(on_success_callback)
``` ```
___
## tmr.alarm()
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)
- `tmr.ALARM_AUTO`: automatically repeating alarm
####Returns #### Returns
True if the timer was started, false on error. `true` if the timer was started, `false` on error
####Example #### Example
```lua ```lua
if not tmr.alarm(0, 5000, tmr.ALARM_SINGLE, function() print("hey there") end) then print("whoopsie") end tmr.register(0, 5000, tmr.ALARM_SINGLE, function() print("hey there") end)
if not tmr.start(0) then print("uh oh") end
``` ```
####See also #### See also
- `tmr.register()` - [`tmr.register()`](#tmrregister)
- `tmr.start()` - [`tmr.stop()`](#tmrstop)
- `tmr.unregister()` - [`tmr.unregister()`](#tmrunregister)
___ ## tmr.state()
## tmr.now()
Returns the system counter, which counts in microseconds. Limited to 31 bits, after that it wraps around back to zero. Checks the state of a timer.
####Syntax #### Syntax
`tmr.now()` `tmr.state(id)`
####Parameters #### Parameters
`nil` `id` timer id (0-6)
#### Returns
(bool, int) or `nil`
####Returns If the specified timer is registered, returns whether it is currently started and its mode. If the timer is not registered, `nil` is returned.
The current value of the system counter.
####Example #### Example
```lua ```lua
print(tmr.now()) running, mode = tmr.state(0)
print(tmr.now())
``` ```
___
## tmr.time()
Returns the system uptime, in seconds. Limited to 31 bits, after that it wraps around back to zero. ## tmr.stop()
####Syntax Stops a running timer, but does *not* unregister it. A stopped timer can be restarted with [`tmr.start()`](#tmrstart).
`tmr.time()`
####Parameter #### Syntax
`nil` `tmr.stop(id)`
####Returns #### Parameters
The system uptime, in seconds, possibly wrapped around. `id` timer id (0-6)
####Example #### Returns
`true` if the timer was stopped, `false` on error
#### Example
```lua ```lua
print("Uptime (probably):", tmr.time()) if not tmr.stop(2) then print("timer 2 not stopped, not registered?") end
``` ```
___ #### See also
## tmr.softwd() - [`tmr.register()`](#tmrregister)
- [`tmr.stop()`](#tmrstop)
- [`tmr.unregister()`](#tmrunregister)
Provides a simple software watchdog, which needs to be re-armed or disabled before it expires, or the system will be restarted. ## tmr.time()
####Syntax Returns the system uptime, in seconds. Limited to 31 bits, after that it wraps around back to zero.
`tmr.softwd(timeout_s)`
####Parameters #### Syntax
- `timeout_s`: watchdog timeout, in seconds. To disable the watchdog, use -1 (or any other negative value). `tmr.time()`
####Returns #### Parameters
`nil` none
####Example #### Returns
```lua the system uptime, in seconds, possibly wrapped around
function on_success_callback()
tmr.softwd(-1)
print("Complex task done, soft watchdog disabled!")
end
tmr.softwd(5) #### Example
-- go off and attempt to do whatever might need a restart to recover from ```lua
complex_stuff_which_might_never_call_the_callback(on_success_callback) print("Uptime (probably):", tmr.time())
``` ```
___
## tmr.delay()
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.
####Syntax #### Syntax
`tmr.wdclr()` `tmr.wdclr()`
####Parameters #### Parameters
`nil` none
####Returns
`nil`
####Example #### Returns
```lua `nil`
tmr.wdclr() \ No newline at end of file
```
___
# uart Module # UART Module
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.
####Syntax
`uart.setup(id, baud, databits, parity, stopbits, echo)`
####Parameters
- `id`: Always zero, only one uart supported.
- `baud`: One of 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 460800, 921600, 1843200, 2686400.
- `databits`: One of 5, 6, 7, 8.
- `parity`: uart.PARITY_NONE, uart.PARITY_ODD, or uart.PARITY_EVEN.
- `stopbits`: uart.STOPBITS_1, uart.STOPBITS_1_5, or uart.STOPBITS_2.
- `echo`: if zero, disable echo, otherwise enable echo.
####Returns
number:configured baud rate
####Example
```lua
-- configure for 9600, 8N1, with echo
uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)
```
___
## uart.write()
Write string or byte to the uart.
####Syntax
uart.write(id, data1 [, data2, ...])
####Parameters
- `id`: Always zero, only one uart supported.
- `data1`...: String or byte to send via uart.
####Returns
`nil`
####Example
```lua
uart.write(0, "Hello, world\n")
```
___
## uart.on() ## uart.on()
Sets the callback function to handle uart events. Sets the callback function to handle UART events.
Currently only the "data" event is supported. Currently only the "data" event is supported.
####Syntax #### Syntax
`uart.on(method, [number/end_char], [function], [run_input])` `uart.on(method, [number/end_char], [function], [run_input])`
####Parameters #### Parameters
- `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.
#### Syntax
`uart.setup(id, baud, databits, parity, stopbits, echo)`
#### Parameters
- `id` always zero, only one uart supported
- `baud` one of 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 460800, 921600, 1843200, 2686400
- `databits` one of 5, 6, 7, 8
- `parity` `uart.PARITY_NONE`, `uart.PARITY_ODD`, or `uart.PARITY_EVEN`
- `stopbits` `uart.STOPBITS_1`, `uart.STOPBITS_1_5`, or `uart.STOPBITS_2`
- `echo` if 0, disable echo, otherwise enable echo
#### Returns
configured baud rate (number)
#### Example
```lua
-- configure for 9600, 8N1, with echo
uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)
```
## uart.write()
Write string or byte to the UART.
#### Syntax
`uart.write(id, data1 [, data2, ...])`
#### Parameters
- `id` always 0, only one UART supported
- `data1`... string or byte to send via UART
#### Returns
`nil`
#### Example
```lua
uart.write(0, "Hello, world\n")
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment