periph 1.4.0 → 1.5.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 (112) hide show
  1. package/README.md +27 -0
  2. package/examples/adc_dac/ad5251/complete.js +66 -0
  3. package/examples/adc_dac/ad5251/demo.js +69 -0
  4. package/examples/adc_dac/ad5251/minimal.js +23 -0
  5. package/examples/adc_dac/ad5252/complete.js +66 -0
  6. package/examples/adc_dac/ad5252/demo.js +69 -0
  7. package/examples/adc_dac/ad5252/minimal.js +23 -0
  8. package/examples/adc_dac/ads7828/complete.js +46 -0
  9. package/examples/adc_dac/ads7828/demo.js +45 -0
  10. package/examples/adc_dac/ads7828/minimal.js +17 -0
  11. package/examples/adc_dac/cat5171/complete.js +52 -0
  12. package/examples/adc_dac/cat5171/demo.js +59 -0
  13. package/examples/adc_dac/cat5171/minimal.js +23 -0
  14. package/examples/adc_dac/cs43l22/complete.js +88 -0
  15. package/examples/adc_dac/cs43l22/demo.js +56 -0
  16. package/examples/adc_dac/cs43l22/minimal.js +20 -0
  17. package/examples/adc_dac/ds1881/complete.js +62 -0
  18. package/examples/adc_dac/ds1881/demo.js +61 -0
  19. package/examples/adc_dac/ds1881/minimal.js +25 -0
  20. package/examples/adc_dac/ds3502/complete.js +46 -0
  21. package/examples/adc_dac/ds3502/demo.js +54 -0
  22. package/examples/adc_dac/ds3502/minimal.js +28 -0
  23. package/examples/adc_dac/mcp3422/complete.js +48 -0
  24. package/examples/adc_dac/mcp3422/demo.js +49 -0
  25. package/examples/adc_dac/mcp3422/minimal.js +25 -0
  26. package/examples/display/sh1106/complete.js +101 -0
  27. package/examples/display/sh1106/demo.js +68 -0
  28. package/examples/display/sh1106/minimal.js +25 -0
  29. package/examples/display/ssd1305/complete.js +129 -0
  30. package/examples/display/ssd1305/demo.js +71 -0
  31. package/examples/display/ssd1305/minimal.js +25 -0
  32. package/examples/display/ssd1306/complete.js +115 -0
  33. package/examples/display/ssd1306/demo.js +70 -0
  34. package/examples/display/ssd1306/minimal.js +25 -0
  35. package/examples/environmental/am2315/complete.js +34 -0
  36. package/examples/environmental/am2315/demo.js +46 -0
  37. package/examples/environmental/am2315/minimal.js +21 -0
  38. package/examples/humidity/htu21df/complete.js +49 -0
  39. package/examples/humidity/htu21df/demo.js +50 -0
  40. package/examples/humidity/htu21df/minimal.js +22 -0
  41. package/examples/humidity/sht31/complete.js +53 -0
  42. package/examples/humidity/sht31/demo.js +53 -0
  43. package/examples/humidity/sht31/minimal.js +21 -0
  44. package/examples/humidity/si7021/complete.js +55 -0
  45. package/examples/humidity/si7021/demo.js +58 -0
  46. package/examples/humidity/si7021/minimal.js +21 -0
  47. package/examples/imu/bno055/complete.js +85 -0
  48. package/examples/imu/bno055/demo.js +70 -0
  49. package/examples/imu/bno055/minimal.js +20 -0
  50. package/examples/io_expander/pca6408a/complete.js +63 -0
  51. package/examples/io_expander/pca6408a/demo.js +58 -0
  52. package/examples/io_expander/pca6408a/minimal.js +20 -0
  53. package/examples/io_expander/pca6416a/complete.js +68 -0
  54. package/examples/io_expander/pca6416a/demo.js +60 -0
  55. package/examples/io_expander/pca6416a/minimal.js +20 -0
  56. package/examples/io_expander/pca9536/complete.js +63 -0
  57. package/examples/io_expander/pca9536/demo.js +57 -0
  58. package/examples/io_expander/pca9536/minimal.js +20 -0
  59. package/examples/io_expander/pca9539/complete.js +68 -0
  60. package/examples/io_expander/pca9539/demo.js +60 -0
  61. package/examples/io_expander/pca9539/minimal.js +20 -0
  62. package/examples/light/max44009/complete.js +56 -0
  63. package/examples/light/max44009/demo.js +49 -0
  64. package/examples/light/max44009/minimal.js +22 -0
  65. package/examples/memory/at24c02n/complete.js +39 -0
  66. package/examples/memory/at24c02n/demo.js +39 -0
  67. package/examples/memory/at24c02n/minimal.js +15 -0
  68. package/examples/memory/at24c64/complete.js +39 -0
  69. package/examples/memory/at24c64/demo.js +39 -0
  70. package/examples/memory/at24c64/minimal.js +15 -0
  71. package/examples/memory/cat24c512/complete.js +39 -0
  72. package/examples/memory/cat24c512/demo.js +39 -0
  73. package/examples/memory/cat24c512/minimal.js +15 -0
  74. package/examples/other/pca9541/complete.js +52 -0
  75. package/examples/other/pca9541/demo.js +61 -0
  76. package/examples/other/pca9541/minimal.js +23 -0
  77. package/examples/temperature/tmp102/complete.js +74 -0
  78. package/examples/temperature/tmp102/demo.js +71 -0
  79. package/examples/temperature/tmp102/minimal.js +20 -0
  80. package/examples/tof/vl6180x/complete.js +62 -0
  81. package/examples/tof/vl6180x/demo.js +77 -0
  82. package/examples/tof/vl6180x/minimal.js +23 -0
  83. package/package.json +1 -1
  84. package/src/chips/adc_dac/ad5251.js +345 -0
  85. package/src/chips/adc_dac/ad5252.js +345 -0
  86. package/src/chips/adc_dac/ads7828.js +225 -0
  87. package/src/chips/adc_dac/cat5171.js +213 -0
  88. package/src/chips/adc_dac/cs43l22.js +645 -0
  89. package/src/chips/adc_dac/ds1881.js +357 -0
  90. package/src/chips/adc_dac/ds3502.js +241 -0
  91. package/src/chips/adc_dac/mcp3422.js +312 -0
  92. package/src/chips/display/mono_oled.js +182 -0
  93. package/src/chips/display/sh1106.js +496 -0
  94. package/src/chips/display/ssd1305.js +655 -0
  95. package/src/chips/display/ssd1306.js +458 -0
  96. package/src/chips/environmental/am2315.js +237 -0
  97. package/src/chips/humidity/htu21df.js +222 -0
  98. package/src/chips/humidity/sht31.js +284 -0
  99. package/src/chips/humidity/si7021.js +282 -0
  100. package/src/chips/imu/bno055.js +496 -0
  101. package/src/chips/io_expander/pca6408a.js +407 -0
  102. package/src/chips/io_expander/pca6416a.js +455 -0
  103. package/src/chips/io_expander/pca9536.js +398 -0
  104. package/src/chips/io_expander/pca9539.js +457 -0
  105. package/src/chips/light/max44009.js +336 -0
  106. package/src/chips/memory/at24c02n.js +220 -0
  107. package/src/chips/memory/at24c64.js +220 -0
  108. package/src/chips/memory/cat24c512.js +221 -0
  109. package/src/chips/other/pca9541.js +315 -0
  110. package/src/chips/temperature/tmp102.js +361 -0
  111. package/src/chips/tof/vl6180x.js +580 -0
  112. package/src/discovery/registry.js +65 -13
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { CAT24C512Full } = require('../../../src/chips/memory/cat24c512');
5
+
6
+ async function main() {
7
+ const connection = new I2CConnection(1, 0x50, null, null, 2); // Create I2C connection, (bus=1, addr=0x50, intPin=null, enPin=null, regBytes=2) → Connection
8
+ const ee = new CAT24C512Full(connection); // Create CAT24C512 driver, (connection) → void
9
+
10
+ console.log('Size:', ee.size(), 'bytes'); // Get array size, () → number
11
+ // always 65536
12
+ console.log('Page:', ee.pageSize(), 'bytes'); // Get write page size, () → number
13
+ // always 128
14
+ ee.maxChunk = 16; // Set max bytes per bus transaction, (maxChunk=30) → void
15
+ console.log('maxChunk:', ee.maxChunk); // Get max bytes per bus transaction, () → number
16
+
17
+ await ee.writeByte(0x0010, 0xA5); // Write a single byte, (address 0–65535, value 0–255) → void
18
+ // ACK-polled until the write cycle ends
19
+ console.log('Byte:', (await ee.readByte(0x0010)).toString(16)); // Read a single byte, (address 0–65535) → number
20
+ // random read
21
+
22
+ await ee.write(0x0100, Buffer.from(Array.from({ length: 40 }, (_, i) => i))); // Write data, (address, data) → void
23
+ // split at 128-byte page boundaries and maxChunk, ACK-polled per chunk
24
+ const data = await ee.read(0x0100, 40); // Sequential read, (address, length) → Buffer
25
+ // split into maxChunk-byte transactions
26
+ console.log('Block:', data.toString('hex'));
27
+
28
+ await ee.writePage(0x0180, Buffer.from(Array.from({ length: 16 }, (_, i) => i))); // Write within one page, (address, data) → void
29
+ // rejects with RangeError if the data crosses a page boundary
30
+ await ee.fill(0x0100, 40, 0xFF); // Fill a range, (address, length, value=0) → void
31
+ // page-wise writes of value
32
+ console.log('After fill:', (await ee.read(0x0100, 8)).toString('hex'));
33
+
34
+ console.log('Ready:', await ee.waitReady(10)); // ACK-poll the chip, (timeoutMs) → boolean
35
+ // true once the chip answers its address
36
+ connection.close();
37
+ }
38
+
39
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { CAT24C512Full } = require('../../../src/chips/memory/cat24c512');
5
+
6
+ async function main() {
7
+ const connection = new I2CConnection(1, 0x50, null, null, 2); // Create I2C connection, (bus=1, addr=0x50, intPin=null, enPin=null, regBytes=2) → Connection
8
+ const ee = new CAT24C512Full(connection); // Create CAT24C512 driver, (connection) → void
9
+
10
+ // --- Persistent boot counter: 4-byte big-endian value at address 0 ---
11
+ const counter = ((await ee.read(0x0000, 4)).readUInt32BE(0) + 1) >>> 0; // Sequential read, (address, length) → Buffer
12
+ const cb = Buffer.alloc(4);
13
+ cb.writeUInt32BE(counter, 0);
14
+ await ee.write(0x0000, cb); // Write data, (address, data) → void
15
+ console.log('Boot counter:', counter);
16
+
17
+ // --- Page-write benchmark: 3 pages, 128-byte aligned ---
18
+ const size = 3 * ee.pageSize(); // Get write page size, () → number
19
+ const pattern = Buffer.from(Array.from({ length: size }, (_, i) => (i * 7) & 0xFF));
20
+ const t0 = process.hrtime.bigint();
21
+ await ee.write(0x0080, pattern); // Write data, (address, data) → void
22
+ const ms = Number(process.hrtime.bigint() - t0) / 1e6;
23
+ console.log('Wrote %d bytes in %s ms (%s B/s incl. ACK-polled write cycles)', size, ms.toFixed(1), Math.round(size / (ms / 1000)));
24
+ console.log('Read-back matches:', (await ee.read(0x0080, size)).equals(pattern)); // Sequential read, (address, length) → Buffer
25
+
26
+ // --- A write that crosses a page boundary is refused ---
27
+ try {
28
+ await ee.writePage(0x00FC, Buffer.alloc(8)); // Write within one page, (address, data) → void
29
+ } catch (e) {
30
+ console.log('writePage refused:', e.message);
31
+ }
32
+
33
+ await ee.fill(0x0080, size); // Fill a range, (address, length, value=0) → void
34
+ console.log('Zeroed:', (await ee.read(0x0080, size)).equals(Buffer.alloc(size)));
35
+ console.log('Size', ee.size(), 'ready', await ee.waitReady(10)); // Get array size, () → number ; ACK-poll the chip, (timeoutMs) → boolean
36
+ connection.close();
37
+ }
38
+
39
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { CAT24C512Minimal } = require('../../../src/chips/memory/cat24c512');
5
+
6
+ async function main() {
7
+ const connection = new I2CConnection(1, 0x50, null, null, 2); // Create I2C connection, (bus=1, addr=0x50, intPin=null, enPin=null, regBytes=2) → Connection
8
+ const ee = new CAT24C512Minimal(connection); // Create CAT24C512 driver, (connection) → void
9
+
10
+ await ee.writeByte(0x0000, 0xA5); // Write a single byte, (address 0–65535, value 0–255) → void
11
+ console.log('Byte:', (await ee.readByte(0x0000)).toString(16)); // Read a single byte, (address 0–65535) → number
12
+ connection.close();
13
+ }
14
+
15
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ // Complete example: exercise every method in the public API.
4
+ // Reads the registers, takes the bus (with and without bus initialization),
5
+ // disconnects and reconnects the downstream bus, programs the interrupt masks,
6
+ // runs the INT functional test and subscribes to this master's interrupt.
7
+
8
+ const { I2CConnection } = require('periph/src/connection/i2c');
9
+ const { PCA9541Full } = require('periph/src/chips/other/pca9541');
10
+
11
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
12
+
13
+ (async () => {
14
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
15
+ const connection = new I2CConnection(bus, PCA9541Full.I2C_ADDRESS);
16
+ const selector = new PCA9541Full(connection); // Create PCA9541 Full driver, (connection)
17
+ await selector.init(); // Run initialization sequence, () → void (reads CONTROL and ISTAT; writes nothing)
18
+
19
+ console.log(await selector.getControl()); // Read the CONTROL byte, () → int
20
+ console.log(await selector.status()); // Decode CONTROL, () → {hasControl, connected, busInitRequested, ownTest, otherTest}
21
+ console.log(await selector.readAll()); // Read IE, CONTROL, ISTAT, () → {ie, control, istat}
22
+ // one auto-increment read; clears the ISTAT flags
23
+
24
+ console.log(await selector.acquire()); // Take the downstream bus, (busInit=false, timeoutMs=20) → bool
25
+ // reads CONTROL, writes MYBUS/BUSON per the datasheet rule
26
+ console.log(await selector.recoverBus()); // Recover and take the bus, (timeoutMs=20) → bool
27
+ // nine clocks, NACK and STOP on the downstream bus, then connect
28
+ console.log(await selector.hasControl()); // Report control, () → bool
29
+ console.log(await selector.isConnected()); // Report the connection, () → bool
30
+ // BUSON != NBUSON: some master is connected
31
+ await selector.disconnect(); // Isolate the downstream bus, () → void
32
+ // BUSON = NBUSON; ownership bits are left
33
+ await selector.connect(); // Reconnect without changing ownership, () → void
34
+
35
+ await selector.setInterruptMask({ busNotIdle: true }); // Write IE, ({busLost, busNotIdle, busInit, intIn}) → void
36
+ // true = masked, no interrupt
37
+ console.log(await selector.getInterruptMask()); // Read IE, () → int
38
+ await selector.disableInterrupt(PCA9541Full.SOURCE_INT_IN); // Mask one source, (source) → void
39
+ await selector.enableInterrupt(PCA9541Full.SOURCE_INT_IN); // Unmask one source, (source) → void
40
+
41
+ await selector.onInterrupt((status) => console.log('interrupt', status));// Subscribe, (callback(status), {intPin}) → void
42
+ // falling edge on this master's INT, or a 100 ms poll without a pin
43
+ await selector.testInterrupt(true); // Drive this master's INT low, (own=false, other=false) → void
44
+ // the bus bits are written back unchanged
45
+ await sleep(200);
46
+ await selector.testInterrupt(); // Clear the test, () → void
47
+ console.log(await selector.pollInterrupt()); // Read and clear ISTAT, () → int
48
+ await selector.offInterrupt(); // Unsubscribe, () → void
49
+ await selector.release(); // Give the bus to the other master, () → void
50
+
51
+ await connection.close();
52
+ })();
@@ -0,0 +1,61 @@
1
+ 'use strict';
2
+
3
+ // Demo: master failover for one of two redundant masters. Prints the power-up
4
+ // registers (which reveal the /01 or /03 version), unmasks all interrupts, takes
5
+ // the downstream bus with a bus initialization, checks the INT wire with the
6
+ // functional test, hands the bus over, takes it back, toggles the connection and
7
+ // prints the ISTAT flags every 500 ms for 10 s.
8
+
9
+ const { I2CConnection } = require('periph/src/connection/i2c');
10
+ const { PCA9541Full } = require('periph/src/chips/other/pca9541');
11
+
12
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
13
+
14
+ (async () => {
15
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
16
+ const connection = new I2CConnection(bus, PCA9541Full.I2C_ADDRESS);
17
+ const selector = new PCA9541Full(connection); // Create PCA9541 Full driver, (connection)
18
+ await selector.init(); // Run initialization sequence, () → void (reads CONTROL and ISTAT; writes nothing)
19
+
20
+ // --- Power-up state ---
21
+ // CONTROL 0x04 for master 0 means /01 (bus on, master 0); 0x00 means /03 (bus off).
22
+ console.log('IE, CONTROL, ISTAT:', await selector.readAll()); // Read IE, CONTROL, ISTAT, () → {ie, control, istat}
23
+ console.log('status:', await selector.status()); // Decode CONTROL, () → {...}
24
+
25
+ // --- Interrupts ---
26
+ await selector.setInterruptMask(); // Unmask all interrupts, ({...}) → void
27
+ await selector.onInterrupt((status) => { // Subscribe, (callback(status), {intPin}) → void
28
+ if (status & PCA9541Full.SOURCE_BUS_LOST) console.log('BUS_LOST: the other master took the bus');
29
+ if (status & PCA9541Full.SOURCE_TEST) console.log('MYTEST: the INT wire works');
30
+ console.log('ISTAT', status.toString(16));
31
+ });
32
+
33
+ // --- Take the bus with a bus initialization ---
34
+ // The chip clocks nine pulses, a NACK and a STOP on the downstream bus first, which
35
+ // completes a transfer the previous master left half-done.
36
+ console.log('recoverBus', await selector.recoverBus()); // Recover and take the bus, (timeoutMs=20) → bool
37
+ await sleep(10);
38
+ // tBUF: bus-free time before talking to a downstream device through a second Connection
39
+
40
+ // --- INT wire check ---
41
+ await selector.testInterrupt(true); // Drive this master's INT low, (own=false, other=false) → void
42
+ await sleep(100);
43
+ await selector.testInterrupt(); // Clear the test, () → void
44
+
45
+ // --- Hand over and take back ---
46
+ await selector.release(); // Give the bus to the other master, () → void
47
+ console.log('has control after release', await selector.hasControl()); // Report control, () → bool
48
+ console.log('acquire', await selector.acquire()); // Take the downstream bus, (busInit=false, timeoutMs=20) → bool
49
+ await selector.disconnect(); // Isolate the downstream bus, () → void
50
+ await selector.connect(); // Reconnect, () → void
51
+
52
+ // --- Watch the flags for 10 s ---
53
+ for (let i = 0; i < 20; i++) {
54
+ console.log('ISTAT', (await selector.pollInterrupt()).toString(16));// Read and clear ISTAT, () → int
55
+ await sleep(500);
56
+ }
57
+
58
+ await selector.offInterrupt(); // Unsubscribe, () → void
59
+
60
+ await connection.close();
61
+ })();
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ // Minimal example: take and release the downstream I²C bus.
4
+
5
+ const { I2CConnection } = require('periph/src/connection/i2c');
6
+ const { PCA9541Minimal } = require('periph/src/chips/other/pca9541');
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, PCA9541Minimal.I2C_ADDRESS);
13
+ const selector = new PCA9541Minimal(connection); // Create PCA9541 driver, (connection)
14
+ await selector.init(); // Run initialization sequence, () → void (reads CONTROL and ISTAT; writes nothing)
15
+
16
+ console.log('has control', await selector.hasControl()); // Report control, () → bool
17
+ console.log('acquire', await selector.acquire()); // Take the downstream bus, (busInit=false, timeoutMs=20) → bool
18
+ await sleep(1000);
19
+ await selector.release(); // Give the bus to the other master, () → void
20
+ console.log('has control', await selector.hasControl());
21
+
22
+ await connection.close();
23
+ })();
@@ -0,0 +1,74 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { TMP102Full } = require('periph/src/chips/temperature/tmp102');
5
+
6
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
7
+
8
+ (async () => {
9
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
10
+ const connection = new I2CConnection(bus, 0x48);
11
+ const sensor = new TMP102Full(connection); // Create TMP102 Full driver, (connection)
12
+
13
+ const t = await sensor.readTemperature(); // Read temperature, () → number °C
14
+ // decodes TEMPERATURE, 0.0625 °C two's complement
15
+ console.log(`temperature ${t.toFixed(4)} °C`);
16
+ console.log('raw', await sensor.readRaw()); // Read raw count, () → number
17
+ // signed 12/13-bit count, 0.0625 °C per LSB
18
+
19
+ console.log('config 0x' + (await sensor.readConfig()).toString(16)); // Read CONFIGURATION, () → number
20
+ // reset value 0x60A0
21
+ await sensor.writeConfig(await sensor.readConfig()); // Write CONFIGURATION, (value) → None
22
+ // read-only bits are masked out
23
+
24
+ await sensor.setConversionRate(8); // Set conversion rate, (hz) → None
25
+ // 0.25, 1, 4 or 8 Hz
26
+ await sensor.setExtendedMode(true); // Set extended mode, (on) → None
27
+ // EM bit, 13-bit readings up to +255.9375 °C
28
+ await sleep(200);
29
+ console.log(`extended ${(await sensor.readTemperature()).toFixed(4)} °C`);
30
+ await sensor.setExtendedMode(false); // Set extended mode, (on) → None
31
+ // back to 12 bit
32
+
33
+ await sensor.setTHigh(30.0); // Set T_HIGH, (celsius °C) → None
34
+ // rounded to 0.0625 °C
35
+ await sensor.setTLow(10.0); // Set T_LOW, (celsius °C) → None
36
+ // rounded to 0.0625 °C
37
+ console.log('high', await sensor.getTHigh()); // Read T_HIGH, () → number °C
38
+ // same format as TEMPERATURE
39
+ console.log('low', await sensor.getTLow()); // Read T_LOW, () → number °C
40
+ // same format as TEMPERATURE
41
+
42
+ await sensor.setFaultQueue(2); // Set fault queue, (count) → None
43
+ // 1, 2, 4 or 6 consecutive faults
44
+ await sensor.setPolarity(false); // Set ALERT polarity, (activeHigh) → None
45
+ // POL bit, false = active low
46
+ await sensor.setThermostatMode(false); // Set thermostat mode, (interrupt) → None
47
+ // TM bit, false = comparator
48
+ console.log('alert', await sensor.alertState()); // Read alert state, () → boolean
49
+ // AL bit corrected for POL
50
+
51
+ await sensor.shutdown(); // Shut down, () → None
52
+ // SD bit, stops after the current conversion
53
+ console.log(`one-shot ${(await sensor.oneShot()).toFixed(4)} °C`); // One-shot reading, () → number °C
54
+ // OS=1, polls until done, restores the mode
55
+ await sensor.wake(); // Wake, () → None
56
+ // resume continuous conversion
57
+
58
+ console.log('polling', await sensor.pollInterrupt()); // Poll the alert state, () → boolean
59
+ // reads CONFIGURATION and TEMPERATURE
60
+ await sensor.onInterrupt((c) => console.log(`ALERT at ${c.toFixed(2)} °C`)); // Subscribe to ALERT, (callback) → None
61
+ // callback(temperatureC)
62
+ await sleep(3000);
63
+ await sensor.offInterrupt(); // Unsubscribe, () → None
64
+ // stop delivery
65
+
66
+ await sensor.setConversionRate(4); // Set conversion rate, (hz) → None
67
+ // back to the power-on default
68
+ await sensor.setTHigh(80.0); // Set T_HIGH, (celsius °C) → None
69
+ await sensor.setTLow(75.0); // Set T_LOW, (celsius °C) → None
70
+ await sensor.reset(); // General-call reset, () → None
71
+ // resets every general-call device on the bus
72
+
73
+ await connection.close();
74
+ })();
@@ -0,0 +1,71 @@
1
+ 'use strict';
2
+
3
+ // Thermostat with one-shot sampling: read the power-on state, sample at 4 Hz,
4
+ // switch to 8 Hz and the 13-bit mode, take timed one-shot readings, then run an
5
+ // interrupt-mode thermostat around the current temperature while the sensor is
6
+ // warmed with a finger. Finally the power-on defaults are restored.
7
+
8
+ const { I2CConnection } = require('periph/src/connection/i2c');
9
+ const { TMP102Full } = require('periph/src/chips/temperature/tmp102');
10
+
11
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
12
+
13
+ (async () => {
14
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
15
+ const connection = new I2CConnection(bus, 0x48);
16
+ const sensor = new TMP102Full(connection); // Create TMP102 Full driver, (connection)
17
+
18
+ console.log('config 0x' + (await sensor.readConfig()).toString(16)); // Read CONFIGURATION, () → number
19
+ console.log('limits', await sensor.getTLow(), await sensor.getTHigh()); // Read T_LOW, () → number °C; Read T_HIGH, () → number °C
20
+
21
+ // --- Continuous reading at 4 Hz for 5 s ---
22
+ for (let i = 0; i < 20; i++) {
23
+ const t = await sensor.readTemperature(); // Read temperature, () → number °C
24
+ console.log(`${t.toFixed(4)} °C (raw ${await sensor.readRaw()})`); // Read raw count, () → number
25
+ await sleep(250);
26
+ }
27
+
28
+ // --- 8 Hz and the 13-bit extended mode ---
29
+ await sensor.setConversionRate(8); // Set conversion rate, (hz) → None
30
+ await sensor.setExtendedMode(true); // Set extended mode, (on) → None
31
+ await sleep(200);
32
+ console.log(`extended ${(await sensor.readTemperature()).toFixed(4)} °C`); // Read temperature, () → number °C
33
+ await sensor.setExtendedMode(false); // Set extended mode, (on) → None
34
+ await sensor.setConversionRate(4); // Set conversion rate, (hz) → None
35
+
36
+ // --- Shutdown and timed one-shot readings ---
37
+ await sensor.shutdown(); // Shut down, () → None
38
+ for (let i = 0; i < 5; i++) {
39
+ const start = Date.now();
40
+ const t = await sensor.oneShot(); // One-shot reading, () → number °C
41
+ console.log(`one-shot ${t.toFixed(4)} °C in ${Date.now() - start} ms`);
42
+ }
43
+ await sensor.wake(); // Wake, () → None
44
+
45
+ // --- Interrupt-mode thermostat around the current temperature ---
46
+ const now = await sensor.readTemperature(); // Read temperature, () → number °C
47
+ await sensor.setTHigh(now + 1.0); // Set T_HIGH, (celsius °C) → None
48
+ await sensor.setTLow(now - 1.0); // Set T_LOW, (celsius °C) → None
49
+ await sensor.setFaultQueue(2); // Set fault queue, (count) → None
50
+ await sensor.setPolarity(false); // Set ALERT polarity, (activeHigh) → None
51
+ await sensor.setThermostatMode(true); // Set thermostat mode, (interrupt) → None
52
+ console.log('warm the sensor with a finger ...');
53
+ for (let i = 0; i < 40; i++) {
54
+ if (await sensor.pollInterrupt()) { // Poll the alert state, () → boolean
55
+ console.log(`ALERT: ${(await sensor.readTemperature()).toFixed(2)} °C, alert_state=${await sensor.alertState()}`); // Read alert state, () → boolean
56
+ }
57
+ await sleep(250);
58
+ }
59
+ await sensor.onInterrupt((c) => console.log(`ALERT at ${c.toFixed(2)} °C`)); // Subscribe to ALERT, (callback) → None
60
+ await sensor.offInterrupt(); // Unsubscribe, () → None
61
+
62
+ // --- Restore the power-on defaults ---
63
+ await sensor.setThermostatMode(false); // Set thermostat mode, (interrupt) → None
64
+ await sensor.setFaultQueue(1); // Set fault queue, (count) → None
65
+ await sensor.setTHigh(80.0); // Set T_HIGH, (celsius °C) → None
66
+ await sensor.setTLow(75.0); // Set T_LOW, (celsius °C) → None
67
+ await sensor.writeConfig(0x60A0); // Write CONFIGURATION, (value) → None
68
+ await sensor.reset(); // General-call reset, () → None
69
+
70
+ await connection.close();
71
+ })();
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { TMP102Minimal } = require('periph/src/chips/temperature/tmp102');
5
+
6
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
7
+
8
+ (async () => {
9
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
10
+ const connection = new I2CConnection(bus, TMP102Minimal.I2C_ADDRESS);
11
+ const sensor = new TMP102Minimal(connection); // Create TMP102 driver, (connection)
12
+
13
+ for (let i = 0; i < 10; i++) {
14
+ const t = await sensor.readTemperature(); // Read temperature, () → number °C
15
+ console.log(`${t.toFixed(4)} °C`);
16
+ await sleep(1000);
17
+ }
18
+
19
+ await connection.close();
20
+ })();
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ // Complete example: every method of the VL6180X Full API.
4
+
5
+ const { I2CConnection } = require('periph/src/connection/i2c');
6
+ const { VL6180XFull } = require('periph/src/chips/tof/vl6180x');
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, VL6180XFull.I2C_ADDRESS, null, null, 2);
13
+ const sensor = new VL6180XFull(connection); // Create VL6180X Full driver, (connection)
14
+ await sensor.init(); // Run the init sequence, () → undefined
15
+ // private settings only when fresh out of reset
16
+
17
+ console.log(await sensor.readRange()); // Single-shot range, () → number mm
18
+ // rejects on a non-zero range error code
19
+ console.log(await sensor.readRangeStatus()); // Range and error code, () → {mm, error}
20
+ // never rejects on a range error; 0 = valid
21
+ console.log(await sensor.readAlsRaw()); // Single-shot ALS count, () → number
22
+ console.log(await sensor.readLux()); // Single-shot ALS in lux, () → number lux
23
+ // 0.32 lux/count at gain 1, 100 ms
24
+
25
+ await sensor.startContinuousRange(50); // Start continuous ranging, (periodMs) → undefined
26
+ // 10-2550 ms in 10 ms steps
27
+ console.log(await sensor.readRangeContinuous()); // Wait for next sample, () → number mm
28
+ await sensor.stopContinuousRange(); // Stop continuous ranging, () → undefined
29
+ await sensor.startContinuousAls(100); // Start continuous ALS, (periodMs) → undefined
30
+ await sensor.stopContinuousAls(); // Stop continuous ALS, () → undefined
31
+ await sensor.setInterleaved(false); // Interleaved ALS + range, (on) → undefined
32
+
33
+ await sensor.setAlsGain(6); // ALS gain code, (code) → undefined
34
+ // 0 = 20 ... 6 = 1, 7 = 40
35
+ await sensor.setAlsIntegrationTime(100); // ALS integration time, (ms) → undefined
36
+ // 1-512 ms
37
+ await sensor.setAlsScaler(1); // ALS result scaler, (scaler) → undefined
38
+ await sensor.setRangeThresholds(30, 80); // Range thresholds, (low, high) → undefined
39
+ await sensor.setAlsThresholds(100, 5000); // ALS thresholds, (low, high) → undefined
40
+ await sensor.setMaxConvergenceTime(49); // Max convergence time, (ms) → undefined
41
+ await sensor.setAveragingPeriod(48); // Averaging period, (units) → undefined
42
+ await sensor.setRangeOffset(0); // Part-to-part offset, (mm) → undefined
43
+ console.log(await sensor.getRangeOffset()); // Read the offset, () → number mm
44
+ await sensor.setCrosstalkRate(0); // Crosstalk rate, (rateMcps) → undefined
45
+ await sensor.recalibrateVhv(); // Recalibrate VHV, () → undefined
46
+
47
+ console.log((await sensor.modelId()).toString(16), await sensor.revision()); // Identification, () → number ; () → {major, minor}
48
+ // model ID always 0xB4
49
+ console.log(await sensor.readHistory()); // History buffer, () → number[8]
50
+
51
+ await sensor.enableInterrupt(VL6180XFull.SOURCE_RANGE, VL6180XFull.MODE_OUT_OF_WINDOW);// Enable an interrupt, (source, mode) → undefined
52
+ console.log(await sensor.pollInterrupt()); // Read and clear, () → number SOURCE_* mask
53
+ await sensor.onInterrupt((status) => console.log('irq', status)); // Subscribe, (callback) → undefined
54
+ // falling edge on connection.intPin, else a 5 ms polling timer
55
+ await sleep(100); // Wait
56
+ await sensor.offInterrupt(); // Unsubscribe, () → undefined
57
+ await sensor.disableInterrupt(VL6180XFull.SOURCE_RANGE); // Disable an interrupt, (source) → undefined
58
+ await sensor.enableInterrupt(VL6180XFull.SOURCE_RANGE, VL6180XFull.MODE_NEW_SAMPLE_READY);// Restore the default mode, (source, mode) → undefined
59
+ // await sensor.setAddress(0x30); // Change address (volatile), (address) → undefined
60
+
61
+ await connection.close();
62
+ })();
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ // Demo: proximity-activated light meter — ranging, interrupts, ALS, interleaved mode, address change.
4
+
5
+ const { I2CConnection } = require('periph/src/connection/i2c');
6
+ const { VL6180XFull } = require('periph/src/chips/tof/vl6180x');
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, VL6180XFull.I2C_ADDRESS, null, null, 2);
13
+ const sensor = new VL6180XFull(connection); // Create VL6180X Full driver, (connection)
14
+ await sensor.init(); // Run the init sequence, () → undefined
15
+
16
+ // --- Identification ---
17
+ console.log('model', (await sensor.modelId()).toString(16), 'revision', await sensor.revision());// Identification, () → number ; () → {major, minor}
18
+
19
+ // --- Single-shot ranging at 5 Hz for 10 s ---
20
+ for (let i = 0; i < 50; i++) {
21
+ console.log((await sensor.readRange()) + ' mm'); // Single-shot range, () → number mm
22
+ await sleep(200);
23
+ }
24
+
25
+ // --- Continuous ranging at 50 ms: move a hand towards the sensor ---
26
+ await sensor.startContinuousRange(50); // Start continuous ranging, (periodMs) → undefined
27
+ for (let i = 0; i < 40; i++) {
28
+ const { mm, error } = await sensor.readRangeStatus(); // Range and error code, () → {mm, error}
29
+ console.log(`${mm} mm (error ${error})`);
30
+ }
31
+ await sensor.stopContinuousRange(); // Stop continuous ranging, () → undefined
32
+ await sleep(200);
33
+
34
+ // --- Out-of-window interrupt for 30-80 mm ---
35
+ await sensor.setRangeThresholds(30, 80); // Range thresholds, (low, high) → undefined
36
+ await sensor.enableInterrupt(VL6180XFull.SOURCE_RANGE, VL6180XFull.MODE_OUT_OF_WINDOW);// Enable an interrupt, (source, mode) → undefined
37
+ await sensor.startContinuousRange(50);
38
+ for (let i = 0; i < 50; i++) {
39
+ if ((await sensor.pollInterrupt()) & VL6180XFull.SOURCE_RANGE) console.log('range out of window');// Read and clear, () → number SOURCE_* mask
40
+ await sleep(100);
41
+ }
42
+ await sensor.stopContinuousRange();
43
+ await sleep(200);
44
+ await sensor.enableInterrupt(VL6180XFull.SOURCE_RANGE, VL6180XFull.MODE_NEW_SAMPLE_READY);
45
+
46
+ // --- Light meter at gain 1 and 20 ---
47
+ for (const code of [6, 0]) {
48
+ await sensor.setAlsGain(code); // ALS gain code, (code) → undefined
49
+ console.log(`gain code ${code}: ${(await sensor.readLux()).toFixed(2)} lux`);// Single-shot ALS in lux, () → number lux
50
+ }
51
+ await sensor.setAlsGain(6);
52
+
53
+ // --- Interleaved ALS + range for 5 s ---
54
+ await sensor.setInterleaved(true); // Interleaved ALS + range, (on) → undefined
55
+ await sensor.startContinuousAls(100); // Start continuous ALS, (periodMs) → undefined
56
+ await sleep(5000);
57
+ await sensor.stopContinuousAls(); // Stop continuous ALS, () → undefined
58
+ await sensor.setInterleaved(false);
59
+
60
+ // --- Calibration and diagnostics ---
61
+ await sensor.setRangeOffset(0); // Part-to-part offset, (mm) → undefined
62
+ await sensor.setCrosstalkRate(0); // Crosstalk rate, (rateMcps) → undefined
63
+ await sensor.setAlsIntegrationTime(100); // ALS integration time, (ms) → undefined
64
+ await sensor.setAlsScaler(1); // ALS result scaler, (scaler) → undefined
65
+ await sensor.recalibrateVhv(); // Recalibrate VHV, () → undefined
66
+ console.log('als raw', await sensor.readAlsRaw(), 'offset', await sensor.getRangeOffset());// Single-shot ALS count, () → number ; () → number mm
67
+ console.log('history', await sensor.readHistory()); // History buffer, () → number[8]
68
+
69
+ // --- Change the device address and back (volatile) ---
70
+ await sensor.setAddress(0x30); // Change address, (address) → undefined
71
+ const moved = new VL6180XFull(new I2CConnection(bus, 0x30, null, null, 2));
72
+ await moved.init();
73
+ console.log('on 0x30: model', (await moved.modelId()).toString(16));
74
+ await moved.setAddress(VL6180XFull.I2C_ADDRESS);
75
+
76
+ await connection.close();
77
+ })();
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ // Minimal example: print the distance in mm five times a second.
4
+
5
+ const { I2CConnection } = require('periph/src/connection/i2c');
6
+ const { VL6180XMinimal } = require('periph/src/chips/tof/vl6180x');
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, VL6180XMinimal.I2C_ADDRESS, null, null, 2);
13
+ const sensor = new VL6180XMinimal(connection); // Create VL6180X driver, (connection)
14
+ await sensor.init(); // Run the init sequence, () → undefined
15
+
16
+ for (let i = 0; i < 50; i++) {
17
+ const mm = await sensor.readRange(); // Single-shot range, () → number mm
18
+ console.log(`${mm} mm`);
19
+ await sleep(200);
20
+ }
21
+
22
+ await connection.close();
23
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "periph",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "JavaScript drivers for peripheral chips (I2C, SPI)",
5
5
  "main": "src/index.js",
6
6
  "keywords": [