Update Iot.js
[platform/upstream/iotjs.git] / test / run_pass / test_i2c.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 /* This test is based on Raspberry Pi with GY-30 Sensor. */
17
18 var assert = require('assert');
19 var I2C = require('i2c');
20
21 var i2c = new I2C();
22
23 var wire = i2c.open({device: '/dev/i2c-1', address: 0x23}, function(err) {
24   if (err) {
25     throw err;
26   }
27 });
28
29 wire.write([0x10], function(err) {
30   assert.equal(err, null);
31   console.log('write done');
32 });
33
34 wire.writeByte(0x10, function(err) {
35   assert.equal(err, null);
36   console.log('writeByte done');
37 });
38
39 wire.read(2, function(err, res) {
40   assert.equal(err, null);
41   assert.equal(res.length, 2, 'I2C read failed.(length is not equal)');
42   console.log('read result: '+res[0]+', '+res[1]);
43 });
44
45 wire.readByte(function(err, res) {
46   assert.equal(err, null);
47   console.log('readByte result: '+res);
48 });
49
50 wire.readBytes(0x20, 2, function(err, res) {
51   assert.equal(err, null);
52   assert.equal(res.length, 2, 'I2C readBytes failed.(length is not equal)');
53   console.log('readBytes(cmd:0x20, length:2) result: '+res[0]+', '+res[1]);
54 });