-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
279 lines (231 loc) · 6.62 KB
/
index.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2014 Technical Machine, Inc. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
var events = require('events');
var util = require('util');
var prom = require('prom');
/**
* Configuration
*/
// Datasheet: http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7005.pdf
var
REG_STATUS = 0x00,
REG_DATA = 0x01,
REG_CONFIG = 0x03,
REG_ID = 0x11,
/* Status Register */
STATUS_NOT_READY = 0x01,
/* Config Register */
CONFIG_START = 0x01,
CONFIG_HEAT = 0x02,
CONFIG_HUMIDITY = 0x00,
CONFIG_TEMPERATURE = 0x10,
CONFIG_FAST = 0x20,
/* ID Register */
ID_SAMPLE = 0xF0,
ID_SI7005 = 0x50,
/* Coefficients */
TEMPERATURE_OFFSET = 50,
TEMPERATURE_SLOPE = 32,
HUMIDITY_OFFSET = 24,
HUMIDITY_SLOPE = 16,
a0 = (-4.7844),
a1 = 0.4008,
a2 = (-0.00393),
q0 = 0.1973,
q1 = 0.00237,
WAKE_UP_TIME = 15,
/* Constants */
I2C_ADDRESS = 0x40,
DATAh = 0x01, // Relative Humidity or Temperature, High Byte
DATAl = 0x02; // Relative Humidity or Temperature, Low Byte
/**
* ClimateSensor
*/
function ClimateSensor (hardware, csn) {
/**
Constructor
Args
hardware
Tessel port to use
csn
Chip select pin to use (active low). Wired to GPIO 1 on the module.
*/
this.onReady = prom(); // An promise that will be delivered on 'ready' event
this.hardware = hardware;
this.csn = csn || 0;
this._configReg = 0;
// I2C object for address
this.i2c = this.hardware.I2C(I2C_ADDRESS);
this.hardware.digital[this.csn].write(0);
var self = this;
setTimeout(function () {
self._readRegister(REG_ID, function ok (err, reg) {
var id = reg & ID_SAMPLE;
if (id != ID_SI7005) {
self.emit('error', new Error('Cannot connect to Si7005. Got id: ' + id.toString(16)));
}
else {
self.emit('ready');
self.onReady.deliver('delivered request'); // Delviers promise when module is ready
}
});
}, WAKE_UP_TIME);
}
util.inherits(ClimateSensor, events.EventEmitter);
ClimateSensor.prototype._readRegister = function (addressToRead, next) {
/**
Read from registers on the Si7005 via I2C
Args
addressToRead
Register to read
next
Callback; gets reply byte as its arg
*/
this.i2c.transfer(new Buffer([addressToRead]), 1, function (err, ret) {
if (next) {
next(err, ret && ret[0]);
}
});
};
ClimateSensor.prototype._writeRegister = function (addressToWrite, dataToWrite, next) {
/**
Write to registers on the Si7005 via I2C
Args
addressToWrite
Register to read
dataToWrite
Bytes to send
next
Callback
*/
this.i2c.send(new Buffer([addressToWrite, dataToWrite]), next);
};
ClimateSensor.prototype.getData = function (configValue, next) {
/**
Get data from the sensor. Effectively a wrapper function.
Args
configValue
Value corresponding to what data is being requested
next
Callback; gets err, data as args
*/
// Pull the cs line low
this.hardware.digital[this.csn].write(0);
// Wait until the chip wakes up
var self = this;
setTimeout(function () {
self._writeRegister(REG_CONFIG, CONFIG_START | configValue | self._configReg, function () {
setImmediate(function untilready () {
self._readRegister(REG_STATUS, function (err, status) {
if (status & STATUS_NOT_READY) {
setImmediate(untilready);
return;
}
self._writeRegister(REG_DATA, 0, function () {
self._readRegister(DATAh, function (err, datah) {
self._readRegister(DATAl, function (err, datal) {
self.hardware.digital[self.csn].write(1, function(err, data){
next(err, datal | datah << 8);
});
});
});
});
});
});
});
}, WAKE_UP_TIME);
};
ClimateSensor.prototype.readHumidity = function (next) {
/**
Read and return the relative humidity
Args
next
Callback; gets err, relHumidity as args
*/
var self = this;
self.onReady(function () {
self.getData(CONFIG_HUMIDITY, function (err, reg) {
var rawHumidity = reg >> 4;
var curve = ( rawHumidity / HUMIDITY_SLOPE ) - HUMIDITY_OFFSET;
var linearHumidity = curve - ( (curve * curve) * a2 + curve * a1 + a0);
linearHumidity = linearHumidity + ( self._lastTemperature - 30 ) * ( linearHumidity * q1 + q0 );
self.emit('humidity', linearHumidity);
if (next) {
next(null, linearHumidity);
}
})
});
};
ClimateSensor.prototype.readTemperature = function (/*optional*/ type, next) {
/**
Read and return the temperature. Celcius by default, Farenheit if type === 'f'
Args
type
if type === 'f', use Farenheit
next
Callback; gets err, temperature as args
*/
next = next || type;
var self = this;
self.onReady( function () {
self.getData(CONFIG_TEMPERATURE, function (err, reg) {
// console.log('Temp regs:', reg);
var rawTemperature = reg >> 2;
var temp = ( rawTemperature / TEMPERATURE_SLOPE ) - TEMPERATURE_OFFSET;
self._lastTemperature = temp;
if (type === 'f') {
temp = temp * (9/5) + 32;
}
self.emit('temperature', temp, type);
next(null, temp);
})
});
};
ClimateSensor.prototype.setHeater = function (status) {
/**
Turn the chip's internal heater on or off. Enabling the heater will drive
condensation off of the sensor, thereby reducing its hysteresis and allowing
for more accurate humidity measurements in high humidity conditions.
Note that this will interfere with (raise) temperature mesurement.
Args
status
true = heater on, false = heater off
*/
if (status) {
this._configReg |= CONFIG_HEAT;
} else {
if (this._configReg)
{
this._configReg &= ~CONFIG_HEAT;
}
}
};
ClimateSensor.prototype.setFastMeasure = function (status) {
/**
Draw less power on successive polling at the cost of resolution.
Note that this module already uses very little power.
Args
status
true = fast mode, false = normal mode
*/
if (status) {
this._configReg |= CONFIG_FAST;
} else {
if (this._configReg) {
this._configReg &= ~CONFIG_FAST;
}
}
};
/**
* Module API
*/
exports.ClimateSensor = ClimateSensor;
exports.use = function (hardware, csn) {
return new ClimateSensor(hardware, csn);
};