periph 1.5.3 → 1.5.4

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 (53) hide show
  1. package/README.md +12 -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/humidity/hts221/complete.js +83 -0
  9. package/examples/humidity/hts221/demo.js +94 -0
  10. package/examples/humidity/hts221/minimal.js +22 -0
  11. package/examples/humidity/htu31d/complete.js +50 -0
  12. package/examples/humidity/htu31d/demo.js +65 -0
  13. package/examples/humidity/htu31d/minimal.js +22 -0
  14. package/examples/humidity/shtc3/complete.js +48 -0
  15. package/examples/humidity/shtc3/demo.js +45 -0
  16. package/examples/humidity/shtc3/minimal.js +22 -0
  17. package/examples/led/saa1064/complete.js +51 -0
  18. package/examples/led/saa1064/demo.js +54 -0
  19. package/examples/led/saa1064/minimal.js +23 -0
  20. package/examples/light/apds9250/complete.js +71 -0
  21. package/examples/light/apds9250/demo.js +79 -0
  22. package/examples/light/apds9250/minimal.js +23 -0
  23. package/examples/light/bh1750/complete.js +46 -0
  24. package/examples/light/bh1750/demo.js +52 -0
  25. package/examples/light/bh1750/minimal.js +22 -0
  26. package/examples/light/si1132/complete.js +70 -0
  27. package/examples/light/si1132/demo.js +73 -0
  28. package/examples/light/si1132/minimal.js +22 -0
  29. package/examples/pressure/mpl3115a2/complete.js +49 -0
  30. package/examples/pressure/mpl3115a2/demo.js +48 -0
  31. package/examples/pressure/mpl3115a2/minimal.js +22 -0
  32. package/examples/pressure/ms5607/complete.js +49 -0
  33. package/examples/pressure/ms5607/demo.js +65 -0
  34. package/examples/pressure/ms5607/minimal.js +22 -0
  35. package/examples/pressure/ms5611/complete.js +49 -0
  36. package/examples/pressure/ms5611/demo.js +65 -0
  37. package/examples/pressure/ms5611/minimal.js +22 -0
  38. package/package.json +1 -1
  39. package/src/chips/color/isl29125.js +379 -0
  40. package/src/chips/environmental/bme688.js +573 -0
  41. package/src/chips/humidity/hts221.js +369 -0
  42. package/src/chips/humidity/htu31d.js +242 -0
  43. package/src/chips/humidity/shtc3.js +239 -0
  44. package/src/chips/led/saa1064.js +241 -0
  45. package/src/chips/light/apds9250.js +431 -0
  46. package/src/chips/light/bh1750.js +196 -0
  47. package/src/chips/light/si1132.js +416 -0
  48. package/src/chips/pressure/mpl3115a2.js +459 -0
  49. package/src/chips/pressure/ms5607.js +290 -0
  50. package/src/chips/pressure/ms5611.js +290 -0
  51. package/src/discovery/registry.js +12 -12
  52. package/examples/gpio/.gitkeep +0 -0
  53. package/src/chips/gpio/.gitkeep +0 -0
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HTU31DFull } = require('../../../src/chips/humidity/htu31d');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 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=0x40) → Connection
11
+ const sensor = new HTU31DFull(connection); // Create HTU31D driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset, () → void
15
+
16
+ // --- Identify the chip and check its health ---
17
+ // The serial number is unique per device; the diagnostic flags report NVM
18
+ // and out-of-range errors.
19
+ console.log('serial 0x' + (await sensor.serialNumber()).toString(16)); // Read the serial number, () → number
20
+ console.log('diagnostic', await sensor.diagnosticFlags()); // Named diagnostic flags, () → object
21
+
22
+ // --- Condensation-aware hygrometer: 15 readings, 2 s apart ---
23
+ // The dew point shows how close the air is to condensing on a surface.
24
+ for (let i = 0; i < 15; i++) {
25
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c °C, humidity_pct %RH}
26
+ const dew = await sensor.dewPoint(); // Compute dew point, () → number °C
27
+ console.log('%s C %s %%RH dew %s C', m.temperature_c.toFixed(2), m.humidity_pct.toFixed(1), dew.toFixed(2));
28
+ console.log('h', (await sensor.readHumidity()).toFixed(1), 't', (await sensor.readTemperature()).toFixed(2), 'raw', await sensor.readRaw()); // Read single values and raw codes
29
+ await sleep(2000);
30
+ }
31
+
32
+ // --- Compare the four oversampling settings ---
33
+ // Higher OSR gives finer resolution at the cost of a longer conversion.
34
+ for (let osr = 0; osr < 4; osr++) {
35
+ sensor.setResolution(osr, osr); // Set oversampling, (osrRh 0-3, osrT 0-3) → void
36
+ const start = Date.now();
37
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c, humidity_pct}
38
+ console.log('OSR', sensor.getResolution(), 'typ', (sensor.conversionTime() * 1000).toFixed(2), 'ms', 'read', Date.now() - start, 'ms', m); // Get oversampling and typical conversion time
39
+ }
40
+ sensor.setResolution(3, 3);
41
+
42
+ // --- Heater: drive off condensation for 10 s ---
43
+ // Humidity falls and temperature rises while the heater runs; measure
44
+ // before and after, never during, then let the sensor cool.
45
+ const before = await sensor.read(); // Read temperature and humidity, () → {temperature_c, humidity_pct}
46
+ await sensor.setHeater(true); // Switch the heater, (on) → void
47
+ console.log('heater on:', await sensor.isHeaterOn(), (await sensor.diagnostic()).toString(16)); // Heater state and diagnostic byte, () → boolean / number
48
+ await sleep(10000);
49
+ await sensor.setHeater(false); // Switch the heater, (on) → void
50
+ const after = await sensor.read(); // Read temperature and humidity, () → {temperature_c, humidity_pct}
51
+ console.log('before', before, 'after', after);
52
+
53
+ // --- Non-blocking conversion ---
54
+ // Start the conversion, do other work, then collect the result.
55
+ await sensor.startConversion(); // Start a conversion, () → void
56
+ await sleep(sensor.conversionTime() * 1250);
57
+ console.log('result', await sensor.readResult()); // Read last conversion result, () → {temperature_c, humidity_pct}
58
+
59
+ // --- Reset ---
60
+ await sensor.softReset(); // Soft reset, () → void
61
+ console.log('done: %s C', (await sensor.read()).temperature_c.toFixed(2));
62
+ connection.close();
63
+ }
64
+
65
+ 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 { HTU31DMinimal } = require('../../../src/chips/humidity/htu31d');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 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=0x40) → Connection
11
+ const sensor = new HTU31DMinimal(connection); // Create HTU31D driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset, () → 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,48 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { SHTC3Full } = 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 SHTC3Full(connection); // Create SHTC3 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset and ID check, () → void
15
+
16
+ console.log(await sensor.read()); // Read temperature and humidity, () → {temperature_c: number °C, humidity_pct: number %RH}
17
+ // normal mode, both CRCs verified, humidity clamped 0-100
18
+ console.log(await sensor.readHumidity()); // Read humidity only, () → number %RH
19
+ console.log(await sensor.readTemperature()); // Read temperature only, () → number °C
20
+ console.log(await sensor.readRaw()); // Read raw codes, () → [number, number]
21
+ // [raw_temperature, raw_humidity], 16-bit
22
+ console.log((await sensor.readId()).toString(16)); // Read the ID word, () → number
23
+ // (id & 0x083F) === 0x0807 for the SHTC3
24
+
25
+ sensor.setLowPower(true); // Select low-power mode, (on) → void
26
+ // 0.7 ms measurement, slightly noisier
27
+ console.log(await sensor.read());
28
+ sensor.setLowPower(false);
29
+
30
+ sensor.setClockStretching(true); // Select clock stretching, (on) → void
31
+ // needs a controller that tolerates SCL held low
32
+ console.log(await sensor.read());
33
+ sensor.setClockStretching(false);
34
+
35
+ sensor.setSleepBetween(false); // Sleep after every read?, (on) → void
36
+ // off keeps the sensor idle (higher supply current)
37
+ console.log(await sensor.read());
38
+ sensor.setSleepBetween(true);
39
+ await sensor.wake(); // Wake-up command, () → void
40
+ await sensor.sleep(); // Sleep command, () → void
41
+ console.log(await sensor.dewPoint()); // Compute dew point, () → number °C
42
+ // one measurement, Magnus formula
43
+ await sensor.softReset(); // Soft reset, () → void
44
+ // 0x805D, leaves the sensor idle
45
+ connection.close();
46
+ }
47
+
48
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { SHTC3Full } = 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 SHTC3Full(connection); // Create SHTC3 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset and ID check, () → void
15
+
16
+ // --- Identify the chip ---
17
+ // The ID word carries the SHTC3 product code in bits 11 and 5:0.
18
+ console.log('id 0x' + (await sensor.readId()).toString(16)); // Read the ID word, () → number
19
+
20
+ // --- Battery-friendly climate logger: 6 readings, 10 s apart ---
21
+ // The sensor sleeps (0.3 uA) between the readings; every read wakes it.
22
+ for (let i = 0; i < 6; i++) {
23
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c: number °C, humidity_pct: number %RH}
24
+ console.log('%s C %s %%RH', m.temperature_c.toFixed(2), m.humidity_pct.toFixed(1));
25
+ await sleep(10000);
26
+ }
27
+
28
+ // --- Compare normal and low-power mode ---
29
+ // Low power measures in 0.7 ms instead of 10.8 ms at slightly higher noise.
30
+ for (const low of [false, true]) {
31
+ sensor.setLowPower(low); // Select low-power mode, (on) → void
32
+ const start = Date.now();
33
+ const m = await sensor.read(); // Read temperature and humidity, () → {temperature_c: number °C, humidity_pct: number %RH}
34
+ console.log(low ? 'low power' : 'normal ', (Date.now() - start) + ' ms', m);
35
+ }
36
+ sensor.setLowPower(false);
37
+
38
+ // --- Dew point and raw words, then finish asleep ---
39
+ console.log('dew point', (await sensor.dewPoint()).toFixed(2), 'C'); // Compute dew point, () → number °C
40
+ console.log('raw', await sensor.readRaw()); // Read raw codes, () → [number, number]
41
+ await sensor.sleep(); // Sleep command, () → void
42
+ connection.close();
43
+ }
44
+
45
+ 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 { 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); });