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,512 @@
1
+ 'use strict';
2
+
3
+ const CONST_ARRAY1 = [
4
+ 2147483647, 2147483647, 2147483647, 2147483647, 2147483647,
5
+ 2126008810, 2147483647, 2130303777, 2147483647, 2147483647,
6
+ 2143188679, 2136746228, 2147483647, 2126008810, 2147483647,
7
+ 2147483647
8
+ ];
9
+
10
+ const CONST_ARRAY2 = [
11
+ 4096000000, 2048000000, 1024000000, 512000000, 255744255,
12
+ 127110228, 64000000, 32258064, 16016016, 8000000,
13
+ 4000000, 2000000, 1000000, 500000, 250000,
14
+ 125000
15
+ ];
16
+
17
+ const _REG_RES_HEAT_VAL = 0x00;
18
+ const _REG_RES_HEAT_RANGE = 0x02;
19
+ const _REG_RANGE_SW_ERR = 0x04;
20
+ const _REG_MEAS_STATUS = 0x1D;
21
+ const _REG_PRESS_MSB = 0x1F;
22
+ const _REG_CTRL_GAS_0 = 0x70;
23
+ const _REG_CTRL_GAS_1 = 0x71;
24
+ const _REG_CTRL_HUM = 0x72;
25
+ const _REG_CTRL_MEAS = 0x74;
26
+ const _REG_CONFIG = 0x75;
27
+ const _REG_CAL_BLOCK1 = 0x8A;
28
+ const _REG_ID = 0xD0;
29
+ const _REG_RESET = 0xE0;
30
+ const _REG_CAL_BLOCK2 = 0xE1;
31
+
32
+ const _CHIP_ID = 0x61;
33
+ const _RESET_CMD = 0xB6;
34
+
35
+ const _MEAS_TIME_MS = 200;
36
+
37
+ function _delay(ms) {
38
+ const start = Date.now();
39
+ while (Date.now() - start < ms) { /* spin */ }
40
+ }
41
+
42
+ /**
43
+ * BME680 4-in-1 environmental sensor: temperature, pressure, humidity, gas resistance — minimal interface.
44
+ *
45
+ * Provides calibrated readings with no configuration beyond the connection.
46
+ * I²C address is 0x76 (SDO=GND) or 0x77 (SDO=VDDIO).
47
+ *
48
+ * Default: forced mode, osrs_t=×1, osrs_p=×1, osrs_h=×1, IIR filter off,
49
+ * heater profile 0 at 320 °C / 150 ms.
50
+ *
51
+ * Constructor caveat: JS constructors cannot be async. The calibration read
52
+ * and initial register writes are fired off unawaited, matching the
53
+ * fire-and-forget convention used throughout this port — in practice they
54
+ * settle before any subsequent `await` in caller code is scheduled, since
55
+ * the underlying I2C connection is synchronous under the hood, but this is
56
+ * not a guaranteed blocking wait the way the pre-async version was. Call
57
+ * `await sensor.temperature()` (or any other async method) once before
58
+ * relying on calibrated readings if this matters for your use case.
59
+ *
60
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
61
+ */
62
+ class BME680Minimal {
63
+ constructor(connection) {
64
+ this._conn = connection;
65
+ this._osrsT = 1;
66
+ this._osrsP = 1;
67
+ this._osrsH = 1;
68
+ this._filter = 0;
69
+ this._tFine = 0;
70
+ this._ambientTemp = 25.0;
71
+ this._heatTemp = 320;
72
+ this._heatDur = 150;
73
+ this._gasEnabled = true;
74
+ this._nbConv = 0;
75
+ this._init();
76
+ }
77
+
78
+ async _init() {
79
+ await this._readCalibration();
80
+ await this._writeReg(_REG_CTRL_HUM, this._osrsH);
81
+ await this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | 0);
82
+ await this._writeReg(_REG_CONFIG, 0);
83
+ await this._setupHeater(0, this._heatTemp, this._heatDur);
84
+ await this._writeReg(_REG_CTRL_GAS_1, (1 << 4) | 0);
85
+ }
86
+
87
+ async _readCalibration() {
88
+ const b1 = await this._conn.writeRead(Buffer.from([_REG_CAL_BLOCK1]), 23);
89
+ const b2 = await this._conn.writeRead(Buffer.from([_REG_CAL_BLOCK2]), 14);
90
+ const s1 = await this._conn.writeRead(Buffer.from([_REG_RES_HEAT_VAL]), 1);
91
+ const s2 = await this._conn.writeRead(Buffer.from([_REG_RES_HEAT_RANGE]), 1);
92
+ const s3 = await this._conn.writeRead(Buffer.from([_REG_RANGE_SW_ERR]), 1);
93
+
94
+ this._parT2 = b1.readInt16LE(0);
95
+ this._parT3 = b1.readInt8(2);
96
+ this._parP1 = b1.readUInt16LE(4);
97
+ this._parP2 = b1.readInt16LE(6);
98
+ this._parP3 = b1.readInt8(8);
99
+ this._parP4 = b1.readInt16LE(10);
100
+ this._parP5 = b1.readInt16LE(12);
101
+ this._parP7 = b1.readInt8(14);
102
+ this._parP6 = b1.readInt8(15);
103
+ this._parP8 = b1.readInt16LE(18);
104
+ this._parP9 = b1.readInt16LE(20);
105
+ this._parP10 = b1[22];
106
+
107
+ this._parH2 = (b2[0] << 4) | (b2[1] >> 4);
108
+ this._parH1 = (b2[2] << 4) | (b2[1] & 0x0F);
109
+ this._parH3 = b2.readInt8(3);
110
+ this._parH4 = b2.readInt8(4);
111
+ this._parH5 = b2.readInt8(5);
112
+ this._parH6 = b2[6];
113
+ this._parH7 = b2.readInt8(7);
114
+ this._parT1 = b2.readUInt16LE(8);
115
+ this._parG2 = b2.readInt16LE(10);
116
+ this._parG1 = b2.readInt8(12);
117
+ this._parG3 = b2.readInt8(13);
118
+
119
+ this._resHeatVal = s1.readInt8(0);
120
+ this._resHeatRange = (s2[0] >> 4) & 0x03;
121
+ const rse = (s3[0] >> 4) & 0x0F;
122
+ this._rangeSwitchingError = rse < 8 ? rse : rse - 16;
123
+ }
124
+
125
+ async _writeReg(reg, value) {
126
+ await this._conn.write(Buffer.from([reg, value]));
127
+ }
128
+
129
+ async _readReg(reg, n) {
130
+ return this._conn.writeRead(Buffer.from([reg]), n);
131
+ }
132
+
133
+ _calcHeaterResistance(targetTemp, ambientTemp) {
134
+ const parG1 = this._parG1;
135
+ const parG2 = this._parG2;
136
+ const parG3 = this._parG3;
137
+ const rhr = this._resHeatRange;
138
+ const rhv = this._resHeatVal;
139
+
140
+ const var1 = ((ambientTemp * parG3) / 10 | 0) << 8;
141
+ const var2 = (parG1 + 784) * ((((parG2 + 154009) * targetTemp * 5 / 100 | 0) + 3276800) / 10 | 0);
142
+ const var3 = var1 + (var2 >> 1);
143
+ const var4 = var3 / (rhr + 4) | 0;
144
+ const var5 = (131 * rhv) + 65536;
145
+ const resHeatX100 = ((var4 / var5 | 0) - 250) * 34;
146
+ const resHeatX = (resHeatX100 + 50) / 100 | 0;
147
+ return resHeatX & 0xFF;
148
+ }
149
+
150
+ _calcGasWait(targetMs) {
151
+ if (targetMs <= 0x3F) return targetMs;
152
+ if (targetMs <= 0x3F * 4) return (1 << 6) | (targetMs / 4 | 0);
153
+ if (targetMs <= 0x3F * 16) return (2 << 6) | (targetMs / 16 | 0);
154
+ return (3 << 6) | Math.min(targetMs / 64 | 0, 0x3F);
155
+ }
156
+
157
+ async _setupHeater(index, tempC, durMs) {
158
+ const res = this._calcHeaterResistance(tempC, this._ambientTemp);
159
+ const gw = this._calcGasWait(durMs);
160
+ await this._writeReg(0x5A + index, res);
161
+ await this._writeReg(0x64 + index, gw);
162
+ }
163
+
164
+ async _triggerAndRead() {
165
+ await this._writeReg(_REG_CTRL_HUM, this._osrsH);
166
+ const ctrl = (this._osrsT << 5) | (this._osrsP << 2) | 1;
167
+ await this._writeReg(_REG_CTRL_MEAS, ctrl);
168
+ _delay(_MEAS_TIME_MS);
169
+ const raw = await this._readReg(_REG_PRESS_MSB, 13);
170
+ const pressAdc = (raw[0] << 12) | (raw[1] << 4) | (raw[2] >> 4);
171
+ const tempAdc = (raw[3] << 12) | (raw[4] << 4) | (raw[5] >> 4);
172
+ const humAdc = (raw[6] << 8) | raw[7];
173
+ const gasAdc = (raw[11] << 2) | (raw[12] >> 6);
174
+ const gasRange = raw[12] & 0x0F;
175
+ const gasValid = (raw[12] >> 5) & 1;
176
+ const heatStab = (raw[12] >> 4) & 1;
177
+ return { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab };
178
+ }
179
+
180
+ _compensateTemp(adcT) {
181
+ const parT1 = this._parT1;
182
+ const parT2 = this._parT2;
183
+ const parT3 = this._parT3;
184
+ const var1 = (adcT >> 3) - (parT1 << 1);
185
+ const var2 = (var1 * parT2) >> 11;
186
+ const var3 = (((var1 >> 1) * (var1 >> 1)) >> 12) * (parT3 << 4) >> 14;
187
+ this._tFine = var2 + var3;
188
+ return ((this._tFine * 5 + 128) >> 8) / 100.0;
189
+ }
190
+
191
+ _compensatePressure(adcP) {
192
+ const tFine = this._tFine;
193
+ const parP1 = this._parP1;
194
+ const parP2 = this._parP2;
195
+ const parP3 = this._parP3;
196
+ const parP4 = this._parP4;
197
+ const parP5 = this._parP5;
198
+ const parP6 = this._parP6;
199
+ const parP7 = this._parP7;
200
+ const parP8 = this._parP8;
201
+ const parP9 = this._parP9;
202
+ const parP10 = this._parP10;
203
+
204
+ let var1 = (tFine >> 1) - 64000;
205
+ let var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * parP6) >> 2;
206
+ var2 = var2 + ((var1 * parP5) << 1);
207
+ var2 = (var2 >> 2) + (parP4 << 16);
208
+ var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) * (parP3 << 5)) >> 3) + ((parP2 * var1) >> 1);
209
+ var1 = var1 >> 18;
210
+ var1 = ((32768 + var1) * parP1) >> 15;
211
+ let pressComp = 1048576 - adcP;
212
+ pressComp = ((pressComp - (var2 >> 12)) * 3125);
213
+ if (pressComp >= (1 << 30)) {
214
+ pressComp = (pressComp / var1 | 0) << 1;
215
+ } else {
216
+ pressComp = (pressComp << 1) / var1 | 0;
217
+ }
218
+ var1 = (parP9 * (((pressComp >> 3) * (pressComp >> 3)) >> 13)) >> 12;
219
+ var2 = ((pressComp >> 2) * parP8) >> 13;
220
+ const var3 = ((pressComp >> 8) * (pressComp >> 8) * (pressComp >> 8) * parP10) >> 17;
221
+ pressComp = pressComp + ((var1 + var2 + var3 + (parP7 << 7)) >> 4);
222
+ return pressComp / 100.0;
223
+ }
224
+
225
+ _compensateHumidity(humAdc) {
226
+ const tempScaled = this._tFine;
227
+ const parH1 = this._parH1;
228
+ const parH2 = this._parH2;
229
+ const parH3 = this._parH3;
230
+ const parH4 = this._parH4;
231
+ const parH5 = this._parH5;
232
+ const parH6 = this._parH6;
233
+ const parH7 = this._parH7;
234
+
235
+ const var1 = humAdc - ((parH1 << 4) + (Math.floor((tempScaled * parH3) / 100) >> 1));
236
+ const h5 = Math.floor(tempScaled * Math.floor((tempScaled * parH5) / 100) / 64);
237
+ const var2 = Math.floor(parH2 * (Math.floor((tempScaled * parH4) / 100) +
238
+ Math.floor(h5 / 100) + (1 << 14)) / 1024);
239
+ const var3 = var1 * var2;
240
+ const var4 = Math.floor(((parH6 << 7) + Math.floor((tempScaled * parH7) / 100)) / 16);
241
+ const var5 = Math.floor(Math.floor(var3 / 16384) * Math.floor(var3 / 16384) / 1024);
242
+ const var6 = Math.floor(var4 * var5 / 2);
243
+ let humComp = Math.floor(Math.floor((var3 + var6) / 1024) * 1000 / 4096);
244
+ if (humComp < 0) humComp = 0;
245
+ if (humComp > 100000) humComp = 100000;
246
+ return humComp / 1000.0;
247
+ }
248
+
249
+ _compensateGas(gasAdc, gasRange) {
250
+ const rse = this._rangeSwitchingError;
251
+ const var1 = Math.floor((1340 + 5 * rse) * CONST_ARRAY1[gasRange] / 65536);
252
+ const var2 = ((gasAdc << 15) - (1 << 24)) + var1;
253
+ if (var2 === 0) return NaN;
254
+ const gasRes = Math.floor(CONST_ARRAY2[gasRange] * var1 / 512) + Math.floor(var2 / 2);
255
+ return Math.floor(gasRes / var2);
256
+ }
257
+
258
+ /**
259
+ * Read calibrated temperature.
260
+ * @returns {Promise<number>} Temperature in degrees Celsius.
261
+ */
262
+ async temperature() {
263
+ const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = await this._triggerAndRead();
264
+ const t = this._compensateTemp(tempAdc);
265
+ this._ambientTemp = t;
266
+ return t;
267
+ }
268
+
269
+ /**
270
+ * Read calibrated pressure.
271
+ * @returns {Promise<number>} Pressure in hPa.
272
+ */
273
+ async pressure() {
274
+ const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = await this._triggerAndRead();
275
+ this._compensateTemp(tempAdc);
276
+ return this._compensatePressure(pressAdc);
277
+ }
278
+
279
+ /**
280
+ * Read calibrated humidity.
281
+ * @returns {Promise<number>} Relative humidity in %RH.
282
+ */
283
+ async humidity() {
284
+ const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = await this._triggerAndRead();
285
+ this._compensateTemp(tempAdc);
286
+ return this._compensateHumidity(humAdc);
287
+ }
288
+
289
+ /**
290
+ * Read gas sensor resistance.
291
+ * @returns {Promise<number>} Gas resistance in Ohms, or NaN on invalid reading.
292
+ */
293
+ async gasResistance() {
294
+ const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = await this._triggerAndRead();
295
+ this._compensateTemp(tempAdc);
296
+ if (!gasValid || !heatStab) return NaN;
297
+ return this._compensateGas(gasAdc, gasRange);
298
+ }
299
+ }
300
+
301
+ /**
302
+ * BME680 full interface — extends BME680Minimal with configuration, multi-profile heater, and status.
303
+ *
304
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
305
+ */
306
+ class BME680Full extends BME680Minimal {
307
+ static OSRS_SKIP = 0;
308
+ static OSRS_X1 = 1;
309
+ static OSRS_X2 = 2;
310
+ static OSRS_X4 = 3;
311
+ static OSRS_X8 = 4;
312
+ static OSRS_X16 = 5;
313
+
314
+ static MODE_SLEEP = 0;
315
+ static MODE_FORCED = 1;
316
+
317
+ static FILTER_0 = 0;
318
+ static FILTER_1 = 1;
319
+ static FILTER_3 = 2;
320
+ static FILTER_7 = 3;
321
+ static FILTER_15 = 4;
322
+ static FILTER_31 = 5;
323
+ static FILTER_63 = 6;
324
+ static FILTER_127 = 7;
325
+
326
+ static STATUS_NEW_DATA = 0x80;
327
+ static STATUS_GAS_MEASURING = 0x40;
328
+ static STATUS_MEASURING = 0x20;
329
+ static STATUS_GAS_VALID = 0x20;
330
+ static STATUS_HEATER_STABLE = 0x10;
331
+
332
+ constructor(connection) {
333
+ super(connection);
334
+ }
335
+
336
+ /**
337
+ * Write ctrl_hum, ctrl_meas, and config registers in the correct order.
338
+ * @param {number} osrsT - Temperature oversampling (0–5).
339
+ * @param {number} osrsP - Pressure oversampling (0–5).
340
+ * @param {number} osrsH - Humidity oversampling (0–5).
341
+ * @param {number} mode - Power mode (0=sleep, 1=forced).
342
+ * @param {number} filter - IIR filter coefficient (0–7).
343
+ * @returns {Promise<void>}
344
+ */
345
+ async configure(osrsT, osrsP, osrsH, mode, filter) {
346
+ this._osrsT = osrsT;
347
+ this._osrsP = osrsP;
348
+ this._osrsH = osrsH;
349
+ this._filter = filter;
350
+ await this._writeReg(_REG_CTRL_HUM, osrsH);
351
+ await this._writeReg(_REG_CONFIG, filter << 2);
352
+ await this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | mode);
353
+ }
354
+
355
+ /**
356
+ * Update oversampling for all three TPH channels.
357
+ * @param {number} osrsT - Temperature oversampling (0–5).
358
+ * @param {number} osrsP - Pressure oversampling (0–5).
359
+ * @param {number} osrsH - Humidity oversampling (0–5).
360
+ * @returns {Promise<void>}
361
+ */
362
+ async setOversampling(osrsT, osrsP, osrsH) {
363
+ this._osrsT = osrsT;
364
+ this._osrsP = osrsP;
365
+ this._osrsH = osrsH;
366
+ await this._writeReg(_REG_CTRL_HUM, osrsH);
367
+ await this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | 0);
368
+ }
369
+
370
+ /**
371
+ * Update IIR filter coefficient.
372
+ * @param {number} coeff - Filter coefficient (0–7).
373
+ * @returns {Promise<void>}
374
+ */
375
+ async setFilter(coeff) {
376
+ this._filter = coeff;
377
+ await this._writeReg(_REG_CONFIG, coeff << 2);
378
+ }
379
+
380
+ /**
381
+ * Configure heater profile 0 and activate it.
382
+ * @param {number} tempC - Target heater temperature in degrees Celsius.
383
+ * @param {number} durationMs - Heater on-time in milliseconds (1–4032).
384
+ * @returns {Promise<void>}
385
+ */
386
+ async setHeater(tempC, durationMs) {
387
+ this._heatTemp = tempC;
388
+ this._heatDur = durationMs;
389
+ await this._setupHeater(0, tempC, durationMs);
390
+ await this._writeReg(_REG_CTRL_GAS_1, (1 << 4) | 0);
391
+ }
392
+
393
+ /**
394
+ * Configure one of the 10 heater profiles.
395
+ * @param {number} index - Profile index (0–9).
396
+ * @param {number} tempC - Target heater temperature in degrees Celsius.
397
+ * @param {number} durationMs - Heater on-time in milliseconds (1–4032).
398
+ * @returns {Promise<void>}
399
+ */
400
+ async setHeaterProfile(index, tempC, durationMs) {
401
+ await this._setupHeater(index, tempC, durationMs);
402
+ }
403
+
404
+ /**
405
+ * Select which heater profile to use in the next forced cycle.
406
+ * @param {number} index - Profile index (0–9).
407
+ * @returns {Promise<void>}
408
+ */
409
+ async selectHeaterProfile(index) {
410
+ this._nbConv = index;
411
+ const gas1 = this._gasEnabled ? ((1 << 4) | index) : index;
412
+ await this._writeReg(_REG_CTRL_GAS_1, gas1);
413
+ }
414
+
415
+ /**
416
+ * Enable or disable gas conversion.
417
+ * @param {boolean} enabled - True to enable gas measurement.
418
+ * @returns {Promise<void>}
419
+ */
420
+ async setGasEnabled(enabled) {
421
+ this._gasEnabled = enabled;
422
+ const gas1 = enabled ? ((1 << 4) | this._nbConv) : this._nbConv;
423
+ await this._writeReg(_REG_CTRL_GAS_1, gas1);
424
+ }
425
+
426
+ /**
427
+ * Turn the heater off or on via ctrl_gas_0.
428
+ * @param {boolean} off - True to disable the heater.
429
+ * @returns {Promise<void>}
430
+ */
431
+ async setHeaterOff(off) {
432
+ await this._writeReg(_REG_CTRL_GAS_0, off ? 0x08 : 0x00);
433
+ }
434
+
435
+ /**
436
+ * Override the ambient temperature used for heater-resistance calculation.
437
+ * @param {number} tempC - Ambient temperature in degrees Celsius.
438
+ * @returns {Promise<void>}
439
+ */
440
+ async setAmbientTemperature(tempC) {
441
+ this._ambientTemp = tempC;
442
+ await this._setupHeater(this._nbConv, this._heatTemp, this._heatDur);
443
+ }
444
+
445
+ /**
446
+ * Read all four sensor values from a single TPHG cycle.
447
+ * @returns {Promise<{ temperature: number, pressure: number, humidity: number, gasResistance: number }>}
448
+ */
449
+ async readAll() {
450
+ const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = await this._triggerAndRead();
451
+ const t = this._compensateTemp(tempAdc);
452
+ this._ambientTemp = t;
453
+ const p = this._compensatePressure(pressAdc);
454
+ const h = this._compensateHumidity(humAdc);
455
+ const g = (gasValid && heatStab) ? this._compensateGas(gasAdc, gasRange) : NaN;
456
+ return { temperature: t, pressure: p, humidity: h, gasResistance: g };
457
+ }
458
+
459
+ /**
460
+ * Check if the most recent gas reading is valid.
461
+ * @returns {Promise<boolean>} True if gas_valid_r was set.
462
+ */
463
+ async gasValid() {
464
+ const raw = await this._readReg(0x2B, 1);
465
+ return ((raw[0] >> 5) & 1) === 1;
466
+ }
467
+
468
+ /**
469
+ * Check if the heater reached its target temperature.
470
+ * @returns {Promise<boolean>} True if heat_stab_r was set.
471
+ */
472
+ async heaterStable() {
473
+ const raw = await this._readReg(0x2B, 1);
474
+ return ((raw[0] >> 4) & 1) === 1;
475
+ }
476
+
477
+ /**
478
+ * Read the measurement status register.
479
+ * @returns {Promise<number>} Status byte with flags.
480
+ */
481
+ async status() {
482
+ const raw = await this._readReg(_REG_MEAS_STATUS, 1);
483
+ return raw[0];
484
+ }
485
+
486
+ /**
487
+ * Read the chip ID register.
488
+ * @returns {Promise<number>} Chip ID; expect 0x61.
489
+ */
490
+ async chipId() {
491
+ const raw = await this._readReg(_REG_ID, 1);
492
+ return raw[0];
493
+ }
494
+
495
+ /**
496
+ * Perform a soft reset, re-read calibration, and re-apply configuration.
497
+ * @returns {Promise<void>}
498
+ */
499
+ async reset() {
500
+ await this._writeReg(_REG_RESET, _RESET_CMD);
501
+ _delay(2);
502
+ await this._readCalibration();
503
+ await this._writeReg(_REG_CTRL_HUM, this._osrsH);
504
+ await this._writeReg(_REG_CONFIG, this._filter << 2);
505
+ await this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | 0);
506
+ await this._setupHeater(this._nbConv, this._heatTemp, this._heatDur);
507
+ const gas1 = this._gasEnabled ? ((1 << 4) | this._nbConv) : this._nbConv;
508
+ await this._writeReg(_REG_CTRL_GAS_1, gas1);
509
+ }
510
+ }
511
+
512
+ module.exports = { BME680Minimal, BME680Full };