periph 1.5.0 → 1.5.2

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.
Files changed (79) hide show
  1. package/README.md +19 -0
  2. package/examples/color/as7262/complete.js +60 -0
  3. package/examples/color/as7262/demo.js +65 -0
  4. package/examples/color/as7262/minimal.js +23 -0
  5. package/examples/environmental/aht10/complete.js +37 -0
  6. package/examples/environmental/aht10/demo.js +43 -0
  7. package/examples/environmental/aht10/minimal.js +12 -0
  8. package/examples/environmental/aht20/complete.js +41 -0
  9. package/examples/environmental/aht20/demo.js +49 -0
  10. package/examples/environmental/aht20/minimal.js +12 -0
  11. package/examples/environmental/am2320/complete.js +34 -0
  12. package/examples/environmental/am2320/demo.js +46 -0
  13. package/examples/environmental/am2320/minimal.js +21 -0
  14. package/examples/humidity/hdc1008/complete.js +39 -0
  15. package/examples/humidity/hdc1008/demo.js +46 -0
  16. package/examples/humidity/hdc1008/minimal.js +22 -0
  17. package/examples/humidity/hdc1080/complete.js +34 -0
  18. package/examples/humidity/hdc1080/demo.js +46 -0
  19. package/examples/humidity/hdc1080/minimal.js +22 -0
  20. package/examples/humidity/hs30xx/complete.js +43 -0
  21. package/examples/humidity/hs30xx/demo.js +40 -0
  22. package/examples/humidity/hs30xx/minimal.js +21 -0
  23. package/examples/humidity/hs40xx/complete.js +54 -0
  24. package/examples/humidity/hs40xx/demo.js +38 -0
  25. package/examples/humidity/hs40xx/minimal.js +21 -0
  26. package/examples/io_expander/tca9554/complete.js +63 -0
  27. package/examples/io_expander/tca9554/demo.js +58 -0
  28. package/examples/io_expander/tca9554/minimal.js +20 -0
  29. package/examples/led/ht16k33/complete.js +45 -0
  30. package/examples/led/ht16k33/demo.js +54 -0
  31. package/examples/led/ht16k33/minimal.js +24 -0
  32. package/examples/led/is31fl3731/complete.js +60 -0
  33. package/examples/led/is31fl3731/demo.js +72 -0
  34. package/examples/led/is31fl3731/minimal.js +26 -0
  35. package/examples/led/pca9685/complete.js +43 -0
  36. package/examples/led/pca9685/demo.js +77 -0
  37. package/examples/led/pca9685/minimal.js +24 -0
  38. package/examples/power/ina260/complete.js +38 -0
  39. package/examples/power/ina260/demo.js +32 -0
  40. package/examples/power/ina260/minimal.js +11 -0
  41. package/examples/power/lc709203f/complete.js +57 -0
  42. package/examples/power/lc709203f/demo.js +34 -0
  43. package/examples/power/lc709203f/minimal.js +11 -0
  44. package/examples/power/lm25066/complete.js +54 -0
  45. package/examples/power/lm25066/demo.js +41 -0
  46. package/examples/power/lm25066/minimal.js +12 -0
  47. package/examples/power/ltc4151/complete.js +40 -0
  48. package/examples/power/ltc4151/demo.js +49 -0
  49. package/examples/power/ltc4151/minimal.js +11 -0
  50. package/examples/power/max17048/complete.js +68 -0
  51. package/examples/power/max17048/demo.js +56 -0
  52. package/examples/power/max17048/minimal.js +19 -0
  53. package/examples/pressure/lps22hb/complete.js +43 -0
  54. package/examples/pressure/lps22hb/demo.js +43 -0
  55. package/examples/pressure/lps22hb/minimal.js +19 -0
  56. package/examples/pressure/mpl115a2/complete.js +25 -0
  57. package/examples/pressure/mpl115a2/demo.js +30 -0
  58. package/examples/pressure/mpl115a2/minimal.js +20 -0
  59. package/package.json +1 -1
  60. package/src/chips/color/as7262.js +419 -0
  61. package/src/chips/environmental/aht10.js +186 -0
  62. package/src/chips/environmental/aht20.js +226 -0
  63. package/src/chips/environmental/am2320.js +237 -0
  64. package/src/chips/humidity/hdc1008.js +262 -0
  65. package/src/chips/humidity/hdc1080.js +208 -0
  66. package/src/chips/humidity/hs30xx.js +254 -0
  67. package/src/chips/humidity/hs40xx.js +382 -0
  68. package/src/chips/io_expander/tca9554.js +407 -0
  69. package/src/chips/led/ht16k33.js +273 -0
  70. package/src/chips/led/is31fl3731.js +501 -0
  71. package/src/chips/led/pca9685.js +350 -0
  72. package/src/chips/power/ina260.js +229 -0
  73. package/src/chips/power/lc709203f.js +376 -0
  74. package/src/chips/power/lm25066.js +370 -0
  75. package/src/chips/power/ltc4151.js +297 -0
  76. package/src/chips/power/max17048.js +302 -0
  77. package/src/chips/pressure/lps22hb.js +257 -0
  78. package/src/chips/pressure/mpl115a2.js +171 -0
  79. package/src/discovery/registry.js +56 -14
@@ -0,0 +1,376 @@
1
+ 'use strict';
2
+
3
+ const _REG_BEFORE_RSOC = 0x04;
4
+ const _REG_THERMISTOR_B = 0x06;
5
+ const _REG_INITIAL_RSOC = 0x07;
6
+ const _REG_TEMPERATURE = 0x08;
7
+ const _REG_VOLTAGE = 0x09;
8
+ const _REG_DIRECTION = 0x0A;
9
+ const _REG_APA = 0x0B;
10
+ const _REG_APT = 0x0C;
11
+ const _REG_RSOC = 0x0D;
12
+ const _REG_ITE = 0x0F;
13
+ const _REG_IC_VERSION = 0x11;
14
+ const _REG_PROFILE = 0x12;
15
+ const _REG_ALARM_RSOC = 0x13;
16
+ const _REG_ALARM_VOLTAGE = 0x14;
17
+ const _REG_POWER_MODE = 0x15;
18
+ const _REG_STATUS = 0x16;
19
+ const _REG_PROFILE_ID = 0x1A;
20
+
21
+ const _APA_TABLE = [[100, 0x08], [200, 0x0B], [500, 0x10], [1000, 0x19], [2000, 0x2D], [3000, 0x36]];
22
+
23
+ function _sleep(ms) {
24
+ return new Promise((resolve) => setTimeout(resolve, ms));
25
+ }
26
+
27
+ /**
28
+ * CRC-8 (ATM): polynomial 0x07, init 0x00, MSB first, no reflection, no final XOR.
29
+ * @param {Iterable<number>} data - Bytes to checksum.
30
+ * @returns {number} CRC byte.
31
+ */
32
+ function crc8Atm(data) {
33
+ let crc = 0;
34
+ for (const byte of data) {
35
+ crc ^= byte;
36
+ for (let i = 0; i < 8; i++) {
37
+ crc = (crc & 0x80) ? ((crc << 1) ^ 0x07) & 0xFF : (crc << 1) & 0xFF;
38
+ }
39
+ }
40
+ return crc;
41
+ }
42
+
43
+ function _apaFor(capacityMah) {
44
+ const t = _APA_TABLE;
45
+ if (capacityMah <= t[0][0]) return t[0][1];
46
+ if (capacityMah >= t[t.length - 1][0]) return t[t.length - 1][1];
47
+ for (let i = 1; i < t.length; i++) {
48
+ if (capacityMah <= t[i][0]) {
49
+ const [c0, a0] = t[i - 1];
50
+ const [c1, a1] = t[i];
51
+ return Math.round(a0 + (a1 - a0) * (capacityMah - c0) / (c1 - c0));
52
+ }
53
+ }
54
+ return t[t.length - 1][1];
55
+ }
56
+
57
+ /**
58
+ * LC709203F 1-cell Li-ion fuel gauge — minimal interface.
59
+ *
60
+ * Reports cell voltage, state of charge and temperature. The constructor starts the
61
+ * datasheet starting flow (operational mode, APA, battery profile, I²C temperature mode
62
+ * at 25 °C); JS constructors cannot be async, so it runs unawaited and every method awaits
63
+ * it first (errors surface from the first call, or from `ready`). Every transfer carries a
64
+ * CRC-8 that the driver appends on writes and verifies on reads (one retry).
65
+ */
66
+ class LC709203FMinimal {
67
+ /**
68
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (address 0x0B).
69
+ * @param {number} [capacityMah=1000] - Design capacity in mAh (clamped to 100-3000 for the APA interpolation).
70
+ * @param {number} [profile=1] - Battery profile for register 0x12 (0 or 1).
71
+ */
72
+ constructor(connection, capacityMah = 1000, profile = 1) {
73
+ this._conn = connection;
74
+ this._ready = this._init(capacityMah, profile);
75
+ this._ready.catch(() => {});
76
+ }
77
+
78
+ /** @returns {Promise<void>} Resolves when the starting flow is complete. */
79
+ get ready() { return this._ready; }
80
+
81
+ async _init(capacityMah, profile) {
82
+ await this._wakeAndOperational();
83
+ await this._writeWord(_REG_APA, _apaFor(capacityMah));
84
+ await this._writeWord(_REG_PROFILE, profile & 1);
85
+ await _sleep(100); // TINIT: re-initialization like a battery insertion
86
+ await this._wakeAndOperational();
87
+ await this._writeWord(_REG_STATUS, 0x0000);
88
+ await this._setTemperature(25.0);
89
+ }
90
+
91
+ async _wakeAndOperational() {
92
+ // Written twice: the first transfer may be lost while the chip wakes up.
93
+ for (let i = 0; i < 2; i++) {
94
+ try { await this._writeWord(_REG_POWER_MODE, 0x0001, false); } catch (e) { /* ignored */ }
95
+ }
96
+ await _sleep(1);
97
+ }
98
+
99
+ async _readWord(cmd) {
100
+ let last;
101
+ for (let attempt = 0; attempt < 2; attempt++) {
102
+ try {
103
+ const raw = await this._conn.readReg(cmd, 3);
104
+ if (crc8Atm([0x16, cmd, 0x17, raw[0], raw[1]]) === raw[2]) return raw[0] | (raw[1] << 8);
105
+ last = new Error('LC709203F CRC mismatch on command 0x' + cmd.toString(16));
106
+ } catch (e) {
107
+ last = e;
108
+ }
109
+ }
110
+ throw last;
111
+ }
112
+
113
+ async _writeWord(cmd, value, retry = true) {
114
+ const low = value & 0xFF;
115
+ const high = (value >> 8) & 0xFF;
116
+ const data = Buffer.from([low, high, crc8Atm([0x16, cmd, low, high])]);
117
+ try {
118
+ await this._conn.writeReg(cmd, data);
119
+ } catch (e) {
120
+ if (!retry) throw e;
121
+ await this._conn.writeReg(cmd, data);
122
+ }
123
+ }
124
+
125
+ async _setTemperature(celsius) {
126
+ const c = Math.max(-20, Math.min(60, celsius));
127
+ await this._writeWord(_REG_TEMPERATURE, Math.round((c + 273.15) * 10));
128
+ }
129
+
130
+ /**
131
+ * Read the cell voltage.
132
+ * @returns {Promise<number>} Cell voltage in volts (raw × 1 mV).
133
+ */
134
+ async voltage() { await this._ready; return (await this._readWord(_REG_VOLTAGE)) / 1000; }
135
+
136
+ /**
137
+ * Read the state of charge from the ITE register.
138
+ * @returns {Promise<number>} State of charge in % (0.0-100.0).
139
+ */
140
+ async percent() { await this._ready; return (await this._readWord(_REG_ITE)) / 10; }
141
+
142
+ /**
143
+ * Read the cell temperature register.
144
+ * @returns {Promise<number>} Cell temperature in °C.
145
+ */
146
+ async temperature() { await this._ready; return (await this._readWord(_REG_TEMPERATURE)) / 10 - 273.15; }
147
+
148
+ /**
149
+ * Supply the cell temperature (I²C temperature mode); clamped to -20...+60 °C.
150
+ * @param {number} celsius - Cell temperature in °C.
151
+ * @returns {Promise<void>}
152
+ */
153
+ async setTemperature(celsius) { await this._ready; await this._setTemperature(celsius); }
154
+ }
155
+
156
+ /**
157
+ * LC709203F full interface — RSOC control, thermistor mode, alarms, profiles, power mode.
158
+ *
159
+ * Alarm bitmask constants: `ALARM_RSOC` (1), `ALARM_VOLTAGE` (2). The chip has no status
160
+ * register; `pollInterrupt()` evaluates RSOC and voltage against the cached thresholds.
161
+ */
162
+ class LC709203FFull extends LC709203FMinimal {
163
+ /**
164
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (address 0x0B).
165
+ * @param {number} [capacityMah=1000] - Design capacity in mAh.
166
+ * @param {number} [profile=1] - Battery profile (0 or 1).
167
+ */
168
+ constructor(connection, capacityMah = 1000, profile = 1) {
169
+ super(connection, capacityMah, profile);
170
+ this._callback = null;
171
+ this._intPinUsed = null;
172
+ this._pollTimer = null;
173
+ this._polling = false;
174
+ this._alarmRsoc = 0;
175
+ this._alarmVoltage = 0;
176
+ this._edgeHandler = () => { this._service(); };
177
+ this._ready = this._ready.then(async () => {
178
+ this._alarmRsoc = await this._readWord(_REG_ALARM_RSOC);
179
+ this._alarmVoltage = await this._readWord(_REG_ALARM_VOLTAGE);
180
+ });
181
+ this._ready.catch(() => {});
182
+ }
183
+
184
+ /** @returns {Promise<number>} State of charge in % (register 0x0D, 0-100). */
185
+ async rsoc() { await this._ready; return this._readWord(_REG_RSOC); }
186
+
187
+ /**
188
+ * Force the RSOC register (use sleep mode to keep the value).
189
+ * @param {number} percent - 0-100.
190
+ * @returns {Promise<void>}
191
+ */
192
+ async setRsoc(percent) { await this._ready; await this._writeWord(_REG_RSOC, Math.max(0, Math.min(100, Math.trunc(percent)))); }
193
+
194
+ /** Initialize RSOC from the present voltage (0x07 ← 0xAA55); waits 2 ms. @returns {Promise<void>} */
195
+ async initRsoc() { await this._ready; await this._writeWord(_REG_INITIAL_RSOC, 0xAA55); await _sleep(2); }
196
+
197
+ /** Initialize RSOC from the maximum voltage since reset (0x04 ← 0xAA55); waits 2 ms. @returns {Promise<void>} */
198
+ async beforeRsoc() { await this._ready; await this._writeWord(_REG_BEFORE_RSOC, 0xAA55); await _sleep(2); }
199
+
200
+ /**
201
+ * Switch to thermistor temperature mode.
202
+ * @param {number} [bConstant=3380] - Thermistor B constant in K.
203
+ * @returns {Promise<void>}
204
+ */
205
+ async useThermistor(bConstant = 3380) {
206
+ await this._ready;
207
+ await this._writeWord(_REG_THERMISTOR_B, bConstant);
208
+ await this._writeWord(_REG_STATUS, 0x0001);
209
+ }
210
+
211
+ /**
212
+ * Switch to I²C temperature mode and write a temperature.
213
+ * @param {number} [celsius=25] - Cell temperature in °C.
214
+ * @returns {Promise<void>}
215
+ */
216
+ async useI2cTemperature(celsius = 25.0) {
217
+ await this._ready;
218
+ await this._writeWord(_REG_STATUS, 0x0000);
219
+ await this._setTemperature(celsius);
220
+ }
221
+
222
+ /** @returns {Promise<boolean>} True if the temperature source is the thermistor. */
223
+ async isThermistorMode() { await this._ready; return (await this._readWord(_REG_STATUS)) === 0x0001; }
224
+
225
+ /** @returns {Promise<number>} Thermistor B constant in K. */
226
+ async getBConstant() { await this._ready; return this._readWord(_REG_THERMISTOR_B); }
227
+
228
+ /** Write the Adjustment Pack Application register. @param {number} value - 0-255. @returns {Promise<void>} */
229
+ async setApa(value) { await this._ready; await this._writeWord(_REG_APA, value & 0xFF); }
230
+
231
+ /** @returns {Promise<number>} Adjustment Pack Application register. */
232
+ async getApa() { await this._ready; return this._readWord(_REG_APA); }
233
+
234
+ /** Write the Adjustment Pack Thermistor register. @param {number} value - 0-65535. @returns {Promise<void>} */
235
+ async setApt(value) { await this._ready; await this._writeWord(_REG_APT, value & 0xFFFF); }
236
+
237
+ /** @returns {Promise<number>} Adjustment Pack Thermistor register. */
238
+ async getApt() { await this._ready; return this._readWord(_REG_APT); }
239
+
240
+ /**
241
+ * Set the current direction.
242
+ * @param {number} mode - `LC709203FFull.DIRECTION_AUTO`, `DIRECTION_CHARGE` or `DIRECTION_DISCHARGE`.
243
+ * @returns {Promise<void>}
244
+ * @throws {Error} For any other value.
245
+ */
246
+ async setCurrentDirection(mode) {
247
+ if (mode !== 0x0000 && mode !== 0x0001 && mode !== 0xFFFF) throw new Error('invalid current direction');
248
+ await this._ready;
249
+ await this._writeWord(_REG_DIRECTION, mode);
250
+ }
251
+
252
+ /** @returns {Promise<number>} Current direction register. */
253
+ async getCurrentDirection() { await this._ready; return this._readWord(_REG_DIRECTION); }
254
+
255
+ /**
256
+ * Select battery profile 0 or 1 (re-initializes the chip; waits 90 ms).
257
+ * @param {number} profile - 0 or 1.
258
+ * @returns {Promise<void>}
259
+ */
260
+ async setProfile(profile) {
261
+ await this._ready;
262
+ await this._writeWord(_REG_PROFILE, profile & 1);
263
+ await _sleep(100);
264
+ await this._wakeAndOperational();
265
+ }
266
+
267
+ /** @returns {Promise<number>} Selected battery profile (0 or 1). */
268
+ async getProfile() { await this._ready; return this._readWord(_REG_PROFILE); }
269
+
270
+ /** @returns {Promise<number>} Number of the Parameter register (e.g. 0x0301). */
271
+ async profileId() { await this._ready; return this._readWord(_REG_PROFILE_ID); }
272
+
273
+ /** @returns {Promise<number>} IC Version register. */
274
+ async icVersion() { await this._ready; return this._readWord(_REG_IC_VERSION); }
275
+
276
+ /** Set the low-RSOC alarm threshold. @param {number} percent - 0 disables, 1-100. @returns {Promise<void>} */
277
+ async setAlarmRsoc(percent) {
278
+ await this._ready;
279
+ const p = Math.max(0, Math.min(100, Math.trunc(percent)));
280
+ await this._writeWord(_REG_ALARM_RSOC, p);
281
+ this._alarmRsoc = p;
282
+ }
283
+
284
+ /** @returns {Promise<number>} Low-RSOC alarm threshold in %. */
285
+ async getAlarmRsoc() { await this._ready; return this._readWord(_REG_ALARM_RSOC); }
286
+
287
+ /** Set the low-voltage alarm threshold. @param {number} volts - 0 disables; resolution 1 mV. @returns {Promise<void>} */
288
+ async setAlarmVoltage(volts) {
289
+ await this._ready;
290
+ const raw = Math.max(0, Math.min(0xFFFF, Math.round(volts * 1000)));
291
+ await this._writeWord(_REG_ALARM_VOLTAGE, raw);
292
+ this._alarmVoltage = raw;
293
+ }
294
+
295
+ /** @returns {Promise<number>} Low-voltage alarm threshold in volts. */
296
+ async getAlarmVoltage() { await this._ready; return (await this._readWord(_REG_ALARM_VOLTAGE)) / 1000; }
297
+
298
+ /** Enter sleep mode (only I²C stays alive; charge counting stops). @returns {Promise<void>} */
299
+ async sleep() { await this._ready; await this._writeWord(_REG_POWER_MODE, 0x0002); }
300
+
301
+ /** Wake from sleep: dummy transaction (SDA pulse), wait 400 µs, enter operational mode. @returns {Promise<void>} */
302
+ async wake() { await this._ready; await this._wakeAndOperational(); }
303
+
304
+ /** @returns {Promise<boolean>} True if the chip is in sleep mode (wakes the bus first). */
305
+ async isSleeping() {
306
+ await this._ready;
307
+ try { await this._conn.writeReg(_REG_POWER_MODE, Buffer.alloc(0)); } catch (e) { /* ignored */ }
308
+ await _sleep(1);
309
+ return (await this._readWord(_REG_POWER_MODE)) === 0x0002;
310
+ }
311
+
312
+ async _arm(intPin) {
313
+ this._intPinUsed = intPin;
314
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
315
+ if (intPin) {
316
+ await intPin.onEdge(this._edgeHandler, 'falling');
317
+ } else {
318
+ this._pollTimer = setInterval(() => { this._service(); }, 1000);
319
+ }
320
+ }
321
+
322
+ async _service() {
323
+ if (this._polling) return;
324
+ this._polling = true;
325
+ try {
326
+ const status = await this.pollInterrupt();
327
+ if (status && this._callback) this._callback(status);
328
+ } finally {
329
+ this._polling = false;
330
+ }
331
+ }
332
+
333
+ /**
334
+ * Subscribe to ALARMB (falling edge). `callback(status)` receives the ALARM_RSOC / ALARM_VOLTAGE
335
+ * bitmask. Delivery is via the INT line (options.intPin or connection.intPin) or a 1 s polling timer.
336
+ * @param {function} callback - Called with (status).
337
+ * @param {object} [options]
338
+ * @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
339
+ * @returns {Promise<void>}
340
+ */
341
+ async onInterrupt(callback, options = {}) {
342
+ await this._ready;
343
+ this._callback = callback;
344
+ await this._arm(options.intPin ?? this._conn.intPin ?? null);
345
+ }
346
+
347
+ /** Unsubscribe and stop delivery. @returns {Promise<void>} */
348
+ async offInterrupt() {
349
+ if (this._intPinUsed) {
350
+ await this._intPinUsed.offEdge(this._edgeHandler);
351
+ this._intPinUsed = null;
352
+ }
353
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
354
+ this._callback = null;
355
+ }
356
+
357
+ /**
358
+ * Evaluate RSOC and voltage against the cached alarm thresholds.
359
+ * @returns {Promise<number>} Bitmask of ALARM_RSOC / ALARM_VOLTAGE (0 = no alarm).
360
+ */
361
+ async pollInterrupt() {
362
+ await this._ready;
363
+ let status = 0;
364
+ if (this._alarmRsoc && (await this._readWord(_REG_RSOC)) < this._alarmRsoc) status |= LC709203FFull.ALARM_RSOC;
365
+ if (this._alarmVoltage && (await this._readWord(_REG_VOLTAGE)) < this._alarmVoltage) status |= LC709203FFull.ALARM_VOLTAGE;
366
+ return status;
367
+ }
368
+ }
369
+
370
+ LC709203FFull.DIRECTION_AUTO = 0x0000;
371
+ LC709203FFull.DIRECTION_CHARGE = 0x0001;
372
+ LC709203FFull.DIRECTION_DISCHARGE = 0xFFFF;
373
+ LC709203FFull.ALARM_RSOC = 0x01;
374
+ LC709203FFull.ALARM_VOLTAGE = 0x02;
375
+
376
+ module.exports = { LC709203FMinimal, LC709203FFull, crc8Atm };