Skip to content

Commit c82a3d3

Browse files
hs0225yichoi
authored andcommitted
Improve coverage of System I/O module (#958)
- cover all function in System I/O module - change the pins to test all module at the same time - fix bug in pwm IoT.js-DCO-1.0-Signed-off-by: Hosung Kim [email protected]
1 parent f58d3b1 commit c82a3d3

File tree

9 files changed

+237
-148
lines changed

9 files changed

+237
-148
lines changed

src/js/pwm.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,16 @@ function pwmPinOpen(configuration, callback) {
213213
};
214214

215215
PwmPin.prototype.close = function(callback) {
216+
var self = this;
217+
216218
if (util.isNull(_binding)) {
217219
throw new Error('Pwm pin is not opened');
218220
}
219221

220222
_binding.close(function(err) {
221223
util.isFunction(callback) && callback.call(self, err);
222224
});
225+
_binding = null;
223226
};
224227

225228
PwmPin.prototype.closeSync = function() {
@@ -228,6 +231,7 @@ function pwmPinOpen(configuration, callback) {
228231
}
229232

230233
_binding.close();
234+
_binding = null;
231235
};
232236

233237
return new PwmPin(configuration, callback);

test/run_pass/test_adc.js

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,58 +28,67 @@ if (process.platform === 'linux') {
2828
assert.fail();
2929
}
3030

31-
var adc0 = adc.open(configuration, function(err) {
32-
console.log('ADC initialized');
31+
asyncTest();
3332

34-
if (err) {
35-
assert.fail();
36-
}
33+
// read async test
34+
function asyncTest() {
35+
var adc0 = adc.open(configuration, function(err) {
36+
console.log('ADC initialized');
3737

38-
test1();
39-
});
38+
if (err) {
39+
assert.fail();
40+
}
4041

41-
// read async test
42-
function test1() {
43-
var loopCnt = 5;
42+
var loopCnt = 5;
4443

45-
console.log('test1 start(read async test)');
46-
var test1Loop = setInterval(function() {
47-
if (--loopCnt < 0) {
48-
console.log('test1 complete');
49-
clearInterval(test1Loop);
50-
test2();
51-
} else {
52-
adc0.read(function(err, value) {
53-
if (err) {
54-
console.log('read failed');
55-
assert.fail();
56-
}
44+
console.log('test1 start(read async test)');
45+
var test1Loop = setInterval(function() {
46+
if (--loopCnt < 0) {
47+
console.log('test1 complete');
48+
clearInterval(test1Loop);
49+
adc0.closeSync();
50+
syncTestst();
51+
} else {
52+
adc0.read(function(err, value) {
53+
if (err) {
54+
console.log('read failed');
55+
assert.fail();
56+
}
5757

58-
console.log(value);
59-
});
60-
}
61-
}, 1000);
58+
console.log(value);
59+
});
60+
}
61+
}, 1000);
62+
});
6263
}
6364

6465
// read sync test
65-
function test2() {
66-
var loopCnt = 5,
67-
value = -1;
66+
function syncTestst() {
67+
var adc0 = adc.open(configuration, function(err) {
68+
console.log('ADC initialized');
69+
70+
if (err) {
71+
assert.fail();
72+
}
73+
74+
var loopCnt = 5,
75+
value = -1;
6876

69-
console.log('test2 start(read sync test)');
70-
var test2Loop = setInterval(function() {
71-
if (--loopCnt < 0) {
72-
console.log('test2 complete');
73-
clearInterval(test2Loop);
74-
adc0.close();
75-
} else {
76-
value = adc0.readSync();
77-
if (value < 0) {
78-
console.log('read failed');
79-
assert.fail();
77+
console.log('test2 start(read sync test)');
78+
var test2Loop = setInterval(function() {
79+
if (--loopCnt < 0) {
80+
console.log('test2 complete');
81+
clearInterval(test2Loop);
82+
adc0.close();
8083
} else {
81-
console.log(value);
84+
value = adc0.readSync();
85+
if (value < 0) {
86+
console.log('read failed');
87+
assert.fail();
88+
} else {
89+
console.log(value);
90+
}
8291
}
83-
}
84-
}, 1000);
92+
}, 1000);
93+
});
8594
}

test/run_pass/test_gpio_event.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ var gpio = new Gpio();
1818

1919
var testGpioInfo = [
2020
{
21-
pin: 14,
21+
pin: 13,
2222
edge: gpio.EDGE.RISING
2323
},
2424
{
25-
pin: 15,
25+
pin: 19,
2626
edge: gpio.EDGE.FALLING
2727
},
2828
{
29-
pin: 18,
29+
pin: 26,
3030
edge: gpio.EDGE.BOTH
3131
}
3232
];

test/run_pass/test_gpio_input.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ var SWITCH_ON = false,
2828
var loopCnt = 0;
2929

3030
if (process.platform === 'linux') {
31-
ledPin = 10;
32-
switchPin = 9;
31+
ledPin = 20;
32+
switchPin = 13;
3333
ledMode = gpio.MODE.NONE;
3434
} else if (process.platform === 'nuttx') {
3535
var pin = require('stm32f4dis').pin;
3636
ledPin = pin.PA10;
37-
switchPin = pin.PA8;
37+
switchPin = pin.PA15;
3838
ledMode = gpio.MODE.PUSHPULL;
3939
} else if(process.platform === 'tizenrt') {
4040
ledPin = 41;
@@ -64,6 +64,11 @@ var loop = setInterval(function() {
6464

6565
if ((++loopCnt) == 10) {
6666
clearInterval(loop);
67+
ledGpio.closeSync();
68+
switchGpio.closeSync();
69+
ledGpio = switchGpio = null;
70+
console.log('finish test');
71+
return;
6772
}
6873

6974
switchGpio.read(function(err, value) {

test/run_pass/test_gpio_output.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ var gpio = new Gpio();
2121
var LED_ON = true,
2222
LED_OFF = false;
2323
var pin, mode;
24-
var gpio10;
24+
var gpio20;
2525

2626
if (process.platform === 'linux') {
27-
pin = 10;
27+
pin = 20;
2828
mode = gpio.MODE.NONE;
2929
} else if (process.platform === 'nuttx') {
3030
pin = require('stm32f4dis').pin.PA10;
@@ -38,7 +38,7 @@ if (process.platform === 'linux') {
3838

3939
test1();
4040

41-
gpio10 = gpio.open({
41+
gpio20 = gpio.open({
4242
pin: pin,
4343
direction: gpio.DIRECTION.OUT,
4444
mode: mode
@@ -61,19 +61,22 @@ function test1() {
6161
function test2(err) {
6262
assert.equal(err, null);
6363

64-
gpio10.write(LED_ON, function(writeErr) {
64+
gpio20.write(LED_ON, function(writeErr) {
6565
assert.equal(writeErr, null);
6666
console.log('gpio write');
6767

68-
gpio10.read(function(readErr, value) {
68+
gpio20.read(function(readErr, value) {
6969
assert.equal(readErr, null);
7070
console.log('gpio read:', value);
7171
assert.equal(LED_ON, value);
7272

7373
setTimeout(function() {
74-
gpio10.writeSync(LED_OFF);
75-
assert.equal(LED_OFF, gpio10.readSync());
76-
gpio10.close();
74+
gpio20.writeSync(LED_OFF);
75+
var value = gpio20.readSync();
76+
console.log('gpio read:', value);
77+
assert.equal(LED_OFF, value);
78+
gpio20.close();
79+
console.log('finish test');
7780
}, 3000);
7881
});
7982
});

test/run_pass/test_pwm.js

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ var Pwm = require('pwm');
2020
var pwm = new Pwm();
2121

2222
var configuration = {
23-
period: 0.001, // 1kHz
24-
dutyCycle: 0.2 // 20%
23+
period: 0.001 // 1kHz
2524
};
2625

2726
if (process.platform === 'linux') {
@@ -53,56 +52,78 @@ var testCb = function (err) {
5352
}
5453
};
5554

56-
var pwm0 = pwm.open(configuration, function (err) {
57-
console.log('PWM initialized');
55+
var pwm0;
56+
testPeriods();
5857

59-
if (err) {
60-
console.log('Have an error: ' + err.message);
61-
assert.fail();
62-
}
58+
function testPeriods() {
59+
pwm0 = pwm.open(configuration, function (err) {
60+
console.log('PWM initialized');
6361

64-
pwm0.setEnable(1, testCb);
65-
testPeriods(pwm0, testCb);
66-
});
67-
68-
function testPeriods(pwm, callback) {
69-
var options = periodOptions;
70-
console.log('PWM: period test start ');
71-
var idx = 0;
72-
var period = options.values[idx++];
73-
console.log("Period(%d)", period);
74-
pwm.setFrequency(1.0 / period, callback);
75-
pwm.setDutyCycleSync(options.dutyCycle);
76-
77-
var loop = setInterval(function () {
78-
if (idx == options.values.length) {
79-
pwm.setPeriodSync(options.values[0]);
80-
clearInterval(loop);
81-
console.log('PWM period test complete');
82-
testDutyCycles(pwm, callback);
83-
} else {
84-
period = options.values[idx++];
85-
console.log("Period(%d)", period);
86-
pwm.setPeriod(period, callback);
62+
if (err) {
63+
console.log('Have an error: ' + err.message);
64+
assert.fail();
8765
}
88-
}, 1000);
66+
67+
pwm0.setEnable(1, function(err) {
68+
testCb(err);
69+
70+
var options = periodOptions;
71+
console.log('PWM: period test start ');
72+
var idx = 0;
73+
var period = options.values[idx++];
74+
console.log("Period(%d)", period);
75+
pwm0.setFrequencySync(1.0 / period);
76+
pwm0.setDutyCycleSync(options.dutyCycle);
77+
78+
var loop = setInterval(function () {
79+
if (idx == options.values.length) {
80+
clearInterval(loop);
81+
console.log('PWM period test complete');
82+
pwm0.setPeriodSync(options.values[0]);
83+
pwm0.setEnableSync(0);
84+
pwm0.closeSync();
85+
testDutyCycles();
86+
} else {
87+
period = options.values[idx++];
88+
console.log("Period(%d)", period);
89+
pwm0.setPeriod(period, testCb);
90+
}
91+
}, 1000);
92+
});
93+
});
8994
}
9095

91-
function testDutyCycles(pwm, callback) {
96+
function testDutyCycles() {
9297
var options = dutyOptions;
9398

9499
console.log('PWM: duty cycle test start');
95-
pwm.setFrequencySync(1.0 / options.period);
100+
pwm0 = pwm.open(configuration, function (err) {
101+
console.log('PWM initialized');
96102

97-
var idx = 0;
98-
var loop = setInterval(function () {
99-
console.log('Duty cycle %d', options.values[idx]);
100-
pwm.setDutyCycle(options.values[idx], callback);
101-
102-
if (++idx == options.values.length) {
103-
clearInterval(loop);
104-
pwm.setEnableSync(0);
105-
console.log('PWM duty cycle test complete');
103+
if (err) {
104+
console.log('Have an error: ' + err.message);
105+
assert.fail();
106106
}
107-
}, 1000);
107+
108+
pwm0.setPeriod(options.period, function(err) {
109+
testCb(err);
110+
111+
pwm0.setEnableSync(1, testCb);
112+
pwm0.setFrequency(1.0 / options.period, function(err) {
113+
testCb(err);
114+
var idx = 0;
115+
var loop = setInterval(function () {
116+
console.log('Duty cycle %d', options.values[idx]);
117+
pwm0.setDutyCycle(options.values[idx], testCb);
118+
119+
if (++idx == options.values.length) {
120+
clearInterval(loop);
121+
pwm0.setEnableSync(0);
122+
pwm0.close(testCb.bind(err));
123+
console.log('PWM duty cycle test complete');
124+
}
125+
}, 1000);
126+
});
127+
});
128+
});
108129
}

test/run_pass/test_spi.js renamed to test/run_pass/test_spi_buffer.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,6 @@ var Spi = require('spi');
1818

1919
var spi = new Spi();
2020

21-
// mcp3008 test
22-
var channel = 0;
23-
var spi0 = spi.open({
24-
device: '/dev/spidev0.0'
25-
}, function() {
26-
var mode = (8 + channel) << 4;
27-
var tx = [1, mode, 0];
28-
var rx = [0, 0, 0];
29-
30-
spi0.transferSync(tx, rx);
31-
console.log(((rx[1] & 0x03) << 8) + rx[2]);
32-
33-
setTimeout(function() {
34-
spi0.transfer(tx, rx, function(err) {
35-
assert.equal(err, null);
36-
assert.equal(rx.length, 3);
37-
38-
var value = ((rx[1] & 0x03) << 8) + rx[2];
39-
console.log(value);
40-
41-
spi0.close();
42-
});
43-
}, 500);
44-
});
45-
4621
// Buffer test
4722
var spi1 = spi.open({device: '/dev/spidev0.0'}, function() {
4823
var data = 'Hello IoTjs';

0 commit comments

Comments
 (0)