You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 5, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: board/aio.md
+39-38
Original file line number
Diff line number
Diff line change
@@ -3,22 +3,42 @@ AIO API
3
3
4
4
The AIO API supports reading analog input pins that measure the analog voltage signal between 0 and a maximum voltage (usually 3.3 or 5 Volts), then do Analog-to-Digital Conversion (ADC) with a resolution of 10 or 12 bits on most boards, so that the result (pin value) is 0 to 1023 or 0 to 4095, inclusively.
5
5
6
-
On some boards access to AIO may be asynchronous. This API uses synchronous read.
6
+
On some boards access to AIO may be asynchronous. This API uses synchronous methods.
7
7
8
-
The API object
9
-
--------------
10
-
AIO functionality is exposed by the [`AIO`](#aio) object that can be obtained by using the [`aio()`](./README.md/#aio) method of the [`Board` API](./README.md/#board). See also the [Web IDL](./webidl.md).
8
+
<aname="apiobject"></a>
9
+
### The AIO API object
10
+
AIO functionality is exposed by an object that can be obtained by using the [`aio()`](./README.md/#aio) method of the [`Board` API](./README.md/#board). See also the [Web IDL](./webidl.md). The API object exposes the following method:
11
+
12
+
| Method | Description |
13
+
| --- | --- |
14
+
|[`open()`](#open)| synchronous open |
15
+
16
+
<aname="open"></a>
17
+
#### The `AIO open(options)` method
18
+
Configures an AIO pin using data provided by the `options` argument. It involves the following steps:
19
+
- If `options` is a string, create a dictionary 'init' and use the value of `options` to initialize the `init.pin` property.
20
+
- Otherwise if `options` is a number, create a dictionary 'init' and use the value of `options` to initialize the `init.pin` property.
21
+
- Otherwise if `options` is a dictionary, let `init` be `options`. It may contain the following [`AIO`](#aio) properties, where at least `pin` MUST be specified:
22
+
*`pin` for board pin name with the valid values defined by the board, or for the numeric index of the analog pin;
23
+
*`precision` for the bit width of a sample (if the board supports setting the sampling rate).
24
+
- If any property of `init` is specified and has an invalid value on the given board, as defined by the board documentation, throw `TypeError`.
25
+
- Request the underlying platform to initialize AIO on `init.pin` (if defined) or otherwise `init.channel`.
26
+
- In case of failure, throw `InvalidAccessError`.
27
+
- Let `aio` be the `AIOPin`](#aiopin) object that represents the hardware pin identified by `init.pin`, as defined by the board documentation.
28
+
- If `init.precision` is defined, request the board to set the precision and initialize the `aio.precision` property with the value supported by the board. If there is an error, throw `InvalidAccessError`.
29
+
- Initialize the `aio.value` property with `undefined`.
30
+
- Return the `aio` object.
11
31
12
32
<aname="aio"></a>
13
33
### The `AIO` interface
14
-
Represents the properties and methods that expose AIO functionality. The `AIO` object implements the [`EventEmitter`](../README.md/#events) interface, and extends the [`Pin`](./README.md/#pin) object. It has the following properties and methods:
34
+
The `AIO` object extends the [`Pin`](./README.md/#pin) object.
15
35
16
36
| Property | Type | Optional | Default value | Represents |
17
37
| --- | --- | --- | --- | --- |
18
38
|`pin`| String or Number | no |`undefined`| board name for the pin |
19
39
|`precision`| unsigned long | yes |`undefined`| bit length of digital sample |
20
40
21
-
| Method signature| Description |
41
+
| Method | Description |
22
42
| --- | --- |
23
43
|[`read()`](#read)| synchronous read |
24
44
|[`close()`](#close)| close the pin |
@@ -27,52 +47,33 @@ The `pin` property inherited from [`Pin`](./README.md/#pin) can take values defi
27
47
28
48
The `precision` property represents the bit length of the digital sample. It is usually 10 or 12 bits, depending on board.
29
49
30
-
<aname="init"></a>
31
-
#### AIO initialization
32
-
This internal algorithm is used by the [`Board.aio()`](./README.md/#aio) method. Configures the AIO pin provided by the `options` argument. It involves the following steps:
33
-
- If `options` is a string, create a dictionary 'init' and use the value of `options` to initialize the `init.pin` property.
34
-
- Otherwise if `options` is a number, create a dictionary 'init' and use the value of `options` to initialize the `init.pin` property.
35
-
- Otherwise if `options` is a dictionary, let `init` be `options`. It may contain the following [`AIO`](#aio) properties, where at least `pin` MUST be specified:
36
-
*`pin` for board pin name with the valid values defined by the board, or for the numeric index of the analog pin;
37
-
*`precision` for the bit width of a sample (if the board supports setting the sampling rate).
38
-
- If any property of `init` is specified and has an invalid value on the given board, as defined by the board documentation, throw `TypeError`.
39
-
- Request the underlying platform to initialize AIO on `init.pin` (if defined) or otherwise `init.channel`.
40
-
- In case of failure, throw `InvalidAccessError`.
41
-
- Let `aio` be the `AIO`](#aio) object that represents the hardware pin identified by `init.pin`, as defined by the board documentation.
42
-
- If `init.precision` is defined, request the board to set the precision and initialize the `aio.precision` property with the value supported by the board. If there is an error, throw `InvalidAccessError`.
43
-
- Initialize the `aio.value` property with `undefined`.
44
-
- Return the `aio` object.
45
-
46
50
<aname="read"></a>
47
51
#### The `unsigned long read()` method
48
52
Performs a synchronous read operation for the pin value. It returns the pin value representing the last sampling.
49
53
50
54
<aname="close"></a>
51
55
#### The `close()` method
52
-
Called when the application is no longer interested in the pin. Until the next [initialization](#init), invoking the `read()` method SHOULD throw`InvalidAccessError`.
56
+
Called when the application is no longer interested in the pin. Until the next [open()](#open) is called, invoking the `read()` method throws`InvalidAccessError`.
53
57
54
58
### Examples
55
59
```javascript
56
-
var board =require("board");
60
+
try {
61
+
var aio =require("board").aio(); // initialize AIO on the board
0 commit comments