periph 1.0.1 → 1.2.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 (241) hide show
  1. package/README.md +54 -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 +42 -0
  21. package/examples/adc_dac/hx711/demo.js +35 -0
  22. package/examples/adc_dac/hx711/minimal.js +16 -0
  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 +43 -0
  27. package/examples/adc_dac/mcp4728/demo.js +41 -0
  28. package/examples/adc_dac/mcp4728/minimal.js +13 -0
  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 +35 -0
  42. package/examples/display/pcf8576/demo.js +48 -0
  43. package/examples/display/pcf8576/minimal.js +20 -0
  44. package/examples/environmental/aht21/complete.js +32 -0
  45. package/examples/environmental/aht21/demo.js +46 -0
  46. package/examples/environmental/aht21/minimal.js +12 -0
  47. package/examples/environmental/bme280/complete.js +37 -0
  48. package/examples/environmental/bme280/demo.js +67 -0
  49. package/examples/environmental/bme280/minimal.js +20 -0
  50. package/examples/environmental/bme680/complete.js +40 -0
  51. package/examples/environmental/bme680/demo.js +54 -0
  52. package/examples/environmental/bme680/minimal.js +21 -0
  53. package/examples/gas/ens160/complete.js +54 -0
  54. package/examples/gas/ens160/demo.js +54 -0
  55. package/examples/gas/ens160/minimal.js +29 -0
  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 +58 -0
  69. package/examples/imu/mpu6050/demo.js +37 -0
  70. package/examples/imu/mpu6050/minimal.js +14 -0
  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 +64 -0
  105. package/examples/light/apds9960/demo.js +34 -0
  106. package/examples/light/apds9960/minimal.js +12 -0
  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 +42 -0
  114. package/examples/memory/24aa02uid/demo.js +42 -0
  115. package/examples/memory/24aa02uid/minimal.js +12 -0
  116. package/examples/other/mpr121/complete.js +50 -0
  117. package/examples/other/mpr121/demo.js +30 -0
  118. package/examples/other/mpr121/minimal.js +17 -0
  119. package/examples/power/ade7953/complete.js +44 -0
  120. package/examples/power/ade7953/demo.js +28 -0
  121. package/examples/power/ade7953/minimal.js +15 -0
  122. package/examples/power/ina219/complete.js +20 -18
  123. package/examples/power/ina219/demo.js +15 -13
  124. package/examples/power/ina219/minimal.js +8 -8
  125. package/examples/power/ina226/complete.js +24 -22
  126. package/examples/power/ina226/demo.js +22 -20
  127. package/examples/power/ina226/minimal.js +5 -5
  128. package/examples/power/ina3221/complete.js +51 -49
  129. package/examples/power/ina3221/demo.js +13 -13
  130. package/examples/power/ina3221/minimal.js +8 -8
  131. package/examples/pressure/bmp085/complete.js +29 -0
  132. package/examples/pressure/bmp085/demo.js +34 -0
  133. package/examples/pressure/bmp085/minimal.js +19 -0
  134. package/examples/pressure/bmp180/complete.js +20 -17
  135. package/examples/pressure/bmp180/demo.js +24 -22
  136. package/examples/pressure/bmp180/minimal.js +13 -11
  137. package/examples/pressure/bmp280/complete.js +29 -22
  138. package/examples/pressure/bmp280/demo.js +42 -23
  139. package/examples/pressure/bmp280/minimal.js +13 -11
  140. package/examples/pressure/bmp384/complete.js +29 -0
  141. package/examples/pressure/bmp384/demo.js +40 -0
  142. package/examples/pressure/bmp384/minimal.js +18 -0
  143. package/examples/pressure/bmp581/complete.js +34 -0
  144. package/examples/pressure/bmp581/demo.js +56 -0
  145. package/examples/pressure/bmp581/minimal.js +19 -0
  146. package/examples/pressure/lps22df/complete.js +50 -0
  147. package/examples/pressure/lps22df/demo.js +41 -0
  148. package/examples/pressure/lps22df/minimal.js +19 -0
  149. package/examples/pressure/lps28dfw/complete.js +37 -0
  150. package/examples/pressure/lps28dfw/demo.js +33 -0
  151. package/examples/pressure/lps28dfw/minimal.js +19 -0
  152. package/examples/pressure/lps33hw/complete.js +43 -0
  153. package/examples/pressure/lps33hw/demo.js +43 -0
  154. package/examples/pressure/lps33hw/minimal.js +19 -0
  155. package/examples/rfid/mfrc522/complete.js +44 -0
  156. package/examples/rfid/mfrc522/demo.js +70 -0
  157. package/examples/rfid/mfrc522/minimal.js +17 -0
  158. package/package.json +5 -1
  159. package/src/chips/accelerometer/adxl345.js +482 -0
  160. package/src/chips/accelerometer/adxl362.js +594 -0
  161. package/src/chips/adc_dac/ad7705.js +345 -0
  162. package/src/chips/adc_dac/ad7706.js +331 -0
  163. package/src/chips/adc_dac/hx710a.js +184 -0
  164. package/src/chips/adc_dac/hx710b.js +186 -0
  165. package/src/chips/adc_dac/hx711.js +165 -0
  166. package/src/chips/adc_dac/mcp4725.js +48 -40
  167. package/src/chips/adc_dac/mcp4728.js +297 -0
  168. package/src/chips/adc_dac/pcf8591.js +196 -0
  169. package/src/chips/comms/mcp2515.js +659 -0
  170. package/src/chips/comms/rda5807m.js +393 -0
  171. package/src/chips/comms/rfm9x.js +590 -0
  172. package/src/chips/display/pcf8576.js +232 -0
  173. package/src/chips/environmental/aht21.js +185 -0
  174. package/src/chips/environmental/bme280.js +385 -0
  175. package/src/chips/environmental/bme680.js +512 -0
  176. package/src/chips/gas/ens160.js +298 -0
  177. package/src/chips/gnss/neo6.js +438 -0
  178. package/src/chips/gyroscope/l3g4200d.js +366 -0
  179. package/src/chips/gyroscope/l3gd20h.js +314 -0
  180. package/src/chips/humidity/dht11.js +135 -0
  181. package/src/chips/imu/mpu6050.js +289 -0
  182. package/src/chips/imu/mpu9250.js +394 -0
  183. package/src/chips/imu/mpu9255.js +447 -0
  184. package/src/chips/io_expander/mcp23017.js +240 -157
  185. package/src/chips/io_expander/pcf8574.js +149 -124
  186. package/src/chips/io_expander/pcf8575.js +164 -135
  187. package/src/chips/io_expander/tpic6b595.js +242 -0
  188. package/src/chips/led/_NeoPixelRGBBase.js +204 -0
  189. package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
  190. package/src/chips/led/apa102.js +236 -0
  191. package/src/chips/led/sk6812rgbw.js +15 -149
  192. package/src/chips/led/ws2812b.js +14 -139
  193. package/src/chips/led/ws2814.js +49 -0
  194. package/src/chips/light/apds-9930.js +342 -0
  195. package/src/chips/light/apds9960.js +499 -0
  196. package/src/chips/magnetometer/as5600.js +102 -82
  197. package/src/chips/magnetometer/hmc5883l.js +222 -0
  198. package/src/chips/memory/_24aa02uid.js +182 -0
  199. package/src/chips/other/mpr121.js +375 -0
  200. package/src/chips/power/ade7953.js +427 -0
  201. package/src/chips/power/ina219.js +45 -40
  202. package/src/chips/power/ina226.js +50 -45
  203. package/src/chips/power/ina3221.js +95 -76
  204. package/src/chips/pressure/bmp085.js +251 -0
  205. package/src/chips/pressure/bmp180.js +54 -37
  206. package/src/chips/pressure/bmp280.js +188 -206
  207. package/src/chips/pressure/bmp384.js +410 -0
  208. package/src/chips/pressure/bmp581.js +489 -0
  209. package/src/chips/pressure/lps22df.js +341 -0
  210. package/src/chips/pressure/lps28dfw.js +314 -0
  211. package/src/chips/pressure/lps33hw.js +255 -0
  212. package/src/chips/rfid/mfrc522.js +674 -0
  213. package/src/connection/connection.js +111 -0
  214. package/src/connection/dhtxx.js +138 -0
  215. package/src/connection/dhtxx_mock.js +39 -0
  216. package/src/connection/hx711.js +132 -0
  217. package/src/connection/hx711_mock.js +52 -0
  218. package/src/{transport → connection}/i2c.js +13 -9
  219. package/src/connection/i2c_mock.js +91 -0
  220. package/src/connection/input_pin.js +156 -0
  221. package/src/connection/neo6_mock.js +45 -0
  222. package/src/{transport → connection}/neopixel.js +23 -4
  223. package/src/connection/output_pin.js +41 -0
  224. package/src/connection/sipo.js +159 -0
  225. package/src/{transport → connection}/smbus.js +14 -10
  226. package/src/{transport → connection}/spi.js +12 -8
  227. package/src/connection/spi_mock.js +151 -0
  228. package/src/{transport → connection}/uart.js +33 -26
  229. package/examples/display/.gitkeep +0 -0
  230. package/examples/environmental/.gitkeep +0 -0
  231. package/examples/gnss/.gitkeep +0 -0
  232. package/examples/humidity/.gitkeep +0 -0
  233. package/examples/imu/.gitkeep +0 -0
  234. package/examples/light/.gitkeep +0 -0
  235. package/examples/pressure/.gitkeep +0 -0
  236. package/src/chips/display/.gitkeep +0 -0
  237. package/src/chips/environmental/.gitkeep +0 -0
  238. package/src/chips/gnss/.gitkeep +0 -0
  239. package/src/chips/imu/.gitkeep +0 -0
  240. package/src/chips/light/.gitkeep +0 -0
  241. package/src/chips/pressure/.gitkeep +0 -0
@@ -0,0 +1,410 @@
1
+ 'use strict';
2
+
3
+ const _REG_CHIP_ID = 0x00;
4
+ const _REG_STATUS = 0x03;
5
+ const _REG_DATA_0 = 0x04;
6
+ const _REG_PWR_CTRL = 0x1B;
7
+ const _REG_OSR = 0x1C;
8
+ const _REG_ODR = 0x1D;
9
+ const _REG_CONFIG = 0x1F;
10
+ const _REG_CMD = 0x7E;
11
+ const _REG_CAL_START = 0x31;
12
+ const _REG_CAL_LEN = 21;
13
+
14
+ const _CHIP_ID = 0x50;
15
+ const _SOFT_RESET_CMD = 0xB6;
16
+ const _FIFO_FLUSH_CMD = 0xB0;
17
+
18
+ const _MODE_SLEEP = 0x00;
19
+ const _MODE_FORCED = 0x01;
20
+ const _MODE_NORMAL = 0x03;
21
+
22
+ const _PWR_PRESS_EN = 0x01;
23
+ const _PWR_TEMP_EN = 0x02;
24
+
25
+ const _FIFO_HEADER_PRESS = 0x84;
26
+ const _FIFO_HEADER_TEMP = 0x90;
27
+ const _FIFO_HEADER_SENSORT = 0xA0;
28
+ const _FIFO_HEADER_ERROR = 0x44;
29
+ const _FIFO_HEADER_EMPTY = 0x80;
30
+
31
+ const _MEAS_TIME_MS = 40;
32
+
33
+ function _delay(ms) {
34
+ const start = Date.now();
35
+ while (Date.now() - start < ms) { /* spin */ }
36
+ }
37
+
38
+ function _s8(b) {
39
+ return b >= 128 ? b - 256 : b;
40
+ }
41
+
42
+ /**
43
+ * BMP384 high-precision barometric pressure and temperature sensor — minimal interface.
44
+ *
45
+ * Provides calibrated temperature (°C) and pressure (hPa) with no configuration
46
+ * beyond the connection. I²C address is 0x76 (SDO=GND) or 0x77 (SDO=VDD).
47
+ *
48
+ * Default: normal mode, osr_p=×16, osr_t=×2, iir=coef 3, ODR=25 Hz.
49
+ *
50
+ * Constructor caveat: JS constructors cannot be async. The calibration read
51
+ * and initial register writes are fired off unawaited, matching the
52
+ * fire-and-forget convention used throughout this port — in practice they
53
+ * settle before any subsequent `await` in caller code is scheduled, since
54
+ * the underlying I2C/SPI connection is synchronous under the hood, but this
55
+ * is not a guaranteed blocking wait the way the pre-async version was. Call
56
+ * `await sensor.temperature()` (or any other async method) once before
57
+ * relying on calibrated readings if this matters for your use case.
58
+ *
59
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
60
+ * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
61
+ */
62
+ class BMP384Minimal {
63
+ constructor(connection, busType = 'i2c') {
64
+ this._conn = connection;
65
+ this._busType = busType;
66
+ this._osrP = 4; // ×16
67
+ this._osrT = 1; // ×2
68
+ this._iir = 2; // coefficient 3
69
+ this._odr = 0x03; // 25 Hz
70
+ this._mode = _MODE_NORMAL;
71
+ this._init();
72
+ }
73
+
74
+ async _init() {
75
+ await this._readCalibration();
76
+ await this._applyConfig();
77
+ }
78
+
79
+ async _readCalibration() {
80
+ const data = await this._conn.writeRead(Buffer.from([_REG_CAL_START]), _REG_CAL_LEN);
81
+ // NVM_PAR_T1 (u16 LE)
82
+ this._parT1 = data.readUInt16LE(0);
83
+ // NVM_PAR_T2 (u16 LE)
84
+ this._parT2 = data.readUInt16LE(2);
85
+ // NVM_PAR_T3 (s8)
86
+ this._parT3 = _s8(data[4]);
87
+ // NVM_PAR_P1 (s16 LE)
88
+ this._parP1 = data.readInt16LE(5);
89
+ // NVM_PAR_P2 (s16 LE)
90
+ this._parP2 = data.readInt16LE(7);
91
+ // NVM_PAR_P3 (s8)
92
+ this._parP3 = _s8(data[9]);
93
+ // NVM_PAR_P4 (s8)
94
+ this._parP4 = _s8(data[10]);
95
+ // NVM_PAR_P5 (u16 LE)
96
+ this._parP5 = data.readUInt16LE(11);
97
+ // NVM_PAR_P6 (u16 LE)
98
+ this._parP6 = data.readUInt16LE(13);
99
+ // NVM_PAR_P7 (s8)
100
+ this._parP7 = _s8(data[15]);
101
+ // NVM_PAR_P8 (s8)
102
+ this._parP8 = _s8(data[16]);
103
+ // NVM_PAR_P9 (s16 LE)
104
+ this._parP9 = data.readInt16LE(17);
105
+ // NVM_PAR_P10 (s8)
106
+ this._parP10 = _s8(data[19]);
107
+ // NVM_PAR_P11 (s8)
108
+ this._parP11 = _s8(data[20]);
109
+
110
+ // Convert raw NVM coefficients to floating-point PAR values per datasheet.
111
+ // PAR_T1 = NVM_PAR_T1 × 256
112
+ this._parT1 = this._parT1 * 256.0;
113
+ this._parT2 = this._parT2 / Math.pow(2, 30);
114
+ this._parT3 = this._parT3 / Math.pow(2, 48);
115
+ this._parP1 = (this._parP1 - Math.pow(2, 14)) / Math.pow(2, 20);
116
+ this._parP2 = (this._parP2 - Math.pow(2, 14)) / Math.pow(2, 29);
117
+ this._parP3 = this._parP3 / Math.pow(2, 32);
118
+ this._parP4 = this._parP4 / Math.pow(2, 37);
119
+ this._parP5 = this._parP5 * 8.0;
120
+ this._parP6 = this._parP6 / Math.pow(2, 6);
121
+ this._parP7 = this._parP7 / Math.pow(2, 8);
122
+ this._parP8 = this._parP8 / Math.pow(2, 15);
123
+ this._parP9 = this._parP9 / Math.pow(2, 48);
124
+ this._parP10 = this._parP10 / Math.pow(2, 48);
125
+ this._parP11 = this._parP11 / Math.pow(2, 65);
126
+ }
127
+
128
+ async _applyConfig() {
129
+ const osrReg = (this._osrT << 3) | (this._osrP << 0);
130
+ const configReg = (this._iir << 1);
131
+ const pwrReg = (this._mode << 4) | _PWR_TEMP_EN | _PWR_PRESS_EN;
132
+ await this._writeReg(_REG_OSR, osrReg);
133
+ await this._writeReg(_REG_CONFIG, configReg);
134
+ await this._writeReg(_REG_ODR, this._odr);
135
+ await this._writeReg(_REG_PWR_CTRL, pwrReg);
136
+ }
137
+
138
+ async _writeReg(reg, value) {
139
+ const addr = this._busType === 'spi' ? (reg & 0x7F) : reg;
140
+ await this._conn.write(Buffer.from([addr, value]));
141
+ }
142
+
143
+ async _readReg(reg, n) {
144
+ return this._conn.writeRead(Buffer.from([reg]), n);
145
+ }
146
+
147
+ async _readBurst() {
148
+ // DATA_0..DATA_5 (0x04..0x09) = pressure XLSB/LSB/MSB, then temperature XLSB/LSB/MSB
149
+ const raw = await this._readReg(_REG_DATA_0, 6);
150
+ const uncompPress = (raw[2] << 16) | (raw[1] << 8) | raw[0];
151
+ const uncompTemp = (raw[5] << 16) | (raw[4] << 8) | raw[3];
152
+ return { uncompPress, uncompTemp };
153
+ }
154
+
155
+ _compensateTemperature(uncompTemp) {
156
+ const partial1 = uncompTemp - this._parT1;
157
+ const partial2 = partial1 * this._parT2;
158
+ this._tLin = partial2 + (partial1 * partial1) * this._parT3;
159
+ return this._tLin;
160
+ }
161
+
162
+ _compensatePressure(uncompPress) {
163
+ const tLin = this._tLin;
164
+ const p = Number(uncompPress);
165
+
166
+ const partial1a = this._parP6 * tLin;
167
+ const partial2a = this._parP7 * tLin * tLin;
168
+ const partial3a = this._parP8 * tLin * tLin * tLin;
169
+ const partialOut1 = this._parP5 + partial1a + partial2a + partial3a;
170
+
171
+ const partial1b = this._parP2 * tLin;
172
+ const partial2b = this._parP3 * tLin * tLin;
173
+ const partial3b = this._parP4 * tLin * tLin * tLin;
174
+ const partialOut2 = p * (this._parP1 + partial1b + partial2b + partial3b);
175
+
176
+ const partial1c = p * p;
177
+ const partial2c = this._parP9 + this._parP10 * tLin;
178
+ const partial3c = partial1c * partial2c;
179
+ const partial4 = partial3c + (p * p * p) * this._parP11;
180
+
181
+ return partialOut1 + partialOut2 + partial4;
182
+ }
183
+
184
+ /**
185
+ * Read calibrated temperature.
186
+ * @returns {Promise<number>} Temperature in degrees Celsius.
187
+ */
188
+ async temperature() {
189
+ if (this._mode === _MODE_FORCED) {
190
+ const pwrReg = (_MODE_FORCED << 4) | _PWR_TEMP_EN | _PWR_PRESS_EN;
191
+ await this._writeReg(_REG_PWR_CTRL, pwrReg);
192
+ _delay(_MEAS_TIME_MS);
193
+ }
194
+ const { uncompTemp } = await this._readBurst();
195
+ return this._compensateTemperature(uncompTemp);
196
+ }
197
+
198
+ /**
199
+ * Read calibrated pressure.
200
+ * @returns {Promise<number>} Pressure in hectopascals (hPa).
201
+ */
202
+ async pressure() {
203
+ if (this._mode === _MODE_FORCED) {
204
+ const pwrReg = (_MODE_FORCED << 4) | _PWR_TEMP_EN | _PWR_PRESS_EN;
205
+ await this._writeReg(_REG_PWR_CTRL, pwrReg);
206
+ _delay(_MEAS_TIME_MS);
207
+ }
208
+ const { uncompPress, uncompTemp } = await this._readBurst();
209
+ this._compensateTemperature(uncompTemp);
210
+ return this._compensatePressure(uncompPress) / 100.0;
211
+ }
212
+ }
213
+
214
+ /**
215
+ * BMP384 full interface — extends BMP384Minimal with configuration, mode control, and FIFO access.
216
+ *
217
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
218
+ * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
219
+ */
220
+ class BMP384Full extends BMP384Minimal {
221
+ static MODE_SLEEP = 0x00;
222
+ static MODE_FORCED = 0x01;
223
+ static MODE_NORMAL = 0x03;
224
+
225
+ constructor(connection, busType = 'i2c') {
226
+ super(connection, busType);
227
+ }
228
+
229
+ /**
230
+ * Write OSR, CONFIG, and ODR; does not enforce ODR ≥ T_conv at the JS layer.
231
+ * @param {number} osrP - Pressure oversampling (0–5).
232
+ * @param {number} osrT - Temperature oversampling (0–5).
233
+ * @param {number} iirFilter - IIR filter coefficient index (0–7).
234
+ * @param {number} odrSel - Output data rate selector (0x00–0x11).
235
+ * @returns {Promise<void>}
236
+ */
237
+ async configure(osrP, osrT, iirFilter, odrSel) {
238
+ this._osrP = osrP;
239
+ this._osrT = osrT;
240
+ this._iir = iirFilter;
241
+ this._odr = odrSel;
242
+ await this._writeReg(_REG_OSR, (osrT << 3) | (osrP << 0));
243
+ await this._writeReg(_REG_CONFIG, (iirFilter << 1));
244
+ await this._writeReg(_REG_ODR, odrSel);
245
+ }
246
+
247
+ /**
248
+ * Read both pressure and temperature in a single burst.
249
+ * @returns {Promise<{pressure:number, temperature:number}>} pressure in hPa, temperature in °C.
250
+ */
251
+ async read() {
252
+ if (this._mode === _MODE_FORCED) {
253
+ await this._triggerForced();
254
+ }
255
+ const { uncompPress, uncompTemp } = await this._readBurst();
256
+ const t = this._compensateTemperature(uncompTemp);
257
+ const p = this._compensatePressure(uncompPress) / 100.0;
258
+ return { pressure: p, temperature: t };
259
+ }
260
+
261
+ /**
262
+ * Trigger a forced-mode measurement, wait T_conv, return both values.
263
+ * @returns {Promise<{pressure:number, temperature:number}>}
264
+ */
265
+ async readForced() {
266
+ const prevMode = this._mode;
267
+ try {
268
+ await this.setMode(BMP384Full.MODE_FORCED);
269
+ await this._triggerForced();
270
+ const tConvMs = BMP384Full._computeTConvMs(this._osrP, this._osrT);
271
+ _delay(tConvMs);
272
+ const { uncompPress, uncompTemp } = await this._readBurst();
273
+ const t = this._compensateTemperature(uncompTemp);
274
+ const p = this._compensatePressure(uncompPress) / 100.0;
275
+ return { pressure: p, temperature: t };
276
+ } finally {
277
+ this._mode = prevMode;
278
+ await this._applyPwr();
279
+ }
280
+ }
281
+
282
+ /**
283
+ * Set the power mode.
284
+ * @param {number} mode
285
+ * @returns {Promise<void>}
286
+ */
287
+ async setMode(mode) {
288
+ this._mode = mode;
289
+ await this._applyPwr();
290
+ }
291
+
292
+ /**
293
+ * True if STATUS.drdy_press is set.
294
+ * @returns {Promise<boolean>}
295
+ */
296
+ async isDataReady() {
297
+ const raw = await this._readReg(_REG_STATUS, 1);
298
+ return (raw[0] & (1 << 5)) !== 0;
299
+ }
300
+
301
+ /**
302
+ * Soft reset, re-read calibration, re-apply config.
303
+ * @returns {Promise<void>}
304
+ */
305
+ async softreset() {
306
+ await this._writeReg(_REG_CMD, _SOFT_RESET_CMD);
307
+ _delay(3);
308
+ await this._readCalibration();
309
+ await this._applyConfig();
310
+ }
311
+
312
+ /**
313
+ * Configure FIFO source, watermark, and stop-on-full behaviour.
314
+ * @param {boolean} pressEn - Store pressure frames.
315
+ * @param {boolean} tempEn - Store temperature frames.
316
+ * @param {number} wtm - Watermark in bytes (0–511).
317
+ * @param {boolean} [stopOnFull=false]
318
+ * @returns {Promise<void>}
319
+ */
320
+ async fifoConfigure(pressEn, tempEn, wtm, stopOnFull = false) {
321
+ const cfg1 = (1 << 4)
322
+ | ((stopOnFull ? 1 : 0) << 3)
323
+ | ((tempEn ? 1 : 0) << 1)
324
+ | (pressEn ? 1 : 0);
325
+ await this._writeReg(0x17, cfg1);
326
+ await this._writeReg(0x15, wtm & 0xFF);
327
+ await this._writeReg(0x16, (wtm >> 8) & 0x01);
328
+ }
329
+
330
+ /**
331
+ * Read and parse every available FIFO frame.
332
+ * @returns {Promise<Array<{type:string, value:number|null}>>}
333
+ */
334
+ async fifoRead() {
335
+ const lenLo = (await this._readReg(0x12, 1))[0];
336
+ const lenHi = (await this._readReg(0x13, 1))[0];
337
+ const length = (lenHi << 8) | lenLo;
338
+ if (length === 0) return [];
339
+ const buf = await this._readReg(0x14, length);
340
+ const frames = [];
341
+ let i = 0;
342
+ while (i < buf.length) {
343
+ const hdr = buf[i];
344
+ if (hdr === _FIFO_HEADER_PRESS) {
345
+ const uncomp = (buf[i + 3] << 16) | (buf[i + 2] << 8) | buf[i + 1];
346
+ const valuePa = this._compensatePressureWithTLin(uncomp, this._tLin);
347
+ frames.push({ type: 'pressure', value: valuePa / 100.0 });
348
+ i += 4;
349
+ } else if (hdr === _FIFO_HEADER_TEMP) {
350
+ const uncomp = (buf[i + 3] << 16) | (buf[i + 2] << 8) | buf[i + 1];
351
+ const t = this._compensateTemperature(uncomp);
352
+ frames.push({ type: 'temperature', value: t });
353
+ i += 4;
354
+ } else if (hdr === _FIFO_HEADER_SENSORT) {
355
+ const uncomp = (buf[i + 3] << 16) | (buf[i + 2] << 8) | buf[i + 1];
356
+ frames.push({ type: 'sensortime', value: uncomp });
357
+ i += 4;
358
+ } else if (hdr === _FIFO_HEADER_ERROR || hdr === _FIFO_HEADER_EMPTY) {
359
+ frames.push({ type: hdr === _FIFO_HEADER_ERROR ? 'error' : 'empty', value: null });
360
+ i += 1;
361
+ } else {
362
+ frames.push({ type: 'unknown', value: null });
363
+ i += 1;
364
+ }
365
+ }
366
+ return frames;
367
+ }
368
+
369
+ /**
370
+ * Flush the FIFO contents.
371
+ * @returns {Promise<void>}
372
+ */
373
+ async fifoFlush() {
374
+ await this._writeReg(_REG_CMD, _FIFO_FLUSH_CMD);
375
+ }
376
+
377
+ /**
378
+ * Compute altitude above sea level from the current pressure.
379
+ * @param {number} [seaLevelHpa=1013.25]
380
+ * @returns {Promise<number>} Altitude in metres.
381
+ */
382
+ async altitude(seaLevelHpa = 1013.25) {
383
+ const p = await this.pressure();
384
+ if (p <= 0) return 0.0;
385
+ return 44330 * (1 - Math.pow(p / seaLevelHpa, 1 / 5.255));
386
+ }
387
+
388
+ async _triggerForced() {
389
+ const pwrReg = (_MODE_FORCED << 4) | _PWR_TEMP_EN | _PWR_PRESS_EN;
390
+ await this._writeReg(_REG_PWR_CTRL, pwrReg);
391
+ }
392
+
393
+ async _applyPwr() {
394
+ const pwrReg = (this._mode << 4) | _PWR_TEMP_EN | _PWR_PRESS_EN;
395
+ await this._writeReg(_REG_PWR_CTRL, pwrReg);
396
+ }
397
+
398
+ _compensatePressureWithTLin(uncompPress, tLin) {
399
+ this._tLin = tLin;
400
+ return this._compensatePressure(uncompPress);
401
+ }
402
+
403
+ static _computeTConvMs(osrP, osrT) {
404
+ // T_conv per spec: 234 + 392 + 2^osr_p*2000 + 313 + 2^osr_t*2000 µs.
405
+ const tConvUs = 234 + 392 + (1 << osrP) * 2000 + 313 + (1 << osrT) * 2000;
406
+ return Math.ceil(tConvUs / 1000) + 1;
407
+ }
408
+ }
409
+
410
+ module.exports = { BMP384Minimal, BMP384Full };