Update Iot.js
[platform/upstream/iotjs.git] / tools / src / js / i2c.js
1 /* Copyright (c) 2013, Kelly Korevec <korevec@gmail.com>
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above copyright
9  *       notice, this list of conditions and the following disclaimer in the
10  *       documentation and/or other materials provided with the distribution.
11  *     * Neither the name of the author nor the
12  *       names of its contributors may be used to endorse or promote products
13  *       derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
28  *
29  * Licensed under the Apache License, Version 2.0 (the "License");
30  * you may not use this file except in compliance with the License.
31  * You may obtain a copy of the License at
32  *
33  *     http://www.apache.org/licenses/LICENSE-2.0
34  *
35  * Unless required by applicable law or agreed to in writing, software
36  * distributed under the License is distributed on an "AS IS" BASIS
37  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38  * See the License for the specific language governing permissions and
39  * limitations under the License.
40  */
41
42 /* This file includes all APIs in 'node-i2c'(https://github.com/kelly/node-i2c).
43  * Some functions are translated from coffee script(i2c.coffee) in 'node-i2c'.
44  */
45
46 var util = require('util');
47 var i2c = process.binding(process.binding.i2c);
48
49 function I2C() {
50   if (!(this instanceof I2C)) {
51     return new I2C();
52   }
53 };
54
55 I2C.prototype.open = function(configurable, callback) {
56   var i2c_bus = new I2CBus(configurable, callback);
57   return i2c_bus;
58 };
59
60 function I2CBus(configurable, callback) {
61   if (util.isObject(configurable)) {
62     if (process.platform === 'linux') {
63       if (!util.isString(configurable.device)) {
64         throw new TypeError('Bad configurable - device: String');
65       }
66     } else if (process.platform === 'nuttx') {
67       if (!util.isNumber(configurable.device)) {
68         throw new TypeError('Bad configurable - device: Number');
69       }
70     }
71
72     if (!util.isNumber(configurable.address)) {
73       throw new TypeError('Bad configurable - address: Number');
74     }
75
76     this.address = configurable.address;
77
78     this.i2c = new i2c(configurable.device, (function(_this) {
79       return function(err) {
80         if (!err) {
81           _this.setAddress(configurable.address);
82         }
83         util.isFunction(callback) && callback(err);
84       };
85     })(this));
86   }
87 };
88
89 I2CBus.prototype.close = function() {
90   this.i2c.close();
91 };
92
93
94 I2CBus.prototype.setAddress = function(address, callback) {
95   if (!util.isNumber(address)) {
96     throw new TypeError('Bad argument - address: Number');
97   }
98
99   this.address = address;
100   this.i2c.setAddress(this.address);
101
102   util.isFunction(callback) && callback();
103 };
104
105 I2CBus.prototype.write = function(array, callback) {
106   if (!util.isArray(array)) {
107     throw new TypeError('Bad argument - array: Array');
108   }
109
110   this.setAddress(this.address);
111   this.i2c.write(array, function(err) {
112     util.isFunction(callback) && callback(err);
113   });
114 };
115
116 I2CBus.prototype.writeByte = function(byte, callback) {
117   if (!util.isNumber(byte)) {
118     throw new TypeError('Bad argument - byte: Number');
119   }
120
121   this.setAddress(this.address);
122   this.i2c.writeByte(byte, function(err) {
123     util.isFunction(callback) && callback(err);
124   });
125 };
126
127 I2CBus.prototype.writeBytes = function(cmd, array, callback) {
128   if (!util.isNumber(cmd)) {
129     throw new TypeError('Bad argument - cmd: Number');
130   }
131   if (!util.isArray(array)) {
132     throw new TypeError('Bad argument - array: Array');
133   }
134
135   this.setAddress(this.address);
136   this.i2c.writeBlock(cmd, array, function(err) {
137     util.isFunction(callback) && callback(err);
138   });
139 };
140
141 I2CBus.prototype.read = function(length, callback) {
142   if (!util.isNumber(length)) {
143     throw new TypeError('Bad argument - length: Number');
144   }
145
146   this.setAddress(this.address);
147   this.i2c.read(length, function(err, data) {
148     util.isFunction(callback) && callback(err, data);
149   });
150 };
151
152 I2CBus.prototype.readByte = function(callback) {
153   this.setAddress(this.address);
154   this.i2c.readByte(function(err, data) {
155     util.isFunction(callback) && callback(err, data);
156   });
157 };
158
159 I2CBus.prototype.readBytes = function(cmd, length, callback) {
160   if (!util.isNumber(cmd)) {
161     throw new TypeError('Bad argument - cmd: Number');
162   }
163   if (!util.isNumber(length)) {
164     throw new TypeError('Bad argument - length: Number');
165   }
166
167   this.setAddress(this.address);
168   this.i2c.readBlock(cmd, length, 0, function(err, resArray) {
169     util.isFunction(callback) && callback(err, resArray);
170   });
171 };
172
173 module.exports = I2C;