periph 1.5.3 → 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.
Files changed (79) hide show
  1. package/README.md +18 -0
  2. package/examples/color/isl29125/complete.js +57 -0
  3. package/examples/color/isl29125/demo.js +77 -0
  4. package/examples/color/isl29125/minimal.js +23 -0
  5. package/examples/environmental/bme688/complete.js +48 -0
  6. package/examples/environmental/bme688/demo.js +47 -0
  7. package/examples/environmental/bme688/minimal.js +21 -0
  8. package/examples/gas/ccs811/complete.js +87 -0
  9. package/examples/gas/ccs811/demo.js +108 -0
  10. package/examples/gas/ccs811/minimal.js +20 -0
  11. package/examples/gas/rrh46410/complete.js +78 -0
  12. package/examples/gas/rrh46410/demo.js +86 -0
  13. package/examples/gas/rrh46410/minimal.js +21 -0
  14. package/examples/humidity/hts221/complete.js +83 -0
  15. package/examples/humidity/hts221/demo.js +94 -0
  16. package/examples/humidity/hts221/minimal.js +22 -0
  17. package/examples/humidity/htu31d/complete.js +50 -0
  18. package/examples/humidity/htu31d/demo.js +65 -0
  19. package/examples/humidity/htu31d/minimal.js +22 -0
  20. package/examples/humidity/shtc3/complete.js +48 -0
  21. package/examples/humidity/shtc3/demo.js +45 -0
  22. package/examples/humidity/shtc3/minimal.js +22 -0
  23. package/examples/led/saa1064/complete.js +51 -0
  24. package/examples/led/saa1064/demo.js +54 -0
  25. package/examples/led/saa1064/minimal.js +23 -0
  26. package/examples/light/apds9250/complete.js +71 -0
  27. package/examples/light/apds9250/demo.js +79 -0
  28. package/examples/light/apds9250/minimal.js +23 -0
  29. package/examples/light/bh1750/complete.js +46 -0
  30. package/examples/light/bh1750/demo.js +52 -0
  31. package/examples/light/bh1750/minimal.js +22 -0
  32. package/examples/light/si1132/complete.js +70 -0
  33. package/examples/light/si1132/demo.js +73 -0
  34. package/examples/light/si1132/minimal.js +22 -0
  35. package/examples/light/si1133/complete.js +89 -0
  36. package/examples/light/si1133/demo.js +93 -0
  37. package/examples/light/si1133/minimal.js +22 -0
  38. package/examples/light/si1145/complete.js +93 -0
  39. package/examples/light/si1145/demo.js +98 -0
  40. package/examples/light/si1145/minimal.js +22 -0
  41. package/examples/pressure/mpl3115a2/complete.js +49 -0
  42. package/examples/pressure/mpl3115a2/demo.js +48 -0
  43. package/examples/pressure/mpl3115a2/minimal.js +22 -0
  44. package/examples/pressure/ms5607/complete.js +49 -0
  45. package/examples/pressure/ms5607/demo.js +65 -0
  46. package/examples/pressure/ms5607/minimal.js +22 -0
  47. package/examples/pressure/ms5611/complete.js +49 -0
  48. package/examples/pressure/ms5611/demo.js +65 -0
  49. package/examples/pressure/ms5611/minimal.js +22 -0
  50. package/examples/pressure/spl06/complete.js +81 -0
  51. package/examples/pressure/spl06/demo.js +106 -0
  52. package/examples/pressure/spl06/minimal.js +22 -0
  53. package/examples/rfid/pn532/complete.js +99 -0
  54. package/examples/rfid/pn532/demo.js +121 -0
  55. package/examples/rfid/pn532/minimal.js +22 -0
  56. package/package.json +1 -1
  57. package/src/chips/color/isl29125.js +379 -0
  58. package/src/chips/environmental/bme688.js +573 -0
  59. package/src/chips/gas/ccs811.js +400 -0
  60. package/src/chips/gas/rrh46410.js +534 -0
  61. package/src/chips/humidity/hts221.js +369 -0
  62. package/src/chips/humidity/htu31d.js +242 -0
  63. package/src/chips/humidity/shtc3.js +239 -0
  64. package/src/chips/led/saa1064.js +241 -0
  65. package/src/chips/light/apds9250.js +431 -0
  66. package/src/chips/light/bh1750.js +196 -0
  67. package/src/chips/light/si1132.js +416 -0
  68. package/src/chips/light/si1133.js +583 -0
  69. package/src/chips/light/si1145.js +630 -0
  70. package/src/chips/pressure/mpl3115a2.js +459 -0
  71. package/src/chips/pressure/ms5607.js +290 -0
  72. package/src/chips/pressure/ms5611.js +290 -0
  73. package/src/chips/pressure/spl06.js +461 -0
  74. package/src/chips/rfid/pn532.js +513 -0
  75. package/src/connection/i2c.js +46 -13
  76. package/src/connection/smbus.js +21 -8
  77. package/src/discovery/registry.js +18 -18
  78. package/examples/gpio/.gitkeep +0 -0
  79. package/src/chips/gpio/.gitkeep +0 -0
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { SHTC3Minimal } = require('../../../src/chips/humidity/shtc3');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x70', 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=0x70) → Connection
11
+ const sensor = new SHTC3Minimal(connection); // Create SHTC3 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset and ID check, () → void
15
+ while (true) {
16
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c: number °C, humidity_pct: number %RH}
17
+ console.log('%s C, %s %%RH', m.temperature_c.toFixed(2), m.humidity_pct.toFixed(1));
18
+ await sleep(2000);
19
+ }
20
+ }
21
+
22
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,51 @@
1
+ 'use strict';
2
+ const { I2CConnection } = require('../../../src/connection/i2c');
3
+ const { SAA1064Full } = require('../../../src/chips/led/saa1064');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x38', 16);
7
+
8
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
9
+
10
+ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
11
+
12
+ (async () => {
13
+ const display = new SAA1064Full(connection); // Create SAA1064 driver, (connection)
14
+ await display.ready;
15
+ await display.showDigits([1, 2, 3, 4]); // Show four digits, (digits 4 × 0–9 or null) → void
16
+ // default mapping a=bit0 … g=bit6, dp=bit7
17
+ await sleep(1000);
18
+ await display.showNumber(42); // Show a number, (value 0–9999) → void
19
+ // leading zeros are blanked
20
+ await display.writeRaw([0x7F, 0x7F], 3); // Write raw segment bytes, (patterns 1–4, firstDigit 1–4) → void
21
+ // digits 3 and 4 show 8
22
+ await sleep(1000);
23
+ await display.showDigit(2, 7, true); // Show one digit, (position 1–4, value 0–9 or null, dp) → void
24
+ // digit 2 = 7 with decimal point
25
+ await display.setDecimalPoint(2, false); // Switch a decimal point, (position 1–4, on) → void
26
+ // keeps the cached segments
27
+ display.setSegmentMap({ a: 0, b: 1, c: 2, d: 3, e: 4, f: 5, g: 6, dp: 7 }); // Remap segment bits, (mapping a–g, dp → bit 0–7) → void
28
+ await display.setCurrent(21); // Set segment current, (ma 0, 3, 6, 9, 12, 15, 18, 21) → void
29
+ // C4–C6
30
+ await display.setMode(false); // Select static/dynamic mode, (dynamic) → void
31
+ // static shows only digits 1 and 2
32
+ await sleep(1000);
33
+ await display.setMode(true);
34
+ await display.blank(true, false); // Blank digit pairs, (digits13, digits24) → void
35
+ // digits 1+3 off
36
+ await sleep(1000);
37
+ await display.blank(false, false);
38
+ await display.segmentTest(true); // Segment test, (on) → void
39
+ // lights every segment
40
+ await sleep(1000);
41
+ await display.segmentTest(false);
42
+ console.log(await display.readStatus()); // Read the status byte, () → number
43
+ console.log(await display.powerResetFlag()); // Read the PR flag, () → boolean
44
+ // set after power-up, clears when read
45
+ await display.writeControl(0x47); // Write the raw control byte, (value 0–127) → void
46
+ // dynamic mode, unblanked, 12 mA
47
+ await display.clear(); // Blank all digits, () → void
48
+
49
+ await connection.close();
50
+ console.log('===DONE: 0 passed, 0 failed===');
51
+ })();
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+ const { I2CConnection } = require('../../../src/connection/i2c');
3
+ const { SAA1064Full } = require('../../../src/chips/led/saa1064');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x38', 16);
7
+
8
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
9
+
10
+ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
11
+
12
+ (async () => {
13
+ // --- Four-digit counter ---
14
+ // A fast counter from 0 to 9999, the decimal point sweeping across the four
15
+ // positions, the segment current stepped through 3/9/15/21 mA, a switch to
16
+ // static mode (only digits 1 and 2 stay lit), a 2 s segment test and the
17
+ // status byte (the PR flag is set after power-up and clears on the first read).
18
+ const display = new SAA1064Full(connection); // Create SAA1064 driver, (connection)
19
+ await display.ready;
20
+
21
+ for (let n = 0; n < 10000; n += 37) {
22
+ await display.showNumber(n); // Show the counter, (value 0–9999) → void
23
+ await sleep(10);
24
+ }
25
+
26
+ await display.showDigits([1, 2, 3, 4]); // Show four digits, (digits) → void
27
+ for (const pos of [1, 2, 3, 4]) {
28
+ await display.setDecimalPoint(pos, true); // Light a decimal point, (position, on) → void
29
+ await sleep(400);
30
+ await display.setDecimalPoint(pos, false);
31
+ }
32
+
33
+ for (const ma of [3, 9, 15, 21]) {
34
+ await display.setCurrent(ma); // Step the segment current, (ma) → void
35
+ await display.showNumber(8888);
36
+ await sleep(1000);
37
+ }
38
+ await display.setCurrent(12);
39
+
40
+ await display.setMode(false); // Static mode, (dynamic) → void
41
+ await sleep(2000);
42
+ await display.setMode(true);
43
+
44
+ await display.segmentTest(true); // Segment test, (on) → void
45
+ await sleep(2000);
46
+ await display.segmentTest(false);
47
+
48
+ console.log((await display.readStatus()).toString(16)); // Read the status byte, () → number
49
+ console.log(await display.powerResetFlag()); // Read the PR flag, () → boolean
50
+ await display.clear(); // Blank all digits, () → void
51
+
52
+ await connection.close();
53
+ console.log('===DONE: 0 passed, 0 failed===');
54
+ })();
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+ const { I2CConnection } = require('../../../src/connection/i2c');
3
+ const { SAA1064Minimal } = require('../../../src/chips/led/saa1064');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x38', 16);
7
+
8
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
9
+
10
+ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
11
+
12
+ (async () => {
13
+ const display = new SAA1064Minimal(connection); // Create SAA1064 driver, (connection)
14
+ await display.ready;
15
+
16
+ for (let n = 0; n < 100; n++) {
17
+ await display.showNumber(n); // Show a right-aligned number, (value 0–9999) → void
18
+ await sleep(100);
19
+ }
20
+
21
+ await connection.close();
22
+ console.log('===DONE: 0 passed, 0 failed===');
23
+ })();
@@ -0,0 +1,71 @@
1
+ 'use strict';
2
+
3
+ // Complete example: exercise every method in the public API.
4
+ // Reads the channels singly and as a block, changes mode, resolution, rate
5
+ // and gain, programs the threshold and variation interrupts and handles one.
6
+
7
+ const { I2CConnection } = require('periph/src/connection/i2c');
8
+ const { APDS9250Full } = require('periph/src/chips/light/apds9250');
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, APDS9250Full.I2C_ADDRESS);
15
+ const sensor = new APDS9250Full(connection); // Create APDS-9250 Full driver, (connection)
16
+ await sensor.init(); // Check PART_ID, start colour mode, () → void
17
+
18
+ console.log(await sensor.partId()); // Read PART_ID, () → {part, revision}
19
+ // {part: 0xB, revision: 0x5} for the APDS-9250
20
+ console.log(await sensor.read()); // Read all channels, () → {red, green, blue, ir}
21
+ console.log(await sensor.readRed()); // Read the red channel, () → int count
22
+ console.log(await sensor.readGreen()); // Read the green / ALS channel, () → int count
23
+ console.log(await sensor.readBlue()); // Read the blue channel, () → int count
24
+ console.log(await sensor.readIr()); // Read the IR channel, () → int count
25
+ console.log(await sensor.dataReady()); // Query LS_DATA, () → bool
26
+ // clears the status flags on the chip
27
+ console.log(await sensor.readChannels()); // Block-read without waiting, () → {red, green, blue, ir}
28
+
29
+ await sensor.setMode(false); // Select ALS/IR mode, (colour bool) → void
30
+ // false: only green and IR are active
31
+ await sensor.setMode(true); // Back to colour mode, (colour bool) → void
32
+ await sensor.disable(); // Standby, () → void
33
+ await sensor.enable(); // Activate the sensor, () → void
34
+
35
+ await sensor.setResolution(16); // Set the ADC resolution, (bits) → void
36
+ // 13, 16, 17, 18, 19 or 20; restarts the measurement
37
+ await sensor.setMeasurementRate(200); // Set the measurement rate, (milliseconds) → void
38
+ // 25, 50, 100, 200, 500, 1000 or 2000
39
+ await sensor.setGain(6); // Set the gain, (gain) → void
40
+ // 1, 3, 6, 9 or 18; restarts the measurement
41
+ console.log(await sensor.getResolution()); // Read the resolution, () → int bits
42
+ console.log(await sensor.getMeasurementRate()); // Read the rate, () → int ms
43
+ console.log(await sensor.getGain()); // Read the gain, () → int
44
+ console.log(await sensor.integrationTime()); // Integration time, () → float s
45
+ console.log(await sensor.normalized(await sensor.readGreen())); // Counts per gain-second, (counts) → float
46
+
47
+ await sensor.setThresholds(1000, 100000); // Set the threshold window, (low, high) → void
48
+ // 20-bit counts
49
+ console.log(await sensor.getThresholds()); // Read the window, () → {low, high}
50
+ await sensor.setInterruptChannel('green'); // Choose the monitored channel, (channel) → void
51
+ // 'ir', 'green', 'red' or 'blue'
52
+ await sensor.setPersistence(2); // Set the persistence, (count) → void
53
+ // 1-16 consecutive events
54
+ await sensor.setVariance(64); // Set the variation threshold, (counts) → void
55
+ // 8 ... 1024, power of two
56
+
57
+ await sensor.enableInterrupt(APDS9250Full.SOURCE_VARIATION); // Enable the interrupt, (source) → void
58
+ // SOURCE_THRESHOLD or SOURCE_VARIATION
59
+ await sensor.onInterrupt((status) => console.log('interrupt', status.toString(16))); // Subscribe, (callback(status), {intPin}) → void
60
+ // falling edge on INT, or a 50 ms poll without a pin
61
+ await sleep(10000);
62
+ console.log((await sensor.pollInterrupt()).toString(16)); // Read and clear MAIN_STATUS, () → int
63
+ // 0x20 power-on, 0x10 interrupt, 0x08 data
64
+ await sensor.offInterrupt(); // Unsubscribe, () → void
65
+ await sensor.disableInterrupt(); // Disable the interrupt, () → void
66
+ console.log((await sensor.status()).toString(16)); // Raw MAIN_STATUS, () → int
67
+ await sensor.softReset(); // Software reset, () → void
68
+ // all registers to defaults, sensor in standby
69
+
70
+ await connection.close();
71
+ })();
@@ -0,0 +1,79 @@
1
+ 'use strict';
2
+
3
+ // Demo: a colour sampler.
4
+ // Prints the part ID and settings, samples R/G/B/IR for 10 s and shows the
5
+ // colour as #rrggbb, compares ALS/IR mode, steps gain and resolution with
6
+ // normalised counts, arms a variation interrupt and a threshold interrupt on
7
+ // the green channel and finishes with disable and soft reset.
8
+
9
+ const { I2CConnection } = require('periph/src/connection/i2c');
10
+ const { APDS9250Full } = require('periph/src/chips/light/apds9250');
11
+
12
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
13
+ const hex2 = (v) => v.toString(16).padStart(2, '0');
14
+
15
+ function colour(c) {
16
+ const top = Math.max(c.red, c.green, c.blue, 1);
17
+ return '#' + [c.red, c.green, c.blue].map((v) => hex2(Math.floor(v * 255 / top))).join('');
18
+ }
19
+
20
+ (async () => {
21
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
22
+ const connection = new I2CConnection(bus, APDS9250Full.I2C_ADDRESS);
23
+ const sensor = new APDS9250Full(connection); // Create APDS-9250 Full driver, (connection)
24
+ await sensor.init(); // Check PART_ID, start colour mode, () → void
25
+
26
+ console.log('part/revision', await sensor.partId()); // Read PART_ID, () → {part, revision}
27
+ console.log('resolution', await sensor.getResolution(), 'bit, rate', await sensor.getMeasurementRate(),
28
+ 'ms, gain', await sensor.getGain()); // Read the settings, () → int each
29
+
30
+ for (let i = 0; i < 20; i++) {
31
+ const c = await sensor.read(); // Read the four channels, () → {red, green, blue, ir}
32
+ console.log(colour(c), 'IR fraction', (c.ir / Math.max(1, c.ir + c.green)).toFixed(2));
33
+ await sleep(500);
34
+ }
35
+
36
+ await sensor.setMode(false); // Select ALS/IR mode, (colour bool) → void
37
+ console.log('ALS/IR', await sensor.readGreen(), await sensor.readIr()); // Read green and IR, () → int each
38
+ await sensor.setMode(true); // Back to colour mode, (colour bool) → void
39
+
40
+ for (const gain of [1, 3, 6, 9, 18]) {
41
+ await sensor.setGain(gain); // Set the gain, (gain) → void
42
+ await sleep(300);
43
+ const g = await sensor.readGreen(); // Read green, () → int
44
+ console.log('gain', gain, 'green', g, 'normalized', (await sensor.normalized(g)).toFixed(0));
45
+ }
46
+ await sensor.setGain(3);
47
+ for (const bits of [13, 16, 17, 18, 19, 20]) {
48
+ await sensor.setResolution(bits); // Set the resolution, (bits) → void
49
+ await sleep((await sensor.integrationTime()) * 2000 + 50);
50
+ const g = await sensor.readGreen();
51
+ console.log(bits, 'bit full scale', 2 ** bits - 1, 'green', g, 'normalized', (await sensor.normalized(g)).toFixed(0));
52
+ }
53
+ await sensor.setResolution(18);
54
+ await sensor.setMeasurementRate(100); // Set the rate, (milliseconds) → void
55
+
56
+ await sensor.setInterruptChannel('green'); // Choose the channel, (channel) → void
57
+ await sensor.setPersistence(2); // Set the persistence, (count) → void
58
+ await sensor.setVariance(64); // Set the variation threshold, (counts) → void
59
+ await sensor.enableInterrupt(APDS9250Full.SOURCE_VARIATION); // Enable the variation interrupt, (source) → void
60
+ for (let i = 0; i < 20; i++) {
61
+ await sleep(500);
62
+ const status = await sensor.pollInterrupt(); // Read and clear MAIN_STATUS, () → int
63
+ if (status & APDS9250Full.STATUS_INTERRUPT) console.log('variation interrupt, green', await sensor.readGreen());
64
+ }
65
+
66
+ const base = await sensor.readGreen();
67
+ await sensor.setThresholds(Math.max(0, base - Math.floor(base / 4)), base + Math.floor(base / 4)); // Set the window, (low, high) → void
68
+ await sensor.enableInterrupt(APDS9250Full.SOURCE_THRESHOLD); // Enable the threshold interrupt, (source) → void
69
+ await sensor.onInterrupt((s) => console.log('threshold interrupt', s.toString(16))); // Subscribe, (callback, {intPin}) → void
70
+ await sleep(10000);
71
+ await sensor.offInterrupt(); // Unsubscribe, () → void
72
+ await sensor.disableInterrupt(); // Disable the interrupt, () → void
73
+ console.log(await sensor.getThresholds(), (await sensor.status()).toString(16), await sensor.dataReady());
74
+ console.log(await sensor.readChannels(), await sensor.readRed(), await sensor.readBlue());
75
+ await sensor.disable(); // Standby, () → void
76
+ await sensor.softReset(); // Software reset, () → void
77
+
78
+ await connection.close();
79
+ })();
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ // Minimal example: read red, green, blue and IR counts once a second.
4
+
5
+ const { I2CConnection } = require('periph/src/connection/i2c');
6
+ const { APDS9250Minimal } = require('periph/src/chips/light/apds9250');
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, APDS9250Minimal.I2C_ADDRESS);
13
+ const sensor = new APDS9250Minimal(connection); // Create APDS-9250 driver, (connection)
14
+ await sensor.init(); // Check PART_ID, start colour mode, () → void
15
+
16
+ for (let i = 0; i < 10; i++) {
17
+ const c = await sensor.read(); // Read the four channels, () → {red, green, blue, ir} counts
18
+ console.log('R', c.red, 'G', c.green, 'B', c.blue, 'IR', c.ir);
19
+ await sleep(1000);
20
+ }
21
+
22
+ await connection.close();
23
+ })();
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { BH1750Full } = require('../../../src/chips/light/bh1750');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x23', 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=0x23) → Connection
11
+ const sensor = new BH1750Full(connection); // Create BH1750 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Power on, () → void
15
+ // opcode 0x01; the chip powers down by itself after each one-time measurement
16
+
17
+ console.log(await sensor.read()); // Measure illuminance, () → number lx
18
+ // one-time H-resolution (1 lx), waits 180 ms
19
+ console.log(await sensor.readRaw()); // Read the raw count, () → number
20
+ // 16-bit, one one-time measurement in the current mode
21
+ console.log(await sensor.measureOnce(BH1750Full.MODE_H2)); // One-time measurement, (mode MODE_H|MODE_H2|MODE_L) → number lx
22
+ // MODE_H2 = 0.5 lx steps, MODE_L = 4 lx steps and 24 ms
23
+ console.log(sensor.measurementTime()); // Maximum measurement time, (mode=MODE_H) → number s
24
+ console.log(sensor.luxPerCount()); // Lux per raw count, (mode=MODE_H) → number
25
+
26
+ await sensor.startContinuous(BH1750Full.MODE_H); // Start continuous mode, (mode=MODE_H) → void
27
+ // first result after measurementTime()
28
+ await sleep(200);
29
+ console.log(await sensor.readContinuous()); // Read the latest result, () → number lx
30
+ // no wait
31
+
32
+ await sensor.setMeasurementTime(138); // Set MTreg, (mtreg 31-254) → void
33
+ // default 69; larger = more sensitive and slower
34
+ console.log(sensor.getMeasurementTimeRegister()); // Get MTreg, () → number
35
+ // cached, the chip has no read-back
36
+ await sensor.setSensitivity(1.0); // Set sensitivity, (factor 0.45-3.68) → void
37
+ // MTreg = round(69 x factor)
38
+
39
+ await sensor.powerDown(); // Power down, () → void
40
+ await sensor.powerOn(); // Power on, () → void
41
+ await sensor.reset(); // Clear the data register, () → void
42
+ // powers on first; MTreg and mode are kept
43
+ connection.close();
44
+ }
45
+
46
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { BH1750Full } = require('../../../src/chips/light/bh1750');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x23', 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=0x23) → Connection
11
+ const sensor = new BH1750Full(connection); // Create BH1750 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Power on, () → void
15
+
16
+ // --- Light meter ---
17
+ // One reading per mode, ten continuous values per second for 5 s (cover and
18
+ // uncover the sensor), the effect of the sensitivity, reset and a power cycle.
19
+ for (const [name, mode] of [['H (1 lx)', BH1750Full.MODE_H], ['H2 (0.5 lx)', BH1750Full.MODE_H2], ['L (4 lx)', BH1750Full.MODE_L]]) {
20
+ const start = Date.now();
21
+ const lux = await sensor.measureOnce(mode); // One-time measurement, (mode) → number lx
22
+ console.log(name, lux.toFixed(2), 'lx', Date.now() - start, 'ms');
23
+ }
24
+
25
+ await sensor.startContinuous(BH1750Full.MODE_H); // Start continuous mode, (mode) → void
26
+ for (let i = 0; i < 50; i++) {
27
+ await sleep(100);
28
+ console.log((await sensor.readContinuous()).toFixed(1), 'lx'); // Read the latest result, () → number lx
29
+ }
30
+
31
+ for (const factor of [0.45, 3.68]) {
32
+ await sensor.setSensitivity(factor); // Set sensitivity, (factor) → void
33
+ const lux = await sensor.measureOnce(BH1750Full.MODE_H);
34
+ console.log('x' + factor, 'MTreg', sensor.getMeasurementTimeRegister(), 'raw', await sensor.readRaw(), lux.toFixed(2), 'lx', 'max', (sensor.measurementTime() * 1000).toFixed(0), 'ms');
35
+ }
36
+ await sensor.setSensitivity(1.0);
37
+
38
+ await sensor.reset(); // Clear the data register, () → void
39
+ await sensor.powerDown(); // Power down, () → void
40
+ await sensor.powerOn(); // Power on, () → void
41
+
42
+ // Log-scale bar graph
43
+ for (let i = 0; i < 20; i++) {
44
+ const lux = await sensor.measureOnce(BH1750Full.MODE_H);
45
+ const bar = Math.floor(Math.max(0, Math.log10(Math.max(lux, 1))) * 8);
46
+ console.log(lux.toFixed(1).padStart(9), 'lx |' + '#'.repeat(bar));
47
+ await sleep(500);
48
+ }
49
+ connection.close();
50
+ }
51
+
52
+ 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 { BH1750Minimal } = require('../../../src/chips/light/bh1750');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x23', 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=0x23) → Connection
11
+ const sensor = new BH1750Minimal(connection); // Create BH1750 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Power on, () → void
15
+ while (true) {
16
+ const lux = await sensor.read(); // Measure illuminance, () → number lx
17
+ console.log('%s lx', lux.toFixed(1));
18
+ await sleep(1000);
19
+ }
20
+ }
21
+
22
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { Si1132Full } = require('../../../src/chips/light/si1132');
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 Si1132Full(connection); // Create Si1132 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Verify PART_ID and start forced mode, () → void
15
+ // writes HW_KEY, UV coefficients and CHLIST
16
+ console.log(await sensor.read()); // Read UV index and ALS counts, () → {uv_index, visible, infrared}
17
+ // forced measurement of the enabled channels
18
+ console.log('uv index', await sensor.readUvIndex()); // Read UV index, () → number
19
+ // forced measurement, AUX_DATA / 100
20
+ console.log('visible', await sensor.readVisible()); // Read visible counts, () → number
21
+ // forced measurement, 16-bit raw count
22
+ console.log('infrared', await sensor.readInfrared()); // Read infrared counts, () → number
23
+ // forced measurement, 16-bit raw count
24
+ console.log('raw', await sensor.readRaw()); // Read data registers, () → {visible, infrared, aux}
25
+ // latest values, no measurement is started
26
+ console.log('temperature raw', await sensor.readTemperatureRaw()); // Read die temperature, () → number
27
+ // auxiliary channel, relative only
28
+ console.log('vdd raw', await sensor.readVddRaw()); // Read supply voltage, () → number
29
+ // auxiliary channel with AUX_ADCMUX = 0x75
30
+
31
+ await sensor.setChannels(true, true, true); // Select channels, (uv, visible, infrared, aux=false) → void
32
+ // writes CHLIST; uv and aux cannot be combined
33
+ await sensor.setGain(2, 2); // Set ADC gain, (visible 0–7, infrared 0–7) → void
34
+ // integration time x 2^n plus the recommended recovery count
35
+ await sensor.setGain(0, 0);
36
+ await sensor.setHighRange(false, false); // Set high signal range, (visible, infrared) → void
37
+ // true divides the sensitivity by 14.5 (direct sunlight)
38
+ await sensor.setAlignment(true, true); // Set data alignment, (visibleLow, infraredLow) → void
39
+ // true reports the least significant 16 bits
40
+ await sensor.setUvCoefficients([0x7B, 0x6B, 0x01, 0x00]); // Set UV coefficients, (ucoef 4 bytes) → void
41
+ // datasheet defaults for an open-air UV index
42
+ console.log('calibration', await sensor.readCalibration()); // Read factory calibration, () → number[12]
43
+ // GET_CAL then registers 0x22–0x2D
44
+
45
+ await sensor.force(); // Start a forced measurement, () → void
46
+ // ALS_FORCE without waiting
47
+ await sleep(50);
48
+ console.log('ready', await sensor.measurementReady()); // Check measurement done, () → boolean
49
+ // IRQ_STATUS.ALS_INT
50
+ console.log('response', await sensor.readResponse()); // Read command response, () → {counter, error}
51
+ // RESPONSE decoded, error 0 = none
52
+ console.log('poll', await sensor.pollInterrupt()); // Poll interrupt status, () → number
53
+ // reads IRQ_STATUS and clears it
54
+
55
+ await sensor.startAutonomous(0.5); // Start autonomous mode, (interval 0–2.05 s) → void
56
+ // MEAS_RATE in 31.25 µs steps then ALS_AUTO
57
+ await sensor.onInterrupt((status) => console.log('INT status 0x' + status.toString(16))); // Subscribe to measurements, (callback, intPin=null) → void
58
+ // falling edge on INT, else polling
59
+ await sleep(2000);
60
+ await sensor.offInterrupt(); // Unsubscribe, () → void
61
+ await sensor.pause(); // Pause autonomous mode, () → void
62
+ // ALS_PAUSE
63
+ await sensor.setI2cAddress(0x60); // Set I²C address, (address 7-bit) → void
64
+ // I2C_ADDR parameter then BUSADDR, volatile
65
+ await sensor.reset(); // Reset the chip, () → void
66
+ // RESET, waits, writes HW_KEY again
67
+ connection.close();
68
+ }
69
+
70
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,73 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { Si1132Full } = require('../../../src/chips/light/si1132');
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 Si1132Full(connection); // Create Si1132 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Verify PART_ID and start forced mode, () → void
15
+
16
+ // --- Identify the chip and show the factory calibration ---
17
+ // The calibration bytes belong to the Silicon Labs Si114x calibration
18
+ // structure; they let you correct part-to-part variation of the UV index.
19
+ console.log('calibration', await sensor.readCalibration()); // Read factory calibration, () → number[12]
20
+
21
+ // --- UV and light monitor: 20 readings, 1 s apart ---
22
+ // Index, visible and infrared counts side by side show whether the
23
+ // light is sunlight (high infrared share) or artificial.
24
+ for (let i = 0; i < 20; i++) {
25
+ const m = await sensor.read(); // Read UV index and ALS counts, () → {uv_index, visible, infrared}
26
+ console.log('UV %s vis %d ir %d', m.uv_index.toFixed(2), m.visible, m.infrared);
27
+ console.log('raw', await sensor.readRaw()); // Read data registers, () → {visible, infrared, aux}
28
+ await sleep(1000);
29
+ }
30
+
31
+ // --- Effect of gain and of the high-range bits on the same light ---
32
+ // Each gain step doubles the integration time (and the count); the high
33
+ // range divides the sensitivity by 14.5 for direct sunlight.
34
+ for (const gain of [0, 2, 4]) {
35
+ await sensor.setGain(gain, gain); // Set ADC gain, (visible 0–7, infrared 0–7) → void
36
+ console.log('gain', gain, await sensor.read()); // Read UV index and ALS counts, () → object
37
+ }
38
+ await sensor.setGain(0, 0); // Set ADC gain, (visible 0–7, infrared 0–7) → void
39
+ await sensor.setHighRange(true, true); // Set high signal range, (visible, infrared) → void
40
+ console.log('high range', await sensor.read()); // Read UV index and ALS counts, () → object
41
+ await sensor.setHighRange(false, false); // Set high signal range, (visible, infrared) → void
42
+ await sensor.setAlignment(false, false); // Set data alignment, (visibleLow, infraredLow) → void
43
+ await sensor.setUvCoefficients([0x7B, 0x6B, 0x01, 0x00]); // Set UV coefficients, (ucoef 4 bytes) → void
44
+
45
+ // --- Auxiliary channels ---
46
+ // The temperature value is relative only; the VDD count tracks the supply.
47
+ console.log('temperature raw', await sensor.readTemperatureRaw()); // Read die temperature, () → number
48
+ console.log('vdd raw', await sensor.readVddRaw()); // Read supply voltage, () → number
49
+
50
+ // --- Autonomous mode at 0.5 s with the interrupt ---
51
+ // Every completed measurement raises INT; the callback prints the status.
52
+ await sensor.startAutonomous(0.5); // Start autonomous mode, (interval 0–2.05 s) → void
53
+ await sensor.onInterrupt((status) => console.log('INT 0x' + status.toString(16))); // Subscribe to measurements, (callback, intPin=null) → void
54
+ await sleep(5000);
55
+ await sensor.offInterrupt(); // Unsubscribe, () → void
56
+ await sensor.pause(); // Pause autonomous mode, () → void
57
+ console.log('ready', await sensor.measurementReady(), 'response', await sensor.readResponse()); // Check measurement done / read RESPONSE, () → boolean / object
58
+ await sensor.enableInterrupt(); // Enable the INT pin, () → void
59
+ await sensor.force(); // Start a forced measurement, () → void
60
+ await sleep(50);
61
+ console.log('poll', await sensor.pollInterrupt()); // Poll interrupt status, () → number
62
+ await sensor.disableInterrupt(); // Disable the INT pin, () → void
63
+
64
+ // --- UV only, then shut down ---
65
+ // Dropping the ALS channels shortens each measurement.
66
+ await sensor.setChannels(true, false, false); // Select channels, (uv, visible, infrared, aux=false) → void
67
+ console.log('UV only', (await sensor.readUvIndex()).toFixed(2)); // Read UV index, () → number
68
+ await sensor.setI2cAddress(0x60); // Set I²C address, (address 7-bit) → void
69
+ await sensor.reset(); // Reset the chip, () → void
70
+ connection.close();
71
+ }
72
+
73
+ 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 { Si1132Minimal } = require('../../../src/chips/light/si1132');
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 Si1132Minimal(connection); // Create Si1132 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Verify PART_ID and start forced mode, () → void
15
+ while (true) {
16
+ const m = await sensor.read(); // Read UV index and ALS counts, () → {uv_index: number, visible: number counts, infrared: number counts}
17
+ console.log('UV %s, visible %d, infrared %d', 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); });