Upload packaging folder
[platform/upstream/iotjs.git] / tools / src / js / adc.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 var util = require('util');
17 var adc = process.binding(process.binding.adc);
18
19
20 function Adc() {
21   if (!(this instanceof Adc)) {
22     return new Adc();
23   }
24 }
25
26 Adc.prototype.open = function(configuration, callback) {
27   return new AdcPin(configuration, callback);
28 };
29
30
31 function AdcPin(configuration, callback) {
32   var self = this;
33
34   if (util.isObject(configuration)) {
35     if (process.platform === 'linux') {
36       if (!util.isString(configuration.device)) {
37         throw new TypeError(
38           'Bad configuration - device is mandatory and should be String');
39       }
40     } else if (process.platform === 'nuttx') {
41       if (!util.isNumber(configuration.pin)) {
42         throw new TypeError(
43           'Bad configuration - pin is mandatory and should be Number');
44       }
45     }
46   } else {
47     throw new TypeError('Bad arguments - configuration should be Object')
48   }
49
50   this._binding = new adc.Adc(configuration, function(err) {
51     util.isFunction(callback) && callback.call(self, err);
52   });
53
54   process.on('exit', (function(self) {
55     return function() {
56       if (!util.isNull(self._binding)) {
57         self.closeSync();
58       }
59     };
60   })(this));
61 }
62
63 AdcPin.prototype.read = function(callback) {
64   var self = this;
65
66   if (util.isNull(this._binding)) {
67     throw new Error('ADC pin is not opened');
68   }
69
70   this._binding.read(function(err, value) {
71     util.isFunction(callback) && callback.call(self, err, value);
72   });
73 };
74
75 AdcPin.prototype.readSync = function() {
76   if (util.isNull(this._binding)) {
77     throw new Error('ADC pin is not opened');
78   }
79
80   return this._binding.read();
81 };
82
83 AdcPin.prototype.close = function(callback) {
84   var self = this;
85
86   if (util.isNull(this._binding)) {
87     throw new Error('ADC pin is not opened');
88   }
89
90   this._binding.close(function(err) {
91     util.isFunction(callback) && callback.call(self, err);
92   });
93
94   this._binding = null;
95 };
96
97 AdcPin.prototype.closeSync = function() {
98   if (util.isNull(this._binding)) {
99     throw new Error('ADC pin is not opened');
100   }
101
102   this._binding.close();
103
104   this._binding = null;
105 };
106
107 module.exports = Adc;