periph 1.0.1 → 1.1.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.
Files changed (56) hide show
  1. package/examples/adc_dac/hx711/complete.js +42 -0
  2. package/examples/adc_dac/hx711/demo.js +35 -0
  3. package/examples/adc_dac/hx711/minimal.js +16 -0
  4. package/examples/adc_dac/mcp4728/complete.js +41 -0
  5. package/examples/adc_dac/mcp4728/demo.js +41 -0
  6. package/examples/adc_dac/mcp4728/minimal.js +11 -0
  7. package/examples/display/pcf8576/complete.js +32 -0
  8. package/examples/display/pcf8576/demo.js +48 -0
  9. package/examples/display/pcf8576/minimal.js +18 -0
  10. package/examples/environmental/aht21/complete.js +30 -0
  11. package/examples/environmental/aht21/demo.js +44 -0
  12. package/examples/environmental/aht21/minimal.js +12 -0
  13. package/examples/environmental/bme280/complete.js +34 -0
  14. package/examples/environmental/bme280/demo.js +67 -0
  15. package/examples/environmental/bme280/minimal.js +18 -0
  16. package/examples/environmental/bme680/complete.js +37 -0
  17. package/examples/environmental/bme680/demo.js +52 -0
  18. package/examples/environmental/bme680/minimal.js +19 -0
  19. package/examples/gas/ens160/complete.js +52 -0
  20. package/examples/gas/ens160/demo.js +52 -0
  21. package/examples/gas/ens160/minimal.js +27 -0
  22. package/examples/imu/mpu6050/complete.js +56 -0
  23. package/examples/imu/mpu6050/demo.js +34 -0
  24. package/examples/imu/mpu6050/minimal.js +14 -0
  25. package/examples/light/apds9960/complete.js +62 -0
  26. package/examples/light/apds9960/demo.js +32 -0
  27. package/examples/light/apds9960/minimal.js +12 -0
  28. package/examples/memory/24aa02uid/complete.js +40 -0
  29. package/examples/memory/24aa02uid/demo.js +40 -0
  30. package/examples/memory/24aa02uid/minimal.js +12 -0
  31. package/examples/pressure/bmp280/complete.js +22 -18
  32. package/examples/pressure/bmp280/demo.js +34 -18
  33. package/examples/pressure/bmp280/minimal.js +4 -4
  34. package/package.json +1 -1
  35. package/src/chips/adc_dac/hx711.js +165 -0
  36. package/src/chips/adc_dac/mcp4728.js +284 -0
  37. package/src/chips/display/pcf8576.js +223 -0
  38. package/src/chips/environmental/aht21.js +170 -0
  39. package/src/chips/environmental/bme280.js +366 -0
  40. package/src/chips/environmental/bme680.js +489 -0
  41. package/src/chips/gas/ens160.js +280 -0
  42. package/src/chips/imu/mpu6050.js +266 -0
  43. package/src/chips/light/apds9960.js +464 -0
  44. package/src/chips/memory/_24aa02uid.js +179 -0
  45. package/src/chips/pressure/bmp280.js +154 -191
  46. package/src/transport/hx711.js +105 -0
  47. package/examples/display/.gitkeep +0 -0
  48. package/examples/environmental/.gitkeep +0 -0
  49. package/examples/imu/.gitkeep +0 -0
  50. package/examples/light/.gitkeep +0 -0
  51. package/examples/pressure/.gitkeep +0 -0
  52. package/src/chips/display/.gitkeep +0 -0
  53. package/src/chips/environmental/.gitkeep +0 -0
  54. package/src/chips/imu/.gitkeep +0 -0
  55. package/src/chips/light/.gitkeep +0 -0
  56. package/src/chips/pressure/.gitkeep +0 -0
@@ -0,0 +1,464 @@
1
+ 'use strict';
2
+
3
+ const _REG_ENABLE = 0x80;
4
+ const _REG_ATIME = 0x81;
5
+ const _REG_WTIME = 0x83;
6
+ const _REG_AILTL = 0x84;
7
+ const _REG_AILTH = 0x85;
8
+ const _REG_AIHTL = 0x86;
9
+ const _REG_AIHTH = 0x87;
10
+ const _REG_PILT = 0x89;
11
+ const _REG_PIHT = 0x8B;
12
+ const _REG_PERS = 0x8C;
13
+ const _REG_CONFIG1 = 0x8D;
14
+ const _REG_PPULSE = 0x8E;
15
+ const _REG_CONTROL = 0x8F;
16
+ const _REG_CONFIG2 = 0x90;
17
+ const _REG_ID = 0x92;
18
+ const _REG_STATUS = 0x93;
19
+ const _REG_CDATAL = 0x94;
20
+ const _REG_RDATAL = 0x96;
21
+ const _REG_GDATAL = 0x98;
22
+ const _REG_BDATAL = 0x9A;
23
+ const _REG_PDATA = 0x9C;
24
+ const _REG_POFFSET_UR = 0x9D;
25
+ const _REG_POFFSET_DL = 0x9E;
26
+ const _REG_CONFIG3 = 0x9F;
27
+ const _REG_GPENTH = 0xA0;
28
+ const _REG_GEXTH = 0xA1;
29
+ const _REG_GCONF1 = 0xA2;
30
+ const _REG_GCONF2 = 0xA3;
31
+ const _REG_GOFFSET_U = 0xA4;
32
+ const _REG_GOFFSET_D = 0xA5;
33
+ const _REG_GPULSE = 0xA6;
34
+ const _REG_GOFFSET_L = 0xA7;
35
+ const _REG_GOFFSET_R = 0xA9;
36
+ const _REG_GCONF3 = 0xAA;
37
+ const _REG_GCONF4 = 0xAB;
38
+ const _REG_GFLVL = 0xAE;
39
+ const _REG_GSTATUS = 0xAF;
40
+ const _REG_PICLEAR = 0xE5;
41
+ const _REG_CICLEAR = 0xE6;
42
+ const _REG_AICLEAR = 0xE7;
43
+ const _REG_GFIFO_U = 0xFC;
44
+
45
+ const _ATIME_DEFAULT = 0xB6;
46
+ const _CONTROL_DEFAULT = 0x01;
47
+ const _CONFIG2_DEFAULT = 0x01;
48
+
49
+ function sleep(ms) {
50
+ const end = Date.now() + ms;
51
+ while (Date.now() < end) {}
52
+ }
53
+
54
+ /**
55
+ * APDS-9960 digital proximity, ambient light, RGB and gesture sensor — minimal interface.
56
+ *
57
+ * Provides ambient light and color (RGBC) readings with no configuration
58
+ * beyond the transport. The ALS/Color engine is enabled at construction
59
+ * with sensible defaults.
60
+ *
61
+ * Default configuration (written at construction):
62
+ * - ATIME = 0xB6 (72 cycles, ~200 ms integration, max count 65535)
63
+ * - AGAIN = 1 (4x ALS gain)
64
+ * - CONFIG2 = 0x01 (LED_BOOST=100%, reserved bit 0 set)
65
+ * - PON + AEN enabled; no wait, proximity, gesture, or interrupts
66
+ */
67
+ class APDS9960Minimal {
68
+ /**
69
+ * @param {object} transport - Configured I2C transport pointing at the device (address 0x39).
70
+ */
71
+ constructor(transport) {
72
+ this._transport = transport;
73
+ sleep(6);
74
+ const id = this._readReg(_REG_ID);
75
+ if (id !== 0xAB) throw new Error('APDS-9960 not found (ID=0x' + id.toString(16) + ', expected 0xAB)');
76
+ this._writeReg(_REG_ENABLE, 0x00);
77
+ this._writeReg(_REG_ATIME, _ATIME_DEFAULT);
78
+ this._writeReg(_REG_CONTROL, _CONTROL_DEFAULT);
79
+ this._writeReg(_REG_CONFIG2, _CONFIG2_DEFAULT);
80
+ this._writeReg(_REG_ENABLE, 0x03);
81
+ sleep(210);
82
+ }
83
+
84
+ _writeReg(reg, value) {
85
+ this._transport.write(Buffer.from([reg, value]));
86
+ }
87
+
88
+ _readReg(reg) {
89
+ return this._transport.writeRead(Buffer.from([reg]), 1)[0];
90
+ }
91
+
92
+ _readReg16LE(reg) {
93
+ const buf = this._transport.writeRead(Buffer.from([reg]), 2);
94
+ return buf[0] | (buf[1] << 8);
95
+ }
96
+
97
+ /**
98
+ * Read the clear (unfiltered) channel.
99
+ * @returns {number} Raw clear channel count, 0-65535.
100
+ */
101
+ colorClear() { return this._readReg16LE(_REG_CDATAL); }
102
+
103
+ /**
104
+ * Read the red channel.
105
+ *
106
+ * Burst-reads all 8 bytes from CDATAL to trigger the atomic latch.
107
+ * @returns {number} Raw red channel count, 0-65535.
108
+ */
109
+ colorRed() {
110
+ const raw = this._transport.writeRead(Buffer.from([_REG_CDATAL]), 8);
111
+ return raw[2] | (raw[3] << 8);
112
+ }
113
+
114
+ /**
115
+ * Read the green channel.
116
+ *
117
+ * Burst-reads all 8 bytes from CDATAL to trigger the atomic latch.
118
+ * @returns {number} Raw green channel count, 0-65535.
119
+ */
120
+ colorGreen() {
121
+ const raw = this._transport.writeRead(Buffer.from([_REG_CDATAL]), 8);
122
+ return raw[4] | (raw[5] << 8);
123
+ }
124
+
125
+ /**
126
+ * Read the blue channel.
127
+ *
128
+ * Burst-reads all 8 bytes from CDATAL to trigger the atomic latch.
129
+ * @returns {number} Raw blue channel count, 0-65535.
130
+ */
131
+ colorBlue() {
132
+ const raw = this._transport.writeRead(Buffer.from([_REG_CDATAL]), 8);
133
+ return raw[6] | (raw[7] << 8);
134
+ }
135
+
136
+ /**
137
+ * Read all four RGBC channels in one burst.
138
+ *
139
+ * Reading CDATAL at 0x94 atomically latches all eight bytes 0x94-0x9B.
140
+ *
141
+ * @returns {{ clear: number, red: number, green: number, blue: number }}
142
+ */
143
+ color() {
144
+ const raw = this._transport.writeRead(Buffer.from([_REG_CDATAL]), 8);
145
+ return {
146
+ clear: raw[0] | (raw[1] << 8),
147
+ red: raw[2] | (raw[3] << 8),
148
+ green: raw[4] | (raw[5] << 8),
149
+ blue: raw[6] | (raw[7] << 8)
150
+ };
151
+ }
152
+ }
153
+
154
+ /**
155
+ * APDS-9960 full interface — extends APDS9960Minimal with proximity, gesture, and configuration.
156
+ *
157
+ * Adds proximity detection, gesture engine, wait engine, threshold and
158
+ * interrupt configuration, status queries, and device identification.
159
+ */
160
+ class APDS9960Full extends APDS9960Minimal {
161
+ /**
162
+ * @param {object} transport - Configured I2C transport pointing at the device (address 0x39).
163
+ */
164
+ constructor(transport) {
165
+ super(transport);
166
+ }
167
+
168
+ /**
169
+ * Enable or disable the proximity engine.
170
+ * @param {boolean} enabled - true to enable PEN, false to disable.
171
+ */
172
+ enableProximity(enabled) {
173
+ let val = this._readReg(_REG_ENABLE);
174
+ if (enabled) val |= 0x04; else val &= ~0x04;
175
+ this._writeReg(_REG_ENABLE, val);
176
+ }
177
+
178
+ /**
179
+ * Read the proximity count.
180
+ * @returns {number} Proximity count 0-255; higher means closer.
181
+ */
182
+ proximity() { return this._readReg(_REG_PDATA); }
183
+
184
+ /**
185
+ * Enable or disable the wait engine.
186
+ * @param {boolean} enabled - true to enable WEN, false to disable.
187
+ */
188
+ enableWait(enabled) {
189
+ let val = this._readReg(_REG_ENABLE);
190
+ if (enabled) val |= 0x08; else val &= ~0x08;
191
+ this._writeReg(_REG_ENABLE, val);
192
+ }
193
+
194
+ /**
195
+ * Configure the wait time between ALS/proximity cycles.
196
+ * @param {number} wtime - WTIME register value 0-255.
197
+ * @param {boolean} [wlong=false] - true to enable WLONG 12x multiplier.
198
+ */
199
+ configureWait(wtime, wlong = false) {
200
+ this._writeReg(_REG_WTIME, wtime & 0xFF);
201
+ let c1 = this._readReg(_REG_CONFIG1);
202
+ if (wlong) c1 |= 0x02; else c1 &= ~0x02;
203
+ c1 = (c1 & 0x03) | 0x60;
204
+ this._writeReg(_REG_CONFIG1, c1);
205
+ }
206
+
207
+ /**
208
+ * Configure ALS integration time and gain.
209
+ * @param {number} atime - ATIME register value 0-255.
210
+ * @param {number} again - ALS gain 0-3 (0=1x, 1=4x, 2=16x, 3=64x).
211
+ */
212
+ configureAls(atime, again) {
213
+ this._writeReg(_REG_ATIME, atime & 0xFF);
214
+ let ctrl = this._readReg(_REG_CONTROL);
215
+ ctrl = (ctrl & 0xFC) | (again & 0x03);
216
+ this._writeReg(_REG_CONTROL, ctrl);
217
+ }
218
+
219
+ /**
220
+ * Configure proximity LED drive, gain, pulse count and length.
221
+ * @param {number} ldrive - LED drive strength 0-3.
222
+ * @param {number} pgain - Proximity gain 0-3.
223
+ * @param {number} ppulse - Pulse count minus 1, 0-63.
224
+ * @param {number} pplen - Pulse length 0-3.
225
+ */
226
+ configureProximityLed(ldrive, pgain, ppulse, pplen) {
227
+ let ctrl = this._readReg(_REG_CONTROL);
228
+ ctrl = ((ldrive & 0x03) << 6) | ((pgain & 0x03) << 2) | (ctrl & 0x03);
229
+ this._writeReg(_REG_CONTROL, ctrl);
230
+ this._writeReg(_REG_PPULSE, ((pplen & 0x03) << 6) | (ppulse & 0x3F));
231
+ }
232
+
233
+ /**
234
+ * Set additional LED current boost.
235
+ * @param {number} boost - LED_BOOST 0-3 (0=100%, 1=150%, 2=200%, 3=300%).
236
+ */
237
+ setLedBoost(boost) {
238
+ let c2 = this._readReg(_REG_CONFIG2);
239
+ c2 = (c2 & 0xCF) | ((boost & 0x03) << 4) | 0x01;
240
+ this._writeReg(_REG_CONFIG2, c2);
241
+ }
242
+
243
+ /**
244
+ * Set ALS interrupt thresholds.
245
+ * @param {number} low - Low threshold 0-65535.
246
+ * @param {number} high - High threshold 0-65535.
247
+ */
248
+ alsThreshold(low, high) {
249
+ this._writeReg(_REG_AILTL, low & 0xFF);
250
+ this._writeReg(_REG_AILTH, (low >> 8) & 0xFF);
251
+ this._writeReg(_REG_AIHTL, high & 0xFF);
252
+ this._writeReg(_REG_AIHTH, (high >> 8) & 0xFF);
253
+ }
254
+
255
+ /**
256
+ * Set proximity interrupt thresholds.
257
+ * @param {number} low - Low threshold 0-255.
258
+ * @param {number} high - High threshold 0-255.
259
+ */
260
+ proximityThreshold(low, high) {
261
+ this._writeReg(_REG_PILT, low & 0xFF);
262
+ this._writeReg(_REG_PIHT, high & 0xFF);
263
+ }
264
+
265
+ /**
266
+ * Set interrupt persistence filters.
267
+ * @param {number} ppers - Proximity persistence 0-15.
268
+ * @param {number} apers - ALS persistence 0-15.
269
+ */
270
+ setPersistence(ppers, apers) {
271
+ this._writeReg(_REG_PERS, ((ppers & 0x0F) << 4) | (apers & 0x0F));
272
+ }
273
+
274
+ /**
275
+ * Enable or disable ALS interrupt.
276
+ * @param {boolean} enabled - true to enable AIEN, false to disable.
277
+ */
278
+ enableAlsInterrupt(enabled) {
279
+ let val = this._readReg(_REG_ENABLE);
280
+ if (enabled) val |= 0x10; else val &= ~0x10;
281
+ this._writeReg(_REG_ENABLE, val);
282
+ }
283
+
284
+ /**
285
+ * Enable or disable proximity interrupt.
286
+ * @param {boolean} enabled - true to enable PIEN, false to disable.
287
+ */
288
+ enableProximityInterrupt(enabled) {
289
+ let val = this._readReg(_REG_ENABLE);
290
+ if (enabled) val |= 0x20; else val &= ~0x20;
291
+ this._writeReg(_REG_ENABLE, val);
292
+ }
293
+
294
+ /**
295
+ * Clear the proximity interrupt via address-only write to PICLEAR.
296
+ */
297
+ clearProximityInterrupt() {
298
+ this._transport.write(Buffer.from([_REG_PICLEAR]));
299
+ }
300
+
301
+ /**
302
+ * Clear the ALS/color interrupt via address-only write to CICLEAR.
303
+ */
304
+ clearAlsInterrupt() {
305
+ this._transport.write(Buffer.from([_REG_CICLEAR]));
306
+ }
307
+
308
+ /**
309
+ * Clear all non-gesture interrupts via address-only write to AICLEAR.
310
+ */
311
+ clearAllInterrupts() {
312
+ this._transport.write(Buffer.from([_REG_AICLEAR]));
313
+ }
314
+
315
+ /**
316
+ * Set proximity offset for UP/RIGHT and DOWN/LEFT photodiodes.
317
+ * @param {number} ur - UP/RIGHT offset -127 to +127 (sign-magnitude).
318
+ * @param {number} dl - DOWN/LEFT offset -127 to +127 (sign-magnitude).
319
+ */
320
+ setProximityOffset(ur, dl) {
321
+ const encode = (v) => v < 0 ? (0x80 | ((-v) & 0x7F)) : (v & 0x7F);
322
+ this._writeReg(_REG_POFFSET_UR, encode(ur));
323
+ this._writeReg(_REG_POFFSET_DL, encode(dl));
324
+ }
325
+
326
+ /**
327
+ * Mask individual photodiodes in proximity detection.
328
+ * @param {boolean} u - true to mask UP.
329
+ * @param {boolean} d - true to mask DOWN.
330
+ * @param {boolean} l - true to mask LEFT.
331
+ * @param {boolean} r - true to mask RIGHT.
332
+ */
333
+ setProximityMask(u, d, l, r) {
334
+ let c3 = this._readReg(_REG_CONFIG3) & 0xF0;
335
+ if (u) c3 |= 0x08;
336
+ if (d) c3 |= 0x04;
337
+ if (l) c3 |= 0x02;
338
+ if (r) c3 |= 0x01;
339
+ this._writeReg(_REG_CONFIG3, c3);
340
+ }
341
+
342
+ /**
343
+ * Enable or disable the gesture engine.
344
+ * @param {boolean} enabled - true to enable GEN and set GMODE, false to disable.
345
+ */
346
+ enableGesture(enabled) {
347
+ let val = this._readReg(_REG_ENABLE);
348
+ if (enabled) {
349
+ val |= 0x40;
350
+ this._writeReg(_REG_ENABLE, val);
351
+ let g4 = this._readReg(_REG_GCONF4);
352
+ g4 |= 0x01;
353
+ this._writeReg(_REG_GCONF4, g4);
354
+ } else {
355
+ val &= ~0x40;
356
+ this._writeReg(_REG_ENABLE, val);
357
+ let g4 = this._readReg(_REG_GCONF4);
358
+ g4 &= ~0x01;
359
+ this._writeReg(_REG_GCONF4, g4);
360
+ }
361
+ }
362
+
363
+ /**
364
+ * Configure gesture engine parameters.
365
+ * @param {number} ggain - Gesture gain 0-3.
366
+ * @param {number} gldrive - Gesture LED drive 0-3.
367
+ * @param {number} gpulse - Gesture pulse count minus 1, 0-63.
368
+ * @param {number} gplen - Gesture pulse length 0-3.
369
+ * @param {number} gwtime - Gesture wait time 0-7.
370
+ * @param {number} gpenth - Gesture proximity entry threshold 0-255.
371
+ * @param {number} gexth - Gesture exit threshold 0-255.
372
+ */
373
+ configureGesture(ggain, gldrive, gpulse, gplen, gwtime, gpenth, gexth) {
374
+ this._writeReg(_REG_GPENTH, gpenth & 0xFF);
375
+ this._writeReg(_REG_GEXTH, gexth & 0xFF);
376
+ const g2 = ((ggain & 0x03) << 5) | ((gldrive & 0x03) << 3) | (gwtime & 0x07);
377
+ this._writeReg(_REG_GCONF2, g2);
378
+ this._writeReg(_REG_GPULSE, ((gplen & 0x03) << 6) | (gpulse & 0x3F));
379
+ }
380
+
381
+ /**
382
+ * Check if gesture data is available in the FIFO.
383
+ * @returns {boolean} true if GSTATUS.GVALID is set.
384
+ */
385
+ gestureAvailable() { return !!(this._readReg(_REG_GSTATUS) & 0x01); }
386
+
387
+ /**
388
+ * Read all gesture datasets from the FIFO.
389
+ * @returns {Array<{ u: number, d: number, l: number, r: number }>}
390
+ */
391
+ readGestureFifo() {
392
+ const level = this._readReg(_REG_GFLVL);
393
+ if (level === 0) return [];
394
+ const result = [];
395
+ for (let i = 0; i < level; i++) {
396
+ const raw = this._transport.writeRead(Buffer.from([_REG_GFIFO_U]), 4);
397
+ result.push({ u: raw[0], d: raw[1], l: raw[2], r: raw[3] });
398
+ }
399
+ return result;
400
+ }
401
+
402
+ /**
403
+ * Read the number of datasets in the gesture FIFO.
404
+ * @returns {number} Number of 4-byte datasets currently in FIFO.
405
+ */
406
+ gestureFifoLevel() { return this._readReg(_REG_GFLVL); }
407
+
408
+ /**
409
+ * Clear the gesture FIFO by setting GFIFO_CLR in GCONF4.
410
+ */
411
+ clearGestureFifo() {
412
+ let g4 = this._readReg(_REG_GCONF4);
413
+ g4 |= 0x04;
414
+ this._writeReg(_REG_GCONF4, g4);
415
+ }
416
+
417
+ /**
418
+ * Enable or disable gesture interrupt.
419
+ * @param {boolean} enabled - true to enable GIEN, false to disable.
420
+ */
421
+ enableGestureInterrupt(enabled) {
422
+ let g4 = this._readReg(_REG_GCONF4);
423
+ if (enabled) g4 |= 0x02; else g4 &= ~0x02;
424
+ this._writeReg(_REG_GCONF4, g4);
425
+ }
426
+
427
+ /**
428
+ * Read the raw STATUS register.
429
+ * @returns {number} Raw STATUS byte.
430
+ */
431
+ status() { return this._readReg(_REG_STATUS); }
432
+
433
+ /**
434
+ * Check if ALS/color data is valid.
435
+ * @returns {boolean} true if STATUS.AVALID is set.
436
+ */
437
+ isAlsValid() { return !!(this._readReg(_REG_STATUS) & 0x01); }
438
+
439
+ /**
440
+ * Check if proximity data is valid.
441
+ * @returns {boolean} true if STATUS.PVALID is set.
442
+ */
443
+ isProximityValid() { return !!(this._readReg(_REG_STATUS) & 0x02); }
444
+
445
+ /**
446
+ * Check if the clear photodiode is saturated.
447
+ * @returns {boolean} true if STATUS.CPSAT is set.
448
+ */
449
+ isAlsSaturated() { return !!(this._readReg(_REG_STATUS) & 0x80); }
450
+
451
+ /**
452
+ * Check if analog saturation occurred during proximity.
453
+ * @returns {boolean} true if STATUS.PGSAT is set.
454
+ */
455
+ isProximitySaturated() { return !!(this._readReg(_REG_STATUS) & 0x40); }
456
+
457
+ /**
458
+ * Read the device ID register.
459
+ * @returns {number} ID register value (expect 0xAB).
460
+ */
461
+ chipId() { return this._readReg(_REG_ID); }
462
+ }
463
+
464
+ module.exports = { APDS9960Minimal, APDS9960Full };
@@ -0,0 +1,179 @@
1
+ 'use strict';
2
+
3
+ const ADDR_UID_BASE = 0xFC;
4
+ const ADDR_MFR_CODE = 0xFA;
5
+ const ADDR_DEV_CODE = 0xFB;
6
+ const WRITE_CYCLE_MS = 5;
7
+ const ACK_POLL_MAX = 20;
8
+ const PAGE_SIZE = 8;
9
+
10
+ function _sleep(ms) {
11
+ const end = Date.now() + ms;
12
+ while (Date.now() < end) {}
13
+ }
14
+
15
+ /**
16
+ * 24AA02UID 2K I2C EEPROM with 32-bit unique serial number — minimal interface.
17
+ *
18
+ * Provides read access to the chip's unique 32-bit serial number and basic
19
+ * single-byte read/write access to the user EEPROM. The chip has no
20
+ * configuration registers; it is ready for use immediately after power-on.
21
+ *
22
+ * Memory layout:
23
+ * - 0x00-0x7F — 128 bytes general-purpose user EEPROM (R/W)
24
+ * - 0x80-0xF9 — reserved, read-only
25
+ * - 0xFA — manufacturer code (always 0x29, Microchip)
26
+ * - 0xFB — device code (always 0x41)
27
+ * - 0xFC-0xFF — 32-bit unique serial number, MSB first
28
+ *
29
+ * Default configuration (no registers to configure):
30
+ * - User EEPROM is read/written as raw bytes (no interpretation)
31
+ * - writeByte() waits for the internal write cycle to finish via
32
+ * ACK polling before returning (max 5 ms)
33
+ * - All addresses 0x80-0xFF are write-protected; writes are silently
34
+ * ignored by the chip. Drivers accept the call but the data is not
35
+ * retained.
36
+ */
37
+ class EEPROM24AA02UIDMinimal {
38
+ /**
39
+ * @param {object} transport - Configured I2C transport pointing at the device (address 0x50).
40
+ */
41
+ constructor(transport) {
42
+ this._transport = transport;
43
+ }
44
+
45
+ /**
46
+ * Read the chip's factory-programmed 32-bit unique serial number.
47
+ * @returns {Buffer} 4-byte UID, MSB first (0xFC, 0xFD, 0xFE, 0xFF).
48
+ */
49
+ readUid() {
50
+ return this._transport.writeRead(Buffer.from([ADDR_UID_BASE]), 4);
51
+ }
52
+
53
+ /**
54
+ * Read a single byte from user EEPROM at 0x00-0x7F.
55
+ * @param {number} address - Memory address 0-127.
56
+ * @returns {number} Byte value 0-255.
57
+ */
58
+ readByte(address) {
59
+ return this._transport.writeRead(Buffer.from([address & 0xFF]), 1)[0];
60
+ }
61
+
62
+ /**
63
+ * Write a single byte to user EEPROM at 0x00-0x7F and wait for completion.
64
+ *
65
+ * Sends the byte, then ACK-polls the chip until the internal write
66
+ * cycle completes (max 5 ms). Writes to 0x80-0xFF are accepted by the
67
+ * device but silently ignored (write-protected region).
68
+ *
69
+ * @param {number} address - Memory address 0-255.
70
+ * @param {number} value - Byte value 0-255.
71
+ */
72
+ writeByte(address, value) {
73
+ this._transport.write(Buffer.from([address & 0xFF, value & 0xFF]));
74
+ this._ackPoll();
75
+ }
76
+
77
+ _ackPoll() {
78
+ for (let i = 0; i < ACK_POLL_MAX; i++) {
79
+ try {
80
+ this._transport.writeRead(Buffer.from([0x00]), 1);
81
+ return;
82
+ } catch (e) {
83
+ _sleep(1);
84
+ }
85
+ }
86
+ }
87
+ }
88
+
89
+ /**
90
+ * 24AA02UID full interface — extends minimal with multi-byte read/write.
91
+ *
92
+ * Adds sequential read, raw page write (8-byte page), arbitrary-length
93
+ * write that automatically crosses page boundaries, and accessors for
94
+ * the manufacturer and device codes in the upper (read-only) block.
95
+ */
96
+ class EEPROM24AA02UIDFull extends EEPROM24AA02UIDMinimal {
97
+ /**
98
+ * @param {object} transport - Configured I2C transport pointing at the device (address 0x50).
99
+ */
100
+ constructor(transport) {
101
+ super(transport);
102
+ }
103
+
104
+ /**
105
+ * Sequential read of `length` bytes starting at `address`.
106
+ *
107
+ * The internal address pointer auto-increments; reads may cross any
108
+ * boundary and wrap at the end of the 256-byte address space.
109
+ *
110
+ * @param {number} address - Starting address 0-255.
111
+ * @param {number} length - Number of bytes to read.
112
+ * @returns {Buffer} `length` bytes from the device.
113
+ */
114
+ read(address, length) {
115
+ return this._transport.writeRead(Buffer.from([address & 0xFF]), length);
116
+ }
117
+
118
+ /**
119
+ * Write up to 8 bytes within a single 8-byte page.
120
+ *
121
+ * The caller is responsible for ensuring all bytes lie within the
122
+ * same page. Bytes that would overflow the page boundary wrap to the
123
+ * start of the same page (FIFO overwrite) — use write() to handle
124
+ * boundaries automatically.
125
+ *
126
+ * @param {number} address - Start address within an 8-byte page (0, 8, 16, …).
127
+ * @param {Buffer|Uint8Array} data - Bytes to write (1 to 8 bytes).
128
+ */
129
+ writePage(address, data) {
130
+ if (data.length === 0) return;
131
+ const buf = Buffer.alloc(1 + data.length);
132
+ buf[0] = address & 0xFF;
133
+ for (let i = 0; i < data.length; i++) buf[1 + i] = data[i];
134
+ this._transport.write(buf);
135
+ this._ackPoll();
136
+ }
137
+
138
+ /**
139
+ * Write an arbitrary-length buffer, splitting at 8-byte page boundaries.
140
+ *
141
+ * Automatically splits the write into page-aligned chunks and waits
142
+ * for the write cycle of each chunk before continuing.
143
+ *
144
+ * @param {number} address - Starting address 0-255.
145
+ * @param {Buffer|Uint8Array} data - Bytes to write.
146
+ */
147
+ write(address, data) {
148
+ let offset = 0;
149
+ let remaining = data.length;
150
+ let current = address;
151
+ while (remaining > 0) {
152
+ const pageOffset = current & (PAGE_SIZE - 1);
153
+ let chunk = PAGE_SIZE - pageOffset;
154
+ if (chunk > remaining) chunk = remaining;
155
+ this.writePage(current, data.slice(offset, offset + chunk));
156
+ offset += chunk;
157
+ current += chunk;
158
+ remaining -= chunk;
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Read the manufacturer code at 0xFA.
164
+ * @returns {number} Manufacturer code; expect 0x29 (Microchip).
165
+ */
166
+ readManufacturerCode() {
167
+ return this.readByte(ADDR_MFR_CODE);
168
+ }
169
+
170
+ /**
171
+ * Read the device code at 0xFB.
172
+ * @returns {number} Device code; expect 0x41 (I2C 2-Kbit EEPROM).
173
+ */
174
+ readDeviceCode() {
175
+ return this.readByte(ADDR_DEV_CODE);
176
+ }
177
+ }
178
+
179
+ module.exports = { EEPROM24AA02UIDMinimal, EEPROM24AA02UIDFull };