1 /* Copyright (c) 2013, Kelly Korevec <korevec@gmail.com>
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.
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.
27 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
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
33 * http://www.apache.org/licenses/LICENSE-2.0
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.
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'.
46 var util = require('util');
47 var i2c = process.binding(process.binding.i2c);
50 if (!(this instanceof I2C)) {
55 I2C.prototype.open = function(configurable, callback) {
56 var i2c_bus = new I2CBus(configurable, callback);
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');
66 } else if (process.platform === 'nuttx') {
67 if (!util.isNumber(configurable.device)) {
68 throw new TypeError('Bad configurable - device: Number');
72 if (!util.isNumber(configurable.address)) {
73 throw new TypeError('Bad configurable - address: Number');
76 this.address = configurable.address;
78 this.i2c = new i2c(configurable.device, (function(_this) {
79 return function(err) {
81 _this.setAddress(configurable.address);
83 util.isFunction(callback) && callback(err);
89 I2CBus.prototype.close = function() {
94 I2CBus.prototype.setAddress = function(address, callback) {
95 if (!util.isNumber(address)) {
96 throw new TypeError('Bad argument - address: Number');
99 this.address = address;
100 this.i2c.setAddress(this.address);
102 util.isFunction(callback) && callback();
105 I2CBus.prototype.write = function(array, callback) {
106 if (!util.isArray(array)) {
107 throw new TypeError('Bad argument - array: Array');
110 this.setAddress(this.address);
111 this.i2c.write(array, function(err) {
112 util.isFunction(callback) && callback(err);
116 I2CBus.prototype.writeByte = function(byte, callback) {
117 if (!util.isNumber(byte)) {
118 throw new TypeError('Bad argument - byte: Number');
121 this.setAddress(this.address);
122 this.i2c.writeByte(byte, function(err) {
123 util.isFunction(callback) && callback(err);
127 I2CBus.prototype.writeBytes = function(cmd, array, callback) {
128 if (!util.isNumber(cmd)) {
129 throw new TypeError('Bad argument - cmd: Number');
131 if (!util.isArray(array)) {
132 throw new TypeError('Bad argument - array: Array');
135 this.setAddress(this.address);
136 this.i2c.writeBlock(cmd, array, function(err) {
137 util.isFunction(callback) && callback(err);
141 I2CBus.prototype.read = function(length, callback) {
142 if (!util.isNumber(length)) {
143 throw new TypeError('Bad argument - length: Number');
146 this.setAddress(this.address);
147 this.i2c.read(length, function(err, data) {
148 util.isFunction(callback) && callback(err, data);
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);
159 I2CBus.prototype.readBytes = function(cmd, length, callback) {
160 if (!util.isNumber(cmd)) {
161 throw new TypeError('Bad argument - cmd: Number');
163 if (!util.isNumber(length)) {
164 throw new TypeError('Bad argument - length: Number');
167 this.setAddress(this.address);
168 this.i2c.readBlock(cmd, length, 0, function(err, resArray) {
169 util.isFunction(callback) && callback(err, resArray);
173 module.exports = I2C;