periph 1.4.0 → 1.5.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 +27 -0
- package/examples/adc_dac/ad5251/complete.js +66 -0
- package/examples/adc_dac/ad5251/demo.js +69 -0
- package/examples/adc_dac/ad5251/minimal.js +23 -0
- package/examples/adc_dac/ad5252/complete.js +66 -0
- package/examples/adc_dac/ad5252/demo.js +69 -0
- package/examples/adc_dac/ad5252/minimal.js +23 -0
- package/examples/adc_dac/ads7828/complete.js +46 -0
- package/examples/adc_dac/ads7828/demo.js +45 -0
- package/examples/adc_dac/ads7828/minimal.js +17 -0
- package/examples/adc_dac/cat5171/complete.js +52 -0
- package/examples/adc_dac/cat5171/demo.js +59 -0
- package/examples/adc_dac/cat5171/minimal.js +23 -0
- package/examples/adc_dac/cs43l22/complete.js +88 -0
- package/examples/adc_dac/cs43l22/demo.js +56 -0
- package/examples/adc_dac/cs43l22/minimal.js +20 -0
- package/examples/adc_dac/ds1881/complete.js +62 -0
- package/examples/adc_dac/ds1881/demo.js +61 -0
- package/examples/adc_dac/ds1881/minimal.js +25 -0
- package/examples/adc_dac/ds3502/complete.js +46 -0
- package/examples/adc_dac/ds3502/demo.js +54 -0
- package/examples/adc_dac/ds3502/minimal.js +28 -0
- package/examples/adc_dac/mcp3422/complete.js +48 -0
- package/examples/adc_dac/mcp3422/demo.js +49 -0
- package/examples/adc_dac/mcp3422/minimal.js +25 -0
- package/examples/display/sh1106/complete.js +101 -0
- package/examples/display/sh1106/demo.js +68 -0
- package/examples/display/sh1106/minimal.js +25 -0
- package/examples/display/ssd1305/complete.js +129 -0
- package/examples/display/ssd1305/demo.js +71 -0
- package/examples/display/ssd1305/minimal.js +25 -0
- package/examples/display/ssd1306/complete.js +115 -0
- package/examples/display/ssd1306/demo.js +70 -0
- package/examples/display/ssd1306/minimal.js +25 -0
- package/examples/environmental/am2315/complete.js +34 -0
- package/examples/environmental/am2315/demo.js +46 -0
- package/examples/environmental/am2315/minimal.js +21 -0
- package/examples/humidity/htu21df/complete.js +49 -0
- package/examples/humidity/htu21df/demo.js +50 -0
- package/examples/humidity/htu21df/minimal.js +22 -0
- package/examples/humidity/sht31/complete.js +53 -0
- package/examples/humidity/sht31/demo.js +53 -0
- package/examples/humidity/sht31/minimal.js +21 -0
- package/examples/humidity/si7021/complete.js +55 -0
- package/examples/humidity/si7021/demo.js +58 -0
- package/examples/humidity/si7021/minimal.js +21 -0
- package/examples/imu/bno055/complete.js +85 -0
- package/examples/imu/bno055/demo.js +70 -0
- package/examples/imu/bno055/minimal.js +20 -0
- package/examples/io_expander/pca6408a/complete.js +63 -0
- package/examples/io_expander/pca6408a/demo.js +58 -0
- package/examples/io_expander/pca6408a/minimal.js +20 -0
- package/examples/io_expander/pca6416a/complete.js +68 -0
- package/examples/io_expander/pca6416a/demo.js +60 -0
- package/examples/io_expander/pca6416a/minimal.js +20 -0
- package/examples/io_expander/pca9536/complete.js +63 -0
- package/examples/io_expander/pca9536/demo.js +57 -0
- package/examples/io_expander/pca9536/minimal.js +20 -0
- package/examples/io_expander/pca9539/complete.js +68 -0
- package/examples/io_expander/pca9539/demo.js +60 -0
- package/examples/io_expander/pca9539/minimal.js +20 -0
- package/examples/light/max44009/complete.js +56 -0
- package/examples/light/max44009/demo.js +49 -0
- package/examples/light/max44009/minimal.js +22 -0
- package/examples/memory/at24c02n/complete.js +39 -0
- package/examples/memory/at24c02n/demo.js +39 -0
- package/examples/memory/at24c02n/minimal.js +15 -0
- package/examples/memory/at24c64/complete.js +39 -0
- package/examples/memory/at24c64/demo.js +39 -0
- package/examples/memory/at24c64/minimal.js +15 -0
- package/examples/memory/cat24c512/complete.js +39 -0
- package/examples/memory/cat24c512/demo.js +39 -0
- package/examples/memory/cat24c512/minimal.js +15 -0
- package/examples/other/pca9541/complete.js +52 -0
- package/examples/other/pca9541/demo.js +61 -0
- package/examples/other/pca9541/minimal.js +23 -0
- package/examples/temperature/tmp102/complete.js +74 -0
- package/examples/temperature/tmp102/demo.js +71 -0
- package/examples/temperature/tmp102/minimal.js +20 -0
- package/examples/tof/vl6180x/complete.js +62 -0
- package/examples/tof/vl6180x/demo.js +77 -0
- package/examples/tof/vl6180x/minimal.js +23 -0
- package/package.json +1 -1
- package/src/chips/adc_dac/ad5251.js +345 -0
- package/src/chips/adc_dac/ad5252.js +345 -0
- package/src/chips/adc_dac/ads7828.js +225 -0
- package/src/chips/adc_dac/cat5171.js +213 -0
- package/src/chips/adc_dac/cs43l22.js +645 -0
- package/src/chips/adc_dac/ds1881.js +357 -0
- package/src/chips/adc_dac/ds3502.js +241 -0
- package/src/chips/adc_dac/mcp3422.js +312 -0
- package/src/chips/display/mono_oled.js +182 -0
- package/src/chips/display/sh1106.js +496 -0
- package/src/chips/display/ssd1305.js +655 -0
- package/src/chips/display/ssd1306.js +458 -0
- package/src/chips/environmental/am2315.js +237 -0
- package/src/chips/humidity/htu21df.js +222 -0
- package/src/chips/humidity/sht31.js +284 -0
- package/src/chips/humidity/si7021.js +282 -0
- package/src/chips/imu/bno055.js +496 -0
- package/src/chips/io_expander/pca6408a.js +407 -0
- package/src/chips/io_expander/pca6416a.js +455 -0
- package/src/chips/io_expander/pca9536.js +398 -0
- package/src/chips/io_expander/pca9539.js +457 -0
- package/src/chips/light/max44009.js +336 -0
- package/src/chips/memory/at24c02n.js +220 -0
- package/src/chips/memory/at24c64.js +220 -0
- package/src/chips/memory/cat24c512.js +221 -0
- package/src/chips/other/pca9541.js +315 -0
- package/src/chips/temperature/tmp102.js +361 -0
- package/src/chips/tof/vl6180x.js +580 -0
- package/src/discovery/registry.js +65 -13
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const REG_IE = 0x00;
|
|
4
|
+
const REG_CONTROL = 0x01;
|
|
5
|
+
const REG_ISTAT = 0x02;
|
|
6
|
+
const AI = 0x10;
|
|
7
|
+
|
|
8
|
+
const C_MYBUS = 0x01;
|
|
9
|
+
const C_NMYBUS = 0x02;
|
|
10
|
+
const C_BUSON = 0x04;
|
|
11
|
+
const C_NBUSON = 0x08;
|
|
12
|
+
const C_BUSINIT = 0x10;
|
|
13
|
+
const C_TESTON = 0x40;
|
|
14
|
+
const C_NTESTON = 0x80;
|
|
15
|
+
|
|
16
|
+
const MASKABLE = 0x0F;
|
|
17
|
+
const POLL_INTERVAL_MS = 100;
|
|
18
|
+
|
|
19
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
20
|
+
|
|
21
|
+
const hasControlBits = (c) => ((c & C_MYBUS) !== 0) === ((c & C_NMYBUS) !== 0);
|
|
22
|
+
const connectedBits = (c) => ((c & C_BUSON) !== 0) !== ((c & C_NBUSON) !== 0);
|
|
23
|
+
const nbuson = (c) => (c & C_NBUSON ? 1 : 0);
|
|
24
|
+
const nmybus = (c) => (c & C_NMYBUS ? 1 : 0);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* PCA9541 2-to-1 I²C-bus master selector (NXP) — minimal interface.
|
|
28
|
+
*
|
|
29
|
+
* Two upstream masters share one downstream bus of slave devices through the
|
|
30
|
+
* PCA9541's pass gates; this driver is used by **one** of the two masters (the
|
|
31
|
+
* same software runs on both). There is no arbitration: whichever master writes
|
|
32
|
+
* its CONTROL register last before a STOP wins. Each master sees its own copy of
|
|
33
|
+
* the IE, CONTROL and ISTAT registers behind the same addresses. The 7-bit I²C
|
|
34
|
+
* address is 0x70 + A (A3..A0); prefer 0x70–0x77.
|
|
35
|
+
*
|
|
36
|
+
* `init()` reads CONTROL as a bus check (it rejects if the chip does not
|
|
37
|
+
* acknowledge) and ISTAT once to clear stale flags; it writes nothing, so
|
|
38
|
+
* initialising a master never changes the connection state. The CONTROL byte to
|
|
39
|
+
* write depends on the read-only complement bits, so every change reads CONTROL
|
|
40
|
+
* first and applies the datasheet's Table 12 rule.
|
|
41
|
+
*/
|
|
42
|
+
class PCA9541Minimal {
|
|
43
|
+
/**
|
|
44
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (1-byte command code).
|
|
45
|
+
*/
|
|
46
|
+
constructor(connection) {
|
|
47
|
+
this._conn = connection;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 7-bit I²C address with A3..A0 = 0. */
|
|
51
|
+
static get I2C_ADDRESS() { return 0x70; }
|
|
52
|
+
|
|
53
|
+
async _readReg(reg) {
|
|
54
|
+
return (await this._conn.readReg(reg, 1))[0];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async _writeReg(reg, value) {
|
|
58
|
+
await this._conn.writeReg(reg, value & 0xFF);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Run the initialization sequence: read CONTROL as a bus check and ISTAT once
|
|
63
|
+
* to clear stale BUSLOST / BUSOK / BUSINIT flags. Writes nothing.
|
|
64
|
+
* @returns {Promise<void>}
|
|
65
|
+
*/
|
|
66
|
+
async init() {
|
|
67
|
+
await this._readReg(REG_CONTROL);
|
|
68
|
+
await this._readReg(REG_ISTAT);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Report whether this master's channel is connected to the downstream bus.
|
|
73
|
+
* @returns {Promise<boolean>} true if MYBUS equals NMYBUS and the bus is connected (BUSON differs from NBUSON).
|
|
74
|
+
*/
|
|
75
|
+
async hasControl() {
|
|
76
|
+
const c = await this._readReg(REG_CONTROL);
|
|
77
|
+
return hasControlBits(c) && connectedBits(c);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Take the downstream bus. Reads CONTROL and writes MYBUS / BUSON per the
|
|
82
|
+
* datasheet rule; the write ends with a STOP, which makes the switch. With
|
|
83
|
+
* `busInit` the chip first sends nine clocks, a NACK and a STOP on the
|
|
84
|
+
* downstream bus and flags BUSINIT in ISTAT.
|
|
85
|
+
* @param {boolean} [busInit=false] - Request bus initialization / recovery.
|
|
86
|
+
* @param {number} [timeoutMs=20] - Maximum time to wait for the switch, in milliseconds.
|
|
87
|
+
* @returns {Promise<boolean>} true if this master has control afterwards, false if not within `timeoutMs`.
|
|
88
|
+
*/
|
|
89
|
+
async acquire(busInit = false, timeoutMs = 20) {
|
|
90
|
+
const c = await this._readReg(REG_CONTROL);
|
|
91
|
+
let w = ((1 - nbuson(c)) << 2) | nmybus(c);
|
|
92
|
+
if (busInit) w |= C_BUSINIT;
|
|
93
|
+
await this._writeReg(REG_CONTROL, w);
|
|
94
|
+
const attempts = Math.floor(timeoutMs) + 1;
|
|
95
|
+
for (let i = 0; i < attempts; i++) {
|
|
96
|
+
if (busInit && !((await this._readReg(REG_ISTAT)) & 0x02)) {
|
|
97
|
+
await sleep(1);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (await this.hasControl()) return true;
|
|
101
|
+
await sleep(1);
|
|
102
|
+
}
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Give the downstream bus to the other master (MYBUS = NOT NMYBUS; the bus stays connected).
|
|
108
|
+
* @returns {Promise<void>}
|
|
109
|
+
*/
|
|
110
|
+
async release() {
|
|
111
|
+
const c = await this._readReg(REG_CONTROL);
|
|
112
|
+
await this._writeReg(REG_CONTROL, ((1 - nbuson(c)) << 2) | (1 - nmybus(c)));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* PCA9541 full interface — extends Minimal with raw control access, connect /
|
|
118
|
+
* disconnect, bus recovery, interrupt masks, the INT functional test and
|
|
119
|
+
* interrupt delivery.
|
|
120
|
+
*/
|
|
121
|
+
class PCA9541Full extends PCA9541Minimal {
|
|
122
|
+
constructor(connection) {
|
|
123
|
+
super(connection);
|
|
124
|
+
this._callback = null;
|
|
125
|
+
this._intPinUsed = null;
|
|
126
|
+
this._pollTimer = null;
|
|
127
|
+
this._polling = false;
|
|
128
|
+
this._edgeHandler = () => { this._service(); };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static get SOURCE_INT_IN() { return 0x01; }
|
|
132
|
+
static get SOURCE_BUS_INIT() { return 0x02; }
|
|
133
|
+
static get SOURCE_BUS_NOT_IDLE() { return 0x04; }
|
|
134
|
+
static get SOURCE_BUS_LOST() { return 0x08; }
|
|
135
|
+
static get SOURCE_TEST() { return 0x40; }
|
|
136
|
+
static get SOURCE_OTHER_TEST() { return 0x80; }
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Read the raw CONTROL byte.
|
|
140
|
+
* @returns {Promise<number>}
|
|
141
|
+
*/
|
|
142
|
+
async getControl() {
|
|
143
|
+
return this._readReg(REG_CONTROL);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Whether the downstream bus is connected to some master (BUSON differs from NBUSON).
|
|
148
|
+
* @returns {Promise<boolean>}
|
|
149
|
+
*/
|
|
150
|
+
async isConnected() {
|
|
151
|
+
return connectedBits(await this._readReg(REG_CONTROL));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Decode the CONTROL register.
|
|
156
|
+
* @returns {Promise<{hasControl: boolean, connected: boolean, busInitRequested: boolean, ownTest: boolean, otherTest: boolean}>}
|
|
157
|
+
*/
|
|
158
|
+
async status() {
|
|
159
|
+
const c = await this._readReg(REG_CONTROL);
|
|
160
|
+
return {
|
|
161
|
+
hasControl: hasControlBits(c),
|
|
162
|
+
connected: connectedBits(c),
|
|
163
|
+
busInitRequested: Boolean(c & C_BUSINIT),
|
|
164
|
+
ownTest: Boolean(c & C_TESTON),
|
|
165
|
+
otherTest: Boolean(c & C_NTESTON),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Turn the connection on without changing ownership (BUSON = NOT NBUSON; MYBUS as read).
|
|
171
|
+
* @returns {Promise<void>}
|
|
172
|
+
*/
|
|
173
|
+
async connect() {
|
|
174
|
+
const c = await this._readReg(REG_CONTROL);
|
|
175
|
+
await this._writeReg(REG_CONTROL, ((1 - nbuson(c)) << 2) | (c & C_MYBUS));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Disconnect both masters from the downstream bus (BUSON = NBUSON; ownership bits left).
|
|
180
|
+
* @returns {Promise<void>}
|
|
181
|
+
*/
|
|
182
|
+
async disconnect() {
|
|
183
|
+
const c = await this._readReg(REG_CONTROL);
|
|
184
|
+
await this._writeReg(REG_CONTROL, (nbuson(c) << 2) | (c & C_MYBUS));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Recover a hung downstream bus and take it (acquire with bus initialization).
|
|
189
|
+
* @param {number} [timeoutMs=20] - Maximum time to wait for BUSINIT, in milliseconds.
|
|
190
|
+
* @returns {Promise<boolean>} true if this master has control afterwards.
|
|
191
|
+
*/
|
|
192
|
+
async recoverBus(timeoutMs = 20) {
|
|
193
|
+
return this.acquire(true, timeoutMs);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Write the IE register (true = masked, no interrupt); all-false re-enables every interrupt.
|
|
198
|
+
* @param {object} [mask]
|
|
199
|
+
* @param {boolean} [mask.busLost=false] - Mask the "other master took the bus" interrupt.
|
|
200
|
+
* @param {boolean} [mask.busNotIdle=false] - Mask the "bus non-idle at the switch" interrupt.
|
|
201
|
+
* @param {boolean} [mask.busInit=false] - Mask the "bus initialization done" interrupt.
|
|
202
|
+
* @param {boolean} [mask.intIn=false] - Mask the downstream INT_IN interrupt.
|
|
203
|
+
* @returns {Promise<void>}
|
|
204
|
+
*/
|
|
205
|
+
async setInterruptMask({ busLost = false, busNotIdle = false, busInit = false, intIn = false } = {}) {
|
|
206
|
+
await this._writeReg(REG_IE, (busLost ? 8 : 0) | (busNotIdle ? 4 : 0) | (busInit ? 2 : 0) | (intIn ? 1 : 0));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Read the IE register (bit set = interrupt masked).
|
|
211
|
+
* @returns {Promise<number>}
|
|
212
|
+
*/
|
|
213
|
+
async getInterruptMask() {
|
|
214
|
+
return this._readReg(REG_IE);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Drive the INT lines for the functional test; the bus bits are written back unchanged.
|
|
219
|
+
* @param {boolean} [own=false] - Drive this master's INT low (TESTON).
|
|
220
|
+
* @param {boolean} [other=false] - Drive the other master's INT low (NTESTON).
|
|
221
|
+
* @returns {Promise<void>}
|
|
222
|
+
*/
|
|
223
|
+
async testInterrupt(own = false, other = false) {
|
|
224
|
+
const c = await this._readReg(REG_CONTROL);
|
|
225
|
+
await this._writeReg(REG_CONTROL, (c & (C_BUSON | C_MYBUS)) | (own ? C_TESTON : 0) | (other ? C_NTESTON : 0));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Read IE, CONTROL and ISTAT in one auto-increment transaction (clears the self-clearing ISTAT flags).
|
|
230
|
+
* @returns {Promise<{ie: number, control: number, istat: number}>}
|
|
231
|
+
*/
|
|
232
|
+
async readAll() {
|
|
233
|
+
const b = await this._conn.readReg(AI | REG_IE, 3);
|
|
234
|
+
return { ie: b[0], control: b[1], istat: b[2] };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Enable an interrupt source (clear its mask bit in IE); the test sources need no enable.
|
|
239
|
+
* @param {number} source - One of SOURCE_INT_IN, SOURCE_BUS_INIT, SOURCE_BUS_NOT_IDLE, SOURCE_BUS_LOST.
|
|
240
|
+
* @returns {Promise<void>}
|
|
241
|
+
*/
|
|
242
|
+
async enableInterrupt(source) {
|
|
243
|
+
const bit = source & MASKABLE;
|
|
244
|
+
if (bit) await this._writeReg(REG_IE, (await this._readReg(REG_IE)) & ~bit);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Disable an interrupt source (set its mask bit in IE).
|
|
249
|
+
* @param {number} source - One of SOURCE_INT_IN, SOURCE_BUS_INIT, SOURCE_BUS_NOT_IDLE, SOURCE_BUS_LOST.
|
|
250
|
+
* @returns {Promise<void>}
|
|
251
|
+
*/
|
|
252
|
+
async disableInterrupt(source) {
|
|
253
|
+
const bit = source & MASKABLE;
|
|
254
|
+
if (bit) await this._writeReg(REG_IE, (await this._readReg(REG_IE)) | bit);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Read ISTAT (clears BUSLOST / BUSOK / BUSINIT).
|
|
259
|
+
* @returns {Promise<number>} ISTAT byte; bits as the SOURCE_* constants.
|
|
260
|
+
*/
|
|
261
|
+
async pollInterrupt() {
|
|
262
|
+
return this._readReg(REG_ISTAT);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async _arm(intPin) {
|
|
266
|
+
this._intPinUsed = intPin;
|
|
267
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
268
|
+
if (intPin) {
|
|
269
|
+
await intPin.onEdge(this._edgeHandler, 'falling');
|
|
270
|
+
} else {
|
|
271
|
+
this._pollTimer = setInterval(() => { this._service(); }, POLL_INTERVAL_MS);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Subscribe to this master's INT pin (falling edge). `callback(status)`
|
|
277
|
+
* receives the ISTAT byte read (which clears the self-clearing flags).
|
|
278
|
+
* Delivery is via the INT line (options.intPin or connection.intPin) or a
|
|
279
|
+
* 100 ms polling timer.
|
|
280
|
+
* @param {function} callback - Called with (status).
|
|
281
|
+
* @param {object} [options]
|
|
282
|
+
* @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
|
|
283
|
+
* @returns {Promise<void>}
|
|
284
|
+
*/
|
|
285
|
+
async onInterrupt(callback, options = {}) {
|
|
286
|
+
this._callback = callback;
|
|
287
|
+
await this._arm(options.intPin ?? this._conn.intPin ?? null);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Unsubscribe and stop delivery.
|
|
292
|
+
* @returns {Promise<void>}
|
|
293
|
+
*/
|
|
294
|
+
async offInterrupt() {
|
|
295
|
+
if (this._intPinUsed) {
|
|
296
|
+
await this._intPinUsed.offEdge(this._edgeHandler);
|
|
297
|
+
this._intPinUsed = null;
|
|
298
|
+
}
|
|
299
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
300
|
+
this._callback = null;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async _service() {
|
|
304
|
+
if (this._polling) return;
|
|
305
|
+
this._polling = true;
|
|
306
|
+
try {
|
|
307
|
+
const status = await this.pollInterrupt();
|
|
308
|
+
if (status && this._callback) this._callback(status);
|
|
309
|
+
} finally {
|
|
310
|
+
this._polling = false;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
module.exports = { PCA9541Minimal, PCA9541Full };
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_TEMPERATURE = 0x00;
|
|
4
|
+
const _REG_CONFIG = 0x01;
|
|
5
|
+
const _REG_T_LOW = 0x02;
|
|
6
|
+
const _REG_T_HIGH = 0x03;
|
|
7
|
+
|
|
8
|
+
// CONFIGURATION (0x01) bits.
|
|
9
|
+
const _CFG_OS = 0x8000;
|
|
10
|
+
const _CFG_F_SHIFT = 11;
|
|
11
|
+
const _CFG_F_MASK = 0x1800;
|
|
12
|
+
const _CFG_POL = 0x0400;
|
|
13
|
+
const _CFG_TM = 0x0200;
|
|
14
|
+
const _CFG_SD = 0x0100;
|
|
15
|
+
const _CFG_CR_SHIFT = 6;
|
|
16
|
+
const _CFG_CR_MASK = 0x00C0;
|
|
17
|
+
const _CFG_AL = 0x0020;
|
|
18
|
+
const _CFG_EM = 0x0010;
|
|
19
|
+
const _CFG_WRITABLE = 0x9FD0;
|
|
20
|
+
const _CFG_RESERVED = 0x6000; // R1:R0, read-only, always 11
|
|
21
|
+
|
|
22
|
+
const _LSB_C = 0.0625;
|
|
23
|
+
const _LIMIT_MIN = -55.0;
|
|
24
|
+
const _LIMIT_MAX = 150.0;
|
|
25
|
+
const _CONVERSION_RATES = [0.25, 1.0, 4.0, 8.0];
|
|
26
|
+
const _FAULT_QUEUES = [1, 2, 4, 6];
|
|
27
|
+
|
|
28
|
+
const _sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
29
|
+
|
|
30
|
+
function _signed(value, bits) {
|
|
31
|
+
const sign = 1 << (bits - 1);
|
|
32
|
+
return (value ^ sign) - sign;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Raw signed count of a TEMPERATURE/limit register; bit 0 = 1 marks the 13-bit format.
|
|
36
|
+
function _decodeRaw(raw16) {
|
|
37
|
+
if (raw16 & 0x0001) return _signed(raw16 >> 3, 13);
|
|
38
|
+
return _signed(raw16 >> 4, 12);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* TMP102 12-bit digital temperature sensor with ALERT output (Texas
|
|
43
|
+
* Instruments) — minimal interface.
|
|
44
|
+
*
|
|
45
|
+
* Two-wire temperature sensor (0.0625 °C per LSB, ±0.5 °C typical) read over
|
|
46
|
+
* an I²C/SMBus-compatible bus. Registers are 16-bit, big-endian, addressed
|
|
47
|
+
* through a persistent Register Pointer. Four selectable addresses
|
|
48
|
+
* (0x48–0x4B) via the ADD0 strap.
|
|
49
|
+
*
|
|
50
|
+
* The constructor makes no bus access: the power-on default (continuous
|
|
51
|
+
* conversion at 4 Hz, comparator mode, 12-bit) already serves the primary
|
|
52
|
+
* use case.
|
|
53
|
+
*/
|
|
54
|
+
class TMP102Minimal {
|
|
55
|
+
/**
|
|
56
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x48–0x4B).
|
|
57
|
+
*/
|
|
58
|
+
constructor(connection) {
|
|
59
|
+
this._conn = connection;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async _readReg(reg) {
|
|
63
|
+
const buf = await this._conn.readReg(reg & 0xFF, 2);
|
|
64
|
+
return buf.readUInt16BE(0);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async _writeReg(reg, value) {
|
|
68
|
+
await this._conn.writeReg(reg & 0xFF, Buffer.from([(value >> 8) & 0xFF, value & 0xFF]));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Read the temperature. Decodes the 12-bit (0.0625 °C per LSB) or, in
|
|
73
|
+
* extended mode, 13-bit two's-complement value; the format is taken from
|
|
74
|
+
* bit 0 of the result.
|
|
75
|
+
* @returns {Promise<number>} Temperature in °C.
|
|
76
|
+
*/
|
|
77
|
+
async readTemperature() {
|
|
78
|
+
return _decodeRaw(await this._readReg(_REG_TEMPERATURE)) * _LSB_C;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* TMP102 full interface — extends Minimal with conversion rate, extended mode,
|
|
84
|
+
* the T_LOW/T_HIGH limits, fault queue, polarity, thermostat mode,
|
|
85
|
+
* shutdown/one-shot, alert status, general-call reset and the ALERT
|
|
86
|
+
* interrupt API.
|
|
87
|
+
*/
|
|
88
|
+
class TMP102Full extends TMP102Minimal {
|
|
89
|
+
/**
|
|
90
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x48–0x4B).
|
|
91
|
+
*/
|
|
92
|
+
constructor(connection) {
|
|
93
|
+
super(connection);
|
|
94
|
+
this._callback = null;
|
|
95
|
+
this._edgeHandler = null;
|
|
96
|
+
this._pollTimer = null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async _updateConfig(clear, set) {
|
|
100
|
+
const config = ((await this.readConfig()) & ~clear) | set;
|
|
101
|
+
await this.writeConfig(config);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Read the raw signed temperature count.
|
|
106
|
+
* @returns {Promise<number>} Signed 12-bit (13-bit in extended mode) count; temperature = count × 0.0625 °C.
|
|
107
|
+
*/
|
|
108
|
+
async readRaw() {
|
|
109
|
+
return _decodeRaw(await this._readReg(_REG_TEMPERATURE));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Read the 16-bit CONFIGURATION register (reset value 0x60A0).
|
|
114
|
+
* @returns {Promise<number>} Register value.
|
|
115
|
+
*/
|
|
116
|
+
async readConfig() {
|
|
117
|
+
return this._readReg(_REG_CONFIG);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Write the 16-bit CONFIGURATION register; read-only bits (R1:R0, AL,
|
|
122
|
+
* bits 3:0) are masked out.
|
|
123
|
+
* @param {number} value - Register value; only OS, F1:F0, POL, TM, SD, CR1:CR0 and EM are applied.
|
|
124
|
+
* @returns {Promise<void>}
|
|
125
|
+
*/
|
|
126
|
+
async writeConfig(value) {
|
|
127
|
+
await this._writeReg(_REG_CONFIG, (value & _CFG_WRITABLE) | _CFG_RESERVED);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Set the conversion rate.
|
|
132
|
+
* @param {number} hz - 0.25, 1, 4 or 8 Hz.
|
|
133
|
+
* @returns {Promise<void>}
|
|
134
|
+
* @throws {RangeError} If hz is not a supported rate.
|
|
135
|
+
*/
|
|
136
|
+
async setConversionRate(hz) {
|
|
137
|
+
const code = _CONVERSION_RATES.indexOf(hz);
|
|
138
|
+
if (code < 0) throw new RangeError('hz must be 0.25, 1, 4 or 8');
|
|
139
|
+
await this._updateConfig(_CFG_CR_MASK, code << _CFG_CR_SHIFT);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Enable or disable the 13-bit extended mode (EM bit).
|
|
144
|
+
* @param {boolean} on - true for 13-bit (-256…+255.9375 °C), false for 12-bit.
|
|
145
|
+
* @returns {Promise<void>}
|
|
146
|
+
*/
|
|
147
|
+
async setExtendedMode(on) {
|
|
148
|
+
await this._updateConfig(_CFG_EM, on ? _CFG_EM : 0);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async _setLimit(reg, celsius) {
|
|
152
|
+
const extended = ((await this.readConfig()) & _CFG_EM) !== 0;
|
|
153
|
+
let c = Math.min(Math.max(celsius, _LIMIT_MIN), _LIMIT_MAX);
|
|
154
|
+
const steps = c / _LSB_C;
|
|
155
|
+
let counts = steps >= 0 ? Math.floor(steps + 0.5) : -Math.floor(-steps + 0.5);
|
|
156
|
+
if (extended) {
|
|
157
|
+
await this._writeReg(reg, (counts << 3) & 0xFFF8);
|
|
158
|
+
} else {
|
|
159
|
+
if (counts > 2047) counts = 2047;
|
|
160
|
+
await this._writeReg(reg, (counts << 4) & 0xFFF0);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async _getLimit(reg) {
|
|
165
|
+
const extended = ((await this.readConfig()) & _CFG_EM) !== 0;
|
|
166
|
+
const raw16 = await this._readReg(reg);
|
|
167
|
+
const counts = extended ? _signed(raw16 >> 3, 13) : _signed(raw16 >> 4, 12);
|
|
168
|
+
return counts * _LSB_C;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Set the T_HIGH limit (reset 80 °C).
|
|
173
|
+
* @param {number} celsius - Limit in °C, rounded to 0.0625 °C and clamped to -55…+150 °C (12-bit mode: up to +127.9375 °C).
|
|
174
|
+
* @returns {Promise<void>}
|
|
175
|
+
*/
|
|
176
|
+
async setTHigh(celsius) {
|
|
177
|
+
await this._setLimit(_REG_T_HIGH, celsius);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Read the T_HIGH limit.
|
|
182
|
+
* @returns {Promise<number>} Limit in °C.
|
|
183
|
+
*/
|
|
184
|
+
async getTHigh() {
|
|
185
|
+
return this._getLimit(_REG_T_HIGH);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Set the T_LOW limit (reset 75 °C).
|
|
190
|
+
* @param {number} celsius - Limit in °C, rounded to 0.0625 °C and clamped to -55…+150 °C (12-bit mode: up to +127.9375 °C).
|
|
191
|
+
* @returns {Promise<void>}
|
|
192
|
+
*/
|
|
193
|
+
async setTLow(celsius) {
|
|
194
|
+
await this._setLimit(_REG_T_LOW, celsius);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Read the T_LOW limit.
|
|
199
|
+
* @returns {Promise<number>} Limit in °C.
|
|
200
|
+
*/
|
|
201
|
+
async getTLow() {
|
|
202
|
+
return this._getLimit(_REG_T_LOW);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Set the number of consecutive faults that trigger ALERT.
|
|
207
|
+
* @param {number} count - 1, 2, 4 or 6.
|
|
208
|
+
* @returns {Promise<void>}
|
|
209
|
+
* @throws {RangeError} If count is not a supported value.
|
|
210
|
+
*/
|
|
211
|
+
async setFaultQueue(count) {
|
|
212
|
+
const code = _FAULT_QUEUES.indexOf(count);
|
|
213
|
+
if (code < 0) throw new RangeError('count must be 1, 2, 4 or 6');
|
|
214
|
+
await this._updateConfig(_CFG_F_MASK, code << _CFG_F_SHIFT);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Set the ALERT polarity (POL bit).
|
|
219
|
+
* @param {boolean} activeHigh - true for active-high, false for active-low (default).
|
|
220
|
+
* @returns {Promise<void>}
|
|
221
|
+
*/
|
|
222
|
+
async setPolarity(activeHigh) {
|
|
223
|
+
await this._updateConfig(_CFG_POL, activeHigh ? _CFG_POL : 0);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Select the thermostat mode (TM bit).
|
|
228
|
+
* @param {boolean} interrupt - true for interrupt mode, false for comparator mode (default).
|
|
229
|
+
* @returns {Promise<void>}
|
|
230
|
+
*/
|
|
231
|
+
async setThermostatMode(interrupt) {
|
|
232
|
+
await this._updateConfig(_CFG_TM, interrupt ? _CFG_TM : 0);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Shut the device down after the current conversion (SD=1).
|
|
237
|
+
* @returns {Promise<void>}
|
|
238
|
+
*/
|
|
239
|
+
async shutdown() {
|
|
240
|
+
await this._updateConfig(0, _CFG_SD);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Resume continuous conversion (SD=0).
|
|
245
|
+
* @returns {Promise<void>}
|
|
246
|
+
*/
|
|
247
|
+
async wake() {
|
|
248
|
+
await this._updateConfig(_CFG_SD, 0);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Take a single conversion and return the temperature. Puts the device in
|
|
253
|
+
* shutdown if needed, sets OS=1, polls OS until the conversion is done
|
|
254
|
+
* (30 ms timeout) and restores the previous mode.
|
|
255
|
+
* @returns {Promise<number>} Temperature in °C.
|
|
256
|
+
* @throws {Error} If the conversion does not finish within 30 ms.
|
|
257
|
+
*/
|
|
258
|
+
async oneShot() {
|
|
259
|
+
let config = await this.readConfig();
|
|
260
|
+
const wasRunning = (config & _CFG_SD) === 0;
|
|
261
|
+
if (wasRunning) {
|
|
262
|
+
config |= _CFG_SD;
|
|
263
|
+
await this.writeConfig(config);
|
|
264
|
+
}
|
|
265
|
+
await this.writeConfig(config | _CFG_OS);
|
|
266
|
+
let done = false;
|
|
267
|
+
for (let i = 0; i < 30; i++) {
|
|
268
|
+
await _sleep(1);
|
|
269
|
+
if ((await this.readConfig()) & _CFG_OS) { done = true; break; }
|
|
270
|
+
}
|
|
271
|
+
const temperature = await this.readTemperature();
|
|
272
|
+
if (wasRunning) await this.wake();
|
|
273
|
+
if (!done) throw new Error('TMP102: one-shot conversion timed out');
|
|
274
|
+
return temperature;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Report whether ALERT is currently active.
|
|
279
|
+
* @returns {Promise<boolean>} true while the comparator state is alerting (AL bit corrected for POL).
|
|
280
|
+
*/
|
|
281
|
+
async alertState() {
|
|
282
|
+
const config = await this.readConfig();
|
|
283
|
+
const al = (config & _CFG_AL) !== 0;
|
|
284
|
+
// AL reads 1 while inactive with POL=0 (inverted); POL=1 flips it.
|
|
285
|
+
return (config & _CFG_POL) ? al : !al;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Send the general-call reset (0x00 0x06). Resets every general-call
|
|
290
|
+
* device on the bus to power-up values. Needs a Connection that transmits
|
|
291
|
+
* the bytes to the general-call address.
|
|
292
|
+
* @returns {Promise<void>}
|
|
293
|
+
*/
|
|
294
|
+
async reset() {
|
|
295
|
+
await this._conn.write(Buffer.from([0x00, 0x06]));
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Read the alert state and clear an interrupt-mode alert (reads
|
|
300
|
+
* CONFIGURATION, then TEMPERATURE).
|
|
301
|
+
* @returns {Promise<boolean>} true while the comparator state is alerting.
|
|
302
|
+
*/
|
|
303
|
+
async pollInterrupt() {
|
|
304
|
+
const alerting = await this.alertState();
|
|
305
|
+
await this._readReg(_REG_TEMPERATURE);
|
|
306
|
+
return alerting;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Subscribe to ALERT events. With an interrupt pin on the connection the
|
|
311
|
+
* callback runs on every active ALERT edge (direction follows POL);
|
|
312
|
+
* otherwise a 1 s polling task calls it when pollInterrupt() reports an alert.
|
|
313
|
+
* @param {(temperatureC: number) => void} callback
|
|
314
|
+
* @returns {Promise<void>}
|
|
315
|
+
*/
|
|
316
|
+
async onInterrupt(callback) {
|
|
317
|
+
this._callback = callback;
|
|
318
|
+
if (this._conn.intPin) {
|
|
319
|
+
const activeHigh = ((await this.readConfig()) & _CFG_POL) !== 0;
|
|
320
|
+
this._edgeHandler = async () => {
|
|
321
|
+
const t = await this.readTemperature();
|
|
322
|
+
if (this._callback) this._callback(t);
|
|
323
|
+
};
|
|
324
|
+
await this._conn.intPin.onEdge(this._edgeHandler, activeHigh ? 'rising' : 'falling');
|
|
325
|
+
} else {
|
|
326
|
+
let busy = false;
|
|
327
|
+
this._pollTimer = setInterval(async () => {
|
|
328
|
+
if (busy) return;
|
|
329
|
+
busy = true;
|
|
330
|
+
try {
|
|
331
|
+
if (await this.pollInterrupt()) {
|
|
332
|
+
const t = await this.readTemperature();
|
|
333
|
+
if (this._callback) this._callback(t);
|
|
334
|
+
}
|
|
335
|
+
} finally {
|
|
336
|
+
busy = false;
|
|
337
|
+
}
|
|
338
|
+
}, 1000);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Unsubscribe and stop delivery.
|
|
344
|
+
* @returns {Promise<void>}
|
|
345
|
+
*/
|
|
346
|
+
async offInterrupt() {
|
|
347
|
+
if (this._conn.intPin && this._edgeHandler) {
|
|
348
|
+
await this._conn.intPin.offEdge(this._edgeHandler);
|
|
349
|
+
this._edgeHandler = null;
|
|
350
|
+
}
|
|
351
|
+
if (this._pollTimer) {
|
|
352
|
+
clearInterval(this._pollTimer);
|
|
353
|
+
this._pollTimer = null;
|
|
354
|
+
}
|
|
355
|
+
this._callback = null;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
TMP102Minimal.I2C_ADDRESS = 0x48;
|
|
360
|
+
|
|
361
|
+
module.exports = { TMP102Minimal, TMP102Full };
|