Skip to content

Commit 13b0fb1

Browse files
hs0225yichoi
authored andcommitted
Add net, gpio samples (#1005)
IoT.js-DCO-1.0-Signed-off-by: Hosung Kim [email protected]
1 parent 88486dd commit 13b0fb1

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

samples/gpio_led.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Copyright 2017-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+
var Gpio = require('gpio');
17+
var pin = require('systemio_pin').pin;
18+
19+
var gpio = new Gpio();
20+
21+
var gpio_led = gpio.open({
22+
pin: pin.led1,
23+
direction: gpio.DIRECTION.OUT
24+
}, function(err) {
25+
if (!err) {
26+
gpio_led.writeSync(true);
27+
28+
var interval = setInterval(function() {
29+
gpio_led.read(function(err, value) {
30+
if (!err) {
31+
console.log("read value:%d", value);
32+
gpio_led.write(!value);
33+
} else {
34+
clearInterval(interval);
35+
}
36+
});
37+
}, 1000);
38+
}
39+
});

samples/net_hello/client.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Copyright 2017-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+
var net = require('net');
17+
18+
var port = 7468;
19+
20+
var msg = '';
21+
var socket = new net.Socket();
22+
23+
var address = process.argv[2] ? process.argv[2] : "127.0.0.1";
24+
25+
socket.connect(port, address);
26+
27+
socket.on('data', function(data) {
28+
msg += data;
29+
});
30+
31+
socket.on('end', function() {
32+
console.log(msg);
33+
socket.end();
34+
});

samples/net_hello/server.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* Copyright 2017-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+
var net = require('net');
17+
18+
var port = 7468;
19+
var server = net.createServer();
20+
21+
server.listen(port, 5);
22+
23+
server.on('connection', function(socket) {
24+
socket.end('Hello IoT.js');
25+
});

samples/systemio_pin.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright 2017-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+
var pin = {};
17+
18+
if (process.platform === 'linux') {
19+
pin.led1 = 20;
20+
} else if (process.platform === 'nuttx') {
21+
var stm32_pin = require('stm32f4dis').pin;
22+
pin.led1 = stm32_pin.PA10;
23+
} else {
24+
throw new Error('Unsupported platform');
25+
}
26+
27+
exports.pin = pin;

0 commit comments

Comments
 (0)