forked from Innovation-Hub/barbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mq3-controller.js
160 lines (142 loc) · 4.15 KB
/
mq3-controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Crafted by Obero on 29/09/14
* For Epitech Innovation Hub
*/
"use strict";
var galil = require('./gpio-galileo-helper');
/**
* Mq3Controller helps you to control BarBot mq3 alcohol gaz-meter.
*
* @class Mq3Controller
* @constructor
*/
var Mq3Controller = function()
{
/**
* Sets the pin id on which the mq3 is plugged
* @property pin
* @type String
* @default 'A0'
*/
this.pin = 'A0';
/**
* The mq3 base value (no alcohol detected)
* @property base_value
* @type Number
* @default 0
*/
this.base_value = 0;
};
/**
* Helper : Gets the current mq3 value.
*
* @method getValue
* @return {Number} Current mq3 value
*/
Mq3Controller.prototype.getValue = function()
{
var value;
galil.openPin(this.pin);
galil.setPinDirection(this.pin, 'out');
galil.writePin(this.pin, '0');
value = parseInt(galil.readAnalogPin(this.pin));
galil.closePin(this.pin);
return value;
};
/**
* Helper : Gets the average measure value over time
*
* @method getAverageValue
* @param {Number} interval A measure is done every interval (ms)
* @param {Number} count Number of measures to do
* @param {Function} [callback] Callback. Average value is passed to it.
*/
Mq3Controller.prototype.getAverageValue = function(interval, count, callback)
{
var that = this;
var average_value = 0;
galil.openPin(this.pin);
galil.setPinDirection(this.pin, 'out');
galil.writePin(this.pin, '0');
console.log('[MQ3] Starting measurement');
var timer = 0;
var int_id = setInterval(function()
{
console.log('[MQ3] ' + (count - timer) + ' left...');
average_value += parseInt(galil.readAnalogPin(that.pin));
timer++;
if (timer > count)
{
clearInterval(int_id);
galil.closePin(that.pin);
average_value = average_value / (count + 1);
console.log('[MQ3] Measurement done');
callback(average_value);
}
}, interval);
};
/**
* Helper : Gets the higher measure value over time
*
* @method getHigherValue
* @param {Number} interval A measure is done every interval (ms)
* @param {Number} count Number of measures to do
* @param {Function} [callback] Callback. Higher value is passed to it.
*/
Mq3Controller.prototype.getHigherValue = function(interval, count, callback)
{
var that = this;
var higher_value = 0;
galil.openPin(this.pin);
galil.setPinDirection(this.pin, 'out');
galil.writePin(this.pin, '0');
console.log('[MQ3] Starting measurement');
var timer = 0;
var int_id = setInterval(function()
{
var current_value = parseInt(galil.readAnalogPin(that.pin));
higher_value = (higher_value > current_value) ? higher_value : current_value;
timer++;
if (timer > count)
{
galil.closePin(that.pin);
clearInterval(int_id);
console.log('[MQ3] Measurement done');
callback(higher_value);
}
console.log('[MQ3] ' + (count - timer) + ' left...');
}, interval);
};
/**
* Helper : Gets the lower measure value over time
*
* @method getHigherValue
* @param {Number} interval A measure is done every interval (ms)
* @param {Number} count Number of measures to do
* @param {Function} [callback] Callback. Lower value is passed to it.
*/
Mq3Controller.prototype.getLowerValue = function(interval, count, callback)
{
var that = this;
var higher_value = 0;
galil.openPin(this.pin);
galil.setPinDirection(this.pin, 'out');
galil.writePin(this.pin, '0');
console.log('[MQ3] Starting measurement');
var timer = 0;
var int_id = setInterval(function()
{
var current_value = parseInt(galil.readAnalogPin(that.pin));
higher_value = (higher_value > current_value) ? higher_value : current_value;
timer++;
if (timer > count)
{
galil.closePin(that.pin);
clearInterval(int_id);
console.log('[MQ3] Measurement done');
callback(higher_value);
}
console.log('[MQ3] ' + (count - timer) + ' left...');
}, interval);
};
module.exports = new Mq3Controller;