periph 1.5.0 → 1.5.2

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 +19 -0
  2. package/examples/color/as7262/complete.js +60 -0
  3. package/examples/color/as7262/demo.js +65 -0
  4. package/examples/color/as7262/minimal.js +23 -0
  5. package/examples/environmental/aht10/complete.js +37 -0
  6. package/examples/environmental/aht10/demo.js +43 -0
  7. package/examples/environmental/aht10/minimal.js +12 -0
  8. package/examples/environmental/aht20/complete.js +41 -0
  9. package/examples/environmental/aht20/demo.js +49 -0
  10. package/examples/environmental/aht20/minimal.js +12 -0
  11. package/examples/environmental/am2320/complete.js +34 -0
  12. package/examples/environmental/am2320/demo.js +46 -0
  13. package/examples/environmental/am2320/minimal.js +21 -0
  14. package/examples/humidity/hdc1008/complete.js +39 -0
  15. package/examples/humidity/hdc1008/demo.js +46 -0
  16. package/examples/humidity/hdc1008/minimal.js +22 -0
  17. package/examples/humidity/hdc1080/complete.js +34 -0
  18. package/examples/humidity/hdc1080/demo.js +46 -0
  19. package/examples/humidity/hdc1080/minimal.js +22 -0
  20. package/examples/humidity/hs30xx/complete.js +43 -0
  21. package/examples/humidity/hs30xx/demo.js +40 -0
  22. package/examples/humidity/hs30xx/minimal.js +21 -0
  23. package/examples/humidity/hs40xx/complete.js +54 -0
  24. package/examples/humidity/hs40xx/demo.js +38 -0
  25. package/examples/humidity/hs40xx/minimal.js +21 -0
  26. package/examples/io_expander/tca9554/complete.js +63 -0
  27. package/examples/io_expander/tca9554/demo.js +58 -0
  28. package/examples/io_expander/tca9554/minimal.js +20 -0
  29. package/examples/led/ht16k33/complete.js +45 -0
  30. package/examples/led/ht16k33/demo.js +54 -0
  31. package/examples/led/ht16k33/minimal.js +24 -0
  32. package/examples/led/is31fl3731/complete.js +60 -0
  33. package/examples/led/is31fl3731/demo.js +72 -0
  34. package/examples/led/is31fl3731/minimal.js +26 -0
  35. package/examples/led/pca9685/complete.js +43 -0
  36. package/examples/led/pca9685/demo.js +77 -0
  37. package/examples/led/pca9685/minimal.js +24 -0
  38. package/examples/power/ina260/complete.js +38 -0
  39. package/examples/power/ina260/demo.js +32 -0
  40. package/examples/power/ina260/minimal.js +11 -0
  41. package/examples/power/lc709203f/complete.js +57 -0
  42. package/examples/power/lc709203f/demo.js +34 -0
  43. package/examples/power/lc709203f/minimal.js +11 -0
  44. package/examples/power/lm25066/complete.js +54 -0
  45. package/examples/power/lm25066/demo.js +41 -0
  46. package/examples/power/lm25066/minimal.js +12 -0
  47. package/examples/power/ltc4151/complete.js +40 -0
  48. package/examples/power/ltc4151/demo.js +49 -0
  49. package/examples/power/ltc4151/minimal.js +11 -0
  50. package/examples/power/max17048/complete.js +68 -0
  51. package/examples/power/max17048/demo.js +56 -0
  52. package/examples/power/max17048/minimal.js +19 -0
  53. package/examples/pressure/lps22hb/complete.js +43 -0
  54. package/examples/pressure/lps22hb/demo.js +43 -0
  55. package/examples/pressure/lps22hb/minimal.js +19 -0
  56. package/examples/pressure/mpl115a2/complete.js +25 -0
  57. package/examples/pressure/mpl115a2/demo.js +30 -0
  58. package/examples/pressure/mpl115a2/minimal.js +20 -0
  59. package/package.json +1 -1
  60. package/src/chips/color/as7262.js +419 -0
  61. package/src/chips/environmental/aht10.js +186 -0
  62. package/src/chips/environmental/aht20.js +226 -0
  63. package/src/chips/environmental/am2320.js +237 -0
  64. package/src/chips/humidity/hdc1008.js +262 -0
  65. package/src/chips/humidity/hdc1080.js +208 -0
  66. package/src/chips/humidity/hs30xx.js +254 -0
  67. package/src/chips/humidity/hs40xx.js +382 -0
  68. package/src/chips/io_expander/tca9554.js +407 -0
  69. package/src/chips/led/ht16k33.js +273 -0
  70. package/src/chips/led/is31fl3731.js +501 -0
  71. package/src/chips/led/pca9685.js +350 -0
  72. package/src/chips/power/ina260.js +229 -0
  73. package/src/chips/power/lc709203f.js +376 -0
  74. package/src/chips/power/lm25066.js +370 -0
  75. package/src/chips/power/ltc4151.js +297 -0
  76. package/src/chips/power/max17048.js +302 -0
  77. package/src/chips/pressure/lps22hb.js +257 -0
  78. package/src/chips/pressure/mpl115a2.js +171 -0
  79. package/src/discovery/registry.js +56 -14
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HDC1008Minimal } = require('../../../src/chips/humidity/hdc1008');
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 HDC1008Minimal(connection); // Create HDC1008 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Write default configuration, () → void
15
+ while (true) {
16
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number °C, number %RH]
17
+ console.log('%s C, %s %%RH', t.toFixed(2), rh.toFixed(1));
18
+ await sleep(2000);
19
+ }
20
+ }
21
+
22
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HDC1080Full } = require('../../../src/chips/humidity/hdc1080');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
10
+ const sensor = new HDC1080Full(connection); // Create HDC1080 driver, (connection) → void
11
+
12
+ async function main() {
13
+ await sensor.init(); // Write default configuration, () → void
14
+ console.log('serial id 0x' + (await sensor.serialId()).toString(16)); // Read the 40-bit serial ID, () → number
15
+ console.log('config 0x' + (await sensor.readConfig()).toString(16)); // Read the configuration register, () → number
16
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number °C, number %RH]
17
+ console.log('%s C, %s %%RH', t.toFixed(2), rh.toFixed(1));
18
+ console.log('temperature', (await sensor.readTemperature()).toFixed(2)); // () → number °C, single mode
19
+ console.log('humidity', (await sensor.readHumidity()).toFixed(1)); // () → number %RH, single mode
20
+ console.log('raw', await sensor.readRaw()); // Read raw words, () → [number, number]
21
+ await sensor.setResolution(11, 8); // Set resolution, (temperatureBits, humidityBits) → void
22
+ console.log('conversion time %s ms', (sensor.conversionTime() * 1000).toFixed(2)); // Typical conversion time, () → number s
23
+ await sensor.setResolution(14, 14);
24
+ await sensor.setHeater(true); // Switch the heater, (on) → void
25
+ console.log('heater on:', await sensor.isHeaterOn()); // Heater state, () → boolean
26
+ await sensor.setHeater(false);
27
+ console.log('battery low:', await sensor.batteryLow()); // VDD below 2.8 V, () → boolean
28
+ await sensor.softwareReset(); // Software reset, () → void
29
+ console.log('manufacturer 0x' + (await sensor.manufacturerId()).toString(16)); // Manufacturer ID (0x5449), () → number
30
+ console.log('device 0x' + (await sensor.deviceId()).toString(16)); // Device ID (0x1050), () → number
31
+ connection.close();
32
+ }
33
+
34
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HDC1080Full } = require('../../../src/chips/humidity/hdc1080');
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 HDC1080Full(connection); // Create HDC1080 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Write default configuration, () → void
15
+ console.log('serial id 0x' + (await sensor.serialId()).toString(16)); // Read the 40-bit serial ID, () → number
16
+
17
+ // --- Condensation-aware hygrometer: 15 readings, 2 s apart ---
18
+ for (let i = 0; i < 15; i++) {
19
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number, number]
20
+ const g = Math.log(Math.max(rh, 0.01) / 100.0) + 17.62 * t / (243.12 + t);
21
+ const dew = 243.12 * g / (17.62 - g); // Magnus dew point, °C
22
+ console.log('%s C %s %%RH dew point %s C', t.toFixed(2), rh.toFixed(1), dew.toFixed(2));
23
+ await sleep(2000);
24
+ }
25
+
26
+ // --- Heater test: 10 s ---
27
+ const rh0 = await sensor.readHumidity(); // () → number %RH
28
+ const t0 = await sensor.readTemperature(); // () → number °C
29
+ await sensor.setHeater(true); // Switch the heater, (on) → void
30
+ await sleep(10000);
31
+ const rh1 = await sensor.readHumidity();
32
+ const t1 = await sensor.readTemperature();
33
+ console.log('RH drop %s %%RH, temperature rise %s K', (rh0 - rh1).toFixed(1), (t1 - t0).toFixed(2));
34
+ await sensor.setHeater(false);
35
+
36
+ // --- Resolution comparison ---
37
+ await sensor.setResolution(11, 8); // Set resolution, (temperatureBits, humidityBits) → void
38
+ const start = Date.now();
39
+ const [t, rh] = await sensor.read();
40
+ console.log('11/8 bit: %s C %s %%RH in %d ms', t.toFixed(2), rh.toFixed(1), Date.now() - start);
41
+ await sensor.setResolution(14, 14);
42
+ console.log('battery low:', await sensor.batteryLow()); // VDD below 2.8 V, () → boolean
43
+ connection.close();
44
+ }
45
+
46
+ 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 { HDC1080Minimal } = require('../../../src/chips/humidity/hdc1080');
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 HDC1080Minimal(connection); // Create HDC1080 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Write default configuration, () → void
15
+ while (true) {
16
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number °C, number %RH]
17
+ console.log('%s C, %s %%RH', t.toFixed(2), rh.toFixed(1));
18
+ await sleep(2000);
19
+ }
20
+ }
21
+
22
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HS30xxFull } = require('../../../src/chips/humidity/hs30xx');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x44', 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=0x44) → Connection
11
+ const sensor = new HS30xxFull(connection); // Create HS30xx driver, (connection) → void
12
+
13
+ // Replace with code that cuts and restores the sensor supply (MOSFET or GPIO).
14
+ const powerCycle = async () => { await sleep(100); };
15
+
16
+ async function main() {
17
+ const d = await sensor.read(); // Read temperature and humidity, () → Promise<{temperature_c, humidity_pct}>
18
+ // request, 40 ms wait, 4-byte fetch; one retry on stale data
19
+ console.log(d.temperature_c, d.humidity_pct);
20
+ console.log(await sensor.readTemperature()); // Read temperature only, () → Promise<number °C>
21
+ console.log(await sensor.readHumidity()); // Read humidity only, () → Promise<number %RH>
22
+ // 2-byte fetch
23
+ console.log(await sensor.readRaw()); // Read raw values, () → Promise<{raw_humidity, raw_temperature, status}>
24
+ // 14-bit values; status 0 = valid, 1 = stale
25
+ sensor.setMeasurementWait(60); // Override the post-request wait, (ms) → void
26
+ // default 40 ms; lower resolutions need less
27
+ console.log(await sensor.dewPoint()); // Compute dew point, () → Promise<number °C>
28
+ // one measurement, Magnus formula
29
+
30
+ // NVM access only works within 10 ms after power-up, so it needs the powerCycle helper above.
31
+ console.log(await sensor.getResolution(powerCycle)); // Get NVM resolution, (powerCycle) → Promise<{humidity_bits, temperature_bits}>
32
+ await sensor.setResolution(12, 12, powerCycle); // Set NVM resolution, (humidityBits, temperatureBits, powerCycle) → Promise<void>
33
+ // 8, 10, 12 or 14 bit; null leaves a channel unchanged
34
+ console.log((await sensor.readId(powerCycle)).toString(16)); // Read the 32-bit sensor ID, (powerCycle) → Promise<number>
35
+ await sensor.enterProgrammingMode(); // Enter programming mode, () → Promise<void>
36
+ // only within 10 ms after power-up
37
+ console.log((await sensor.readNvm(0x06)).toString(16)); // Read an NVM register, (reg) → Promise<number>
38
+ // 0x06 humidity / 0x11 temperature resolution, 0x1E / 0x1F ID
39
+ await sensor.exitProgrammingMode(); // Leave programming mode, () → Promise<void>
40
+ connection.close();
41
+ }
42
+
43
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HS30xxFull } = require('../../../src/chips/humidity/hs30xx');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x44', 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=0x44) → Connection
11
+ const sensor = new HS30xxFull(connection); // Create HS30xx driver, (connection) → void
12
+
13
+ // Replace with code that cuts and restores the sensor supply (MOSFET or GPIO).
14
+ const powerCycle = async () => { await sleep(100); };
15
+
16
+ async function main() {
17
+ // --- Optional: show and change the stored resolution (run with --resolution) ---
18
+ // The NVM is only reachable within 10 ms after power-up, so this needs powerCycle().
19
+ if (process.argv.includes('--resolution')) {
20
+ console.log('resolution', await sensor.getResolution(powerCycle)); // Get NVM resolution, (powerCycle) → Promise<{humidity_bits, temperature_bits}>
21
+ await sensor.setResolution(12, 12, powerCycle); // Set NVM resolution, (humidityBits, temperatureBits, powerCycle) → Promise<void>
22
+ sensor.setMeasurementWait(15); // 12-bit RH + T need about 10 ms, (ms) → void
23
+ console.log('resolution', await sensor.getResolution(powerCycle));
24
+ }
25
+
26
+ // --- Hygrometer: one reading every 2 s for 30 s ---
27
+ // Raw values and status come from the 14-bit data-fetch frame; status 0 means fresh data.
28
+ console.log('t(s) RH raw T raw status T (C) RH (%) Dew (C)');
29
+ for (let n = 0; n < 15; n++) {
30
+ const raw = await sensor.readRaw(); // Read raw values, () → Promise<{raw_humidity, raw_temperature, status}>
31
+ const d = await sensor.read(); // Read temperature and humidity, () → Promise<{temperature_c, humidity_pct}>
32
+ const dew = await sensor.dewPoint(); // Dew point, () → Promise<number °C>
33
+ console.log([n * 2, raw.raw_humidity, raw.raw_temperature, raw.status,
34
+ d.temperature_c.toFixed(2), d.humidity_pct.toFixed(1), dew.toFixed(2)].join(' '));
35
+ await sleep(2000);
36
+ }
37
+ connection.close();
38
+ }
39
+
40
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HS30xxMinimal } = require('../../../src/chips/humidity/hs30xx');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x44', 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=0x44) → Connection
11
+ const sensor = new HS30xxMinimal(connection); // Create HS30xx driver, (connection) → void
12
+
13
+ async function main() {
14
+ while (true) {
15
+ const d = await sensor.read(); // Read temperature and humidity, () → Promise<{temperature_c, humidity_pct}>
16
+ console.log('%s C, %s %%RH', d.temperature_c.toFixed(2), d.humidity_pct.toFixed(1));
17
+ await sleep(2000);
18
+ }
19
+ }
20
+
21
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HS40xxFull } = require('../../../src/chips/humidity/hs40xx');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x54', 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=0x54) → Connection
11
+ const sensor = new HS40xxFull(connection); // Create HS40xx driver, (connection) → void
12
+
13
+ async function main() {
14
+ const d = await sensor.read(); // Read temperature and humidity, () → Promise<{temperature_c, humidity_pct}>
15
+ // no-hold 0xF5, 5 bytes, CRC verified; one retry on mismatch
16
+ console.log(d.temperature_c, d.humidity_pct);
17
+ console.log(await sensor.readTemperature()); // Read temperature only, () → Promise<number °C>
18
+ // no-hold 0xF3, 3 bytes
19
+ console.log(await sensor.readHumidity()); // Read humidity only, () → Promise<number %RH>
20
+ console.log(await sensor.readHold()); // Read with clock stretching, () → Promise<{temperature_c, humidity_pct}>
21
+ // 0xE5; needs a stretching controller and SCL >= 200 kHz
22
+ console.log(await sensor.readRaw()); // Read raw values, () → Promise<{raw_humidity, raw_temperature}>
23
+ // 14-bit values, CRC verified
24
+ await sensor.setResolution(12, 12); // Set resolution, (humidityBits, temperatureBits) → Promise<void>
25
+ // 8, 10, 12 or 14 bit; null leaves a channel unchanged
26
+ console.log(await sensor.getResolution()); // Get resolution, () → Promise<{humidity_bits, temperature_bits}>
27
+ console.log(await sensor.readRegister(0x00)); // Read a register, (reg) → Promise<number>
28
+ await sensor.writeRegister(0x00, 0x0F); // Write a register, (reg, value) → Promise<void>
29
+ // 0x0F = 14 bit for both channels (default)
30
+ await sensor.setAlertThresholds({ rhHigh: 60, rhHighReset: 55, tempHigh: 40, tempHighReset: 38 });// Set alert thresholds, ({rhHigh, rhHighReset, rhLow, rhLowReset, tempHigh, tempHighReset, tempLow, tempLowReset}) → Promise<void>
31
+ // %RH / °C; omitted thresholds stay unchanged
32
+ await sensor.setAlertPolarity(true); // Set ALERT polarity, (activeHigh) → Promise<void>
33
+ await sensor.enableInterrupt(HS40xxFull.SOURCE_RH_HIGH | HS40xxFull.SOURCE_TEMP_HIGH);// Enable alert sources, (source) → Promise<void>
34
+ await sensor.startPeriodic(1); // Start periodic mode, (periodS) → Promise<void>
35
+ // 0.5, 1 or 2.5 s
36
+ await sleep(1500);
37
+ console.log(await sensor.readPeriodic()); // Fetch the latest periodic result, () → Promise<{temperature_c, humidity_pct, status}>
38
+ console.log(await sensor.pollInterrupt()); // Read alert status, () → Promise<number>
39
+ // SOURCE_* bitmask
40
+ await sensor.onInterrupt((status) => console.log('alert', status)); // Subscribe to the ALERT pin, (callback, intPin?) → Promise<void>
41
+ // polls at the periodic interval without a pin
42
+ await sleep(5000);
43
+ await sensor.offInterrupt(); // Unsubscribe, () → Promise<void>
44
+ await sensor.disableInterrupt(HS40xxFull.SOURCE_RH_HIGH | HS40xxFull.SOURCE_TEMP_HIGH);// Disable alert sources, (source) → Promise<void>
45
+ await sensor.stopPeriodic(); // Stop periodic mode, () → Promise<void>
46
+ console.log((await sensor.readId()).toString(16)); // Read the 32-bit sensor ID, () → Promise<number>
47
+ console.log(await sensor.dewPoint()); // Compute dew point, () → Promise<number °C>
48
+ // one measurement, Magnus formula
49
+ await sensor.softReset(); // Soft reset, () → Promise<void>
50
+ // stops periodic mode, waits 500 ms
51
+ connection.close();
52
+ }
53
+
54
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HS40xxFull } = require('../../../src/chips/humidity/hs40xx');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x54', 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=0x54) → Connection
11
+ const sensor = new HS40xxFull(connection); // Create HS40xx driver, (connection) → void
12
+
13
+ async function main() {
14
+ // --- Humidity watchdog ---
15
+ // Single readings for 10 s, then periodic mode with an RH-high alert (breathe on the sensor).
16
+ console.log('ID 0x' + (await sensor.readId()).toString(16)); // Read the 32-bit sensor ID, () → Promise<number>
17
+ for (let n = 0; n < 5; n++) {
18
+ const d = await sensor.read(); // Read temperature and humidity, () → Promise<{temperature_c, humidity_pct}>
19
+ console.log('%s C, %s %%RH', d.temperature_c.toFixed(2), d.humidity_pct.toFixed(1));
20
+ await sleep(2000);
21
+ }
22
+
23
+ await sensor.setAlertThresholds({ rhHigh: 60, rhHighReset: 55 }); // Set alert thresholds, ({rhHigh, rhHighReset, ...}) → Promise<void>
24
+ await sensor.enableInterrupt(HS40xxFull.SOURCE_RH_HIGH); // Enable alert sources, (source) → Promise<void>
25
+ await sensor.startPeriodic(1); // Start periodic mode, (periodS) → Promise<void>
26
+ for (let n = 0; n < 15; n++) {
27
+ await sleep(1000);
28
+ const p = await sensor.readPeriodic(); // Fetch the latest periodic result, () → Promise<{temperature_c, humidity_pct, status}>
29
+ const alert = (p.status & HS40xxFull.SOURCE_RH_HIGH) ? 'ALERT' : 'ok';
30
+ console.log('%s C, %s %%RH %s', p.temperature_c.toFixed(2), p.humidity_pct.toFixed(1), alert);
31
+ }
32
+
33
+ await sensor.stopPeriodic(); // Stop periodic mode, () → Promise<void>
34
+ await sensor.softReset(); // Soft reset, () → Promise<void>
35
+ connection.close();
36
+ }
37
+
38
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HS40xxMinimal } = require('../../../src/chips/humidity/hs40xx');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x54', 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=0x54) → Connection
11
+ const sensor = new HS40xxMinimal(connection); // Create HS40xx driver, (connection) → void
12
+
13
+ async function main() {
14
+ while (true) {
15
+ const d = await sensor.read(); // Read temperature and humidity, () → Promise<{temperature_c, humidity_pct}>
16
+ console.log('%s C, %s %%RH', d.temperature_c.toFixed(2), d.humidity_pct.toFixed(1));
17
+ await sleep(2000);
18
+ }
19
+ }
20
+
21
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+ const { I2CConnection } = require('periph/src/connection/i2c');
3
+ const { Tca9554Full } = require('periph/src/chips/io_expander/tca9554');
4
+
5
+ async function main() {
6
+ const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
7
+ const chip = new Tca9554Full(connection); // Create TCA9554 full driver, (connection, addr=0x20)
8
+ // writes the reset values; all pins start as inputs
9
+ await chip.ready;
10
+
11
+ // --- Pin proxy — async API ---
12
+ const p0 = chip.pin(0, 'out'); // Get full pin proxy, (n, direction='in') → FullPin
13
+ // P0 as output; P7 is a normal bidirectional pin
14
+ console.log('direction:', p0.direction); // direction property, () → string 'in'|'out'
15
+ await p0.write(0); // Write pin, (value=0|1) → Promise<void>
16
+ // clears the OUTPUT bit via the shadow
17
+ const v = await p0.read(); // Read pin, () → Promise<int 0|1>
18
+ // reads INPUT (actual pin level)
19
+ console.log('P0 level:', v);
20
+ await p0.setDirection('in'); // Set direction, (direction) → Promise<void>
21
+ // writes CONFIG; OUTPUT is latched first for outputs
22
+
23
+ // --- Port-level operations ---
24
+ await chip.writePort(0, 0b00001111); // Write all 8 pins, (port=0, mask) → Promise<void>
25
+ // whole-port OUTPUT write
26
+ const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
27
+ // INPUT register
28
+ console.log('PORT=0x' + port.toString(16).padStart(2, '0'));
29
+
30
+ // --- Configuration ---
31
+ await chip.configurePolarity(0x00); // Configure input polarity, (mask) → Promise<void>
32
+ // 1 bits invert the INPUT read
33
+ await chip.setDirection(0xF0); // Set whole-port direction, (mask) → Promise<void>
34
+ // P0–P3 outputs, P4–P7 inputs; OUTPUT is written first
35
+ const dir = chip.getDirection(); // Read direction shadow, () → int
36
+ // 1 = input, 0 = output
37
+ console.log('CONFIG=0x' + dir.toString(16).padStart(2, '0'));
38
+ const config = await chip.readRegister(0x03); // Read raw register, (reg) → Promise<int>
39
+ // escape hatch for debugging
40
+ console.log('CONFIG=0x' + config.toString(16).padStart(2, '0'));
41
+ await chip.writeRegister(0x02, 0x10); // Write raw register, (reg, value) → Promise<void>
42
+ // POLARITY: invert P4
43
+
44
+ // --- Interrupts ---
45
+ await chip.onInterrupt((changed) => { // Subscribe to INT, (callback, options) → Promise<void>
46
+ console.log('interrupt, changed=0x' + changed.toString(16));// INT line if wired, else 5 ms polling
47
+ });
48
+ const p4 = chip.pin(4, 'in');
49
+ await p4.watch((pin) => console.log('P4 changed'), 'change'); // Watch a pin, (handler, trigger='change') → Promise<void>
50
+ // edge filtered in software; all pins share INT
51
+ const changed = await chip.pollInterrupt(); // Read INPUT, get changed mask, () → Promise<int>
52
+ // XOR with the last INPUT value
53
+ console.log('changed=0x' + changed.toString(16));
54
+ await p4.unwatch(); // Stop watching, () → Promise<void>
55
+ // drops the handler
56
+ await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
57
+ // stops delivery
58
+ await chip.reset(); // Restore reset values, () → Promise<void>
59
+ // OUTPUT=0xFF, POLARITY=0x00, CONFIG=0xFF
60
+ await connection.close();
61
+ }
62
+
63
+ main();
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+ /**
3
+ * TCA9554 demo — LED scanner with interrupt-driven button input.
4
+ *
5
+ * Hardware:
6
+ * P0–P3: four active-low LEDs (anode → VCC via 220 Ω, cathode → P; 0 lights the LED)
7
+ * P4–P7: four push buttons (pin → GND when pressed) with the internal pull-ups (always on)
8
+ * (the chip's internal pull-ups are always on)
9
+ *
10
+ * Runs a scanner pattern on the LEDs. Button presses raise an interrupt; the
11
+ * callback prints the changed-pin mask and the port state and holds the LED
12
+ * matching the pressed button (P4→P0, …) for one second.
13
+ */
14
+ const { I2CConnection } = require('periph/src/connection/i2c');
15
+ const { Tca9554Full } = require('periph/src/chips/io_expander/tca9554');
16
+
17
+ async function main() {
18
+ // --- Initialise bus and driver ---
19
+ const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
20
+ const chip = new Tca9554Full(connection); // Create TCA9554 full driver, (connection, addr=0x20)
21
+ await chip.ready;
22
+
23
+ // --- Configure LEDs and buttons ---
24
+ // P0–P3 are outputs (all LEDs off = 1); P4–P7 stay inputs, a pressed button reads 0.
25
+ await chip.writePort(0, 0xFF); // Turn all LEDs off, (port=0, mask) → Promise<void>
26
+ await chip.setDirection(0xF0); // P0–P3 outputs, P4–P7 inputs, (mask) → Promise<void>
27
+
28
+ // --- Subscribe to input changes ---
29
+ // Any button edge raises INT (or the 5 ms poll sees it); the callback reports the changed
30
+ // mask, then holds the matching LED.
31
+ let holdUntil = 0;
32
+ let holdMask = 0x0F;
33
+ await chip.onInterrupt(async (changed) => { // Subscribe to INT, (callback, options) → Promise<void>
34
+ const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
35
+ console.log('button changed=0x' + changed.toString(16) + ' port=0x' + port.toString(16));
36
+ holdMask = ~((changed >> 4) & 0x0F) & 0x0F;
37
+ holdUntil = Date.now() + 1000;
38
+ });
39
+
40
+ // --- Scanner loop ---
41
+ let step = 0;
42
+ console.log('Running — press buttons P4–P7 to hold the matching LED');
43
+ const timer = setInterval(async () => {
44
+ const leds = Date.now() < holdUntil ? holdMask : (~(1 << (step % 4)) & 0x0F);
45
+ await chip.writePort(0, 0xF0 | leds); // Write all 8 pins, (port=0, mask) → Promise<void>
46
+ const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
47
+ console.log('pos=' + (step % 4) + ' port=0x' + port.toString(16).padStart(2, '0'));
48
+ step++;
49
+ if (step >= 40) {
50
+ clearInterval(timer);
51
+ await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
52
+ await chip.writePort(0, 0xFF); // Turn all LEDs off, (port=0, mask) → Promise<void>
53
+ await connection.close();
54
+ }
55
+ }, 100);
56
+ }
57
+
58
+ main();
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+ const { I2CConnection } = require('periph/src/connection/i2c');
3
+ const { Tca9554Minimal } = require('periph/src/chips/io_expander/tca9554');
4
+
5
+ async function main() {
6
+ const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
7
+ const chip = new Tca9554Minimal(connection); // Create TCA9554 driver, (connection, addr=0x20)
8
+
9
+ const p0 = chip.pin(0, 'in'); // Get pin proxy, (n, direction='in') → Pin
10
+ const p7 = chip.pin(7, 'out'); // Get pin proxy, (n, direction='out') → Pin
11
+
12
+ setInterval(async () => {
13
+ const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
14
+ const level = await p0.read(); // Read pin, () → Promise<int 0|1>
15
+ await p7.write(level); // Write pin, (value=0|1) → Promise<void>
16
+ console.log('PORT=0x' + port.toString(16).padStart(2, '0') + ' P0=' + level);
17
+ }, 200);
18
+ }
19
+
20
+ main();
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+ const { I2CConnection } = require('../../../src/connection/i2c');
3
+ const { HT16K33Full } = require('../../../src/chips/led/ht16k33');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x70', 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 matrix = new HT16K33Full(connection); // Create HT16K33 driver, (connection)
14
+ await matrix.ready;
15
+
16
+ await matrix.clear(); // Blank the display, () → void
17
+ // zeros the shadow RAM and writes it
18
+ matrix.setPixel(2, 5); // Set one LED, (com 0–7, row 0–15, on) → void
19
+ await matrix.show(); // Write the shadow RAM, () → void
20
+ console.log(matrix.getPixel(2, 5)); // Read a pixel from the shadow RAM, (com, row) → boolean
21
+ await matrix.writeRaw([0xFF, 0x0F], 0); // Write raw RAM bytes, (data 1–16 bytes, address 0–15) → void
22
+ // com 0 rows 0–11 lit
23
+ await matrix.setBrightness(8); // Set dimming, (level 0–15) → void
24
+ // 9/16 duty
25
+ await matrix.setBlink(1); // Set blink frequency, (0, 0.5, 1, 2 Hz) → void
26
+ // blinks at 1 Hz
27
+ await sleep(3000);
28
+ await matrix.setBlink(0);
29
+ await matrix.displayOff(); // Display output off, () → void
30
+ await matrix.displayOn(); // Display output on, () → void
31
+ await matrix.setIntOutput(true, false); // ROW15/INT pin as INT output, (enable, activeHigh) → void
32
+ // active low interrupt output
33
+ console.log(await matrix.readKeys()); // Read and clear key data, () → number bitmask
34
+ console.log(await matrix.keyPressed()); // Read the INT flag, () → boolean
35
+ console.log(await matrix.pollInterrupt()); // Read keys if INT flag set, () → number (0 if none)
36
+ await matrix.onInterrupt((keys) => console.log(keys)); // Subscribe to key presses, (callback, {intPin}) → void
37
+ // polls every 20 ms when no INT pin is wired
38
+ await sleep(100);
39
+ await matrix.offInterrupt(); // Unsubscribe, () → void
40
+ await matrix.standby(); // Oscillator off, () → void
41
+ await matrix.wake(); // Oscillator on and restore state, () → void
42
+
43
+ await connection.close();
44
+ console.log('===DONE: 0 passed, 0 failed===');
45
+ })();
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+ const { I2CConnection } = require('../../../src/connection/i2c');
3
+ const { HT16K33Full } = require('../../../src/chips/led/ht16k33');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x70', 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
+ // --- Scrolling pixel, brightness sweep, blink and key matrix on a 16x8 matrix ---
14
+ // A single lit pixel visits all 16 x 8 positions, the brightness is swept from
15
+ // 0 to 15, the display blinks at 2, 1 and 0.5 Hz, then key presses are reported
16
+ // for 20 s via the INT output before standby and wake.
17
+ const matrix = new HT16K33Full(connection); // Create HT16K33 driver, (connection)
18
+ await matrix.ready;
19
+
20
+ for (let com = 0; com < HT16K33Full.COMMONS; com++) {
21
+ for (let row = 0; row < HT16K33Full.ROWS; row++) {
22
+ await matrix.clear(); // Blank the display, () → void
23
+ matrix.setPixel(com, row); // Set one LED, (com 0–7, row 0–15, on) → void
24
+ await matrix.show(); // Write the shadow RAM, () → void
25
+ await sleep(50);
26
+ }
27
+ }
28
+
29
+ await matrix.writeRaw(new Array(16).fill(0xFF), 0); // All LEDs on, (data, address) → void
30
+ for (let level = 0; level < 16; level++) {
31
+ await matrix.setBrightness(level); // Sweep dimming, (level 0–15) → void
32
+ await sleep(100);
33
+ }
34
+
35
+ for (const f of [2, 1, 0.5, 0]) {
36
+ await matrix.setBlink(f); // Set blink frequency, (0, 0.5, 1, 2 Hz) → void
37
+ await sleep(3000);
38
+ }
39
+
40
+ await matrix.setIntOutput(true); // INT output, active low, (enable, activeHigh) → void
41
+ const deadline = Date.now() + 20000;
42
+ while (Date.now() < deadline) {
43
+ const keys = await matrix.pollInterrupt(); // Read keys if pending, () → number bitmask
44
+ if (keys) console.log('keys: 0x' + keys.toString(16));
45
+ await sleep(20);
46
+ }
47
+
48
+ await matrix.standby(); // Oscillator off, () → void
49
+ await sleep(1000);
50
+ await matrix.wake(); // Oscillator on, state restored, () → void
51
+
52
+ await connection.close();
53
+ console.log('===DONE: 0 passed, 0 failed===');
54
+ })();
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+ const { I2CConnection } = require('../../../src/connection/i2c');
3
+ const { HT16K33Minimal } = require('../../../src/chips/led/ht16k33');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x70', 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 matrix = new HT16K33Minimal(connection); // Create HT16K33 driver, (connection)
14
+ await matrix.ready;
15
+
16
+ for (let row = 0; row < HT16K33Minimal.ROWS; row++) {
17
+ matrix.setPixel(0, row); // Set one LED in the shadow RAM, (com 0–7, row 0–15, on) → void
18
+ }
19
+ await matrix.show(); // Write the shadow RAM to the chip, () → void
20
+ await sleep(1000);
21
+
22
+ await connection.close();
23
+ console.log('===DONE: 0 passed, 0 failed===');
24
+ })();