Upload packaging folder
[platform/upstream/iotjs.git] / docs / api / IoT.js-API-ADC.md
1 ### Platform Support
2
3 The following shows ADC module APIs available for each platform.
4
5 |  | Linux<br/>(Ubuntu) | Raspbian<br/>(Raspberry Pi) | Nuttx<br/>(STM32F4-Discovery) |
6 | :---: | :---: | :---: | :---: |
7 | adc.open | O | O | O |
8 | adcpin.read | O | O | O |
9 | adcpin.readSync | O | O | O |
10 | adcpin.close | O | O | O |
11 | adcpin.closeSync | O | O | O |
12
13
14 ## Contents
15 * [Pin](#pin)
16 * [ADC](#adc)
17   * [Constructor](#adc-constructor)
18     * [`new ADC()`](#adc-new)
19   * [Prototype methods](#adc-prototype-methods)
20     * [`adc.open(configuration[, callback])`](#adc-open)
21 * [ADCPin](#adcpin)
22   * [Prototype methods](#adcpin-prototype-methods)
23     * [`adcpin.read([callback])`](#adcpin-read)
24     * [`adcpin.readSync()`](#adcpin-read-sync)
25     * [`adcpin.close([callback])`](#adcpin-close)
26     * [`adcpin.closeSync()`](#adcpin-close-sync)
27
28 ## `pin` <a name="pin"></a>
29 On Nuttx, you have to know pin number that is defined in target board module. For more module information, please see below list.
30   * [STM32F4-discovery](../../targets/nuttx-stm32f4/Stm32f4dis.md#adc-pin)
31
32 ## Class: ADC <a name="adc"></a>
33
34
35 ## Constructor <a name="adc-constructor"></a>
36
37
38 ### `new ADC()` <a name="adc-new"></a>
39
40 Returns a new ADC object which can open ADC pin.
41
42
43 ## Prototype methods <a name="adc-prototype-methods"></a>
44
45
46 ### `adc.open(configuration[, callback])` <a name="adc-open"></a>
47 * `configuration <Object>`
48   * `device <String>`, mandatory configuration on Linux
49   * `pin <Number>`, mandatory configuration on Nuttx
50 * `callback <Function(err: Error | null)>`
51 * Returns: `<ADCPin>`
52
53 Opens ADC pin with the specified configuration.
54
55 **Example**
56 ```js
57 var Adc = require('adc');
58 var adc = new Adc();
59 var adc0 = adc.open({
60   device: '/sys/devices/12d10000.adc/iio:device0/in_voltage0_raw'
61 }, function(err) {
62   if (err) {
63     throw err;
64   }
65 });
66 ```
67
68
69 ## Class: ADCPin <a name="adcpin"></a>
70
71
72 ## Prototype methods <a name="adcpin-prototype-methods"></a>
73
74
75 ### `adcpin.read([callback])` <a name="adcpin-read"></a>
76 * `callback <Function(err: Error | null, value: Number)>`
77
78 Reads the analog value from pin asynchronously.
79
80 `callback` will be called after read the analog value.
81
82 **Example**
83 ```js
84 adc0.read(function(err, value) {
85   if (err) {
86     throw err;
87   }
88   console.log('value:', value);
89 });
90 ```
91
92
93 ### `adcpin.readSync()` <a name="adcpin-read-sync"></a>
94 * Return: `<Number>`, analog value
95
96 Reads the analog value from pin synchronously.
97
98 **Example**
99 ```js
100 var value = adc0.readSync();
101 console.log('value:', value);
102 ```
103
104
105 ### `adcpin.close([callback])` <a name="adcpin-close"></a>
106 * `callback <Function(err: Error | null)>`
107
108 Closes ADC pin asynchronously. This function must be called after the work of ADC finished.
109
110 `callback` will be called after ADC device is released.
111
112 **Example**
113 ```js
114 adc0.clsoe(function(err) {
115   if (err) {
116     throw err;
117   }
118 });
119 ```
120
121
122 ### `adcpin.closeSync()` <a name="adcpin-close-sync"></a>
123
124 Closes ADC pin synchronously. This function must be called after the work of ADC finished.
125
126 **Example**
127 ```js
128 adc0.clsoeSync();
129 console.log('adc pin is closed');
130 ```