Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 89db4e8

Browse files
committed
Board: make API synchronous
Signed-off-by: Zoltan Kis <[email protected]>
1 parent dcd617e commit 89db4e8

File tree

8 files changed

+335
-339
lines changed

8 files changed

+335
-339
lines changed

board/README.md

+31-45
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Represents a hardware board.
5757
| [`name`](#name) | String | no | `undefined` | board name |
5858
| [`version`](#version) | String | no | `versions.board` in [`package.json`](../package.json) | API version |
5959

60-
| Method signature | Description |
60+
| Method | Description |
6161
| --- | --- |
6262
| [`aio()`](#aio) | request an AIO object |
6363
| [`gpio()`](#gpio) | request a GPIO object |
@@ -85,57 +85,43 @@ Board errors are represented as augmented [`Error`](https://nodejs.org/api/error
8585
#### `Board` methods
8686

8787
<a name="aio"></a>
88-
##### The `aio(options)` method
89-
Configures an AIO pin. The method runs the following steps:
90-
- Return a [`Promise`](../README.md/#promise) object `promise` and continue [in parallel](https://html.spec.whatwg.org/#in-parallel).
91-
- If the AIO functionality is not supported, reject `promise` with `"NotSupportedError"`.
92-
- Run the internal [`AIO initialization`](./aio.md/#init) algorithm with `options` as argument and let `aio` be the returned result.
93-
- If it throws an error, reject promise with that error.
94-
- Resolve `promise` with the `aio` object.
88+
##### The `aio()` method
89+
Provides the AIO API object. The method runs the following steps:
90+
- If the AIO functionality is not supported on the board, throw `"NotSupportedError"`.
91+
- Initialize AIO functionality on the board. If it fails, throw `"SystemError"`.
92+
- Let `aio` be the [AIO API object](./aio.md/#apiobject). Return `aio`.
9593

9694
<a name="gpio"></a>
97-
##### The `gpio(options)` method
98-
Configures a GPIO pin or GPIO port. The method runs the following steps:
99-
- Return a [`Promise`](../README.md/#promise) object `promise` and continue [in parallel](https://html.spec.whatwg.org/#in-parallel).
100-
- If the GPIO functionality is not supported, reject `promise` with `"NotSupportedError"`.
101-
- Run the internal [`GPIO initialization`](./gpio.md/#init) algorithm with `options` as argument and let `gpio` be the returned result.
102-
- If it throws an error, reject promise with that error.
103-
- Resolve `promise` with the `gpio` object.
95+
##### The `gpio()` method
96+
Provides the GPIO API object. The method runs the following steps:
97+
- If the GPIO functionality is not supported on the board, throw `"NotSupportedError"`.
98+
- Initialize GPIO functionality on the board. If it fails, throw `"SystemError"`.
99+
- Let `gpio` be the [GPIO API object](./gpio.md/#apiobject). Return `gpio`.
104100

105101
<a name="pwm"></a>
106-
##### The `pwm(options)` method
107-
Configures a PWM pin. The method runs the following steps:
108-
- Return a [`Promise`](../README.md/#promise) object `promise` and continue [in parallel](https://html.spec.whatwg.org/#in-parallel).
109-
- If the PWM functionality is not supported, reject `promise` with `"NotSupportedError"`.
110-
- Run the internal [`PWM initialization`](./pwm.md/#init) algorithm with `options` as argument and let `pwm` be the returned result.
111-
- If it throws an error, reject promise with that error.
112-
- Resolve `promise` with the `pwm` object.
102+
##### The `pwm()` method
103+
Provides the PWM API object. The method runs the following steps:
104+
- If the PWM functionality is not supported on the board, throw `"NotSupportedError"`.
105+
- Initialize PWM functionality on the board. If it fails, throw `"SystemError"`.
106+
- Let `pwm` be the [PWM API object`](./pwm.md/#apiobject). Return `pwm`.
113107

114108
<a name="i2c"></a>
115-
##### The `i2c(options)` method
116-
Configures I2C communication. The method runs the following steps:
117-
- Return a [`Promise`](../README.md/#promise) object `promise` and continue [in parallel](https://html.spec.whatwg.org/#in-parallel).
118-
- If the I2C functionality is not supported, reject `promise` with `"NotSupportedError"`.
119-
- Run the internal [`I2C initialization`](./i2c.md/#init) algorithm with `options` as argument and let `i2c` be the returned result.
120-
- If it throws an error, reject promise with that error.
121-
- Resolve `promise` with the `i2c` object.
109+
##### The `i2c()` method
110+
Provides the I2C API object. The method runs the following steps:
111+
- If the I2C functionality is not supported on the board, throw `"NotSupportedError"`.
112+
- Initialize I2C functionality on the board. If it fails, throw `"SystemError"`.
113+
- Let `i2c` be the [I2C API Object](./i2c.md/#apiobject). Return `pwm`.
122114

123115
<a name="spi"></a>
124-
##### The `spi(options)` method
125-
Configures SPI communication.
126-
The method runs the following steps:
127-
- Return a [`Promise`](../README.md/#promise) object `promise` and continue [in parallel](https://html.spec.whatwg.org/#in-parallel).
128-
- If the SPI functionality is not supported, reject `promise` with `"NotSupportedError"`.
129-
- Run the [`SPI init`](./spi.md/#init) steps with `options` as argument and let `spi` be the returned result.
130-
- If it throws an error, reject promise with that error.
131-
- Resolve `promise` with the `spi` object.
116+
##### The `spi()` method
117+
Provides the SPI API object. The method runs the following steps:
118+
- If the SPI functionality is not supported on the board, throw `"NotSupportedError"`.
119+
- Initialize SPI functionality on the board. If it fails, throw `"SystemError"`.
120+
- Let `spi` be the [SPI API object](./spi.md/#apiobject). Return `spi`.
132121

133122
<a name="uart"></a>
134-
##### The `uart(options)` method
135-
Configures UART communication. It takes a dictionary object as argument.
136-
The method runs the following steps:
137-
- Return a [`Promise`](../README.md/#promise) object `promise` and continue [in parallel](https://html.spec.whatwg.org/#in-parallel).
138-
- If the UART functionality is not supported, reject `promise` with `"NotSupportedError"`.
139-
- Run the [`UART init`](./uart.md/#init) steps with `options` as argument and let `uart` be the returned result.
140-
- If it throws an error, reject promise with that error.
141-
- Resolve `promise` with the `uart` object.
123+
##### The `uart()` method
124+
Provides the UART API object. The method runs the following steps:
125+
- If the UART functionality is not supported on the board, throw `"NotSupportedError"`.
126+
- Initialize UART functionality on the board. If it fails, throw `"SystemError"`.
127+
- Let `uart` be the [UART API object](./uart.md/#apiobject) object. Return `uart`.

board/aio.md

+39-38
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,42 @@ AIO API
33

44
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.
55

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.
77

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+
<a name="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+
<a name="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.
1131

1232
<a name="aio"></a>
1333
### 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.
1535

1636
| Property | Type | Optional | Default value | Represents |
1737
| --- | --- | --- | --- | --- |
1838
| `pin` | String or Number | no | `undefined` | board name for the pin |
1939
| `precision` | unsigned long | yes | `undefined` | bit length of digital sample |
2040

21-
| Method signature | Description |
41+
| Method | Description |
2242
| --- | --- |
2343
| [`read()`](#read) | synchronous read |
2444
| [`close()`](#close) | close the pin |
@@ -27,52 +47,33 @@ The `pin` property inherited from [`Pin`](./README.md/#pin) can take values defi
2747

2848
The `precision` property represents the bit length of the digital sample. It is usually 10 or 12 bits, depending on board.
2949

30-
<a name="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-
4650
<a name="read"></a>
4751
#### The `unsigned long read()` method
4852
Performs a synchronous read operation for the pin value. It returns the pin value representing the last sampling.
4953

5054
<a name="close"></a>
5155
#### 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`.
5357

5458
### Examples
5559
```javascript
56-
var board = require("board");
60+
try {
61+
var aio = require("board").aio(); // initialize AIO on the board
5762

58-
// Configure AIO using the board
59-
board.aio("A1").then(function(aio){
60-
// Read pin values.
61-
console.log(board.name + " AIO pin 1 value: " + aio.read());
63+
var a1 = aio.open("A1");
64+
console.log(board.name + " AIO pin 1 value: " + a1.read());
65+
a1.close();
6266

63-
// Release the pin.
64-
aio.close();
65-
});
67+
var a4 = aio.open({pin: "A4", precision: 12 });
6668

67-
board.aio({pin: "A4", precision: 12 })
68-
.then(function(aio){ // read 10 samples, one every second
6969
setTimeout(function() {
70-
aio.close();
70+
a4.close();
7171
}, 10500);
72+
7273
setInterval(function() {
73-
console.log("AIO pin 4 value: " + aio.read());
74+
console.log("AIO pin 4 value: " + a4.read());
7475
}, 1000);
75-
}).catch(function(err) {
76-
console.log("AIO error.");
77-
});
76+
} catch (err) {
77+
console.log("AIO error: " + err.message);
78+
};
7879
```

0 commit comments

Comments
 (0)