periph 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/examples/accelerometer/bma150/complete.js +62 -0
- package/examples/accelerometer/bma150/demo.js +65 -0
- package/examples/accelerometer/bma150/minimal.js +18 -0
- package/examples/adc_dac/hy3131/complete.js +66 -0
- package/examples/adc_dac/hy3131/demo.js +37 -0
- package/examples/adc_dac/hy3131/minimal.js +18 -0
- package/examples/discovery/complete.js +25 -0
- package/examples/discovery/demo.js +30 -0
- package/examples/discovery/minimal.js +12 -0
- package/examples/display/il0373/complete.js +71 -0
- package/examples/display/il0373/demo.js +49 -0
- package/examples/display/il0373/minimal.js +26 -0
- package/examples/display/ssd1331/complete.js +83 -0
- package/examples/display/ssd1331/demo.js +49 -0
- package/examples/display/ssd1331/minimal.js +27 -0
- package/examples/display/st7036/complete.js +71 -0
- package/examples/display/st7036/demo.js +56 -0
- package/examples/display/st7036/minimal.js +21 -0
- package/examples/display/uc1611s/complete.js +135 -0
- package/examples/display/uc1611s/complete_spi.js +128 -0
- package/examples/display/uc1611s/demo.js +90 -0
- package/examples/display/uc1611s/demo_spi.js +91 -0
- package/examples/display/uc1611s/minimal.js +28 -0
- package/examples/display/uc1611s/minimal_spi.js +30 -0
- package/examples/display/uc8159/complete.js +64 -0
- package/examples/display/uc8159/demo.js +41 -0
- package/examples/display/uc8159/minimal.js +28 -0
- package/examples/display/uc8276c/complete.js +73 -0
- package/examples/display/uc8276c/demo.js +49 -0
- package/examples/display/uc8276c/minimal.js +26 -0
- package/examples/environmental/bme280/complete.js +1 -1
- package/examples/environmental/bme280/demo.js +1 -1
- package/examples/environmental/bme280/minimal.js +1 -1
- package/examples/gyroscope/l3g4200d/complete.js +1 -1
- package/examples/gyroscope/l3g4200d/demo.js +1 -1
- package/examples/gyroscope/l3g4200d/minimal.js +1 -1
- package/examples/memory/23x1024/complete.js +54 -0
- package/examples/memory/23x1024/demo.js +51 -0
- package/examples/memory/23x1024/minimal.js +22 -0
- package/examples/memory/mx25l12845e/complete.js +66 -0
- package/examples/memory/mx25l12845e/demo.js +66 -0
- package/examples/memory/mx25l12845e/minimal.js +27 -0
- package/examples/memory/sd_spi/complete.js +55 -0
- package/examples/memory/sd_spi/demo.js +67 -0
- package/examples/memory/sd_spi/minimal.js +32 -0
- package/examples/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -0
- package/examples/pressure/bmp280/complete.js +1 -1
- package/examples/pressure/bmp280/demo.js +1 -1
- package/examples/pressure/bmp280/minimal.js +1 -1
- package/examples/pressure/bmp581/complete.js +1 -1
- package/examples/pressure/bmp581/demo.js +1 -1
- package/examples/pressure/bmp581/minimal.js +1 -1
- package/examples/pressure/lps22df/complete.js +1 -1
- package/examples/pressure/lps22df/demo.js +1 -1
- package/examples/pressure/lps22df/minimal.js +1 -1
- package/examples/rtc/ds3231/complete.js +77 -0
- package/examples/rtc/ds3231/demo.js +60 -0
- package/examples/rtc/ds3231/minimal.js +23 -0
- package/examples/rtc/pcf8523/complete.js +66 -0
- package/examples/rtc/pcf8523/demo.js +65 -0
- package/examples/rtc/pcf8523/minimal.js +20 -0
- package/examples/temperature/mcp9808/complete.js +89 -0
- package/examples/temperature/mcp9808/demo.js +62 -0
- package/examples/temperature/mcp9808/minimal.js +21 -0
- package/examples/temperature/tmp117/complete.js +90 -0
- package/examples/temperature/tmp117/demo.js +73 -0
- package/examples/temperature/tmp117/minimal.js +21 -0
- package/examples/tof/vl53l0x/complete.js +118 -0
- package/examples/tof/vl53l0x/demo.js +83 -0
- package/examples/tof/vl53l0x/minimal.js +25 -0
- package/examples/tof/vl53l1x/complete.js +151 -0
- package/examples/tof/vl53l1x/demo.js +100 -0
- package/examples/tof/vl53l1x/minimal.js +25 -0
- package/package.json +1 -1
- package/src/chips/accelerometer/adxl345.js +10 -31
- package/src/chips/accelerometer/adxl362.js +7 -7
- package/src/chips/accelerometer/bma150.js +419 -0
- package/src/chips/adc_dac/ad7705.js +36 -9
- package/src/chips/adc_dac/ad7706.js +39 -9
- package/src/chips/adc_dac/hy3131.js +311 -0
- package/src/chips/display/il0373.js +607 -0
- package/src/chips/display/ssd1331.js +412 -0
- package/src/chips/display/st7036.js +401 -0
- package/src/chips/display/uc1611s.js +648 -0
- package/src/chips/display/uc8159.js +394 -0
- package/src/chips/display/uc8276c.js +658 -0
- package/src/chips/environmental/bme280.js +31 -46
- package/src/chips/environmental/bme680.js +38 -43
- package/src/chips/gas/ens160.js +18 -26
- package/src/chips/gyroscope/l3g4200d.js +34 -50
- package/src/chips/gyroscope/l3gd20h.js +24 -40
- package/src/chips/imu/mpu6050.js +29 -41
- package/src/chips/imu/mpu9250.js +45 -69
- package/src/chips/imu/mpu9255.js +54 -78
- package/src/chips/io_expander/mcp23017.js +24 -33
- package/src/chips/light/apds-9930.js +52 -60
- package/src/chips/light/apds9960.js +69 -77
- package/src/chips/magnetometer/as5600.js +13 -27
- package/src/chips/magnetometer/hmc5883l.js +32 -29
- package/src/chips/memory/_23x1024.js +228 -0
- package/src/chips/memory/_24aa02uid.js +8 -11
- package/src/chips/memory/mx25l12845e.js +467 -0
- package/src/chips/memory/sd_spi.js +486 -0
- package/src/chips/motor/drv8830.js +239 -0
- package/src/chips/other/mpr121.js +7 -7
- package/src/chips/power/ina219.js +5 -8
- package/src/chips/power/ina226.js +5 -8
- package/src/chips/power/ina3221.js +5 -8
- package/src/chips/pressure/bmp085.js +17 -11
- package/src/chips/pressure/bmp180.js +7 -7
- package/src/chips/pressure/bmp280.js +21 -33
- package/src/chips/pressure/bmp384.js +28 -39
- package/src/chips/pressure/bmp581.js +59 -70
- package/src/chips/pressure/lps22df.js +50 -52
- package/src/chips/pressure/lps28dfw.js +15 -13
- package/src/chips/pressure/lps33hw.js +7 -5
- package/src/chips/rtc/ds3231.js +504 -0
- package/src/chips/rtc/pcf8523.js +533 -0
- package/src/chips/temperature/mcp9808.js +490 -0
- package/src/chips/temperature/tmp117.js +471 -0
- package/src/chips/tof/_vl53_base.js +160 -0
- package/src/chips/tof/vl53l0x.js +816 -0
- package/src/chips/tof/vl53l1x.js +816 -0
- package/src/connection/connection.js +6 -4
- package/src/connection/dhtxx.js +5 -3
- package/src/connection/display.js +278 -0
- package/src/connection/display_mock.js +77 -0
- package/src/connection/hx711.js +5 -3
- package/src/connection/i2c.js +6 -4
- package/src/connection/i2c_mock.js +47 -0
- package/src/connection/input_pin.js +36 -5
- package/src/connection/register.js +14 -0
- package/src/connection/register_connection.js +65 -0
- package/src/connection/sipo.js +4 -2
- package/src/connection/sipo_mock.js +49 -0
- package/src/connection/smbus.js +6 -4
- package/src/connection/softspi.js +73 -0
- package/src/connection/spi.js +110 -23
- package/src/connection/spi_mock.js +50 -0
- package/src/connection/uart.js +2 -1
- package/src/discovery/discovery.js +218 -0
- package/src/discovery/registry.js +85 -0
- package/src/storage/block_device.js +95 -0
- package/src/storage/ram_block_device.js +72 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { toSigned } = require('../../connection/register');
|
|
4
|
+
|
|
5
|
+
const _REG_CONFIG = 0x01;
|
|
6
|
+
const _REG_TUPPER = 0x02;
|
|
7
|
+
const _REG_TLOWER = 0x03;
|
|
8
|
+
const _REG_TCRIT = 0x04;
|
|
9
|
+
const _REG_TA = 0x05;
|
|
10
|
+
const _REG_MFR_ID = 0x06;
|
|
11
|
+
const _REG_DEVICE_ID = 0x07;
|
|
12
|
+
const _REG_RESOLUTION = 0x08;
|
|
13
|
+
|
|
14
|
+
const _MANUFACTURER_ID = 0x0054;
|
|
15
|
+
const _DEVICE_ID = 0x04;
|
|
16
|
+
|
|
17
|
+
// CONFIG (0x01) bits.
|
|
18
|
+
const _CFG_THYST_SHIFT = 9;
|
|
19
|
+
const _CFG_THYST_MASK = 0x0600;
|
|
20
|
+
const _CFG_SHDN = 0x0100;
|
|
21
|
+
const _CFG_CRIT_LOCK = 0x0080;
|
|
22
|
+
const _CFG_WIN_LOCK = 0x0040;
|
|
23
|
+
const _CFG_INT_CLEAR = 0x0020;
|
|
24
|
+
const _CFG_ALERT_STAT = 0x0010;
|
|
25
|
+
const _CFG_ALERT_CNT = 0x0008;
|
|
26
|
+
const _CFG_ALERT_SEL = 0x0004;
|
|
27
|
+
const _CFG_ALERT_POL = 0x0002;
|
|
28
|
+
const _CFG_ALERT_MOD = 0x0001;
|
|
29
|
+
const _CFG_LOCKS = 0x00C0;
|
|
30
|
+
// Writable bits: all but the unimplemented 15:11, the read-only ALERT_STAT,
|
|
31
|
+
// and the self-clearing INT_CLEAR (set only on purpose).
|
|
32
|
+
const _CFG_WRITE_MASK = 0x07CF;
|
|
33
|
+
|
|
34
|
+
const _RESOLUTIONS = [0.5, 0.25, 0.125, 0.0625];
|
|
35
|
+
const _HYSTERESES = [0, 1.5, 3.0, 6.0];
|
|
36
|
+
|
|
37
|
+
function _decodeTemperature(raw16) {
|
|
38
|
+
let raw = raw16 & 0x1FFF;
|
|
39
|
+
raw = toSigned(raw, 13);
|
|
40
|
+
return raw / 16;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function _decodeLimit(raw16) {
|
|
44
|
+
let value = (raw16 >> 2) & 0x3FF;
|
|
45
|
+
if (raw16 & 0x1000) value -= 1024;
|
|
46
|
+
return value / 4;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function _encodeLimit(celsius) {
|
|
50
|
+
// Round half away from zero, then clamp to the 11-bit two's-complement range.
|
|
51
|
+
let quarters = celsius >= 0 ? Math.floor(celsius * 4 + 0.5) : -Math.floor(-celsius * 4 + 0.5);
|
|
52
|
+
if (quarters < -1024) quarters = -1024;
|
|
53
|
+
if (quarters > 1023) quarters = 1023;
|
|
54
|
+
return (quarters & 0x7FF) << 2;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function _indexOfStep(table, value) {
|
|
58
|
+
for (let i = 0; i < table.length; i++) {
|
|
59
|
+
if (Math.abs(value - table[i]) < 1e-6) return i;
|
|
60
|
+
}
|
|
61
|
+
return -1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* MCP9808 ±0.5°C maximum accuracy digital temperature sensor (Microchip)
|
|
66
|
+
* — minimal interface.
|
|
67
|
+
*
|
|
68
|
+
* Band-gap temperature sensor with a delta-sigma ADC, read over I²C.
|
|
69
|
+
* Registers are 16-bit, big-endian, addressed through a non-incrementing
|
|
70
|
+
* Register Pointer. Eight selectable addresses (0x18–0x1F) via the
|
|
71
|
+
* A0/A1/A2 strap pins.
|
|
72
|
+
*
|
|
73
|
+
* No register writes are made at construction: the POR default (continuous
|
|
74
|
+
* conversion at 0.0625 °C resolution, Alert output disabled) already serves
|
|
75
|
+
* the primary use case.
|
|
76
|
+
*
|
|
77
|
+
* Identity check: JS constructors cannot be async, so the MANUFACTURER_ID
|
|
78
|
+
* (0x0054) / DEVICE_ID (0x04) check starts in the constructor and every
|
|
79
|
+
* public method awaits it first. A wrong or absent device therefore rejects
|
|
80
|
+
* the first call made on the driver — `await sensor.init()` right after
|
|
81
|
+
* construction to surface it at a predictable point.
|
|
82
|
+
*/
|
|
83
|
+
class MCP9808Minimal {
|
|
84
|
+
/**
|
|
85
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x18–0x1F).
|
|
86
|
+
*/
|
|
87
|
+
constructor(connection) {
|
|
88
|
+
this._conn = connection;
|
|
89
|
+
this._ready = this._checkIdentity();
|
|
90
|
+
// The rejection is re-raised by every public method; mark it handled
|
|
91
|
+
// here so it never surfaces as an unhandled rejection on its own.
|
|
92
|
+
this._ready.catch(() => {});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Wait for the identity check started by the constructor. Makes no
|
|
97
|
+
* register writes. Optional — every other method awaits the same check —
|
|
98
|
+
* but calling it right after construction surfaces a wrong or absent
|
|
99
|
+
* device at a predictable point.
|
|
100
|
+
* @returns {Promise<void>}
|
|
101
|
+
* @throws {Error} If MANUFACTURER_ID or DEVICE_ID does not match.
|
|
102
|
+
*/
|
|
103
|
+
async init() {
|
|
104
|
+
await this._ready;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async _checkIdentity() {
|
|
108
|
+
const mfr = await this._readReg(_REG_MFR_ID);
|
|
109
|
+
if (mfr !== _MANUFACTURER_ID) {
|
|
110
|
+
throw new Error('MCP9808 not found: expected MANUFACTURER_ID 0x0054, got 0x' +
|
|
111
|
+
mfr.toString(16).padStart(4, '0'));
|
|
112
|
+
}
|
|
113
|
+
const dev = (await this._readReg(_REG_DEVICE_ID)) >> 8;
|
|
114
|
+
if (dev !== _DEVICE_ID) {
|
|
115
|
+
throw new Error('MCP9808 not found: expected DEVICE_ID 0x04, got 0x' +
|
|
116
|
+
dev.toString(16).padStart(2, '0'));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async _readReg(reg) {
|
|
121
|
+
const buf = await this._conn.readReg(reg & 0xFF, 2);
|
|
122
|
+
return buf.readUInt16BE(0);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async _writeReg(reg, value) {
|
|
126
|
+
await this._conn.writeReg(reg & 0xFF, Buffer.from([(value >> 8) & 0xFF, value & 0xFF]));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Read the ambient temperature. Masks off TA's three boundary-status bits
|
|
131
|
+
* and decodes the 13-bit two's-complement value (0.0625 °C per LSB).
|
|
132
|
+
* @returns {Promise<number>} Ambient temperature in °C.
|
|
133
|
+
*/
|
|
134
|
+
async readTemperature() {
|
|
135
|
+
await this._ready;
|
|
136
|
+
return _decodeTemperature(await this._readReg(_REG_TA));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* MCP9808 full interface — extends Minimal with resolution control,
|
|
142
|
+
* Shutdown mode, the TUPPER/TLOWER/TCRIT boundaries, hysteresis, the
|
|
143
|
+
* one-way register locks, and the Level-2 Alert/interrupt API.
|
|
144
|
+
*/
|
|
145
|
+
class MCP9808Full extends MCP9808Minimal {
|
|
146
|
+
/**
|
|
147
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x18–0x1F).
|
|
148
|
+
*/
|
|
149
|
+
constructor(connection) {
|
|
150
|
+
super(connection);
|
|
151
|
+
this._callback = null;
|
|
152
|
+
this._edgeHandler = null;
|
|
153
|
+
this._pollTimer = null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async _readConfig() {
|
|
157
|
+
return (await this._readReg(_REG_CONFIG)) & _CFG_WRITE_MASK;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async _writeConfig(value) {
|
|
161
|
+
await this._writeReg(_REG_CONFIG, value & _CFG_WRITE_MASK);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// -- Resolution ---------------------------------------------------------
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Set the measurement resolution. Finer steps take longer to convert:
|
|
168
|
+
* 0.5 °C = 30 ms, 0.25 °C = 65 ms, 0.125 °C = 130 ms, 0.0625 °C = 250 ms.
|
|
169
|
+
* @param {number} celsius - One of 0.5, 0.25, 0.125, 0.0625.
|
|
170
|
+
* @returns {Promise<void>}
|
|
171
|
+
* @throws {RangeError} If celsius is not a supported step.
|
|
172
|
+
*/
|
|
173
|
+
async setResolution(celsius) {
|
|
174
|
+
const code = _indexOfStep(_RESOLUTIONS, celsius);
|
|
175
|
+
if (code < 0) throw new RangeError('resolution must be one of 0.5, 0.25, 0.125, 0.0625');
|
|
176
|
+
await this._ready;
|
|
177
|
+
await this._conn.writeReg(_REG_RESOLUTION, code);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Read the measurement resolution.
|
|
182
|
+
* @returns {Promise<number>} Resolution step in °C.
|
|
183
|
+
*/
|
|
184
|
+
async getResolution() {
|
|
185
|
+
await this._ready;
|
|
186
|
+
const buf = await this._conn.readReg(_REG_RESOLUTION, 1);
|
|
187
|
+
return _RESOLUTIONS[buf[0] & 0x03];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// -- Shutdown -----------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Enter Shutdown (low-power) mode; TA holds its last value. No-op while
|
|
194
|
+
* either lock bit is set (the chip ignores SHDN=1 then).
|
|
195
|
+
* @returns {Promise<void>}
|
|
196
|
+
*/
|
|
197
|
+
async shutdown() {
|
|
198
|
+
await this._ready;
|
|
199
|
+
const config = await this._readConfig();
|
|
200
|
+
if (config & _CFG_LOCKS) return;
|
|
201
|
+
await this._writeConfig(config | _CFG_SHDN);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Leave Shutdown mode and resume continuous conversion.
|
|
206
|
+
* @returns {Promise<void>}
|
|
207
|
+
*/
|
|
208
|
+
async wake() {
|
|
209
|
+
await this._ready;
|
|
210
|
+
await this._writeConfig((await this._readConfig()) & ~_CFG_SHDN);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Report whether the sensor is in Shutdown mode.
|
|
215
|
+
* @returns {Promise<boolean>} True if SHDN is set.
|
|
216
|
+
*/
|
|
217
|
+
async isShutdown() {
|
|
218
|
+
await this._ready;
|
|
219
|
+
return ((await this._readReg(_REG_CONFIG)) & _CFG_SHDN) !== 0;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// -- Boundaries ---------------------------------------------------------
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Read the TUPPER boundary.
|
|
226
|
+
* @returns {Promise<number>} Upper boundary in °C (0.25 °C steps).
|
|
227
|
+
*/
|
|
228
|
+
async getUpperLimit() {
|
|
229
|
+
await this._ready;
|
|
230
|
+
return _decodeLimit(await this._readReg(_REG_TUPPER));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Write the TUPPER boundary, rounded to the nearest 0.25 °C (ignored by
|
|
235
|
+
* the chip while WIN_LOCK is set).
|
|
236
|
+
* @param {number} celsius - Upper boundary in °C (−256.0 to 255.75, clamped).
|
|
237
|
+
* @returns {Promise<void>}
|
|
238
|
+
*/
|
|
239
|
+
async setUpperLimit(celsius) {
|
|
240
|
+
await this._ready;
|
|
241
|
+
await this._writeReg(_REG_TUPPER, _encodeLimit(celsius));
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Read the TLOWER boundary.
|
|
246
|
+
* @returns {Promise<number>} Lower boundary in °C (0.25 °C steps).
|
|
247
|
+
*/
|
|
248
|
+
async getLowerLimit() {
|
|
249
|
+
await this._ready;
|
|
250
|
+
return _decodeLimit(await this._readReg(_REG_TLOWER));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Write the TLOWER boundary, rounded to the nearest 0.25 °C (ignored by
|
|
255
|
+
* the chip while WIN_LOCK is set).
|
|
256
|
+
* @param {number} celsius - Lower boundary in °C (−256.0 to 255.75, clamped).
|
|
257
|
+
* @returns {Promise<void>}
|
|
258
|
+
*/
|
|
259
|
+
async setLowerLimit(celsius) {
|
|
260
|
+
await this._ready;
|
|
261
|
+
await this._writeReg(_REG_TLOWER, _encodeLimit(celsius));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Read the TCRIT boundary.
|
|
266
|
+
* @returns {Promise<number>} Critical boundary in °C (0.25 °C steps).
|
|
267
|
+
*/
|
|
268
|
+
async getCriticalLimit() {
|
|
269
|
+
await this._ready;
|
|
270
|
+
return _decodeLimit(await this._readReg(_REG_TCRIT));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Write the TCRIT boundary, rounded to the nearest 0.25 °C (ignored by
|
|
275
|
+
* the chip while CRIT_LOCK is set).
|
|
276
|
+
* @param {number} celsius - Critical boundary in °C (−256.0 to 255.75, clamped).
|
|
277
|
+
* @returns {Promise<void>}
|
|
278
|
+
*/
|
|
279
|
+
async setCriticalLimit(celsius) {
|
|
280
|
+
await this._ready;
|
|
281
|
+
await this._writeReg(_REG_TCRIT, _encodeLimit(celsius));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// -- Hysteresis ---------------------------------------------------------
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Set the boundary hysteresis (applies to the cooling edge only; ignored
|
|
288
|
+
* by the chip while either lock bit is set).
|
|
289
|
+
* @param {number} celsius - One of 0, 1.5, 3.0, 6.0.
|
|
290
|
+
* @returns {Promise<void>}
|
|
291
|
+
* @throws {RangeError} If celsius is not a supported value.
|
|
292
|
+
*/
|
|
293
|
+
async setHysteresis(celsius) {
|
|
294
|
+
const code = _indexOfStep(_HYSTERESES, celsius);
|
|
295
|
+
if (code < 0) throw new RangeError('hysteresis must be one of 0, 1.5, 3.0, 6.0');
|
|
296
|
+
await this._ready;
|
|
297
|
+
const config = (await this._readConfig()) & ~_CFG_THYST_MASK;
|
|
298
|
+
await this._writeConfig(config | (code << _CFG_THYST_SHIFT));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Read the boundary hysteresis.
|
|
303
|
+
* @returns {Promise<number>} Hysteresis in °C.
|
|
304
|
+
*/
|
|
305
|
+
async getHysteresis() {
|
|
306
|
+
await this._ready;
|
|
307
|
+
const config = await this._readReg(_REG_CONFIG);
|
|
308
|
+
return _HYSTERESES[(config & _CFG_THYST_MASK) >> _CFG_THYST_SHIFT];
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// -- Locks --------------------------------------------------------------
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Lock TCRIT (and ALERT_SEL/POL/MOD). Irreversible except by power-on reset.
|
|
315
|
+
* @returns {Promise<void>}
|
|
316
|
+
*/
|
|
317
|
+
async lockCriticalLimit() {
|
|
318
|
+
await this._ready;
|
|
319
|
+
await this._writeConfig((await this._readConfig()) | _CFG_CRIT_LOCK);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Lock TUPPER/TLOWER (and ALERT_SEL/POL/MOD). Irreversible except by
|
|
324
|
+
* power-on reset.
|
|
325
|
+
* @returns {Promise<void>}
|
|
326
|
+
*/
|
|
327
|
+
async lockWindowLimits() {
|
|
328
|
+
await this._ready;
|
|
329
|
+
await this._writeConfig((await this._readConfig()) | _CFG_WIN_LOCK);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Report whether CRIT_LOCK is set.
|
|
334
|
+
* @returns {Promise<boolean>} True if TCRIT is locked.
|
|
335
|
+
*/
|
|
336
|
+
async isCriticalLimitLocked() {
|
|
337
|
+
await this._ready;
|
|
338
|
+
return ((await this._readReg(_REG_CONFIG)) & _CFG_CRIT_LOCK) !== 0;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Report whether WIN_LOCK is set.
|
|
343
|
+
* @returns {Promise<boolean>} True if TUPPER/TLOWER are locked.
|
|
344
|
+
*/
|
|
345
|
+
async isWindowLimitsLocked() {
|
|
346
|
+
await this._ready;
|
|
347
|
+
return ((await this._readReg(_REG_CONFIG)) & _CFG_WIN_LOCK) !== 0;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// -- Alert output -------------------------------------------------------
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Configure the Alert output's source, mode and polarity together.
|
|
354
|
+
* @param {'all'|'critical_only'} [mode='all'] - Boundaries that drive the Alert output.
|
|
355
|
+
* @param {'comparator'|'interrupt'} [output='comparator'] - Comparator or latching interrupt output.
|
|
356
|
+
* @param {'active_low'|'active_high'} [polarity='active_low'] - Output polarity.
|
|
357
|
+
* @returns {Promise<void>}
|
|
358
|
+
* @throws {RangeError} If an argument is not one of the listed values.
|
|
359
|
+
* @throws {Error} If either lock bit is set (the bits are frozen).
|
|
360
|
+
*/
|
|
361
|
+
async configureAlert(mode = 'all', output = 'comparator', polarity = 'active_low') {
|
|
362
|
+
if (mode !== 'all' && mode !== 'critical_only') throw new RangeError("mode must be 'all' or 'critical_only'");
|
|
363
|
+
if (output !== 'comparator' && output !== 'interrupt') throw new RangeError("output must be 'comparator' or 'interrupt'");
|
|
364
|
+
if (polarity !== 'active_low' && polarity !== 'active_high') throw new RangeError("polarity must be 'active_low' or 'active_high'");
|
|
365
|
+
await this._ready;
|
|
366
|
+
let config = await this._readConfig();
|
|
367
|
+
if (config & _CFG_LOCKS) throw new Error('MCP9808: Alert configuration is locked until power-on reset');
|
|
368
|
+
config &= ~(_CFG_ALERT_SEL | _CFG_ALERT_POL | _CFG_ALERT_MOD);
|
|
369
|
+
if (mode === 'critical_only') config |= _CFG_ALERT_SEL;
|
|
370
|
+
if (polarity === 'active_high') config |= _CFG_ALERT_POL;
|
|
371
|
+
if (output === 'interrupt') config |= _CFG_ALERT_MOD;
|
|
372
|
+
await this._writeConfig(config);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Enable the Alert output (ALERT_CNT = 1).
|
|
377
|
+
* @returns {Promise<void>}
|
|
378
|
+
*/
|
|
379
|
+
async enableAlert() {
|
|
380
|
+
await this._ready;
|
|
381
|
+
await this._writeConfig((await this._readConfig()) | _CFG_ALERT_CNT);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Disable the Alert output (ALERT_CNT = 0).
|
|
386
|
+
* @returns {Promise<void>}
|
|
387
|
+
*/
|
|
388
|
+
async disableAlert() {
|
|
389
|
+
await this._ready;
|
|
390
|
+
await this._writeConfig((await this._readConfig()) & ~_CFG_ALERT_CNT);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Report whether the Alert output is currently asserted.
|
|
395
|
+
* @returns {Promise<boolean>} True if ALERT_STAT is set.
|
|
396
|
+
*/
|
|
397
|
+
async isAlertAsserted() {
|
|
398
|
+
await this._ready;
|
|
399
|
+
return ((await this._readReg(_REG_CONFIG)) & _CFG_ALERT_STAT) !== 0;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Clear an asserted interrupt-mode Alert output (INT_CLEAR = 1). Has no
|
|
404
|
+
* effect in comparator mode.
|
|
405
|
+
* @returns {Promise<void>}
|
|
406
|
+
*/
|
|
407
|
+
async clearInterrupt() {
|
|
408
|
+
await this._ready;
|
|
409
|
+
await this._writeReg(_REG_CONFIG, (await this._readConfig()) | _CFG_INT_CLEAR);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// -- Interrupt API (Level 2) --------------------------------------------
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Read TA's live boundary-status bits. Nothing is cleared — the bits are
|
|
416
|
+
* a live comparison, always current.
|
|
417
|
+
* @returns {Promise<number>} Mask of SOURCE_LOWER / SOURCE_UPPER / SOURCE_CRITICAL.
|
|
418
|
+
*/
|
|
419
|
+
async pollInterrupt() {
|
|
420
|
+
await this._ready;
|
|
421
|
+
return ((await this._readReg(_REG_TA)) >> 13) & 0x07;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Subscribe to Alert events. Uses `connection.intPin.onEdge()` when an
|
|
426
|
+
* InputPin is wired — the edge follows the configured ALERT_POL (falling
|
|
427
|
+
* for active-low, rising for active-high) and the callback runs on every
|
|
428
|
+
* edge. Otherwise falls back to a 5 ms polling loop that calls back
|
|
429
|
+
* whenever the status mask changes. The Alert output must be enabled
|
|
430
|
+
* (`enableAlert()`) for a pin to see edges.
|
|
431
|
+
* @param {function(number): void} callback - Receives the pollInterrupt() mask.
|
|
432
|
+
* @returns {Promise<void>}
|
|
433
|
+
*/
|
|
434
|
+
async onInterrupt(callback) {
|
|
435
|
+
await this._ready;
|
|
436
|
+
this._callback = callback;
|
|
437
|
+
if (this._conn.intPin) {
|
|
438
|
+
const activeHigh = ((await this._readReg(_REG_CONFIG)) & _CFG_ALERT_POL) !== 0;
|
|
439
|
+
this._edgeHandler = async () => {
|
|
440
|
+
const status = await this.pollInterrupt();
|
|
441
|
+
if (this._callback) this._callback(status);
|
|
442
|
+
};
|
|
443
|
+
await this._conn.intPin.onEdge(this._edgeHandler, activeHigh ? 'rising' : 'falling');
|
|
444
|
+
} else {
|
|
445
|
+
let last = await this.pollInterrupt();
|
|
446
|
+
let busy = false;
|
|
447
|
+
this._pollTimer = setInterval(async () => {
|
|
448
|
+
if (busy) return;
|
|
449
|
+
busy = true;
|
|
450
|
+
try {
|
|
451
|
+
const status = await this.pollInterrupt();
|
|
452
|
+
if (status !== last) {
|
|
453
|
+
last = status;
|
|
454
|
+
if (this._callback) this._callback(status);
|
|
455
|
+
}
|
|
456
|
+
} finally {
|
|
457
|
+
busy = false;
|
|
458
|
+
}
|
|
459
|
+
}, 5);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Unsubscribe and stop delivery.
|
|
465
|
+
* @returns {Promise<void>}
|
|
466
|
+
*/
|
|
467
|
+
async offInterrupt() {
|
|
468
|
+
if (this._conn.intPin && this._edgeHandler) {
|
|
469
|
+
await this._conn.intPin.offEdge(this._edgeHandler);
|
|
470
|
+
this._edgeHandler = null;
|
|
471
|
+
}
|
|
472
|
+
if (this._pollTimer) {
|
|
473
|
+
clearInterval(this._pollTimer);
|
|
474
|
+
this._pollTimer = null;
|
|
475
|
+
}
|
|
476
|
+
this._callback = null;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** Default 7-bit I²C address (A0 = A1 = A2 = GND). Valid range 0x18–0x1F. */
|
|
481
|
+
MCP9808Minimal.I2C_ADDRESS = 0x18;
|
|
482
|
+
|
|
483
|
+
/** TA < TLOWER. */
|
|
484
|
+
MCP9808Full.SOURCE_LOWER = 0x01;
|
|
485
|
+
/** TA > TUPPER. */
|
|
486
|
+
MCP9808Full.SOURCE_UPPER = 0x02;
|
|
487
|
+
/** TA ≥ TCRIT. */
|
|
488
|
+
MCP9808Full.SOURCE_CRITICAL = 0x04;
|
|
489
|
+
|
|
490
|
+
module.exports = { MCP9808Minimal, MCP9808Full };
|