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
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ const Gpio = require('onoff').Gpio;
4
+ const { HX711Transport } = require('../../../src/transport/hx711');
5
+ const { HX711Full } = require('../../../src/chips/adc_dac/hx711');
6
+
7
+ const dout = new Gpio(5, 'in'); // DOUT input pin from HX711
8
+ const pd_sck = new Gpio(6, 'out'); // PD_SCK clock / power-down output pin
9
+ const transport = new HX711Transport(dout, pd_sck); // Create HX711 transport, (dout, pd_sck)
10
+ const chip = new HX711Full(transport); // Create HX711 driver — discards first conversion, (transport)
11
+
12
+ const ready = chip.isReady(); // Check if conversion is ready (non-blocking), () → boolean
13
+ // returns true when DOUT is LOW
14
+ const raw = chip.readRaw(); // Read signed 24-bit ADC value at current gain, () → number
15
+ // blocks until DOUT goes LOW, then clocks out 24 bits
16
+
17
+ chip.setGain(64); // Select channel and gain, (gain: 128|64|32) → undefined
18
+ // 128 → Channel A, 64 → Channel A, 32 → Channel B; issues dummy read to apply
19
+ chip.setGain(32);
20
+ chip.setGain(128);
21
+
22
+ const avg = chip.readAverage(10); // Average multiple raw readings, (times=10) → number
23
+ // blocks for `times` complete conversions
24
+
25
+ chip.tare(10); // Capture zero offset from 10-reading average, (times=10) → undefined
26
+ // stores result in internal _offset; call with nothing on the scale
27
+ const offset = chip.getOffset(); // Return stored tare offset, () → number
28
+
29
+ chip.setScale(420.0); // Set calibration scale factor, (factor: number) → undefined
30
+ // factor = (readAverage() - offset) / known_weight_in_target_unit
31
+ const scale = chip.getScale(); // Return current scale factor, () → number
32
+
33
+ const weight = chip.readWeight(5); // Return calibrated weight, (times=1) → number
34
+ // computes (readAverage(times) - offset) / scale
35
+ console.log('weight:', weight);
36
+
37
+ chip.powerDown(); // Enter power-down mode, () → undefined
38
+ // holds PD_SCK HIGH for >60 µs
39
+ chip.powerUp(); // Exit power-down, reset chip, discard settling conversion, () → undefined
40
+ // resets to Channel A Gain 128; first post-reset conversion discarded internally
41
+
42
+ transport.close();
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ const Gpio = require('onoff').Gpio;
4
+ const { HX711Transport } = require('../../../src/transport/hx711');
5
+ const { HX711Full } = require('../../../src/chips/adc_dac/hx711');
6
+
7
+ // Kitchen scale demo: tare at startup, then print weight continuously.
8
+ // Replace SCALE_FACTOR with the value calibrated for your load cell and V_DD.
9
+ // Calibration: (1) call tare() with nothing on the scale; (2) place a known
10
+ // weight W grams; (3) SCALE_FACTOR = (readAverage() - getOffset()) / W.
11
+ const SCALE_FACTOR = 420.0;
12
+
13
+ const dout = new Gpio(5, 'in');
14
+ const pd_sck = new Gpio(6, 'out');
15
+ const transport = new HX711Transport(dout, pd_sck); // Create HX711 transport, (dout, pd_sck)
16
+ const chip = new HX711Full(transport); // Create HX711 driver — discards first conversion, (transport)
17
+
18
+ console.log('Taring — keep scale empty...');
19
+ chip.tare(10); // Capture zero offset from 10-reading average, (times=10) → undefined
20
+ chip.setScale(SCALE_FACTOR); // Set calibration scale factor, (factor: number) → undefined
21
+ console.log('Tare done. Place weight on scale.');
22
+
23
+ let prevWeight = null;
24
+
25
+ function poll() {
26
+ const weight = chip.readWeight(3); // Return calibrated weight, (times=3) → number
27
+ const rounded = Math.round(weight * 10) / 10;
28
+ if (prevWeight === null || Math.abs(rounded - prevWeight) > 1.0) {
29
+ console.log(`-> ${rounded.toFixed(1)} g`);
30
+ prevWeight = rounded;
31
+ }
32
+ setTimeout(poll, 500);
33
+ }
34
+
35
+ poll();
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ const Gpio = require('onoff').Gpio;
4
+ const { HX711Transport } = require('../../../src/transport/hx711');
5
+ const { HX711Minimal } = require('../../../src/chips/adc_dac/hx711');
6
+
7
+ const dout = new Gpio(5, 'in'); // DOUT input pin from HX711
8
+ const pd_sck = new Gpio(6, 'out'); // PD_SCK clock / power-down output pin
9
+ const transport = new HX711Transport(dout, pd_sck); // Create HX711 transport, (dout, pd_sck)
10
+ const chip = new HX711Minimal(transport); // Create HX711 driver — discards first conversion, (transport)
11
+
12
+ const ready = chip.isReady(); // Check if conversion is ready (non-blocking), () → boolean
13
+ const raw = chip.readRaw(); // Read signed 24-bit ADC value, () → number
14
+ console.log(raw);
15
+
16
+ transport.close();
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ const { I2CTransport } = require('periph/src/transport/i2c');
4
+ const { MCP4728Full } = require('periph/src/chips/adc_dac/mcp4728');
5
+
6
+ const transport = new I2CTransport(1, 0x60); // Create I2C transport, (bus=1, addr=0x60)
7
+ const dac = new MCP4728Full(transport); // Create MCP4728 driver, (transport)
8
+
9
+ dac.set_voltage(0, 0.75); // Set channel A as fraction of V_DD, (channel=0–3, fraction=0.0–1.0) → None
10
+ // Multi-Write, V_REF=external, gain=×1, PD=00
11
+ dac.set_raw(2, 3000); // Set channel C raw code, (channel=0–3, code=0–4095) → None
12
+ // clamps to [0, 4095]; writes channel C only
13
+ dac.set_all([0.1, 0.2, 0.3, 0.4]); // Update all four channels simultaneously, (fractions[4]) → None
14
+ // single 8-byte Fast Write transaction; EEPROM unaffected
15
+ dac.set_voltage_eeprom(0, 0.5, 0, 1); // Set channel A and persist to EEPROM, (channel=0–3, fraction, vref=0/1, gain=1/2) → None
16
+ // Single Write updates DAC register and nonvolatile EEPROM
17
+ dac.set_raw_eeprom(1, 2048, 0, 1); // Set channel B raw code and persist, (channel=0–3, code, vref=0/1, gain=1/2) → None
18
+ // Single Write; takes up to 50 ms for EEPROM write
19
+ dac.set_all_eeprom([0.0, 0.25, 0.5, 0.75], // Update all four channels + EEPROM, (fractions[4], vrefs[4], gains[4]) → None
20
+ [0, 0, 0, 0], // each channel uses external V_DD
21
+ [1, 1, 1, 1]); // and gain ×1
22
+ // Sequential Write from A to D; persists all four at the end
23
+ dac.set_vref(0, 0, 0, 0); // Set V_REF for all four channels, (vref_a, vref_b, vref_c, vref_d) → None
24
+ // 0 = external V_DD; volatile register only
25
+ dac.set_gain(1, 1, 1, 1); // Set gain for all four channels, (gain_a, gain_b, gain_c, gain_d) → None
26
+ // 1 = ×1, 2 = ×2; volatile register only
27
+ dac.set_power_down(0, 0, 0, 0); // Set power-down for all four channels, (pd_a, pd_b, pd_c, pd_d) → None
28
+ // 0 = normal, 1 = 1 kΩ, 2 = 100 kΩ, 3 = 500 kΩ to GND
29
+ const state = dac.read(); // Read all four channels' DAC and EEPROM state, () → {channel[], eeprom_ready}
30
+ // 4 channel objects with code, vref, gain, power_down, eeprom_*
31
+ console.log('ch0 code:', state.channel[0].code);
32
+ console.log('eeprom_ready:', state.eeprom_ready);
33
+ dac.software_update(); // Latch all V_OUT simultaneously, () → None
34
+ // General Call 0x00, 0x08; equivalent to LDAC pin pulse
35
+ dac.wake_up(); // Clear all PD bits via General Call Wake-Up, () → None
36
+ // sends 0x00, 0x09; clears power-down on all four channels
37
+ dac.reset(); // Reload EEPROM into all DAC registers, () → None
38
+ // General Call 0x00, 0x06; triggers internal POR
39
+ const ready = dac.is_eeprom_ready(); // Check if EEPROM write is complete, () → bool
40
+ // True when any pending EEPROM write has finished
41
+ console.log('eeprom ready:', ready);
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ const { I2CTransport } = require('periph/src/transport/i2c');
4
+ const { MCP4728Full } = require('periph/src/chips/adc_dac/mcp4728');
5
+
6
+ const VDD = 3.3;
7
+
8
+ const transport = new I2CTransport(1, 0x60); // Create I2C transport, (bus=1, addr=0x60)
9
+ const dac = new MCP4728Full(transport); // Create MCP4728 driver, (transport)
10
+
11
+ // --- Apply four-point calibration voltages to channels A–D ---
12
+ // A 4-channel DAC is the canonical way to bias a 4-point sensor bridge
13
+ // (load cell, RTD conditioning, strain gauge). Each channel gets a
14
+ // different fraction of full scale to demonstrate independent outputs.
15
+ // Voltages printed below assume a 3.3 V supply.
16
+ const calibration = [0.0, 1.0 / 3, 2.0 / 3, 1.0];
17
+ dac.set_all(calibration); // Update all four channels simultaneously, (fractions[4]) → None
18
+ for (let ch = 0; ch < 4; ch++) {
19
+ const code = Math.round(calibration[ch] * 4095);
20
+ console.log(`ch=${ch} fraction=${calibration[ch].toFixed(4)} code=${code} approx_v=${(code * VDD / 4096).toFixed(3)}V`);
21
+ }
22
+
23
+ // --- Synchronous staircase from 0 to full scale on all four channels ---
24
+ // Using set_all with the same fraction across channels keeps them in lock-step
25
+ // and demonstrates simultaneous V_OUT update via Fast Write. A 50 ms pause
26
+ // between steps lets the host controller observe each level on the scope.
27
+ async function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
28
+ (async () => {
29
+ const STEP = 1.0 / 16;
30
+ for (let n = 0; n <= 16; n++) {
31
+ const f = n * STEP;
32
+ dac.set_all([f, f, f, f]); // Update all four channels simultaneously, (fractions[4]) → None
33
+ const code = Math.round(f * 4095);
34
+ console.log(`step=${n.toString().padStart(2)} fraction=${f.toFixed(4)} code=${code} approx_v=${(code * VDD / 4096).toFixed(3)}V`);
35
+ await sleep(50);
36
+ }
37
+
38
+ // --- Reset all channels to 0 V before exit ---
39
+ dac.set_all([0.0, 0.0, 0.0, 0.0]); // Update all four channels simultaneously, (fractions[4]) → None
40
+ transport.close();
41
+ })();
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ const { I2CTransport } = require('periph/src/transport/i2c');
4
+ const { MCP4728Minimal } = require('periph/src/chips/adc_dac/mcp4728');
5
+
6
+ const transport = new I2CTransport(1, 0x60); // Create I2C transport, (bus=1, addr=0x60)
7
+ const dac = new MCP4728Minimal(transport); // Create MCP4728 driver, (transport)
8
+
9
+ dac.set_voltage(0, 0.5); // Set channel A as fraction of V_DD, (channel=0–3, fraction=0.0–1.0) → None
10
+ dac.set_raw(1, 2048); // Set channel B raw 12-bit code, (channel=0–3, code=0–4095) → None
11
+ dac.set_all([0.0, 0.25, 0.5, 1.0]); // Update all four channels simultaneously, (fractions[4]) → None
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../src/transport/i2c');
3
+ const { PCF8576Full } = require('../../../src/chips/display/pcf8576');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x38', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+ const lcd = new PCF8576Full(transport); // Create PCF8576 driver, (transport)
10
+ lcd.clear(); // Blank the display, () → void
11
+ // zeros all 40 columns of display RAM
12
+ lcd.deviceSelect(0); // Select device on the bus, (subaddress 0–7) → void
13
+ // sets the subaddress counter for cascaded use
14
+ lcd.setMode(PCF8576Full.BACKPLANES_4, PCF8576Full.BIAS_1_3); // Set drive mode, (backplanes 1–4, bias 0/1) → void
15
+ // configures 1:4 multiplex with 1/3 bias
16
+ lcd.setBlink(PCF8576Full.BLINK_2_HZ); // Set blink frequency, (frequency 0–3) → void
17
+ // ~2 Hz blink for visual attention
18
+ lcd.setBank(PCF8576Full.BANK_0, PCF8576Full.BANK_0); // Select RAM bank, (input_bank 0/1, output_bank 0/1) → void
19
+ // selects rows 0-1 for both input and output
20
+
21
+ const digits = [5, 6, 7, 8];
22
+ const out = digits.map(d => PCF8576Full.SEVEN_SEG[d]); // Encode 7-segment digit, (digit 0–9) → number
23
+ lcd.writeRaw(0, out); // Write raw bytes, (address 0–39, data) → void
24
+ // sets data pointer to 0 and writes all four digits
25
+
26
+ lcd.disable(); // Disable display output, () → void
27
+ // blanks the panel while keeping RAM contents
28
+ lcd.enable(); // Enable display output, () → void
29
+ // resumes output from RAM with the prior configuration
30
+
31
+ transport.close();
32
+ console.log('===DONE: 0 passed, 0 failed===');
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../src/transport/i2c');
3
+ const { PCF8576Full } = require('../../../src/chips/display/pcf8576');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x38', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+
10
+ async function sleep(ms) {
11
+ return new Promise(resolve => setTimeout(resolve, ms));
12
+ }
13
+
14
+ (async () => {
15
+ // --- 4-digit countdown from 9999 to 0000 on a 1:4 multiplex 7-segment LCD ---
16
+ // The PCF8576 drives four 7-segment digits from a single I2C bus; the host
17
+ // encodes each digit using the chip's 1:4 multiplex bit layout (a/c/b/DP/f/e/g/d)
18
+ // and writes all four with one writeRaw() call. The countdown runs once per
19
+ // second and the terminal mirrors the value sent to the display.
20
+ const lcd = new PCF8576Full(transport); // Create PCF8576 driver, (transport)
21
+
22
+ for (let n = 9999; n >= 0; n--) {
23
+ const d0 = Math.floor(n / 1000) % 10;
24
+ const d1 = Math.floor(n / 100) % 10;
25
+ const d2 = Math.floor(n / 10) % 10;
26
+ const d3 = n % 10;
27
+ const out = [
28
+ PCF8576Full.SEVEN_SEG[d0], // Encode 7-segment digit, (digit 0–9) → number
29
+ PCF8576Full.SEVEN_SEG[d1], // Encode 7-segment digit, (digit 0–9) → number
30
+ PCF8576Full.SEVEN_SEG[d2], // Encode 7-segment digit, (digit 0–9) → number
31
+ PCF8576Full.SEVEN_SEG[d3], // Encode 7-segment digit, (digit 0–9) → number
32
+ ];
33
+ lcd.writeRaw(0, out); // Write all four digits, (address 0, 4 bytes) → void
34
+ console.log(`countdown: ${String(n).padStart(4, '0')}`);
35
+ await sleep(1000);
36
+ }
37
+
38
+ // --- Stop indicator: light only the middle segments (g) on every digit ---
39
+ // When the counter reaches zero we replace the "0000" pattern with "----" to
40
+ // signal that the demo has finished. Each digit's g segment is bit 1, so a
41
+ // 0x02 byte lights just the bar across the middle.
42
+ const dash = [0x02, 0x02, 0x02, 0x02];
43
+ lcd.writeRaw(0, dash); // Write dash pattern, (address 0, 4 bytes) → void
44
+ console.log('countdown complete');
45
+
46
+ transport.close();
47
+ console.log('===DONE: 0 passed, 0 failed===');
48
+ })();
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../src/transport/i2c');
3
+ const { PCF8576Minimal } = require('../../../src/chips/display/pcf8576');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x38', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+ const lcd = new PCF8576Minimal(transport); // Create PCF8576 driver, (transport)
10
+
11
+ const digits = [1, 2, 3, 4];
12
+ for (let i = 0; i < digits.length; i++) {
13
+ const seg = PCF8576Minimal.SEVEN_SEG[digits[i]]; // Encode 7-segment digit, (digit 0–9) → number
14
+ lcd.setDigit7seg(i, seg); // Write one digit, (position 0–19, segments 0–255) → void
15
+ }
16
+
17
+ transport.close();
18
+ console.log('===DONE: 0 passed, 0 failed===');
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ const { I2CTransport } = require('../../../src/transport/i2c');
4
+ const { AHT21Full } = require('../../../src/chips/environmental/aht21');
5
+
6
+ const transport = new I2CTransport(1, 0x38);
7
+ const aht = new AHT21Full(transport); // Create AHT21 driver, (transport, addr=0x38) → void
8
+
9
+ console.log(aht.isCalibrated()); // Check calibration status, () → boolean
10
+ // reads CAL bit from status byte
11
+ console.log(aht.isBusy()); // Check busy status, () → boolean
12
+ // reads BUSY bit from status byte
13
+
14
+ const r = aht.read(); // Trigger measurement, () → { temperature_c, humidity_pct }
15
+ // sends 0xAC trigger, waits 80 ms, decodes 6 bytes
16
+ console.log(r.temperature_c, r.humidity_pct);
17
+
18
+ console.log(aht.readTemperature()); // Read temperature only, () → number °C
19
+ // triggers full measurement, returns temperature_c
20
+ console.log(aht.readHumidity()); // Read humidity only, () → number %RH
21
+ // triggers full measurement, returns humidity_pct
22
+
23
+ const rc = aht.readWithCrc(); // Read with CRC verification, () → { temperature_c, humidity_pct, crc_ok }
24
+ // reads 7 bytes, verifies CRC-8 (poly 0x31, init 0xFF)
25
+ console.log(rc.temperature_c, rc.humidity_pct, rc.crc_ok);
26
+
27
+ aht.softReset(); // Send soft reset command, () → void
28
+ // sends 0xBA, waits 20 ms for recovery
29
+
30
+ transport.close();
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const { I2CTransport } = require('../../../src/transport/i2c');
4
+ const { AHT21Full } = require('../../../src/chips/environmental/aht21');
5
+
6
+ const transport = new I2CTransport(1, 0x38);
7
+ const aht = new AHT21Full(transport); // Create AHT21 driver, (transport, addr=0x38) → void
8
+
9
+ // --- Verify calibration before starting the logging session ---
10
+ // Most AHT21 modules ship pre-calibrated; if the CAL bit is not set
11
+ // the driver already sent the calibration init sequence during construction.
12
+ console.log('Calibrated:', aht.isCalibrated()); // Check calibration status, () → boolean
13
+
14
+ console.log('Time T (C) RH (%) Dew (C)');
15
+
16
+ let n = 0;
17
+ setInterval(() => {
18
+ // --- Each reading requires an 80 ms measurement cycle ---
19
+ // The sensor cannot output data faster than this; the driver
20
+ // handles the trigger + wait internally.
21
+ const rc = aht.readWithCrc(); // Read with CRC verification, () → { temperature_c, humidity_pct, crc_ok }
22
+ if (!rc.crc_ok) {
23
+ console.log('CRC error at sample', n);
24
+ return;
25
+ }
26
+
27
+ const t = rc.temperature_c;
28
+ const rh = rc.humidity_pct;
29
+
30
+ // --- Magnus formula dew-point approximation ---
31
+ // gamma = ln(RH/100) + (17.625 * T) / (243.04 + T)
32
+ // dew_point = (243.04 * gamma) / (17.625 - gamma)
33
+ // Accurate to ±0.5 °C for 0 < T < 60 °C and 1 < RH < 100 %RH.
34
+ const gamma = Math.log(rh / 100.0) + (17.625 * t) / (243.04 + t);
35
+ const dew = (243.04 * gamma) / (17.625 - gamma);
36
+
37
+ console.log(
38
+ String(n).padEnd(9) +
39
+ t.toFixed(2).padEnd(11) +
40
+ rh.toFixed(2).padEnd(11) +
41
+ dew.toFixed(2)
42
+ );
43
+ n++;
44
+ }, 5000);
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ const { I2CTransport } = require('../../../src/transport/i2c');
4
+ const { AHT21Minimal } = require('../../../src/chips/environmental/aht21');
5
+
6
+ const transport = new I2CTransport(1, 0x38);
7
+ const aht = new AHT21Minimal(transport); // Create AHT21 driver, (transport, addr=0x38) → void
8
+
9
+ setInterval(() => {
10
+ const r = aht.read(); // Trigger measurement, () → { temperature_c, humidity_pct }
11
+ console.log(r.temperature_c, r.humidity_pct);
12
+ }, 1000);
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../src/transport/i2c');
3
+ const { BME280Full } = require('../../../src/chips/environmental/bme280');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+ const bme = new BME280Full(transport); // Create BME280 driver, (transport, busType='i2c')
10
+ const cid = bme.chipId(); // Read chip ID, () → number
11
+ // returns 0x60 for BME280
12
+ bme.configure(1, 1, 1, 0, 0, 0); // Configure chip, (osrsT 0–5, osrsP 0–5, osrsH 0–5, mode 0/1/3, filter 0–4, tSb 0–7) → void
13
+ // writes ctrl_hum, config, ctrl_meas in correct order
14
+ bme.setOversampling(BME280Full.OSRS_X4, BME280Full.OSRS_X2, BME280Full.OSRS_X1); // Set oversampling, (osrsT 0–5, osrsP 0–5, osrsH 0–5) → void
15
+ // humidity update requires ctrl_meas write to latch
16
+ bme.setMode(BME280Full.MODE_FORCED); // Set power mode, (mode 0/1/3) → void
17
+ bme.setFilter(BME280Full.FILTER_4); // Set IIR filter, (coeff 0–4) → void
18
+ // suppresses short-term pressure disturbances
19
+ bme.setStandby(BME280Full.T_SB_125_MS); // Set standby time, (tSb 0–7) → void
20
+ // only relevant in normal mode; codes 6/7 mean 10/20 ms on BME280
21
+ const st = bme.status(); // Read status register, () → number
22
+ const t = bme.temperature(); // Read temperature, () → number °C
23
+ const p = bme.pressure(); // Read pressure, () → number hPa
24
+ const h = bme.humidity(); // Read humidity, () → number %RH
25
+ const alt = bme.altitude(); // Compute altitude, (seaLevelHpa=1013.25) → number m
26
+ // uses barometric formula to convert pressure to metres
27
+ const slp = bme.seaLevelPressure(alt); // Compute sea-level pressure, (altitudeM) → number hPa
28
+ const dp = bme.dewPoint(); // Compute dew point, () → number °C
29
+ // Magnus-Tetens approximation from current T and RH
30
+ bme.reset(); // Soft reset chip, () → void
31
+ // re-reads calibration and re-applies configuration
32
+ console.log(`T=${t.toFixed(1)} C, P=${p.toFixed(1)} hPa, RH=${h.toFixed(1)} %RH, alt=${alt.toFixed(1)} m, dp=${dp.toFixed(1)} C`);
33
+ transport.close();
34
+ console.log('===DONE: 0 passed, 0 failed===');
@@ -0,0 +1,67 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../src/transport/i2c');
3
+ const { BME280Full } = require('../../../src/chips/environmental/bme280');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+
10
+ function stats(arr) {
11
+ if (!arr.length) return [0, 0, 0];
12
+ const sum = arr.reduce((a, b) => a + b, 0);
13
+ return [Math.min(...arr), sum / arr.length, Math.max(...arr)];
14
+ }
15
+
16
+ async function sleep(ms) {
17
+ return new Promise(resolve => setTimeout(resolve, ms));
18
+ }
19
+
20
+ (async () => {
21
+ // --- Weather monitoring preset: forced mode, ×1/×1/×1, filter off ---
22
+ // BME280 datasheet "weather monitoring" preset: minimum power,
23
+ // single-shot, 8 ms typ / 9.3 ms max per cycle. Sleep between samples
24
+ // to demonstrate battery-friendly indoor monitoring.
25
+ const bme = new BME280Full(transport); // Create BME280 driver, (transport, busType='i2c')
26
+ bme.configure(BME280Full.OSRS_X1, BME280Full.OSRS_X1, BME280Full.OSRS_X1, BME280Full.MODE_FORCED, BME280Full.FILTER_OFF, BME280Full.T_SB_0_5_MS); // Configure chip, (osrsT=×1, osrsP=×1, osrsH=×1, mode=forced, filter=off, tSb=0) → void
27
+
28
+ const temps = [], hums = [], pressures = [], alts = [], dews = [];
29
+ for (let n = 0; n < 10; n++) {
30
+ const t = bme.temperature(); // Read temperature, () → number °C
31
+ const p = bme.pressure(); // Read pressure, () → number hPa
32
+ const h = bme.humidity(); // Read humidity, () → number %RH
33
+ const a = bme.altitude(); // Compute altitude, (seaLevelHpa=1013.25) → number m
34
+ const d = bme.dewPoint(); // Compute dew point, () → number °C
35
+ temps.push(t); hums.push(h); pressures.push(p); alts.push(a); dews.push(d);
36
+ console.log(`${n}: ${t.toFixed(1)} C, ${h.toFixed(1)} %RH, ${p.toFixed(1)} hPa, dew=${d.toFixed(1)} C, alt=${a.toFixed(1)} m`);
37
+ await sleep(1000);
38
+ }
39
+
40
+ // --- Half-way: breathe gently on the sensor for 3 seconds ---
41
+ // User exposes the sensor to humid exhaled air; humidity climbs from
42
+ // ~40 %RH toward ~80 %RH, dew point spikes toward ambient temperature,
43
+ // pressure stays flat, temperature rises only slightly. Demonstrates
44
+ // the humidity channel's response and the dew-point alarm use case.
45
+ console.log('--- Breathe gently on the sensor for 3 seconds ---');
46
+ await sleep(3000);
47
+ {
48
+ const t = bme.temperature(); // Read temperature, () → number °C
49
+ const p = bme.pressure(); // Read pressure, () → number hPa
50
+ const h = bme.humidity(); // Read humidity, () → number %RH
51
+ const d = bme.dewPoint(); // Compute dew point, () → number °C
52
+ temps.push(t); hums.push(h); pressures.push(p); dews.push(d);
53
+ console.log(`after breath: ${t.toFixed(1)} C, ${h.toFixed(1)} %RH, ${p.toFixed(1)} hPa, dew=${d.toFixed(1)} C`);
54
+ }
55
+
56
+ const [tMin, tAvg, tMax] = stats(temps);
57
+ const [hMin, hAvg, hMax] = stats(hums);
58
+ const [pMin, pAvg, pMax] = stats(pressures);
59
+ const [dMin, dAvg, dMax] = stats(dews);
60
+ console.log(`T: ${tMin.toFixed(1)}/${tAvg.toFixed(1)}/${tMax.toFixed(1)} C`);
61
+ console.log(`RH: ${hMin.toFixed(1)}/${hAvg.toFixed(1)}/${hMax.toFixed(1)} %`);
62
+ console.log(`P: ${pMin.toFixed(1)}/${pAvg.toFixed(1)}/${pMax.toFixed(1)} hPa`);
63
+ console.log(`dew: ${dMin.toFixed(1)}/${dAvg.toFixed(1)}/${dMax.toFixed(1)} C`);
64
+
65
+ transport.close();
66
+ console.log('===DONE: 0 passed, 0 failed===');
67
+ })();
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../src/transport/i2c');
3
+ const { BME280Minimal } = require('../../../src/chips/environmental/bme280');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+ const bme = new BME280Minimal(transport); // Create BME280 driver, (transport, busType='i2c')
10
+
11
+ for (let i = 0; i < 5; i++) {
12
+ const t = bme.temperature(); // Read temperature, () → number °C
13
+ const p = bme.pressure(); // Read pressure, () → number hPa
14
+ const h = bme.humidity(); // Read humidity, () → number %RH
15
+ console.log(`${t.toFixed(1)} C, ${p.toFixed(1)} hPa, ${h.toFixed(1)} %RH`);
16
+ }
17
+ transport.close();
18
+ console.log('===DONE: 0 passed, 0 failed===');
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../src/transport/i2c');
3
+ const { BME680Full } = require('../../../src/chips/environmental/bme680');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+ const bme = new BME680Full(transport); // Create BME680 driver, (transport)
10
+ const cid = bme.chipId(); // Read chip ID, () → number
11
+ // returns 0x61 for BME680
12
+ console.log('chip_id=' + cid.toString(16));
13
+ bme.configure(BME680Full.OSRS_X1, BME680Full.OSRS_X1, BME680Full.OSRS_X1, BME680Full.MODE_SLEEP, BME680Full.FILTER_0); // Configure chip, (osrsT 0–5, osrsP 0–5, osrsH 0–5, mode 0/1, filter 0–7) → undefined
14
+ // writes ctrl_hum, config, ctrl_meas in correct order
15
+ bme.setOversampling(BME680Full.OSRS_X4, BME680Full.OSRS_X2, BME680Full.OSRS_X1); // Set oversampling, (osrsT 0–5, osrsP 0–5, osrsH 0–5) → undefined
16
+ // changes conversion time vs resolution trade-off
17
+ bme.setFilter(BME680Full.FILTER_7); // Set IIR filter, (coeff 0–7) → undefined
18
+ // applies to temperature and pressure only
19
+ bme.setHeater(320, 150); // Configure heater profile 0, (tempC, durationMs) → undefined
20
+ // sets target temperature and on-time for gas measurement
21
+ bme.setHeaterProfile(1, 200, 100); // Configure heater profile 1, (index 0–9, tempC, durationMs) → undefined
22
+ bme.selectHeaterProfile(0); // Select active profile, (index 0–9) → undefined
23
+ bme.setGasEnabled(true); // Enable gas conversion, (enabled) → undefined
24
+ bme.setHeaterOff(false); // Control heater override, (off) → undefined
25
+ bme.setAmbientTemperature(25.0); // Override ambient for heater calc, (tempC) → undefined
26
+ // re-applies the active heater profile
27
+ const st = bme.status(); // Read status register, () → number
28
+ // bit 7 = new_data, bit 6 = gas_measuring, bit 5 = measuring
29
+ const r = bme.readAll(); // Read all sensors in one cycle, () → object
30
+ // returns {temperature, pressure, humidity, gasResistance}
31
+ const gv = bme.gasValid(); // Check gas validity, () → boolean
32
+ const hs = bme.heaterStable(); // Check heater stability, () → boolean
33
+ bme.reset(); // Soft reset chip, () → undefined
34
+ // re-reads calibration and re-applies configuration
35
+ console.log(`T=${r.temperature.toFixed(1)} C, P=${r.pressure.toFixed(1)} hPa, RH=${r.humidity.toFixed(1)} %, R_gas=${r.gasResistance.toFixed(0)} Ohm`);
36
+ transport.close();
37
+ console.log('===DONE: 0 passed, 0 failed===');
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../src/transport/i2c');
3
+ const { BME680Full } = require('../../../src/chips/environmental/bme680');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+
10
+ // --- Room air quality probe: 4-in-1 sensor polling with VOC event ---
11
+ // Polls all four sensors once every 5 seconds for 5 minutes (60 ticks).
12
+ // At tick 30, the user is prompted to expose the sensor to a VOC source
13
+ // (isopropyl alcohol, marker pen). Gas resistance drops sharply on exposure
14
+ // and recovers over the remaining ticks, demonstrating raw VOC sensitivity
15
+ // without the closed-source BSEC library.
16
+ const bme = new BME680Full(transport); // Create BME680 driver, (transport)
17
+ bme.configure(BME680Full.OSRS_X2, BME680Full.OSRS_X16, BME680Full.OSRS_X1, BME680Full.MODE_FORCED, BME680Full.FILTER_15); // Configure chip, (osrsT=×2, osrsP=×16, osrsH=×1, mode=forced, filter=15) → undefined
18
+ bme.setHeater(320, 150); // Configure heater profile 0, (tempC=320, durationMs=150) → undefined
19
+
20
+ const temps = [], hums = [], pressures = [], gases = [];
21
+ for (let n = 0; n < 60; n++) {
22
+ if (n === 30) {
23
+ console.log('--- Expose sensor to VOC source now (alcohol/marker) ---');
24
+ }
25
+ const r = bme.readAll(); // Read all sensors in one cycle, () → object
26
+ temps.push(r.temperature);
27
+ pressures.push(r.pressure);
28
+ hums.push(r.humidity);
29
+ if (!isNaN(r.gasResistance)) gases.push(r.gasResistance);
30
+ console.log(`${n}: ${r.temperature.toFixed(1)} C, ${r.humidity.toFixed(1)} %RH, ${r.pressure.toFixed(1)} hPa, ${r.gasResistance.toFixed(0)} Ohm`);
31
+ }
32
+
33
+ function stats(arr) {
34
+ if (!arr.length) return { min: 0, avg: 0, max: 0 };
35
+ const min = Math.min(...arr);
36
+ const max = Math.max(...arr);
37
+ const avg = arr.reduce((s, v) => s + v, 0) / arr.length;
38
+ return { min, avg, max };
39
+ }
40
+
41
+ const ts = stats(temps);
42
+ const hs = stats(hums);
43
+ const ps = stats(pressures);
44
+ const gs = stats(gases);
45
+ console.log(`T: ${ts.min.toFixed(1)}/${ts.avg.toFixed(1)}/${ts.max.toFixed(1)} C`);
46
+ console.log(`RH: ${hs.min.toFixed(1)}/${hs.avg.toFixed(1)}/${hs.max.toFixed(1)} %`);
47
+ console.log(`P: ${ps.min.toFixed(1)}/${ps.avg.toFixed(1)}/${ps.max.toFixed(1)} hPa`);
48
+ console.log(`R_gas: ${gs.min.toFixed(0)}/${gs.avg.toFixed(0)}/${gs.max.toFixed(0)} Ohm`);
49
+ if (gs.min > 0) console.log(`VOC response ratio: ${(gs.max / gs.min).toFixed(1)}x`);
50
+
51
+ transport.close();
52
+ console.log('===DONE: 0 passed, 0 failed===');
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../src/transport/i2c');
3
+ const { BME680Minimal } = require('../../../src/chips/environmental/bme680');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+ const bme = new BME680Minimal(transport); // Create BME680 driver, (transport)
10
+
11
+ for (let i = 0; i < 5; i++) {
12
+ const t = bme.temperature(); // Read temperature, () → number °C
13
+ const p = bme.pressure(); // Read pressure, () → number hPa
14
+ const h = bme.humidity(); // Read humidity, () → number %RH
15
+ const g = bme.gasResistance(); // Read gas resistance, () → number Ω
16
+ console.log(`${t.toFixed(1)} C, ${p.toFixed(1)} hPa, ${h.toFixed(1)} %RH, ${g.toFixed(0)} Ohm`);
17
+ }
18
+ transport.close();
19
+ console.log('===DONE: 0 passed, 0 failed===');