Update Iot.js
[platform/upstream/iotjs.git] / test / run_pass / test_gpio2.js
1 /* Copyright 2015-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 assert = require('assert');
18 var Gpio = require('gpio');
19 var gpio = new Gpio();
20
21 var ledGpio = null, switchGpio = null;
22 var ledPin, switchPin, ledMode;
23
24 var SWITCH_ON = false,
25     LED_ON = true,
26     LED_OFF = false;
27
28 var loopCnt = 0;
29
30 if (process.platform === 'linux') {
31   ledPin = 10;
32   switchPin = 9;
33   ledMode = gpio.MODE.NONE;
34 } else if (process.platform === 'nuttx') {
35   var pin = require('stm32f4dis').pin;
36   ledPin = pin.PA10;
37   switchPin = pin.PA8;
38   ledMode = gpio.MODE.PUSHPULL;
39 } else {
40   assert.fail();
41 }
42
43 ledGpio = gpio.open({
44   pin: ledPin,
45   direction: gpio.DIRECTION.OUT,
46   mode: ledMode
47 }, function() {
48   this.writeSync(LED_OFF);
49 });
50
51 switchGpio = gpio.open({
52   pin: switchPin,
53   direction: gpio.DIRECTION.IN
54 });
55
56 var loop = setInterval(function() {
57   if (!ledGpio || !switchGpio) {
58     return;
59   }
60
61   if ((++loopCnt) == 10) {
62     clearInterval(loop);
63   }
64
65   switchGpio.read(function(err, value) {
66     if (err) {
67       clearInterval(loop);
68       assert.fail();
69     }
70
71     if (value === SWITCH_ON) {
72       console.log('led on');
73       ledGpio.writeSync(LED_ON);
74     } else {
75       ledGpio.writeSync(LED_OFF);
76     }
77   });
78 }, 500);