Skip to content

Commit 11cf56a

Browse files
authored
[Tests] Add tests for PWM APIs (zephyrproject-rtos#392)
Signed-off-by: cuiyanx <[email protected]> Fixed whitespace issues. Two test failures on A101 are by design but will be addressed with redesign relatively soon. Signed-off-by: Geoff Gustafson <[email protected]>
1 parent 2cd69fd commit 11cf56a

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

tests/test-pwm.js

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Copyright (c) 2016, Intel Corporation.
2+
3+
console.log("Wire IO3 to IO2");
4+
5+
var total = 0;
6+
var passed = 0;
7+
8+
function assert(actual, description) {
9+
total += 1;
10+
11+
var label = "\033[1m\033[31mFAIL\033[0m";
12+
if (actual === true) {
13+
passed += 1;
14+
label = "\033[1m\033[32mPASS\033[0m";
15+
}
16+
17+
console.log(label + " - " + description);
18+
}
19+
20+
function expectThrow(description, func) {
21+
var threw = false;
22+
try {
23+
func();
24+
}
25+
catch (err) {
26+
threw = true;
27+
}
28+
assert(threw, description);
29+
}
30+
31+
var pwm = require("pwm");
32+
var gpio = require("gpio");
33+
var pins = require("arduino101_pins");
34+
var pinA, pinB, msTimer, cycleTimer;
35+
36+
pinB = gpio.open({pin: pins.IO2, direction: 'in'});
37+
38+
// PWMPins open
39+
pinA = pwm.open({channel: pins.IO3});
40+
assert(pinA != null && typeof pinA == "object",
41+
"open: defined pin and default argument");
42+
43+
expectThrow("open: undefined pin", function () {
44+
pinA = pwm.open({channel: 1024});
45+
});
46+
47+
// set Period and PulseWidth with ms
48+
// duty cycle: 30%
49+
var msTrue = 0;
50+
var msFalse = 0;
51+
var msCount = 0;
52+
53+
expectThrow("pwmpin: set pulseWidth without period", function () {
54+
pinA.setPulseWidth(300);
55+
});
56+
57+
pinA = pwm.open({channel: pins.IO3, period: 3, pulseWidth: 1});
58+
assert(pinA != null && typeof pinA == "object",
59+
"open: with period and pulseWidth");
60+
61+
pinA.setPeriod(1000);
62+
63+
expectThrow("pwmpin: set pulseWidth greater than period", function () {
64+
pinA.setPulseWidth(3000);
65+
});
66+
67+
pinA.setPulseWidth(300);
68+
69+
msTimer = setInterval(function () {
70+
if (pinB.read()) {
71+
msTrue = msTrue + 1;
72+
} else {
73+
msFalse = msFalse + 1;
74+
}
75+
76+
msCount = msCount + 1;
77+
}, 50);
78+
79+
setTimeout(function() {
80+
assert(msTrue == 6 && msFalse == 14 && msCount == 20,
81+
"pwmpin: set period and pulseWidth");
82+
clearInterval(msTimer);
83+
84+
expectThrow("pwmpin: set period with invalid value", function () {
85+
pinA.setPeriod("Value");
86+
});
87+
88+
expectThrow("pwmpin: set pulseWidth with invalid value", function () {
89+
pinA.setPulseWidth("Value");
90+
});
91+
92+
// set Period and PulseWidth with cycle
93+
// duty cycle: 70%
94+
var cyclesTrue = 0;
95+
var cyclesFlase = 0;
96+
var cyclesCount = 0;
97+
var periodCount = 0;
98+
var Flag = false;
99+
var oldFlag = false;
100+
pinA = pwm.open({channel: pins.IO3, polarity: "reverse"});
101+
assert(pinA != null && typeof pinA == "object", "open: reverse polarity");
102+
103+
pinA.setPeriodCycles(10000000);
104+
pinA.setPulseWidthCycles(3000000);
105+
106+
cycleTimer = setInterval(function () {
107+
Flag = pinB.read();
108+
109+
if(Flag == oldFlag){
110+
cyclesCount = cyclesCount + 1;
111+
} else {
112+
if (oldFlag) {
113+
cyclesTrue = cyclesTrue + cyclesCount;
114+
} else {
115+
cyclesFlase = cyclesFlase + cyclesCount;
116+
}
117+
118+
oldFlag = Flag;
119+
cyclesCount = 0;
120+
121+
if (Flag == false) {
122+
periodCount = periodCount + 1;
123+
}
124+
125+
if (periodCount == 3) {
126+
assert((25 < cyclesFlase) && (cyclesFlase < 29) &&
127+
(60 < cyclesTrue) && (cyclesTrue < 64),
128+
"pwmpin: set periodCycles and pulseWidthCycles");
129+
130+
console.log("TOTAL: " + passed + " of " + total + " passed");
131+
clearInterval(cycleTimer);
132+
}
133+
}
134+
}, 10);
135+
}, 1000);
136+
137+
expectThrow("pwmpin: set periodCycles with invalid value", function () {
138+
pinA.setPeriodCycles("Value");
139+
});
140+
141+
expectThrow("pwmpin: set pulseWidthCycles with invalid value", function () {
142+
pinA.setPulseWidthCycles("Value");
143+
});

0 commit comments

Comments
 (0)