periph 1.0.1 → 1.1.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 (56) hide show
  1. package/examples/adc_dac/hx711/complete.js +42 -0
  2. package/examples/adc_dac/hx711/demo.js +35 -0
  3. package/examples/adc_dac/hx711/minimal.js +16 -0
  4. package/examples/adc_dac/mcp4728/complete.js +41 -0
  5. package/examples/adc_dac/mcp4728/demo.js +41 -0
  6. package/examples/adc_dac/mcp4728/minimal.js +11 -0
  7. package/examples/display/pcf8576/complete.js +32 -0
  8. package/examples/display/pcf8576/demo.js +48 -0
  9. package/examples/display/pcf8576/minimal.js +18 -0
  10. package/examples/environmental/aht21/complete.js +30 -0
  11. package/examples/environmental/aht21/demo.js +44 -0
  12. package/examples/environmental/aht21/minimal.js +12 -0
  13. package/examples/environmental/bme280/complete.js +34 -0
  14. package/examples/environmental/bme280/demo.js +67 -0
  15. package/examples/environmental/bme280/minimal.js +18 -0
  16. package/examples/environmental/bme680/complete.js +37 -0
  17. package/examples/environmental/bme680/demo.js +52 -0
  18. package/examples/environmental/bme680/minimal.js +19 -0
  19. package/examples/gas/ens160/complete.js +52 -0
  20. package/examples/gas/ens160/demo.js +52 -0
  21. package/examples/gas/ens160/minimal.js +27 -0
  22. package/examples/imu/mpu6050/complete.js +56 -0
  23. package/examples/imu/mpu6050/demo.js +34 -0
  24. package/examples/imu/mpu6050/minimal.js +14 -0
  25. package/examples/light/apds9960/complete.js +62 -0
  26. package/examples/light/apds9960/demo.js +32 -0
  27. package/examples/light/apds9960/minimal.js +12 -0
  28. package/examples/memory/24aa02uid/complete.js +40 -0
  29. package/examples/memory/24aa02uid/demo.js +40 -0
  30. package/examples/memory/24aa02uid/minimal.js +12 -0
  31. package/examples/pressure/bmp280/complete.js +22 -18
  32. package/examples/pressure/bmp280/demo.js +34 -18
  33. package/examples/pressure/bmp280/minimal.js +4 -4
  34. package/package.json +1 -1
  35. package/src/chips/adc_dac/hx711.js +165 -0
  36. package/src/chips/adc_dac/mcp4728.js +284 -0
  37. package/src/chips/display/pcf8576.js +223 -0
  38. package/src/chips/environmental/aht21.js +170 -0
  39. package/src/chips/environmental/bme280.js +366 -0
  40. package/src/chips/environmental/bme680.js +489 -0
  41. package/src/chips/gas/ens160.js +280 -0
  42. package/src/chips/imu/mpu6050.js +266 -0
  43. package/src/chips/light/apds9960.js +464 -0
  44. package/src/chips/memory/_24aa02uid.js +179 -0
  45. package/src/chips/pressure/bmp280.js +154 -191
  46. package/src/transport/hx711.js +105 -0
  47. package/examples/display/.gitkeep +0 -0
  48. package/examples/environmental/.gitkeep +0 -0
  49. package/examples/imu/.gitkeep +0 -0
  50. package/examples/light/.gitkeep +0 -0
  51. package/examples/pressure/.gitkeep +0 -0
  52. package/src/chips/display/.gitkeep +0 -0
  53. package/src/chips/environmental/.gitkeep +0 -0
  54. package/src/chips/imu/.gitkeep +0 -0
  55. package/src/chips/light/.gitkeep +0 -0
  56. package/src/chips/pressure/.gitkeep +0 -0
@@ -1,16 +1,17 @@
1
1
  'use strict';
2
2
 
3
+ const _REG_CAL_START = 0x88;
3
4
  const _REG_ID = 0xD0;
4
5
  const _REG_RESET = 0xE0;
5
6
  const _REG_STATUS = 0xF3;
6
7
  const _REG_CTRL_MEAS = 0xF4;
7
8
  const _REG_CONFIG = 0xF5;
8
- const _REG_CAL_START = 0x88;
9
- const _REG_DATA = 0xF7;
9
+ const _REG_DATA_START = 0xF7;
10
+
11
+ const _CHIP_ID = 0x58;
12
+ const _RESET_CMD = 0xB6;
10
13
 
11
- const _CHIP_ID = 0x58;
12
- const _RESET_CMD = 0xB6;
13
- const _CTRL_MEAS_DEFAULT = 0x25;
14
+ const _MEAS_TIME_MS = 7;
14
15
 
15
16
  function _delay(ms) {
16
17
  const start = Date.now();
@@ -21,159 +22,149 @@ function _delay(ms) {
21
22
  * BMP280 piezo-resistive pressure + temperature sensor — minimal interface.
22
23
  *
23
24
  * Provides calibrated temperature (°C) and pressure (hPa) with no configuration
24
- * beyond the transport. Default configuration: forced mode, oversampling ×1 for
25
- * both channels, IIR filter off.
25
+ * beyond the transport. I²C address is 0x76 (SDO=GND) or 0x77 (SDO=VDDIO).
26
26
  *
27
- * @param {object} transport - Configured I²C transport.
28
- * @param {number} [addr=0x76] - 7-bit I²C address (default 0x76, alternate 0x77).
27
+ * Default: forced mode, osrs_t=×1, osrs_p=×1, IIR filter off.
28
+ *
29
+ * @param {object} transport - Configured I²C or SPI transport.
30
+ * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
29
31
  */
30
32
  class BMP280Minimal {
31
- constructor(transport, addr = 0x76) {
33
+ constructor(transport, busType = 'i2c') {
32
34
  this._transport = transport;
33
- this._addr = addr;
34
- this._t_fine = null;
35
- this._ctrlMeasCache = _CTRL_MEAS_DEFAULT;
36
- this._loadCalibration();
37
- this._writeCtrlMeas(_CTRL_MEAS_DEFAULT);
38
- this._writeConfig(0x00);
39
- }
40
-
41
- _writeReg(reg, value) {
42
- this._transport.write(Buffer.from([reg, value]));
43
- }
44
-
45
- _readReg(reg, len) {
46
- return this._transport.writeRead(Buffer.from([reg]), len);
35
+ this._busType = busType;
36
+ this._mode = 0;
37
+ this._osrsT = 1;
38
+ this._osrsP = 1;
39
+ this._filter = 0;
40
+ this._tSb = 0;
41
+ this._tFine = 0;
42
+ this._readCalibration();
43
+ this._writeReg(_REG_CTRL_MEAS, (1 << 5) | (1 << 2) | 0);
44
+ this._writeReg(_REG_CONFIG, 0);
47
45
  }
48
46
 
49
- _writeCtrlMeas(value) {
50
- this._writeReg(_REG_CTRL_MEAS, value);
47
+ _readCalibration() {
48
+ const data = this._transport.writeRead(Buffer.from([_REG_CAL_START]), 24);
49
+ this._digT1 = data.readUInt16LE(0);
50
+ this._digT2 = data.readInt16LE(2);
51
+ this._digT3 = data.readInt16LE(4);
52
+ this._digP1 = data.readUInt16LE(6);
53
+ this._digP2 = data.readInt16LE(8);
54
+ this._digP3 = data.readInt16LE(10);
55
+ this._digP4 = data.readInt16LE(12);
56
+ this._digP5 = data.readInt16LE(14);
57
+ this._digP6 = data.readInt16LE(16);
58
+ this._digP7 = data.readInt16LE(18);
59
+ this._digP8 = data.readInt16LE(20);
60
+ this._digP9 = data.readInt16LE(22);
51
61
  }
52
62
 
53
- _writeConfig(value) {
54
- this._writeReg(_REG_CONFIG, value);
63
+ _writeReg(reg, value) {
64
+ const addr = this._busType === 'spi' ? (reg & 0x7F) : reg;
65
+ this._transport.write(Buffer.from([addr, value]));
55
66
  }
56
67
 
57
- _loadCalibration() {
58
- const raw = this._readReg(_REG_CAL_START, 24);
59
- this._dig_T1 = raw.readUInt16LE(0);
60
- this._dig_T2 = raw.readInt16LE(2);
61
- this._dig_T3 = raw.readInt16LE(4);
62
- this._dig_P1 = raw.readUInt16LE(6);
63
- this._dig_P2 = raw.readInt16LE(8);
64
- this._dig_P3 = raw.readInt16LE(10);
65
- this._dig_P4 = raw.readInt16LE(12);
66
- this._dig_P5 = raw.readInt16LE(14);
67
- this._dig_P6 = raw.readInt16LE(16);
68
- this._dig_P7 = raw.readInt16LE(18);
69
- this._dig_P8 = raw.readInt16LE(20);
70
- this._dig_P9 = raw.readInt16LE(22);
68
+ _readReg(reg, n) {
69
+ return this._transport.writeRead(Buffer.from([reg]), n);
71
70
  }
72
71
 
73
- _triggerReadBurst() {
74
- const raw = this._readReg(_REG_DATA, 6);
75
- const adc_P = (raw[0] << 12) | (raw[1] << 4) | (raw[2] >> 4);
76
- const adc_T = (raw[3] << 12) | (raw[4] << 4) | (raw[5] >> 4);
77
- return [adc_T, adc_P];
72
+ _triggerAndRead() {
73
+ if (this._mode !== 3) {
74
+ const ctrl = (this._osrsT << 5) | (this._osrsP << 2) | 1;
75
+ this._writeReg(_REG_CTRL_MEAS, ctrl);
76
+ _delay(_MEAS_TIME_MS);
77
+ }
78
+ const raw = this._readReg(_REG_DATA_START, 6);
79
+ const adcP = (raw[0] << 12) | (raw[1] << 4) | (raw[2] >> 4);
80
+ const adcT = (raw[3] << 12) | (raw[4] << 4) | (raw[5] >> 4);
81
+ return { adcP, adcT };
78
82
  }
79
83
 
80
- _compensateTemp(adc_T) {
81
- const T1 = BigInt(this._dig_T1);
82
- const T2 = BigInt(this._dig_T2);
83
- const T3 = BigInt(this._dig_T3);
84
- const t = BigInt(adc_T);
85
- const var1 = (((t >> 3n) - (T1 << 1n)) * T2) >> 11n;
86
- const var2 = (((((t >> 4n) - T1) * ((t >> 4n) - T1)) >> 12n) * T3) >> 14n;
87
- this._t_fine = Number(var1 + var2);
88
- return Number(((var1 + var2) * 5n + 128n) >> 8n) / 100.0;
84
+ _compensateTemp(adcT) {
85
+ const digT1 = this._digT1;
86
+ const digT2 = this._digT2;
87
+ const digT3 = this._digT3;
88
+ let var1 = (((adcT >> 3) - (digT1 << 1)) * digT2) >> 11;
89
+ let var2 = (((((adcT >> 4) - digT1) * ((adcT >> 4) - digT1)) >> 12) * digT3) >> 14;
90
+ this._tFine = var1 + var2;
91
+ return ((this._tFine * 5 + 128) >> 8) / 100.0;
89
92
  }
90
93
 
91
- _compensatePressure(adc_P) {
92
- const tf = BigInt(this._t_fine !== null ? this._t_fine : 0);
93
- const P1 = BigInt(this._dig_P1), P2 = BigInt(this._dig_P2), P3 = BigInt(this._dig_P3);
94
- const P4 = BigInt(this._dig_P4), P5 = BigInt(this._dig_P5), P6 = BigInt(this._dig_P6);
95
- const P7 = BigInt(this._dig_P7), P8 = BigInt(this._dig_P8), P9 = BigInt(this._dig_P9);
96
- let var1 = tf - 128000n;
97
- let var2 = var1 * var1 * P6;
98
- var2 = var2 + ((var1 * P5) << 17n);
99
- var2 = var2 + (P4 << 35n);
100
- var1 = ((var1 * var1 * P3) >> 8n) + ((var1 * P2) << 12n);
101
- var1 = (((1n << 47n) + var1) * P1) >> 33n;
94
+ _compensatePressure(adcP) {
95
+ const tFine = BigInt(this._tFine);
96
+ const digP1 = BigInt(this._digP1);
97
+ const digP2 = BigInt(this._digP2);
98
+ const digP3 = BigInt(this._digP3);
99
+ const digP4 = BigInt(this._digP4);
100
+ const digP5 = BigInt(this._digP5);
101
+ const digP6 = BigInt(this._digP6);
102
+ const digP7 = BigInt(this._digP7);
103
+ const digP8 = BigInt(this._digP8);
104
+ const digP9 = BigInt(this._digP9);
105
+
106
+ let var1 = tFine - 128000n;
107
+ let var2 = var1 * var1 * digP6;
108
+ var2 = var2 + ((var1 * digP5) << 17n);
109
+ var2 = var2 + (digP4 << 35n);
110
+ var1 = ((var1 * var1 * digP3) >> 8n) + ((var1 * digP2) << 12n);
111
+ var1 = (((1n << 47n) + var1) * digP1) >> 33n;
102
112
  if (var1 === 0n) return 0.0;
103
- let p = BigInt(1048576) - BigInt(adc_P);
113
+ let p = 1048576n - BigInt(adcP);
104
114
  p = (((p << 31n) - var2) * 3125n) / var1;
105
- var1 = (P9 * (p >> 13n) * (p >> 13n)) >> 25n;
106
- var2 = (P8 * p) >> 19n;
107
- p = ((p + var1 + var2) >> 8n) + (P7 << 4n);
108
- return Number(p) / 25600.0;
109
- }
110
-
111
- _triggerMeasurement() {
112
- this._writeCtrlMeas(this._ctrlMeasCache);
113
- _delay(7);
115
+ var1 = (digP9 * (p >> 13n) * (p >> 13n)) >> 25n;
116
+ var2 = (digP8 * p) >> 19n;
117
+ p = ((p + var1 + var2) >> 8n) + (digP7 << 4n);
118
+ return Number(p) / 256.0 / 100.0;
114
119
  }
115
120
 
116
121
  /**
117
122
  * Read calibrated temperature.
118
- *
119
- * Triggers a forced-mode conversion and returns temperature in °C.
120
- * Caches t_fine for use in subsequent pressure() calls.
121
- *
122
123
  * @returns {number} Temperature in degrees Celsius.
123
124
  */
124
125
  temperature() {
125
- this._triggerMeasurement();
126
- const [adc_T] = this._triggerReadBurst();
127
- return this._compensateTemp(adc_T);
126
+ const { adcP, adcT } = this._triggerAndRead();
127
+ return this._compensateTemp(adcT);
128
128
  }
129
129
 
130
130
  /**
131
131
  * Read calibrated pressure.
132
132
  *
133
- * Triggers a forced-mode conversion and returns pressure in hPa.
134
- * Re-reads the temperature ADC alongside pressure to refresh t_fine.
133
+ * Reads both ADCs and refreshes t_fine.
134
+ * Self-contained — may be called without a prior temperature() call.
135
135
  *
136
136
  * @returns {number} Pressure in hPa.
137
137
  */
138
138
  pressure() {
139
- this._triggerMeasurement();
140
- const [adc_T, adc_P] = this._triggerReadBurst();
141
- this._compensateTemp(adc_T);
142
- return this._compensatePressure(adc_P);
139
+ const { adcP, adcT } = this._triggerAndRead();
140
+ this._compensateTemp(adcT);
141
+ return this._compensatePressure(adcP);
143
142
  }
144
143
  }
145
144
 
146
145
  /**
147
146
  * BMP280 full interface — extends BMP280Minimal with configuration and altitude helpers.
148
147
  *
149
- * Adds power-mode control, oversampling settings, IIR filter, standby time,
150
- * status read, altitude / sea-level helpers, chip_id, and reset.
151
- *
152
- * @param {object} transport - Configured I²C transport.
153
- * @param {number} [addr=0x76] - 7-bit I²C address.
154
- * @param {number} [osrs_t=1] - Temperature oversampling 0–5 (default ×1).
155
- * @param {number} [osrs_p=1] - Pressure oversampling 0–5 (default ×1).
156
- * @param {number} [mode=1] - Power mode (default forced).
157
- * @param {number} [filter=0] - IIR filter coefficient (default off).
158
- * @param {number} [t_sb=0] - Standby time in normal mode 0–7 (default 0.5 ms).
148
+ * @param {object} transport - Configured I²C or SPI transport.
149
+ * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
159
150
  */
160
151
  class BMP280Full extends BMP280Minimal {
161
- static OSRS_SKIP = 0;
162
- static OSRS_X1 = 1;
163
- static OSRS_X2 = 2;
164
- static OSRS_X4 = 3;
165
- static OSRS_X8 = 4;
166
- static OSRS_X16 = 5;
167
-
168
- static MODE_SLEEP = 0;
169
- static MODE_FORCED = 1;
170
- static MODE_NORMAL = 3;
171
-
172
- static FILTER_OFF = 0;
173
- static FILTER_2 = 1;
174
- static FILTER_4 = 2;
175
- static FILTER_8 = 3;
176
- static FILTER_16 = 4;
152
+ static OSRS_SKIP = 0;
153
+ static OSRS_X1 = 1;
154
+ static OSRS_X2 = 2;
155
+ static OSRS_X4 = 3;
156
+ static OSRS_X8 = 4;
157
+ static OSRS_X16 = 5;
158
+
159
+ static MODE_SLEEP = 0;
160
+ static MODE_FORCED = 1;
161
+ static MODE_NORMAL = 3;
162
+
163
+ static FILTER_OFF = 0;
164
+ static FILTER_2 = 1;
165
+ static FILTER_4 = 2;
166
+ static FILTER_8 = 3;
167
+ static FILTER_16 = 4;
177
168
 
178
169
  static T_SB_0_5_MS = 0;
179
170
  static T_SB_62_5_MS = 1;
@@ -187,102 +178,77 @@ class BMP280Full extends BMP280Minimal {
187
178
  static STATUS_MEASURING = 0x08;
188
179
  static STATUS_IM_UPDATE = 0x01;
189
180
 
190
- constructor(transport, addr = 0x76, osrs_t = 1, osrs_p = 1, mode = 1, filter = 0, t_sb = 0) {
191
- super(transport, addr);
192
- this._osrs_t = osrs_t;
193
- this._osrs_p = osrs_p;
194
- this._mode = mode;
195
- this._filter = filter;
196
- this._t_sb = t_sb;
197
- this._ctrlMeasCache = this._ctrlMeasValue();
198
- this._writeCtrlMeas(this._ctrlMeasCache);
199
- this._writeConfig(this._configValue());
200
- }
201
-
202
- _ctrlMeasValue() {
203
- return (this._osrs_t << 5) | (this._osrs_p << 2) | this._mode;
204
- }
205
-
206
- _configValue() {
207
- return (this._t_sb << 5) | (this._filter << 2);
181
+ constructor(transport, busType = 'i2c') {
182
+ super(transport, busType);
208
183
  }
209
184
 
210
185
  /**
211
- * Update chip configuration.
212
- *
213
- * @param {number} [osrs_t] - Temperature oversampling 0–5.
214
- * @param {number} [osrs_p] - Pressure oversampling 0–5.
215
- * @param {number} [mode] - Power mode (0=sleep, 1=forced, 3=normal).
216
- * @param {number} [filter] - IIR filter coefficient (0=off, 1, 2, 3, 4=×16).
217
- * @param {number} [t_sb] - Standby time in normal mode (0–7).
186
+ * Write both ctrl_meas and config registers.
187
+ * @param {number} osrsT - Temperature oversampling (0–5).
188
+ * @param {number} osrsP - Pressure oversampling (0–5).
189
+ * @param {number} mode - Power mode (0=sleep, 1=forced, 3=normal).
190
+ * @param {number} filter - IIR filter coefficient (0–4).
191
+ * @param {number} tSb - Standby time in normal mode (0–7).
218
192
  */
219
- configure(osrs_t, osrs_p, mode, filter, t_sb) {
220
- if (osrs_t !== undefined) this._osrs_t = osrs_t;
221
- if (osrs_p !== undefined) this._osrs_p = osrs_p;
222
- if (mode !== undefined) this._mode = mode;
223
- if (filter !== undefined) this._filter = filter;
224
- if (t_sb !== undefined) this._t_sb = t_sb;
225
- this._ctrlMeasCache = this._ctrlMeasValue();
226
- this._writeCtrlMeas(this._ctrlMeasCache);
227
- this._writeConfig(this._configValue());
193
+ configure(osrsT, osrsP, mode, filter, tSb) {
194
+ this._osrsT = osrsT;
195
+ this._osrsP = osrsP;
196
+ this._mode = mode;
197
+ this._filter = filter;
198
+ this._tSb = tSb;
199
+ this._writeReg(_REG_CONFIG, (tSb << 5) | (filter << 2));
200
+ this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | mode);
228
201
  }
229
202
 
230
203
  /**
231
- * Update oversampling settings.
232
- *
233
- * @param {number} osrs_t - Temperature oversampling 0–5.
234
- * @param {number} osrs_p - Pressure oversampling 0–5.
204
+ * Update temperature and pressure oversampling.
205
+ * @param {number} osrsT - Temperature oversampling (0–5).
206
+ * @param {number} osrsP - Pressure oversampling (0–5).
235
207
  */
236
- setOversampling(osrs_t, osrs_p) {
237
- this._osrs_t = osrs_t;
238
- this._osrs_p = osrs_p;
239
- this._ctrlMeasCache = this._ctrlMeasValue();
240
- this._writeCtrlMeas(this._ctrlMeasCache);
208
+ setOversampling(osrsT, osrsP) {
209
+ this._osrsT = osrsT;
210
+ this._osrsP = osrsP;
211
+ this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | this._mode);
241
212
  }
242
213
 
243
214
  /**
244
215
  * Update power mode.
245
- *
246
- * @param {number} mode - 0=sleep, 1=forced, 3=normal.
216
+ * @param {number} mode - Power mode (0=sleep, 1=forced, 3=normal).
247
217
  */
248
218
  setMode(mode) {
249
219
  this._mode = mode;
250
- this._ctrlMeasCache = this._ctrlMeasValue();
251
- this._writeCtrlMeas(this._ctrlMeasCache);
220
+ this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | mode);
252
221
  }
253
222
 
254
223
  /**
255
224
  * Update IIR filter coefficient.
256
- *
257
- * @param {number} coeff - 0=off, 1=×2, 2=×4, 3=×8, 4=×16.
225
+ * @param {number} coeff - Filter coefficient (0–4).
258
226
  */
259
227
  setFilter(coeff) {
260
228
  this._filter = coeff;
261
- this._writeConfig(this._configValue());
229
+ this._writeReg(_REG_CONFIG, (this._tSb << 5) | (coeff << 2));
262
230
  }
263
231
 
264
232
  /**
265
- * Update standby time (only relevant in normal mode).
266
- *
267
- * @param {number} t_sb - 0=0.5ms, 1=62.5ms, 2=125ms, 3=250ms, 4=500ms, 5=1s, 6=2s, 7=4s.
233
+ * Update standby time for normal mode.
234
+ * @param {number} tSb - Standby time value (0–7).
268
235
  */
269
- setStandby(t_sb) {
270
- this._t_sb = t_sb;
271
- this._writeConfig(this._configValue());
236
+ setStandby(tSb) {
237
+ this._tSb = tSb;
238
+ this._writeReg(_REG_CONFIG, (tSb << 5) | (this._filter << 2));
272
239
  }
273
240
 
274
241
  /**
275
- * Read status register.
276
- *
277
- * @returns {number} Status byte; check STATUS_MEASURING and STATUS_IM_UPDATE bits.
242
+ * Read the status register.
243
+ * @returns {number} Status byte; bit 3 = measuring, bit 0 = im_update.
278
244
  */
279
245
  status() {
280
- return this._readReg(_REG_STATUS, 1)[0];
246
+ const data = this._readReg(_REG_STATUS, 1);
247
+ return data[0];
281
248
  }
282
249
 
283
250
  /**
284
- * Compute altitude above sea level from current pressure.
285
- *
251
+ * Compute altitude above sea level from the current pressure.
286
252
  * @param {number} [seaLevelHpa=1013.25] - Reference sea-level pressure in hPa.
287
253
  * @returns {number} Altitude in metres.
288
254
  */
@@ -292,8 +258,7 @@ class BMP280Full extends BMP280Minimal {
292
258
  }
293
259
 
294
260
  /**
295
- * Compute sea-level pressure for a known altitude.
296
- *
261
+ * Compute sea-level pressure from current pressure and known altitude.
297
262
  * @param {number} altitudeM - Altitude in metres.
298
263
  * @returns {number} Sea-level pressure in hPa.
299
264
  */
@@ -303,26 +268,24 @@ class BMP280Full extends BMP280Minimal {
303
268
  }
304
269
 
305
270
  /**
306
- * Read chip ID register.
307
- *
308
- * @returns {number} Chip ID; expect 0x58 for BMP280.
271
+ * Read the chip ID register.
272
+ * @returns {number} Chip ID; expect 0x58.
309
273
  */
310
274
  chipId() {
311
- return this._readReg(_REG_ID, 1)[0];
275
+ const data = this._readReg(_REG_ID, 1);
276
+ return data[0];
312
277
  }
313
278
 
314
279
  /**
315
- * Perform soft reset and re-read calibration coefficients.
316
- *
317
- * Re-applies the current ctrl_meas and config settings.
280
+ * Perform a soft reset, re-read calibration, and re-apply configuration.
318
281
  */
319
282
  reset() {
320
283
  this._writeReg(_REG_RESET, _RESET_CMD);
321
284
  _delay(2);
322
- this._loadCalibration();
323
- this._writeCtrlMeas(this._ctrlMeasValue());
324
- this._writeConfig(this._configValue());
285
+ this._readCalibration();
286
+ this._writeReg(_REG_CONFIG, (this._tSb << 5) | (this._filter << 2));
287
+ this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | this._mode);
325
288
  }
326
289
  }
327
290
 
328
- module.exports = { BMP280Minimal, BMP280Full };
291
+ module.exports = { BMP280Minimal, BMP280Full };
@@ -0,0 +1,105 @@
1
+ 'use strict';
2
+
3
+ const Gpio = require('onoff').Gpio;
4
+
5
+ /**
6
+ * HX711 GPIO bit-bang transport for Node.js (wraps onoff Gpio).
7
+ *
8
+ * Implements the 2-wire bit-bang protocol used exclusively by the HX711
9
+ * 24-bit ADC. DOUT is sampled on each falling edge of PD_SCK; the pulse
10
+ * count selects the channel and gain for the next conversion.
11
+ *
12
+ * The DOUT poll loop uses a synchronous spin (readSync) which is acceptable
13
+ * for short waits during the clock cycle itself. The blocking wait before the
14
+ * cycle uses setImmediate yielding to avoid monopolising the event loop.
15
+ */
16
+ class HX711Transport {
17
+ /**
18
+ * @param {object} dout - onoff Gpio instance configured as 'in'.
19
+ * @param {object} pd_sck - onoff Gpio instance configured as 'out'.
20
+ */
21
+ constructor(dout, pd_sck) {
22
+ this._dout = dout;
23
+ this._sck = pd_sck;
24
+ this._sck.writeSync(0);
25
+ }
26
+
27
+ /**
28
+ * Return true if a conversion result is available (DOUT is LOW).
29
+ *
30
+ * Non-blocking.
31
+ *
32
+ * @returns {boolean} True when DOUT is LOW (data ready).
33
+ */
34
+ isReady() {
35
+ return this._dout.readSync() === 0;
36
+ }
37
+
38
+ /**
39
+ * Wait up to 1 s for data ready, then clock out a conversion.
40
+ *
41
+ * Polls DOUT until LOW (conversion ready), then sends exactly numPulses
42
+ * PD_SCK pulses, sampling DOUT at each falling edge (HIGH→LOW transition).
43
+ * Leaves PD_SCK LOW after the last pulse. The pulse count programs the
44
+ * channel and gain for the next conversion:
45
+ * 25 → Channel A Gain 128, 26 → Channel B Gain 32, 27 → Channel A Gain 64.
46
+ *
47
+ * @param {number} numPulses - Number of PD_SCK pulses (must be 25, 26, or 27).
48
+ * @returns {number} Signed 24-bit ADC value.
49
+ * @throws {Error} If numPulses is not 25, 26, or 27, or DOUT stays HIGH for >1 s.
50
+ */
51
+ readRaw(numPulses = 25) {
52
+ if (numPulses !== 25 && numPulses !== 26 && numPulses !== 27)
53
+ throw new Error('numPulses must be 25, 26, or 27');
54
+ const deadline = Date.now() + 1000;
55
+ while (this._dout.readSync() !== 0) {
56
+ if (Date.now() >= deadline)
57
+ throw new Error('HX711 DOUT did not go low within 1 second');
58
+ const end = Date.now() + 1;
59
+ while (Date.now() < end) {}
60
+ }
61
+ const endOf = (us) => process.hrtime.bigint() + BigInt(us * 1000);
62
+ let raw = 0;
63
+ for (let i = 0; i < numPulses; i++) {
64
+ this._sck.writeSync(1);
65
+ let t = endOf(1); while (process.hrtime.bigint() < t) {}
66
+ this._sck.writeSync(0);
67
+ t = endOf(1); while (process.hrtime.bigint() < t) {}
68
+ raw = (raw << 1) | this._dout.readSync();
69
+ }
70
+ raw >>>= numPulses - 24;
71
+ if (raw >= 0x800000) raw -= 0x1000000;
72
+ return raw;
73
+ }
74
+
75
+ /**
76
+ * Enter power-down mode by holding PD_SCK HIGH for >60 µs.
77
+ *
78
+ * Uses a busy-spin for the delay since Node.js has no µs sleep.
79
+ */
80
+ powerDown() {
81
+ this._sck.writeSync(1);
82
+ const end = Date.now() + 1; // 1 ms >> 60 µs, safe margin
83
+ while (Date.now() < end) {}
84
+ }
85
+
86
+ /**
87
+ * Exit power-down mode and reset the chip.
88
+ *
89
+ * Drives PD_SCK LOW. The chip resets to Channel A, Gain 128. The first
90
+ * conversion after power-up must be discarded.
91
+ */
92
+ powerUp() {
93
+ this._sck.writeSync(0);
94
+ }
95
+
96
+ /**
97
+ * Release both GPIO pins. Must be called when the transport is no longer needed.
98
+ */
99
+ close() {
100
+ this._dout.unexport();
101
+ this._sck.unexport();
102
+ }
103
+ }
104
+
105
+ module.exports = { HX711Transport };
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes