955871704ab3c3a2333157892db59c3d62e65942
[platform/upstream/iotjs.git] / test / run_pass / test_adc.js
1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16
17 var Adc = require('adc');
18 var assert = require('assert');
19 var adc = new Adc();
20 var configuration = {};
21
22 if (process.platform === 'linux') {
23   configuration.device =
24     '/sys/devices/12d10000.adc/iio:device0/in_voltage0_raw';
25 } else if (process.platform === 'nuttx') {
26   configuration.pin = require('stm32f4dis').pin.ADC1_3;
27 } else {
28   assert.fail();
29 }
30
31 var adc0 = adc.open(configuration, function(err) {
32   console.log('ADC initialized');
33
34   if (err) {
35     assert.fail();
36   }
37
38   test1();
39 });
40
41 // read async test
42 function test1() {
43   var loopCnt = 5;
44
45   console.log('test1 start(read async test)');
46   var test1Loop = setInterval(function() {
47     if (--loopCnt < 0) {
48       console.log('test1 complete');
49       clearInterval(test1Loop);
50       test2();
51     } else {
52       adc0.read(function(err, value) {
53         if (err) {
54           console.log('read failed');
55           assert.fail();
56         }
57
58         console.log(value);
59       });
60     }
61   }, 1000);
62 }
63
64 // read sync test
65 function test2() {
66   var loopCnt = 5,
67       value = -1;
68
69   console.log('test2 start(read sync test)');
70   var test2Loop = setInterval(function() {
71     if (--loopCnt < 0) {
72       console.log('test2 complete');
73       clearInterval(test2Loop);
74       adc0.close();
75     } else {
76       value = adc0.readSync();
77       if (value < 0) {
78         console.log('read failed');
79         assert.fail();
80       } else {
81         console.log(value);
82       }
83     }
84   }, 1000);
85 }