-
Notifications
You must be signed in to change notification settings - Fork 57
/
MCP9808-async.js
135 lines (117 loc) · 2.78 KB
/
MCP9808-async.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
// The code below is based on the Adafruit MCP9808 library (https://github.com/adafruit/Adafruit_MCP9808_Library)
'use strict';
var I2C_CONTROLLER_NAME = 'I2C1'; // Specific to RPi2 with Windows 10 IoT Core
var async = require('async'),
i2c = require('i2c-bus'),
i2c1;
var MCP9808_I2CADDR_DEFAULT = 0x18,
MCP9808_REG_CONFIG = 0x01,
MCP9808_REG_AMBIENT_TEMP = 0x05,
MCP9808_REG_CONFIG_SHUTDOWN = 0x0100;
var BUFFER_SIZE = 2;
function shutdown_wake(sw_ID, callback) {
var conf_shutdown;
const buffer = new Buffer(BUFFER_SIZE);
async.series([
function (cb) {
i2c1.readI2cBlock(MCP9808_I2CADDR_DEFAULT, MCP9808_REG_CONFIG, BUFFER_SIZE, buffer, function (err, conf_register) {
if (err) {
throw err;
}
if (sw_ID == 1) {
conf_shutdown = conf_register | MCP9808_REG_CONFIG_SHUTDOWN;
}
if (sw_ID == 0) {
conf_shutdown = conf_register ^ MCP9808_REG_CONFIG_SHUTDOWN;
}
cb(null);
});
},
function (cb) {
i2c1.i2cWrite(MCP9808_I2CADDR_DEFAULT, 2, new Buffer([MCP9808_REG_CONFIG, conf_shutdown]), function (err, bytesWrittern) {
if (err) {
throw err;
}
cb(null);
});
},
], function (err) {
if (err) {
throw err;
}
callback(null);
});
}
function readTempC(callback) {
const buffer = new Buffer(BUFFER_SIZE);
var t;
var temp;
async.series([
function (cb) {
i2c1.readI2cBlock(MCP9808_I2CADDR_DEFAULT, MCP9808_REG_AMBIENT_TEMP, BUFFER_SIZE, buffer, function (err, result) {
if (err) {
throw err;
}
t = result;
cb(null);
});
},
function (cb) {
t = buffer[0];
t <<= 8;
t |= buffer[1];
temp = t & 0x0FFF;
temp /= 16.0;
if (t & 0x1000) temp -= 256;
cb(null, temp);
},
], function (err, results) {
if (err) {
throw err;
}
callback(results[1]);
return temp;
});
}
(function () {
var rawTemp;
var c;
async.series([
function (cb) {
i2c1 = i2c.open(I2C_CONTROLLER_NAME, cb);
},
function (cb) {
console.log("wake up MCP9808.... ");
shutdown_wake(0, function () {
cb(null);
});
},
function (cb) {
readTempC(function (temp) {
c = temp;
cb(null);
});
},
function (cb) {
var f = c * 9.0 / 5.0 + 32;
console.log("Temp: " + c + "*C");
console.log("Temp: " + f + "*F");
cb(null);
},
function (cb) {
setTimeout(function () {
console.log("Shutdown MCP9808.... ");
shutdown_wake(1, function () {
cb(null);
});
}, 250);
},
function (cb) {
i2c1.close(cb);
},
], function (err) {
if (err) {
throw err;
}
});
}());