periph 1.5.3 → 1.6.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 (79) hide show
  1. package/README.md +18 -0
  2. package/examples/color/isl29125/complete.js +57 -0
  3. package/examples/color/isl29125/demo.js +77 -0
  4. package/examples/color/isl29125/minimal.js +23 -0
  5. package/examples/environmental/bme688/complete.js +48 -0
  6. package/examples/environmental/bme688/demo.js +47 -0
  7. package/examples/environmental/bme688/minimal.js +21 -0
  8. package/examples/gas/ccs811/complete.js +87 -0
  9. package/examples/gas/ccs811/demo.js +108 -0
  10. package/examples/gas/ccs811/minimal.js +20 -0
  11. package/examples/gas/rrh46410/complete.js +78 -0
  12. package/examples/gas/rrh46410/demo.js +86 -0
  13. package/examples/gas/rrh46410/minimal.js +21 -0
  14. package/examples/humidity/hts221/complete.js +83 -0
  15. package/examples/humidity/hts221/demo.js +94 -0
  16. package/examples/humidity/hts221/minimal.js +22 -0
  17. package/examples/humidity/htu31d/complete.js +50 -0
  18. package/examples/humidity/htu31d/demo.js +65 -0
  19. package/examples/humidity/htu31d/minimal.js +22 -0
  20. package/examples/humidity/shtc3/complete.js +48 -0
  21. package/examples/humidity/shtc3/demo.js +45 -0
  22. package/examples/humidity/shtc3/minimal.js +22 -0
  23. package/examples/led/saa1064/complete.js +51 -0
  24. package/examples/led/saa1064/demo.js +54 -0
  25. package/examples/led/saa1064/minimal.js +23 -0
  26. package/examples/light/apds9250/complete.js +71 -0
  27. package/examples/light/apds9250/demo.js +79 -0
  28. package/examples/light/apds9250/minimal.js +23 -0
  29. package/examples/light/bh1750/complete.js +46 -0
  30. package/examples/light/bh1750/demo.js +52 -0
  31. package/examples/light/bh1750/minimal.js +22 -0
  32. package/examples/light/si1132/complete.js +70 -0
  33. package/examples/light/si1132/demo.js +73 -0
  34. package/examples/light/si1132/minimal.js +22 -0
  35. package/examples/light/si1133/complete.js +89 -0
  36. package/examples/light/si1133/demo.js +93 -0
  37. package/examples/light/si1133/minimal.js +22 -0
  38. package/examples/light/si1145/complete.js +93 -0
  39. package/examples/light/si1145/demo.js +98 -0
  40. package/examples/light/si1145/minimal.js +22 -0
  41. package/examples/pressure/mpl3115a2/complete.js +49 -0
  42. package/examples/pressure/mpl3115a2/demo.js +48 -0
  43. package/examples/pressure/mpl3115a2/minimal.js +22 -0
  44. package/examples/pressure/ms5607/complete.js +49 -0
  45. package/examples/pressure/ms5607/demo.js +65 -0
  46. package/examples/pressure/ms5607/minimal.js +22 -0
  47. package/examples/pressure/ms5611/complete.js +49 -0
  48. package/examples/pressure/ms5611/demo.js +65 -0
  49. package/examples/pressure/ms5611/minimal.js +22 -0
  50. package/examples/pressure/spl06/complete.js +81 -0
  51. package/examples/pressure/spl06/demo.js +106 -0
  52. package/examples/pressure/spl06/minimal.js +22 -0
  53. package/examples/rfid/pn532/complete.js +99 -0
  54. package/examples/rfid/pn532/demo.js +121 -0
  55. package/examples/rfid/pn532/minimal.js +22 -0
  56. package/package.json +1 -1
  57. package/src/chips/color/isl29125.js +379 -0
  58. package/src/chips/environmental/bme688.js +573 -0
  59. package/src/chips/gas/ccs811.js +400 -0
  60. package/src/chips/gas/rrh46410.js +534 -0
  61. package/src/chips/humidity/hts221.js +369 -0
  62. package/src/chips/humidity/htu31d.js +242 -0
  63. package/src/chips/humidity/shtc3.js +239 -0
  64. package/src/chips/led/saa1064.js +241 -0
  65. package/src/chips/light/apds9250.js +431 -0
  66. package/src/chips/light/bh1750.js +196 -0
  67. package/src/chips/light/si1132.js +416 -0
  68. package/src/chips/light/si1133.js +583 -0
  69. package/src/chips/light/si1145.js +630 -0
  70. package/src/chips/pressure/mpl3115a2.js +459 -0
  71. package/src/chips/pressure/ms5607.js +290 -0
  72. package/src/chips/pressure/ms5611.js +290 -0
  73. package/src/chips/pressure/spl06.js +461 -0
  74. package/src/chips/rfid/pn532.js +513 -0
  75. package/src/connection/i2c.js +46 -13
  76. package/src/connection/smbus.js +21 -8
  77. package/src/discovery/registry.js +18 -18
  78. package/examples/gpio/.gitkeep +0 -0
  79. package/src/chips/gpio/.gitkeep +0 -0
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { RRH46410Full } = require('../../../src/chips/gas/rrh46410');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x38', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x38) → Connection
11
+ // The module has the fixed address 0x38 and stretches the I²C clock.
12
+ const sensor = new RRH46410Full(connection); // Create RRH46410 driver, (connection) → void
13
+ // Constructor stores the connection only; no bus traffic yet.
14
+
15
+ async function main() {
16
+ await sensor.init(); // Check product ID, start IAQ 2nd Gen mode, () → void
17
+ // Retries Get Product ID for up to 1 s, verifies 0x2310, selects IAQ 2nd Gen (3 s samples).
18
+
19
+ console.log(await sensor.productId()); // Product ID and firmware, () → {productId, firmware: [major, minor, patch]}
20
+ // The product ID is always 0x2310.
21
+ console.log(await sensor.trackingNumber()); // ASIC tracking number, () → Buffer (6)
22
+ // Unique per module.
23
+ console.log(await sensor.getMode()); // Operation mode, () → number
24
+ // 1 = IAQ 2nd Gen after init.
25
+
26
+ await sensor.setHumidity(45.0); // Ambient humidity, (percent %RH) → void
27
+ // Sent as round(percent / 100 × 255); the module assumes 50 %RH otherwise.
28
+ console.log(await sensor.read()); // Next sample, () → {iaq, tvoc_mg_m3 mg/m³, etoh_ppm ppm, eco2_ppm ppm, relative_iaq, sample, warmed_up}
29
+ // Waits for the sample counter to change; values are fixed during the warm-up.
30
+ console.log(await sensor.waitForSample(4.0)); // Wait for a new sample, (timeout s) → boolean
31
+ // True when the counter changed (or INT went high) within the timeout.
32
+ console.log(await sensor.readResults()); // Latest result, () → object|null
33
+ // Does not wait; null before the first sample of the mode.
34
+ console.log(RRH46410Full.airQualityLabel(2.4)); // UBA rating, (iaq) → string
35
+ // "very good", "good", "medium", "poor" or "bad".
36
+ console.log(sensor.samplePeriod(), sensor.warmupSamples()); // Mode timing, (mode=null) → number s / number
37
+ // 3 s and 100 samples in IAQ 2nd Gen mode.
38
+ console.log(sensor.status()); // Last status byte, () → object
39
+ // Error bits and the operation-mode error code (0xA = sensor damaged).
40
+
41
+ await sensor.onInterrupt((r) => console.log('sample', r.sample, r.iaq)); // Subscribe, (callback, intPin=null) → void
42
+ // Rising edge on INT, or a 200 ms poll comparing the sample counter.
43
+ await sleep(7000);
44
+ await sensor.offInterrupt(); // Unsubscribe, () → void
45
+ // Stops the pin handler or polling timer.
46
+ console.log(await sensor.pollInterrupt()); // New sample pending, () → boolean
47
+ // INT level if wired, otherwise a counter comparison.
48
+
49
+ await sensor.setMode(RRH46410Full.MODE_PBAQ); // Operation mode, (mode) → void
50
+ // PBAQ: TVOC and ethanol only, every 5 s; restarts the warm-up.
51
+ console.log(await sensor.read()); // Next sample, () → {tvoc_mg_m3, etoh_ppm, sample, warmed_up}
52
+ // PBAQ results have a finer resolution (raw / 1000).
53
+ await sensor.setMode(RRH46410Full.MODE_ULP); // Operation mode, (mode) → void
54
+ // ULP: one sample every 90 s at 0.23 mW.
55
+ console.log(sensor.samplePeriod(), sensor.warmupSamples()); // Mode timing, (mode=null) → number s / number
56
+ // 90 s and 10 samples.
57
+
58
+ await sensor.configGpio(0x03); // GPIO directions, (directions) → void
59
+ // Bit 0 = GPIO1 output, bit 1 = GPIO2 output.
60
+ await sensor.setGpio(0x01); // Drive high, (mask) → void
61
+ // GPIO1 high.
62
+ console.log((await sensor.getGpio()).toString(2)); // GPIO levels, () → number
63
+ // Bits 0–3 = GPIO1–GPIO4.
64
+ await sensor.clearGpio(0x01); // Drive low, (mask) → void
65
+ // GPIO1 low.
66
+
67
+ console.log(await sensor.sendCommand(0x10, [], 1)); // Raw request, (command, params=[], responseLen=0) → Buffer
68
+ // Checksums added and verified; here Get Operation Mode.
69
+ // await sensor.cleanSensor(true); // One-time cleaning, (confirm=false) → void
70
+ // Runs for one minute, once in the sensor's lifetime; left disabled here.
71
+ await sensor.suspend(); // Suspend mode, () → void
72
+ // Stops measuring, about 0.7 µA.
73
+ await sensor.reset(); // Reset, () → void
74
+ // Waits 500 ms and verifies the product ID; the module restarts in SUSPEND.
75
+ await connection.close();
76
+ }
77
+
78
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,86 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { RRH46410Full } = require('../../../src/chips/gas/rrh46410');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x38', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x38) → Connection
11
+ const sensor = new RRH46410Full(connection); // Create RRH46410 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Check product ID, start IAQ 2nd Gen mode, () → void
15
+
16
+ // --- Identity ---
17
+ // Get Product ID returns 0x2310 and the firmware version; the tracking number
18
+ // identifies the individual module.
19
+ const { productId, firmware } = await sensor.productId(); // Product ID and firmware, () → {productId, firmware}
20
+ console.log('product 0x%s, firmware %s', productId.toString(16), firmware.join('.'));
21
+ console.log('tracking', (await sensor.trackingNumber()).toString('hex')); // Tracking number, () → Buffer
22
+ console.log('mode', await sensor.getMode()); // Operation mode, () → number
23
+
24
+ // --- Humidity compensation ---
25
+ // The MOx element reacts to humidity; feeding the real value (e.g. from a
26
+ // humidity sensor) improves the results. The module assumes 50 %RH.
27
+ await sensor.setHumidity(42.0); // Ambient humidity, (percent %RH) → void
28
+
29
+ // --- Indoor air-quality monitor ---
30
+ // A new sample arrives every 3 s; the first 100 samples (about 5 min) are the
31
+ // warm-up during which the values stay fixed.
32
+ const warmup = sensor.warmupSamples(); // Warm-up length, (mode=null) → number samples
33
+ console.log('sample period %d s, warm-up %d samples', sensor.samplePeriod(), warmup); // Sample period, (mode=null) → number s
34
+ for (let i = 0; i < 20; i++) {
35
+ const r = await sensor.read(); // Next sample, () → object
36
+ const progress = r.warmed_up ? 'ok' : `warm-up ${Math.min(100, Math.floor(r.sample * 100 / warmup))}%`;
37
+ console.log('IAQ %s (%s) TVOC %s mg/m3 EtOH %s ppm eCO2 %d ppm rel %d %s', r.iaq.toFixed(1),
38
+ sensor.airQualityLabel(r.iaq), r.tvoc_mg_m3.toFixed(2), // UBA rating, (iaq) → string
39
+ r.etoh_ppm.toFixed(2), r.eco2_ppm, r.relative_iaq, progress);
40
+ }
41
+
42
+ // --- Interrupt instead of polling ---
43
+ // With INT wired the driver reacts to its rising edge; otherwise it polls the
44
+ // sample counter. The callback receives the decoded result.
45
+ await sensor.onInterrupt((r) => console.log('INT sample %d IAQ %s', r.sample, r.iaq.toFixed(1))); // Subscribe, (callback, intPin=null) → void
46
+ await sleep(10000);
47
+ await sensor.offInterrupt(); // Unsubscribe, () → void
48
+ while (!(await sensor.pollInterrupt())) await sleep(500); // New sample pending, () → boolean
49
+ if (await sensor.waitForSample(4.0)) { // Wait for a new sample, (timeout s) → boolean
50
+ console.log('latest', await sensor.readResults()); // Latest result, () → object|null
51
+ }
52
+
53
+ // --- Other operation modes ---
54
+ // PBAQ reports TVOC and ethanol for public buildings; ULP saves power with
55
+ // one sample every 90 s. Each mode change restarts the warm-up.
56
+ for (const [mode, name] of [[RRH46410Full.MODE_PBAQ, 'PBAQ'], [RRH46410Full.MODE_ULP, 'ULP']]) {
57
+ await sensor.setMode(mode); // Operation mode, (mode) → void
58
+ console.log('%s: sample every %d s, warm-up %d samples (%d min)', name, sensor.samplePeriod(),
59
+ sensor.warmupSamples(), sensor.samplePeriod() * sensor.warmupSamples() / 60); // Mode timing, (mode=null) → number
60
+ }
61
+ await sensor.setMode(RRH46410Full.MODE_PBAQ); // Operation mode, (mode) → void
62
+ console.log('PBAQ', await sensor.read()); // Next sample, () → {tvoc_mg_m3, etoh_ppm, sample, warmed_up}
63
+
64
+ // --- GPIOs ---
65
+ // GPIO1 and GPIO2 can drive an indicator LED or a fan relay.
66
+ await sensor.configGpio(0x03); // Both outputs, (directions) → void
67
+ for (let i = 0; i < 3; i++) {
68
+ await sensor.setGpio(0x01); // GPIO1 high, (mask) → void
69
+ await sleep(200);
70
+ await sensor.clearGpio(0x01); // GPIO1 low, (mask) → void
71
+ await sleep(200);
72
+ }
73
+ console.log('GPIO levels', (await sensor.getGpio()).toString(2)); // GPIO levels, () → number
74
+ console.log('status', sensor.status()); // Last status byte, () → object
75
+ console.log('raw mode', (await sensor.sendCommand(0x10, [], 1))[0]); // Raw request, (command, params, responseLen) → Buffer
76
+
77
+ // --- Wrap up ---
78
+ // cleanSensor(true) runs the 1-minute cleaning once in the sensor's lifetime;
79
+ // it is skipped here. Suspend stops measuring; reset restarts the module in SUSPEND.
80
+ await sensor.suspend(); // Suspend mode, () → void
81
+ await sensor.reset(); // Reset, () → void
82
+ console.log('mode after reset', await sensor.getMode()); // Operation mode, () → number
83
+ await connection.close();
84
+ }
85
+
86
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { RRH46410Minimal } = require('../../../src/chips/gas/rrh46410');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x38', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x38) → Connection
10
+ const sensor = new RRH46410Minimal(connection); // Create RRH46410 driver, (connection) → void
11
+
12
+ async function main() {
13
+ await sensor.init(); // Check product ID, start IAQ 2nd Gen mode, () → void
14
+ while (true) {
15
+ const r = await sensor.read(); // Wait for the next sample, () → {iaq, tvoc_mg_m3 mg/m³, etoh_ppm ppm, eco2_ppm ppm, relative_iaq, sample, warmed_up}
16
+ console.log('IAQ %s TVOC %s mg/m3 EtOH %s ppm eCO2 %d ppm %s', r.iaq.toFixed(1), r.tvoc_mg_m3.toFixed(2),
17
+ r.etoh_ppm.toFixed(2), r.eco2_ppm, r.warmed_up ? 'ok' : 'warming up');
18
+ }
19
+ }
20
+
21
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,83 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HTS221Full } = require('../../../src/chips/humidity/hts221');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x5F', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x5F) → Connection
11
+ const sensor = new HTS221Full(connection); // Create HTS221 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Read calibration and start one-shot mode, () → void
15
+ // decodes the per-device two-point calibration
16
+ console.log('who am i 0x' + (await sensor.whoAmI()).toString(16)); // Read WHO_AM_I, () → number (0xBC)
17
+ // identifies the chip on the bus
18
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c °C, humidity_pct %rH}
19
+ // one-shot conversion, converted with the calibration
20
+ console.log(m.temperature_c.toFixed(2) + ' C', m.humidity_pct.toFixed(1) + ' %rH');
21
+ console.log('humidity', (await sensor.readHumidity()).toFixed(1)); // Read humidity only, () → number %rH
22
+ // one-shot conversion, clamped to 0–100
23
+ console.log('temperature', (await sensor.readTemperature()).toFixed(2)); // Read temperature only, () → number °C
24
+ // one-shot conversion
25
+ console.log('raw humidity', await sensor.readRawHumidity()); // Read raw humidity, () → number
26
+ // signed 16-bit ADC output
27
+ console.log('raw temperature', await sensor.readRawTemperature()); // Read raw temperature, () → number
28
+ // signed 16-bit ADC output
29
+ console.log('calibration', await sensor.readCalibration()); // Read calibration, () → object
30
+ // decoded two-point coefficients
31
+ console.log('dew point', (await sensor.dewPoint()).toFixed(2)); // Compute dew point, () → number °C
32
+ // Magnus formula over one measurement
33
+ console.log('status', await sensor.status()); // Read STATUS_REG, () → number
34
+ // H_DA = 0x02, T_DA = 0x01
35
+ console.log('data ready', await sensor.dataReady()); // Check data available, () → boolean
36
+ // both H_DA and T_DA set
37
+
38
+ await sensor.setOutputDataRate(1); // Set output data rate, (hz 0|1|7|12.5) → void
39
+ // continuous sampling at 1 Hz
40
+ console.log('rate', await sensor.getOutputDataRate(), 'Hz'); // Get output data rate, () → number Hz
41
+ // decodes CTRL_REG1.ODR
42
+ await sleep(1200);
43
+ const c = await sensor.readContinuous(); // Read latest sample, () → {temperature_c °C, humidity_pct %rH}
44
+ // no conversion is started
45
+ console.log('continuous', c);
46
+ await sensor.setOutputDataRate(0); // Set output data rate, (hz 0|1|7|12.5) → void
47
+ // back to one-shot mode
48
+
49
+ await sensor.setAveraging(32, 64); // Set averaging, (temperatureSamples 2–256, humiditySamples 4–512) → void
50
+ // more samples lower the noise and lengthen the conversion
51
+ console.log('averaging', await sensor.getAveraging()); // Get averaging, () → [number, number]
52
+ // [temperature samples, humidity samples]
53
+ await sensor.setAveraging(16, 32);
54
+ await sensor.setBlockDataUpdate(true); // Set block data update, (enabled) → void
55
+ // keeps LSB/MSB of one sample consistent
56
+
57
+ await sensor.setHeater(true); // Switch the heater, (on) → void
58
+ // drives off condensation; do not measure while on
59
+ console.log('heater on:', await sensor.isHeaterOn()); // Heater state, () → boolean
60
+ // reads CTRL_REG2.HEATER
61
+ await sensor.setHeater(false);
62
+
63
+ await sensor.configureDrdy(true, true, false); // Configure DRDY pin, (enabled, activeHigh=true, openDrain=false) → void
64
+ // routes data-ready to the DRDY pin
65
+ console.log('poll', await sensor.pollInterrupt()); // Poll interrupt status, () → number
66
+ // STATUS_REG; flags clear when data is read
67
+ await sensor.configureDrdy(false);
68
+
69
+ await sensor.powerDown(); // Power down, () → void
70
+ // clears CTRL_REG1.PD
71
+ await sensor.powerUp(); // Power up, () → void
72
+ // sets CTRL_REG1.PD
73
+ await sensor.onInterrupt((status) => console.log('DRDY status', status)); // Subscribe to data-ready, (callback, intPin=null) → void
74
+ // edge on the pin or a polling timer
75
+ await sleep(2000);
76
+ await sensor.offInterrupt(); // Unsubscribe, () → void
77
+ // disables the DRDY output
78
+ await sensor.reboot(); // Reload trimming, () → void
79
+ // sets BOOT, waits, re-reads the calibration
80
+ connection.close();
81
+ }
82
+
83
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,94 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HTS221Full } = require('../../../src/chips/humidity/hts221');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x5F', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x5F) → Connection
11
+ const sensor = new HTS221Full(connection); // Create HTS221 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Read calibration and start one-shot mode, () → void
15
+
16
+ // --- Identify the chip and show the factory calibration ---
17
+ // Every HTS221 stores its own two-point calibration; the driver read it
18
+ // during init() and uses it for every conversion.
19
+ console.log('who am i 0x' + (await sensor.whoAmI()).toString(16)); // Read WHO_AM_I, () → number (0xBC)
20
+ console.log('calibration', await sensor.readCalibration()); // Read calibration, () → object
21
+
22
+ // --- One-shot hygrometer: 10 readings, 2 s apart ---
23
+ // Each read() starts a conversion; the dew point shows what the pair
24
+ // means for condensation risk.
25
+ for (let i = 0; i < 10; i++) {
26
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c °C, humidity_pct %rH}
27
+ const dew = await sensor.dewPoint(); // Compute dew point, () → number °C
28
+ console.log(m.temperature_c.toFixed(2) + ' C ' + m.humidity_pct.toFixed(1) + ' %rH dew ' + dew.toFixed(2) + ' C');
29
+ console.log('raw', await sensor.readRawHumidity(), await sensor.readRawTemperature()); // Read raw outputs, () → number
30
+ console.log('h', (await sensor.readHumidity()).toFixed(1), 't', (await sensor.readTemperature()).toFixed(2)); // Read single values, () → number
31
+ await sleep(2000);
32
+ }
33
+
34
+ // --- Continuous sampling at 1 Hz and 12.5 Hz ---
35
+ // dataReady() paces the loop; the sample count per second follows the rate.
36
+ await sensor.setBlockDataUpdate(true); // Set block data update, (enabled) → void
37
+ for (const hz of [1, 12.5]) {
38
+ await sensor.setOutputDataRate(hz); // Set output data rate, (hz 0|1|7|12.5) → void
39
+ console.log('rate', await sensor.getOutputDataRate(), 'Hz'); // Get output data rate, () → number Hz
40
+ let count = 0;
41
+ const end = Date.now() + 3000;
42
+ while (Date.now() < end) {
43
+ if (await sensor.dataReady()) { // Check data available, () → boolean
44
+ await sensor.readContinuous(); // Read latest sample, () → object
45
+ count++;
46
+ }
47
+ await sleep(5);
48
+ }
49
+ console.log((count / 3).toFixed(1) + ' samples/s');
50
+ }
51
+ await sensor.setOutputDataRate(0); // Set output data rate, (hz 0|1|7|12.5) → void
52
+
53
+ // --- Noise at lowest and highest averaging ---
54
+ // Ten one-shot samples each; the spread of the raw humidity shrinks as
55
+ // the averaging grows.
56
+ for (const [t, h] of [[2, 4], [256, 512]]) {
57
+ await sensor.setAveraging(t, h); // Set averaging, (temperatureSamples, humiditySamples) → void
58
+ console.log('averaging', await sensor.getAveraging()); // Get averaging, () → [number, number]
59
+ const raws = [];
60
+ for (let i = 0; i < 10; i++) raws.push(await sensor.readRawHumidity()); // Read raw humidity, () → number
61
+ console.log('raw humidity spread', Math.max(...raws) - Math.min(...raws));
62
+ }
63
+ await sensor.setAveraging(16, 32); // Set averaging, (temperatureSamples, humiditySamples) → void
64
+
65
+ // --- Heater: drive off condensation for 10 s ---
66
+ // Humidity falls and temperature rises while the heater runs; measure
67
+ // before and after, never during, then let the sensor cool.
68
+ const before = await sensor.read(); // Read temperature and humidity, () → object
69
+ await sensor.setHeater(true); // Switch the heater, (on) → void
70
+ console.log('heater on:', await sensor.isHeaterOn()); // Heater state, () → boolean
71
+ await sleep(10000);
72
+ await sensor.setHeater(false); // Switch the heater, (on) → void
73
+ const after = await sensor.read(); // Read temperature and humidity, () → object
74
+ console.log('before', before, 'after', after);
75
+
76
+ // --- Data-ready notification for 5 s ---
77
+ // Uses the DRDY pin when the connection has one, otherwise a polling timer.
78
+ await sensor.configureDrdy(true, true, false); // Configure DRDY pin, (enabled, activeHigh=true, openDrain=false) → void
79
+ await sensor.setOutputDataRate(1); // Set output data rate, (hz 0|1|7|12.5) → void
80
+ await sensor.onInterrupt((status) => console.log('DRDY status', status)); // Subscribe to data-ready, (callback, intPin=null) → void
81
+ await sleep(5000);
82
+ await sensor.offInterrupt(); // Unsubscribe, () → void
83
+ console.log('poll', await sensor.pollInterrupt(), 'status', await sensor.status()); // Poll interrupt status, () → number
84
+ await sensor.setOutputDataRate(0);
85
+
86
+ // --- Power cycle and reload ---
87
+ await sensor.powerDown(); // Power down, () → void
88
+ await sensor.powerUp(); // Power up, () → void
89
+ await sensor.reboot(); // Reload trimming, () → void
90
+ console.log('done:', (await sensor.read()).temperature_c.toFixed(2), 'C');
91
+ connection.close();
92
+ }
93
+
94
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HTS221Minimal } = require('../../../src/chips/humidity/hts221');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x5F', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x5F) → Connection
11
+ const sensor = new HTS221Minimal(connection); // Create HTS221 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Read calibration and start one-shot mode, () → void
15
+ while (true) {
16
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c: number °C, humidity_pct: number %rH}
17
+ console.log('%s C, %s %%rH', m.temperature_c.toFixed(2), m.humidity_pct.toFixed(1));
18
+ await sleep(2000);
19
+ }
20
+ }
21
+
22
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HTU31DFull } = require('../../../src/chips/humidity/htu31d');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
11
+ const sensor = new HTU31DFull(connection); // Create HTU31D driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset, () → void
15
+ // 0x1E, waits 15 ms; highest resolution, heater off
16
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c: number °C, humidity_pct: number %RH}
17
+ // OSR 3 / 3 by default, both CRCs verified, humidity clamped 0-100
18
+ console.log(m);
19
+ console.log(await sensor.readHumidity()); // Read humidity only, () → number %RH
20
+ console.log(await sensor.readTemperature()); // Read temperature only, () → number °C
21
+ console.log(await sensor.readRaw()); // Read raw codes, () → [t_raw, rh_raw]
22
+ // 16-bit unsigned
23
+
24
+ sensor.setResolution(1, 2); // Set oversampling, (osrRh 0-3, osrT 0-3) → void
25
+ // higher OSR = finer resolution, longer conversion
26
+ console.log(sensor.getResolution()); // Get oversampling, () → [osrRh, osrT]
27
+ console.log(sensor.conversionTime()); // Typical conversion time, () → number s
28
+ sensor.setResolution(3, 3);
29
+
30
+ await sensor.startConversion(); // Start a conversion, () → void
31
+ // non-blocking; wait conversionTime() before reading
32
+ await sleep(30);
33
+ console.log(await sensor.readResult()); // Read last conversion result, () → {temperature_c, humidity_pct}
34
+
35
+ await sensor.setHeater(true); // Switch the heater, (on) → void
36
+ // 0x04; readings drift while the heater runs
37
+ console.log(await sensor.isHeaterOn()); // Heater state, () → boolean
38
+ await sensor.setHeater(false);
39
+ console.log((await sensor.diagnostic()).toString(16)); // Read the diagnostic byte, () → number
40
+ console.log(await sensor.diagnosticFlags()); // Named diagnostic flags, () → object
41
+ console.log((await sensor.serialNumber()).toString(16)); // Read the serial number, () → number
42
+ // 24-bit, CRC verified
43
+ console.log(await sensor.dewPoint()); // Compute dew point, () → number °C
44
+ // one measurement, Magnus formula
45
+ await sensor.softReset(); // Soft reset, () → void
46
+ // 0x1E, waits 15 ms; heater off, OSR 3 / 3
47
+ connection.close();
48
+ }
49
+
50
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HTU31DFull } = require('../../../src/chips/humidity/htu31d');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
11
+ const sensor = new HTU31DFull(connection); // Create HTU31D driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset, () → void
15
+
16
+ // --- Identify the chip and check its health ---
17
+ // The serial number is unique per device; the diagnostic flags report NVM
18
+ // and out-of-range errors.
19
+ console.log('serial 0x' + (await sensor.serialNumber()).toString(16)); // Read the serial number, () → number
20
+ console.log('diagnostic', await sensor.diagnosticFlags()); // Named diagnostic flags, () → object
21
+
22
+ // --- Condensation-aware hygrometer: 15 readings, 2 s apart ---
23
+ // The dew point shows how close the air is to condensing on a surface.
24
+ for (let i = 0; i < 15; i++) {
25
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c °C, humidity_pct %RH}
26
+ const dew = await sensor.dewPoint(); // Compute dew point, () → number °C
27
+ console.log('%s C %s %%RH dew %s C', m.temperature_c.toFixed(2), m.humidity_pct.toFixed(1), dew.toFixed(2));
28
+ console.log('h', (await sensor.readHumidity()).toFixed(1), 't', (await sensor.readTemperature()).toFixed(2), 'raw', await sensor.readRaw()); // Read single values and raw codes
29
+ await sleep(2000);
30
+ }
31
+
32
+ // --- Compare the four oversampling settings ---
33
+ // Higher OSR gives finer resolution at the cost of a longer conversion.
34
+ for (let osr = 0; osr < 4; osr++) {
35
+ sensor.setResolution(osr, osr); // Set oversampling, (osrRh 0-3, osrT 0-3) → void
36
+ const start = Date.now();
37
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c, humidity_pct}
38
+ console.log('OSR', sensor.getResolution(), 'typ', (sensor.conversionTime() * 1000).toFixed(2), 'ms', 'read', Date.now() - start, 'ms', m); // Get oversampling and typical conversion time
39
+ }
40
+ sensor.setResolution(3, 3);
41
+
42
+ // --- Heater: drive off condensation for 10 s ---
43
+ // Humidity falls and temperature rises while the heater runs; measure
44
+ // before and after, never during, then let the sensor cool.
45
+ const before = await sensor.read(); // Read temperature and humidity, () → {temperature_c, humidity_pct}
46
+ await sensor.setHeater(true); // Switch the heater, (on) → void
47
+ console.log('heater on:', await sensor.isHeaterOn(), (await sensor.diagnostic()).toString(16)); // Heater state and diagnostic byte, () → boolean / number
48
+ await sleep(10000);
49
+ await sensor.setHeater(false); // Switch the heater, (on) → void
50
+ const after = await sensor.read(); // Read temperature and humidity, () → {temperature_c, humidity_pct}
51
+ console.log('before', before, 'after', after);
52
+
53
+ // --- Non-blocking conversion ---
54
+ // Start the conversion, do other work, then collect the result.
55
+ await sensor.startConversion(); // Start a conversion, () → void
56
+ await sleep(sensor.conversionTime() * 1250);
57
+ console.log('result', await sensor.readResult()); // Read last conversion result, () → {temperature_c, humidity_pct}
58
+
59
+ // --- Reset ---
60
+ await sensor.softReset(); // Soft reset, () → void
61
+ console.log('done: %s C', (await sensor.read()).temperature_c.toFixed(2));
62
+ connection.close();
63
+ }
64
+
65
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HTU31DMinimal } = require('../../../src/chips/humidity/htu31d');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
11
+ const sensor = new HTU31DMinimal(connection); // Create HTU31D driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset, () → void
15
+ while (true) {
16
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c: number °C, humidity_pct: number %RH}
17
+ console.log('%s C, %s %%RH', m.temperature_c.toFixed(2), m.humidity_pct.toFixed(1));
18
+ await sleep(2000);
19
+ }
20
+ }
21
+
22
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { SHTC3Full } = require('../../../src/chips/humidity/shtc3');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x70', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x70) → Connection
11
+ const sensor = new SHTC3Full(connection); // Create SHTC3 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset and ID check, () → void
15
+
16
+ console.log(await sensor.read()); // Read temperature and humidity, () → {temperature_c: number °C, humidity_pct: number %RH}
17
+ // normal mode, both CRCs verified, humidity clamped 0-100
18
+ console.log(await sensor.readHumidity()); // Read humidity only, () → number %RH
19
+ console.log(await sensor.readTemperature()); // Read temperature only, () → number °C
20
+ console.log(await sensor.readRaw()); // Read raw codes, () → [number, number]
21
+ // [raw_temperature, raw_humidity], 16-bit
22
+ console.log((await sensor.readId()).toString(16)); // Read the ID word, () → number
23
+ // (id & 0x083F) === 0x0807 for the SHTC3
24
+
25
+ sensor.setLowPower(true); // Select low-power mode, (on) → void
26
+ // 0.7 ms measurement, slightly noisier
27
+ console.log(await sensor.read());
28
+ sensor.setLowPower(false);
29
+
30
+ sensor.setClockStretching(true); // Select clock stretching, (on) → void
31
+ // needs a controller that tolerates SCL held low
32
+ console.log(await sensor.read());
33
+ sensor.setClockStretching(false);
34
+
35
+ sensor.setSleepBetween(false); // Sleep after every read?, (on) → void
36
+ // off keeps the sensor idle (higher supply current)
37
+ console.log(await sensor.read());
38
+ sensor.setSleepBetween(true);
39
+ await sensor.wake(); // Wake-up command, () → void
40
+ await sensor.sleep(); // Sleep command, () → void
41
+ console.log(await sensor.dewPoint()); // Compute dew point, () → number °C
42
+ // one measurement, Magnus formula
43
+ await sensor.softReset(); // Soft reset, () → void
44
+ // 0x805D, leaves the sensor idle
45
+ connection.close();
46
+ }
47
+
48
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { SHTC3Full } = require('../../../src/chips/humidity/shtc3');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x70', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x70) → Connection
11
+ const sensor = new SHTC3Full(connection); // Create SHTC3 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset and ID check, () → void
15
+
16
+ // --- Identify the chip ---
17
+ // The ID word carries the SHTC3 product code in bits 11 and 5:0.
18
+ console.log('id 0x' + (await sensor.readId()).toString(16)); // Read the ID word, () → number
19
+
20
+ // --- Battery-friendly climate logger: 6 readings, 10 s apart ---
21
+ // The sensor sleeps (0.3 uA) between the readings; every read wakes it.
22
+ for (let i = 0; i < 6; i++) {
23
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c: number °C, humidity_pct: number %RH}
24
+ console.log('%s C %s %%RH', m.temperature_c.toFixed(2), m.humidity_pct.toFixed(1));
25
+ await sleep(10000);
26
+ }
27
+
28
+ // --- Compare normal and low-power mode ---
29
+ // Low power measures in 0.7 ms instead of 10.8 ms at slightly higher noise.
30
+ for (const low of [false, true]) {
31
+ sensor.setLowPower(low); // Select low-power mode, (on) → void
32
+ const start = Date.now();
33
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c: number °C, humidity_pct: number %RH}
34
+ console.log(low ? 'low power' : 'normal ', (Date.now() - start) + ' ms', m);
35
+ }
36
+ sensor.setLowPower(false);
37
+
38
+ // --- Dew point and raw words, then finish asleep ---
39
+ console.log('dew point', (await sensor.dewPoint()).toFixed(2), 'C'); // Compute dew point, () → number °C
40
+ console.log('raw', await sensor.readRaw()); // Read raw codes, () → [number, number]
41
+ await sensor.sleep(); // Sleep command, () → void
42
+ connection.close();
43
+ }
44
+
45
+ main().catch(err => { console.error(err); process.exit(1); });