periph 1.5.4 → 1.6.1
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.
- package/README.md +17 -0
- package/examples/gas/ccs811/complete.js +87 -0
- package/examples/gas/ccs811/demo.js +108 -0
- package/examples/gas/ccs811/minimal.js +20 -0
- package/examples/gas/rrh46410/complete.js +78 -0
- package/examples/gas/rrh46410/demo.js +86 -0
- package/examples/gas/rrh46410/minimal.js +21 -0
- package/examples/gas/rrh62000/complete.js +74 -0
- package/examples/gas/rrh62000/demo.js +87 -0
- package/examples/gas/rrh62000/minimal.js +22 -0
- package/examples/gas/scd30/complete.js +73 -0
- package/examples/gas/scd30/demo.js +102 -0
- package/examples/gas/scd30/minimal.js +20 -0
- package/examples/gas/scd40/complete.js +76 -0
- package/examples/gas/scd40/demo.js +109 -0
- package/examples/gas/scd40/minimal.js +20 -0
- package/examples/light/si1133/complete.js +89 -0
- package/examples/light/si1133/demo.js +93 -0
- package/examples/light/si1133/minimal.js +22 -0
- package/examples/light/si1145/complete.js +93 -0
- package/examples/light/si1145/demo.js +98 -0
- package/examples/light/si1145/minimal.js +22 -0
- package/examples/light/tsl2561/complete.js +74 -0
- package/examples/light/tsl2561/demo.js +46 -0
- package/examples/light/tsl2561/minimal.js +22 -0
- package/examples/light/tsl2591/complete.js +90 -0
- package/examples/light/tsl2591/demo.js +119 -0
- package/examples/light/tsl2591/minimal.js +22 -0
- package/examples/light/vcnl4040/complete.js +102 -0
- package/examples/light/vcnl4040/demo.js +125 -0
- package/examples/light/vcnl4040/minimal.js +25 -0
- package/examples/light/vcnl4200/complete.js +101 -0
- package/examples/light/vcnl4200/demo.js +126 -0
- package/examples/light/vcnl4200/minimal.js +25 -0
- package/examples/pressure/spl06/complete.js +81 -0
- package/examples/pressure/spl06/demo.js +106 -0
- package/examples/pressure/spl06/minimal.js +22 -0
- package/examples/rfid/pn532/complete.js +99 -0
- package/examples/rfid/pn532/demo.js +121 -0
- package/examples/rfid/pn532/minimal.js +22 -0
- package/examples/rfid/st25dv/complete.js +94 -0
- package/examples/rfid/st25dv/demo.js +121 -0
- package/examples/rfid/st25dv/minimal.js +21 -0
- package/examples/rtc/bq32000/complete.js +53 -0
- package/examples/rtc/bq32000/demo.js +77 -0
- package/examples/rtc/bq32000/minimal.js +20 -0
- package/examples/rtc/ds1307/complete.js +54 -0
- package/examples/rtc/ds1307/demo.js +96 -0
- package/examples/rtc/ds1307/minimal.js +20 -0
- package/examples/rtc/ds1371/complete.js +53 -0
- package/examples/rtc/ds1371/demo.js +85 -0
- package/examples/rtc/ds1371/minimal.js +25 -0
- package/package.json +1 -1
- package/src/chips/gas/ccs811.js +400 -0
- package/src/chips/gas/rrh46410.js +534 -0
- package/src/chips/gas/rrh62000.js +418 -0
- package/src/chips/gas/scd30.js +368 -0
- package/src/chips/gas/scd40.js +451 -0
- package/src/chips/light/si1133.js +583 -0
- package/src/chips/light/si1145.js +630 -0
- package/src/chips/light/tsl2561.js +445 -0
- package/src/chips/light/tsl2591.js +561 -0
- package/src/chips/light/vcnl4040.js +576 -0
- package/src/chips/light/vcnl4200.js +592 -0
- package/src/chips/pressure/spl06.js +461 -0
- package/src/chips/rfid/pn532.js +513 -0
- package/src/chips/rfid/st25dv.js +628 -0
- package/src/chips/rtc/bq32000.js +334 -0
- package/src/chips/rtc/ds1307.js +327 -0
- package/src/chips/rtc/ds1371.js +453 -0
- package/src/connection/i2c.js +46 -13
- package/src/connection/smbus.js +21 -8
- package/src/discovery/registry.js +17 -17
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { RRH62000Minimal } = require('../../../src/chips/gas/rrh62000');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x69', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x69) → Connection
|
|
10
|
+
const sensor = new RRH62000Minimal(connection); // Create RRH62000 driver, (connection, {accessIntervalMs=1000}) → void
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
await sensor.init(); // Wait for start-up, check firmware version, () → void
|
|
14
|
+
while (true) {
|
|
15
|
+
const r = await sensor.read(); // Wait for a new sample, () → {pm1_ug_m3, pm2_5_ug_m3, pm10_ug_m3 µg/m³, temperature_c °C, humidity_rh %RH, tvoc µg/m³, eco2_ppm ppm, iaq}
|
|
16
|
+
console.log('PM1 %s PM2.5 %s PM10 %s ug/m3 %s C %s %%RH TVOC %d ug/m3 eCO2 %d ppm IAQ %s',
|
|
17
|
+
r.pm1_ug_m3.toFixed(1), r.pm2_5_ug_m3.toFixed(1), r.pm10_ug_m3.toFixed(1), r.temperature_c.toFixed(2),
|
|
18
|
+
r.humidity_rh.toFixed(2), r.tvoc, r.eco2_ppm, r.iaq.toFixed(2));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { SCD30Full } = require('../../../src/chips/gas/scd30');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x61', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x61) → Connection
|
|
10
|
+
// The module has the fixed address 0x61; the bus must support clock stretching (≤ 100 kHz).
|
|
11
|
+
const sensor = new SCD30Full(connection); // Create SCD30 driver, (connection) → void
|
|
12
|
+
// No bus access yet.
|
|
13
|
+
|
|
14
|
+
async function main() {
|
|
15
|
+
await sensor.init(); // Read firmware/interval, start measuring, () → void
|
|
16
|
+
// Starts the continuous measurement without pressure compensation; stored settings are kept.
|
|
17
|
+
console.log(await sensor.firmwareVersion()); // Firmware version, () → [major, minor]
|
|
18
|
+
// Command 0xD100.
|
|
19
|
+
console.log(await sensor.read()); // Next sample, () → {co2_ppm, temperature_c, humidity_rh}
|
|
20
|
+
// Polls the data-ready status, then reads 18 bytes with six CRCs.
|
|
21
|
+
console.log(await sensor.readCo2()); // CO₂ of a fresh sample, () → number ppm
|
|
22
|
+
// Each single-value call waits for its own sample (one interval).
|
|
23
|
+
console.log(await sensor.readTemperature()); // Temperature of a fresh sample, () → number °C
|
|
24
|
+
// The temperature offset has already been subtracted.
|
|
25
|
+
console.log(await sensor.readHumidity()); // Humidity of a fresh sample, () → number %RH
|
|
26
|
+
// Compensated for the temperature offset.
|
|
27
|
+
console.log(await sensor.dataReady()); // New sample available, () → boolean
|
|
28
|
+
// Command 0x0202; usually false right after a read.
|
|
29
|
+
console.log(await sensor.readMeasurement()); // Current sample without waiting, () → [co2_ppm, temperature_c, humidity_rh]
|
|
30
|
+
// Call after dataReady(); otherwise the values may be stale.
|
|
31
|
+
|
|
32
|
+
await sensor.stop(); // Stop measuring, () → void
|
|
33
|
+
// Command 0x0104.
|
|
34
|
+
await sensor.start(101325); // Start measuring, (pressurePa=0 or 70000–140000) → void
|
|
35
|
+
// Pressure compensation with 1013 mbar; overrides the altitude until the next start.
|
|
36
|
+
await sensor.start(); // Start measuring, (pressurePa=0) → void
|
|
37
|
+
// 0 switches the pressure compensation off again.
|
|
38
|
+
|
|
39
|
+
await sensor.setInterval(5); // Measurement interval, (seconds 2–1800) → void
|
|
40
|
+
// Stored in non-volatile memory; read() waits up to the interval plus 1 s.
|
|
41
|
+
console.log(await sensor.getInterval()); // Measurement interval, () → number s
|
|
42
|
+
// 5.
|
|
43
|
+
await sensor.setInterval(2); // Measurement interval, (seconds 2–1800) → void
|
|
44
|
+
// Back to the default.
|
|
45
|
+
await sensor.setAutoCalibration(false); // Automatic self-calibration, (enabled) → void
|
|
46
|
+
// ASC needs fresh air (≈ 400 ppm) regularly; off is the module default.
|
|
47
|
+
console.log(await sensor.getAutoCalibration()); // Automatic self-calibration, () → boolean
|
|
48
|
+
// false.
|
|
49
|
+
console.log(await sensor.getForcedCalibration()); // FRC reference, () → number ppm
|
|
50
|
+
// 400 unless a forced recalibration was applied.
|
|
51
|
+
// await sensor.setForcedCalibration(420); // Forced recalibration, (ppm 400–2000) → void
|
|
52
|
+
// Only at a known reference concentration after ≥ 2 minutes of stable measurement.
|
|
53
|
+
await sensor.setTemperatureOffset(1.5); // Temperature offset, (kelvin 0–655.35) → void
|
|
54
|
+
// Compensates self-heating; sent in 0.01 K units.
|
|
55
|
+
console.log(await sensor.getTemperatureOffset()); // Temperature offset, () → number K
|
|
56
|
+
// 1.5.
|
|
57
|
+
await sensor.setAltitude(250); // Altitude compensation, (metres) → void
|
|
58
|
+
// Used while no pressure is given to start().
|
|
59
|
+
console.log(await sensor.getAltitude()); // Altitude compensation, () → number m
|
|
60
|
+
// 250.
|
|
61
|
+
|
|
62
|
+
console.log((await sensor.readWord(0x4600)).toString(16)); // Raw read, (command) → number
|
|
63
|
+
// Writes the command, waits 3 ms, reads one word and checks its CRC.
|
|
64
|
+
await sensor.writeWord(0x4600, 2); // Raw write, (command, value) → void
|
|
65
|
+
// Appends the argument and its CRC.
|
|
66
|
+
await sensor.softReset(); // Soft reset, () → void
|
|
67
|
+
// Waits 2 s and restarts the measurement; stored settings survive.
|
|
68
|
+
console.log(await sensor.read()); // Next sample, () → {co2_ppm, temperature_c, humidity_rh}
|
|
69
|
+
// First sample after the reset.
|
|
70
|
+
await connection.close();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { SCD30Full } = require('../../../src/chips/gas/scd30');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x61', 16);
|
|
8
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
9
|
+
|
|
10
|
+
function classify(co2) {
|
|
11
|
+
if (co2 < 1000) return 'good';
|
|
12
|
+
if (co2 <= 1500) return 'moderate';
|
|
13
|
+
return 'poor';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x61) → Connection
|
|
17
|
+
const sensor = new SCD30Full(connection); // Create SCD30 driver, (connection) → void
|
|
18
|
+
|
|
19
|
+
async function main() {
|
|
20
|
+
await sensor.init(); // Read firmware/interval, start measuring, () → void
|
|
21
|
+
|
|
22
|
+
/* --- Identity and settings ---
|
|
23
|
+
* All settings live in the module's non-volatile memory; the driver does not
|
|
24
|
+
* rewrite them on start-up, so this shows what earlier sessions left behind. */
|
|
25
|
+
const [major, minor] = await sensor.firmwareVersion(); // Firmware version, () → [major, minor]
|
|
26
|
+
console.log(`firmware ${major}.${minor}, interval ${await sensor.getInterval()} s, ` + // Measurement interval, () → number s
|
|
27
|
+
`ASC ${await sensor.getAutoCalibration()}, FRC ${await sensor.getForcedCalibration()} ppm, ` + // ASC, () → boolean / FRC reference, () → number ppm
|
|
28
|
+
`offset ${(await sensor.getTemperatureOffset()).toFixed(2)} K, altitude ${await sensor.getAltitude()} m`); // Temperature offset, () → number K / Altitude, () → number m
|
|
29
|
+
|
|
30
|
+
/* --- Room air-quality monitor ---
|
|
31
|
+
* Outdoor air is about 420 ppm; above 1000 ppm a room feels stuffy, above
|
|
32
|
+
* 1500 ppm it should be ventilated. A sample arrives every 2 s. */
|
|
33
|
+
const end = Date.now() + 30000;
|
|
34
|
+
while (Date.now() < end) {
|
|
35
|
+
const r = await sensor.read(); // Next sample, () → {co2_ppm, temperature_c, humidity_rh}
|
|
36
|
+
console.log(`CO2 ${r.co2_ppm.toFixed(0)} ppm (${classify(r.co2_ppm)}) ${r.temperature_c.toFixed(2)} C ${r.humidity_rh.toFixed(1)} %RH`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/* --- Self-heating compensation ---
|
|
40
|
+
* The module warms itself by a few kelvin. The offset is subtracted from the
|
|
41
|
+
* temperature, and the humidity is recomputed for the corrected temperature. */
|
|
42
|
+
const before = await sensor.read(); // Next sample, () → {co2_ppm, temperature_c, humidity_rh}
|
|
43
|
+
const oldOffset = await sensor.getTemperatureOffset(); // Temperature offset, () → number K
|
|
44
|
+
await sensor.setTemperatureOffset(oldOffset + 2); // Temperature offset, (kelvin) → void
|
|
45
|
+
await sleep(4000);
|
|
46
|
+
const t = await sensor.readTemperature(); // Temperature of a fresh sample, () → number °C
|
|
47
|
+
const h = await sensor.readHumidity(); // Humidity of a fresh sample, () → number %RH
|
|
48
|
+
console.log(`offset +2 K: ${before.temperature_c.toFixed(2)} C -> ${t.toFixed(2)} C, ` +
|
|
49
|
+
`${before.humidity_rh.toFixed(1)} %RH -> ${h.toFixed(1)} %RH`);
|
|
50
|
+
await sensor.setTemperatureOffset(oldOffset); // Temperature offset, (kelvin) → void
|
|
51
|
+
|
|
52
|
+
/* --- Altitude and pressure compensation ---
|
|
53
|
+
* CO₂ absorption depends on the air density. Give the altitude once, or the
|
|
54
|
+
* current ambient pressure (in pascals) with every start. */
|
|
55
|
+
const oldAltitude = await sensor.getAltitude(); // Altitude compensation, () → number m
|
|
56
|
+
await sensor.setAltitude(500); // Altitude compensation, (metres) → void
|
|
57
|
+
console.log(`altitude 500 m: CO2 ${(await sensor.readCo2()).toFixed(0)} ppm`); // CO₂ of a fresh sample, () → number ppm
|
|
58
|
+
await sensor.start(95000); // Start measuring, (pressurePa) → void
|
|
59
|
+
console.log(`pressure 95000 Pa: CO2 ${(await sensor.readCo2()).toFixed(0)} ppm`); // CO₂ of a fresh sample, () → number ppm
|
|
60
|
+
await sensor.start(); // Start measuring, (pressurePa=0) → void
|
|
61
|
+
await sensor.setAltitude(oldAltitude); // Altitude compensation, (metres) → void
|
|
62
|
+
|
|
63
|
+
/* --- Automatic self-calibration ---
|
|
64
|
+
* ASC learns the 400 ppm baseline from the lowest readings of the past days;
|
|
65
|
+
* the sensor must see fresh air at least once a week. */
|
|
66
|
+
const asc = await sensor.getAutoCalibration(); // ASC, () → boolean
|
|
67
|
+
await sensor.setAutoCalibration(true); // ASC, (enabled) → void
|
|
68
|
+
console.log('ASC on:', await sensor.getAutoCalibration()); // ASC, () → boolean
|
|
69
|
+
await sensor.setAutoCalibration(asc); // ASC, (enabled) → void
|
|
70
|
+
|
|
71
|
+
/* --- Data-ready, stop and start --- */
|
|
72
|
+
console.log('data ready:', await sensor.dataReady()); // New sample available, () → boolean
|
|
73
|
+
await sensor.stop(); // Stop measuring, () → void
|
|
74
|
+
await sleep(3000);
|
|
75
|
+
console.log('stopped, data ready:', await sensor.dataReady()); // New sample available, () → boolean
|
|
76
|
+
await sensor.start(); // Start measuring, (pressurePa=0) → void
|
|
77
|
+
while (!(await sensor.dataReady())) await sleep(100); // New sample available, () → boolean
|
|
78
|
+
console.log('restarted:', await sensor.readMeasurement()); // Current sample without waiting, () → [co2_ppm, temperature_c, humidity_rh]
|
|
79
|
+
|
|
80
|
+
/* --- Slower interval ---
|
|
81
|
+
* A longer interval saves power; read() waits up to the interval plus 1 s. */
|
|
82
|
+
await sensor.setInterval(5); // Measurement interval, (seconds) → void
|
|
83
|
+
console.log(`interval ${await sensor.getInterval()} s: CO2 ${(await sensor.readCo2()).toFixed(0)} ppm`); // Measurement interval, () → number s / CO₂, () → number ppm
|
|
84
|
+
await sensor.setInterval(2); // Measurement interval, (seconds) → void
|
|
85
|
+
|
|
86
|
+
/* --- Forced recalibration ---
|
|
87
|
+
* FRC sets the current reading to a known reference concentration. Only do
|
|
88
|
+
* this with the sensor in reference gas (or fresh outdoor air) for at least
|
|
89
|
+
* 2 minutes; it is skipped here. */
|
|
90
|
+
const REFERENCE_GAS = false;
|
|
91
|
+
if (REFERENCE_GAS) await sensor.setForcedCalibration(420); // Forced recalibration, (ppm 400–2000) → void
|
|
92
|
+
console.log(`FRC reference ${await sensor.getForcedCalibration()} ppm (skipped)`); // FRC reference, () → number ppm
|
|
93
|
+
|
|
94
|
+
/* --- Raw commands and reset --- */
|
|
95
|
+
console.log('raw interval word:', await sensor.readWord(0x4600)); // Raw read, (command) → number
|
|
96
|
+
await sensor.writeWord(0x4600, await sensor.getInterval()); // Raw write, (command, value) → void
|
|
97
|
+
await sensor.softReset(); // Soft reset, () → void
|
|
98
|
+
console.log('after reset:', await sensor.read()); // Next sample, () → {co2_ppm, temperature_c, humidity_rh}
|
|
99
|
+
await connection.close();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { SCD30Minimal } = require('../../../src/chips/gas/scd30');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x61', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x61) → Connection
|
|
10
|
+
const sensor = new SCD30Minimal(connection); // Create SCD30 driver, (connection) → void
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
await sensor.init(); // Read firmware/interval, start measuring, () → void
|
|
14
|
+
while (true) {
|
|
15
|
+
const r = await sensor.read(); // Wait for the next sample, () → {co2_ppm ppm, temperature_c °C, humidity_rh %RH}
|
|
16
|
+
console.log('CO2 %s ppm %s C %s %%RH', r.co2_ppm.toFixed(0), r.temperature_c.toFixed(2), r.humidity_rh.toFixed(1));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { SCD40Full } = require('../../../src/chips/gas/scd40');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x62', 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=0x62) → Connection
|
|
11
|
+
// The sensor has the fixed address 0x62; keep the bus at 100 kHz or below.
|
|
12
|
+
const sensor = new SCD40Full(connection); // Create SCD40 driver, (connection) → void
|
|
13
|
+
// No bus access yet.
|
|
14
|
+
|
|
15
|
+
async function main() {
|
|
16
|
+
await sensor.init(); // Stop, read serial, start periodic, () → void
|
|
17
|
+
// Stops a measurement left running (500 ms), then starts the 5 s periodic mode.
|
|
18
|
+
console.log((await sensor.serialNumber()).toString(16)); // Serial number, () → number (48 bit)
|
|
19
|
+
// Command 0x3682; idle only, so the driver stops and restarts the measurement.
|
|
20
|
+
console.log(await sensor.read()); // Next sample, () → {co2_ppm, temperature_c, humidity_rh}
|
|
21
|
+
// Polls the data-ready status every 100 ms (up to 6 s), then reads 9 bytes with three CRCs.
|
|
22
|
+
console.log(await sensor.readCo2()); // CO₂ of a fresh sample, () → number ppm
|
|
23
|
+
// Each single-value call waits for its own sample (5 s).
|
|
24
|
+
console.log(await sensor.readTemperature()); // Temperature of a fresh sample, () → number °C
|
|
25
|
+
// The temperature offset has already been subtracted.
|
|
26
|
+
console.log(await sensor.readHumidity()); // Humidity of a fresh sample, () → number %RH
|
|
27
|
+
// Compensated for the temperature offset.
|
|
28
|
+
console.log(await sensor.dataReady()); // New sample available, () → boolean
|
|
29
|
+
// Command 0xE4B8; usually false right after a read.
|
|
30
|
+
while (!(await sensor.dataReady())) await sleep(100); // New sample available, () → boolean
|
|
31
|
+
console.log(await sensor.readMeasurement()); // Sample without waiting, () → [co2_ppm, temperature_c, humidity_rh]
|
|
32
|
+
// Call after dataReady(); each sample can be read only once.
|
|
33
|
+
|
|
34
|
+
await sensor.setTemperatureOffset(4.0); // Temperature offset, (kelvin 0–175) → void
|
|
35
|
+
// Idle only: stops (500 ms, the pending sample is lost), writes, restarts.
|
|
36
|
+
console.log(await sensor.getTemperatureOffset()); // Temperature offset, () → number K
|
|
37
|
+
// About 4.0 (65536/175 steps).
|
|
38
|
+
await sensor.setAltitude(250); // Sensor altitude, (metres 0–65535) → void
|
|
39
|
+
// Pressure compensation from the altitude.
|
|
40
|
+
console.log(await sensor.getAltitude()); // Sensor altitude, () → number m
|
|
41
|
+
// 250.
|
|
42
|
+
await sensor.setAmbientPressure(101325); // Ambient pressure, (pressurePa) → void
|
|
43
|
+
// Sent as 1013 hPa; allowed during measurement, overrides the altitude until reinit().
|
|
44
|
+
await sensor.setAutoCalibration(true); // Automatic self-calibration, (enabled) → void
|
|
45
|
+
// ASC is on by default and needs fresh air (≈ 400 ppm) once a week.
|
|
46
|
+
console.log(await sensor.getAutoCalibration()); // Automatic self-calibration, () → boolean
|
|
47
|
+
// true.
|
|
48
|
+
// console.log(await sensor.forceRecalibration(420)); // Forced recalibration, (ppm) → number correction ppm
|
|
49
|
+
// Only after > 3 minutes in a known, homogeneous concentration.
|
|
50
|
+
// await sensor.persistSettings(); // Store settings in the EEPROM, () → void
|
|
51
|
+
// At least 2000 write cycles; use sparingly.
|
|
52
|
+
// await sensor.factoryReset(); // Factory reset, () → void
|
|
53
|
+
// Erases the stored settings and the calibration history.
|
|
54
|
+
|
|
55
|
+
await sensor.stopPeriodic(); // Stop measuring, () → void
|
|
56
|
+
// Command 0x3F86, waits 500 ms.
|
|
57
|
+
await sensor.startLowPowerPeriodic(); // Low-power periodic mode, () → void
|
|
58
|
+
// A sample about every 30 s; read() then waits up to 35 s.
|
|
59
|
+
console.log(await sensor.read()); // Next sample, () → {co2_ppm, temperature_c, humidity_rh}
|
|
60
|
+
// First low-power sample.
|
|
61
|
+
await sensor.startPeriodic(); // Standard periodic mode, () → void
|
|
62
|
+
// Stops the low-power mode first, then a sample every 5 s.
|
|
63
|
+
console.log(await sensor.selfTest()); // Self test, () → boolean
|
|
64
|
+
// Blocks 10 s; true means no malfunction.
|
|
65
|
+
console.log((await sensor.readWord(0xE4B8)).toString(16)); // Raw read, (command) → number
|
|
66
|
+
// Writes the command, waits 1 ms, reads one word and checks its CRC.
|
|
67
|
+
await sensor.writeWord(0xE000, 1013); // Raw write, (command, value) → void
|
|
68
|
+
// Appends the argument and its CRC; no idle handling.
|
|
69
|
+
await sensor.reinit(); // Reload settings from the EEPROM, () → void
|
|
70
|
+
// Unsaved settings (offset, altitude, pressure) are discarded.
|
|
71
|
+
console.log(await sensor.read()); // Next sample, () → {co2_ppm, temperature_c, humidity_rh}
|
|
72
|
+
// First sample after the reinit.
|
|
73
|
+
await connection.close();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { SCD40Full } = require('../../../src/chips/gas/scd40');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x62', 16);
|
|
8
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
9
|
+
|
|
10
|
+
function classify(co2) {
|
|
11
|
+
if (co2 < 1000) return 'good';
|
|
12
|
+
if (co2 <= 1500) return 'moderate';
|
|
13
|
+
return 'poor';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x62) → Connection
|
|
17
|
+
const sensor = new SCD40Full(connection); // Create SCD40 driver, (connection) → void
|
|
18
|
+
|
|
19
|
+
async function main() {
|
|
20
|
+
await sensor.init(); // Stop, read serial, start periodic, () → void
|
|
21
|
+
|
|
22
|
+
/* --- Identity and settings ---
|
|
23
|
+
* Settings live in RAM until persistSettings(); after power-up they are the
|
|
24
|
+
* EEPROM values (factory: offset 4 K, altitude 0 m, ASC on). */
|
|
25
|
+
console.log('serial %s', (await sensor.serialNumber()).toString(16).padStart(12, '0')); // Serial number, () → number
|
|
26
|
+
console.log('offset %s K, altitude %d m, ASC %s',
|
|
27
|
+
(await sensor.getTemperatureOffset()).toFixed(2), // Temperature offset, () → number K
|
|
28
|
+
await sensor.getAltitude(), // Altitude, () → number m
|
|
29
|
+
await sensor.getAutoCalibration()); // ASC, () → boolean
|
|
30
|
+
|
|
31
|
+
/* --- Room air-quality monitor ---
|
|
32
|
+
* Outdoor air is about 420 ppm; above 1000 ppm a room feels stuffy, above
|
|
33
|
+
* 1500 ppm it should be ventilated. A sample arrives every 5 s. */
|
|
34
|
+
const end = Date.now() + 30000;
|
|
35
|
+
while (Date.now() < end) {
|
|
36
|
+
const r = await sensor.read(); // Next sample, () → {co2_ppm, temperature_c, humidity_rh}
|
|
37
|
+
console.log('CO2 %s ppm (%s) %s C %s %%RH', r.co2_ppm.toFixed(0).padStart(4), classify(r.co2_ppm),
|
|
38
|
+
r.temperature_c.toFixed(2), r.humidity_rh.toFixed(1));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/* --- Self-heating compensation ---
|
|
42
|
+
* The offset is subtracted from the temperature and the humidity is
|
|
43
|
+
* recomputed; CO2 is not affected. Every setting change costs one sample
|
|
44
|
+
* because the sensor has to be stopped (500 ms) and restarted. */
|
|
45
|
+
const before = await sensor.read(); // Next sample, () → {co2_ppm, temperature_c, humidity_rh}
|
|
46
|
+
const oldOffset = await sensor.getTemperatureOffset(); // Temperature offset, () → number K
|
|
47
|
+
await sensor.setTemperatureOffset(oldOffset + 4.0); // Temperature offset, (kelvin) → void
|
|
48
|
+
const t = await sensor.readTemperature(); // Temperature of a fresh sample, () → number °C
|
|
49
|
+
const rh = await sensor.readHumidity(); // Humidity of a fresh sample, () → number %RH
|
|
50
|
+
console.log('offset +4 K: %s C -> %s C, %s %%RH -> %s %%RH', before.temperature_c.toFixed(2), t.toFixed(2),
|
|
51
|
+
before.humidity_rh.toFixed(1), rh.toFixed(1));
|
|
52
|
+
await sensor.setTemperatureOffset(oldOffset); // Temperature offset, (kelvin) → void
|
|
53
|
+
|
|
54
|
+
/* --- Altitude and pressure compensation ---
|
|
55
|
+
* CO2 absorption depends on the air density. Give the altitude once (idle
|
|
56
|
+
* only), or feed the current ambient pressure in pascals at any time. */
|
|
57
|
+
const oldAltitude = await sensor.getAltitude(); // Altitude, () → number m
|
|
58
|
+
await sensor.setAltitude(500); // Altitude, (metres) → void
|
|
59
|
+
console.log('altitude 500 m: CO2 %s ppm', (await sensor.readCo2()).toFixed(0)); // CO₂ of a fresh sample, () → number ppm
|
|
60
|
+
await sensor.setAmbientPressure(95000); // Ambient pressure, (pressurePa) → void
|
|
61
|
+
console.log('pressure 95000 Pa: CO2 %s ppm', (await sensor.readCo2()).toFixed(0)); // CO₂ of a fresh sample, () → number ppm
|
|
62
|
+
await sensor.setAltitude(oldAltitude); // Altitude, (metres) → void
|
|
63
|
+
|
|
64
|
+
/* --- Data-ready --- */
|
|
65
|
+
console.log('data ready:', await sensor.dataReady()); // New sample available, () → boolean
|
|
66
|
+
while (!(await sensor.dataReady())) await sleep(100); // New sample available, () → boolean
|
|
67
|
+
console.log('sample:', await sensor.readMeasurement()); // Sample without waiting, () → [co2_ppm, temperature_c, humidity_rh]
|
|
68
|
+
|
|
69
|
+
/* --- Low-power periodic mode ---
|
|
70
|
+
* About one sample every 30 s at 3.2 mA instead of 15 mA average. */
|
|
71
|
+
await sensor.stopPeriodic(); // Stop measuring, () → void
|
|
72
|
+
await sensor.startLowPowerPeriodic(); // Low-power periodic mode, () → void
|
|
73
|
+
console.log('low power:', await sensor.read()); // Next sample, () → {co2_ppm, temperature_c, humidity_rh}
|
|
74
|
+
await sensor.startPeriodic(); // Standard periodic mode, () → void
|
|
75
|
+
|
|
76
|
+
/* --- Automatic self-calibration ---
|
|
77
|
+
* ASC learns the 400 ppm baseline from the lowest readings of the past
|
|
78
|
+
* week; in rooms that never see fresh air, disable it and use FRC. */
|
|
79
|
+
const asc = await sensor.getAutoCalibration(); // ASC, () → boolean
|
|
80
|
+
await sensor.setAutoCalibration(false); // ASC, (enabled) → void
|
|
81
|
+
console.log('ASC off:', !(await sensor.getAutoCalibration())); // ASC, () → boolean
|
|
82
|
+
await sensor.setAutoCalibration(asc); // ASC, (enabled) → void
|
|
83
|
+
|
|
84
|
+
/* --- Persisting settings ---
|
|
85
|
+
* Settings are lost at power-off unless stored; the EEPROM endures at least
|
|
86
|
+
* 2000 writes, so this is skipped by default. */
|
|
87
|
+
const PERSIST = false;
|
|
88
|
+
if (PERSIST) await sensor.persistSettings(); // Store settings in the EEPROM, () → void
|
|
89
|
+
console.log('persistSettings skipped');
|
|
90
|
+
|
|
91
|
+
/* --- Forced recalibration ---
|
|
92
|
+
* FRC sets the current reading to a known reference concentration. Only do
|
|
93
|
+
* this after > 3 minutes in reference gas (or fresh outdoor air); skipped. */
|
|
94
|
+
const REFERENCE_GAS = false;
|
|
95
|
+
if (REFERENCE_GAS) console.log('FRC correction %d ppm', await sensor.forceRecalibration(420)); // Forced recalibration, (ppm) → number ppm
|
|
96
|
+
console.log('forced recalibration skipped');
|
|
97
|
+
const FACTORY_RESET = false;
|
|
98
|
+
if (FACTORY_RESET) await sensor.factoryReset(); // Factory reset, () → void
|
|
99
|
+
|
|
100
|
+
/* --- Self test, raw access and reinit --- */
|
|
101
|
+
console.log('self test ok:', await sensor.selfTest()); // Self test (10 s), () → boolean
|
|
102
|
+
console.log('raw data-ready word: 0x%s', (await sensor.readWord(0xE4B8)).toString(16).padStart(4, '0')); // Raw read, (command) → number
|
|
103
|
+
await sensor.writeWord(0xE000, 1013); // Raw write, (command, value) → void
|
|
104
|
+
await sensor.reinit(); // Reload settings from the EEPROM, () → void
|
|
105
|
+
console.log('after reinit:', await sensor.read()); // Next sample, () → {co2_ppm, temperature_c, humidity_rh}
|
|
106
|
+
await connection.close();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { SCD40Minimal } = require('../../../src/chips/gas/scd40');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x62', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x62) → Connection
|
|
10
|
+
const sensor = new SCD40Minimal(connection); // Create SCD40 driver, (connection) → void
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
await sensor.init(); // Stop, read serial, start periodic, () → void
|
|
14
|
+
while (true) {
|
|
15
|
+
const r = await sensor.read(); // Wait for the next sample, () → {co2_ppm ppm, temperature_c °C, humidity_rh %RH}
|
|
16
|
+
console.log('CO2 %s ppm %s C %s %%RH', r.co2_ppm.toFixed(0), r.temperature_c.toFixed(2), r.humidity_rh.toFixed(1));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Si1133Full } = require('../../../src/chips/light/si1133');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x55', 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=0x55) → Connection
|
|
11
|
+
// Default address 0x55 (AD pulled up); 0x52 with AD pulled down.
|
|
12
|
+
const sensor = new Si1133Full(connection); // Create Si1133 driver, (connection) → void
|
|
13
|
+
// Constructor stores the connection only; no bus traffic yet.
|
|
14
|
+
|
|
15
|
+
async function main() {
|
|
16
|
+
await sensor.init(); // Check PART_ID, reset, UV + white channels, () → void
|
|
17
|
+
// Waits 25 ms, verifies PART_ID 0x33, RESET_SW, channel 0 = UV, channel 1 = white, 24-bit outputs.
|
|
18
|
+
|
|
19
|
+
console.log(await sensor.readId()); // Identity registers, () → {partId, hwId, revId}
|
|
20
|
+
// PART_ID is 0x33; HW_ID bits 4:0 are 0x03 for the Si1133-AA00.
|
|
21
|
+
console.log(await sensor.readResponse()); // RESPONSE0 decoded, () → {state, error, counter}
|
|
22
|
+
// State bits RUNNING/SUSPEND/SLEEP, the CMD_ERR flag and the 4-bit command counter.
|
|
23
|
+
|
|
24
|
+
console.log(await sensor.read()); // Forced measurement, () → {uv: number counts, white: number counts}
|
|
25
|
+
// FORCE, wait for both channels in IRQ_STATUS, decode two signed 24-bit counts.
|
|
26
|
+
console.log(await sensor.readUv()); // UV photodiode once, () → number counts
|
|
27
|
+
// Uses channel 5 as a scratch channel and restores CHAN_LIST afterwards.
|
|
28
|
+
console.log(await sensor.readWhite()); // White photodiode once, () → number counts
|
|
29
|
+
// Same scratch channel, HW_GAIN 7.
|
|
30
|
+
console.log(await sensor.readIr(Si1133Full.MUX_LARGE_IR)); // IR photodiode once, (photodiode) → number counts
|
|
31
|
+
// Small, medium or large IR photodiode; the small one is the default.
|
|
32
|
+
|
|
33
|
+
await sensor.configureChannel(2, Si1133Full.MUX_LARGE_WHITE, { hwGain: 4, swGain: 1, decimRate: 0,
|
|
34
|
+
highSignal: false, bits24: false, postshift: 0 }); // Channel setup, (channel, adcmux, {hwGain, swGain, decimRate, highSignal, bits24, postshift}) → void
|
|
35
|
+
// Writes ADCCONFIG2, ADCSENS2 and ADCPOST2; this channel reports 16-bit unsigned counts.
|
|
36
|
+
await sensor.enableChannels(0x07); // CHAN_LIST, (mask) → void
|
|
37
|
+
// Channels 0, 1 and 2 now run on every FORCE.
|
|
38
|
+
console.log(await sensor.readChannels()); // Forced read of all channels, (force=true) → number[]
|
|
39
|
+
// Results in channel order: 24-bit signed for channels 0/1, 16-bit for channel 2.
|
|
40
|
+
await sensor.force(); // FORCE without waiting, () → void
|
|
41
|
+
// Starts a measurement; the results arrive in HOSTOUT when IRQ_STATUS flags the channels.
|
|
42
|
+
await sleep(100);
|
|
43
|
+
console.log(await sensor.dataReady()); // IRQ_STATUS mask, () → number
|
|
44
|
+
// Bit c set when channel c finished; this read clears the flags.
|
|
45
|
+
console.log(await sensor.readChannels(false)); // Decode current results, (force=false) → number[]
|
|
46
|
+
// No new measurement, only HOSTOUT decoded.
|
|
47
|
+
|
|
48
|
+
await sensor.setThreshold(0, 2000); // Threshold value, (index, value) → void
|
|
49
|
+
// THRESHOLD0 = 2000 counts (16-bit).
|
|
50
|
+
await sensor.setChannelThreshold(0, 1); // Threshold select, (channel, index) → void
|
|
51
|
+
// Channel 0 interrupts only when crossing THRESHOLD0 (index 1); 0 turns it off.
|
|
52
|
+
await sensor.setChannelThreshold(0, 0);
|
|
53
|
+
await sensor.setChannelCounter(2, 2); // Counter for a channel, (channel, index) → void
|
|
54
|
+
// Channel 2 uses MEASCOUNT1 in autonomous mode; 0 keeps a channel forced-only.
|
|
55
|
+
await sensor.startAutonomous(0.1, [10, 5, 1]); // Autonomous mode, (rate s, counters) → void
|
|
56
|
+
// MEASRATE = 0.1 s; channels 0/1 run every 1 s (MEASCOUNT0 = 10), channel 2 every 0.5 s.
|
|
57
|
+
await sleep(1500);
|
|
58
|
+
console.log(await sensor.pollInterrupt()); // IRQ_STATUS mask, () → number
|
|
59
|
+
// Same as dataReady; also releases the INT pin.
|
|
60
|
+
await sensor.pause(); // PAUSE, () → void
|
|
61
|
+
// Stops the autonomous measurements.
|
|
62
|
+
await sensor.setBurst(4); // Burst mode, (count) → void
|
|
63
|
+
// Four readings per burst at MEASRATE; 0 disables burst mode.
|
|
64
|
+
await sensor.setBurst(0);
|
|
65
|
+
|
|
66
|
+
await sensor.writeParameter(0x1E, 3); // PARAM_SET, (address, value) → void
|
|
67
|
+
// Direct parameter-table access: MEASCOUNT2 = 3.
|
|
68
|
+
console.log(await sensor.readParameter(0x1E)); // PARAM_QUERY, (address) → number
|
|
69
|
+
// Reads it back through RESPONSE1.
|
|
70
|
+
|
|
71
|
+
await sensor.disableInterrupt(Si1133Full.SOURCE_CHANNEL2); // IRQ_ENABLE clear, (source) → void
|
|
72
|
+
// Channel 2 no longer drives INT.
|
|
73
|
+
await sensor.enableInterrupt(Si1133Full.SOURCE_CHANNEL2); // IRQ_ENABLE set, (source) → void
|
|
74
|
+
// Channel 2 drives INT again.
|
|
75
|
+
await sensor.onInterrupt((status) => console.log('irq 0x%s', status.toString(16))); // Subscribe, (callback, intPin=null) → void
|
|
76
|
+
// INT falling edge when a pin is wired, otherwise a 10 ms IRQ_STATUS poll.
|
|
77
|
+
await sensor.readChannels();
|
|
78
|
+
await sleep(100);
|
|
79
|
+
await sensor.offInterrupt(); // Unsubscribe, () → void
|
|
80
|
+
// Stops the edge handler or polling timer.
|
|
81
|
+
|
|
82
|
+
await sensor.reset(); // RESET_SW + defaults, () → void
|
|
83
|
+
// Parameter table back to power-up values, then channel 0 = UV, channel 1 = white again.
|
|
84
|
+
// await sensor.setI2cAddress(0x56); // Change address, (address) → void
|
|
85
|
+
// Volatile; the chip answers on the new address at once, so a new connection is needed.
|
|
86
|
+
await connection.close();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Si1133Full } = require('../../../src/chips/light/si1133');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x55', 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=0x55) → Connection
|
|
11
|
+
const sensor = new Si1133Full(connection); // Create Si1133 driver, (connection) → void
|
|
12
|
+
const hex = (v) => '0x' + v.toString(16).padStart(2, '0');
|
|
13
|
+
|
|
14
|
+
async function main() {
|
|
15
|
+
await sensor.init(); // Check PART_ID, reset, UV + white channels, () → void
|
|
16
|
+
|
|
17
|
+
// --- Identity ---
|
|
18
|
+
// PART_ID 0x33 identifies the Si1133; HW_ID and REV_ID give silicon and
|
|
19
|
+
// product revision. RESPONSE0 shows the sequencer state and command counter.
|
|
20
|
+
const id = await sensor.readId(); // Identity registers, () → {partId, hwId, revId}
|
|
21
|
+
console.log('PART_ID %s HW_ID %s REV_ID %s', hex(id.partId), hex(id.hwId), hex(id.revId));
|
|
22
|
+
console.log('response', await sensor.readResponse()); // RESPONSE0 decoded, () → {state, error, counter}
|
|
23
|
+
|
|
24
|
+
// --- UV and daylight monitor: 20 s at 1 Hz ---
|
|
25
|
+
// The counts are raw ADC values; a UV index needs Silicon Labs' calibration
|
|
26
|
+
// for the product's window, so watch the trend.
|
|
27
|
+
for (let i = 0; i < 20; i++) {
|
|
28
|
+
const m = await sensor.read(); // Forced measurement, () → {uv: number counts, white: number counts}
|
|
29
|
+
console.log('%d s UV %d white %d', i, m.uv, m.white);
|
|
30
|
+
await sleep(1000);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// --- Gain settings on a constant light level ---
|
|
34
|
+
// HW_GAIN doubles the integration time per step, SW_GAIN accumulates 2^n
|
|
35
|
+
// samples, HIGH_SIGNAL divides the sensitivity by 14.5 for direct sunlight.
|
|
36
|
+
await sensor.enableChannels(0x04); // CHAN_LIST, (mask) → void
|
|
37
|
+
for (const [hwGain, swGain, highSignal] of [[2, 0, false], [6, 0, false], [6, 2, false], [6, 0, true]]) {
|
|
38
|
+
await sensor.configureChannel(2, Si1133Full.MUX_WHITE, { hwGain, swGain, highSignal }); // Channel setup, (channel, adcmux, {hwGain, swGain, highSignal}) → void
|
|
39
|
+
const [value] = await sensor.readChannels(); // Forced read, () → number[]
|
|
40
|
+
console.log('hwGain %d swGain %d highSignal %s -> %d', hwGain, swGain, highSignal, value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// --- IR photodiodes ---
|
|
44
|
+
for (const [name, mux] of [['small', Si1133Full.MUX_SMALL_IR], ['medium', Si1133Full.MUX_MEDIUM_IR],
|
|
45
|
+
['large', Si1133Full.MUX_LARGE_IR]]) {
|
|
46
|
+
console.log('IR %s %d', name, await sensor.readIr(mux)); // IR photodiode once, (photodiode) → number counts
|
|
47
|
+
}
|
|
48
|
+
console.log('UV %d, white %d', await sensor.readUv(), await sensor.readWhite()); // Scratch-channel reads, () → number counts
|
|
49
|
+
|
|
50
|
+
// --- Autonomous mode with a UV threshold ---
|
|
51
|
+
// Channels 0 and 1 run every second; channel 0 only interrupts when crossing
|
|
52
|
+
// THRESHOLD0, channel 1 on every result.
|
|
53
|
+
await sensor.configureChannel(0, Si1133Full.MUX_UV, { hwGain: 9 }); // Channel setup, (channel, adcmux, {hwGain}) → void
|
|
54
|
+
await sensor.enableChannels(0x03); // CHAN_LIST, (mask) → void
|
|
55
|
+
await sensor.setThreshold(0, 500); // Threshold value, (index, value) → void
|
|
56
|
+
await sensor.setChannelThreshold(0, 1); // Threshold select, (channel, index) → void
|
|
57
|
+
await sensor.setChannelCounter(1, 1); // Counter for a channel, (channel, index) → void
|
|
58
|
+
await sensor.disableInterrupt(Si1133Full.SOURCE_CHANNEL1); // IRQ_ENABLE clear, (source) → void
|
|
59
|
+
await sensor.enableInterrupt(Si1133Full.SOURCE_CHANNEL0); // IRQ_ENABLE set, (source) → void
|
|
60
|
+
await sensor.onInterrupt(async (status) => { // Subscribe, (callback, intPin=null) → void
|
|
61
|
+
console.log('interrupt %s values %j', hex(status), await sensor.readChannels(false));
|
|
62
|
+
});
|
|
63
|
+
await sensor.startAutonomous(0.1, [10, 10, 10]); // Autonomous mode, (rate s, counters) → void
|
|
64
|
+
await sleep(5000);
|
|
65
|
+
await sensor.offInterrupt(); // Unsubscribe, () → void
|
|
66
|
+
await sensor.pause(); // PAUSE, () → void
|
|
67
|
+
await sensor.enableInterrupt(Si1133Full.SOURCE_CHANNEL1); // IRQ_ENABLE set, (source) → void
|
|
68
|
+
|
|
69
|
+
// --- Burst mode: four readings in a row ---
|
|
70
|
+
await sensor.setBurst(4); // Burst mode, (count) → void
|
|
71
|
+
await sensor.force(); // FORCE without waiting, () → void
|
|
72
|
+
await sleep(500);
|
|
73
|
+
console.log('burst ready %s', hex(await sensor.dataReady())); // IRQ_STATUS mask, () → number
|
|
74
|
+
console.log('burst values %j', await sensor.readChannels(false)); // Decode current results, (force=false) → number[]
|
|
75
|
+
await sensor.setBurst(0);
|
|
76
|
+
|
|
77
|
+
// --- Parameter table ---
|
|
78
|
+
await sensor.writeParameter(0x1E, 7); // PARAM_SET, (address, value) → void
|
|
79
|
+
const chan = await sensor.readParameter(0x01); // PARAM_QUERY, (address) → number
|
|
80
|
+
const rate = ((await sensor.readParameter(0x1A)) << 8) | (await sensor.readParameter(0x1B));
|
|
81
|
+
console.log('CHAN_LIST %s MEASRATE %d MEASCOUNT2 %d', hex(chan), rate, await sensor.readParameter(0x1E));
|
|
82
|
+
console.log('poll', await sensor.pollInterrupt()); // IRQ_STATUS mask, () → number
|
|
83
|
+
|
|
84
|
+
// --- Finish ---
|
|
85
|
+
// setI2cAddress would move the chip to another address until the next
|
|
86
|
+
// reset; the demo keeps 0x55 and only shows the call.
|
|
87
|
+
// await sensor.setI2cAddress(0x56); // Change address, (address) → void
|
|
88
|
+
await sensor.reset(); // RESET_SW + defaults, () → void
|
|
89
|
+
console.log('after reset', await sensor.read()); // Forced measurement, () → {uv, white}
|
|
90
|
+
await connection.close();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
main().catch(err => { console.error(err); process.exit(1); });
|