periph 1.6.0 → 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.
Files changed (47) hide show
  1. package/README.md +11 -0
  2. package/examples/gas/rrh62000/complete.js +74 -0
  3. package/examples/gas/rrh62000/demo.js +87 -0
  4. package/examples/gas/rrh62000/minimal.js +22 -0
  5. package/examples/gas/scd30/complete.js +73 -0
  6. package/examples/gas/scd30/demo.js +102 -0
  7. package/examples/gas/scd30/minimal.js +20 -0
  8. package/examples/gas/scd40/complete.js +76 -0
  9. package/examples/gas/scd40/demo.js +109 -0
  10. package/examples/gas/scd40/minimal.js +20 -0
  11. package/examples/light/tsl2561/complete.js +74 -0
  12. package/examples/light/tsl2561/demo.js +46 -0
  13. package/examples/light/tsl2561/minimal.js +22 -0
  14. package/examples/light/tsl2591/complete.js +90 -0
  15. package/examples/light/tsl2591/demo.js +119 -0
  16. package/examples/light/tsl2591/minimal.js +22 -0
  17. package/examples/light/vcnl4040/complete.js +102 -0
  18. package/examples/light/vcnl4040/demo.js +125 -0
  19. package/examples/light/vcnl4040/minimal.js +25 -0
  20. package/examples/light/vcnl4200/complete.js +101 -0
  21. package/examples/light/vcnl4200/demo.js +126 -0
  22. package/examples/light/vcnl4200/minimal.js +25 -0
  23. package/examples/rfid/st25dv/complete.js +94 -0
  24. package/examples/rfid/st25dv/demo.js +121 -0
  25. package/examples/rfid/st25dv/minimal.js +21 -0
  26. package/examples/rtc/bq32000/complete.js +53 -0
  27. package/examples/rtc/bq32000/demo.js +77 -0
  28. package/examples/rtc/bq32000/minimal.js +20 -0
  29. package/examples/rtc/ds1307/complete.js +54 -0
  30. package/examples/rtc/ds1307/demo.js +96 -0
  31. package/examples/rtc/ds1307/minimal.js +20 -0
  32. package/examples/rtc/ds1371/complete.js +53 -0
  33. package/examples/rtc/ds1371/demo.js +85 -0
  34. package/examples/rtc/ds1371/minimal.js +25 -0
  35. package/package.json +1 -1
  36. package/src/chips/gas/rrh62000.js +418 -0
  37. package/src/chips/gas/scd30.js +368 -0
  38. package/src/chips/gas/scd40.js +451 -0
  39. package/src/chips/light/tsl2561.js +445 -0
  40. package/src/chips/light/tsl2591.js +561 -0
  41. package/src/chips/light/vcnl4040.js +576 -0
  42. package/src/chips/light/vcnl4200.js +592 -0
  43. package/src/chips/rfid/st25dv.js +628 -0
  44. package/src/chips/rtc/bq32000.js +334 -0
  45. package/src/chips/rtc/ds1307.js +327 -0
  46. package/src/chips/rtc/ds1371.js +453 -0
  47. package/src/discovery/registry.js +11 -11
@@ -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,74 @@
1
+ 'use strict';
2
+
3
+ // Complete example: exercise every method in the public API.
4
+ // Reads lux and the raw channel counts, changes gain, integration time and the
5
+ // package coefficients, auto-ranges, runs one host-timed manual integration,
6
+ // programs the interrupt window and handles interrupts, then powers down.
7
+
8
+ const { I2CConnection } = require('periph/src/connection/i2c');
9
+ const { TSL2561Full, calculateLux } = require('periph/src/chips/light/tsl2561');
10
+
11
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
12
+
13
+ (async () => {
14
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
15
+ const connection = new I2CConnection(bus, TSL2561Full.I2C_ADDRESS);
16
+ const sensor = new TSL2561Full(connection); // Create TSL2561 Full driver, (connection)
17
+ await sensor.init(); // Run initialization sequence, () → void
18
+ // CONTROL ← 0x03 and read back, TIMING ← 0x02 (1×, 402 ms), waits 450 ms
19
+
20
+ console.log((await sensor.readId()).toString(16)); // Read the ID register, () → int
21
+ // bits 7:4 = 1 for the TSL2561, bits 3:0 revision
22
+ console.log(await sensor.readLux()); // Read illuminance, () → float lux
23
+ // throws on a saturated channel
24
+ const { ch0, ch1 } = await sensor.readRaw(); // Read both channels, () → {ch0, ch1}
25
+ // one block read: ch0 visible+IR, ch1 IR
26
+ console.log(await sensor.readLuxRaw()); // Lux plus counts, () → {lux, ch0, ch1}
27
+ console.log(calculateLux(ch0, ch1, 1, 402, 'T')); // Convert counts, (ch0, ch1, gain, integrationMs, package) → float lux
28
+
29
+ await sensor.setGain(16); // Set ADC gain, (gain 1|16) → void
30
+ // 16× for dim light; saturates indoors at ~1–2 klx
31
+ await sensor.setIntegrationTime(101); // Set integration time, (ms 14|101|402) → void
32
+ // the conversion in progress keeps the old setting
33
+ console.log(sensor.getGain(), sensor.getIntegrationTime()); // Current settings, () → int, () → int ms
34
+ await sleep(250);
35
+ sensor.setPackage('CS'); // Select lux coefficients, (package "T"|"FN"|"CL"|"CS") → void
36
+ // T, FN and CL share one set; CS has its own
37
+ console.log(await sensor.readLux());
38
+ sensor.setPackage('T');
39
+ console.log(await sensor.autoRange()); // Auto-range and read lux, () → float lux
40
+ // 16×/402 ms … 1×/13.7 ms until no saturation and ch0 > 100
41
+
42
+ await sensor.startManual(); // Start a host-timed integration, () → void
43
+ // INTEG = 11, MANUAL = 1
44
+ await sleep(200);
45
+ await sensor.stopManual(); // Stop the integration, () → void
46
+ // INTEG = 11, MANUAL = 0; counts are ready
47
+ console.log(await sensor.readRaw());
48
+ await sensor.setIntegrationTime(402);
49
+ await sensor.setGain(1);
50
+
51
+ await sensor.setThresholds(100, 20000); // Set channel-0 window, (low, high counts 0–65535) → void
52
+ // word writes of 0x2–0x3 and 0x4–0x5
53
+ console.log(await sensor.getThresholds()); // Read the window, () → {low, high}
54
+ await sensor.configureInterrupt(1, 2); // Interrupt mode, (mode 0|1|2, persist 0–15) → void
55
+ // level interrupt after 2 cycles outside the window
56
+ console.log(await sensor.pollInterrupt()); // Check ch0 against the window, () → bool
57
+ // does not clear the chip's interrupt
58
+ await sensor.clearInterrupt(); // Release a level interrupt, () → void
59
+ // command byte 0xC0
60
+ await sensor.onInterrupt((c0) => console.log('interrupt', c0)); // Subscribe, (callback(ch0), {intPin}) → void
61
+ // falling edge on INT, or poll once per integration
62
+ await sleep(10000);
63
+ await sensor.offInterrupt(); // Unsubscribe, () → void
64
+ await sensor.configureInterrupt(0, 0);
65
+
66
+ console.log(await sensor.isPowered()); // Power state, () → bool
67
+ // CONTROL reads 0x03 when powered
68
+ await sensor.powerDown(); // Power down, () → void
69
+ // CONTROL = 0x00; registers keep their values
70
+ await sensor.powerUp(); // Power up, () → void
71
+ // CONTROL = 0x03; integration restarts
72
+
73
+ await connection.close();
74
+ })();
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ // Demo: a light meter with alarm.
4
+ // Prints the ID, then takes an auto-ranged lux reading every 2 s for 30 s with
5
+ // the selected gain, integration time and both channel counts. Then it sets an
6
+ // interrupt window of ±50 % around the current channel-0 count with persistence
7
+ // 2, reports pollInterrupt() while you cover the sensor, releases the
8
+ // interrupt and powers the chip down.
9
+
10
+ const { I2CConnection } = require('periph/src/connection/i2c');
11
+ const { TSL2561Full } = require('periph/src/chips/light/tsl2561');
12
+
13
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
14
+
15
+ (async () => {
16
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
17
+ const connection = new I2CConnection(bus, TSL2561Full.I2C_ADDRESS);
18
+ const sensor = new TSL2561Full(connection); // Create TSL2561 Full driver, (connection)
19
+ await sensor.init(); // Run initialization sequence, () → void
20
+ console.log('ID 0x' + (await sensor.readId()).toString(16)); // Read the ID register, () → int
21
+
22
+ /* Light meter: auto-range picks the most sensitive setting that does not saturate. */
23
+ for (let i = 0; i < 15; i++) {
24
+ const lux = await sensor.autoRange(); // Auto-range and read lux, () → float lux
25
+ const { ch0, ch1 } = await sensor.readRaw(); // Read both channels, () → {ch0, ch1}
26
+ console.log(`${lux.toFixed(1)} lx gain ${sensor.getGain()}x ${sensor.getIntegrationTime()} ms ch0 ${ch0} ch1 ${ch1}`); // Current settings, () → int
27
+ await sleep(2000);
28
+ }
29
+
30
+ /* Alarm: a window of ±50 % around the current channel-0 count. */
31
+ const { ch0 } = await sensor.readRaw();
32
+ await sensor.setThresholds(Math.floor(ch0 / 2), Math.min(65535, Math.floor(ch0 * 1.5))); // Set channel-0 window, (low, high) → void
33
+ await sensor.configureInterrupt(1, 2); // Level interrupt, 2 cycles outside, (mode, persist) → void
34
+ console.log('window', await sensor.getThresholds(), '- cover the sensor now'); // Read the window, () → {low, high}
35
+ for (let i = 0; i < 10; i++) {
36
+ console.log('outside window:', await sensor.pollInterrupt()); // Check ch0 against the window, () → bool
37
+ await sleep(1000);
38
+ }
39
+
40
+ await sensor.clearInterrupt(); // Release the level interrupt, () → void
41
+ await sensor.configureInterrupt(0, 0);
42
+ await sensor.powerDown(); // Power down, () → void
43
+ console.log('powered:', await sensor.isPowered()); // Power state, () → bool
44
+
45
+ await connection.close();
46
+ })();
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ // Minimal example: read the illuminance once a second (1× gain, 402 ms, T/FN/CL coefficients).
4
+
5
+ const { I2CConnection } = require('periph/src/connection/i2c');
6
+ const { TSL2561Minimal } = require('periph/src/chips/light/tsl2561');
7
+
8
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
9
+
10
+ (async () => {
11
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
12
+ const connection = new I2CConnection(bus, TSL2561Minimal.I2C_ADDRESS);
13
+ const sensor = new TSL2561Minimal(connection); // Create TSL2561 driver, (connection)
14
+ await sensor.init(); // Run initialization sequence, () → void (powers up, waits one integration)
15
+
16
+ for (let i = 0; i < 10; i++) {
17
+ console.log((await sensor.readLux()).toFixed(1), 'lx'); // Read illuminance, () → float lux
18
+ await sleep(1000);
19
+ }
20
+
21
+ await connection.close();
22
+ })();
@@ -0,0 +1,90 @@
1
+ 'use strict';
2
+
3
+ // Complete example: exercise every method in the public API.
4
+ // Reads lux and the raw channel counts, changes gain and integration time,
5
+ // auto-gains, computes lux from raw counts, reads the status, programs both
6
+ // threshold windows, persistence and sleep-after-interrupt, uses the
7
+ // special-function interrupt commands and interrupt delivery, then powers
8
+ // down, up and resets the chip.
9
+
10
+ const { I2CConnection } = require('periph/src/connection/i2c');
11
+ const { TSL2591Full, calculateLux, saturationCount } = require('periph/src/chips/light/tsl2591');
12
+
13
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
14
+
15
+ (async () => {
16
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
17
+ const connection = new I2CConnection(bus, TSL2591Full.I2C_ADDRESS);
18
+ const sensor = new TSL2591Full(connection); // Create TSL2591 Full driver, (connection)
19
+ await sensor.init(); // Run initialization sequence, () → void
20
+ // checks ID 0x50, CONFIG ← 0x10 (medium, 100 ms), ENABLE ← 0x03, waits for AVALID
21
+
22
+ console.log((await sensor.deviceId()).toString(16)); // Read the ID register, () → int
23
+ // 0x50 for the TSL2591 / TSL25911
24
+ console.log(await sensor.read()); // Read illuminance, () → float lux
25
+ // throws on a saturated channel
26
+ console.log(await sensor.readLux()); // Read illuminance, () → float lux
27
+ // same as read()
28
+ const { ch0, ch1 } = await sensor.readRaw(); // Read both channels, () → {ch0, ch1}
29
+ // one block read at 0xB4: ch0 visible+IR, ch1 IR
30
+ console.log(ch0, ch1);
31
+ console.log(sensor.luxFromRaw(ch0, ch1, TSL2591Full.GAIN_MEDIUM, TSL2591Full.ATIME_100MS)); // Lux from counts, (ch0, ch1, gain, atime) → float lux
32
+ // pure computation of the ams formula, no bus access
33
+ console.log(calculateLux(ch0, ch1, TSL2591Full.GAIN_MEDIUM, TSL2591Full.ATIME_100MS)); // Lux from counts, (ch0, ch1, gain, atime) → float lux
34
+ // module-level form of luxFromRaw()
35
+ console.log(saturationCount(TSL2591Full.ATIME_100MS)); // Full-scale count, (atime) → int
36
+ // 36863 at 100 ms, 65535 at 200 ms and longer
37
+
38
+ await sensor.setGain(TSL2591Full.GAIN_HIGH); // Set ADC gain, (gain GAIN_LOW…GAIN_MAX) → void
39
+ // ≈ 428×; restarts the integration
40
+ console.log(sensor.getGain()); // Current gain, () → int
41
+ // GAIN_LOW 0x00 … GAIN_MAX 0x30
42
+ await sensor.setIntegrationTime(TSL2591Full.ATIME_300MS); // Set integration time, (atime 0–5) → void
43
+ // 300 ms; restarts the integration
44
+ console.log(sensor.getIntegrationTime()); // Current integration setting, () → int
45
+ // ATIME_100MS 0 … ATIME_600MS 5
46
+ console.log(sensor.integrationTime()); // Integration time, () → float s
47
+ // (ATIME + 1) × 0.1
48
+ console.log(await sensor.autoGain()); // Auto-gain, (minCount=100, maxCount=30000) → int
49
+ // steps the gain until ch0 lies within the limits
50
+ await sensor.setIntegrationTime(TSL2591Full.ATIME_100MS);
51
+
52
+ console.log(await sensor.dataValid()); // Integration complete, () → bool
53
+ // STATUS.AVALID
54
+ console.log(await sensor.status()); // Decoded status, () → {avalid, aint, npintr}
55
+ // AVALID, AINT and NPINTR bits of STATUS
56
+
57
+ await sensor.setThresholds(100, 20000); // Set persist window, (low, high counts 0–65535, noPersist=false) → void
58
+ // AILT/AIHT, compared with channel 0
59
+ await sensor.setThresholds(50, 30000, true); // Set no-persist window, (low, high, noPersist=true) → void
60
+ // NPAILT/NPAIHT, bypasses the persist filter
61
+ await sensor.setPersistence(3); // Persist filter, (cycles 0–60) → void
62
+ // 3 consecutive out-of-range cycles raise AINT
63
+ await sensor.setSleepAfterInterrupt(false); // Sleep after interrupt, (enabled) → void
64
+ // ENABLE.SAI: power down after an interrupt cycle
65
+ await sensor.enableInterrupt(TSL2591Full.SOURCE_ALS); // Enable interrupt source, (source) → void
66
+ // ENABLE.AIEN drives INT on AINT
67
+ await sensor.forceInterrupt(); // Force an interrupt, () → void
68
+ // special function 0xE4 sets AINT and NPINTR
69
+ console.log(await sensor.pollInterrupt()); // Pending sources, () → int
70
+ // reads STATUS and clears what it reports
71
+ await sensor.clearInterrupt({ both: true }); // Clear interrupts, ({noPersist=false, both=false}) → void
72
+ // 0xE6 ALS, 0xEA no-persist, 0xE7 both
73
+ await sensor.onInterrupt((s) => console.log('interrupt', s.toString(16))); // Subscribe, (callback(status), {intPin}) → void
74
+ // falling edge on INT, or poll once per integration
75
+ await sleep(10000);
76
+ await sensor.offInterrupt(); // Unsubscribe, () → void
77
+ await sensor.disableInterrupt(TSL2591Full.SOURCE_ALS | TSL2591Full.SOURCE_NO_PERSIST); // Disable interrupt sources, (source) → void
78
+ // clears ENABLE.AIEN / NPIEN
79
+
80
+ await sensor.disable(); // Power down, () → void
81
+ // ENABLE.PON = AEN = 0
82
+ await sensor.enable(); // Power up, () → void
83
+ // ENABLE.PON | AEN; first data after one integration
84
+ await sensor.setGain(TSL2591Full.GAIN_LOW);
85
+ await sensor.reset(); // Reset the chip, () → void
86
+ // CONFIG.SRESET, then the Minimal defaults again
87
+ console.log(await sensor.read());
88
+
89
+ await connection.close();
90
+ })();
@@ -0,0 +1,119 @@
1
+ 'use strict';
2
+
3
+ // Demo: a wide-range light meter.
4
+ // Prints the ID, reads raw counts and lux once a second for 15 s at medium
5
+ // gain, auto-gains while you cover and uncover the sensor, compares the lux
6
+ // estimate across all gains and three integration times, sets a threshold
7
+ // window with persistence 3 and reports the lux whenever it is left, shows the
8
+ // no-persist interrupt, forced interrupts, sleep-after-interrupt, power
9
+ // control and reset.
10
+
11
+ const { I2CConnection } = require('periph/src/connection/i2c');
12
+ const { TSL2591Full } = require('periph/src/chips/light/tsl2591');
13
+
14
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
15
+ const GAIN_NAMES = { 0x00: 'low', 0x10: 'medium', 0x20: 'high', 0x30: 'max' };
16
+
17
+ (async () => {
18
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
19
+ const connection = new I2CConnection(bus, TSL2591Full.I2C_ADDRESS);
20
+ const sensor = new TSL2591Full(connection); // Create TSL2591 Full driver, (connection)
21
+ await sensor.init(); // Run initialization sequence, () → void
22
+ console.log('ID 0x' + (await sensor.deviceId()).toString(16)); // Read the ID register, () → int
23
+
24
+ // --- Light meter at the default medium gain ---
25
+ // Medium gain and 100 ms suit indoor light; both channels are read in one
26
+ // block so the pair always belongs to the same integration.
27
+ for (let i = 0; i < 15; i++) {
28
+ const { ch0, ch1 } = await sensor.readRaw(); // Read both channels, () → {ch0, ch1}
29
+ const lux = await sensor.read(); // Read illuminance, () → float lux
30
+ console.log(`ch0 ${ch0} ch1 ${ch1} ${lux.toFixed(2)} lx`);
31
+ await sleep(1000);
32
+ }
33
+
34
+ // --- Auto-gain: cover and uncover the sensor ---
35
+ // The gain steps up in darkness and down in bright light until channel 0
36
+ // lies between 100 and 30 000 counts.
37
+ for (let i = 0; i < 10; i++) {
38
+ const gain = await sensor.autoGain(); // Auto-gain, (minCount=100, maxCount=30000) → int
39
+ const lux = await sensor.readLux(); // Read illuminance, () → float lux
40
+ console.log(`gain ${GAIN_NAMES[gain]} ${sensor.integrationTime().toFixed(1)} s ${lux.toFixed(2)} lx`); // Integration time, () → float s
41
+ await sleep(1000);
42
+ }
43
+
44
+ // --- Compare the lux estimate across settings on a constant light ---
45
+ // Every gain/integration pair should give about the same lux; saturated
46
+ // settings are reported instead of a meaningless value.
47
+ for (const atime of [TSL2591Full.ATIME_100MS, TSL2591Full.ATIME_200MS, TSL2591Full.ATIME_400MS]) {
48
+ await sensor.setIntegrationTime(atime); // Set integration time, (atime 0–5) → void
49
+ for (const gain of [TSL2591Full.GAIN_LOW, TSL2591Full.GAIN_MEDIUM, TSL2591Full.GAIN_HIGH, TSL2591Full.GAIN_MAX]) {
50
+ await sensor.setGain(gain); // Set ADC gain, (gain) → void
51
+ const { ch0, ch1 } = await sensor.readRaw(); // Read both channels, () → {ch0, ch1}
52
+ const ms = (sensor.getIntegrationTime() + 1) * 100; // Current integration setting, () → int
53
+ try {
54
+ const lux = await sensor.readLux(); // Read illuminance, () → float lux
55
+ const calc = sensor.luxFromRaw(ch0, ch1, gain, atime); // Lux from counts, (ch0, ch1, gain, atime) → float lux
56
+ console.log(`${ms} ms ${GAIN_NAMES[sensor.getGain()]} ${lux.toFixed(2)} lx (formula ${calc.toFixed(2)})`); // Current gain, () → int
57
+ } catch (e) {
58
+ console.log(`${ms} ms ${GAIN_NAMES[gain]} saturated`);
59
+ }
60
+ }
61
+ }
62
+ await sensor.setIntegrationTime(TSL2591Full.ATIME_100MS);
63
+ await sensor.setGain(TSL2591Full.GAIN_MEDIUM);
64
+
65
+ // --- Alarm: report the lux whenever the window is left ---
66
+ // A window of ±50 % around the current channel-0 count with persistence 3
67
+ // ignores short flickers; cover the sensor to trigger it.
68
+ const { ch0 } = await sensor.readRaw();
69
+ const low = Math.floor(ch0 / 2);
70
+ const high = Math.min(65535, Math.floor(ch0 * 3 / 2));
71
+ await sensor.setThresholds(low, high); // Set persist window, (low, high, noPersist=false) → void
72
+ await sensor.setPersistence(3); // Persist filter, (cycles 0–60) → void
73
+ await sensor.clearInterrupt({ both: true }); // Clear interrupts, ({noPersist=false, both=false}) → void
74
+ await sensor.enableInterrupt(TSL2591Full.SOURCE_ALS); // Enable interrupt source, (source) → void
75
+ await sensor.onInterrupt(async () => { // Subscribe, (callback(status), {intPin}) → void
76
+ console.log(`left window: ${(await sensor.readLux()).toFixed(2)} lx`);
77
+ });
78
+ console.log('cover the sensor now');
79
+ await sleep(10000);
80
+ await sensor.offInterrupt(); // Unsubscribe, () → void
81
+ for (let i = 0; i < 5; i++) {
82
+ console.log('pending: 0x' + (await sensor.pollInterrupt()).toString(16)); // Pending sources, () → int
83
+ await sleep(1000);
84
+ }
85
+ await sensor.disableInterrupt(TSL2591Full.SOURCE_ALS); // Disable interrupt source, (source) → void
86
+
87
+ // --- No-persist interrupt ---
88
+ // The no-persist window fires after a single integration outside it.
89
+ await sensor.setThresholds(low, high, true); // Set no-persist window, (low, high, noPersist=true) → void
90
+ await sensor.enableInterrupt(TSL2591Full.SOURCE_NO_PERSIST); // Enable interrupt source, (source) → void
91
+ console.log('cover the sensor briefly');
92
+ for (let i = 0; i < 5; i++) {
93
+ console.log('no-persist pending:', (await sensor.status()).npintr); // Decoded status, () → {avalid, aint, npintr}
94
+ await sleep(1000);
95
+ }
96
+ await sensor.clearInterrupt({ noPersist: true }); // Clear interrupts, ({noPersist=false, both=false}) → void
97
+ await sensor.disableInterrupt(TSL2591Full.SOURCE_NO_PERSIST); // Disable interrupt source, (source) → void
98
+
99
+ // --- Forced interrupt and sleep-after-interrupt ---
100
+ // 0xE4 sets both interrupt flags by software; with SAI set the chip powers
101
+ // down after an interrupt cycle until the interrupt is cleared.
102
+ await sensor.forceInterrupt(); // Force an interrupt, () → void
103
+ console.log('forced:', await sensor.status()); // Decoded status, () → {avalid, aint, npintr}
104
+ await sensor.clearInterrupt({ both: true }); // Clear interrupts, ({noPersist=false, both=false}) → void
105
+ await sensor.setSleepAfterInterrupt(true); // Sleep after interrupt, (enabled) → void
106
+ await sensor.setSleepAfterInterrupt(false); // Sleep after interrupt, (enabled) → void
107
+
108
+ // --- Power control and reset ---
109
+ // disable() powers the chip down; enable() restarts conversion and the first
110
+ // reading waits one integration. reset() restores the Minimal defaults.
111
+ await sensor.disable(); // Power down, () → void
112
+ console.log('valid after disable:', await sensor.dataValid()); // Integration complete, () → bool
113
+ await sensor.enable(); // Power up, () → void
114
+ console.log((await sensor.read()).toFixed(2), 'lx'); // Read illuminance, () → float lux
115
+ await sensor.reset(); // Reset the chip, () → void
116
+ console.log('after reset: gain', GAIN_NAMES[sensor.getGain()], 'atime', sensor.getIntegrationTime()); // Current settings, () → int
117
+
118
+ await connection.close();
119
+ })();
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ // Minimal example: read the illuminance once a second (medium gain ≈ 25×, 100 ms, continuous).
4
+
5
+ const { I2CConnection } = require('periph/src/connection/i2c');
6
+ const { TSL2591Minimal } = require('periph/src/chips/light/tsl2591');
7
+
8
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
9
+
10
+ (async () => {
11
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
12
+ const connection = new I2CConnection(bus, TSL2591Minimal.I2C_ADDRESS);
13
+ const sensor = new TSL2591Minimal(connection); // Create TSL2591 driver, (connection)
14
+ await sensor.init(); // Run initialization sequence, () → void (checks ID, waits for AVALID)
15
+
16
+ for (let i = 0; i < 10; i++) {
17
+ console.log((await sensor.read()).toFixed(2), 'lx'); // Read illuminance, () → float lux
18
+ await sleep(1000);
19
+ }
20
+
21
+ await connection.close();
22
+ })();
@@ -0,0 +1,102 @@
1
+ 'use strict';
2
+
3
+ // Complete example: exercise every method of VCNL4040Full — proximity, lux,
4
+ // raw ALS / white channels, ALS and PS configuration, thresholds,
5
+ // cancellation, active force, logic output, ID, status and interrupts.
6
+
7
+ const { I2CConnection } = require('periph/src/connection/i2c');
8
+ const { VCNL4040Full, alsSensitivity, alsMaxRange } = require('periph/src/chips/light/vcnl4040');
9
+
10
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
11
+
12
+ (async () => {
13
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
14
+ const connection = new I2CConnection(bus, VCNL4040Full.I2C_ADDRESS);
15
+ const sensor = new VCNL4040Full(connection); // Create VCNL4040 Full driver, (connection)
16
+ await sensor.init(); // Run initialization sequence, () → void
17
+ // checks ID 0x0186, ALS 80 ms on, PS 16 bit / duty 1/160 / 50 mA on
18
+
19
+ console.log((await sensor.deviceId()).toString(16)); // Read the ID register, () → int
20
+ // 0x0186 for the VCNL4040
21
+ console.log(await sensor.readProximity()); // Read proximity, () → int counts
22
+ // PS_DATA; larger = closer
23
+ console.log(await sensor.readLux()); // Read ambient light, () → float lux
24
+ // ALS_DATA × sensitivity of the current integration time
25
+ console.log(await sensor.readAls()); // Read raw ALS, () → int counts
26
+ // ALS_DATA, 0–65535
27
+ await sensor.setWhiteChannel(true); // White channel, (on) → void
28
+ // WHITE_EN is active low: true writes 0
29
+ await sleep(100);
30
+ console.log(await sensor.readWhite()); // Read raw white channel, () → int counts
31
+ // WHITE_DATA, 0–65535
32
+ console.log(alsSensitivity(640), alsMaxRange(640)); // ALS resolution / range, (milliseconds) → float
33
+ // 0.0125 lx/count and 819.2 lx at 640 ms
34
+
35
+ await sensor.setAlsIntegrationTime(160); // ALS integration time, (milliseconds 80/160/320/640) → void
36
+ // 160 ms = 0.05 lx/count, 3276.8 lx range
37
+ console.log(await sensor.getAlsIntegrationTime()); // ALS integration time, () → int ms
38
+ // read back from ALS_CONF
39
+ await sensor.setAlsPersistence(2); // ALS persistence, (count 1/2/4/8) → void
40
+ // consecutive results outside the window
41
+ await sensor.setAlsThresholds(1000, 20000); // ALS window, (low, high counts 0–65535) → void
42
+ // counts = lux / sensitivity
43
+ console.log(await sensor.getAlsThresholds()); // ALS window, () → {low, high}
44
+ // ALS_THDL, ALS_THDH
45
+ await sensor.alsDisable(); // ALS off, () → void
46
+ // ALS_SD = 1
47
+ await sensor.alsEnable(); // ALS on, () → void
48
+ // ALS_SD = 0; next result after one integration
49
+
50
+ await sensor.setPsResolution(16); // PS resolution, (bits 12/16) → void
51
+ // set before the thresholds
52
+ await sensor.setPsDuty(160); // IRED duty, (ratio 40/80/160/320) → void
53
+ // on/off ratio 1/160
54
+ await sensor.setPsIntegrationTime(2); // PS integration time, (t 1/1.5/2/2.5/3/3.5/4/8) → void
55
+ // in units of T
56
+ await sensor.setPsPersistence(2); // PS persistence, (count 1–4) → void
57
+ // consecutive hits for an interrupt
58
+ await sensor.setPsMultiPulse(2); // Multi-pulse, (count 1/2/4/8) → void
59
+ // IRED pulses per measurement
60
+ await sensor.setLedCurrent(100); // IRED current, (milliamperes 50–200) → void
61
+ // 50, 75, 100, 120, 140, 160, 180 or 200 mA
62
+ await sensor.setPsThresholds(1000, 3000); // PS window, (low away, high close) → void
63
+ // 0–4095 at 12 bit
64
+ console.log(await sensor.getPsThresholds()); // PS window, () → {low, high}
65
+ // PS_THDL, PS_THDH
66
+ await sensor.setPsCancellation(0); // Cancellation, (level 0–65535) → void
67
+ // subtracted from every PS result
68
+ await sensor.setPsSunlightCancel(true); // Sunlight cancellation, (on) → void
69
+ // PS_SC_EN
70
+ await sensor.setPsSmartPersistence(true); // Smart persistence, (on) → void
71
+ // faster response with persistence > 1
72
+ await sensor.psDisable(); // PS off, () → void
73
+ // PS_SD = 1
74
+ await sensor.psEnable(); // PS on, () → void
75
+ // PS_SD = 0
76
+
77
+ await sensor.setPsActiveForce(true); // Active-force mode, (on) → void
78
+ // PS measures only on trigger
79
+ console.log(await sensor.triggerPs()); // One PS measurement, () → int counts
80
+ // writes PS_TRIG, waits, reads PS_DATA
81
+ await sensor.setPsActiveForce(false);
82
+ await sensor.setPsLogicOutput(true); // Logic-output mode, (on) → void
83
+ // INT low while close, high once away
84
+ await sleep(1000);
85
+ await sensor.setPsLogicOutput(false);
86
+
87
+ console.log(await sensor.status()); // Interrupt flags, () → int
88
+ // INT_FLAG high byte; reading clears it
89
+ await sensor.enableInterrupt(VCNL4040Full.SOURCE_ALS_HIGH | VCNL4040Full.SOURCE_PS_CLOSE); // Enable sources, (source) → void
90
+ // ALS_INT_EN covers both ALS sources
91
+ console.log(await sensor.pollInterrupt()); // Pending flags, () → int
92
+ // reads and clears INT_FLAG
93
+ await sensor.onInterrupt((f) => console.log('interrupt', f.toString(16))); // Subscribe, (callback(flags), {intPin}) → void
94
+ // falling edge on INT, or poll every 100 ms
95
+ await sleep(10000);
96
+ await sensor.offInterrupt(); // Unsubscribe, () → void
97
+ await sensor.disableInterrupt(VCNL4040Full.SOURCE_ALS_HIGH | VCNL4040Full.SOURCE_ALS_LOW
98
+ | VCNL4040Full.SOURCE_PS_CLOSE | VCNL4040Full.SOURCE_PS_AWAY); // Disable sources, (source) → void
99
+ // clears ALS_INT_EN and PS_INT
100
+
101
+ await connection.close();
102
+ })();
@@ -0,0 +1,125 @@
1
+ 'use strict';
2
+
3
+ // Demo: presence-activated display light. Prints the ID and settings, reads
4
+ // proximity and lux every 0.2 s for 10 s, calibrates the cancellation with
5
+ // nothing in front of the sensor, reports close/away and room-light events
6
+ // for 15 s, steps through the ALS integration times, the PS resolutions and
7
+ // IRED currents, shows active-force mode and the logic output, then powers
8
+ // both sensors down.
9
+
10
+ const { I2CConnection } = require('periph/src/connection/i2c');
11
+ const { VCNL4040Full, alsSensitivity, alsMaxRange } = require('periph/src/chips/light/vcnl4040');
12
+
13
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
14
+ const S = VCNL4040Full;
15
+ const ALL = S.SOURCE_PS_CLOSE | S.SOURCE_PS_AWAY | S.SOURCE_ALS_HIGH | S.SOURCE_ALS_LOW;
16
+
17
+ (async () => {
18
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
19
+ const connection = new I2CConnection(bus, VCNL4040Full.I2C_ADDRESS);
20
+ const sensor = new VCNL4040Full(connection); // Create VCNL4040 Full driver, (connection)
21
+ await sensor.init(); // Run initialization sequence, () → void
22
+ console.log('ID 0x' + (await sensor.deviceId()).toString(16)); // Read the ID register, () → int
23
+ console.log('ALS integration', await sensor.getAlsIntegrationTime(), 'ms'); // ALS integration time, () → int ms
24
+
25
+ /* Live readings: move a hand towards the sensor. The proximity count
26
+ rises as more IRED light is reflected; the lux value follows the room. */
27
+ await sensor.setWhiteChannel(true); // White channel, (on) → void
28
+ for (let i = 0; i < 50; i++) {
29
+ const p = await sensor.readProximity(); // Read proximity, () → int counts
30
+ const lx = await sensor.readLux(); // Read ambient light, () → float lux
31
+ const w = await sensor.readWhite(); // Read raw white channel, () → int counts
32
+ const a = await sensor.readAls(); // Read raw ALS, () → int counts
33
+ console.log(`proximity ${p} ${lx.toFixed(1)} lx white ${w} als ${a}`);
34
+ await sleep(200);
35
+ }
36
+
37
+ /* Calibrate the cancellation: with nothing in front of the sensor the
38
+ count is cross talk from the cover glass; subtracting it makes
39
+ "no target" read close to 0. */
40
+ console.log('remove any object in front of the sensor');
41
+ await sleep(3000);
42
+ let sum = 0;
43
+ for (let i = 0; i < 8; i++) sum += await sensor.readProximity();
44
+ await sensor.setPsCancellation(Math.floor(sum / 8)); // Cancellation, (level 0–65535) → void
45
+ console.log('cancellation level', Math.floor(sum / 8));
46
+
47
+ /* Close/away window: above the high threshold the PS reports "close",
48
+ below the low threshold "away"; persistence 2 ignores single noisy
49
+ samples and smart persistence keeps the response fast. The ALS window
50
+ reports a change of the room light. */
51
+ await sensor.setPsThresholds(300, 1000); // PS window, (low away, high close) → void
52
+ console.log('PS window', await sensor.getPsThresholds()); // PS window, () → {low, high}
53
+ await sensor.setPsPersistence(2); // PS persistence, (count 1–4) → void
54
+ await sensor.setPsSmartPersistence(true); // Smart persistence, (on) → void
55
+ await sensor.setPsSunlightCancel(true); // Sunlight cancellation, (on) → void
56
+ const als = await sensor.readAls();
57
+ await sensor.setAlsThresholds(Math.floor(als / 2), Math.min(65535, als * 2)); // ALS window, (low, high counts) → void
58
+ console.log('ALS window', await sensor.getAlsThresholds()); // ALS window, () → {low, high}
59
+ await sensor.setAlsPersistence(4); // ALS persistence, (count 1/2/4/8) → void
60
+ await sensor.status(); // Interrupt flags, () → int (clears stale flags)
61
+ await sensor.enableInterrupt(ALL); // Enable sources, (source) → void
62
+ await sensor.onInterrupt(async (flags) => { // Subscribe, (callback(flags), {intPin}) → void
63
+ if (flags & S.SOURCE_PS_CLOSE) console.log('close: wake the display');
64
+ if (flags & S.SOURCE_PS_AWAY) console.log('away: dim the display');
65
+ if (flags & (S.SOURCE_ALS_HIGH | S.SOURCE_ALS_LOW)) console.log('room light changed');
66
+ if (flags & S.FLAG_PROTECTION) console.log('PS protection mode');
67
+ });
68
+ console.log('wave a hand in front of the sensor for 15 s');
69
+ await sleep(15000);
70
+ await sensor.offInterrupt(); // Unsubscribe, () → void
71
+ console.log('pending', await sensor.pollInterrupt()); // Pending flags, () → int
72
+ await sensor.disableInterrupt(ALL); // Disable sources, (source) → void
73
+
74
+ /* ALS integration times: longer integration gives finer resolution but a
75
+ smaller range. */
76
+ for (const ms of [80, 160, 320, 640]) {
77
+ await sensor.setAlsIntegrationTime(ms); // ALS integration time, (milliseconds) → void
78
+ await sleep(ms * 2);
79
+ console.log(`${ms} ms ${alsSensitivity(ms)} lx/count range ${alsMaxRange(ms).toFixed(1)} lx ${(await sensor.readLux()).toFixed(2)} lx`);
80
+ }
81
+ await sensor.setAlsIntegrationTime(80);
82
+
83
+ /* PS resolution and IRED current: 12-bit output saturates at 4095; a
84
+ higher IRED current reaches further but costs current (average = pulse
85
+ current × duty ratio). */
86
+ for (const bits of [12, 16]) {
87
+ await sensor.setPsResolution(bits); // PS resolution, (bits 12/16) → void
88
+ for (const ma of [50, 75, 100, 120, 140, 160, 180, 200]) {
89
+ await sensor.setLedCurrent(ma); // IRED current, (milliamperes) → void
90
+ await sleep(50);
91
+ console.log(`${bits} bit ${ma} mA proximity ${await sensor.readProximity()}`);
92
+ }
93
+ }
94
+ await sensor.setLedCurrent(50);
95
+ await sensor.setPsDuty(320); // IRED duty, (ratio 40/80/160/320) → void
96
+ await sensor.setPsIntegrationTime(1.5); // PS integration time, (t) → void
97
+ await sensor.setPsMultiPulse(2); // Multi-pulse, (count 1/2/4/8) → void
98
+
99
+ /* Active force: the PS stays in standby and measures only when
100
+ triggered, the lowest-power way to sample proximity. */
101
+ await sensor.setPsActiveForce(true); // Active-force mode, (on) → void
102
+ for (let i = 0; i < 5; i++) {
103
+ console.log('triggered proximity', await sensor.triggerPs()); // One PS measurement, () → int counts
104
+ await sleep(500);
105
+ }
106
+ await sensor.setPsActiveForce(false);
107
+
108
+ /* Logic output: INT follows the close/away state directly, e.g. to gate
109
+ a backlight without the host. */
110
+ await sensor.setPsLogicOutput(true); // Logic-output mode, (on) → void
111
+ console.log('INT is now a close/away output for 5 s');
112
+ await sleep(5000);
113
+ await sensor.setPsLogicOutput(false);
114
+
115
+ /* Shut down: both sensors stop measuring; the IRED is off. */
116
+ await sensor.psDisable(); // PS off, () → void
117
+ await sensor.alsDisable(); // ALS off, () → void
118
+ await sensor.psEnable(); // PS on, () → void
119
+ await sensor.alsEnable(); // ALS on, () → void
120
+ await sensor.psDisable();
121
+ await sensor.alsDisable();
122
+ console.log('both sensors shut down');
123
+
124
+ await connection.close();
125
+ })();