periph 1.3.3 → 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 (150) hide show
  1. package/README.md +36 -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/es8311/complete.js +80 -0
  24. package/examples/adc_dac/es8311/demo.js +59 -0
  25. package/examples/adc_dac/es8311/minimal.js +18 -0
  26. package/examples/adc_dac/mcp3422/complete.js +48 -0
  27. package/examples/adc_dac/mcp3422/demo.js +49 -0
  28. package/examples/adc_dac/mcp3422/minimal.js +25 -0
  29. package/examples/adc_dac/mcp4xxx/complete.js +97 -0
  30. package/examples/adc_dac/mcp4xxx/demo.js +76 -0
  31. package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
  32. package/examples/display/sh1106/complete.js +101 -0
  33. package/examples/display/sh1106/demo.js +68 -0
  34. package/examples/display/sh1106/minimal.js +25 -0
  35. package/examples/display/ssd1305/complete.js +129 -0
  36. package/examples/display/ssd1305/demo.js +71 -0
  37. package/examples/display/ssd1305/minimal.js +25 -0
  38. package/examples/display/ssd1306/complete.js +115 -0
  39. package/examples/display/ssd1306/demo.js +70 -0
  40. package/examples/display/ssd1306/minimal.js +25 -0
  41. package/examples/environmental/am2315/complete.js +34 -0
  42. package/examples/environmental/am2315/demo.js +46 -0
  43. package/examples/environmental/am2315/minimal.js +21 -0
  44. package/examples/humidity/htu21df/complete.js +49 -0
  45. package/examples/humidity/htu21df/demo.js +50 -0
  46. package/examples/humidity/htu21df/minimal.js +22 -0
  47. package/examples/humidity/sht31/complete.js +53 -0
  48. package/examples/humidity/sht31/demo.js +53 -0
  49. package/examples/humidity/sht31/minimal.js +21 -0
  50. package/examples/humidity/si7021/complete.js +55 -0
  51. package/examples/humidity/si7021/demo.js +58 -0
  52. package/examples/humidity/si7021/minimal.js +21 -0
  53. package/examples/imu/bno055/complete.js +85 -0
  54. package/examples/imu/bno055/demo.js +70 -0
  55. package/examples/imu/bno055/minimal.js +20 -0
  56. package/examples/imu/qmi8658a/complete.js +134 -0
  57. package/examples/imu/qmi8658a/demo.js +81 -0
  58. package/examples/imu/qmi8658a/minimal.js +22 -0
  59. package/examples/io_expander/pca6408a/complete.js +63 -0
  60. package/examples/io_expander/pca6408a/demo.js +58 -0
  61. package/examples/io_expander/pca6408a/minimal.js +20 -0
  62. package/examples/io_expander/pca6416a/complete.js +68 -0
  63. package/examples/io_expander/pca6416a/demo.js +60 -0
  64. package/examples/io_expander/pca6416a/minimal.js +20 -0
  65. package/examples/io_expander/pca9536/complete.js +63 -0
  66. package/examples/io_expander/pca9536/demo.js +57 -0
  67. package/examples/io_expander/pca9536/minimal.js +20 -0
  68. package/examples/io_expander/pca9539/complete.js +68 -0
  69. package/examples/io_expander/pca9539/demo.js +60 -0
  70. package/examples/io_expander/pca9539/minimal.js +20 -0
  71. package/examples/light/max44009/complete.js +56 -0
  72. package/examples/light/max44009/demo.js +49 -0
  73. package/examples/light/max44009/minimal.js +22 -0
  74. package/examples/memory/at24c02n/complete.js +39 -0
  75. package/examples/memory/at24c02n/demo.js +39 -0
  76. package/examples/memory/at24c02n/minimal.js +15 -0
  77. package/examples/memory/at24c64/complete.js +39 -0
  78. package/examples/memory/at24c64/demo.js +39 -0
  79. package/examples/memory/at24c64/minimal.js +15 -0
  80. package/examples/memory/cat24c512/complete.js +39 -0
  81. package/examples/memory/cat24c512/demo.js +39 -0
  82. package/examples/memory/cat24c512/minimal.js +15 -0
  83. package/examples/motor/tmc2130/complete.js +138 -0
  84. package/examples/motor/tmc2130/demo.js +96 -0
  85. package/examples/motor/tmc2130/minimal.js +30 -0
  86. package/examples/motor/tmc2208/complete.js +92 -0
  87. package/examples/motor/tmc2208/demo.js +85 -0
  88. package/examples/motor/tmc2208/minimal.js +25 -0
  89. package/examples/motor/tmc2209/complete.js +106 -0
  90. package/examples/motor/tmc2209/demo.js +97 -0
  91. package/examples/motor/tmc2209/minimal.js +25 -0
  92. package/examples/motor/tmc5160/complete.js +205 -0
  93. package/examples/motor/tmc5160/demo.js +104 -0
  94. package/examples/motor/tmc5160/minimal.js +35 -0
  95. package/examples/other/pca9541/complete.js +52 -0
  96. package/examples/other/pca9541/demo.js +61 -0
  97. package/examples/other/pca9541/minimal.js +23 -0
  98. package/examples/power/axp2101/complete.js +135 -0
  99. package/examples/power/axp2101/demo.js +68 -0
  100. package/examples/power/axp2101/minimal.js +24 -0
  101. package/examples/rtc/pcf85063a/complete.js +68 -0
  102. package/examples/rtc/pcf85063a/demo.js +76 -0
  103. package/examples/rtc/pcf85063a/minimal.js +20 -0
  104. package/examples/temperature/tmp102/complete.js +74 -0
  105. package/examples/temperature/tmp102/demo.js +71 -0
  106. package/examples/temperature/tmp102/minimal.js +20 -0
  107. package/examples/tof/vl6180x/complete.js +62 -0
  108. package/examples/tof/vl6180x/demo.js +77 -0
  109. package/examples/tof/vl6180x/minimal.js +23 -0
  110. package/package.json +1 -1
  111. package/src/chips/adc_dac/ad5251.js +345 -0
  112. package/src/chips/adc_dac/ad5252.js +345 -0
  113. package/src/chips/adc_dac/ads7828.js +225 -0
  114. package/src/chips/adc_dac/cat5171.js +213 -0
  115. package/src/chips/adc_dac/cs43l22.js +645 -0
  116. package/src/chips/adc_dac/ds1881.js +357 -0
  117. package/src/chips/adc_dac/ds3502.js +241 -0
  118. package/src/chips/adc_dac/es8311.js +621 -0
  119. package/src/chips/adc_dac/mcp3422.js +312 -0
  120. package/src/chips/adc_dac/mcp4xxx.js +982 -0
  121. package/src/chips/display/mono_oled.js +182 -0
  122. package/src/chips/display/sh1106.js +496 -0
  123. package/src/chips/display/ssd1305.js +655 -0
  124. package/src/chips/display/ssd1306.js +458 -0
  125. package/src/chips/environmental/am2315.js +237 -0
  126. package/src/chips/humidity/htu21df.js +222 -0
  127. package/src/chips/humidity/sht31.js +284 -0
  128. package/src/chips/humidity/si7021.js +282 -0
  129. package/src/chips/imu/bno055.js +496 -0
  130. package/src/chips/imu/qmi8658a.js +1011 -0
  131. package/src/chips/io_expander/pca6408a.js +407 -0
  132. package/src/chips/io_expander/pca6416a.js +455 -0
  133. package/src/chips/io_expander/pca9536.js +398 -0
  134. package/src/chips/io_expander/pca9539.js +457 -0
  135. package/src/chips/light/max44009.js +336 -0
  136. package/src/chips/memory/at24c02n.js +220 -0
  137. package/src/chips/memory/at24c64.js +220 -0
  138. package/src/chips/memory/cat24c512.js +221 -0
  139. package/src/chips/motor/tmc2130.js +537 -0
  140. package/src/chips/motor/tmc2208.js +490 -0
  141. package/src/chips/motor/tmc2209.js +565 -0
  142. package/src/chips/motor/tmc5160.js +900 -0
  143. package/src/chips/other/pca9541.js +315 -0
  144. package/src/chips/power/axp2101.js +968 -0
  145. package/src/chips/rtc/pcf85063a.js +482 -0
  146. package/src/chips/temperature/tmp102.js +361 -0
  147. package/src/chips/tof/vl6180x.js +580 -0
  148. package/src/connection/uart.js +9 -0
  149. package/src/connection/uart_mock.js +65 -0
  150. package/src/discovery/registry.js +85 -8
@@ -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.3.3",
3
+ "version": "1.5.0",
4
4
  "description": "JavaScript drivers for peripheral chips (I2C, SPI)",
5
5
  "main": "src/index.js",
6
6
  "keywords": [
@@ -0,0 +1,345 @@
1
+ 'use strict';
2
+
3
+ const _RDAC = [0x01, 0x03];
4
+ const _EEMEM_FLAG = 0x20;
5
+ const _TOL_INT = [0x3A, 0x3E];
6
+
7
+ const _Q_NOP = 0x80;
8
+ const _Q_RESTORE = 0x88;
9
+ const _Q_STORE = 0x90;
10
+ const _Q_HALVE = 0x98;
11
+ const _Q_DEC_STEP = 0xA8;
12
+ const _Q_INC_6DB = 0xC0;
13
+ const _Q_INC_STEP = 0xD0;
14
+ const _Q_DEC_6DB_ALL = 0xA0;
15
+ const _Q_DEC_STEP_ALL = 0xB0;
16
+ const _Q_RESET = 0xB8;
17
+ const _Q_INC_6DB_ALL = 0xC8;
18
+ const _Q_INC_STEP_ALL = 0xD8;
19
+
20
+ const _POLL_MS = 2;
21
+ const _POLL_TIMEOUT_MS = 100;
22
+
23
+ const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
24
+
25
+ function _checkChannel(channel) {
26
+ if (channel !== 0 && channel !== 1) throw new RangeError('channel must be 0 or 1');
27
+ }
28
+
29
+ function _clampCode(code) {
30
+ code = Math.trunc(code);
31
+ if (code < 0) return 0;
32
+ if (code > 63) return 63;
33
+ return code;
34
+ }
35
+
36
+ /**
37
+ * AD5251 dual 64-position I²C digital potentiometer with EEMEM (Analog
38
+ * Devices) — minimal interface.
39
+ *
40
+ * Two independent 3-terminal potentiometers (A, W, B). The chip has no
41
+ * register map: the instruction byte works as a one-byte register pointer
42
+ * (RDAC1 = 0x01 for channel 0, RDAC3 = 0x03 for channel 1), so the driver
43
+ * uses a RegisterConnection with a 1-byte register width. The I²C address is
44
+ * 0x2C–0x2F (AD1, AD0 pins). Wipers power up from EEMEM (midscale 32 until
45
+ * a value is stored).
46
+ */
47
+ class AD5251Minimal {
48
+ /**
49
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x2C–0x2F).
50
+ * @param {number} [rAb=50000] - End-to-end resistance RAB in ohms.
51
+ * @param {number} [rWiper=75] - Wiper resistance RW in ohms.
52
+ */
53
+ constructor(connection, rAb = 50000.0, rWiper = 75.0) {
54
+ this._conn = connection;
55
+ this._rAb = rAb;
56
+ this._rW = rWiper;
57
+ }
58
+
59
+ async _writeCode(channel, code) {
60
+ _checkChannel(channel);
61
+ await this._conn.writeReg(_RDAC[channel], _clampCode(code));
62
+ }
63
+
64
+ async _readCode(channel) {
65
+ _checkChannel(channel);
66
+ const buf = await this._conn.readReg(_RDAC[channel], 1);
67
+ return buf[0];
68
+ }
69
+
70
+ /**
71
+ * Prepare the driver. The AD5251 has no identity register and needs no
72
+ * initialisation writes, so this performs no I²C traffic.
73
+ * @returns {Promise<void>}
74
+ */
75
+ async init() {}
76
+
77
+ /**
78
+ * Set the wiper position of a channel.
79
+ * @param {number} channel - 0 or 1.
80
+ * @param {number} code - Wiper position 0–63 (clamped).
81
+ * @returns {Promise<void>}
82
+ */
83
+ async setWiper(channel, code) {
84
+ await this._writeCode(channel, code);
85
+ }
86
+
87
+ /**
88
+ * Read back the wiper position of a channel (instruction byte alone,
89
+ * then one data byte).
90
+ * @param {number} channel - 0 or 1.
91
+ * @returns {Promise<number>} Wiper position 0–63.
92
+ */
93
+ async getWiper(channel) {
94
+ return this._readCode(channel);
95
+ }
96
+
97
+ /**
98
+ * Set the W–B resistance to the nearest reachable code.
99
+ * @param {number} channel - 0 or 1.
100
+ * @param {number} ohms - Target W–B resistance in ohms (clamped to the reachable range).
101
+ * @returns {Promise<void>}
102
+ */
103
+ async setResistance(channel, ohms) {
104
+ await this._writeCode(channel, Math.floor((ohms - this._rW) / this._rAb * 64 + 0.5));
105
+ }
106
+
107
+ /**
108
+ * Read the W–B resistance for the current wiper code.
109
+ * @param {number} channel - 0 or 1.
110
+ * @returns {Promise<number>} Resistance in ohms, `D / 64 * RAB + RW`.
111
+ */
112
+ async getResistance(channel) {
113
+ return (await this._readCode(channel)) / 64 * this._rAb + this._rW;
114
+ }
115
+ }
116
+
117
+ /**
118
+ * AD5251 full interface — extends Minimal with ratio and W–A helpers, quick
119
+ * commands (±1 step, ±6 dB), EEMEM store/restore/reset, user EEMEM and the
120
+ * factory RAB tolerance. EEMEM writes poll with the NOP quick command until
121
+ * the chip acknowledges again (timeout 100 ms).
122
+ */
123
+ class AD5251Full extends AD5251Minimal {
124
+ async _quickByte(byte) {
125
+ await this._conn.write(Buffer.from([byte]));
126
+ }
127
+
128
+ async _quick(base, baseAll, channel) {
129
+ if (channel === undefined || channel === null) {
130
+ await this._quickByte(baseAll);
131
+ return;
132
+ }
133
+ _checkChannel(channel);
134
+ await this._quickByte(base | _RDAC[channel]);
135
+ }
136
+
137
+ async _waitReady() {
138
+ const start = Date.now();
139
+ for (;;) {
140
+ try {
141
+ await this._quickByte(_Q_NOP);
142
+ return;
143
+ } catch (e) {
144
+ if (Date.now() - start >= _POLL_TIMEOUT_MS) {
145
+ throw new Error('AD5251 busy: EEMEM write did not finish');
146
+ }
147
+ await _sleep(_POLL_MS);
148
+ }
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Write both wipers in one consecutive-write transaction.
154
+ * @param {number} code0 - Channel 0 wiper position 0–63.
155
+ * @param {number} code1 - Channel 1 wiper position 0–63.
156
+ * @returns {Promise<void>}
157
+ */
158
+ async setBoth(code0, code1) {
159
+ await this._conn.writeReg(_RDAC[0], Buffer.from([_clampCode(code0), _clampCode(code1)]));
160
+ }
161
+
162
+ /**
163
+ * Set the wiper by divider ratio V(W−B)/V(A−B).
164
+ * @param {number} channel - 0 or 1.
165
+ * @param {number} ratio - 0.0–1.0; the code is round(ratio * 64) clamped to 63.
166
+ * @returns {Promise<void>}
167
+ */
168
+ async setRatio(channel, ratio) {
169
+ await this._writeCode(channel, Math.floor(ratio * 64 + 0.5));
170
+ }
171
+
172
+ /**
173
+ * Read the divider ratio of a channel.
174
+ * @param {number} channel - 0 or 1.
175
+ * @returns {Promise<number>} `code / 64`.
176
+ */
177
+ async getRatio(channel) {
178
+ return (await this._readCode(channel)) / 64;
179
+ }
180
+
181
+ /**
182
+ * Set the W–A resistance (rheostat mode, B open) to the nearest code.
183
+ * @param {number} channel - 0 or 1.
184
+ * @param {number} ohms - Target W–A resistance in ohms.
185
+ * @returns {Promise<void>}
186
+ */
187
+ async setResistanceWa(channel, ohms) {
188
+ const steps = Math.floor((ohms - this._rW) / this._rAb * 64 + 0.5);
189
+ await this._writeCode(channel, 64 - steps);
190
+ }
191
+
192
+ /**
193
+ * Read the W–A resistance for the current wiper code.
194
+ * @param {number} channel - 0 or 1.
195
+ * @returns {Promise<number>} Resistance in ohms, `(64 - D) / 64 * RAB + RW`.
196
+ */
197
+ async getResistanceWa(channel) {
198
+ return (64 - (await this._readCode(channel))) / 64 * this._rAb + this._rW;
199
+ }
200
+
201
+ /**
202
+ * Increment the wiper by one step (quick command).
203
+ * @param {number} [channel] - 0 or 1; omit for both channels.
204
+ * @returns {Promise<void>}
205
+ */
206
+ async increment(channel) {
207
+ await this._quick(_Q_INC_STEP, _Q_INC_STEP_ALL, channel);
208
+ }
209
+
210
+ /**
211
+ * Decrement the wiper by one step (quick command).
212
+ * @param {number} [channel] - 0 or 1; omit for both channels.
213
+ * @returns {Promise<void>}
214
+ */
215
+ async decrement(channel) {
216
+ await this._quick(_Q_DEC_STEP, _Q_DEC_STEP_ALL, channel);
217
+ }
218
+
219
+ /**
220
+ * Increase by 6 dB — the code doubles (quick command).
221
+ * @param {number} [channel] - 0 or 1; omit for both channels.
222
+ * @returns {Promise<void>}
223
+ */
224
+ async double(channel) {
225
+ await this._quick(_Q_INC_6DB, _Q_INC_6DB_ALL, channel);
226
+ }
227
+
228
+ /**
229
+ * Decrease by 6 dB — the code halves (quick command).
230
+ * @param {number} [channel] - 0 or 1; omit for both channels.
231
+ * @returns {Promise<void>}
232
+ */
233
+ async halve(channel) {
234
+ await this._quick(_Q_HALVE, _Q_DEC_6DB_ALL, channel);
235
+ }
236
+
237
+ /**
238
+ * Store the wiper in EEMEM, then wait for the ≈ 26 ms write cycle.
239
+ * @param {number} [channel] - 0 or 1; omit for both channels.
240
+ * @returns {Promise<void>}
241
+ * @throws {Error} If the chip does not acknowledge again within 100 ms.
242
+ */
243
+ async store(channel) {
244
+ if (channel === undefined || channel === null) {
245
+ await this._quick(_Q_STORE, 0, 0);
246
+ await this._waitReady();
247
+ await this._quick(_Q_STORE, 0, 1);
248
+ } else {
249
+ await this._quick(_Q_STORE, 0, channel);
250
+ }
251
+ await this._waitReady();
252
+ }
253
+
254
+ /**
255
+ * Restore the wiper from EEMEM, then send a NOP to return to idle.
256
+ * @param {number} [channel] - 0 or 1; omit for both channels.
257
+ * @returns {Promise<void>}
258
+ */
259
+ async restore(channel) {
260
+ if (channel === undefined || channel === null) {
261
+ await this._quick(_Q_RESTORE, 0, 0);
262
+ await this._quick(_Q_RESTORE, 0, 1);
263
+ } else {
264
+ await this._quick(_Q_RESTORE, 0, channel);
265
+ }
266
+ await this.nop();
267
+ }
268
+
269
+ /**
270
+ * Quick-command reset: restore both wipers from EEMEM and wait.
271
+ * @returns {Promise<void>}
272
+ * @throws {Error} If the chip does not acknowledge again within 100 ms.
273
+ */
274
+ async reset() {
275
+ await this._quickByte(_Q_RESET);
276
+ await this._waitReady();
277
+ }
278
+
279
+ /**
280
+ * Read a user EEMEM byte.
281
+ * @param {number} index - 4–15.
282
+ * @returns {Promise<number>} The stored byte.
283
+ */
284
+ async readEemem(index) {
285
+ if (index < 4 || index > 15) throw new RangeError('index must be 4..15');
286
+ return (await this._conn.readReg(_EEMEM_FLAG | index, 1))[0];
287
+ }
288
+
289
+ /**
290
+ * Write a user EEMEM byte and wait for the ≈ 26 ms write cycle.
291
+ * @param {number} index - 4–15.
292
+ * @param {number} value - Byte 0–63.
293
+ * @returns {Promise<void>}
294
+ * @throws {Error} If the chip does not acknowledge again within 100 ms.
295
+ */
296
+ async writeEemem(index, value) {
297
+ if (index < 4 || index > 15) throw new RangeError('index must be 4..15');
298
+ await this._conn.writeReg(_EEMEM_FLAG | index, value & 0xFF);
299
+ await this._waitReady();
300
+ }
301
+
302
+ /**
303
+ * Read the factory-stored RAB tolerance of a channel.
304
+ * @param {number} channel - 0 or 1.
305
+ * @returns {Promise<number>} Tolerance in percent (sign-magnitude integer byte plus decimal byte / 256).
306
+ */
307
+ async readTolerance(channel) {
308
+ _checkChannel(channel);
309
+ const raw = await this._conn.readReg(_TOL_INT[channel], 2);
310
+ const value = (raw[0] & 0x7F) + raw[1] / 256;
311
+ return (raw[0] & 0x80) ? -value : value;
312
+ }
313
+
314
+ /**
315
+ * Actual RAB of a channel from its stored tolerance.
316
+ * @param {number} channel - 0 or 1.
317
+ * @returns {Promise<number>} `rAb * (1 + tolerance / 100)` in ohms.
318
+ */
319
+ async rabActual(channel) {
320
+ return this._rAb * (1 + (await this.readTolerance(channel)) / 100);
321
+ }
322
+
323
+ /**
324
+ * Send the NOP quick command (0x80), returning the chip to idle.
325
+ * @returns {Promise<void>}
326
+ */
327
+ async nop() {
328
+ await this._quickByte(_Q_NOP);
329
+ }
330
+
331
+ /**
332
+ * Change the resistance parameters used by conversions (no I²C traffic).
333
+ * @param {number} rAb - End-to-end resistance RAB in ohms.
334
+ * @param {number} [rWiper] - Wiper resistance RW in ohms; unchanged if omitted.
335
+ */
336
+ setRab(rAb, rWiper) {
337
+ this._rAb = rAb;
338
+ if (rWiper !== undefined && rWiper !== null) this._rW = rWiper;
339
+ }
340
+ }
341
+
342
+ /** Default 7-bit I²C address (AD1 = AD0 = GND); 0x2C–0x2F are possible. */
343
+ AD5251Minimal.I2C_ADDRESS = 0x2C;
344
+
345
+ module.exports = { AD5251Minimal, AD5251Full };