1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 var util = require('util');
17 var adc = process.binding(process.binding.adc);
21 if (!(this instanceof Adc)) {
26 Adc.prototype.open = function(configuration, callback) {
27 return new AdcPin(configuration, callback);
31 function AdcPin(configuration, callback) {
34 if (util.isObject(configuration)) {
35 if (process.platform === 'linux') {
36 if (!util.isString(configuration.device)) {
38 'Bad configuration - device is mandatory and should be String');
40 } else if (process.platform === 'nuttx') {
41 if (!util.isNumber(configuration.pin)) {
43 'Bad configuration - pin is mandatory and should be Number');
47 throw new TypeError('Bad arguments - configuration should be Object')
50 this._binding = new adc.Adc(configuration, function(err) {
51 util.isFunction(callback) && callback.call(self, err);
54 process.on('exit', (function(self) {
56 if (!util.isNull(self._binding)) {
63 AdcPin.prototype.read = function(callback) {
66 if (util.isNull(this._binding)) {
67 throw new Error('ADC pin is not opened');
70 this._binding.read(function(err, value) {
71 util.isFunction(callback) && callback.call(self, err, value);
75 AdcPin.prototype.readSync = function() {
76 if (util.isNull(this._binding)) {
77 throw new Error('ADC pin is not opened');
80 return this._binding.read();
83 AdcPin.prototype.close = function(callback) {
86 if (util.isNull(this._binding)) {
87 throw new Error('ADC pin is not opened');
90 this._binding.close(function(err) {
91 util.isFunction(callback) && callback.call(self, err);
97 AdcPin.prototype.closeSync = function() {
98 if (util.isNull(this._binding)) {
99 throw new Error('ADC pin is not opened');
102 this._binding.close();
104 this._binding = null;
107 module.exports = Adc;