-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (37 loc) · 959 Bytes
/
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
module.exports = class HumiChip{
constructor(addr, config, comm){
this.config = config;
this.comm = comm;
this.addr = addr;
this.settable = [];
this.init();
}
init(){
var sensor = this;
this.comm.writeByte(this.addr, 80).then((r) => {
sensor.initialized = true;
}).catch((e) => {
sensor.initialized = false;
});
}
get(){
var sensor = this;
return new Promise((fulfill, reject) => {
sensor.comm.readBytes(sensor.addr, 4).then((res) => {
fulfill(sensor.parseStatus(res));
}).catch((e) => {
sensor.initialized = false;
reject(e);
});
});
}
parseStatus(data){
var readings = {
humidity: (((data[0] & 0x3F) << 8) + data[1]) / 16384 * 100,
temperature: (((data[2] << 8) + (data[3] & 0xFC)) / 4) / 16384 * 165 - 40
};
if(this.config.scale == 'f') readings.temperature = (readings.temperature * 1.8) + 32;
if(this.config.scale == 'k') readings.temperature += 273.15;
return readings;
}
};