Update Iot.js
[platform/upstream/iotjs.git] / test / run_pass / test_pwm.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 assert = require('assert');
18 var Pwm = require('pwm');
19
20 var pwm = new Pwm();
21
22 var configuration = {};
23
24 if (process.platform === 'linux') {
25   configuration.pin = 0;
26 } else if (process.platform === 'nuttx') {
27   configuration.pin = require('stm32f4dis').pin.PWM1.CH1_1;
28 } else {
29   assert.fail();
30 }
31
32 var periodOptions = {
33   dutyCycle: 0.5,
34   // The platform PWM is tested on (artik10/tizen 3.0) has an upper limit
35   // of 75.2 Hz of PWM0 frequency.
36   //values: [0.2, 0.4, 0.6, 0.8, 1]
37   values: [ 0.5 ]
38 };
39
40 var dutyOptions = {
41   period: 0.5,
42   values: [ 0, 0.1, 0.5, 0.9, 1 ]
43 };
44
45 var testCb = function (err) {
46   if (err) {
47     assert.fail();
48   }
49 };
50
51 var pwm0 = pwm.open(configuration, function (err) {
52   console.log('PWM initialized');
53
54   if (err) {
55     console.log('Have an error: ' + err.message);
56     assert.fail();
57   }
58
59   pwm0.setEnable(1, testCb);
60   testPeriods(pwm0, testCb);
61 });
62
63 function testPeriods(pwm, callback) {
64   var options = periodOptions;
65   console.log('PWM: period test start ');
66   var idx = 0;
67   var period = options.values[idx++];
68   console.log("Period(%d)", period);
69   pwm.setFrequency(1.0 / period, callback);
70   pwm.setDutyCycleSync(options.dutyCycle);
71
72   var loop = setInterval(function () {
73     if (idx == options.values.length) {
74       pwm.setPeriodSync(options.values[0]);
75       clearInterval(loop);
76       console.log('PWM period test complete');
77       testDutyCycles(pwm, callback);
78     } else {
79       period = options.values[idx++];
80       console.log("Period(%d)", period);
81       pwm.setPeriod(period, callback);
82     }
83   }, 1000);
84 }
85
86 function testDutyCycles(pwm, callback) {
87   var options = dutyOptions;
88
89   console.log('PWM: duty cycle test start');
90   pwm.setFrequencySync(1.0 / options.period);
91
92   var idx = 0;
93   var loop = setInterval(function () {
94     console.log('Duty cycle %d', options.values[idx]);
95     pwm.setDutyCycle(options.values[idx], callback);
96
97     if (++idx == options.values.length) {
98       clearInterval(loop);
99       pwm.setEnableSync(0);
100       console.log('PWM duty cycle test complete');
101     }
102   }, 1000);
103 }