periph 1.1.0 → 1.2.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 (266) hide show
  1. package/README.md +61 -12
  2. package/examples/accelerometer/adxl345/complete.js +52 -0
  3. package/examples/accelerometer/adxl345/demo.js +34 -0
  4. package/examples/accelerometer/adxl345/minimal.js +18 -0
  5. package/examples/accelerometer/adxl362/complete.js +57 -0
  6. package/examples/accelerometer/adxl362/demo.js +51 -0
  7. package/examples/accelerometer/adxl362/minimal.js +21 -0
  8. package/examples/adc_dac/ad7705/complete.js +32 -0
  9. package/examples/adc_dac/ad7705/demo.js +42 -0
  10. package/examples/adc_dac/ad7705/minimal.js +16 -0
  11. package/examples/adc_dac/ad7706/complete.js +35 -0
  12. package/examples/adc_dac/ad7706/demo.js +44 -0
  13. package/examples/adc_dac/ad7706/minimal.js +16 -0
  14. package/examples/adc_dac/hx710a/complete.js +45 -0
  15. package/examples/adc_dac/hx710a/demo.js +52 -0
  16. package/examples/adc_dac/hx710a/minimal.js +16 -0
  17. package/examples/adc_dac/hx710b/complete.js +45 -0
  18. package/examples/adc_dac/hx710b/demo.js +64 -0
  19. package/examples/adc_dac/hx710b/minimal.js +16 -0
  20. package/examples/adc_dac/hx711/complete.js +7 -7
  21. package/examples/adc_dac/hx711/demo.js +6 -6
  22. package/examples/adc_dac/hx711/minimal.js +7 -7
  23. package/examples/adc_dac/mcp4725/complete.js +19 -17
  24. package/examples/adc_dac/mcp4725/demo.js +5 -5
  25. package/examples/adc_dac/mcp4725/minimal.js +8 -6
  26. package/examples/adc_dac/mcp4728/complete.js +24 -22
  27. package/examples/adc_dac/mcp4728/demo.js +22 -22
  28. package/examples/adc_dac/mcp4728/minimal.js +8 -6
  29. package/examples/adc_dac/pcf8591/complete.js +41 -0
  30. package/examples/adc_dac/pcf8591/demo.js +39 -0
  31. package/examples/adc_dac/pcf8591/minimal.js +19 -0
  32. package/examples/comms/mcp2515/complete.js +75 -0
  33. package/examples/comms/mcp2515/demo.js +66 -0
  34. package/examples/comms/mcp2515/minimal.js +27 -0
  35. package/examples/comms/rda5807m/complete.js +55 -0
  36. package/examples/comms/rda5807m/demo.js +68 -0
  37. package/examples/comms/rda5807m/minimal.js +15 -0
  38. package/examples/comms/rfm9x/complete.js +50 -0
  39. package/examples/comms/rfm9x/demo.js +55 -0
  40. package/examples/comms/rfm9x/minimal.js +20 -0
  41. package/examples/display/pcf8576/complete.js +18 -15
  42. package/examples/display/pcf8576/demo.js +6 -6
  43. package/examples/display/pcf8576/minimal.js +12 -10
  44. package/examples/environmental/aht21/complete.js +15 -13
  45. package/examples/environmental/aht21/demo.js +8 -6
  46. package/examples/environmental/aht21/minimal.js +5 -5
  47. package/examples/environmental/bme280/complete.js +23 -20
  48. package/examples/environmental/bme280/demo.js +14 -14
  49. package/examples/environmental/bme280/minimal.js +13 -11
  50. package/examples/environmental/bme680/complete.js +25 -22
  51. package/examples/environmental/bme680/demo.js +38 -36
  52. package/examples/environmental/bme680/minimal.js +14 -12
  53. package/examples/gas/ens160/complete.js +29 -27
  54. package/examples/gas/ens160/demo.js +39 -37
  55. package/examples/gas/ens160/minimal.js +17 -15
  56. package/examples/gnss/neo6/complete.js +49 -0
  57. package/examples/gnss/neo6/demo.js +54 -0
  58. package/examples/gnss/neo6/minimal.js +30 -0
  59. package/examples/gyroscope/l3g4200d/complete.js +56 -0
  60. package/examples/gyroscope/l3g4200d/demo.js +49 -0
  61. package/examples/gyroscope/l3g4200d/minimal.js +19 -0
  62. package/examples/gyroscope/l3gd20h/complete.js +42 -0
  63. package/examples/gyroscope/l3gd20h/demo.js +29 -0
  64. package/examples/gyroscope/l3gd20h/minimal.js +13 -0
  65. package/examples/humidity/dht11/complete.js +22 -0
  66. package/examples/humidity/dht11/demo.js +38 -0
  67. package/examples/humidity/dht11/minimal.js +18 -0
  68. package/examples/imu/mpu6050/complete.js +27 -25
  69. package/examples/imu/mpu6050/demo.js +29 -26
  70. package/examples/imu/mpu6050/minimal.js +6 -6
  71. package/examples/imu/mpu9250/complete.js +65 -0
  72. package/examples/imu/mpu9250/demo.js +53 -0
  73. package/examples/imu/mpu9250/minimal.js +21 -0
  74. package/examples/imu/mpu9255/complete.js +73 -0
  75. package/examples/imu/mpu9255/demo.js +73 -0
  76. package/examples/imu/mpu9255/minimal.js +21 -0
  77. package/examples/io_expander/mcp23017/complete.js +70 -77
  78. package/examples/io_expander/mcp23017/demo.js +37 -33
  79. package/examples/io_expander/mcp23017/minimal.js +15 -11
  80. package/examples/io_expander/pcf8574/complete.js +60 -65
  81. package/examples/io_expander/pcf8574/demo.js +30 -25
  82. package/examples/io_expander/pcf8574/minimal.js +15 -11
  83. package/examples/io_expander/pcf8575/complete.js +24 -20
  84. package/examples/io_expander/pcf8575/demo.js +27 -23
  85. package/examples/io_expander/pcf8575/minimal.js +16 -12
  86. package/examples/io_expander/tpic6b595/complete.js +57 -0
  87. package/examples/io_expander/tpic6b595/demo.js +75 -0
  88. package/examples/io_expander/tpic6b595/minimal.js +26 -0
  89. package/examples/led/apa102/complete.js +72 -0
  90. package/examples/led/apa102/demo.js +92 -0
  91. package/examples/led/apa102/minimal.js +21 -0
  92. package/examples/led/sk6812rgbw/complete.js +42 -40
  93. package/examples/led/sk6812rgbw/demo.js +16 -16
  94. package/examples/led/sk6812rgbw/minimal.js +15 -17
  95. package/examples/led/ws2812b/complete.js +40 -38
  96. package/examples/led/ws2812b/demo.js +16 -16
  97. package/examples/led/ws2812b/minimal.js +13 -14
  98. package/examples/led/ws2814/complete.js +71 -0
  99. package/examples/led/ws2814/demo.js +82 -0
  100. package/examples/led/ws2814/minimal.js +23 -0
  101. package/examples/light/apds-9930/complete.js +46 -0
  102. package/examples/light/apds-9930/demo.js +31 -0
  103. package/examples/light/apds-9930/minimal.js +20 -0
  104. package/examples/light/apds9960/complete.js +45 -43
  105. package/examples/light/apds9960/demo.js +11 -9
  106. package/examples/light/apds9960/minimal.js +5 -5
  107. package/examples/magnetometer/as5600/complete.js +31 -29
  108. package/examples/magnetometer/as5600/demo.js +35 -33
  109. package/examples/magnetometer/as5600/minimal.js +7 -7
  110. package/examples/magnetometer/hmc5883l/complete.js +47 -0
  111. package/examples/magnetometer/hmc5883l/demo.js +50 -0
  112. package/examples/magnetometer/hmc5883l/minimal.js +15 -0
  113. package/examples/memory/24aa02uid/complete.js +23 -21
  114. package/examples/memory/24aa02uid/demo.js +32 -30
  115. package/examples/memory/24aa02uid/minimal.js +5 -5
  116. package/examples/motor/drv8830/complete.js +50 -0
  117. package/examples/motor/drv8830/demo.js +57 -0
  118. package/examples/motor/drv8830/minimal.js +23 -0
  119. package/examples/other/mpr121/complete.js +50 -0
  120. package/examples/other/mpr121/demo.js +30 -0
  121. package/examples/other/mpr121/minimal.js +17 -0
  122. package/examples/power/ade7953/complete.js +44 -0
  123. package/examples/power/ade7953/demo.js +28 -0
  124. package/examples/power/ade7953/minimal.js +15 -0
  125. package/examples/power/ina219/complete.js +20 -18
  126. package/examples/power/ina219/demo.js +15 -13
  127. package/examples/power/ina219/minimal.js +8 -8
  128. package/examples/power/ina226/complete.js +24 -22
  129. package/examples/power/ina226/demo.js +22 -20
  130. package/examples/power/ina226/minimal.js +5 -5
  131. package/examples/power/ina3221/complete.js +51 -49
  132. package/examples/power/ina3221/demo.js +13 -13
  133. package/examples/power/ina3221/minimal.js +8 -8
  134. package/examples/pressure/bmp085/complete.js +29 -0
  135. package/examples/pressure/bmp085/demo.js +34 -0
  136. package/examples/pressure/bmp085/minimal.js +19 -0
  137. package/examples/pressure/bmp180/complete.js +20 -17
  138. package/examples/pressure/bmp180/demo.js +24 -22
  139. package/examples/pressure/bmp180/minimal.js +13 -11
  140. package/examples/pressure/bmp280/complete.js +23 -20
  141. package/examples/pressure/bmp280/demo.js +44 -41
  142. package/examples/pressure/bmp280/minimal.js +13 -11
  143. package/examples/pressure/bmp384/complete.js +29 -0
  144. package/examples/pressure/bmp384/demo.js +40 -0
  145. package/examples/pressure/bmp384/minimal.js +18 -0
  146. package/examples/pressure/bmp581/complete.js +34 -0
  147. package/examples/pressure/bmp581/demo.js +56 -0
  148. package/examples/pressure/bmp581/minimal.js +19 -0
  149. package/examples/pressure/lps22df/complete.js +50 -0
  150. package/examples/pressure/lps22df/demo.js +41 -0
  151. package/examples/pressure/lps22df/minimal.js +19 -0
  152. package/examples/pressure/lps28dfw/complete.js +37 -0
  153. package/examples/pressure/lps28dfw/demo.js +33 -0
  154. package/examples/pressure/lps28dfw/minimal.js +19 -0
  155. package/examples/pressure/lps33hw/complete.js +43 -0
  156. package/examples/pressure/lps33hw/demo.js +43 -0
  157. package/examples/pressure/lps33hw/minimal.js +19 -0
  158. package/examples/rfid/mfrc522/complete.js +44 -0
  159. package/examples/rfid/mfrc522/demo.js +70 -0
  160. package/examples/rfid/mfrc522/minimal.js +17 -0
  161. package/examples/rtc/ds3231/complete.js +77 -0
  162. package/examples/rtc/ds3231/demo.js +60 -0
  163. package/examples/rtc/ds3231/minimal.js +23 -0
  164. package/examples/rtc/pcf8523/complete.js +66 -0
  165. package/examples/rtc/pcf8523/demo.js +65 -0
  166. package/examples/rtc/pcf8523/minimal.js +20 -0
  167. package/examples/temperature/mcp9808/complete.js +89 -0
  168. package/examples/temperature/mcp9808/demo.js +62 -0
  169. package/examples/temperature/mcp9808/minimal.js +21 -0
  170. package/examples/temperature/tmp117/complete.js +90 -0
  171. package/examples/temperature/tmp117/demo.js +73 -0
  172. package/examples/temperature/tmp117/minimal.js +21 -0
  173. package/examples/tof/vl53l0x/complete.js +118 -0
  174. package/examples/tof/vl53l0x/demo.js +83 -0
  175. package/examples/tof/vl53l0x/minimal.js +25 -0
  176. package/examples/tof/vl53l1x/complete.js +151 -0
  177. package/examples/tof/vl53l1x/demo.js +100 -0
  178. package/examples/tof/vl53l1x/minimal.js +25 -0
  179. package/package.json +5 -1
  180. package/src/chips/accelerometer/adxl345.js +482 -0
  181. package/src/chips/accelerometer/adxl362.js +594 -0
  182. package/src/chips/adc_dac/ad7705.js +345 -0
  183. package/src/chips/adc_dac/ad7706.js +331 -0
  184. package/src/chips/adc_dac/hx710a.js +184 -0
  185. package/src/chips/adc_dac/hx710b.js +186 -0
  186. package/src/chips/adc_dac/hx711.js +15 -15
  187. package/src/chips/adc_dac/mcp4725.js +48 -40
  188. package/src/chips/adc_dac/mcp4728.js +57 -44
  189. package/src/chips/adc_dac/pcf8591.js +196 -0
  190. package/src/chips/comms/mcp2515.js +659 -0
  191. package/src/chips/comms/rda5807m.js +393 -0
  192. package/src/chips/comms/rfm9x.js +590 -0
  193. package/src/chips/display/pcf8576.js +42 -33
  194. package/src/chips/environmental/aht21.js +52 -37
  195. package/src/chips/environmental/bme280.js +85 -66
  196. package/src/chips/environmental/bme680.js +110 -87
  197. package/src/chips/gas/ens160.js +82 -64
  198. package/src/chips/gnss/neo6.js +438 -0
  199. package/src/chips/gyroscope/l3g4200d.js +366 -0
  200. package/src/chips/gyroscope/l3gd20h.js +314 -0
  201. package/src/chips/humidity/dht11.js +135 -0
  202. package/src/chips/imu/mpu6050.js +89 -66
  203. package/src/chips/imu/mpu9250.js +394 -0
  204. package/src/chips/imu/mpu9255.js +447 -0
  205. package/src/chips/io_expander/mcp23017.js +240 -157
  206. package/src/chips/io_expander/pcf8574.js +149 -124
  207. package/src/chips/io_expander/pcf8575.js +164 -135
  208. package/src/chips/io_expander/tpic6b595.js +242 -0
  209. package/src/chips/led/_NeoPixelRGBBase.js +204 -0
  210. package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
  211. package/src/chips/led/apa102.js +236 -0
  212. package/src/chips/led/sk6812rgbw.js +15 -149
  213. package/src/chips/led/ws2812b.js +14 -139
  214. package/src/chips/led/ws2814.js +49 -0
  215. package/src/chips/light/apds-9930.js +342 -0
  216. package/src/chips/light/apds9960.js +158 -123
  217. package/src/chips/magnetometer/as5600.js +102 -82
  218. package/src/chips/magnetometer/hmc5883l.js +222 -0
  219. package/src/chips/memory/_24aa02uid.js +32 -29
  220. package/src/chips/motor/drv8830.js +239 -0
  221. package/src/chips/other/mpr121.js +375 -0
  222. package/src/chips/power/ade7953.js +427 -0
  223. package/src/chips/power/ina219.js +45 -40
  224. package/src/chips/power/ina226.js +50 -45
  225. package/src/chips/power/ina3221.js +95 -76
  226. package/src/chips/pressure/bmp085.js +251 -0
  227. package/src/chips/pressure/bmp180.js +54 -37
  228. package/src/chips/pressure/bmp280.js +72 -53
  229. package/src/chips/pressure/bmp384.js +410 -0
  230. package/src/chips/pressure/bmp581.js +489 -0
  231. package/src/chips/pressure/lps22df.js +341 -0
  232. package/src/chips/pressure/lps28dfw.js +314 -0
  233. package/src/chips/pressure/lps33hw.js +255 -0
  234. package/src/chips/rfid/mfrc522.js +674 -0
  235. package/src/chips/rtc/ds3231.js +504 -0
  236. package/src/chips/rtc/pcf8523.js +539 -0
  237. package/src/chips/temperature/mcp9808.js +488 -0
  238. package/src/chips/temperature/tmp117.js +471 -0
  239. package/src/chips/tof/_vl53_base.js +160 -0
  240. package/src/chips/tof/vl53l0x.js +816 -0
  241. package/src/chips/tof/vl53l1x.js +816 -0
  242. package/src/connection/connection.js +111 -0
  243. package/src/connection/dhtxx.js +138 -0
  244. package/src/connection/dhtxx_mock.js +39 -0
  245. package/src/{transport → connection}/hx711.js +49 -22
  246. package/src/connection/hx711_mock.js +52 -0
  247. package/src/{transport → connection}/i2c.js +13 -9
  248. package/src/connection/i2c_mock.js +91 -0
  249. package/src/connection/input_pin.js +156 -0
  250. package/src/connection/neo6_mock.js +45 -0
  251. package/src/{transport → connection}/neopixel.js +23 -4
  252. package/src/connection/output_pin.js +41 -0
  253. package/src/connection/sipo.js +159 -0
  254. package/src/{transport → connection}/smbus.js +14 -10
  255. package/src/{transport → connection}/spi.js +12 -8
  256. package/src/connection/spi_mock.js +151 -0
  257. package/src/{transport → connection}/uart.js +33 -26
  258. package/examples/gnss/.gitkeep +0 -0
  259. package/examples/humidity/.gitkeep +0 -0
  260. package/examples/motor/.gitkeep +0 -0
  261. package/examples/temperature/.gitkeep +0 -0
  262. package/examples/tof/.gitkeep +0 -0
  263. package/src/chips/gnss/.gitkeep +0 -0
  264. package/src/chips/motor/.gitkeep +0 -0
  265. package/src/chips/temperature/.gitkeep +0 -0
  266. package/src/chips/tof/.gitkeep +0 -0
@@ -0,0 +1,471 @@
1
+ 'use strict';
2
+
3
+ const _REG_TEMP_RESULT = 0x00;
4
+ const _REG_CONFIG = 0x01;
5
+ const _REG_THIGH = 0x02;
6
+ const _REG_TLOW = 0x03;
7
+ const _REG_EEPROM_UL = 0x04;
8
+ const _REG_TEMP_OFFSET = 0x07;
9
+ const _REG_DEVICE_ID = 0x0F;
10
+
11
+ const _DEVICE_ID = 0x117;
12
+
13
+ // CONFIGURATION (0x01) bits.
14
+ const _CFG_HIGH_ALERT = 0x8000;
15
+ const _CFG_LOW_ALERT = 0x4000;
16
+ const _CFG_DATA_READY = 0x2000;
17
+ const _CFG_MOD_SHIFT = 10;
18
+ const _CFG_MOD_MASK = 0x0C00;
19
+ const _CFG_CONV_SHIFT = 7;
20
+ const _CFG_CONV_MASK = 0x0380;
21
+ const _CFG_AVG_SHIFT = 5;
22
+ const _CFG_AVG_MASK = 0x0060;
23
+ const _CFG_TNA = 0x0010;
24
+ const _CFG_POL = 0x0008;
25
+ const _CFG_DR_ALERT = 0x0004;
26
+ const _CFG_SOFT_RESET = 0x0002;
27
+ // Writable bits: MOD/CONV/AVG/T-nA/POL/DR-Alert. Soft_Reset is set only on purpose.
28
+ const _CFG_WRITE_MASK = 0x0FFC;
29
+
30
+ const _MOD_ONE_SHOT = 0x03;
31
+
32
+ // EEPROM_UL (0x04) bits.
33
+ const _EUN = 0x8000;
34
+ const _EEPROM_BUSY = 0x4000;
35
+
36
+ // Conversion cycle times in s, indexed by CONV[2:0] (no-averaging column).
37
+ const _CYCLES = [0.0155, 0.125, 0.25, 0.5, 1.0, 4.0, 8.0, 16.0];
38
+ // Averaging counts, indexed by AVG[1:0].
39
+ const _AVERAGINGS = [0, 8, 32, 64];
40
+ // Conversion modes, indexed by MOD[1:0] (0b10 reads back as continuous).
41
+ const _MODES = ['continuous', 'shutdown', 'continuous', 'one_shot'];
42
+ // EEPROM scratch slot -> register address.
43
+ const _SCRATCH_REGS = { 1: 0x05, 2: 0x06, 3: 0x08 };
44
+
45
+ const _LSB_C = 0.0078125;
46
+
47
+ function _decodeTemperature(raw16) {
48
+ const raw = raw16 & 0x8000 ? raw16 - 0x10000 : raw16;
49
+ return raw * _LSB_C;
50
+ }
51
+
52
+ function _encodeTemperature(celsius) {
53
+ // Round half away from zero, then clamp to the 16-bit two's-complement range.
54
+ const steps = celsius / _LSB_C;
55
+ let value = steps >= 0 ? Math.floor(steps + 0.5) : -Math.floor(-steps + 0.5);
56
+ if (value < -32768) value = -32768;
57
+ if (value > 32767) value = 32767;
58
+ return value & 0xFFFF;
59
+ }
60
+
61
+ /**
62
+ * TMP117 ±0.1°C high-accuracy, low-power digital temperature sensor (Texas
63
+ * Instruments) — minimal interface.
64
+ *
65
+ * NIST-traceable 16-bit temperature sensor (0.0078125 °C per LSB) read over
66
+ * an I²C/SMBus-compatible bus. Registers are 16-bit, big-endian, addressed
67
+ * through a non-incrementing Register Pointer. Four selectable addresses
68
+ * (0x48–0x4B) via the 4-level ADD0 strap.
69
+ *
70
+ * No register writes are made at construction: the POR/EEPROM default
71
+ * (continuous conversion, 8-conversion averaging, 1 s cycle, Alert mode)
72
+ * already serves the primary use case.
73
+ *
74
+ * Identity check: JS constructors cannot be async, so the DEVICE_ID (0x117)
75
+ * check starts in the constructor and every public method awaits it first.
76
+ * A wrong or absent device therefore rejects the first call made on the
77
+ * driver — `await sensor.init()` right after construction to surface it at a
78
+ * predictable point.
79
+ */
80
+ class TMP117Minimal {
81
+ /**
82
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection (0x48–0x4B).
83
+ */
84
+ constructor(connection) {
85
+ this._conn = connection;
86
+ this._ready = this._checkIdentity();
87
+ // The rejection is re-raised by every public method; mark it handled
88
+ // here so it never surfaces as an unhandled rejection on its own.
89
+ this._ready.catch(() => {});
90
+ }
91
+
92
+ /**
93
+ * Wait for the identity check started by the constructor. Makes no
94
+ * register writes. Optional — every other method awaits the same check —
95
+ * but calling it right after construction surfaces a wrong or absent
96
+ * device at a predictable point.
97
+ * @returns {Promise<void>}
98
+ * @throws {Error} If DEVICE_ID bits 11:0 are not 0x117.
99
+ */
100
+ async init() {
101
+ await this._ready;
102
+ }
103
+
104
+ async _checkIdentity() {
105
+ const did = (await this._readReg(_REG_DEVICE_ID)) & 0x0FFF;
106
+ if (did !== _DEVICE_ID) {
107
+ throw new Error('TMP117 not found: expected DEVICE_ID 0x117, got 0x' +
108
+ did.toString(16).padStart(3, '0'));
109
+ }
110
+ }
111
+
112
+ async _readReg(reg) {
113
+ const buf = await this._conn.writeRead(Buffer.from([reg & 0xFF]), 2);
114
+ return buf.readUInt16BE(0);
115
+ }
116
+
117
+ async _writeReg(reg, value) {
118
+ await this._conn.write(Buffer.from([reg & 0xFF, (value >> 8) & 0xFF, value & 0xFF]));
119
+ }
120
+
121
+ /**
122
+ * Read the temperature. Decodes TEMP_RESULT's 16-bit two's-complement
123
+ * value (0.0078125 °C per LSB). Returns -256 until the first conversion
124
+ * after power-up completes.
125
+ * @returns {Promise<number>} Temperature in °C.
126
+ */
127
+ async readTemperature() {
128
+ await this._ready;
129
+ return _decodeTemperature(await this._readReg(_REG_TEMP_RESULT));
130
+ }
131
+ }
132
+
133
+ /**
134
+ * TMP117 full interface — extends Minimal with conversion mode, averaging
135
+ * and cycle-time control, one-shot triggering, both temperature limits, the
136
+ * calibration offset, soft reset, EEPROM persistence and scratch storage,
137
+ * and the Level-2 Alert/interrupt API.
138
+ */
139
+ class TMP117Full extends TMP117Minimal {
140
+ /**
141
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection (0x48–0x4B).
142
+ */
143
+ constructor(connection) {
144
+ super(connection);
145
+ this._callback = null;
146
+ this._edgeHandler = null;
147
+ this._pollTimer = null;
148
+ }
149
+
150
+ async _readConfig() {
151
+ return (await this._readReg(_REG_CONFIG)) & _CFG_WRITE_MASK;
152
+ }
153
+
154
+ async _writeConfig(value) {
155
+ await this._writeReg(_REG_CONFIG, value & _CFG_WRITE_MASK);
156
+ }
157
+
158
+ // -- Conversion ---------------------------------------------------------
159
+
160
+ /**
161
+ * Set conversion mode, averaging and cycle time. The cycle time is
162
+ * matched to the nearest CONV[2:0] step from the no-averaging column
163
+ * (15.5 ms, 125 ms, 250 ms, 500 ms, 1 s, 4 s, 8 s, 16 s); at higher
164
+ * averaging the hardware lengthens short cycles automatically. The Alert
165
+ * configuration bits are preserved.
166
+ * @param {'continuous'|'shutdown'|'one_shot'} [mode='continuous'] - Conversion mode.
167
+ * @param {0|8|32|64} [averaging=8] - Conversions averaged per result.
168
+ * @param {number} [cycleSeconds=1.0] - Desired conversion cycle time in s.
169
+ * @returns {Promise<void>}
170
+ * @throws {RangeError} If mode or averaging is not one of the listed values.
171
+ */
172
+ async configure(mode = 'continuous', averaging = 8, cycleSeconds = 1.0) {
173
+ let mod;
174
+ if (mode === 'continuous') mod = 0x00;
175
+ else if (mode === 'shutdown') mod = 0x01;
176
+ else if (mode === 'one_shot') mod = _MOD_ONE_SHOT;
177
+ else throw new RangeError("mode must be 'continuous', 'shutdown' or 'one_shot'");
178
+ const avg = _AVERAGINGS.indexOf(averaging);
179
+ if (avg < 0) throw new RangeError('averaging must be one of 0, 8, 32, 64');
180
+ let conv = 0;
181
+ for (let code = 1; code < _CYCLES.length; code++) {
182
+ if (Math.abs(_CYCLES[code] - cycleSeconds) < Math.abs(_CYCLES[conv] - cycleSeconds)) conv = code;
183
+ }
184
+ await this._ready;
185
+ let config = (await this._readConfig()) & ~(_CFG_MOD_MASK | _CFG_CONV_MASK | _CFG_AVG_MASK);
186
+ config |= (mod << _CFG_MOD_SHIFT) | (conv << _CFG_CONV_SHIFT) | (avg << _CFG_AVG_SHIFT);
187
+ await this._writeConfig(config);
188
+ }
189
+
190
+ /**
191
+ * Read the conversion mode, averaging and cycle time.
192
+ * @returns {Promise<{mode: string, averaging: number, cycleSeconds: number}>}
193
+ * cycleSeconds is the no-averaging CONV[2:0] step, in s.
194
+ */
195
+ async getConfig() {
196
+ await this._ready;
197
+ const config = await this._readReg(_REG_CONFIG);
198
+ return {
199
+ mode: _MODES[(config & _CFG_MOD_MASK) >> _CFG_MOD_SHIFT],
200
+ averaging: _AVERAGINGS[(config & _CFG_AVG_MASK) >> _CFG_AVG_SHIFT],
201
+ cycleSeconds: _CYCLES[(config & _CFG_CONV_MASK) >> _CFG_CONV_SHIFT],
202
+ };
203
+ }
204
+
205
+ /**
206
+ * Report whether the sensor is in Shutdown mode.
207
+ * @returns {Promise<boolean>} True if MOD[1:0] is Shutdown.
208
+ */
209
+ async isShutdown() {
210
+ await this._ready;
211
+ return (((await this._readReg(_REG_CONFIG)) & _CFG_MOD_MASK) >> _CFG_MOD_SHIFT) === 0x01;
212
+ }
213
+
214
+ /**
215
+ * Start a single conversion (MOD[1:0] = One-Shot). The sensor returns to
216
+ * Shutdown once the conversion (including averaging) completes.
217
+ * @returns {Promise<void>}
218
+ */
219
+ async triggerOneShot() {
220
+ await this._ready;
221
+ const config = (await this._readConfig()) & ~_CFG_MOD_MASK;
222
+ await this._writeConfig(config | (_MOD_ONE_SHOT << _CFG_MOD_SHIFT));
223
+ }
224
+
225
+ /**
226
+ * Report whether a fresh conversion result is available. Reading this
227
+ * flag clears it (as does reading TEMP_RESULT).
228
+ * @returns {Promise<boolean>} True if Data_Ready is set.
229
+ */
230
+ async isDataReady() {
231
+ await this._ready;
232
+ return ((await this._readReg(_REG_CONFIG)) & _CFG_DATA_READY) !== 0;
233
+ }
234
+
235
+ // -- Limits and offset --------------------------------------------------
236
+
237
+ /**
238
+ * Read THIGH_LIMIT.
239
+ * @returns {Promise<number>} High limit in °C.
240
+ */
241
+ async getHighLimit() {
242
+ await this._ready;
243
+ return _decodeTemperature(await this._readReg(_REG_THIGH));
244
+ }
245
+
246
+ /**
247
+ * Write THIGH_LIMIT, rounded to the nearest 0.0078125 °C.
248
+ * @param {number} celsius - High limit in °C (-256 to 255.9921875, clamped).
249
+ * @returns {Promise<void>}
250
+ */
251
+ async setHighLimit(celsius) {
252
+ await this._ready;
253
+ await this._writeReg(_REG_THIGH, _encodeTemperature(celsius));
254
+ }
255
+
256
+ /**
257
+ * Read TLOW_LIMIT.
258
+ * @returns {Promise<number>} Low limit in °C.
259
+ */
260
+ async getLowLimit() {
261
+ await this._ready;
262
+ return _decodeTemperature(await this._readReg(_REG_TLOW));
263
+ }
264
+
265
+ /**
266
+ * Write TLOW_LIMIT, rounded to the nearest 0.0078125 °C. In Therm mode
267
+ * this is HIGH_Alert's reset threshold (hysteresis).
268
+ * @param {number} celsius - Low limit in °C (-256 to 255.9921875, clamped).
269
+ * @returns {Promise<void>}
270
+ */
271
+ async setLowLimit(celsius) {
272
+ await this._ready;
273
+ await this._writeReg(_REG_TLOW, _encodeTemperature(celsius));
274
+ }
275
+
276
+ /**
277
+ * Read TEMP_OFFSET.
278
+ * @returns {Promise<number>} Calibration offset in °C.
279
+ */
280
+ async getTemperatureOffset() {
281
+ await this._ready;
282
+ return _decodeTemperature(await this._readReg(_REG_TEMP_OFFSET));
283
+ }
284
+
285
+ /**
286
+ * Write TEMP_OFFSET, added to every result after linearization.
287
+ * @param {number} celsius - Calibration offset in °C (-256 to 255.9921875, clamped).
288
+ * @returns {Promise<void>}
289
+ */
290
+ async setTemperatureOffset(celsius) {
291
+ await this._ready;
292
+ await this._writeReg(_REG_TEMP_OFFSET, _encodeTemperature(celsius));
293
+ }
294
+
295
+ // -- Reset --------------------------------------------------------------
296
+
297
+ /**
298
+ * Software reset (Soft_Reset = 1), then wait the 2 ms reset time. Reloads
299
+ * CONFIGURATION, THIGH_LIMIT, TLOW_LIMIT and TEMP_OFFSET from EEPROM.
300
+ * @returns {Promise<void>}
301
+ */
302
+ async reset() {
303
+ await this._ready;
304
+ await this._writeReg(_REG_CONFIG, _CFG_SOFT_RESET);
305
+ await new Promise((resolve) => setTimeout(resolve, 2));
306
+ }
307
+
308
+ // -- EEPROM -------------------------------------------------------------
309
+
310
+ /**
311
+ * Unlock the EEPROM (EUN = 1). While unlocked, writes to CONFIGURATION,
312
+ * THIGH_LIMIT, TLOW_LIMIT, TEMP_OFFSET and EEPROM2 also program the
313
+ * EEPROM as the new power-on default. Poll isEepromBusy() after each such
314
+ * write.
315
+ * @returns {Promise<void>}
316
+ */
317
+ async unlockEeprom() {
318
+ await this._ready;
319
+ await this._writeReg(_REG_EEPROM_UL, _EUN);
320
+ }
321
+
322
+ /**
323
+ * Lock the EEPROM (EUN = 0); register writes become volatile only.
324
+ * @returns {Promise<void>}
325
+ */
326
+ async lockEeprom() {
327
+ await this._ready;
328
+ await this._writeReg(_REG_EEPROM_UL, 0x0000);
329
+ }
330
+
331
+ /**
332
+ * Report whether an EEPROM programming operation is in progress.
333
+ * @returns {Promise<boolean>} True if EEPROM_Busy is set.
334
+ */
335
+ async isEepromBusy() {
336
+ await this._ready;
337
+ return ((await this._readReg(_REG_EEPROM_UL)) & _EEPROM_BUSY) !== 0;
338
+ }
339
+
340
+ /**
341
+ * Read a general-purpose EEPROM scratch register.
342
+ * @param {1|2|3} slot - EEPROM1, EEPROM2 or EEPROM3; slots 1 and 3 hold
343
+ * factory NIST-traceability data.
344
+ * @returns {Promise<number>} 16-bit register value.
345
+ * @throws {RangeError} If slot is not 1, 2 or 3.
346
+ */
347
+ async readEepromScratch(slot) {
348
+ if (!(slot in _SCRATCH_REGS)) throw new RangeError('slot must be 1, 2 or 3');
349
+ await this._ready;
350
+ return this._readReg(_SCRATCH_REGS[slot]);
351
+ }
352
+
353
+ /**
354
+ * Write the general-purpose EEPROM2 scratch register. Only slot 2 is
355
+ * writable — EEPROM1/EEPROM3 hold factory NIST-traceability data.
356
+ * Persists across power cycles only while the EEPROM is unlocked.
357
+ * @param {2} slot - Must be 2.
358
+ * @param {number} value - 16-bit value.
359
+ * @returns {Promise<void>}
360
+ * @throws {RangeError} If slot is not 2.
361
+ */
362
+ async writeEepromScratch(slot, value) {
363
+ if (slot !== 2) throw new RangeError('only EEPROM scratch slot 2 is writable');
364
+ await this._ready;
365
+ await this._writeReg(_SCRATCH_REGS[2], value & 0xFFFF);
366
+ }
367
+
368
+ // -- Alert output -------------------------------------------------------
369
+
370
+ /**
371
+ * Configure the ALERT output's mode, polarity and pin function together.
372
+ * @param {'alert'|'therm'} [mode='alert'] - Window alert or latching Therm
373
+ * (TLOW_LIMIT becomes the reset threshold).
374
+ * @param {'active_low'|'active_high'} [polarity='active_low'] - Output polarity.
375
+ * @param {'alert'|'data_ready'} [pinFunction='alert'] - Alert/Therm status or Data-Ready.
376
+ * @returns {Promise<void>}
377
+ * @throws {RangeError} If an argument is not one of the listed values.
378
+ */
379
+ async configureAlert(mode = 'alert', polarity = 'active_low', pinFunction = 'alert') {
380
+ if (mode !== 'alert' && mode !== 'therm') throw new RangeError("mode must be 'alert' or 'therm'");
381
+ if (polarity !== 'active_low' && polarity !== 'active_high') throw new RangeError("polarity must be 'active_low' or 'active_high'");
382
+ if (pinFunction !== 'alert' && pinFunction !== 'data_ready') throw new RangeError("pinFunction must be 'alert' or 'data_ready'");
383
+ await this._ready;
384
+ let config = (await this._readConfig()) & ~(_CFG_TNA | _CFG_POL | _CFG_DR_ALERT);
385
+ if (mode === 'therm') config |= _CFG_TNA;
386
+ if (polarity === 'active_high') config |= _CFG_POL;
387
+ if (pinFunction === 'data_ready') config |= _CFG_DR_ALERT;
388
+ await this._writeConfig(config);
389
+ }
390
+
391
+ // -- Interrupt API (Level 2) --------------------------------------------
392
+
393
+ /**
394
+ * Read CONFIGURATION's HIGH_Alert / LOW_Alert flags. In Alert mode this
395
+ * read also clears both flags (a hardware side effect). In Therm mode
396
+ * HIGH_Alert clears only once the temperature drops below TLOW_LIMIT.
397
+ * @returns {Promise<number>} Mask of SOURCE_HIGH / SOURCE_LOW.
398
+ */
399
+ async pollInterrupt() {
400
+ await this._ready;
401
+ const config = await this._readReg(_REG_CONFIG);
402
+ let status = 0;
403
+ if (config & _CFG_HIGH_ALERT) status |= TMP117Full.SOURCE_HIGH;
404
+ if (config & _CFG_LOW_ALERT) status |= TMP117Full.SOURCE_LOW;
405
+ return status;
406
+ }
407
+
408
+ /**
409
+ * Subscribe to ALERT events. Uses `connection.intPin.onEdge()` when an
410
+ * InputPin is wired — the edge follows the configured POL (falling for
411
+ * active-low, rising for active-high) and the callback runs on every
412
+ * edge. Otherwise falls back to a 5 ms polling loop that calls back
413
+ * whenever the status mask changes.
414
+ * @param {function(number): void} callback - Receives the pollInterrupt() mask.
415
+ * @returns {Promise<void>}
416
+ */
417
+ async onInterrupt(callback) {
418
+ await this._ready;
419
+ this._callback = callback;
420
+ if (this._conn.intPin) {
421
+ const activeHigh = ((await this._readReg(_REG_CONFIG)) & _CFG_POL) !== 0;
422
+ this._edgeHandler = async () => {
423
+ const status = await this.pollInterrupt();
424
+ if (this._callback) this._callback(status);
425
+ };
426
+ await this._conn.intPin.onEdge(this._edgeHandler, activeHigh ? 'rising' : 'falling');
427
+ } else {
428
+ let last = await this.pollInterrupt();
429
+ let busy = false;
430
+ this._pollTimer = setInterval(async () => {
431
+ if (busy) return;
432
+ busy = true;
433
+ try {
434
+ const status = await this.pollInterrupt();
435
+ if (status !== last) {
436
+ last = status;
437
+ if (this._callback) this._callback(status);
438
+ }
439
+ } finally {
440
+ busy = false;
441
+ }
442
+ }, 5);
443
+ }
444
+ }
445
+
446
+ /**
447
+ * Unsubscribe and stop delivery.
448
+ * @returns {Promise<void>}
449
+ */
450
+ async offInterrupt() {
451
+ if (this._conn.intPin && this._edgeHandler) {
452
+ await this._conn.intPin.offEdge(this._edgeHandler);
453
+ this._edgeHandler = null;
454
+ }
455
+ if (this._pollTimer) {
456
+ clearInterval(this._pollTimer);
457
+ this._pollTimer = null;
458
+ }
459
+ this._callback = null;
460
+ }
461
+ }
462
+
463
+ /** Default 7-bit I²C address (ADD0 = GND). Valid range 0x48–0x4B. */
464
+ TMP117Minimal.I2C_ADDRESS = 0x48;
465
+
466
+ /** Result > THIGH_LIMIT (HIGH_Alert). */
467
+ TMP117Full.SOURCE_HIGH = 0x01;
468
+ /** Result < TLOW_LIMIT (LOW_Alert; Alert mode only). */
469
+ TMP117Full.SOURCE_LOW = 0x02;
470
+
471
+ module.exports = { TMP117Minimal, TMP117Full };
@@ -0,0 +1,160 @@
1
+ 'use strict';
2
+
3
+ const _TIMEOUT_MS = 500;
4
+ // tBOOT ≤ 1.2 ms, rounded up to the timer resolution.
5
+ const _BOOT_MS = 2;
6
+
7
+ const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
8
+
9
+ /**
10
+ * Shared base for ST's VL53 FlightSense Time-of-Flight ranging family.
11
+ *
12
+ * Internal — not exported from the package index and never instantiated
13
+ * directly. VL53L0X and VL53L1X have different register maps (8-bit vs
14
+ * 16-bit register index), so this base holds no register addresses and no
15
+ * ranging logic, only the plumbing both chips share: big-endian register
16
+ * access with a 1- or 2-byte index, the bounded poll helper, the XSHUT boot
17
+ * wait, the promise queue that serializes multi-register sequences against
18
+ * the interrupt poller, interrupt delivery (intPin edge or 5 ms polling
19
+ * timer), the volatile re-addressing helper, and the family's shared
20
+ * constants. See specs/tof/_vl53_base.md.
21
+ *
22
+ * Subclasses implement `_pollInterruptStatus()`.
23
+ */
24
+ class VL53Base {
25
+ /**
26
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
27
+ * @param {number} indexBytes - Register index width on the wire, 1 or 2.
28
+ * @param {string} chipName - Chip name used in error messages.
29
+ */
30
+ constructor(connection, indexBytes, chipName) {
31
+ this._conn = connection;
32
+ this._indexBytes = indexBytes;
33
+ this._chipName = chipName;
34
+ this._queue = Promise.resolve();
35
+ this._callback = null;
36
+ this._edgeHandler = null;
37
+ this._pollTimer = null;
38
+ }
39
+
40
+ _locked(fn) {
41
+ const run = this._queue.then(fn);
42
+ this._queue = run.catch(() => {});
43
+ return run;
44
+ }
45
+
46
+ _index(reg) {
47
+ return this._indexBytes === 2 ? [(reg >> 8) & 0xFF, reg & 0xFF] : [reg & 0xFF];
48
+ }
49
+
50
+ async _wrBlock(reg, data) {
51
+ await this._conn.write(Buffer.from([...this._index(reg), ...data]));
52
+ }
53
+
54
+ async _rdBlock(reg, n) {
55
+ return Buffer.from(await this._conn.writeRead(Buffer.from(this._index(reg)), n));
56
+ }
57
+
58
+ async _wr8(reg, value) {
59
+ await this._wrBlock(reg, [value & 0xFF]);
60
+ }
61
+
62
+ async _rd8(reg) {
63
+ return (await this._rdBlock(reg, 1))[0];
64
+ }
65
+
66
+ async _wr16(reg, value) {
67
+ await this._wrBlock(reg, [(value >> 8) & 0xFF, value & 0xFF]);
68
+ }
69
+
70
+ async _rd16(reg) {
71
+ return (await this._rdBlock(reg, 2)).readUInt16BE(0);
72
+ }
73
+
74
+ async _wr32(reg, value) {
75
+ const buf = Buffer.alloc(4);
76
+ buf.writeUInt32BE(value >>> 0, 0);
77
+ await this._wrBlock(reg, buf);
78
+ }
79
+
80
+ async _rd32(reg) {
81
+ return (await this._rdBlock(reg, 4)).readUInt32BE(0);
82
+ }
83
+
84
+ async _waitUntil(predicate, what) {
85
+ const start = Date.now();
86
+ for (;;) {
87
+ if (await predicate()) return;
88
+ if (Date.now() - start > _TIMEOUT_MS) throw new Error(`${this._chipName} timeout waiting for ${what}`);
89
+ }
90
+ }
91
+
92
+ async _bootWait() {
93
+ if (this._conn.enPin) await this._conn.enable();
94
+ await _sleep(_BOOT_MS);
95
+ }
96
+
97
+ async _setAddressReg(reg, address) {
98
+ if (!(address >= 0x08 && address <= 0x77)) throw new RangeError('address must be 0x08 to 0x77');
99
+ await this._wr8(reg, address & 0x7F);
100
+ }
101
+
102
+ /**
103
+ * Read and clear a pending interrupt (under the lock).
104
+ * @returns {Promise<number>} The SOURCE_* value, or 0.
105
+ * @abstract
106
+ */
107
+ async _pollInterruptStatus() {
108
+ throw new Error('not implemented');
109
+ }
110
+
111
+ async _subscribe(callback) {
112
+ this._callback = callback;
113
+ if (this._conn.intPin) {
114
+ this._edgeHandler = async () => {
115
+ const status = await this._pollInterruptStatus();
116
+ if (status && this._callback) this._callback(status);
117
+ };
118
+ await this._conn.intPin.onEdge(this._edgeHandler, 'falling');
119
+ } else {
120
+ let busy = false;
121
+ this._pollTimer = setInterval(async () => {
122
+ if (busy) return;
123
+ busy = true;
124
+ try {
125
+ const status = await this._pollInterruptStatus();
126
+ if (status && this._callback) this._callback(status);
127
+ } finally {
128
+ busy = false;
129
+ }
130
+ }, 5);
131
+ }
132
+ }
133
+
134
+ async _unsubscribe() {
135
+ if (this._conn.intPin && this._edgeHandler) {
136
+ await this._conn.intPin.offEdge(this._edgeHandler);
137
+ this._edgeHandler = null;
138
+ }
139
+ if (this._pollTimer) {
140
+ clearInterval(this._pollTimer);
141
+ this._pollTimer = null;
142
+ }
143
+ this._callback = null;
144
+ }
145
+ }
146
+
147
+ /** Default 7-bit I²C address of every family member. */
148
+ VL53Base.I2C_ADDRESS = 0x29;
149
+ /** Range < low threshold. */
150
+ VL53Base.SOURCE_LEVEL_LOW = 1;
151
+ /** Range > high threshold. */
152
+ VL53Base.SOURCE_LEVEL_HIGH = 2;
153
+ /** Range < low threshold or > high threshold. */
154
+ VL53Base.SOURCE_OUT_OF_WINDOW = 3;
155
+ /** A new measurement is available (driver default). */
156
+ VL53Base.SOURCE_NEW_SAMPLE_READY = 4;
157
+ /** low ≤ range ≤ high (VL53L1X only). */
158
+ VL53Base.SOURCE_IN_WINDOW = 5;
159
+
160
+ module.exports = { VL53Base, _sleep };