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,59 @@
1
+ 'use strict';
2
+
3
+ // Programmable level trimmer: sweeps the wiper in steps of 16 printing the
4
+ // W–B resistance, sets a 22 kΩ rheostat and reports the quantisation error,
5
+ // demonstrates shutdown/resume, midscale and reset, and steps the wiper as a
6
+ // voltage divider (A at VDD, B at GND) printing the expected output voltage
7
+ // at VDD = 3.3 V.
8
+
9
+ const { I2CConnection } = require('periph/src/connection/i2c');
10
+ const { CAT5171Full } = require('periph/src/chips/adc_dac/cat5171');
11
+
12
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
13
+ const VDD = 3.3;
14
+
15
+ (async () => {
16
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
17
+ const connection = new I2CConnection(bus, CAT5171Full.I2C_ADDRESS);
18
+ const pot = new CAT5171Full(connection); // Create CAT5171 Full driver, (connection, rAb=100000 Ω, rWiper=50 Ω)
19
+ await pot.init(); // Prepare driver, () → None
20
+
21
+ // --- Sweep the wiper across the full range ---
22
+ // Zero scale is not 0 Ω (it is the wiper resistance) and code 255 sits one
23
+ // LSB below the RAB end.
24
+ for (let code = 0; code < 256; code += 16) {
25
+ await pot.setWiper(code); // Set wiper position, (code 0–255) → None
26
+ const r = await pot.getResistance(); // Read W–B resistance, () → float Ω
27
+ console.log(`code ${code} -> ${r.toFixed(1)} Ω`);
28
+ await sleep(200);
29
+ }
30
+
31
+ // --- Set a 22 kΩ rheostat ---
32
+ // The 256 steps quantise the resistance (~390 Ω per code on a 100 kΩ part),
33
+ // so the readback differs slightly from the request.
34
+ await pot.setResistance(22000.0); // Set W–B resistance, (ohms Ω) → None
35
+ const rheo = await pot.getResistance(); // Read W–B resistance, () → float Ω
36
+ console.log(`rheostat ${rheo.toFixed(1)} Ω, error ${(rheo - 22000.0).toFixed(1)} Ω`);
37
+
38
+ // --- Shutdown, resume, midscale and reset ---
39
+ // Shutdown opens A and shorts the wiper to B while the WCR keeps its value,
40
+ // so resume restores the setting; midscale and reset both return to 0x80.
41
+ const kept = await pot.getWiper(); // Read wiper position, () → int 0–255
42
+ await pot.shutdown(); // Shut down, () → None
43
+ console.log('shutdown:', pot.isShutdown(), 'kept', (await pot.getWiper()) === kept); // Query shutdown state, () → bool
44
+ await sleep(1000);
45
+ await pot.resume(); // Leave shutdown, () → None
46
+ await pot.midscale(); // Move wiper to midscale, () → None
47
+ await pot.reset(); // Midscale reset, () → None
48
+
49
+ // --- Use the wiper as a voltage divider ---
50
+ // With A at VDD and B at GND the wiper voltage is ratio × VDD; the code is
51
+ // round(ratio × 256), so 0.999 lands on the top code 255.
52
+ for (const ratio of [0.0, 0.25, 0.5, 0.75, 0.999]) {
53
+ await pot.setRatio(ratio); // Set divider ratio, (ratio 0.0–1.0) → None
54
+ const actual = await pot.getRatio(); // Read divider ratio, () → float
55
+ console.log(`ratio ${ratio.toFixed(3)} -> ${actual.toFixed(4)} -> ${(actual * VDD).toFixed(3)} V`);
56
+ await sleep(500);
57
+ }
58
+ await connection.close();
59
+ })();
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('periph/src/connection/i2c');
4
+ const { CAT5171Minimal } = require('periph/src/chips/adc_dac/cat5171');
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, CAT5171Minimal.I2C_ADDRESS);
11
+ const pot = new CAT5171Minimal(connection); // Create CAT5171 driver, (connection, rAb=100000 Ω, rWiper=50 Ω)
12
+ await pot.init(); // Prepare driver, () → None
13
+
14
+ for (const code of [0, 64, 128, 192, 255]) {
15
+ await pot.setWiper(code); // Set wiper position, (code 0–255) → None
16
+ console.log(code, await pot.getWiper()); // Read wiper position, () → int 0–255
17
+ console.log(await pot.getResistance()); // Read W–B resistance, () → float Ω
18
+ await sleep(500);
19
+ }
20
+
21
+ await pot.setResistance(22000.0); // Set W–B resistance, (ohms Ω) → None
22
+ await connection.close();
23
+ })();
@@ -0,0 +1,88 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { CS43L22Full, clockingFor, OUT, FMT, BEEP, SRC, GAIN } = require('../../../src/chips/adc_dac/cs43l22');
5
+
6
+ async function main() {
7
+ const connection = new I2CConnection(1, 0x4A); // Create I2C connection, (bus=1, addr=0x4A)
8
+ const dac = new CS43L22Full(connection); // Create CS43L22 Full driver, (connection, resPin=null)
9
+ await dac.init(); // Reset, verify ID, load defaults, () → Promise<void>
10
+ // reset pulse (if RESET pin), ID check, I2S slave defaults, required init block
11
+
12
+ await dac.start(); // Power up the DAC once MCLK is running, () → Promise<void>
13
+ // writes POWER_CTL_1 = 0x9E
14
+ await dac.setVolume(-30.0); // Set master volume, (db −102–12 dB) → Promise<void>
15
+ // code = round(db * 2) & 0xFF to registers 0x20 / 0x21
16
+ console.log(await dac.getVolume()); // Read back master volume, () → Promise<number> dB
17
+ // signed register 0x20 / 2
18
+ console.log(await dac.readId()); // Read chip ID and revision, () → Promise<{chipId, revision}>
19
+ // register 0x01 split into bits 7:3 and 2:0
20
+ await dac.setVolumeChannels(-20.0, -24.0); // Set per-channel master volume, (leftDb, rightDb) → Promise<void>
21
+ // registers 0x20 and 0x21 independently
22
+ await dac.setHeadphoneVolume(-10.0); // Set headphone volume, (leftDb, rightDb=leftDb) → Promise<void>
23
+ // registers 0x22 / 0x23, below -96 dB mutes
24
+ await dac.setSpeakerVolume(-10.0); // Set speaker volume, (leftDb, rightDb=leftDb) → Promise<void>
25
+ // registers 0x24 / 0x25
26
+ await dac.setPcmVolume(0.0); // Set PCM volume, (leftDb, rightDb=leftDb) → Promise<void>
27
+ // 7-bit signed 0.5 dB, -51.5 to +12 dB
28
+ await dac.mutePcm(false); // Unmute PCM channels, (left, right=left) → Promise<void>
29
+ // bit 7 of registers 0x1A / 0x1B
30
+ await dac.setHeadphoneGain(GAIN.G_0_6047); // Select headphone analog gain, (index 0–7) → Promise<void>
31
+ // HPGAIN bits 7:5 of register 0x0D
32
+ console.log(await dac.headphoneGain()); // Read headphone gain multiplier, () → Promise<number>
33
+ // decodes HPGAIN
34
+ await dac.muteOutputs(false, true); // Mute outputs, (headphone=null, speaker=null) → Promise<void>
35
+ // PLAYBACK_CTL_2 mute bits
36
+ await dac.setOutputs(OUT.ON, OUT.OFF); // Select amplifier power, (headphone, speaker) → Promise<void>
37
+ // POWER_CTL_2 two bits per channel
38
+ await dac.setSpeakerMode(false, false); // Configure the speaker channels, (mono, swap, mute5050=false) → Promise<void>
39
+ // SPKMONO / SPKSWAP / MUTE50/50
40
+ await dac.setClocking(clockingFor(12288000, 48000)); // Configure clocking from the datasheet table, ({auto, speed, group32k, videoClk, ratio, mclkDiv2}) → Promise<void>
41
+ // CLOCKING_CTL register 0x05
42
+ await dac.setInterface({ fmt: FMT.I2S }); // Configure the serial port, ({master=false, fmt=FMT.I2S, wordLength=0, dsp=false, invertSclk=false, sclkEqMclk=false, invertSwitch=false}) → Promise<void>
43
+ // registers 0x06 / 0x07
44
+ await dac.setTone(3.0, 3.0); // Configure treble / bass shelves, (trebleDb, bassDb, trebleCorner=0, bassCorner=0, enabled=true) → Promise<void>
45
+ // 1.5 dB per step, TCEN enables
46
+ await dac.setBeep(0xC, 2, 3, -6.0, BEEP.SINGLE); // Play a beep, (frequency 0–15, onTime 0–15, offTime 0–7, volumeDb, mode, mix=true) → Promise<void>
47
+ // timing first, mode last
48
+ await dac.stopBeep(); // Stop the beep generator, () → Promise<void>
49
+ // BEEP[1:0] = 0
50
+ await dac.setLimiter(true, { maxDb: -6, cushionDb: -12 }); // Configure the peak limiter, (enabled, {maxDb=0, cushionDb=0, release=0x3F, attack=0, limitAll=true, softRampDisable=false, zeroCrossDisable=false}) → Promise<void>
51
+ // registers 0x27 - 0x29
52
+ await dac.setSoftRamp(true); // Set soft ramp, (digital, analogLeft=false, analogRight=false) → Promise<void>
53
+ // DIGSFT and ANLGSFTx
54
+ await dac.setZeroCross(false); // Set zero-cross, (digital, analogLeft=false, analogRight=false) → Promise<void>
55
+ // DIGZC and ANLGZCx
56
+ await dac.setDeemphasis(false); // Set de-emphasis, (enabled) → Promise<void>
57
+ // DEEMPH bit of MISC_CTL
58
+ await dac.setPcmSwap(SRC.NORMAL, SRC.NORMAL); // Select PCM channel sources, (leftSource, rightSource) → Promise<void>
59
+ // PCM_MIXER_SWAP
60
+ await dac.invertPcm(false, false); // Invert PCM polarity, (left, right) → Promise<void>
61
+ // INV_PCMx
62
+ await dac.setPassthrough(false); // Route analog inputs to the outputs, (enabled, leftInput=1, rightInput=1) → Promise<void>
63
+ // PASSTHRUx and PASSxSEL
64
+ await dac.setPassthroughVolume(0.0); // Set passthrough volume, (leftDb, rightDb=leftDb) → Promise<void>
65
+ // -60 to +12 dB
66
+ await dac.mutePassthrough(true); // Mute passthrough, (left, right=left) → Promise<void>
67
+ // PASSxMUTE
68
+ await dac.freeze(false); // Release the register freeze, (on) → Promise<void>
69
+ // FREEZE bit of MISC_CTL
70
+ await dac.setBatteryCompensation(false); // Configure battery compensation, (enabled, vpRefIndex=0, monitor=false) → Promise<void>
71
+ // BATTERY_COMP register
72
+ console.log(await dac.readVpVoltage(1.8)); // Read the VP supply voltage, (vaVolts) → Promise<number> V
73
+ // VP_LEVEL * VA / 63.3
74
+ console.log(await dac.readStatus()); // Read status flags (clears them), () → Promise<object>
75
+ // STATUS register is read-clear
76
+ console.log(await dac.readSpeakerStatus()); // Read speaker status, () → Promise<object>
77
+ // short flags and SPKR/HP pin
78
+ await dac.setChargePumpFrequency(5); // Set charge-pump frequency divider, (n 0–15) → Promise<void>
79
+ // f = 64 * Fs / (n + 2)
80
+ console.log(await dac.readRegister(0x02)); // Read a raw register, (reg) → Promise<number>
81
+ // documented registers only
82
+ await dac.writeRegister(0x34, 0x5F); // Write a raw register, (reg, value) → Promise<void>
83
+ // reserved and read-only registers are rejected
84
+ await dac.stop(); // Power down the DAC, () → Promise<void>
85
+ // mute, disable ramps, POWER_CTL_1 = 0x9F, wait 100 us
86
+ }
87
+
88
+ main();
@@ -0,0 +1,56 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { CS43L22Full, BEEP } = require('../../../src/chips/adc_dac/cs43l22');
5
+
6
+ async function main() {
7
+ const connection = new I2CConnection(1, 0x4A); // Create I2C connection, (bus=1, addr=0x4A)
8
+
9
+ // --- Bring up the headphone amplifier ---
10
+ // The control port only configures the codec; MCLK = 12.288 MHz and the 48 kHz
11
+ // I2S stream come from the board's I2S master. init() loads the I2S-slave
12
+ // defaults while the chip is powered down; start() powers it up once MCLK runs.
13
+ const dac = new CS43L22Full(connection); // Create CS43L22 Full driver, (connection, resPin=null)
14
+ await dac.init(); // Reset, verify ID, load defaults, () → Promise<void>
15
+ console.log(await dac.readId()); // Read chip ID and revision, () → Promise<{chipId, revision}>
16
+ await dac.setVolume(-60.0); // Set master volume, (db −102–12 dB) → Promise<void>
17
+ await dac.start(); // Power up the DAC once MCLK is running, () → Promise<void>
18
+
19
+ // --- Wait for the serial port to lock ---
20
+ // SPCLKERR reads 1 until LRCK is valid and the MCLK/LRCK ratio is measured;
21
+ // reading the status register clears the flags.
22
+ for (let i = 0; i < 50; i++) {
23
+ const status = await dac.readStatus(); // Read status flags (clears them), () → Promise<object>
24
+ if (!status.clockError) break;
25
+ await new Promise((resolve) => setTimeout(resolve, 100));
26
+ }
27
+
28
+ // --- Ramp the volume up ---
29
+ // 2 dB steps every 100 ms avoid an abrupt start; the digital soft ramp
30
+ // smooths each step further.
31
+ for (let vol = -60; vol <= 0; vol += 2) {
32
+ await dac.setVolume(vol); // Set master volume, (db −102–12 dB) → Promise<void>
33
+ await new Promise((resolve) => setTimeout(resolve, 100));
34
+ }
35
+
36
+ // --- Shape the tone and protect the headphones ---
37
+ // +6 dB bass and treble shelves add presence; the limiter at -6 dB stops
38
+ // loud peaks from clipping the amplifier.
39
+ await dac.setTone(6.0, 6.0); // Configure treble / bass shelves, (trebleDb, bassDb, trebleCorner=0, bassCorner=0, enabled=true) → Promise<void>
40
+ await dac.setLimiter(true, { maxDb: -6, cushionDb: -12 }); // Configure the peak limiter, (enabled, {maxDb=0, cushionDb=0, release=0x3F, attack=0, limitAll=true, softRampDisable=false, zeroCrossDisable=false}) → Promise<void>
41
+
42
+ // --- Play a single beep ---
43
+ // The beep generator needs no audio data; it mixes into the output once MCLK
44
+ // and LRCK are running. Frequency code 0x1 is 521.74 Hz.
45
+ await dac.setBeep(0x1, 2, 0, -6.0, BEEP.SINGLE); // Play a beep, (frequency 0–15, onTime 0–15, offTime 0–7, volumeDb, mode, mix=true) → Promise<void>
46
+ await new Promise((resolve) => setTimeout(resolve, 1500));
47
+ await dac.stopBeep(); // Stop the beep generator, () → Promise<void>
48
+
49
+ // --- Power down safely ---
50
+ // stop() mutes, disables ramps and waits 100 us after POWER_CTL_1 = 0x9F;
51
+ // only then may the application remove MCLK.
52
+ await dac.stop(); // Power down the DAC, () → Promise<void>
53
+ console.log('MCLK may now be removed');
54
+ }
55
+
56
+ main();
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { CS43L22Minimal } = require('../../../src/chips/adc_dac/cs43l22');
5
+
6
+ async function main() {
7
+ const connection = new I2CConnection(1, 0x4A); // Create I2C connection, (bus=1, addr=0x4A)
8
+ const dac = new CS43L22Minimal(connection); // Create CS43L22 driver, (connection, resPin=null)
9
+ await dac.init(); // Reset, verify ID, load defaults, () → Promise<void>
10
+ await dac.start(); // Power up the DAC once MCLK is running, () → Promise<void>
11
+ await dac.setVolume(-20.0); // Set master volume, (db −102–12 dB, 0.5 dB steps) → Promise<void>
12
+ await dac.mute(false); // Unmute the headphone outputs, (on=true) → Promise<void>
13
+ for (let i = 0; i < 5; i++) {
14
+ console.log(await dac.getVolume()); // Read back master volume, () → Promise<number> dB
15
+ await new Promise((resolve) => setTimeout(resolve, 1000));
16
+ }
17
+ await dac.stop(); // Power down the DAC, () → Promise<void>
18
+ }
19
+
20
+ main();
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ // Complete example: exercises every method of DS1881Full.
4
+
5
+ const { I2CConnection } = require('periph/src/connection/i2c');
6
+ const { DS1881Full } = require('periph/src/chips/adc_dac/ds1881');
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, DS1881Full.I2C_ADDRESS);
13
+ const amp = new DS1881Full(connection); // Create DS1881 Full driver, (connection)
14
+ await amp.init(); // Apply defaults, () → None
15
+ // waits 1 ms, rewrites the config only if it differs
16
+
17
+ await amp.setAttenuation(0, 12); // Set attenuation, (channel 0–1, db dB) → None
18
+ // nearest position, one command byte
19
+ console.log(await amp.getAttenuation(0)); // Read attenuation, (channel 0–1) → float dB
20
+ // three-byte round-robin read, position in dB
21
+ await amp.setPosition(1, 30); // Set raw wiper position, (channel 0–1, position 0–63) → None
22
+ // position 63 is mute in Option 1
23
+ console.log(await amp.getPosition(1)); // Read wiper position, (channel 0–1) → int
24
+ // low six bits of the read-back byte
25
+ await amp.mute(1); // Mute channel, (channel 0–1) → None
26
+ // writes the mute position (63)
27
+
28
+ await amp.setBoth(5, 25); // Set both wipers, (position0 0–63, position1 0–63) → None
29
+ // one transaction, both wipers update together
30
+ await amp.setAttenuationBoth(20); // Set both attenuations, (db0 dB, db1=db0 dB) → None
31
+ // db1 defaults to db0 (stereo volume)
32
+ console.log(await amp.readAll()); // Read everything, () → [position0, position1, config]
33
+ // one three-byte round-robin read
34
+
35
+ await amp.configure(2, true, false); // Configure, (option=1 1–2, zeroCrossing=true, nonvolatile=false) → None
36
+ // writes the config byte only if it changed
37
+ console.log(await amp.getConfig()); // Read configuration, () → {option, zeroCrossing, nonvolatile, raw}
38
+ // fresh read; refreshes the driver's table
39
+ console.log(amp.positionToDb(13), amp.maxPosition()); // Table lookup, (position) → float dB; Last non-mute position, () → int
40
+ // Option 2: 13 → 14 dB, max position 32
41
+ console.log(amp.dbToPosition(39)); // Nearest position, (db dB) → int
42
+ // 70 dB and above map to mute
43
+ await amp.setOption(1); // Select option, (option 1–2) → None
44
+ // positions are not converted
45
+ await amp.setZeroCrossing(false); // Set zero-crossing, (enabled bool) → None
46
+ // ZC bit of the configuration
47
+ await amp.setZeroCrossing(true); // Set zero-crossing, (enabled bool) → None
48
+ // clickless updates again
49
+ await amp.waitReady(0.05); // Acknowledge-poll, (timeoutS=0.05 s) → None
50
+ // resolves once the chip ACKs its address
51
+
52
+ await amp.setNonvolatile(true); // Select EEPROM storage, (enabled bool) → None
53
+ // V/NV bit; every wiper write now wears the EEPROM (50 000 writes)
54
+ await amp.store(); // Store positions, () → None
55
+ // rewrites both wipers and polls for completion
56
+ await amp.setNonvolatile(false); // Select EEPROM storage, (enabled bool) → None
57
+ // back to volatile wipers
58
+ await sleep(1000);
59
+ await amp.reset(); // Reset, () → None
60
+ // volatile, zero-crossing, Option 1, both wipers muted
61
+ await connection.close();
62
+ })();
@@ -0,0 +1,61 @@
1
+ 'use strict';
2
+
3
+ // Demo: stereo volume control with mute and optional EEPROM store.
4
+
5
+ const { I2CConnection } = require('periph/src/connection/i2c');
6
+ const { DS1881Full } = require('periph/src/chips/adc_dac/ds1881');
7
+
8
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
9
+ const STORE = process.argv.includes('--store');
10
+
11
+ (async () => {
12
+ const bus = parseInt(process.env.I2C_BUS || '1', 10);
13
+ const connection = new I2CConnection(bus, DS1881Full.I2C_ADDRESS);
14
+ const amp = new DS1881Full(connection); // Create DS1881 Full driver, (connection)
15
+ await amp.init(); // Apply defaults, () → None
16
+
17
+ // --- Fade the volume down and back up ---
18
+ // Zero-crossing detection (on by default) moves the wiper only at a signal
19
+ // zero crossing, so each 2 dB step is clickless; 100 ms per step is longer
20
+ // than the 50 ms maximum zero-crossing window.
21
+ const steps = [];
22
+ for (let db = 0; db <= 40; db += 2) steps.push(db);
23
+ for (let db = 38; db >= 0; db -= 2) steps.push(db);
24
+ for (const db of steps) {
25
+ await amp.setAttenuationBoth(db); // Set both attenuations, (db0 dB, db1=db0 dB) → None
26
+ const [p0, p1] = await amp.readAll(); // Read everything, () → [position0, position1, config]
27
+ console.log(`${db} dB -> positions ${p0} ${p1}`);
28
+ await sleep(100);
29
+ }
30
+
31
+ // --- Mute and unmute one channel ---
32
+ // Muting channel 0 leaves channel 1 playing; restoring writes the position again.
33
+ await amp.mute(0); // Mute channel, (channel 0–1) → None
34
+ console.log('channel 0 muted:', await amp.getAttenuation(0)); // Read attenuation, (channel 0–1) → float dB
35
+ await sleep(1000);
36
+ await amp.setAttenuation(0, 0); // Set attenuation, (channel 0–1, db dB) → None
37
+
38
+ // --- Inspect the Option 2 attenuation table ---
39
+ // Option 2 (DS1808 compatible) uses 1, 2 and 3 dB steps; positions keep their
40
+ // numeric value when the option changes, so re-set them afterwards.
41
+ await amp.setOption(2); // Select option, (option 1–2) → None
42
+ for (const position of [0, 12, 13, 24, 25, 32, 33]) {
43
+ console.log('position', position, '->', amp.positionToDb(position), 'dB'); // Table lookup, (position) → float dB
44
+ }
45
+ await amp.setOption(1); // Select option, (option 1–2) → None
46
+ await amp.setAttenuationBoth(10); // Set both attenuations, (db0 dB, db1=db0 dB) → None
47
+ console.log(await amp.getConfig()); // Read configuration, () → {option, zeroCrossing, nonvolatile, raw}
48
+
49
+ // --- Optionally persist the volume ---
50
+ // Stored positions are restored at power-up. The EEPROM is rated for only
51
+ // 50 000 writes, so this runs only when requested with --store.
52
+ if (STORE) {
53
+ console.log('WARNING: EEPROM endurance is 50 000 writes');
54
+ await amp.setNonvolatile(true); // Select EEPROM storage, (enabled bool) → None
55
+ const start = Date.now();
56
+ await amp.store(); // Store positions, () → None
57
+ console.log('store took', Date.now() - start, 'ms');
58
+ await amp.setNonvolatile(false); // Select EEPROM storage, (enabled bool) → None
59
+ }
60
+ await connection.close();
61
+ })();
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ // Minimal example: set the volume of two audio channels with DS1881Minimal.
4
+
5
+ const { I2CConnection } = require('periph/src/connection/i2c');
6
+ const { DS1881Minimal } = require('periph/src/chips/adc_dac/ds1881');
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, DS1881Minimal.I2C_ADDRESS);
13
+ const amp = new DS1881Minimal(connection); // Create DS1881 driver, (connection)
14
+ await amp.init(); // Apply defaults, () → None
15
+
16
+ for (const db of [0, 10, 20, 40]) {
17
+ await amp.setAttenuation(0, db); // Set attenuation, (channel 0–1, db dB) → None
18
+ await amp.setAttenuation(1, db); // Set attenuation, (channel 0–1, db dB) → None
19
+ console.log(db, await amp.getAttenuation(0)); // Read attenuation, (channel 0–1) → float dB
20
+ await sleep(500);
21
+ }
22
+
23
+ await amp.mute(0); // Mute channel, (channel 0–1) → None
24
+ await connection.close();
25
+ })();
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ // Complete example: exercise every method in the public API.
4
+ // Sets the wiper by code, voltage and ratio, reads it back, steps with
5
+ // increment, inspects the MODE bit, stores the power-up position in the
6
+ // EEPROM and changes the endpoint voltages.
7
+
8
+ const { I2CConnection } = require('periph/src/connection/i2c');
9
+ const { DS3502Full } = require('periph/src/chips/adc_dac/ds3502');
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, DS3502Full.I2C_ADDRESS);
16
+ const pot = new DS3502Full(connection, 15.0, 0.0); // Create DS3502 Full driver, (connection, vHigh=1.0 V, vLow=0.0 V)
17
+ await pot.init(); // Run initialization sequence, () → void (CR = 0x80, caches WR)
18
+
19
+ await pot.setWiper(100); // Set wiper position, (code 0–127) → void
20
+ // writes WR only; the EEPROM is not touched
21
+ console.log(await pot.getWiper()); // Read wiper position, () → int 0–127
22
+ // reads WR; always the live wiper, never the IVR
23
+ await pot.setVoltage(6.0); // Set RW output voltage, (volts V) → void
24
+ // nearest code of V = V_RL + (V_RH − V_RL)·code/127
25
+ console.log(await pot.getVoltage()); // Read unloaded RW voltage, () → float V
26
+ await pot.setRatio(0.25); // Set divider ratio, (ratio 0.0–1.0) → void
27
+ // code = round(ratio × 127)
28
+ console.log(await pot.getRatio()); // Read divider ratio, () → float
29
+ console.log(await pot.getResistance('L')); // Read wiper-to-RL resistance, (terminal='L') → float Ω
30
+ // nominal, RTOTAL = 10 kΩ ±20 %
31
+ console.log(await pot.getResistance('H')); // Read wiper-to-RH resistance, (terminal='L') → float Ω
32
+ console.log(await pot.increment(5)); // Step the wiper, (steps=1) → int 0–127
33
+ // read, add, clamp, write; returns the new code
34
+ console.log(await pot.getMode()); // Read MODE, () → bool
35
+ // true if writes to 0x00 also program the EEPROM
36
+ await pot.setMode(false); // Select SRAM-only writes, (eeprom bool) → void
37
+ // CR = 0x80; true would write CR = 0x00
38
+ await pot.storeWiper(64); // Store power-up position, (code) → void
39
+ // MODE = 0, write WR+IVR, wait for the EEPROM, MODE = 1
40
+ await pot.waitReady(); // Acknowledge polling, (timeoutMs=30) → void
41
+ // throws if the chip keeps NACKing
42
+ pot.setEndpoints(12.0, 2.0); // Change RH / RL voltages, (vHigh V, vLow=0.0 V) → void
43
+ // affects the conversions only; no I²C traffic
44
+
45
+ await connection.close();
46
+ })();
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ // Demo: a VCOM-style voltage setter. With RH = 15.0 V and RL = 0 V (V+ = 15.5 V)
4
+ // prints the wiper loaded from the IVR at power-up, sets 6.0 V, sweeps the
5
+ // wiper in 16 steps, steps up and down, shows the MODE bit and finally
6
+ // persists the chosen code in the EEPROM (consuming one EEPROM write).
7
+
8
+ const { I2CConnection } = require('periph/src/connection/i2c');
9
+ const { DS3502Full } = require('periph/src/chips/adc_dac/ds3502');
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, DS3502Full.I2C_ADDRESS);
16
+ const pot = new DS3502Full(connection, 15.0, 0.0); // Create DS3502 Full driver, (connection, vHigh=1.0 V, vLow=0.0 V)
17
+ await pot.init(); // Run initialization sequence, () → void (CR = 0x80, caches WR)
18
+
19
+ // --- Power-up position ---
20
+ // init leaves the wiper where the IVR put it (factory default 0x40).
21
+ console.log('power-up wiper', await pot.getWiper(), '->', await pot.getVoltage(), 'V');// Read wiper position, () → int 0–127
22
+
23
+ // --- Set 6.0 V ---
24
+ await pot.setVoltage(6.0); // Set RW output voltage, (volts V) → void
25
+ console.log('code', await pot.getWiper(), 'ratio', (await pot.getRatio()).toFixed(3), 'voltage', (await pot.getVoltage()).toFixed(3), 'V');// Read divider ratio, () → float
26
+
27
+ // --- Sweep in 16 steps ---
28
+ // The step size is (V_RH − V_RL)/127, about 118 mV at a 15 V span.
29
+ for (let code = 0; code <= 127; code += 8) {
30
+ await pot.setWiper(code); // Set wiper position, (code 0–127) → void
31
+ console.log('code', code, '->', (await pot.getVoltage()).toFixed(3), 'V');// Read unloaded RW voltage, () → float V
32
+ await sleep(200);
33
+ }
34
+
35
+ // --- Step up and down ---
36
+ await pot.setVoltage(6.0);
37
+ console.log('+4 ->', await pot.increment(4)); // Step the wiper, (steps=1) → int 0–127
38
+ console.log('-4 ->', await pot.increment(-4));
39
+
40
+ // --- MODE ---
41
+ // MODE = 1 (the working mode) keeps wiper writes in SRAM; storeWiper switches
42
+ // to MODE = 0 only for the duration of the EEPROM write.
43
+ console.log('eeprom mode:', await pot.getMode()); // Read MODE, () → bool
44
+ await pot.setMode(false); // Select SRAM-only writes, (eeprom bool) → void
45
+
46
+ // --- Persist the chosen code ---
47
+ // This consumes one of the EEPROM's limited write cycles.
48
+ const code = await pot.getWiper();
49
+ await pot.storeWiper(code); // Store power-up position, (code) → void
50
+ await pot.waitReady(); // Acknowledge polling, (timeoutMs=30) → void
51
+ console.log('read back', await pot.getWiper());
52
+
53
+ await connection.close();
54
+ })();
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ // Minimal example: set and read back a high-voltage digital potentiometer.
4
+ // Constructs the driver with RH = 15 V, RL = 0 V and steps the wiper through
5
+ // a few positions, printing the unloaded output voltage.
6
+
7
+ const { I2CConnection } = require('periph/src/connection/i2c');
8
+ const { DS3502Minimal } = require('periph/src/chips/adc_dac/ds3502');
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, DS3502Minimal.I2C_ADDRESS);
15
+ const pot = new DS3502Minimal(connection, 15.0, 0.0); // Create DS3502 driver, (connection, vHigh=1.0 V, vLow=0.0 V)
16
+ await pot.init(); // Run initialization sequence, () → void (CR = 0x80, caches WR)
17
+
18
+ for (const code of [0, 32, 64, 96, 127]) {
19
+ await pot.setWiper(code); // Set wiper position, (code 0–127) → void
20
+ console.log(code, await pot.getWiper()); // Read wiper position, () → int 0–127
21
+ console.log(await pot.getVoltage()); // Read unloaded RW voltage, () → float V
22
+ await sleep(500);
23
+ }
24
+
25
+ await pot.setVoltage(6.0); // Set RW output voltage, (volts V) → void
26
+
27
+ await connection.close();
28
+ })();
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ // Complete example: exercise every method in the public API.
4
+
5
+ const { I2CConnection } = require('periph/src/connection/i2c');
6
+ const { MCP3422Full } = require('periph/src/chips/adc_dac/mcp3422');
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, MCP3422Full.I2C_ADDRESS);
13
+ const adc = new MCP3422Full(connection); // Create MCP3422 Full driver, (connection)
14
+ await adc.init(); // Write the default configuration, () → void (one-shot, 16 bit, x1, RDY = 0)
15
+
16
+ console.log(await adc.readRaw(0)); // One-shot code, (channel 0–1) → int
17
+ // writes RDY = 1, O/C = 0, waits, polls RDY
18
+ console.log(await adc.readVoltage(1)); // One-shot voltage, (channel 0–1) → float V
19
+ console.log(await adc.readAll()); // Both channels, () → [float V, float V]
20
+ console.log(await adc.readScan([0, 1, 0])); // Scan a list of channels, (channels) → [float V, ...]
21
+
22
+ await adc.setResolution(18); // Select 18 bit / 3.75 SPS, (bits 12|14|16|18) → void
23
+ // throws RangeError for other values
24
+ await adc.setGain(8); // Select PGA x8, (gain 1|2|4|8) → void
25
+ console.log(adc.getResolution(), adc.getGain()); // Read back the selection, () → int, int
26
+ console.log(adc.lsb(), adc.fullScale()); // LSB and full scale, () → float V, float V
27
+ // 2·2.048/2^bits and 2.048/gain
28
+
29
+ await adc.setResolution(12); // Back to 12 bit / 240 SPS, (bits) → void
30
+ await adc.setGain(1); // Back to x1, (gain) → void
31
+ await adc.setChannel(0); // Channel for continuous conversion, (channel 0–1) → void
32
+ await adc.startContinuous(); // Start continuous conversion, (channel) → void
33
+ // O/C = 1; the first sample after a change is discarded
34
+ console.log(adc.isContinuous()); // Report the mode, () → bool
35
+ console.log(await adc.readContinuousRaw()); // Next fresh code, () → int
36
+ console.log(await adc.readContinuous()); // Next fresh voltage, () → float V
37
+ console.log(await adc.readConfig(), await adc.isReady()); // Config byte and RDY, () → int, bool
38
+ await adc.stopContinuous(); // Back to one-shot / standby, () → void
39
+
40
+ await adc.generalCallConvert(); // Start a conversion on every device, () → void
41
+ // sends 0x00 0x08
42
+ await sleep(100);
43
+ console.log(await adc.readPendingRaw()); // Result of that conversion, () → int
44
+ await adc.generalCallReset(); // Reset to power-on defaults, () → void
45
+ // sends 0x00 0x06; the driver state follows
46
+
47
+ await connection.close();
48
+ })();