periph 1.3.2 → 1.4.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 (114) hide show
  1. package/README.md +27 -0
  2. package/examples/accelerometer/bma180/complete.js +83 -0
  3. package/examples/accelerometer/bma180/demo.js +47 -0
  4. package/examples/accelerometer/bma180/minimal.js +21 -0
  5. package/examples/accelerometer/fxos8700/complete.js +111 -0
  6. package/examples/accelerometer/fxos8700/demo.js +80 -0
  7. package/examples/accelerometer/fxos8700/minimal.js +20 -0
  8. package/examples/accelerometer/lis3dh/complete.js +123 -0
  9. package/examples/accelerometer/lis3dh/demo.js +99 -0
  10. package/examples/accelerometer/lis3dh/minimal.js +19 -0
  11. package/examples/accelerometer/lsm303/complete.js +82 -0
  12. package/examples/accelerometer/lsm303/demo.js +96 -0
  13. package/examples/accelerometer/lsm303/minimal.js +23 -0
  14. package/examples/accelerometer/mma8451q/complete.js +96 -0
  15. package/examples/accelerometer/mma8451q/demo.js +120 -0
  16. package/examples/accelerometer/mma8451q/minimal.js +20 -0
  17. package/examples/adc_dac/ad5243/complete.js +52 -0
  18. package/examples/adc_dac/ad5243/demo.js +50 -0
  19. package/examples/adc_dac/ad5243/minimal.js +23 -0
  20. package/examples/adc_dac/ad5248/complete.js +49 -0
  21. package/examples/adc_dac/ad5248/demo.js +42 -0
  22. package/examples/adc_dac/ad5248/minimal.js +21 -0
  23. package/examples/adc_dac/ads1013/complete.js +51 -0
  24. package/examples/adc_dac/ads1013/demo.js +51 -0
  25. package/examples/adc_dac/ads1013/minimal.js +21 -0
  26. package/examples/adc_dac/ads1014/complete.js +88 -0
  27. package/examples/adc_dac/ads1014/demo.js +52 -0
  28. package/examples/adc_dac/ads1014/minimal.js +21 -0
  29. package/examples/adc_dac/ads1113/complete.js +47 -0
  30. package/examples/adc_dac/ads1113/demo.js +39 -0
  31. package/examples/adc_dac/ads1113/minimal.js +20 -0
  32. package/examples/adc_dac/ads1114/complete.js +77 -0
  33. package/examples/adc_dac/ads1114/demo.js +47 -0
  34. package/examples/adc_dac/ads1114/minimal.js +20 -0
  35. package/examples/adc_dac/ads1115/complete.js +82 -0
  36. package/examples/adc_dac/ads1115/demo.js +55 -0
  37. package/examples/adc_dac/ads1115/minimal.js +20 -0
  38. package/examples/adc_dac/es8311/complete.js +80 -0
  39. package/examples/adc_dac/es8311/demo.js +59 -0
  40. package/examples/adc_dac/es8311/minimal.js +18 -0
  41. package/examples/adc_dac/mcp4xxx/complete.js +97 -0
  42. package/examples/adc_dac/mcp4xxx/demo.js +76 -0
  43. package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
  44. package/examples/display/pcf8578/complete.js +58 -0
  45. package/examples/display/pcf8578/demo.js +62 -0
  46. package/examples/display/pcf8578/minimal.js +19 -0
  47. package/examples/display/pcf8579/complete.js +58 -0
  48. package/examples/display/pcf8579/demo.js +61 -0
  49. package/examples/display/pcf8579/minimal.js +19 -0
  50. package/examples/imu/icm20948/complete.js +114 -0
  51. package/examples/imu/icm20948/demo.js +79 -0
  52. package/examples/imu/icm20948/minimal.js +22 -0
  53. package/examples/imu/qmi8658a/complete.js +134 -0
  54. package/examples/imu/qmi8658a/demo.js +81 -0
  55. package/examples/imu/qmi8658a/minimal.js +22 -0
  56. package/examples/io_expander/mcp23008/complete.js +70 -0
  57. package/examples/io_expander/mcp23008/demo.js +58 -0
  58. package/examples/io_expander/mcp23008/minimal.js +20 -0
  59. package/examples/memory/mb85rc/complete.js +40 -0
  60. package/examples/memory/mb85rc/demo.js +58 -0
  61. package/examples/memory/mb85rc/minimal.js +15 -0
  62. package/examples/motor/drv2605/complete.js +100 -0
  63. package/examples/motor/drv2605/demo.js +55 -0
  64. package/examples/motor/drv2605/minimal.js +24 -0
  65. package/examples/motor/tmc2130/complete.js +138 -0
  66. package/examples/motor/tmc2130/demo.js +96 -0
  67. package/examples/motor/tmc2130/minimal.js +30 -0
  68. package/examples/motor/tmc2208/complete.js +92 -0
  69. package/examples/motor/tmc2208/demo.js +85 -0
  70. package/examples/motor/tmc2208/minimal.js +25 -0
  71. package/examples/motor/tmc2209/complete.js +106 -0
  72. package/examples/motor/tmc2209/demo.js +97 -0
  73. package/examples/motor/tmc2209/minimal.js +25 -0
  74. package/examples/motor/tmc5160/complete.js +205 -0
  75. package/examples/motor/tmc5160/demo.js +104 -0
  76. package/examples/motor/tmc5160/minimal.js +35 -0
  77. package/examples/power/axp2101/complete.js +135 -0
  78. package/examples/power/axp2101/demo.js +68 -0
  79. package/examples/power/axp2101/minimal.js +24 -0
  80. package/examples/rtc/pcf85063a/complete.js +68 -0
  81. package/examples/rtc/pcf85063a/demo.js +76 -0
  82. package/examples/rtc/pcf85063a/minimal.js +20 -0
  83. package/package.json +1 -1
  84. package/src/chips/accelerometer/bma180.js +573 -0
  85. package/src/chips/accelerometer/fxos8700.js +1600 -0
  86. package/src/chips/accelerometer/lis3dh.js +1341 -0
  87. package/src/chips/accelerometer/lsm303.js +1471 -0
  88. package/src/chips/accelerometer/mma8451q.js +1406 -0
  89. package/src/chips/adc_dac/_ads111x_base.js +272 -0
  90. package/src/chips/adc_dac/ad5243.js +231 -0
  91. package/src/chips/adc_dac/ad5248.js +194 -0
  92. package/src/chips/adc_dac/ads1013.js +219 -0
  93. package/src/chips/adc_dac/ads1014.js +509 -0
  94. package/src/chips/adc_dac/ads1113.js +112 -0
  95. package/src/chips/adc_dac/ads1114.js +201 -0
  96. package/src/chips/adc_dac/ads1115.js +232 -0
  97. package/src/chips/adc_dac/es8311.js +621 -0
  98. package/src/chips/adc_dac/mcp4xxx.js +982 -0
  99. package/src/chips/display/pcf8578.js +452 -0
  100. package/src/chips/display/pcf8579.js +451 -0
  101. package/src/chips/imu/icm20948.js +849 -0
  102. package/src/chips/imu/qmi8658a.js +1011 -0
  103. package/src/chips/io_expander/mcp23008.js +558 -0
  104. package/src/chips/memory/mb85rc.js +166 -0
  105. package/src/chips/motor/drv2605.js +592 -0
  106. package/src/chips/motor/tmc2130.js +537 -0
  107. package/src/chips/motor/tmc2208.js +490 -0
  108. package/src/chips/motor/tmc2209.js +565 -0
  109. package/src/chips/motor/tmc5160.js +900 -0
  110. package/src/chips/power/axp2101.js +968 -0
  111. package/src/chips/rtc/pcf85063a.js +482 -0
  112. package/src/connection/uart.js +9 -0
  113. package/src/connection/uart_mock.js +65 -0
  114. package/src/discovery/registry.js +45 -19
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ // Voltage-divider level setter: steps channel 0 as a divider (A at VDD, B at
4
+ // GND) through several ratios and prints the expected output voltage at
5
+ // VDD = 3.3 V; sets channel 1 as a 2.2 kΩ rheostat and reports the
6
+ // quantisation error; demonstrates shutdown/resume and reset.
7
+
8
+ const { I2CConnection } = require('periph/src/connection/i2c');
9
+ const { AD5243Full } = require('periph/src/chips/adc_dac/ad5243');
10
+
11
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
12
+ const VDD = 3.3;
13
+
14
+ (async () => {
15
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
16
+ const connection = new I2CConnection(bus, AD5243Full.I2C_ADDRESS);
17
+ const pot = new AD5243Full(connection); // Create AD5243 Full driver, (connection, rAb=10000 Ω, rWiper=160 Ω)
18
+ await pot.init(); // Prepare driver, () → None
19
+
20
+ // --- Step channel 0 through divider ratios ---
21
+ // With A at VDD and B at GND the wiper voltage is ratio × VDD; the code is
22
+ // round(ratio × 256), so 0.999 lands on the top code 255.
23
+ for (const ratio of [0.0, 0.25, 0.5, 0.75, 0.999]) {
24
+ await pot.setRatio(0, ratio); // Set divider ratio, (channel 0–1, ratio 0.0–1.0) → None
25
+ const code = await pot.getWiper(0); // Read wiper position, (channel 0–1) → int 0–255
26
+ const actual = await pot.getRatio(0); // Read divider ratio, (channel 0–1) → float
27
+ console.log(`ratio ${ratio.toFixed(3)} -> code ${code} (${actual.toFixed(4)}) -> ${(actual * VDD).toFixed(3)} V`);
28
+ await sleep(500);
29
+ }
30
+
31
+ // --- Use channel 1 as a 2.2 kΩ rheostat ---
32
+ // The 256 steps quantise the resistance (~39 Ω per code on a 10 kΩ part), so
33
+ // the readback differs slightly from the request.
34
+ await pot.setResistance(1, 2200.0); // Set W–B resistance, (channel 0–1, ohms Ω) → None
35
+ const r = await pot.getResistance(1); // Read W–B resistance, (channel 0–1) → float Ω
36
+ console.log(`rheostat ${r.toFixed(1)} Ω, error ${(r - 2200.0).toFixed(1)} Ω`);
37
+
38
+ // --- Shut channel 1 down and bring it back ---
39
+ // Shutdown opens A and shorts the wiper to B (about 0 Ω) while the RDAC keeps
40
+ // its value, so resume restores the 2.2 kΩ setting.
41
+ await pot.shutdown(1); // Shut channel down, (channel 0–1) → None
42
+ console.log('shutdown:', pot.isShutdown(1)); // Query shutdown state, (channel 0–1) → bool
43
+ await sleep(1000);
44
+ await pot.resume(1); // Leave shutdown, (channel 0–1) → None
45
+
46
+ // --- Restore the power-up state ---
47
+ // The chip has no reset pin, so reset() rewrites midscale to both channels.
48
+ await pot.reset(); // Restore power-up preset, () → None
49
+ await connection.close();
50
+ })();
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { AD5243Minimal } = require('periph/src/chips/adc_dac/ad5243');
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, AD5243Minimal.I2C_ADDRESS);
11
+ const pot = new AD5243Minimal(connection); // Create AD5243 driver, (connection, rAb=10000 Ω, rWiper=160 Ω)
12
+ await pot.init(); // Prepare driver, () → None
13
+
14
+ for (const code of [0, 64, 128, 192, 255]) {
15
+ await pot.setWiper(0, code); // Set wiper position, (channel 0–1, code 0–255) → None
16
+ console.log(code, await pot.getWiper(0)); // Read wiper position, (channel 0–1) → int 0–255
17
+ console.log(await pot.getResistance(0)); // Read W–B resistance, (channel 0–1) → float Ω
18
+ await sleep(500);
19
+ }
20
+
21
+ await pot.setResistance(1, 2200.0); // Set W–B resistance, (channel 0–1, ohms Ω) → None
22
+ await connection.close();
23
+ })();
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { AD5248Full } = require('../../../src/chips/adc_dac/ad5248');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x2C', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const pot = new AD5248Full(connection, 10000.0, 160.0); // Create AD5248 driver, (connection, rAb=10000 Ω, rWiper=160 Ω) → AD5248Full
11
+
12
+ (async () => {
13
+ // --- Wiper position ---
14
+ await pot.setWiper(0, 100); // Set wiper, (channel 0-1, code 0-255) → void
15
+ // writes [channel << 7, code]; code clamped to 0-255
16
+ console.log(await pot.getWiper(0)); // Read wiper, (channel 0-1) → int 0-255
17
+ // sends the instruction byte alone, then reads one byte
18
+ await pot.setBoth(10, 20); // Set both wipers, (code1 0-255, code2 0-255) → void
19
+ // two transactions, one per channel
20
+
21
+ // --- Resistance ---
22
+ await pot.setResistance(1, 2200.0); // Set W-B resistance, (channel 0-1, ohms) → void
23
+ // nearest code of R = code/256*RAB + 2*RW, clamped
24
+ console.log(await pot.getResistance(1)); // Read W-B resistance, (channel 0-1) → float Ω
25
+ // code/256*RAB + 2*RW
26
+
27
+ // --- Ratio and increment ---
28
+ await pot.setRatio(0, 0.5); // Set wiper ratio, (channel 0-1, ratio 0.0-1.0) → void
29
+ // code = round(ratio*256) clamped to 255; needs terminal A wired
30
+ console.log(await pot.getRatio(0)); // Read wiper ratio, (channel 0-1) → float 0.0-1.0
31
+ // code / 256
32
+ console.log(await pot.increment(0, 5)); // Step wiper, (channel 0-1, steps=1) → int 0-255
33
+ // read-modify-write, clamped to 0-255; negative steps decrement
34
+
35
+ // --- Shutdown ---
36
+ await pot.shutdown(1); // Shut channel down, (channel 0-1) → void
37
+ // opens terminal A, shorts wiper to B, RDAC kept
38
+ console.log(pot.isShutdown(1)); // Query driver-tracked state, (channel 0-1) → bool
39
+ // the chip cannot be read back
40
+ await pot.resume(1); // Leave shutdown, (channel 0-1) → void
41
+ // rewrites the RDAC with SD = 0
42
+
43
+ // --- Model and reset ---
44
+ pot.setRab(10000.0, 160.0); // Set resistance model, (rAb Ω, rWiper Ω) → void
45
+ // no I²C traffic; affects conversions only
46
+ await pot.reset(); // Reset to power-up preset, () → void
47
+ // writes midscale 0x80 to both channels with SD = 0
48
+ await connection.close();
49
+ })();
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { AD5248Full } = require('../../../src/chips/adc_dac/ad5248');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x2C', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const pot = new AD5248Full(connection, 10000.0, 160.0); // Create AD5248 driver, (connection, rAb=10000 Ω, rWiper=160 Ω) → AD5248Full
11
+
12
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
13
+
14
+ (async () => {
15
+ // --- Sweep channel 0 across its full range ---
16
+ // Programmable gain / level trim: step the wiper from code 0 to 255 in steps of 16
17
+ // and print the resulting W-B resistance. Zero scale is not 0 Ω (about 2*RW = 320 Ω).
18
+ for (let code = 0; code <= 255; code += 16) {
19
+ await pot.setWiper(0, code); // Set channel 0 wiper, (channel 0-1, code 0-255) → void
20
+ const ohms = await pot.getResistance(0); // Read W-B resistance, (channel 0-1) → float Ω
21
+ console.log('code=%d R_WB=%s ohm', code, ohms.toFixed(1));
22
+ await sleep(100);
23
+ }
24
+
25
+ // --- Set channel 1 to 2.2 kΩ and report the quantisation error ---
26
+ // 256 positions over 10 kΩ give a step of about 39 Ω, so the result is the nearest code.
27
+ await pot.setResistance(1, 2200.0); // Set channel 1 to 2.2 kΩ, (channel 0-1, ohms) → void
28
+ const actual = await pot.getResistance(1); // Read back W-B resistance, (channel 0-1) → float Ω
29
+ console.log('ch1 target=2200 ohm actual=%s ohm error=%s ohm', actual.toFixed(1), (actual - 2200.0).toFixed(1));
30
+
31
+ // --- Shutdown and resume channel 1 ---
32
+ // Shutdown opens terminal A and shorts the wiper to B (about 0 Ω) without changing the RDAC.
33
+ await pot.shutdown(1); // Shut channel 1 down, (channel 0-1) → void
34
+ console.log('ch1 shutdown=%s code=%d', pot.isShutdown(1), await pot.getWiper(1)); // Query state, (channel 0-1) → bool
35
+ await sleep(1000);
36
+ await pot.resume(1); // Resume channel 1, (channel 0-1) → void
37
+
38
+ // --- Restore the power-up preset ---
39
+ await pot.reset(); // Reset both channels to midscale, () → void
40
+ console.log('after reset: ch0=%d ch1=%d', await pot.getWiper(0), await pot.getWiper(1));
41
+ await connection.close();
42
+ })();
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { AD5248Minimal } = require('../../../src/chips/adc_dac/ad5248');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x2C', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const pot = new AD5248Minimal(connection, 10000.0, 160.0); // Create AD5248 driver, (connection, rAb=10000 Ω, rWiper=160 Ω) → AD5248Minimal
11
+
12
+ (async () => {
13
+ await pot.setWiper(0, 64); // Set channel 0 wiper, (channel 0-1, code 0-255) → void
14
+ await pot.setResistance(1, 2200.0); // Set channel 1 W-B resistance, (channel 0-1, ohms) → void
15
+
16
+ setInterval(async () => {
17
+ const code = await pot.getWiper(0); // Read channel 0 wiper, (channel 0-1) → int 0-255
18
+ const ohms = await pot.getResistance(0); // Read channel 0 W-B resistance, (channel 0-1) → float Ω
19
+ console.log('ch0 code=%d R_WB=%d ohm', code, ohms.toFixed(0));
20
+ }, 1000);
21
+ })();
@@ -0,0 +1,51 @@
1
+ 'use strict';
2
+
3
+ // Exercises every method in the ADS1013 public API: single-shot reads, data
4
+ // rate, continuous conversion, config readback and reset.
5
+
6
+ const { I2CConnection } = require('periph/src/connection/i2c');
7
+ const { ADS1013Full } = require('periph/src/chips/adc_dac/ads1013');
8
+
9
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
10
+
11
+ (async () => {
12
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
13
+ const connection = new I2CConnection(bus, ADS1013Full.I2C_ADDRESS);
14
+ const adc = new ADS1013Full(connection); // Create ADS1013 Full driver, (connection)
15
+ // no I2C traffic; fixed ±2.048 V, 1600 SPS
16
+
17
+ const raw = await adc.readRaw(); // Read raw code, () → number (−2048…2047)
18
+ // single-shot AIN0 − AIN1, polls OS until idle
19
+ const v = await adc.readVoltage(); // Read voltage, () → number V
20
+ // raw × 0.001
21
+ console.log('raw', raw, `voltage ${v.toFixed(4)} V`);
22
+ console.log('full scale', adc.fullScale(), 'V'); // Read full-scale range, () → number V
23
+ // always 2.048
24
+ console.log('lsb', adc.lsb(), 'V'); // Read code size, () → number V
25
+ // always 0.001
26
+
27
+ await adc.setDataRate(ADS1013Full.DR_250); // Set data rate, (dr 0–6) → None
28
+ // DR_250 = 250 SPS
29
+ console.log('dr code', adc.getDataRate()); // Read data-rate code, () → number
30
+ // cached DR[2:0]
31
+ console.log('sps', adc.dataRateSps()); // Read data rate, () → number SPS
32
+ // table lookup of the DR code
33
+
34
+ await adc.startContinuous(); // Start continuous conversion, () → None
35
+ // MODE = 0 at the current data rate
36
+ await sleep(20);
37
+ console.log('continuous raw', await adc.readContinuousRaw()); // Read latest code, () → number
38
+ // reads CONVERSION without touching Config
39
+ console.log('continuous', (await adc.readContinuous()).toFixed(4), 'V'); // Read latest voltage, () → number V
40
+ // raw × lsb()
41
+ await adc.stopContinuous(); // Stop continuous conversion, () → None
42
+ // MODE = 1, back to single-shot
43
+ console.log('converting', await adc.isConverting()); // Check conversion in progress, () → boolean
44
+ // CONFIG.OS = 0 (single-shot only)
45
+ console.log('config 0x' + (await adc.readConfig()).toString(16)); // Read CONFIG register, () → number
46
+ // raw 16-bit register
47
+ await adc.reset(); // Reset to power-up defaults, () → None
48
+ // rewrites CONFIG = 0x8583
49
+
50
+ await connection.close();
51
+ })();
@@ -0,0 +1,51 @@
1
+ 'use strict';
2
+
3
+ // Battery-voltage logger. The cell is read through a 2:1 resistor divider
4
+ // (AIN0, AIN1 to GND) once per second in single-shot mode at 128 SPS,
5
+ // printing raw code, divider voltage and battery voltage plus a running
6
+ // minimum and maximum. The driver then switches to continuous mode at 3300
7
+ // SPS and prints the average of 100 samples.
8
+
9
+ const { I2CConnection } = require('periph/src/connection/i2c');
10
+ const { ADS1013Full } = require('periph/src/chips/adc_dac/ads1013');
11
+
12
+ const DIVIDER_RATIO = 2.0;
13
+ const LOG_SAMPLES = 10;
14
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
15
+
16
+ (async () => {
17
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
18
+ const connection = new I2CConnection(bus, ADS1013Full.I2C_ADDRESS);
19
+ const adc = new ADS1013Full(connection); // Create ADS1013 Full driver, (connection)
20
+
21
+ // --- Slow, low-noise single-shot logging ---
22
+ // 128 SPS gives the lowest noise; one conversion per second keeps the chip
23
+ // in power-down in between.
24
+ await adc.setDataRate(ADS1013Full.DR_128); // Set data rate, (dr 0–6) → None
25
+ let low = Infinity;
26
+ let high = -Infinity;
27
+ for (let i = 0; i < LOG_SAMPLES; i++) {
28
+ const raw = await adc.readRaw(); // Read raw code, () → number (−2048…2047)
29
+ const v = await adc.readVoltage(); // Read voltage, () → number V
30
+ const battery = v * DIVIDER_RATIO;
31
+ low = Math.min(low, battery);
32
+ high = Math.max(high, battery);
33
+ console.log(`raw ${raw} divider ${v.toFixed(3)} V battery ${battery.toFixed(3)} V (min ${low.toFixed(3)}, max ${high.toFixed(3)})`);
34
+ await sleep(1000);
35
+ }
36
+
37
+ // --- Fast continuous averaging ---
38
+ // At 3300 SPS in continuous mode, 100 samples take about 30 ms.
39
+ await adc.setDataRate(ADS1013Full.DR_3300); // Set data rate, (dr 0–6) → None
40
+ await adc.startContinuous(); // Start continuous conversion, () → None
41
+ await sleep(10);
42
+ let total = 0;
43
+ for (let i = 0; i < 100; i++) {
44
+ total += await adc.readContinuous(); // Read latest voltage, () → number V
45
+ await sleep(1);
46
+ }
47
+ await adc.stopContinuous(); // Stop continuous conversion, () → None
48
+ console.log(`average battery ${(total / 100 * DIVIDER_RATIO).toFixed(3)} V`);
49
+
50
+ await connection.close();
51
+ })();
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { ADS1013Minimal } = require('periph/src/chips/adc_dac/ads1013');
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, ADS1013Minimal.I2C_ADDRESS);
11
+ const adc = new ADS1013Minimal(connection); // Create ADS1013 driver, (connection)
12
+
13
+ for (let i = 0; i < 10; i++) {
14
+ const raw = await adc.readRaw(); // Read raw code, () → number (−2048…2047)
15
+ const v = await adc.readVoltage(); // Read voltage, () → number V
16
+ console.log(`${raw} -> ${v.toFixed(3)} V`);
17
+ await sleep(1000);
18
+ }
19
+
20
+ await connection.close();
21
+ })();
@@ -0,0 +1,88 @@
1
+ 'use strict';
2
+
3
+ // Exercises every method in the ADS1014 public API: single-shot reads, PGA
4
+ // gain, data rate, continuous conversion, the comparator and thresholds, and
5
+ // the ALERT/RDY interrupt routing.
6
+
7
+ const { I2CConnection } = require('periph/src/connection/i2c');
8
+ const { ADS1014Full } = require('periph/src/chips/adc_dac/ads1014');
9
+
10
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
11
+
12
+ (async () => {
13
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
14
+ const connection = new I2CConnection(bus, ADS1014Full.I2C_ADDRESS);
15
+ const adc = new ADS1014Full(connection); // Create ADS1014 Full driver, (connection)
16
+ // no I2C traffic; defaults ±2.048 V, 1600 SPS
17
+
18
+ const raw = await adc.readRaw(); // Read raw code, () → number (−2048…2047)
19
+ // single-shot AIN0 − AIN1, polls OS until idle
20
+ const v = await adc.readVoltage(); // Read voltage, () → number V
21
+ // raw × full-scale / 2048
22
+ console.log('raw', raw, `voltage ${v.toFixed(4)} V`);
23
+
24
+ await adc.setGain(ADS1014Full.PGA_1_024); // Set PGA, (pga 0–5) → None
25
+ // ±1.024 V range, 0.5 mV per code
26
+ console.log('gain', adc.getGain()); // Read PGA code, () → number
27
+ // cached PGA[2:0]
28
+ console.log('full scale', adc.fullScale(), 'V'); // Read full-scale range, () → number V
29
+ // ±FSR of the current gain
30
+ console.log('lsb', adc.lsb(), 'V'); // Read code size, () → number V
31
+ // fullScale() / 2048
32
+
33
+ await adc.setDataRate(ADS1014Full.DR_250); // Set data rate, (dr 0–6) → None
34
+ // DR_250 = 250 SPS
35
+ console.log('dr code', adc.getDataRate()); // Read data-rate code, () → number
36
+ // cached DR[2:0]
37
+ console.log('sps', adc.dataRateSps()); // Read data rate, () → number SPS
38
+ // table lookup of the DR code
39
+
40
+ await adc.startContinuous(); // Start continuous conversion, () → None
41
+ // MODE = 0 at the current data rate
42
+ await sleep(20);
43
+ console.log('continuous raw', await adc.readContinuousRaw()); // Read latest code, () → number
44
+ // reads CONVERSION without touching Config
45
+ console.log('continuous', (await adc.readContinuous()).toFixed(4), 'V'); // Read latest voltage, () → number V
46
+ // raw × lsb()
47
+ await adc.stopContinuous(); // Stop continuous conversion, () → None
48
+ // MODE = 1, back to single-shot
49
+ console.log('converting', await adc.isConverting()); // Check conversion in progress, () → boolean
50
+ // CONFIG.OS = 0 (single-shot only)
51
+ console.log('config 0x' + (await adc.readConfig()).toString(16)); // Read CONFIG register, () → number
52
+ // raw 16-bit register
53
+
54
+ await adc.setThresholds(-0.2, 0.5); // Set both thresholds, (low V, high V) → None
55
+ // converted with lsb(), clamped to the code range
56
+ await adc.setLowThresholdRaw(-200); // Set low threshold, (raw −2048…2047) → None
57
+ // LO_THRESH = raw << 4
58
+ await adc.setHighThresholdRaw(500); // Set high threshold, (raw −2048…2047) → None
59
+ // HI_THRESH = raw << 4
60
+ console.log('thresholds', await adc.getThresholdsRaw()); // Read thresholds, () → [number, number]
61
+ // sign-extended 12-bit codes
62
+
63
+ await adc.configureComparator(ADS1014Full.COMP_WINDOW, ADS1014Full.ACTIVE_LOW, true, ADS1014Full.QUEUE_2); // Configure comparator, (mode, polarity, latch, queue) → None
64
+ // window mode, latching, 2 consecutive conversions
65
+ await adc.clearAlert(); // Release latched alert, () → None
66
+ // reads CONVERSION
67
+ await adc.disableComparator(); // Disable comparator, () → None
68
+ // COMP_QUE = 11, ALERT/RDY high-impedance
69
+
70
+ await adc.enableInterrupt(ADS1014Full.SOURCE_CONVERSION_READY); // Route conversion-ready to ALERT/RDY, (source) → None
71
+ // HI_THRESH = 0x8000, LO_THRESH = 0, COMP_QUE = 00
72
+ console.log('status', await adc.pollInterrupt()); // Poll ALERT/RDY, () → number source
73
+ // INT pin level, else CONFIG.OS fallback
74
+ await adc.disableInterrupt(ADS1014Full.SOURCE_CONVERSION_READY); // Release ALERT/RDY, (source) → None
75
+ // COMP_QUE = 11
76
+
77
+ await adc.enableInterrupt(ADS1014Full.SOURCE_COMPARATOR); // Route comparator to ALERT/RDY, (source) → None
78
+ // restores the thresholds set above
79
+ await adc.onInterrupt((status) => console.log('alert source', status)); // Subscribe to ALERT/RDY, (callback) → None
80
+ // pin edge via connection.intPin, else a 5 ms polling loop
81
+ await sleep(1000);
82
+ await adc.offInterrupt(); // Unsubscribe, () → None
83
+ // stops delivery
84
+ await adc.reset(); // Reset to power-up defaults, () → None
85
+ // rewrites CONFIG and both thresholds
86
+
87
+ await connection.close();
88
+ })();
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ // Shunt-current monitor with alert. A 0.1 ohm shunt in the load's supply line
4
+ // is read on the ±0.256 V range at 250 SPS; the current is printed every
5
+ // second, and the traditional comparator (high 0.1 V, low 0.08 V, latching,
6
+ // queue of 4) reports when the load current exceeds 1 A.
7
+
8
+ const { I2CConnection } = require('periph/src/connection/i2c');
9
+ const { ADS1014Full } = require('periph/src/chips/adc_dac/ads1014');
10
+
11
+ const SHUNT_OHMS = 0.1;
12
+ const SAMPLES = 20;
13
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
14
+
15
+ (async () => {
16
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
17
+ const connection = new I2CConnection(bus, ADS1014Full.I2C_ADDRESS);
18
+ const adc = new ADS1014Full(connection); // Create ADS1014 Full driver, (connection)
19
+
20
+ // --- Pick the range and speed for a small shunt voltage ---
21
+ // ±0.256 V gives 0.125 mV per code; 250 SPS keeps noise low and is plenty
22
+ // for a once-per-second report.
23
+ await adc.setGain(ADS1014Full.PGA_0_256); // Set PGA, (pga 0–5) → None
24
+ await adc.setDataRate(ADS1014Full.DR_250); // Set data rate, (dr 0–6) → None
25
+
26
+ // --- Arm the over-current comparator ---
27
+ // 0.1 V across 0.1 ohm is 1 A. Traditional comparator with hysteresis
28
+ // (clears again below 0.08 V), latched so a brief spike is not missed, and
29
+ // 4 consecutive conversions are needed to assert.
30
+ await adc.setThresholds(0.08, 0.1); // Set both thresholds, (low V, high V) → None
31
+ await adc.configureComparator(ADS1014Full.COMP_TRADITIONAL, ADS1014Full.ACTIVE_LOW, true, ADS1014Full.QUEUE_4); // Configure comparator, (mode, polarity, latch, queue) → None
32
+ await adc.enableInterrupt(ADS1014Full.SOURCE_COMPARATOR); // Route comparator to ALERT/RDY, (source) → None
33
+
34
+ // --- Monitor the current ---
35
+ // The pin level is not wired in this demo, so the alert state is inferred
36
+ // from the measured current; clearAlert() releases the latch afterwards.
37
+ for (let i = 0; i < SAMPLES; i++) {
38
+ const v = await adc.readVoltage(); // Read voltage, () → number V
39
+ const amps = v / SHUNT_OHMS;
40
+ console.log(`${v.toFixed(3)} V -> ${amps.toFixed(2)} A`);
41
+ if (amps > 1.0) {
42
+ console.log('over-current!');
43
+ await adc.clearAlert(); // Release latched alert, () → None
44
+ }
45
+ await sleep(1000);
46
+ }
47
+
48
+ // --- Release the alert pin when finished ---
49
+ await adc.disableInterrupt(ADS1014Full.SOURCE_COMPARATOR); // Release ALERT/RDY, (source) → None
50
+
51
+ await connection.close();
52
+ })();
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { ADS1014Minimal } = require('periph/src/chips/adc_dac/ads1014');
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, ADS1014Minimal.I2C_ADDRESS);
11
+ const adc = new ADS1014Minimal(connection); // Create ADS1014 driver, (connection)
12
+
13
+ for (let i = 0; i < 10; i++) {
14
+ const raw = await adc.readRaw(); // Read raw code, () → number (−2048…2047)
15
+ const v = await adc.readVoltage(); // Read voltage, () → number V
16
+ console.log(`${raw} -> ${v.toFixed(3)} V`);
17
+ await sleep(1000);
18
+ }
19
+
20
+ await connection.close();
21
+ })();
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { ADS1113Full } = require('periph/src/chips/adc_dac/ads1113');
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, ADS1113Full.I2C_ADDRESS);
11
+ const adc = new ADS1113Full(connection); // Create ADS1113 Full driver, (connection)
12
+ // no I²C traffic; Config word is rebuilt per conversion
13
+ const raw = await adc.readRaw(); // Read raw code, () → number
14
+ // single-shot conversion, signed 16-bit
15
+ const v = await adc.readVoltage(); // Read voltage, () → number V
16
+ // raw × lsb()
17
+ console.log(`raw ${raw} voltage ${v.toFixed(5)} V`);
18
+ const fs = adc.fullScale(); // Full scale, () → number V
19
+ // fixed ±2.048 V
20
+ const lsb = adc.lsb(); // LSB, () → number V
21
+ // fullScale() / 32768
22
+ console.log(`full scale ${fs} V, lsb ${lsb} V`);
23
+ await adc.setDataRate(ADS1113Full.DR_250); // Set data rate, (dr 0–7) → None
24
+ // DR field of the Config word
25
+ const sps = adc.dataRateSps(); // Data rate, () → number SPS
26
+ // samples per second of the current setting
27
+ console.log(`rate ${adc.getDataRate()} = ${sps} SPS`);
28
+ await adc.startContinuous(); // Start continuous, () → None
29
+ // MODE ← 0
30
+ await sleep(50);
31
+ const craw = await adc.readContinuousRaw(); // Read continuous raw, () → number
32
+ // CONVERSION without touching Config
33
+ const cv = await adc.readContinuous(); // Read continuous, () → number V
34
+ // readContinuousRaw() × lsb()
35
+ console.log(`continuous ${craw} ${cv.toFixed(5)} V`);
36
+ await adc.stopContinuous(); // Stop continuous, () → None
37
+ // MODE ← 1
38
+ const busy = await adc.isConverting(); // Check conversion, () → boolean
39
+ // CONFIG.OS == 0
40
+ const cfg = await adc.readConfig(); // Read CONFIG, () → number
41
+ // raw 16-bit register
42
+ console.log(`converting ${busy}, config 0x${cfg.toString(16)}`);
43
+ await adc.reset(); // Reset, () → None
44
+ // writes the reset values back
45
+
46
+ await connection.close();
47
+ })();
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { ADS1113Full } = require('periph/src/chips/adc_dac/ads1113');
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, ADS1113Full.I2C_ADDRESS);
11
+ const adc = new ADS1113Full(connection); // Create ADS1113 Full driver, (connection)
12
+
13
+ // --- Configure for low-noise slow sampling ---
14
+ // 8 SPS gives the longest integration time per conversion, which
15
+ // suppresses mains and switching noise on a slowly varying voltage.
16
+ await adc.setDataRate(ADS1113Full.DR_8); // Set data rate, (dr 0–7) → None
17
+
18
+ // --- Log single-shot readings ---
19
+ // Each readVoltage() wakes the chip, converts and powers down again.
20
+ for (let i = 0; i < 5; i++) {
21
+ const v = await adc.readVoltage(); // Read voltage, () → number V
22
+ console.log(`${v.toFixed(5)} V`);
23
+ await sleep(1000);
24
+ }
25
+
26
+ // --- Average continuous readings ---
27
+ // Back-to-back conversions averaged over 1.25 s reduce random noise.
28
+ await adc.startContinuous(); // Start continuous, () → None
29
+ await sleep(200);
30
+ let acc = 0;
31
+ for (let i = 0; i < 10; i++) {
32
+ acc += await adc.readContinuous(); // Read continuous, () → number V
33
+ await sleep(125);
34
+ }
35
+ await adc.stopContinuous(); // Stop continuous, () → None
36
+ console.log(`average ${(acc / 10).toFixed(5)} V`);
37
+
38
+ await connection.close();
39
+ })();
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { ADS1113Minimal } = require('periph/src/chips/adc_dac/ads1113');
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, ADS1113Minimal.I2C_ADDRESS);
11
+ const adc = new ADS1113Minimal(connection); // Create ADS1113 driver, (connection)
12
+
13
+ for (let i = 0; i < 10; i++) {
14
+ const v = await adc.readVoltage(); // Read voltage, () → number V
15
+ console.log(`${v.toFixed(5)} V`);
16
+ await sleep(1000);
17
+ }
18
+
19
+ await connection.close();
20
+ })();