periph 1.5.4 → 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.
package/README.md CHANGED
@@ -75,6 +75,7 @@ Each chip exposes two classes:
75
75
  | BNO055 | IMU | `periph/src/chips/imu/bno055` |
76
76
  | CAT24C512 | Memory | `periph/src/chips/memory/cat24c512` |
77
77
  | CAT5171 | ADC/DAC | `periph/src/chips/adc_dac/cat5171` |
78
+ | CCS811 | Gas sensor | `periph/src/chips/gas/ccs811` |
78
79
  | CS43L22 | ADC/DAC | `periph/src/chips/adc_dac/cs43l22` |
79
80
  | DHT11 | Humidity sensor | `periph/src/chips/humidity/dht11` |
80
81
  | DRV2605 | Motor driver | `periph/src/chips/motor/drv2605` |
@@ -156,17 +157,22 @@ Each chip exposes two classes:
156
157
  | PCF8578 | Display driver | `periph/src/chips/display/pcf8578` |
157
158
  | PCF8579 | Display driver | `periph/src/chips/display/pcf8579` |
158
159
  | PCF8591 | ADC/DAC | `periph/src/chips/adc_dac/pcf8591` |
160
+ | PN532 | RFID/NFC | `periph/src/chips/rfid/pn532` |
159
161
  | QMI8658A | IMU | `periph/src/chips/imu/qmi8658a` |
160
162
  | RDA5807M | Comms | `periph/src/chips/comms/rda5807m` |
161
163
  | RFM9X | Comms | `periph/src/chips/comms/rfm9x` |
164
+ | RRH46410 | Gas sensor | `periph/src/chips/gas/rrh46410` |
162
165
  | SAA1064 | LED driver | `periph/src/chips/led/saa1064` |
163
166
  | SD_SPI | Memory | `periph/src/chips/memory/sd_spi` |
164
167
  | SH1106 | Display driver | `periph/src/chips/display/sh1106` |
165
168
  | SHT31 | Humidity sensor | `periph/src/chips/humidity/sht31` |
166
169
  | SHTC3 | Humidity sensor | `periph/src/chips/humidity/shtc3` |
167
170
  | SI1132 | Light sensor | `periph/src/chips/light/si1132` |
171
+ | SI1133 | Light sensor | `periph/src/chips/light/si1133` |
172
+ | SI1145 | Light sensor | `periph/src/chips/light/si1145` |
168
173
  | SI7021 | Humidity sensor | `periph/src/chips/humidity/si7021` |
169
174
  | SK6812RGBW | LED driver | `periph/src/chips/led/sk6812rgbw` |
175
+ | SPL06 | Pressure sensor | `periph/src/chips/pressure/spl06` |
170
176
  | SSD1305 | Display driver | `periph/src/chips/display/ssd1305` |
171
177
  | SSD1306 | Display driver | `periph/src/chips/display/ssd1306` |
172
178
  | SSD1331 | Display driver | `periph/src/chips/display/ssd1331` |
@@ -0,0 +1,87 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { CCS811Full } = require('../../../src/chips/gas/ccs811');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x5A', 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=0x5A) → Connection
11
+ // Default address 0x5A (ADDR low); 0x5B with ADDR high.
12
+ const sensor = new CCS811Full(connection); // Create CCS811 driver, (connection) → void
13
+ // Constructor stores the connection only; no bus traffic yet.
14
+
15
+ async function main() {
16
+ await sensor.init(); // Check HW_ID, start application, mode 1, () → void
17
+ // Verifies HW_ID 0x81, writes APP_START if the chip is in boot mode, selects 1 s sampling.
18
+
19
+ console.log(await sensor.hardwareVersion()); // Hardware ID and version, () → [hw_id, hw_version]
20
+ // HW_ID is 0x81, HW_VERSION 0x1X.
21
+ console.log(await sensor.firmwareVersion()); // Firmware versions, () → {boot: [major, minor, trivial], app: [...]}
22
+ // Bootloader and application firmware, each major.minor.trivial.
23
+ console.log((await sensor.status()).toString(16)); // STATUS register, () → number
24
+ // Bit 7 FW_MODE, bit 4 APP_VALID, bit 3 DATA_READY, bit 0 ERROR.
25
+ console.log((await sensor.error()).toString(16)); // ERROR_ID register, () → number
26
+ // Zero unless STATUS.ERROR is set.
27
+
28
+ console.log(await sensor.read()); // Next sample, () → {eco2_ppm: number ppm, tvoc_ppb: number ppb}
29
+ // Waits for DATA_READY, then reads eCO2 and TVOC.
30
+ console.log(await sensor.readEco2()); // eCO2 only, () → number ppm
31
+ // Equivalent CO2 estimated from the VOC level, 400–8192 ppm.
32
+ console.log(await sensor.readTvoc()); // TVOC only, () → number ppb
33
+ // Total volatile organic compounds, 0–1187 ppb.
34
+ console.log(await sensor.readRaw()); // Raw sensor data, () → [current_ua µA, voltage_v V]
35
+ // Sense current and the voltage across the sensor.
36
+ console.log(await sensor.dataReady()); // New sample waiting, () → boolean
37
+ // False right after a read, because reading the result clears DATA_READY.
38
+
39
+ await sensor.setEnvironment(45.0, 22.5); // Compensation data, (humidityRh %, temperatureC °C) → void
40
+ // Real ambient values improve accuracy; the chip assumes 50 %RH and 25 °C otherwise.
41
+ console.log(await sensor.readNtc()); // NTC circuit voltages, () → [v_ref_mv mV, v_ntc_mv mV]
42
+ // Only meaningful with a reference resistor and NTC wired to the AUX pin.
43
+ try {
44
+ console.log(await sensor.ntcResistance(100000)); // NTC resistance, (rRefOhm Ω) → number Ω
45
+ // V_NTC × R_REF / V_REF; map it to °C with the thermistor's curve.
46
+ } catch (e) {
47
+ console.log('no NTC circuit:', e.message);
48
+ }
49
+
50
+ await sensor.setThresholds(1000, 2000, 50); // Threshold boundaries, (lowMediumPpm=1500, mediumHighPpm=2500, hysteresisPpm=50) → void
51
+ // eCO2 zones used by the threshold interrupt.
52
+ const baseline = await sensor.getBaseline(); // Algorithm baseline, () → number
53
+ // Save it to non-volatile storage about once a day.
54
+ console.log(baseline.toString(16));
55
+ await sensor.setBaseline(baseline); // Restore baseline, (baseline) → void
56
+ // Restore a saved value only after about 20 min of warm-up.
57
+
58
+ await sensor.enableInterrupt(CCS811Full.SOURCE_DATA_READY); // nINT on new data, (source) → void
59
+ // Sets INT_DATARDY and keeps the drive mode.
60
+ await sensor.onInterrupt(async (st) => { // Subscribe, (callback, intPin=null) → void
61
+ console.log('interrupt status 0x%s', st.toString(16), await sensor.read());
62
+ });
63
+ // Without an INT pin the driver polls STATUS; reading the sample releases nINT.
64
+ await sleep(3000);
65
+ await sensor.offInterrupt(); // Unsubscribe, () → void
66
+ // Stops the pin handler or polling timer.
67
+ console.log((await sensor.pollInterrupt()).toString(16)); // STATUS without releasing nINT, () → number
68
+ // DATA_READY shows whether a sample is pending.
69
+ await sensor.disableInterrupt(CCS811Full.SOURCE_DATA_READY); // nINT off, (source) → void
70
+ // Clears INT_DATARDY.
71
+
72
+ await sensor.setMode(4); // Drive mode, (mode 0–4) → void
73
+ // Mode 4: raw data every 250 ms, no eCO2/TVOC updates.
74
+ await sleep(500);
75
+ console.log(await sensor.readRaw()); // Raw sensor data, () → [current_ua µA, voltage_v V]
76
+ // RAW_DATA keeps updating in mode 4.
77
+ await sensor.setMode(0); // Drive mode, (mode 0–4) → void
78
+ // Idle before switching to another mode.
79
+ await sensor.setMode(1); // Drive mode, (mode 0–4) → void
80
+ // Back to one sample per second.
81
+
82
+ await sensor.softReset(); // Software reset, () → void
83
+ // SW_RESET into boot mode, then APP_START and mode 1 again.
84
+ await connection.close();
85
+ }
86
+
87
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,108 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { CCS811Full } = require('../../../src/chips/gas/ccs811');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x5A', 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=0x5A) → Connection
11
+ const sensor = new CCS811Full(connection); // Create CCS811 driver, (connection) → void
12
+
13
+ function classify(eco2) {
14
+ if (eco2 < 1000) return 'good';
15
+ if (eco2 <= 1500) return 'fair';
16
+ return 'poor';
17
+ }
18
+
19
+ async function main() {
20
+ await sensor.init(); // Check HW_ID, start application, mode 1, () → void
21
+
22
+ // --- Identity ---
23
+ // HW_ID 0x81 identifies the CCS81x family; the firmware versions tell which
24
+ // algorithm revision computes eCO2 and TVOC.
25
+ console.log('hardware', await sensor.hardwareVersion()); // Hardware ID and version, () → [hw_id, hw_version]
26
+ console.log('firmware', await sensor.firmwareVersion()); // Firmware versions, () → {boot, app}
27
+ const st = await sensor.status(); // STATUS, () → number
28
+ const err = await sensor.error(); // ERROR_ID, () → number
29
+ console.log('status 0x%s, error 0x%s', st.toString(16), err.toString(16));
30
+
31
+ // --- Air-quality monitor: 30 s at 1 Hz ---
32
+ // Mode 1 delivers a sample every second. Values settle after about 20 min of
33
+ // warm-up; a brand-new sensor needs a 48 h burn-in.
34
+ for (let i = 0; i < 30; i++) {
35
+ const m = await sensor.read(); // Next sample, () → {eco2_ppm ppm, tvoc_ppb ppb}
36
+ console.log('%d s eCO2 %d ppm TVOC %d ppb %s', i, m.eco2_ppm, m.tvoc_ppb, classify(m.eco2_ppm));
37
+ }
38
+ console.log('eCO2 %d ppm', await sensor.readEco2()); // eCO2 only, () → number ppm
39
+ console.log('TVOC %d ppb', await sensor.readTvoc()); // TVOC only, () → number ppb
40
+
41
+ // --- Environment compensation ---
42
+ // Feeding the real temperature and humidity (e.g. from a BME280) corrects the
43
+ // algorithm; without it the chip assumes 25 °C and 50 %RH.
44
+ await sensor.setEnvironment(40.0, 23.0); // Compensation data, (humidityRh %, temperatureC °C) → void
45
+ const [vRef] = await sensor.readNtc(); // NTC voltages, () → [v_ref_mv mV, v_ntc_mv mV]
46
+ if (vRef) {
47
+ console.log('NTC %s ohm', (await sensor.ntcResistance(10000)).toFixed(0)); // NTC resistance, (rRefOhm Ω) → number Ω
48
+ } else {
49
+ console.log('no NTC circuit fitted');
50
+ }
51
+
52
+ // --- Threshold interrupt: only report zone changes ---
53
+ // With INT_THRESH the chip raises nINT only when eCO2 crosses 1000 or
54
+ // 2000 ppm by more than the hysteresis. Polling STATUS here shows each sample.
55
+ await sensor.setThresholds(1000, 2000, 50); // Zone boundaries, (lowMediumPpm, mediumHighPpm, hysteresisPpm) → void
56
+ await sensor.enableInterrupt(CCS811Full.SOURCE_THRESHOLD); // Threshold interrupt, (source) → void
57
+ for (let i = 0; i < 5; i++) {
58
+ while (!((await sensor.pollInterrupt()) & CCS811Full.STATUS_DATA_READY)) { // STATUS, () → number
59
+ await sleep(100);
60
+ }
61
+ console.log('event', await sensor.read()); // Reading releases nINT, () → {eco2_ppm, tvoc_ppb}
62
+ }
63
+ await sensor.disableInterrupt(CCS811Full.SOURCE_THRESHOLD); // Threshold interrupt off, (source) → void
64
+
65
+ // --- Data-ready callback ---
66
+ // The callback gets the STATUS byte; without an INT pin the driver polls.
67
+ await sensor.enableInterrupt(CCS811Full.SOURCE_DATA_READY); // Data-ready interrupt, (source) → void
68
+ await sensor.onInterrupt(async (s) => { // Subscribe, (callback, intPin=null) → void
69
+ console.log('callback 0x%s', s.toString(16), await sensor.read());
70
+ });
71
+ await sleep(3000);
72
+ await sensor.offInterrupt(); // Unsubscribe, () → void
73
+ await sensor.disableInterrupt(CCS811Full.SOURCE_DATA_READY); // Data-ready interrupt off, (source) → void
74
+
75
+ // --- Raw sensor data ---
76
+ // The MOx sensor's current and voltage give its resistance; the algorithm
77
+ // derives eCO2 and TVOC from it.
78
+ const [current, voltage] = await sensor.readRaw(); // Raw data, () → [current_ua µA, voltage_v V]
79
+ const r = current ? voltage / (current * 1e-6) : 0;
80
+ console.log('raw %d uA, %s V, %s ohm', current, voltage.toFixed(3), r.toFixed(0));
81
+
82
+ // --- Slower sampling: mode 2 (every 10 s) ---
83
+ // Pulse heating saves power; pass through idle when changing modes.
84
+ await sensor.setMode(0); // Idle, (mode) → void
85
+ await sensor.setMode(2); // Every 10 s, (mode) → void
86
+ for (let i = 0; i < 2; i++) {
87
+ const m = await sensor.read(); // Next sample, () → {eco2_ppm, tvoc_ppb}
88
+ console.log('mode 2', m, 'ready', await sensor.dataReady()); // Ready flag, () → boolean
89
+ }
90
+ await sensor.setMode(0); // Idle, (mode) → void
91
+ await sensor.setMode(1); // Back to every second, (mode) → void
92
+
93
+ // --- Baseline ---
94
+ // Save the baseline daily and restore it after a power cycle, but only after
95
+ // about 20 min of warm-up; otherwise the chip relearns it from scratch.
96
+ const baseline = await sensor.getBaseline(); // Baseline, () → number
97
+ console.log('baseline 0x%s', baseline.toString(16).padStart(4, '0'));
98
+ await sensor.setBaseline(baseline); // Restore baseline, (baseline) → void
99
+
100
+ // --- Wrap up ---
101
+ // A soft reset puts the chip into boot mode; the driver starts the
102
+ // application again in mode 1.
103
+ await sensor.softReset(); // Software reset, () → void
104
+ console.log('status after reset 0x%s', (await sensor.status()).toString(16)); // STATUS, () → number
105
+ await connection.close();
106
+ }
107
+
108
+ 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 { CCS811Minimal } = require('../../../src/chips/gas/ccs811');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x5A', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x5A) → Connection
10
+ const sensor = new CCS811Minimal(connection); // Create CCS811 driver, (connection) → void
11
+
12
+ async function main() {
13
+ await sensor.init(); // Check HW_ID, start application, mode 1, () → void
14
+ while (true) {
15
+ const m = await sensor.read(); // Wait for the next sample, () → {eco2_ppm: number ppm, tvoc_ppb: number ppb}
16
+ console.log('eCO2 %d ppm, TVOC %d ppb', m.eco2_ppm, m.tvoc_ppb);
17
+ }
18
+ }
19
+
20
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -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,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); });