1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 var assert = require('assert');
18 var Pwm = require('pwm');
22 var configuration = {};
24 if (process.platform === 'linux') {
25 configuration.pin = 0;
26 } else if (process.platform === 'nuttx') {
27 configuration.pin = require('stm32f4dis').pin.PWM1.CH1_1;
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]
42 values: [ 0, 0.1, 0.5, 0.9, 1 ]
45 var testCb = function (err) {
51 var pwm0 = pwm.open(configuration, function (err) {
52 console.log('PWM initialized');
55 console.log('Have an error: ' + err.message);
59 pwm0.setEnable(1, testCb);
60 testPeriods(pwm0, testCb);
63 function testPeriods(pwm, callback) {
64 var options = periodOptions;
65 console.log('PWM: period test start ');
67 var period = options.values[idx++];
68 console.log("Period(%d)", period);
69 pwm.setFrequency(1.0 / period, callback);
70 pwm.setDutyCycleSync(options.dutyCycle);
72 var loop = setInterval(function () {
73 if (idx == options.values.length) {
74 pwm.setPeriodSync(options.values[0]);
76 console.log('PWM period test complete');
77 testDutyCycles(pwm, callback);
79 period = options.values[idx++];
80 console.log("Period(%d)", period);
81 pwm.setPeriod(period, callback);
86 function testDutyCycles(pwm, callback) {
87 var options = dutyOptions;
89 console.log('PWM: duty cycle test start');
90 pwm.setFrequencySync(1.0 / options.period);
93 var loop = setInterval(function () {
94 console.log('Duty cycle %d', options.values[idx]);
95 pwm.setDutyCycle(options.values[idx], callback);
97 if (++idx == options.values.length) {
100 console.log('PWM duty cycle test complete');