-
Notifications
You must be signed in to change notification settings - Fork 0
/
SW10.js
61 lines (46 loc) · 1.37 KB
/
SW10.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
'use strict';
class SW10 {
constructor(options) {
const i2c = require('i2c-bus');
this.i2cBusNo = (options && options.hasOwnProperty('i2cBusNo')) ? options.i2cBusNo : 1;
this.i2cBus = i2c.openSync(this.i2cBusNo);
this.i2cAddress = (options && options.hasOwnProperty('i2cAddress')) ? options.i2cAddress : SW10.LM75_DEFAULT_I2C_ADDRESS();
this.I2C_ADDRESS = 0x48;
this.CHIP_ID = 0x58;
this.LM75B_REG_CONF = 0x01;
this.LM75B_REG_TEMP = 0x00;
this.LM75B_REG_TOS = 0x03;
this.LM75B_REG_THYST = 0x02;
}
init(){
return new Promise((resolve, reject) => {
return resolve(0);
});
}
readSensorData() {
return new Promise((resolve, reject) => {
// Grab temperature, humidity, and pressure in a single read
this.i2cBus.readI2cBlock(this.i2cAddress, this.LM75B_REG_TEMP, 2, new Buffer(2), (err, bytesRead, buffer) => {
if(err) {
return reject(err);
}
// Temperature
let adc_T = SW10.uint16(buffer[0], buffer[1]);
let temperature_C = (adc_T >> 5) / 8;
resolve({
temperature_C : temperature_C,
});
});
});
}
static LM75_DEFAULT_I2C_ADDRESS() {
return 0x48;
}
static uint16(msb, lsb) {
return msb << 8 | lsb;
}
static convertCelciusToFahrenheit(c) {
return c * 9 / 5 + 32;
}
}
module.exports = SW10;