Update Iot.js
[platform/upstream/iotjs.git] / test / run_pass / test_gpio1.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 LED_ON = true,
22   LED_OFF = false;
23 var pin, mode;
24 var gpio10;
25
26 if (process.platform === 'linux') {
27   pin = 10;
28   mode = gpio.MODE.NONE;
29 } else if (process.platform === 'nuttx') {
30   pin = require('stm32f4dis').pin.PA10;
31   mode = gpio.MODE.PUSHPULL;
32 } else {
33   assert.fail();
34 }
35
36 test1();
37
38 gpio10 = gpio.open({
39   pin: pin,
40   direction: gpio.DIRECTION.OUT,
41   mode: mode
42 }, test2);
43
44 function test1() {
45   assert.notEqual(gpio.DIRECTION.IN, undefined);
46   assert.notEqual(gpio.DIRECTION.OUT, undefined);
47   assert.notEqual(gpio.MODE.NONE, undefined);
48   assert.notEqual(gpio.MODE.PULLUP, undefined);
49   assert.notEqual(gpio.MODE.PULLDOWN, undefined);
50   assert.notEqual(gpio.MODE.FLOAT, undefined);
51   assert.notEqual(gpio.MODE.PUSHPULL, undefined);
52   assert.notEqual(gpio.MODE.OPENDRAIN, undefined);
53 }
54
55 // turn on LED for 3000ms
56 function test2(err) {
57   assert.equal(err, null);
58
59   gpio10.write(LED_ON, function(writeErr) {
60     assert.equal(writeErr, null);
61     console.log('gpio write');
62
63     gpio10.read(function(readErr, value) {
64       assert.equal(readErr, null);
65       console.log('gpio read:', value);
66       assert.equal(LED_ON, value);
67
68       setTimeout(function() {
69         gpio10.writeSync(LED_OFF);
70         assert.equal(LED_OFF, gpio10.readSync());
71         gpio10.close();
72       }, 3000);
73     });
74   });
75 }