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.
Only one file can be open at any given time.
## file.fsinfo()
Return size information for the file system, in bytes.
## file.close()
####Syntax
`file.fsinfo()`
Closes the open file, if any.
####Parameters
`nil`
#### Syntax
`file.close()`
####Returns
- `remaining` (number)
- `used` (number)
- `total` (number)
#### Parameters
none
####Example
#### Returns
`nil`
#### Example
```lua
-- get file system info
remaining, used, total=file.fsinfo()
print("\nFile system info:\nTotal : "..total.." Bytes\nUsed : "..used.." Bytes\nRemain: "..remaining.." Bytes\n")
```
-- open 'init.lua', print the first line.
file.open("init.lua", "r")
print(file.readline())
file.close()
```
___
## file.format()
#### See also
[`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
`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();
for k,v in pairs(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.
###Syntax
`file.remove(filename)`
#### Syntax
`file.fsinfo()`
####Parameters
- `filename`: file to remove
#### Parameters
none
####Returns
`nil`
#### Returns
- `remaining` (number)
- `used` (number)
- `total` (number)
####Example
#### Example
```lua
-- remove "foo.lua" from file system.
file.remove("foo.lua")
-- get file system info
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
`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();
for k,v in pairs(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.
#### Parameters
`string` content to be write to file
####Returns
The resulting file position, or `nil` on error.
#### Returns
`true` if write ok, `nil` on error
####Example
#### Example
```lua
file.open("init.lua", "r")
-- skip the first 5 bytes of the file
file.seek("set", 5)
print(file.readline())
file.close()
-- 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()
```
####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.
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.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
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.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
`nil`
#### Returns
`true` if the timer was started, `false` on error
####Example
#### Example
```lua
tmr.register(0, 5000, tmr.ALARM_SINGLE, function() print("hey there") end)
tmr.start(0)
if not tmr.alarm(0, 5000, tmr.ALARM_SINGLE, function() print("hey there") end) then print("whoopsie") end
```
####See also
- `tmr.alarm()`
#### See also
- [`tmr.register()`](#tmrregister)
- [`tmr.start()`](#tmrstart)
- [`tmr.unregister()`](#tmrunregister)
___
## tmr.unregister()
## tmr.delay()
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.0 values >6871948 result in incorrect behaviour.
####Returns
True if the timer was started, false on error.
#### Returns
`nil`
####Example
#### Example
```lua
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.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
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.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.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`
####Example
#### Example
```lua
tmr.register(0, 5000, tmr.ALARM_SINGLE, function() print("hey there") end)
tmr.interval(0, 3000) -- actually, 3 seconds is better!
tmr.start(0)
```
___
## tmr.state()
#### See also
[`tmr.alarm()`](#tmralarm)
Checks the state of a timer.
## tmr.softwd()
####Syntax
`tmr.state(id)`
Provides a simple software watchdog, which needs to be re-armed or disabled before it expires, or the system will be restarted.
####Parameters
- `id`: The timer id (0-6).
#### Syntax
`tmr.softwd(timeout_s)`
####Returns
(bool, int) or nil
#### Parameters
`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
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
- `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)
- `tmr.ALARM_AUTO`: automatically repeating alarm
#### Parameters
`id` timer id (0-6)
####Returns
True if the timer was started, false on error.
#### Returns
`true` if the timer was started, `false` on error
####Example
#### Example
```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
- `tmr.register()`
- `tmr.start()`
- `tmr.unregister()`
#### See also
- [`tmr.register()`](#tmrregister)
- [`tmr.stop()`](#tmrstop)
- [`tmr.unregister()`](#tmrunregister)
___
## tmr.now()
## tmr.state()
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
`tmr.now()`
#### Syntax
`tmr.state(id)`
####Parameters
`nil`
#### Parameters
`id` timer id (0-6)
#### Returns
(bool, int) or `nil`
####Returns
The current value of the system counter.
If the specified timer is registered, returns whether it is currently started and its mode. If the timer is not registered, `nil` is returned.
####Example
#### Example
```lua
print(tmr.now())
print(tmr.now())
running, mode = tmr.state(0)
```
___
## tmr.time()
Returns the system uptime, in seconds. Limited to 31 bits, after that it wraps around back to zero.
## tmr.stop()
####Syntax
`tmr.time()`
Stops a running timer, but does *not* unregister it. A stopped timer can be restarted with [`tmr.start()`](#tmrstart).
####Parameter
`nil`
#### Syntax
`tmr.stop(id)`
####Returns
The system uptime, in seconds, possibly wrapped around.
#### Parameters
`id` timer id (0-6)
####Example
#### Returns
`true` if the timer was stopped, `false` on error
#### Example
```lua
print("Uptime (probably):", tmr.time())
if not tmr.stop(2) then print("timer 2 not stopped, not registered?") end
```
___
## tmr.softwd()
#### See also
- [`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
`tmr.softwd(timeout_s)`
Returns the system uptime, in seconds. Limited to 31 bits, after that it wraps around back to zero.
####Parameters
- `timeout_s`: watchdog timeout, in seconds. To disable the watchdog, use -1 (or any other negative value).
#### Syntax
`tmr.time()`
####Returns
`nil`
#### Parameters
none
####Example
```lua
function on_success_callback()
tmr.softwd(-1)
print("Complex task done, soft watchdog disabled!")
end
#### Returns
the system uptime, in seconds, possibly wrapped around
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)
#### Example
```lua
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
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.
####Syntax
#### Syntax
`tmr.wdclr()`
####Parameters
`nil`
####Returns
`nil`
#### Parameters
none
####Example
```lua
tmr.wdclr()
```
___
#### Returns
`nil`
\ No newline at end of file
# uart Module
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.
####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()
Sets the callback function to handle uart events.
Sets the callback function to handle UART events.
Currently only the "data" event is supported.
####Syntax
#### Syntax
`uart.on(method, [number/end_char], [function], [run_input])`
####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.
#### 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.
#### 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