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,251 @@
1
+ 'use strict';
2
+
3
+ const _REG_ID = 0xD0;
4
+ const _REG_CAL_START = 0xAA;
5
+ const _REG_CTRL_MEAS = 0xF4;
6
+ const _REG_OUT_MSB = 0xF6;
7
+ const _REG_OUT_LSB = 0xF7;
8
+ const _REG_OUT_XLSB = 0xF8;
9
+ const _REG_SOFT_RESET = 0xE0;
10
+
11
+ const _CMD_TEMP = 0x2E;
12
+ const _CMD_PRESSURE = [0x34, 0x74, 0xB4, 0xF4];
13
+
14
+ const _CHIP_ID = 0x55;
15
+ const _SOFT_RESET_CMD = 0xB6;
16
+
17
+ const _CONV_TIME = [0.0045, 0.0075, 0.0135, 0.0255];
18
+ const _CONV_TIME_TEMP = 0.0045;
19
+
20
+ function _delay(ms) {
21
+ const start = Date.now();
22
+ while (Date.now() - start < ms) { /* spin */ }
23
+ }
24
+
25
+ /**
26
+ * BMP085 piezo-resistive pressure + temperature sensor — minimal interface.
27
+ *
28
+ * Provides calibrated temperature (°C) and pressure (Pa) with no configuration
29
+ * beyond the connection. Fixed I²C address 0x77.
30
+ *
31
+ * Default OSS = 0 (Ultra Low Power, 4.5 ms conversion).
32
+ *
33
+ * Constructor caveat: JS constructors cannot be async, but the original
34
+ * synchronous constructor validated the calibration block and threw
35
+ * synchronously on invalid data — that guarantee cannot be preserved
36
+ * exactly. The read is fired off unawaited (matching the fire-and-forget
37
+ * convention used throughout this port); invalid calibration data now
38
+ * surfaces as an *unhandled promise rejection* shortly after construction
39
+ * rather than a synchronous throw from `new BMP085Minimal(...)`. Callers
40
+ * that need to detect this deterministically should call
41
+ * `await sensor.temperature()` right after construction and handle its
42
+ * rejection.
43
+ *
44
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
45
+ */
46
+ class BMP085Minimal {
47
+ constructor(connection) {
48
+ this._conn = connection;
49
+ this._oss = 0;
50
+ this._readCalibration();
51
+ }
52
+
53
+ async _readCalibration() {
54
+ const data = await this._conn.writeRead(Buffer.from([_REG_CAL_START]), 22);
55
+ this._ac1 = data.readInt16BE(0);
56
+ this._ac2 = data.readInt16BE(2);
57
+ this._ac3 = data.readInt16BE(4);
58
+ this._ac4 = data.readUInt16BE(6);
59
+ this._ac5 = data.readUInt16BE(8);
60
+ this._ac6 = data.readUInt16BE(10);
61
+ this._b1 = data.readInt16BE(12);
62
+ this._b2 = data.readInt16BE(14);
63
+ this._mb = data.readInt16BE(16);
64
+ this._mc = data.readInt16BE(18);
65
+ this._md = data.readInt16BE(20);
66
+
67
+ const coefficients = [this._ac1, this._ac2, this._ac3, this._ac4,
68
+ this._ac5, this._ac6, this._b1, this._b2, this._mb, this._mc, this._md];
69
+ if (coefficients.some(c => c === 0 || c === 0xFFFF)) {
70
+ throw new Error('BMP085 calibration data invalid');
71
+ }
72
+ }
73
+
74
+ async _writeReg(reg, value) {
75
+ await this._conn.write(Buffer.from([reg, value]));
76
+ }
77
+
78
+ async _readRawTemp() {
79
+ await this._writeReg(_REG_CTRL_MEAS, _CMD_TEMP);
80
+ _delay(_CONV_TIME_TEMP * 1000);
81
+ const data = await this._conn.writeRead(Buffer.from([_REG_OUT_MSB]), 2);
82
+ return data.readUInt16BE(0);
83
+ }
84
+
85
+ async _readRawPressure() {
86
+ const cmd = _CMD_PRESSURE[this._oss];
87
+ await this._writeReg(_REG_CTRL_MEAS, cmd);
88
+ _delay(_CONV_TIME[this._oss] * 1000);
89
+ const data = await this._conn.writeRead(Buffer.from([_REG_OUT_MSB]), 3);
90
+ let up = ((data.readUInt16BE(0) << 8) | data[2]) >> (8 - this._oss);
91
+ return up;
92
+ }
93
+
94
+ _compensateTemp(ut) {
95
+ const ac1 = this._ac1, ac2 = this._ac2, ac3 = this._ac3;
96
+ const ac4 = this._ac4, ac5 = this._ac5, ac6 = this._ac6;
97
+ const b1 = this._b1, b2 = this._b2;
98
+ const mc = this._mc, md = this._md;
99
+
100
+ let x1 = Math.floor(((ut - ac6) * ac5) / 32768);
101
+ let x2 = Math.floor((mc << 11) / (x1 + md));
102
+ let b5 = x1 + x2;
103
+ this._b5 = b5;
104
+ return b5;
105
+ }
106
+
107
+ _compensatePressure(up) {
108
+ const oss = this._oss;
109
+ const ac1 = this._ac1, ac2 = this._ac2, ac3 = this._ac3;
110
+ const ac4 = this._ac4;
111
+ const b1 = this._b1, b2 = this._b2;
112
+ const b5 = this._b5;
113
+
114
+ let b6 = b5 - 4000;
115
+ let x1 = Math.floor((b2 * Math.floor((b6 * b6) / 4096)) / 2048);
116
+ let x2 = Math.floor((ac2 * b6) / 2048);
117
+ let x3 = x1 + x2;
118
+ let b3 = Math.floor((((ac1 * 4) + x3) << oss) / 4);
119
+ x1 = Math.floor((ac3 * b6) / 8192);
120
+ x2 = Math.floor((b1 * Math.floor((b6 * b6) / 4096)) / 65536);
121
+ x3 = Math.floor((x1 + x2 + 2) / 4);
122
+ let b4 = Math.floor((ac4 * (x3 + 32768)) / 32768);
123
+ let b7 = Math.floor((up - b3) * (50000 >>> oss));
124
+
125
+ let p;
126
+ if (b7 < 0x80000000) {
127
+ p = Math.floor((b7 * 2) / b4);
128
+ } else {
129
+ p = Math.floor((b7 / b4) * 2);
130
+ }
131
+
132
+ x1 = Math.floor((p >> 8) * (p >> 8));
133
+ x1 = Math.floor((x1 * 3038) / 65536);
134
+ x2 = Math.floor((-7357 * p) / 65536);
135
+ p = p + Math.floor((x1 + x2 + 3791) / 16);
136
+
137
+ return p;
138
+ }
139
+
140
+ /**
141
+ * Read calibrated temperature.
142
+ * @returns {Promise<number>} Temperature in degrees Celsius.
143
+ */
144
+ async temperature() {
145
+ const ut = await this._readRawTemp();
146
+ const b5 = this._compensateTemp(ut);
147
+ // Datasheet formula is T = ((B5+8) >> 4) / 10.0 - an integer
148
+ // right-shift (floor), THEN a float divide. Dividing (b5+8) by
149
+ // 160.0 directly is not equivalent: it skips the flooring the
150
+ // shift performs whenever (b5+8) isn't a multiple of 16, silently
151
+ // rounding up instead of down.
152
+ return Math.floor((b5 + 8) / 16) / 10.0;
153
+ }
154
+
155
+ /**
156
+ * Read calibrated pressure.
157
+ *
158
+ * Reads temperature first to refresh B5, then reads pressure.
159
+ * Self-contained — may be called without a prior temperature() call.
160
+ *
161
+ * @returns {Promise<number>} Pressure in pascals.
162
+ */
163
+ async pressure() {
164
+ const ut = await this._readRawTemp();
165
+ this._compensateTemp(ut);
166
+ const up = await this._readRawPressure();
167
+ const p_pa = this._compensatePressure(up);
168
+ return p_pa;
169
+ }
170
+ }
171
+
172
+ /**
173
+ * BMP085 full interface — extends BMP085Minimal with OSS control and altitude helpers.
174
+ *
175
+ * OSS constants:
176
+ * - BMP085Full.OSS_ULP — Ultra Low Power (oss=0, 4.5 ms)
177
+ * - BMP085Full.OSS_STANDARD — Standard (oss=1, 7.5 ms)
178
+ * - BMP085Full.OSS_HIGH_RES — High Resolution (oss=2, 13.5 ms)
179
+ * - BMP085Full.OSS_ULTRA_HIGH_RES — Ultra High Resolution (oss=3, 25.5 ms)
180
+ *
181
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
182
+ * @param {number} [oss=0] - Oversampling mode 0–3.
183
+ */
184
+ class BMP085Full extends BMP085Minimal {
185
+ static OSS_ULP = 0;
186
+ static OSS_STANDARD = 1;
187
+ static OSS_HIGH_RES = 2;
188
+ static OSS_ULTRA_HIGH_RES = 3;
189
+
190
+ constructor(connection, oss = 0) {
191
+ super(connection);
192
+ this._oss = oss & 0x03;
193
+ }
194
+
195
+ /**
196
+ * Read the current oversampling mode.
197
+ * @returns {number} OSS value 0–3.
198
+ */
199
+ oversampling() {
200
+ return this._oss;
201
+ }
202
+
203
+ /**
204
+ * Change the oversampling mode for subsequent pressure() calls.
205
+ * @param {number} oss - New OSS value 0–3.
206
+ */
207
+ setOversampling(oss) {
208
+ this._oss = oss & 0x03;
209
+ }
210
+
211
+ /**
212
+ * Compute altitude above sea level from the current pressure.
213
+ * @param {number} [seaLevelPa=101325.0] - Reference sea-level pressure in Pa.
214
+ * @returns {Promise<number>} Altitude in metres.
215
+ */
216
+ async altitude(seaLevelPa = 101325.0) {
217
+ const p = await this.pressure();
218
+ return 44330 * (1 - Math.pow(p / seaLevelPa, 1 / 5.255));
219
+ }
220
+
221
+ /**
222
+ * Compute sea-level pressure for a known altitude.
223
+ * @param {number} altitudeM - Altitude in metres.
224
+ * @returns {Promise<number>} Sea-level pressure in Pa.
225
+ */
226
+ async seaLevelPressure(altitudeM) {
227
+ const p = await this.pressure();
228
+ return p / Math.pow(1 - altitudeM / 44330, 5.255);
229
+ }
230
+
231
+ /**
232
+ * Read the chip ID register.
233
+ * @returns {Promise<number>} Chip ID; expect 0x55.
234
+ */
235
+ async chipId() {
236
+ const data = await this._conn.writeRead(Buffer.from([_REG_ID]), 1);
237
+ return data[0];
238
+ }
239
+
240
+ /**
241
+ * Perform a soft reset and re-read calibration coefficients.
242
+ * @returns {Promise<void>}
243
+ */
244
+ async reset() {
245
+ await this._writeReg(_REG_SOFT_RESET, _SOFT_RESET_CMD);
246
+ _delay(10);
247
+ await this._readCalibration();
248
+ }
249
+ }
250
+
251
+ module.exports = { BMP085Minimal, BMP085Full };
@@ -26,21 +26,32 @@ function _delay(ms) {
26
26
  * BMP180 piezo-resistive pressure + temperature sensor — minimal interface.
27
27
  *
28
28
  * Provides calibrated temperature (°C) and pressure (hPa) with no configuration
29
- * beyond the transport. Fixed I²C address 0x77.
29
+ * beyond the connection. Fixed I²C address 0x77.
30
30
  *
31
31
  * Default OSS = 0 (Ultra Low Power, 4.5 ms conversion).
32
32
  *
33
- * @param {object} transport - Configured I²C transport.
33
+ * Constructor caveat: JS constructors cannot be async, but the original
34
+ * synchronous constructor validated the calibration block and threw
35
+ * synchronously on invalid data — that guarantee cannot be preserved
36
+ * exactly. The read is fired off unawaited (matching the fire-and-forget
37
+ * convention used throughout this port); invalid calibration data now
38
+ * surfaces as an *unhandled promise rejection* shortly after construction
39
+ * rather than a synchronous throw from `new BMP180Minimal(...)`. Callers
40
+ * that need to detect this deterministically should call
41
+ * `await sensor.temperature()` right after construction and handle its
42
+ * rejection.
43
+ *
44
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
34
45
  */
35
46
  class BMP180Minimal {
36
- constructor(transport) {
37
- this._transport = transport;
47
+ constructor(connection) {
48
+ this._conn = connection;
38
49
  this._oss = 0;
39
50
  this._readCalibration();
40
51
  }
41
52
 
42
- _readCalibration() {
43
- const data = this._transport.writeRead(Buffer.from([_REG_CAL_START]), 22);
53
+ async _readCalibration() {
54
+ const data = await this._conn.writeRead(Buffer.from([_REG_CAL_START]), 22);
44
55
  this._ac1 = data.readInt16BE(0);
45
56
  this._ac2 = data.readInt16BE(2);
46
57
  this._ac3 = data.readInt16BE(4);
@@ -60,22 +71,22 @@ class BMP180Minimal {
60
71
  }
61
72
  }
62
73
 
63
- _writeReg(reg, value) {
64
- this._transport.write(Buffer.from([reg, value]));
74
+ async _writeReg(reg, value) {
75
+ await this._conn.write(Buffer.from([reg, value]));
65
76
  }
66
77
 
67
- _readRawTemp() {
68
- this._writeReg(_REG_CTRL_MEAS, _CMD_TEMP);
78
+ async _readRawTemp() {
79
+ await this._writeReg(_REG_CTRL_MEAS, _CMD_TEMP);
69
80
  _delay(_CONV_TIME_TEMP * 1000);
70
- const data = this._transport.writeRead(Buffer.from([_REG_OUT_MSB]), 2);
81
+ const data = await this._conn.writeRead(Buffer.from([_REG_OUT_MSB]), 2);
71
82
  return data.readUInt16BE(0);
72
83
  }
73
84
 
74
- _readRawPressure() {
85
+ async _readRawPressure() {
75
86
  const cmd = _CMD_PRESSURE[this._oss];
76
- this._writeReg(_REG_CTRL_MEAS, cmd);
87
+ await this._writeReg(_REG_CTRL_MEAS, cmd);
77
88
  _delay(_CONV_TIME[this._oss] * 1000);
78
- const data = this._transport.writeRead(Buffer.from([_REG_OUT_MSB]), 3);
89
+ const data = await this._conn.writeRead(Buffer.from([_REG_OUT_MSB]), 3);
79
90
  let up = ((data.readUInt16BE(0) << 8) | data[2]) >> (8 - this._oss);
80
91
  return up;
81
92
  }
@@ -128,12 +139,17 @@ class BMP180Minimal {
128
139
 
129
140
  /**
130
141
  * Read calibrated temperature.
131
- * @returns {number} Temperature in degrees Celsius.
142
+ * @returns {Promise<number>} Temperature in degrees Celsius.
132
143
  */
133
- temperature() {
134
- const ut = this._readRawTemp();
144
+ async temperature() {
145
+ const ut = await this._readRawTemp();
135
146
  const b5 = this._compensateTemp(ut);
136
- return ((b5 + 8) / 160.0);
147
+ // Datasheet formula is T = ((B5+8) >> 4) / 10.0 - an integer
148
+ // right-shift (floor), THEN a float divide. Dividing (b5+8) by
149
+ // 160.0 directly is not equivalent: it skips the flooring the
150
+ // shift performs whenever (b5+8) isn't a multiple of 16, silently
151
+ // rounding up instead of down.
152
+ return Math.floor((b5 + 8) / 16) / 10.0;
137
153
  }
138
154
 
139
155
  /**
@@ -142,12 +158,12 @@ class BMP180Minimal {
142
158
  * Reads temperature first to refresh B5, then reads pressure.
143
159
  * Self-contained — may be called without a prior temperature() call.
144
160
  *
145
- * @returns {number} Pressure in hPa.
161
+ * @returns {Promise<number>} Pressure in hPa.
146
162
  */
147
- pressure() {
148
- const ut = this._readRawTemp();
163
+ async pressure() {
164
+ const ut = await this._readRawTemp();
149
165
  this._compensateTemp(ut);
150
- const up = this._readRawPressure();
166
+ const up = await this._readRawPressure();
151
167
  const p_pa = this._compensatePressure(up);
152
168
  return p_pa / 100.0;
153
169
  }
@@ -162,7 +178,7 @@ class BMP180Minimal {
162
178
  * - BMP180Full.OSS_HIGH_RES — High Resolution (oss=2, 13.5 ms)
163
179
  * - BMP180Full.OSS_ULTRA_HIGH_RES — Ultra High Resolution (oss=3, 25.5 ms)
164
180
  *
165
- * @param {object} transport - Configured I²C transport.
181
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
166
182
  * @param {number} [oss=0] - Oversampling mode 0–3.
167
183
  */
168
184
  class BMP180Full extends BMP180Minimal {
@@ -171,8 +187,8 @@ class BMP180Full extends BMP180Minimal {
171
187
  static OSS_HIGH_RES = 2;
172
188
  static OSS_ULTRA_HIGH_RES = 3;
173
189
 
174
- constructor(transport, oss = 0) {
175
- super(transport);
190
+ constructor(connection, oss = 0) {
191
+ super(connection);
176
192
  this._oss = oss & 0x03;
177
193
  }
178
194
 
@@ -195,39 +211,40 @@ class BMP180Full extends BMP180Minimal {
195
211
  /**
196
212
  * Compute altitude above sea level from the current pressure.
197
213
  * @param {number} [seaLevelHpa=1013.25] - Reference sea-level pressure in hPa.
198
- * @returns {number} Altitude in metres.
214
+ * @returns {Promise<number>} Altitude in metres.
199
215
  */
200
- altitude(seaLevelHpa = 1013.25) {
201
- const p = this.pressure();
216
+ async altitude(seaLevelHpa = 1013.25) {
217
+ const p = await this.pressure();
202
218
  return 44330 * (1 - Math.pow(p / seaLevelHpa, 1 / 5.255));
203
219
  }
204
220
 
205
221
  /**
206
222
  * Compute sea-level pressure for a known altitude.
207
223
  * @param {number} altitudeM - Altitude in metres.
208
- * @returns {number} Sea-level pressure in hPa.
224
+ * @returns {Promise<number>} Sea-level pressure in hPa.
209
225
  */
210
- seaLevelPressure(altitudeM) {
211
- const p = this.pressure();
226
+ async seaLevelPressure(altitudeM) {
227
+ const p = await this.pressure();
212
228
  return p / Math.pow(1 - altitudeM / 44330, 5.255);
213
229
  }
214
230
 
215
231
  /**
216
232
  * Read the chip ID register.
217
- * @returns {number} Chip ID; expect 0x55.
233
+ * @returns {Promise<number>} Chip ID; expect 0x55.
218
234
  */
219
- chipId() {
220
- const data = this._transport.writeRead(Buffer.from([_REG_ID]), 1);
235
+ async chipId() {
236
+ const data = await this._conn.writeRead(Buffer.from([_REG_ID]), 1);
221
237
  return data[0];
222
238
  }
223
239
 
224
240
  /**
225
241
  * Perform a soft reset and re-read calibration coefficients.
242
+ * @returns {Promise<void>}
226
243
  */
227
- reset() {
228
- this._writeReg(_REG_SOFT_RESET, _SOFT_RESET_CMD);
244
+ async reset() {
245
+ await this._writeReg(_REG_SOFT_RESET, _SOFT_RESET_CMD);
229
246
  _delay(10);
230
- this._readCalibration();
247
+ await this._readCalibration();
231
248
  }
232
249
  }
233
250