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.
Files changed (73) hide show
  1. package/README.md +17 -0
  2. package/examples/gas/ccs811/complete.js +87 -0
  3. package/examples/gas/ccs811/demo.js +108 -0
  4. package/examples/gas/ccs811/minimal.js +20 -0
  5. package/examples/gas/rrh46410/complete.js +78 -0
  6. package/examples/gas/rrh46410/demo.js +86 -0
  7. package/examples/gas/rrh46410/minimal.js +21 -0
  8. package/examples/gas/rrh62000/complete.js +74 -0
  9. package/examples/gas/rrh62000/demo.js +87 -0
  10. package/examples/gas/rrh62000/minimal.js +22 -0
  11. package/examples/gas/scd30/complete.js +73 -0
  12. package/examples/gas/scd30/demo.js +102 -0
  13. package/examples/gas/scd30/minimal.js +20 -0
  14. package/examples/gas/scd40/complete.js +76 -0
  15. package/examples/gas/scd40/demo.js +109 -0
  16. package/examples/gas/scd40/minimal.js +20 -0
  17. package/examples/light/si1133/complete.js +89 -0
  18. package/examples/light/si1133/demo.js +93 -0
  19. package/examples/light/si1133/minimal.js +22 -0
  20. package/examples/light/si1145/complete.js +93 -0
  21. package/examples/light/si1145/demo.js +98 -0
  22. package/examples/light/si1145/minimal.js +22 -0
  23. package/examples/light/tsl2561/complete.js +74 -0
  24. package/examples/light/tsl2561/demo.js +46 -0
  25. package/examples/light/tsl2561/minimal.js +22 -0
  26. package/examples/light/tsl2591/complete.js +90 -0
  27. package/examples/light/tsl2591/demo.js +119 -0
  28. package/examples/light/tsl2591/minimal.js +22 -0
  29. package/examples/light/vcnl4040/complete.js +102 -0
  30. package/examples/light/vcnl4040/demo.js +125 -0
  31. package/examples/light/vcnl4040/minimal.js +25 -0
  32. package/examples/light/vcnl4200/complete.js +101 -0
  33. package/examples/light/vcnl4200/demo.js +126 -0
  34. package/examples/light/vcnl4200/minimal.js +25 -0
  35. package/examples/pressure/spl06/complete.js +81 -0
  36. package/examples/pressure/spl06/demo.js +106 -0
  37. package/examples/pressure/spl06/minimal.js +22 -0
  38. package/examples/rfid/pn532/complete.js +99 -0
  39. package/examples/rfid/pn532/demo.js +121 -0
  40. package/examples/rfid/pn532/minimal.js +22 -0
  41. package/examples/rfid/st25dv/complete.js +94 -0
  42. package/examples/rfid/st25dv/demo.js +121 -0
  43. package/examples/rfid/st25dv/minimal.js +21 -0
  44. package/examples/rtc/bq32000/complete.js +53 -0
  45. package/examples/rtc/bq32000/demo.js +77 -0
  46. package/examples/rtc/bq32000/minimal.js +20 -0
  47. package/examples/rtc/ds1307/complete.js +54 -0
  48. package/examples/rtc/ds1307/demo.js +96 -0
  49. package/examples/rtc/ds1307/minimal.js +20 -0
  50. package/examples/rtc/ds1371/complete.js +53 -0
  51. package/examples/rtc/ds1371/demo.js +85 -0
  52. package/examples/rtc/ds1371/minimal.js +25 -0
  53. package/package.json +1 -1
  54. package/src/chips/gas/ccs811.js +400 -0
  55. package/src/chips/gas/rrh46410.js +534 -0
  56. package/src/chips/gas/rrh62000.js +418 -0
  57. package/src/chips/gas/scd30.js +368 -0
  58. package/src/chips/gas/scd40.js +451 -0
  59. package/src/chips/light/si1133.js +583 -0
  60. package/src/chips/light/si1145.js +630 -0
  61. package/src/chips/light/tsl2561.js +445 -0
  62. package/src/chips/light/tsl2591.js +561 -0
  63. package/src/chips/light/vcnl4040.js +576 -0
  64. package/src/chips/light/vcnl4200.js +592 -0
  65. package/src/chips/pressure/spl06.js +461 -0
  66. package/src/chips/rfid/pn532.js +513 -0
  67. package/src/chips/rfid/st25dv.js +628 -0
  68. package/src/chips/rtc/bq32000.js +334 -0
  69. package/src/chips/rtc/ds1307.js +327 -0
  70. package/src/chips/rtc/ds1371.js +453 -0
  71. package/src/connection/i2c.js +46 -13
  72. package/src/connection/smbus.js +21 -8
  73. package/src/discovery/registry.js +17 -17
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { Si1133Minimal } = 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 Si1133Minimal(connection); // Create Si1133 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Check PART_ID, reset, UV + white channels, () → void
15
+ while (true) {
16
+ const m = await sensor.read(); // Forced measurement, () → {uv: number counts, white: number counts}
17
+ console.log('UV %d, white %d', m.uv, m.white);
18
+ await sleep(1000);
19
+ }
20
+ }
21
+
22
+ 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 { Si1145Full } = require('../../../src/chips/light/si1145');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 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=0x60) → Connection
11
+ // The Si1145 has the fixed address 0x60 (older silicon: 0x5A).
12
+ const sensor = new Si1145Full(connection); // Create Si1145 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, HW_KEY, UV + ALS + PS1, () → void
17
+ // Waits 25 ms, verifies PART_ID 0x45/0x46/0x47, writes HW_KEY 0x17, UCOEF, CHLIST 0xB1, LED1 level 3.
18
+
19
+ console.log(await sensor.readId()); // Identity registers, () → {partId, revId, seqId}
20
+ // PART_ID 0x45 for the Si1145; SEQ_ID 0x01 parts swap the MEAS_RATE bytes.
21
+ console.log(await sensor.readResponse()); // RESPONSE decoded, () → {counter, error}
22
+ // 4-bit command counter and the error code (0 = none, 0x8x = ADC overflow / invalid setting).
23
+
24
+ console.log(await sensor.read()); // Forced PS + ALS, () → {proximity, uv_index, visible, infrared}
25
+ // PSALS_FORCE, waits for ALS_INT and PS1_INT; raw counts and the UV index (AUX_DATA / 100).
26
+ console.log(await sensor.readProximity(1)); // Forced PS, (channel=1) → number counts
27
+ // PS1 only; PS2/PS3 exist on the Si1146/47.
28
+ console.log(await sensor.readUvIndex()); // Forced ALS, () → number UV index
29
+ // UV index with the default UV coefficients 7B 6B 01 00.
30
+ console.log(await sensor.readVisible()); // Forced ALS, () → number counts
31
+ // Raw visible ambient light.
32
+ console.log(await sensor.readInfrared()); // Forced ALS, () → number counts
33
+ // Raw infrared ambient light.
34
+ console.log(await sensor.readRaw()); // Data registers, () → {visible, infrared, ps1, ps2, ps3, aux}
35
+ // No new measurement; the latest values of 0x22–0x2D.
36
+ console.log(await sensor.readTemperatureRaw()); // AUX temperature, () → number counts
37
+ // Relative die temperature; CHLIST is switched to EN_AUX and restored.
38
+ console.log(await sensor.readVddRaw()); // AUX VDD, () → number counts
39
+ // Supply voltage on the auxiliary channel.
40
+
41
+ await sensor.setChannels(true, true, true, true); // CHLIST, (uv, visible, infrared, ps1, ps2=false, ps3=false, aux=false) → void
42
+ // UV and AUX share AUX_DATA, so only one of them can be enabled.
43
+ await sensor.setLedCurrent(1, 8); // LED drive, (led, level) → void
44
+ // Level 0 = off … 15 = maximum (~359 mA); keep the gain low at high currents.
45
+ await sensor.setPsLed(1, 0x01); // PS LED select, (channel, leds) → void
46
+ // PS1 drives LED1; the Si1145 has no LED2/LED3.
47
+ await sensor.setPsPhotodiode(1, true); // PS photodiode, (channel, large) → void
48
+ // Large IR photodiode (0x03); false selects the small one.
49
+ await sensor.setGain(0, 0, 0); // ADC gains, (visible=0, infrared=0, proximity=0) → void
50
+ // Integration time × 2^n, recovery counts at the one's complement of the gain.
51
+ await sensor.setHighRange(false, false, false); // Signal range, (visible, infrared, proximity) → void
52
+ // true divides the sensitivity by 14.5 (direct sunlight).
53
+ await sensor.setAlignment(); // Result alignment, ({visible, infrared, ps1, ps2, ps3}) → void
54
+ // true reports the 16 LSBs instead of the 16 MSBs of the ADC result.
55
+ await sensor.setUvCoefficients(Si1145Full.UCOEF_DEFAULT); // UCOEF0..3, (ucoef) → void
56
+ // 7B 6B 01 00 make AUX_DATA the UV index × 100.
57
+ console.log(await sensor.readCalibration()); // GET_CAL, () → number[12]
58
+ // Factory calibration bytes; overwrites the data registers.
59
+
60
+ await sensor.pollInterrupt(); // IRQ_STATUS, () → number sources
61
+ // Reads and clears pending flags.
62
+ await sensor.startAutonomous(0.1); // Autonomous mode, (interval s, proximity=true, als=true) → void
63
+ // MEAS_RATE = 0.1 s / 31.25 µs = 3200, then PSALS_AUTO.
64
+ await sleep(500);
65
+ console.log(await sensor.pollInterrupt()); // IRQ_STATUS, () → number sources
66
+ // SOURCE_ALS | SOURCE_PS1 when both finished at least once.
67
+ await sensor.pause(); // PSALS_PAUSE, () → void
68
+ // Stops the autonomous loop.
69
+ await sensor.force(true, false); // PS_FORCE without waiting, (proximity=true, als=true) → void
70
+ // The result arrives when IRQ_STATUS flags PS1.
71
+ await sleep(50);
72
+ console.log((await sensor.readRaw()).ps1);
73
+
74
+ const events = [];
75
+ await sensor.enableInterrupt(Si1145Full.SOURCE_ALS | Si1145Full.SOURCE_PS1); // INT_CFG + IRQ_ENABLE, (source) → void
76
+ // INT pin (active low) driven for ALS and PS1 completions.
77
+ await sensor.onInterrupt((s) => events.push(s)); // Subscribe, (callback, intPin=null) → void
78
+ // Falling edge on the connection's intPin, otherwise a 10 ms polling timer.
79
+ await sensor.force(); // PSALS_FORCE without waiting, () → void
80
+ await sleep(200);
81
+ await sensor.offInterrupt(); // Unsubscribe, () → void
82
+ console.log(events);
83
+ await sensor.disableInterrupt(Si1145Full.SOURCE_CMD); // IRQ_ENABLE clear, (source) → void
84
+ // CMD_INT is not used by default.
85
+
86
+ await sensor.setI2cAddress(0x60); // I2C_ADDR + BUSADDR, (address) → void
87
+ // Volatile; here the address stays 0x60, otherwise reconnect on the new address.
88
+ await sensor.reset(); // RESET + HW_KEY + defaults, () → void
89
+ // Software reset, 10 ms wait, then the Minimal configuration again.
90
+ await connection.close();
91
+ }
92
+
93
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,98 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { Si1145Full } = require('../../../src/chips/light/si1145');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 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=0x60) → Connection
11
+ const sensor = new Si1145Full(connection); // Create Si1145 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Check PART_ID, HW_KEY, UV + ALS + PS1, () → void
15
+
16
+ // --- Identity and calibration ---
17
+ // PART_ID tells the Si1145/46/47 apart (one, two or three LEDs); GET_CAL returns the factory
18
+ // calibration bytes.
19
+ const id = await sensor.readId(); // Identity registers, () → {partId, revId, seqId}
20
+ console.log('PART_ID 0x%s REV_ID 0x%s SEQ_ID 0x%s', id.partId.toString(16), id.revId.toString(16), id.seqId.toString(16));
21
+ console.log('calibration', await sensor.readCalibration()); // GET_CAL, () → number[12]
22
+ console.log('response', await sensor.readResponse()); // RESPONSE, () → {counter, error}
23
+
24
+ // --- Proximity, UV index and light for 10 s ---
25
+ // Proximity counts rise as a hand approaches the LED/sensor window.
26
+ for (let i = 0; i < 10; i++) {
27
+ const m = await sensor.read(); // Forced PS + ALS, () → {proximity, uv_index, visible, infrared}
28
+ console.log('%d s prox %d UV %s vis %d ir %d', i, m.proximity, m.uv_index.toFixed(2), m.visible, m.infrared);
29
+ await sleep(1000);
30
+ }
31
+
32
+ // --- LED current and photodiode ---
33
+ // Hold a hand ~5 cm above the sensor: higher LED current gives more counts, the small
34
+ // photodiode a fraction of the large one.
35
+ console.log('Hold a hand above the sensor');
36
+ await sleep(3000);
37
+ for (const level of [1, 4, 8, 15]) {
38
+ await sensor.setLedCurrent(1, level); // LED drive, (led, level) → void
39
+ console.log('LED level %d -> %d', level, await sensor.readProximity()); // Forced PS, (channel=1) → number counts
40
+ }
41
+ await sensor.setLedCurrent(1, 3);
42
+ for (const large of [false, true]) {
43
+ await sensor.setPsPhotodiode(1, large); // PS photodiode, (channel, large) → void
44
+ console.log('%s photodiode -> %d', large ? 'large' : 'small', await sensor.readProximity());
45
+ }
46
+ await sensor.setPsLed(1, 0x01); // PS LED select, (channel, leds) → void
47
+
48
+ // --- Gain and high range ---
49
+ // Each gain step doubles the integration time; the high range divides the sensitivity by 14.5
50
+ // for direct sunlight.
51
+ for (const gain of [0, 2, 4]) {
52
+ await sensor.setGain(gain, gain, 0); // ADC gains, (visible, infrared, proximity) → void
53
+ console.log('gain %d vis %d ir %d', gain, await sensor.readVisible(), await sensor.readInfrared()); // Forced ALS, () → number counts
54
+ }
55
+ await sensor.setGain();
56
+ await sensor.setHighRange(true, true, false); // Signal range, (visible, infrared, proximity) → void
57
+ console.log('high range vis %d UV %s', await sensor.readVisible(), (await sensor.readUvIndex()).toFixed(2)); // Forced ALS, () → number
58
+ await sensor.setHighRange(false, false, false);
59
+ await sensor.setAlignment(); // Result alignment, ({...}) → void
60
+ await sensor.setUvCoefficients(Si1145Full.UCOEF_DEFAULT); // UCOEF0..3, (ucoef) → void
61
+
62
+ // --- Autonomous mode with interrupt events and a proximity switch ---
63
+ // PS + ALS every 100 ms; each finished PS1 measurement raises INT (or is polled).
64
+ const THRESHOLD = 2000;
65
+ await sensor.setChannels(true, true, true, true); // CHLIST, (uv, visible, infrared, ps1, ...) → void
66
+ const events = [];
67
+ await sensor.enableInterrupt(Si1145Full.SOURCE_PS1); // INT_CFG + IRQ_ENABLE, (source) → void
68
+ await sensor.onInterrupt((s) => events.push(s)); // Subscribe, (callback, intPin=null) → void
69
+ await sensor.startAutonomous(0.1); // Autonomous mode, (interval s, proximity, als) → void
70
+ let near = false;
71
+ for (let i = 0; i < 50; i++) {
72
+ const ps1 = (await sensor.readRaw()).ps1; // Data registers, () → {...}
73
+ if ((ps1 > THRESHOLD) !== near) {
74
+ near = ps1 > THRESHOLD;
75
+ console.log('output %s (prox %d)', near ? 'ON' : 'OFF', ps1);
76
+ }
77
+ await sleep(100);
78
+ }
79
+ await sensor.pause(); // PSALS_PAUSE, () → void
80
+ await sensor.offInterrupt(); // Unsubscribe, () → void
81
+ console.log('%d interrupt events', events.length);
82
+ await sensor.disableInterrupt(Si1145Full.SOURCE_PS1); // IRQ_ENABLE clear, (source) → void
83
+ await sensor.pollInterrupt(); // IRQ_STATUS, () → number sources
84
+
85
+ // --- Auxiliary channels ---
86
+ console.log('temperature raw %d VDD raw %d', await sensor.readTemperatureRaw(), await sensor.readVddRaw()); // AUX channel, () → number counts
87
+ await sensor.force(false, true); // ALS_FORCE without waiting, (proximity, als) → void
88
+ await sleep(50);
89
+ console.log('raw', await sensor.readRaw()); // Data registers, () → {...}
90
+
91
+ // --- Address and reset ---
92
+ await sensor.setI2cAddress(0x60); // I2C_ADDR + BUSADDR, (address) → void
93
+ await sensor.reset(); // RESET + HW_KEY + defaults, () → void
94
+ console.log('after reset', await sensor.read());
95
+ await connection.close();
96
+ }
97
+
98
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { Si1145Minimal } = require('../../../src/chips/light/si1145');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 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=0x60) → Connection
11
+ const sensor = new Si1145Minimal(connection); // Create Si1145 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Check PART_ID, HW_KEY, UV + ALS + PS1, () → void
15
+ while (true) {
16
+ const m = await sensor.read(); // Forced PS + ALS, () → {proximity, uv_index, visible, infrared}
17
+ console.log('prox %d UV %s vis %d ir %d', m.proximity, m.uv_index.toFixed(2), m.visible, m.infrared);
18
+ await sleep(1000);
19
+ }
20
+ }
21
+
22
+ 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
+ })();