periph 1.2.1 → 1.3.1

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 (123) hide show
  1. package/README.md +11 -0
  2. package/examples/accelerometer/bma150/complete.js +62 -0
  3. package/examples/accelerometer/bma150/demo.js +65 -0
  4. package/examples/accelerometer/bma150/minimal.js +18 -0
  5. package/examples/adc_dac/hy3131/complete.js +66 -0
  6. package/examples/adc_dac/hy3131/demo.js +37 -0
  7. package/examples/adc_dac/hy3131/minimal.js +18 -0
  8. package/examples/discovery/complete.js +25 -0
  9. package/examples/discovery/demo.js +30 -0
  10. package/examples/discovery/minimal.js +12 -0
  11. package/examples/display/il0373/complete.js +71 -0
  12. package/examples/display/il0373/demo.js +49 -0
  13. package/examples/display/il0373/minimal.js +26 -0
  14. package/examples/display/ssd1331/complete.js +83 -0
  15. package/examples/display/ssd1331/demo.js +49 -0
  16. package/examples/display/ssd1331/minimal.js +27 -0
  17. package/examples/display/st7036/complete.js +71 -0
  18. package/examples/display/st7036/demo.js +56 -0
  19. package/examples/display/st7036/minimal.js +21 -0
  20. package/examples/display/uc1611s/complete.js +135 -0
  21. package/examples/display/uc1611s/complete_spi.js +128 -0
  22. package/examples/display/uc1611s/demo.js +90 -0
  23. package/examples/display/uc1611s/demo_spi.js +91 -0
  24. package/examples/display/uc1611s/minimal.js +28 -0
  25. package/examples/display/uc1611s/minimal_spi.js +30 -0
  26. package/examples/display/uc8159/complete.js +64 -0
  27. package/examples/display/uc8159/demo.js +41 -0
  28. package/examples/display/uc8159/minimal.js +28 -0
  29. package/examples/display/uc8276c/complete.js +73 -0
  30. package/examples/display/uc8276c/demo.js +49 -0
  31. package/examples/display/uc8276c/minimal.js +26 -0
  32. package/examples/environmental/bme280/complete.js +1 -1
  33. package/examples/environmental/bme280/demo.js +1 -1
  34. package/examples/environmental/bme280/minimal.js +1 -1
  35. package/examples/gyroscope/l3g4200d/complete.js +1 -1
  36. package/examples/gyroscope/l3g4200d/demo.js +1 -1
  37. package/examples/gyroscope/l3g4200d/minimal.js +1 -1
  38. package/examples/memory/23x1024/complete.js +54 -0
  39. package/examples/memory/23x1024/demo.js +51 -0
  40. package/examples/memory/23x1024/minimal.js +22 -0
  41. package/examples/memory/mx25l12845e/complete.js +66 -0
  42. package/examples/memory/mx25l12845e/demo.js +66 -0
  43. package/examples/memory/mx25l12845e/minimal.js +27 -0
  44. package/examples/memory/sd_spi/complete.js +55 -0
  45. package/examples/memory/sd_spi/demo.js +67 -0
  46. package/examples/memory/sd_spi/minimal.js +32 -0
  47. package/examples/pressure/bmp280/complete.js +1 -1
  48. package/examples/pressure/bmp280/demo.js +1 -1
  49. package/examples/pressure/bmp280/minimal.js +1 -1
  50. package/examples/pressure/bmp581/complete.js +1 -1
  51. package/examples/pressure/bmp581/demo.js +1 -1
  52. package/examples/pressure/bmp581/minimal.js +1 -1
  53. package/examples/pressure/lps22df/complete.js +1 -1
  54. package/examples/pressure/lps22df/demo.js +1 -1
  55. package/examples/pressure/lps22df/minimal.js +1 -1
  56. package/package.json +1 -1
  57. package/src/chips/accelerometer/adxl345.js +10 -31
  58. package/src/chips/accelerometer/adxl362.js +7 -7
  59. package/src/chips/accelerometer/bma150.js +419 -0
  60. package/src/chips/adc_dac/ad7705.js +36 -9
  61. package/src/chips/adc_dac/ad7706.js +39 -9
  62. package/src/chips/adc_dac/hy3131.js +311 -0
  63. package/src/chips/display/il0373.js +607 -0
  64. package/src/chips/display/ssd1331.js +412 -0
  65. package/src/chips/display/st7036.js +401 -0
  66. package/src/chips/display/uc1611s.js +648 -0
  67. package/src/chips/display/uc8159.js +394 -0
  68. package/src/chips/display/uc8276c.js +658 -0
  69. package/src/chips/environmental/bme280.js +31 -46
  70. package/src/chips/environmental/bme680.js +38 -43
  71. package/src/chips/gas/ens160.js +18 -26
  72. package/src/chips/gyroscope/l3g4200d.js +34 -50
  73. package/src/chips/gyroscope/l3gd20h.js +24 -40
  74. package/src/chips/imu/mpu6050.js +29 -41
  75. package/src/chips/imu/mpu9250.js +42 -66
  76. package/src/chips/imu/mpu9255.js +51 -75
  77. package/src/chips/io_expander/mcp23017.js +24 -33
  78. package/src/chips/light/apds-9930.js +52 -60
  79. package/src/chips/light/apds9960.js +69 -77
  80. package/src/chips/magnetometer/as5600.js +13 -27
  81. package/src/chips/magnetometer/hmc5883l.js +32 -29
  82. package/src/chips/memory/_23x1024.js +228 -0
  83. package/src/chips/memory/_24aa02uid.js +8 -11
  84. package/src/chips/memory/mx25l12845e.js +467 -0
  85. package/src/chips/memory/sd_spi.js +486 -0
  86. package/src/chips/motor/drv8830.js +4 -4
  87. package/src/chips/other/mpr121.js +7 -7
  88. package/src/chips/power/ina219.js +5 -8
  89. package/src/chips/power/ina226.js +5 -8
  90. package/src/chips/power/ina3221.js +5 -8
  91. package/src/chips/pressure/bmp085.js +17 -11
  92. package/src/chips/pressure/bmp180.js +7 -7
  93. package/src/chips/pressure/bmp280.js +21 -33
  94. package/src/chips/pressure/bmp384.js +28 -39
  95. package/src/chips/pressure/bmp581.js +59 -70
  96. package/src/chips/pressure/lps22df.js +50 -52
  97. package/src/chips/pressure/lps28dfw.js +15 -13
  98. package/src/chips/pressure/lps33hw.js +7 -5
  99. package/src/chips/rtc/ds3231.js +8 -8
  100. package/src/chips/rtc/pcf8523.js +8 -14
  101. package/src/chips/temperature/mcp9808.js +9 -7
  102. package/src/chips/temperature/tmp117.js +4 -4
  103. package/src/connection/connection.js +6 -4
  104. package/src/connection/dhtxx.js +5 -3
  105. package/src/connection/display.js +278 -0
  106. package/src/connection/display_mock.js +77 -0
  107. package/src/connection/hx711.js +5 -3
  108. package/src/connection/i2c.js +6 -4
  109. package/src/connection/i2c_mock.js +47 -0
  110. package/src/connection/input_pin.js +36 -5
  111. package/src/connection/register.js +14 -0
  112. package/src/connection/register_connection.js +65 -0
  113. package/src/connection/sipo.js +4 -2
  114. package/src/connection/sipo_mock.js +49 -0
  115. package/src/connection/smbus.js +6 -4
  116. package/src/connection/softspi.js +73 -0
  117. package/src/connection/spi.js +110 -23
  118. package/src/connection/spi_mock.js +50 -0
  119. package/src/connection/uart.js +2 -1
  120. package/src/discovery/discovery.js +218 -0
  121. package/src/discovery/registry.js +85 -0
  122. package/src/storage/block_device.js +95 -0
  123. package/src/storage/ram_block_device.js +72 -0
@@ -77,7 +77,7 @@ function sleep(ms) {
77
77
  */
78
78
  class APDS9960Minimal {
79
79
  /**
80
- * @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x39).
80
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection pointing at the device (address 0x39).
81
81
  */
82
82
  constructor(connection) {
83
83
  this._conn = connection;
@@ -86,26 +86,18 @@ class APDS9960Minimal {
86
86
 
87
87
  async _init() {
88
88
  sleep(6);
89
- const id = await this._readReg(_REG_ID);
89
+ const id = (await this._conn.readReg(_REG_ID, 1))[0];
90
90
  if (id !== 0xAB) throw new Error('APDS-9960 not found (ID=0x' + id.toString(16) + ', expected 0xAB)');
91
- await this._writeReg(_REG_ENABLE, 0x00);
92
- await this._writeReg(_REG_ATIME, _ATIME_DEFAULT);
93
- await this._writeReg(_REG_CONTROL, _CONTROL_DEFAULT);
94
- await this._writeReg(_REG_CONFIG2, _CONFIG2_DEFAULT);
95
- await this._writeReg(_REG_ENABLE, 0x03);
91
+ await this._conn.writeReg(_REG_ENABLE, 0x00);
92
+ await this._conn.writeReg(_REG_ATIME, _ATIME_DEFAULT);
93
+ await this._conn.writeReg(_REG_CONTROL, _CONTROL_DEFAULT);
94
+ await this._conn.writeReg(_REG_CONFIG2, _CONFIG2_DEFAULT);
95
+ await this._conn.writeReg(_REG_ENABLE, 0x03);
96
96
  sleep(210);
97
97
  }
98
98
 
99
- async _writeReg(reg, value) {
100
- await this._conn.write(Buffer.from([reg, value]));
101
- }
102
-
103
- async _readReg(reg) {
104
- return (await this._conn.writeRead(Buffer.from([reg]), 1))[0];
105
- }
106
-
107
99
  async _readReg16LE(reg) {
108
- const buf = await this._conn.writeRead(Buffer.from([reg]), 2);
100
+ const buf = await this._conn.readReg(reg, 2);
109
101
  return buf[0] | (buf[1] << 8);
110
102
  }
111
103
 
@@ -122,7 +114,7 @@ class APDS9960Minimal {
122
114
  * @returns {Promise<number>} Raw red channel count, 0-65535.
123
115
  */
124
116
  async colorRed() {
125
- const raw = await this._conn.writeRead(Buffer.from([_REG_CDATAL]), 8);
117
+ const raw = await this._conn.readReg(_REG_CDATAL, 8);
126
118
  return raw[2] | (raw[3] << 8);
127
119
  }
128
120
 
@@ -133,7 +125,7 @@ class APDS9960Minimal {
133
125
  * @returns {Promise<number>} Raw green channel count, 0-65535.
134
126
  */
135
127
  async colorGreen() {
136
- const raw = await this._conn.writeRead(Buffer.from([_REG_CDATAL]), 8);
128
+ const raw = await this._conn.readReg(_REG_CDATAL, 8);
137
129
  return raw[4] | (raw[5] << 8);
138
130
  }
139
131
 
@@ -144,7 +136,7 @@ class APDS9960Minimal {
144
136
  * @returns {Promise<number>} Raw blue channel count, 0-65535.
145
137
  */
146
138
  async colorBlue() {
147
- const raw = await this._conn.writeRead(Buffer.from([_REG_CDATAL]), 8);
139
+ const raw = await this._conn.readReg(_REG_CDATAL, 8);
148
140
  return raw[6] | (raw[7] << 8);
149
141
  }
150
142
 
@@ -156,7 +148,7 @@ class APDS9960Minimal {
156
148
  * @returns {Promise<{ clear: number, red: number, green: number, blue: number }>}
157
149
  */
158
150
  async color() {
159
- const raw = await this._conn.writeRead(Buffer.from([_REG_CDATAL]), 8);
151
+ const raw = await this._conn.readReg(_REG_CDATAL, 8);
160
152
  return {
161
153
  clear: raw[0] | (raw[1] << 8),
162
154
  red: raw[2] | (raw[3] << 8),
@@ -174,7 +166,7 @@ class APDS9960Minimal {
174
166
  */
175
167
  class APDS9960Full extends APDS9960Minimal {
176
168
  /**
177
- * @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device (address 0x39).
169
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection pointing at the device (address 0x39).
178
170
  */
179
171
  constructor(connection) {
180
172
  super(connection);
@@ -186,16 +178,16 @@ class APDS9960Full extends APDS9960Minimal {
186
178
  * @returns {Promise<void>}
187
179
  */
188
180
  async enableProximity(enabled) {
189
- let val = await this._readReg(_REG_ENABLE);
181
+ let val = (await this._conn.readReg(_REG_ENABLE, 1))[0];
190
182
  if (enabled) val |= 0x04; else val &= ~0x04;
191
- await this._writeReg(_REG_ENABLE, val);
183
+ await this._conn.writeReg(_REG_ENABLE, val);
192
184
  }
193
185
 
194
186
  /**
195
187
  * Read the proximity count.
196
188
  * @returns {Promise<number>} Proximity count 0-255; higher means closer.
197
189
  */
198
- async proximity() { return this._readReg(_REG_PDATA); }
190
+ async proximity() { return (await this._conn.readReg(_REG_PDATA, 1))[0]; }
199
191
 
200
192
  /**
201
193
  * Enable or disable the wait engine.
@@ -203,9 +195,9 @@ class APDS9960Full extends APDS9960Minimal {
203
195
  * @returns {Promise<void>}
204
196
  */
205
197
  async enableWait(enabled) {
206
- let val = await this._readReg(_REG_ENABLE);
198
+ let val = (await this._conn.readReg(_REG_ENABLE, 1))[0];
207
199
  if (enabled) val |= 0x08; else val &= ~0x08;
208
- await this._writeReg(_REG_ENABLE, val);
200
+ await this._conn.writeReg(_REG_ENABLE, val);
209
201
  }
210
202
 
211
203
  /**
@@ -215,11 +207,11 @@ class APDS9960Full extends APDS9960Minimal {
215
207
  * @returns {Promise<void>}
216
208
  */
217
209
  async configureWait(wtime, wlong = false) {
218
- await this._writeReg(_REG_WTIME, wtime & 0xFF);
219
- let c1 = await this._readReg(_REG_CONFIG1);
210
+ await this._conn.writeReg(_REG_WTIME, wtime & 0xFF);
211
+ let c1 = (await this._conn.readReg(_REG_CONFIG1, 1))[0];
220
212
  if (wlong) c1 |= 0x02; else c1 &= ~0x02;
221
213
  c1 = (c1 & 0x03) | 0x60;
222
- await this._writeReg(_REG_CONFIG1, c1);
214
+ await this._conn.writeReg(_REG_CONFIG1, c1);
223
215
  }
224
216
 
225
217
  /**
@@ -229,10 +221,10 @@ class APDS9960Full extends APDS9960Minimal {
229
221
  * @returns {Promise<void>}
230
222
  */
231
223
  async configureAls(atime, again) {
232
- await this._writeReg(_REG_ATIME, atime & 0xFF);
233
- let ctrl = await this._readReg(_REG_CONTROL);
224
+ await this._conn.writeReg(_REG_ATIME, atime & 0xFF);
225
+ let ctrl = (await this._conn.readReg(_REG_CONTROL, 1))[0];
234
226
  ctrl = (ctrl & 0xFC) | (again & 0x03);
235
- await this._writeReg(_REG_CONTROL, ctrl);
227
+ await this._conn.writeReg(_REG_CONTROL, ctrl);
236
228
  }
237
229
 
238
230
  /**
@@ -244,10 +236,10 @@ class APDS9960Full extends APDS9960Minimal {
244
236
  * @returns {Promise<void>}
245
237
  */
246
238
  async configureProximityLed(ldrive, pgain, ppulse, pplen) {
247
- let ctrl = await this._readReg(_REG_CONTROL);
239
+ let ctrl = (await this._conn.readReg(_REG_CONTROL, 1))[0];
248
240
  ctrl = ((ldrive & 0x03) << 6) | ((pgain & 0x03) << 2) | (ctrl & 0x03);
249
- await this._writeReg(_REG_CONTROL, ctrl);
250
- await this._writeReg(_REG_PPULSE, ((pplen & 0x03) << 6) | (ppulse & 0x3F));
241
+ await this._conn.writeReg(_REG_CONTROL, ctrl);
242
+ await this._conn.writeReg(_REG_PPULSE, ((pplen & 0x03) << 6) | (ppulse & 0x3F));
251
243
  }
252
244
 
253
245
  /**
@@ -256,9 +248,9 @@ class APDS9960Full extends APDS9960Minimal {
256
248
  * @returns {Promise<void>}
257
249
  */
258
250
  async setLedBoost(boost) {
259
- let c2 = await this._readReg(_REG_CONFIG2);
251
+ let c2 = (await this._conn.readReg(_REG_CONFIG2, 1))[0];
260
252
  c2 = (c2 & 0xCF) | ((boost & 0x03) << 4) | 0x01;
261
- await this._writeReg(_REG_CONFIG2, c2);
253
+ await this._conn.writeReg(_REG_CONFIG2, c2);
262
254
  }
263
255
 
264
256
  /**
@@ -268,10 +260,10 @@ class APDS9960Full extends APDS9960Minimal {
268
260
  * @returns {Promise<void>}
269
261
  */
270
262
  async alsThreshold(low, high) {
271
- await this._writeReg(_REG_AILTL, low & 0xFF);
272
- await this._writeReg(_REG_AILTH, (low >> 8) & 0xFF);
273
- await this._writeReg(_REG_AIHTL, high & 0xFF);
274
- await this._writeReg(_REG_AIHTH, (high >> 8) & 0xFF);
263
+ await this._conn.writeReg(_REG_AILTL, low & 0xFF);
264
+ await this._conn.writeReg(_REG_AILTH, (low >> 8) & 0xFF);
265
+ await this._conn.writeReg(_REG_AIHTL, high & 0xFF);
266
+ await this._conn.writeReg(_REG_AIHTH, (high >> 8) & 0xFF);
275
267
  }
276
268
 
277
269
  /**
@@ -281,8 +273,8 @@ class APDS9960Full extends APDS9960Minimal {
281
273
  * @returns {Promise<void>}
282
274
  */
283
275
  async proximityThreshold(low, high) {
284
- await this._writeReg(_REG_PILT, low & 0xFF);
285
- await this._writeReg(_REG_PIHT, high & 0xFF);
276
+ await this._conn.writeReg(_REG_PILT, low & 0xFF);
277
+ await this._conn.writeReg(_REG_PIHT, high & 0xFF);
286
278
  }
287
279
 
288
280
  /**
@@ -292,7 +284,7 @@ class APDS9960Full extends APDS9960Minimal {
292
284
  * @returns {Promise<void>}
293
285
  */
294
286
  async setPersistence(ppers, apers) {
295
- await this._writeReg(_REG_PERS, ((ppers & 0x0F) << 4) | (apers & 0x0F));
287
+ await this._conn.writeReg(_REG_PERS, ((ppers & 0x0F) << 4) | (apers & 0x0F));
296
288
  }
297
289
 
298
290
  /**
@@ -301,9 +293,9 @@ class APDS9960Full extends APDS9960Minimal {
301
293
  * @returns {Promise<void>}
302
294
  */
303
295
  async enableAlsInterrupt(enabled) {
304
- let val = await this._readReg(_REG_ENABLE);
296
+ let val = (await this._conn.readReg(_REG_ENABLE, 1))[0];
305
297
  if (enabled) val |= 0x10; else val &= ~0x10;
306
- await this._writeReg(_REG_ENABLE, val);
298
+ await this._conn.writeReg(_REG_ENABLE, val);
307
299
  }
308
300
 
309
301
  /**
@@ -312,9 +304,9 @@ class APDS9960Full extends APDS9960Minimal {
312
304
  * @returns {Promise<void>}
313
305
  */
314
306
  async enableProximityInterrupt(enabled) {
315
- let val = await this._readReg(_REG_ENABLE);
307
+ let val = (await this._conn.readReg(_REG_ENABLE, 1))[0];
316
308
  if (enabled) val |= 0x20; else val &= ~0x20;
317
- await this._writeReg(_REG_ENABLE, val);
309
+ await this._conn.writeReg(_REG_ENABLE, val);
318
310
  }
319
311
 
320
312
  /**
@@ -349,8 +341,8 @@ class APDS9960Full extends APDS9960Minimal {
349
341
  */
350
342
  async setProximityOffset(ur, dl) {
351
343
  const encode = (v) => v < 0 ? (0x80 | ((-v) & 0x7F)) : (v & 0x7F);
352
- await this._writeReg(_REG_POFFSET_UR, encode(ur));
353
- await this._writeReg(_REG_POFFSET_DL, encode(dl));
344
+ await this._conn.writeReg(_REG_POFFSET_UR, encode(ur));
345
+ await this._conn.writeReg(_REG_POFFSET_DL, encode(dl));
354
346
  }
355
347
 
356
348
  /**
@@ -362,12 +354,12 @@ class APDS9960Full extends APDS9960Minimal {
362
354
  * @returns {Promise<void>}
363
355
  */
364
356
  async setProximityMask(u, d, l, r) {
365
- let c3 = (await this._readReg(_REG_CONFIG3)) & 0xF0;
357
+ let c3 = (await this._conn.readReg(_REG_CONFIG3, 1))[0] & 0xF0;
366
358
  if (u) c3 |= 0x08;
367
359
  if (d) c3 |= 0x04;
368
360
  if (l) c3 |= 0x02;
369
361
  if (r) c3 |= 0x01;
370
- await this._writeReg(_REG_CONFIG3, c3);
362
+ await this._conn.writeReg(_REG_CONFIG3, c3);
371
363
  }
372
364
 
373
365
  /**
@@ -376,19 +368,19 @@ class APDS9960Full extends APDS9960Minimal {
376
368
  * @returns {Promise<void>}
377
369
  */
378
370
  async enableGesture(enabled) {
379
- let val = await this._readReg(_REG_ENABLE);
371
+ let val = (await this._conn.readReg(_REG_ENABLE, 1))[0];
380
372
  if (enabled) {
381
373
  val |= 0x40;
382
- await this._writeReg(_REG_ENABLE, val);
383
- let g4 = await this._readReg(_REG_GCONF4);
374
+ await this._conn.writeReg(_REG_ENABLE, val);
375
+ let g4 = (await this._conn.readReg(_REG_GCONF4, 1))[0];
384
376
  g4 |= 0x01;
385
- await this._writeReg(_REG_GCONF4, g4);
377
+ await this._conn.writeReg(_REG_GCONF4, g4);
386
378
  } else {
387
379
  val &= ~0x40;
388
- await this._writeReg(_REG_ENABLE, val);
389
- let g4 = await this._readReg(_REG_GCONF4);
380
+ await this._conn.writeReg(_REG_ENABLE, val);
381
+ let g4 = (await this._conn.readReg(_REG_GCONF4, 1))[0];
390
382
  g4 &= ~0x01;
391
- await this._writeReg(_REG_GCONF4, g4);
383
+ await this._conn.writeReg(_REG_GCONF4, g4);
392
384
  }
393
385
  }
394
386
 
@@ -404,29 +396,29 @@ class APDS9960Full extends APDS9960Minimal {
404
396
  * @returns {Promise<void>}
405
397
  */
406
398
  async configureGesture(ggain, gldrive, gpulse, gplen, gwtime, gpenth, gexth) {
407
- await this._writeReg(_REG_GPENTH, gpenth & 0xFF);
408
- await this._writeReg(_REG_GEXTH, gexth & 0xFF);
399
+ await this._conn.writeReg(_REG_GPENTH, gpenth & 0xFF);
400
+ await this._conn.writeReg(_REG_GEXTH, gexth & 0xFF);
409
401
  const g2 = ((ggain & 0x03) << 5) | ((gldrive & 0x03) << 3) | (gwtime & 0x07);
410
- await this._writeReg(_REG_GCONF2, g2);
411
- await this._writeReg(_REG_GPULSE, ((gplen & 0x03) << 6) | (gpulse & 0x3F));
402
+ await this._conn.writeReg(_REG_GCONF2, g2);
403
+ await this._conn.writeReg(_REG_GPULSE, ((gplen & 0x03) << 6) | (gpulse & 0x3F));
412
404
  }
413
405
 
414
406
  /**
415
407
  * Check if gesture data is available in the FIFO.
416
408
  * @returns {Promise<boolean>} true if GSTATUS.GVALID is set.
417
409
  */
418
- async gestureAvailable() { return !!((await this._readReg(_REG_GSTATUS)) & 0x01); }
410
+ async gestureAvailable() { return !!((await this._conn.readReg(_REG_GSTATUS, 1))[0] & 0x01); }
419
411
 
420
412
  /**
421
413
  * Read all gesture datasets from the FIFO.
422
414
  * @returns {Promise<Array<{ u: number, d: number, l: number, r: number }>>}
423
415
  */
424
416
  async readGestureFifo() {
425
- const level = await this._readReg(_REG_GFLVL);
417
+ const level = (await this._conn.readReg(_REG_GFLVL, 1))[0];
426
418
  if (level === 0) return [];
427
419
  const result = [];
428
420
  for (let i = 0; i < level; i++) {
429
- const raw = await this._conn.writeRead(Buffer.from([_REG_GFIFO_U]), 4);
421
+ const raw = await this._conn.readReg(_REG_GFIFO_U, 4);
430
422
  result.push({ u: raw[0], d: raw[1], l: raw[2], r: raw[3] });
431
423
  }
432
424
  return result;
@@ -436,16 +428,16 @@ class APDS9960Full extends APDS9960Minimal {
436
428
  * Read the number of datasets in the gesture FIFO.
437
429
  * @returns {Promise<number>} Number of 4-byte datasets currently in FIFO.
438
430
  */
439
- async gestureFifoLevel() { return this._readReg(_REG_GFLVL); }
431
+ async gestureFifoLevel() { return (await this._conn.readReg(_REG_GFLVL, 1))[0]; }
440
432
 
441
433
  /**
442
434
  * Clear the gesture FIFO by setting GFIFO_CLR in GCONF4.
443
435
  * @returns {Promise<void>}
444
436
  */
445
437
  async clearGestureFifo() {
446
- let g4 = await this._readReg(_REG_GCONF4);
438
+ let g4 = (await this._conn.readReg(_REG_GCONF4, 1))[0];
447
439
  g4 |= 0x04;
448
- await this._writeReg(_REG_GCONF4, g4);
440
+ await this._conn.writeReg(_REG_GCONF4, g4);
449
441
  }
450
442
 
451
443
  /**
@@ -454,46 +446,46 @@ class APDS9960Full extends APDS9960Minimal {
454
446
  * @returns {Promise<void>}
455
447
  */
456
448
  async enableGestureInterrupt(enabled) {
457
- let g4 = await this._readReg(_REG_GCONF4);
449
+ let g4 = (await this._conn.readReg(_REG_GCONF4, 1))[0];
458
450
  if (enabled) g4 |= 0x02; else g4 &= ~0x02;
459
- await this._writeReg(_REG_GCONF4, g4);
451
+ await this._conn.writeReg(_REG_GCONF4, g4);
460
452
  }
461
453
 
462
454
  /**
463
455
  * Read the raw STATUS register.
464
456
  * @returns {Promise<number>} Raw STATUS byte.
465
457
  */
466
- async status() { return this._readReg(_REG_STATUS); }
458
+ async status() { return (await this._conn.readReg(_REG_STATUS, 1))[0]; }
467
459
 
468
460
  /**
469
461
  * Check if ALS/color data is valid.
470
462
  * @returns {Promise<boolean>} true if STATUS.AVALID is set.
471
463
  */
472
- async isAlsValid() { return !!((await this._readReg(_REG_STATUS)) & 0x01); }
464
+ async isAlsValid() { return !!((await this._conn.readReg(_REG_STATUS, 1))[0] & 0x01); }
473
465
 
474
466
  /**
475
467
  * Check if proximity data is valid.
476
468
  * @returns {Promise<boolean>} true if STATUS.PVALID is set.
477
469
  */
478
- async isProximityValid() { return !!((await this._readReg(_REG_STATUS)) & 0x02); }
470
+ async isProximityValid() { return !!((await this._conn.readReg(_REG_STATUS, 1))[0] & 0x02); }
479
471
 
480
472
  /**
481
473
  * Check if the clear photodiode is saturated.
482
474
  * @returns {Promise<boolean>} true if STATUS.CPSAT is set.
483
475
  */
484
- async isAlsSaturated() { return !!((await this._readReg(_REG_STATUS)) & 0x80); }
476
+ async isAlsSaturated() { return !!((await this._conn.readReg(_REG_STATUS, 1))[0] & 0x80); }
485
477
 
486
478
  /**
487
479
  * Check if analog saturation occurred during proximity.
488
480
  * @returns {Promise<boolean>} true if STATUS.PGSAT is set.
489
481
  */
490
- async isProximitySaturated() { return !!((await this._readReg(_REG_STATUS)) & 0x40); }
482
+ async isProximitySaturated() { return !!((await this._conn.readReg(_REG_STATUS, 1))[0] & 0x40); }
491
483
 
492
484
  /**
493
485
  * Read the device ID register.
494
486
  * @returns {Promise<number>} ID register value (expect 0xAB).
495
487
  */
496
- async chipId() { return this._readReg(_REG_ID); }
488
+ async chipId() { return (await this._conn.readReg(_REG_ID, 1))[0]; }
497
489
  }
498
490
 
499
491
  module.exports = { APDS9960Minimal, APDS9960Full };
@@ -41,7 +41,7 @@ const _STATUS_MH = 0x20;
41
41
  */
42
42
  class AS5600Minimal {
43
43
  /**
44
- * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (fixed address 0x36).
44
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection pointing at the device (fixed address 0x36).
45
45
  */
46
46
  constructor(connection) {
47
47
  this._conn = connection;
@@ -56,28 +56,14 @@ class AS5600Minimal {
56
56
  }
57
57
 
58
58
  async _readReg8(reg) {
59
- return (await this._conn.writeRead(Buffer.from([reg]), 1))[0];
59
+ return (await this._conn.readReg(reg, 1))[0];
60
60
  }
61
61
 
62
62
  async _readReg16(reg) {
63
- const raw = await this._conn.writeRead(Buffer.from([reg]), 2);
63
+ const raw = await this._conn.readReg(reg, 2);
64
64
  return (raw[0] << 8) | raw[1];
65
65
  }
66
66
 
67
- async _writeReg8(reg, value) {
68
- const buf = Buffer.alloc(2);
69
- buf[0] = reg;
70
- buf[1] = value;
71
- await this._conn.write(buf);
72
- }
73
-
74
- async _writeReg16(reg, value) {
75
- const buf = Buffer.alloc(3);
76
- buf[0] = reg;
77
- buf.writeUInt16BE(value, 1);
78
- await this._conn.write(buf);
79
- }
80
-
81
67
  /**
82
68
  * Read the scaled absolute angle.
83
69
  * @returns {Promise<number>} Angle in degrees, 0.0–360.0 (exclusive).
@@ -137,7 +123,7 @@ class AS5600Full extends AS5600Minimal {
137
123
  static OUTS_PWM = 2;
138
124
 
139
125
  /**
140
- * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (fixed address 0x36).
126
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection pointing at the device (fixed address 0x36).
141
127
  */
142
128
  constructor(connection) {
143
129
  super(connection);
@@ -205,7 +191,7 @@ class AS5600Full extends AS5600Minimal {
205
191
  let confL = await this._readReg8(_REG_CONF_L);
206
192
  confH = (confH & 0xC0) | ((wd ? 1 : 0) << 5) | ((fth & 0x07) << 2) | (sf & 0x03);
207
193
  confL = ((pwmf & 0x03) << 6) | ((outs & 0x03) << 4) | ((hyst & 0x03) << 2) | (pm & 0x03);
208
- await this._writeReg16(_REG_CONF_H, (confH << 8) | confL);
194
+ await this._conn.writeReg(_REG_CONF_H, Buffer.from([(((confH << 8) | confL) >> 8) & 0xFF, ((confH << 8) | confL) & 0xFF]));
209
195
  }
210
196
 
211
197
  /**
@@ -214,8 +200,8 @@ class AS5600Full extends AS5600Minimal {
214
200
  * @returns {Promise<void>}
215
201
  */
216
202
  async setZeroPosition(pos) {
217
- await this._writeReg8(_REG_ZPOS_H, (pos >> 8) & 0x0F);
218
- await this._writeReg8(_REG_ZPOS_L, pos & 0xFF);
203
+ await this._conn.writeReg(_REG_ZPOS_H, (pos >> 8) & 0x0F);
204
+ await this._conn.writeReg(_REG_ZPOS_L, pos & 0xFF);
219
205
  }
220
206
 
221
207
  /**
@@ -224,8 +210,8 @@ class AS5600Full extends AS5600Minimal {
224
210
  * @returns {Promise<void>}
225
211
  */
226
212
  async setMaxPosition(pos) {
227
- await this._writeReg8(_REG_MPOS_H, (pos >> 8) & 0x0F);
228
- await this._writeReg8(_REG_MPOS_L, pos & 0xFF);
213
+ await this._conn.writeReg(_REG_MPOS_H, (pos >> 8) & 0x0F);
214
+ await this._conn.writeReg(_REG_MPOS_L, pos & 0xFF);
229
215
  }
230
216
 
231
217
  /**
@@ -234,8 +220,8 @@ class AS5600Full extends AS5600Minimal {
234
220
  * @returns {Promise<void>}
235
221
  */
236
222
  async setMaxAngle(span) {
237
- await this._writeReg8(_REG_MANG_H, (span >> 8) & 0x0F);
238
- await this._writeReg8(_REG_MANG_L, span & 0xFF);
223
+ await this._conn.writeReg(_REG_MANG_H, (span >> 8) & 0x0F);
224
+ await this._conn.writeReg(_REG_MANG_L, span & 0xFF);
239
225
  }
240
226
 
241
227
  /**
@@ -290,7 +276,7 @@ class AS5600Full extends AS5600Minimal {
290
276
  if (zmco >= 3) {
291
277
  throw new Error('AS5600: cannot burn angle — ZMCO limit reached (3)');
292
278
  }
293
- await this._writeReg8(_REG_BURN, 0x80);
279
+ await this._conn.writeReg(_REG_BURN, 0x80);
294
280
  }
295
281
 
296
282
  /**
@@ -306,7 +292,7 @@ class AS5600Full extends AS5600Minimal {
306
292
  if (zmco !== 0) {
307
293
  throw new Error('AS5600: cannot burn setting — ZMCO must be 0');
308
294
  }
309
- await this._writeReg8(_REG_BURN, 0x40);
295
+ await this._conn.writeReg(_REG_BURN, 0x40);
310
296
  }
311
297
  }
312
298
 
@@ -9,6 +9,11 @@ const _REG_ID_A = 0x0A;
9
9
  const _REG_ID_B = 0x0B;
10
10
  const _REG_ID_C = 0x0C;
11
11
 
12
+ function _delay(ms) {
13
+ const start = Date.now();
14
+ while (Date.now() - start < ms) { /* spin */ }
15
+ }
16
+
12
17
  const _GAIN_LSB_PER_GAUSS = {
13
18
  0: 1370, // GN=0: ±0.88 Ga
14
19
  1: 1090, // GN=1: ±1.3 Ga (default)
@@ -34,7 +39,7 @@ const _GAIN_LSB_PER_GAUSS = {
34
39
  */
35
40
  class HMC5883LMinimal {
36
41
  /**
37
- * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (fixed address 0x1E).
42
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection pointing at the device (fixed address 0x1E).
38
43
  */
39
44
  constructor(connection) {
40
45
  this._conn = connection;
@@ -44,30 +49,23 @@ class HMC5883LMinimal {
44
49
  }
45
50
 
46
51
  async _initMinimal() {
47
- await this._writeReg8(_REG_CONFIG_A, 0x70);
48
- await this._writeReg8(_REG_CONFIG_B, 0x20);
49
- await this._writeReg8(_REG_MODE, 0x00);
50
- // 6 ms delay for first measurement - caller should wait if needed
52
+ await this._conn.writeReg(_REG_CONFIG_A, 0x70);
53
+ await this._conn.writeReg(_REG_CONFIG_B, 0x20);
54
+ await this._conn.writeReg(_REG_MODE, 0x00);
55
+ _delay(6); // first measurement available ~6 ms after mode write
51
56
  }
52
57
 
53
58
  async _readReg8(reg) {
54
- return (await this._conn.writeRead(Buffer.from([reg]), 1))[0];
59
+ return (await this._conn.readReg(reg, 1))[0];
55
60
  }
56
61
 
57
62
  async _readReg16(reg) {
58
- const raw = await this._conn.writeRead(Buffer.from([reg]), 2);
63
+ const raw = await this._conn.readReg(reg, 2);
59
64
  return raw.readInt16BE(0);
60
65
  }
61
66
 
62
- async _writeReg8(reg, value) {
63
- const buf = Buffer.alloc(2);
64
- buf[0] = reg;
65
- buf[1] = value;
66
- await this._conn.write(buf);
67
- }
68
-
69
67
  async _readDataBurst() {
70
- const raw = await this._conn.writeRead(Buffer.from([_REG_DATA_X_MSB]), 6);
68
+ const raw = await this._conn.readReg(_REG_DATA_X_MSB, 6);
71
69
  const rawX = raw.readInt16BE(0);
72
70
  const rawZ = raw.readInt16BE(2);
73
71
  const rawY = raw.readInt16BE(4);
@@ -103,7 +101,7 @@ class HMC5883LMinimal {
103
101
  */
104
102
  class HMC5883LFull extends HMC5883LMinimal {
105
103
  /**
106
- * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (fixed address 0x1E).
104
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection pointing at the device (fixed address 0x1E).
107
105
  */
108
106
  constructor(connection) {
109
107
  super(connection);
@@ -121,17 +119,20 @@ class HMC5883LFull extends HMC5883LMinimal {
121
119
  const doMap = { 0.75: 0b000, 1.5: 0b001, 3: 0b010, 7.5: 0b011,
122
120
  15: 0b100, 30: 0b101, 75: 0b110 };
123
121
 
124
- if (!maMap[averaging]) throw new Error('averaging must be 1, 2, 4, or 8');
125
- if (!doMap[odr]) throw new Error('odr must be 0.75, 1.5, 3, 7.5, 15, 30, or 75');
122
+ // NOTE: maMap[1]===0 and doMap[0.75]===0 are valid mapped values, so
123
+ // this must check key presence (`in`), not truthiness of the value --
124
+ // `!maMap[averaging]` would wrongly reject averaging=1 and odr=0.75.
125
+ if (!(averaging in maMap)) throw new Error('averaging must be 1, 2, 4, or 8');
126
+ if (!(odr in doMap)) throw new Error('odr must be 0.75, 1.5, 3, 7.5, 15, 30, or 75');
126
127
  if (gain < 0 || gain > 7) throw new Error('gain must be 0–7');
127
128
 
128
129
  const ma = maMap[averaging];
129
130
  const doBits = doMap[odr];
130
131
  const configA = (ma << 5) | (doBits << 2);
131
- await this._writeReg8(_REG_CONFIG_A, configA);
132
+ await this._conn.writeReg(_REG_CONFIG_A, configA);
132
133
 
133
134
  const configB = (gain << 5);
134
- await this._writeReg8(_REG_CONFIG_B, configB);
135
+ await this._conn.writeReg(_REG_CONFIG_B, configB);
135
136
 
136
137
  this._gain = gain;
137
138
  this._gainLsbPerGauss = _GAIN_LSB_PER_GAUSS[gain];
@@ -144,7 +145,7 @@ class HMC5883LFull extends HMC5883LMinimal {
144
145
  */
145
146
  async setGain(gain) {
146
147
  if (gain < 0 || gain > 7) throw new Error('gain must be 0–7');
147
- await this._writeReg8(_REG_CONFIG_B, gain << 5);
148
+ await this._conn.writeReg(_REG_CONFIG_B, gain << 5);
148
149
  this._gain = gain;
149
150
  this._gainLsbPerGauss = _GAIN_LSB_PER_GAUSS[gain];
150
151
  }
@@ -156,8 +157,10 @@ class HMC5883LFull extends HMC5883LMinimal {
156
157
  */
157
158
  async setMode(mode) {
158
159
  const modeMap = { continuous: 0b00, single: 0b01, idle: 0b10 };
159
- if (!modeMap[mode]) throw new Error("mode must be 'continuous', 'single', or 'idle'");
160
- await this._writeReg8(_REG_MODE, modeMap[mode]);
160
+ // modeMap.continuous === 0, so this must check key presence, not
161
+ // truthiness -- `!modeMap[mode]` would wrongly reject 'continuous'.
162
+ if (!(mode in modeMap)) throw new Error("mode must be 'continuous', 'single', or 'idle'");
163
+ await this._conn.writeReg(_REG_MODE, modeMap[mode]);
161
164
  }
162
165
 
163
166
  /**
@@ -183,8 +186,8 @@ class HMC5883LFull extends HMC5883LMinimal {
183
186
  * Returns null for any axis that overflows (raw == -4096).
184
187
  */
185
188
  async singleMeasurement() {
186
- await this._writeReg8(_REG_MODE, 0x01);
187
- // Caller should wait 6 ms before reading
189
+ await this._conn.writeReg(_REG_MODE, 0x01);
190
+ _delay(6);
188
191
  return this.magneticField();
189
192
  }
190
193
 
@@ -208,13 +211,13 @@ class HMC5883LFull extends HMC5883LMinimal {
208
211
  async selfTest(positive = true) {
209
212
  const configA = await this._readReg8(_REG_CONFIG_A);
210
213
  const ms = positive ? 0b01 : 0b10;
211
- await this._writeReg8(_REG_CONFIG_A, (configA & 0xFC) | ms);
214
+ await this._conn.writeReg(_REG_CONFIG_A, (configA & 0xFC) | ms);
212
215
 
213
- await this._writeReg8(_REG_MODE, 0x01);
214
- // Caller should wait 6 ms
216
+ await this._conn.writeReg(_REG_MODE, 0x01);
217
+ _delay(6);
215
218
  const result = await this.magneticField();
216
219
 
217
- await this._writeReg8(_REG_CONFIG_A, (configA & 0xFC) | 0b00);
220
+ await this._conn.writeReg(_REG_CONFIG_A, (configA & 0xFC) | 0b00);
218
221
  return result;
219
222
  }
220
223
  }