periph 1.0.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 (101) hide show
  1. package/examples/accelerometer/.gitkeep +0 -0
  2. package/examples/adc_dac/.gitkeep +0 -0
  3. package/examples/adc_dac/mcp4725/complete.js +34 -0
  4. package/examples/adc_dac/mcp4725/demo.js +43 -0
  5. package/examples/adc_dac/mcp4725/minimal.js +14 -0
  6. package/examples/color/.gitkeep +0 -0
  7. package/examples/comms/.gitkeep +0 -0
  8. package/examples/display/.gitkeep +0 -0
  9. package/examples/environmental/.gitkeep +0 -0
  10. package/examples/gas/.gitkeep +0 -0
  11. package/examples/gnss/.gitkeep +0 -0
  12. package/examples/gpio/.gitkeep +0 -0
  13. package/examples/gyroscope/.gitkeep +0 -0
  14. package/examples/humidity/.gitkeep +0 -0
  15. package/examples/imu/.gitkeep +0 -0
  16. package/examples/io_expander/mcp23017/complete.js +80 -0
  17. package/examples/io_expander/mcp23017/demo.js +59 -0
  18. package/examples/io_expander/mcp23017/minimal.js +16 -0
  19. package/examples/io_expander/pcf8574/complete.js +68 -0
  20. package/examples/io_expander/pcf8574/demo.js +41 -0
  21. package/examples/io_expander/pcf8574/minimal.js +16 -0
  22. package/examples/io_expander/pcf8575/complete.js +31 -0
  23. package/examples/io_expander/pcf8575/demo.js +33 -0
  24. package/examples/io_expander/pcf8575/minimal.js +20 -0
  25. package/examples/led/.gitkeep +0 -0
  26. package/examples/led/sk6812rgbw/complete.js +69 -0
  27. package/examples/led/sk6812rgbw/demo.js +85 -0
  28. package/examples/led/sk6812rgbw/minimal.js +25 -0
  29. package/examples/led/ws2812b/complete.js +64 -0
  30. package/examples/led/ws2812b/demo.js +101 -0
  31. package/examples/led/ws2812b/minimal.js +22 -0
  32. package/examples/light/.gitkeep +0 -0
  33. package/examples/magnetometer/.gitkeep +0 -0
  34. package/examples/magnetometer/as5600/complete.js +43 -0
  35. package/examples/magnetometer/as5600/demo.js +49 -0
  36. package/examples/magnetometer/as5600/minimal.js +16 -0
  37. package/examples/memory/.gitkeep +0 -0
  38. package/examples/motor/.gitkeep +0 -0
  39. package/examples/other/.gitkeep +0 -0
  40. package/examples/power/ina219/complete.js +30 -0
  41. package/examples/power/ina219/demo.js +26 -0
  42. package/examples/power/ina219/minimal.js +17 -0
  43. package/examples/power/ina226/complete.js +33 -0
  44. package/examples/power/ina226/demo.js +30 -0
  45. package/examples/power/ina226/minimal.js +11 -0
  46. package/examples/power/ina3221/complete.js +65 -0
  47. package/examples/power/ina3221/demo.js +56 -0
  48. package/examples/power/ina3221/minimal.js +16 -0
  49. package/examples/pressure/.gitkeep +0 -0
  50. package/examples/pressure/bmp180/complete.js +26 -0
  51. package/examples/pressure/bmp180/demo.js +32 -0
  52. package/examples/pressure/bmp180/minimal.js +17 -0
  53. package/examples/pressure/bmp280/complete.js +28 -0
  54. package/examples/pressure/bmp280/demo.js +34 -0
  55. package/examples/pressure/bmp280/minimal.js +17 -0
  56. package/examples/rfid/.gitkeep +0 -0
  57. package/examples/rtc/.gitkeep +0 -0
  58. package/examples/temperature/.gitkeep +0 -0
  59. package/examples/tof/.gitkeep +0 -0
  60. package/package.json +20 -0
  61. package/src/chips/accelerometer/.gitkeep +0 -0
  62. package/src/chips/adc_dac/.gitkeep +0 -0
  63. package/src/chips/adc_dac/mcp4725.js +161 -0
  64. package/src/chips/color/.gitkeep +0 -0
  65. package/src/chips/comms/.gitkeep +0 -0
  66. package/src/chips/display/.gitkeep +0 -0
  67. package/src/chips/environmental/.gitkeep +0 -0
  68. package/src/chips/gas/.gitkeep +0 -0
  69. package/src/chips/gnss/.gitkeep +0 -0
  70. package/src/chips/gpio/.gitkeep +0 -0
  71. package/src/chips/gyroscope/.gitkeep +0 -0
  72. package/src/chips/humidity/.gitkeep +0 -0
  73. package/src/chips/imu/.gitkeep +0 -0
  74. package/src/chips/io_expander/mcp23017.js +396 -0
  75. package/src/chips/io_expander/pcf8574.js +298 -0
  76. package/src/chips/io_expander/pcf8575.js +295 -0
  77. package/src/chips/led/.gitkeep +0 -0
  78. package/src/chips/led/_color.js +25 -0
  79. package/src/chips/led/sk6812rgbw.js +183 -0
  80. package/src/chips/led/ws2812b.js +171 -0
  81. package/src/chips/light/.gitkeep +0 -0
  82. package/src/chips/magnetometer/as5600.js +293 -0
  83. package/src/chips/memory/.gitkeep +0 -0
  84. package/src/chips/motor/.gitkeep +0 -0
  85. package/src/chips/other/.gitkeep +0 -0
  86. package/src/chips/power/ina219.js +154 -0
  87. package/src/chips/power/ina226.js +208 -0
  88. package/src/chips/power/ina3221.js +341 -0
  89. package/src/chips/pressure/.gitkeep +0 -0
  90. package/src/chips/pressure/bmp180.js +234 -0
  91. package/src/chips/pressure/bmp280.js +328 -0
  92. package/src/chips/rfid/.gitkeep +0 -0
  93. package/src/chips/rtc/.gitkeep +0 -0
  94. package/src/chips/temperature/.gitkeep +0 -0
  95. package/src/chips/tof/.gitkeep +0 -0
  96. package/src/index.js +2 -0
  97. package/src/transport/i2c.js +67 -0
  98. package/src/transport/neopixel.js +37 -0
  99. package/src/transport/smbus.js +117 -0
  100. package/src/transport/spi.js +72 -0
  101. package/src/transport/uart.js +145 -0
@@ -0,0 +1,56 @@
1
+ 'use strict';
2
+
3
+ const { I2CTransport } = require('../../../src/transport/i2c');
4
+ const { INA3221Full } = require('../../../src/chips/power/ina3221');
5
+
6
+ const transport = new I2CTransport(1, 0x40);
7
+ const ina = new INA3221Full(transport); // Create INA3221 driver, (transport, r_shunt=0.1 Ω)
8
+
9
+ // --- Monitor three rails simultaneously ---
10
+ // User wires CH1 to 5V rail, CH2 to 3.3V rail, CH3 to 12V rail.
11
+ // The demo prints a one-line tabular update each second for 30 seconds.
12
+ console.log('V1 I1 P1 V2 I2 P2 V3 I3 P3');
13
+ let t = 0;
14
+ const interval = setInterval(() => {
15
+ const row = [];
16
+ for (const ch of [1, 2, 3]) {
17
+ const v = ina.voltage(ch); // Read bus voltage, (channel) → float V
18
+ const i = ina.current(ch); // Read load current, (channel) → float A
19
+ const p = ina.power(ch); // Read power, (channel) → float W
20
+ row.push(v.toFixed(3), i.toFixed(4), p.toFixed(4));
21
+ }
22
+ console.log(row.join(' '));
23
+
24
+ if (t === 9) {
25
+ // --- Arm critical-alert limits at 1.5x current draw ---
26
+ for (const ch of [1, 2, 3]) {
27
+ const i = ina.current(ch);
28
+ ina.setCriticalAlert(ch, i * 1.5);
29
+ }
30
+ console.log('alerts armed');
31
+ }
32
+
33
+ if (t === 19) {
34
+ // --- Arm shunt-voltage summation across all three channels ---
35
+ ina.setSummationChannels([1, 2, 3], 0.3); // Set summation channels, (channels, limit_v) → None
36
+ // configures SCC bits and sum limit register
37
+ console.log('summation armed');
38
+ }
39
+
40
+ t++;
41
+ if (t >= 30) {
42
+ clearInterval(interval);
43
+
44
+ // --- Dump alert flags and decode any that fired ---
45
+ const flags = ina.alertFlags(); // Read alert flags, () → int
46
+ // reads Mask/Enable register, clears latched flags
47
+ console.log('Mask/Enable: 0x' + flags.toString(16).toUpperCase());
48
+ const alertNames = ['CF1', 'CF2', 'CF3', 'SF', 'WF1', 'WF2', 'WF3', 'PVF', 'TCF', 'CVRF'];
49
+ const alertBits = [0x0200, 0x0100, 0x0080, 0x0040, 0x0020, 0x0010, 0x0008, 0x0004, 0x0002, 0x0001];
50
+ const fired = alertNames.filter((_, k) => flags & alertBits[k]);
51
+ if (fired.length > 0) console.log('Flags fired: ' + fired.join(', '));
52
+ else console.log('No alert flags fired');
53
+
54
+ transport.close();
55
+ }
56
+ }, 1000);
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ const { I2CTransport } = require('../../../src/transport/i2c');
4
+ const { INA3221Minimal } = require('../../../src/chips/power/ina3221');
5
+
6
+ const transport = new I2CTransport(1, 0x40);
7
+ const ina = new INA3221Minimal(transport); // Create INA3221 driver, (transport, r_shunt=0.1 Ω)
8
+
9
+ setInterval(() => {
10
+ for (const ch of [1, 2, 3]) {
11
+ const v = ina.voltage(ch); // Read bus voltage, (channel) → float V
12
+ const i = ina.current(ch); // Read load current, (channel) → float A
13
+ const p = ina.power(ch); // Read power, (channel) → float W
14
+ console.log(`ch${ch}: ${v.toFixed(3)}V ${i.toFixed(4)}A ${p.toFixed(4)}W`);
15
+ }
16
+ }, 1000);
File without changes
@@ -0,0 +1,26 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../packages/periph/src/transport/i2c');
3
+ const { BMP180Full } = require('../../../packages/periph/src/chips/pressure/bmp180');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x77', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+ const bmp = new BMP180Full(transport); // Create BMP180 driver, (transport, oss=0)
10
+ const cid = bmp.chipId(); // Read chip ID, () → int
11
+ // returns 0x55 for BMP180
12
+ console.log('chip_id=' + cid.toString(16));
13
+ const oss = bmp.oversampling(); // Read OSS, () → int 0–3
14
+ console.log('oss=' + oss);
15
+ bmp.setOversampling(BMP180Full.OSS_STANDARD); // Set OSS, (oss 0–3) → None
16
+ // changes conversion time vs resolution trade-off
17
+ const t = bmp.temperature(); // Read temperature, () → float C
18
+ const p = bmp.pressure(); // Read pressure, () → float hPa
19
+ const alt = bmp.altitude(); // Compute altitude, (sea_level_hpa=1013.25) → float m
20
+ // uses barometric formula to convert pressure to metres
21
+ const slp = bmp.seaLevelPressure(alt); // Compute sea-level pressure, (altitude_m) → float hPa
22
+ bmp.reset(); // Soft reset chip, () → None
23
+ // re-reads calibration after reset
24
+ console.log(`T=${t.toFixed(1)} C, P=${p.toFixed(1)} hPa, alt=${alt.toFixed(1)} m, slp=${slp.toFixed(1)} hPa`);
25
+ transport.close();
26
+ console.log('===DONE: 0 passed, 0 failed===');
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../packages/periph/src/transport/i2c');
3
+ const { BMP180Full } = require('../../../packages/periph/src/chips/pressure/bmp180');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x77', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+ const bmp = new BMP180Full(transport, BMP180Full.OSS_ULP); // Create BMP180 driver, (transport, oss=0 ULP)
10
+
11
+ const t0 = bmp.temperature(); // Read temperature, () → float C
12
+ const p0 = bmp.pressure(); // Read pressure, () → float hPa
13
+ const altRef = bmp.altitude(); // Compute altitude, (sea_level_hpa=1013.25) → float m
14
+ console.log('Reference: ' + t0.toFixed(1) + ' C, ' + p0.toFixed(1) + ' hPa, alt=' + altRef.toFixed(1) + ' m');
15
+ let prevAlt = 0.0;
16
+
17
+ for (let n = 0; n < 60; n++) {
18
+ const t = bmp.temperature(); // Read temperature, () → float C
19
+ const p = bmp.pressure(); // Read pressure, () → float hPa
20
+ const a = bmp.altitude(); // Compute altitude, (sea_level_hpa=1013.25) → float m
21
+ const da = (a - prevAlt) * 100;
22
+
23
+ if (n > 0) {
24
+ console.log(n + 's: ' + t.toFixed(1) + ' C, ' + p.toFixed(1) + ' hPa, alt=' + a.toFixed(1) + ' m (delta=' + da.toFixed(0) + ' cm)');
25
+ } else {
26
+ console.log(n + 's: ' + t.toFixed(1) + ' C, ' + p.toFixed(1) + ' hPa, alt=' + a.toFixed(1) + ' m');
27
+ }
28
+ prevAlt = a;
29
+ }
30
+
31
+ transport.close();
32
+ console.log('===DONE: 0 passed, 0 failed===');
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../packages/periph/src/transport/i2c');
3
+ const { BMP180Minimal } = require('../../../packages/periph/src/chips/pressure/bmp180');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x77', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+ const bmp = new BMP180Minimal(transport); // Create BMP180 driver, (transport)
10
+
11
+ for (let i = 0; i < 5; i++) {
12
+ const t = bmp.temperature(); // Read temperature, () → float C
13
+ const p = bmp.pressure(); // Read pressure, () → float hPa
14
+ console.log(`${t.toFixed(1)} C, ${p.toFixed(1)} hPa`);
15
+ }
16
+ transport.close();
17
+ console.log('===DONE: 0 passed, 0 failed===');
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../packages/periph/src/transport/i2c');
3
+ const { BMP280Full } = require('../../../packages/periph/src/chips/pressure/bmp280');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+ const bmp = new BMP280Full(transport); // Create BMP280 driver, (transport, addr=0x76, osrs_t=1, osrs_p=1, mode=1, filter=0, t_sb=0)
10
+
11
+ const cid = bmp.chipId(); // Read chip ID, () → int
12
+ console.log('chip_id=' + cid.toString(16));
13
+ const s = bmp.status(); // Read status register, () → int
14
+ console.log('status=0x' + s.toString(16));
15
+
16
+ bmp.configure(BMP280Full.OSRS_X2, BMP280Full.OSRS_X4, // Configure ADC, (osrs_t 0–5, osrs_p 0–5, mode 0/1/3, filter 0–4, t_sb 0–7) → None
17
+ BMP280Full.MODE_FORCED, BMP280Full.FILTER_4,
18
+ BMP280Full.T_SB_62_5_MS);
19
+ bmp.setOversampling(BMP280Full.OSRS_X1, BMP280Full.OSRS_X1); // Update oversampling, (osrs_t, osrs_p) → None
20
+ bmp.setFilter(BMP280Full.FILTER_OFF); // Update IIR filter, (coeff 0–4) → None
21
+ bmp.setStandby(BMP280Full.T_SB_250_MS); // Update standby time, (t_sb 0–7) → None
22
+ bmp.reset(); // Soft reset and re-init, () → None
23
+
24
+ const t = bmp.temperature(); // Read temperature, () → float C
25
+ const p = bmp.pressure(); // Read pressure, () → float hPa
26
+ console.log(`T=${t.toFixed(1)} C, P=${p.toFixed(1)} hPa`);
27
+ transport.close();
28
+ console.log('===DONE: 0 passed, 0 failed===');
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../packages/periph/src/transport/i2c');
3
+ const { BMP280Full } = require('../../../packages/periph/src/chips/pressure/bmp280');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+ const bmp = new BMP280Full(transport); // Create BMP280 driver, (transport, addr=0x76, osrs_t=1, osrs_p=1, mode=1, filter=0, t_sb=0)
10
+
11
+ const startMs = Date.now();
12
+
13
+ // --- Weather monitoring preset: forced mode, ×1/×1, filter off ---
14
+ // Lowest power: 2.7 µA at 1 Hz. Suitable for continuous weather logging.
15
+ bmp.configure(BMP280Full.OSRS_X1, BMP280Full.OSRS_X1,
16
+ BMP280Full.MODE_FORCED, BMP280Full.FILTER_OFF,
17
+ BMP280Full.T_SB_0_5_MS); // Configure ADC, (osrs_t 0–5, osrs_p 0–5, mode 0/1/3, filter 0–4, t_sb 0–7) → None
18
+
19
+ console.log('WEATHER-MONITORING T[C] P[hPa] ALT[m]');
20
+
21
+ for (let n = 0; n < 30; n++) {
22
+ const t = bmp.temperature(); // Read temperature, () → float C
23
+ const p = bmp.pressure(); // Read pressure, () → float hPa
24
+ const a = bmp.altitude(); // Compute altitude, (sea_level_hpa=1013.25) → float m
25
+ const elapsed = ((Date.now() - startMs) / 1000).toFixed(1);
26
+ console.log(`${elapsed}s ${t.toFixed(2)} ${p.toFixed(2)} ${a.toFixed(2)}`);
27
+ if (n < 29) {
28
+ const waitMs = 1000 - (Date.now() - startMs) % 1000;
29
+ if (waitMs > 0) setTimeout(() => {}, waitMs);
30
+ }
31
+ }
32
+
33
+ transport.close();
34
+ console.log('===DONE: 0 passed, 0 failed===');
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+ const { I2CTransport } = require('../../../packages/periph/src/transport/i2c');
3
+ const { BMP280Minimal } = require('../../../packages/periph/src/chips/pressure/bmp280');
4
+
5
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
6
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
7
+
8
+ const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
9
+ const bmp = new BMP280Minimal(transport); // Create BMP280 driver, (transport, addr=0x76)
10
+
11
+ for (let i = 0; i < 5; i++) {
12
+ const t = bmp.temperature(); // Read temperature, () → float C
13
+ const p = bmp.pressure(); // Read pressure, () → float hPa
14
+ console.log(`${t.toFixed(1)} C, ${p.toFixed(1)} hPa`);
15
+ }
16
+ transport.close();
17
+ console.log('===DONE: 0 passed, 0 failed===');
File without changes
File without changes
File without changes
File without changes
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "periph",
3
+ "version": "1.0.0",
4
+ "description": "JavaScript drivers for peripheral chips (I2C, SPI)",
5
+ "main": "src/index.js",
6
+ "keywords": [
7
+ "i2c",
8
+ "spi",
9
+ "uart",
10
+ "sensor",
11
+ "peripheral",
12
+ "hardware"
13
+ ],
14
+ "license": "MIT",
15
+ "dependencies": {
16
+ "i2c-bus": "^5.2.2",
17
+ "spi-device": "^3.1.0",
18
+ "serialport": "^12.0.0"
19
+ }
20
+ }
File without changes
File without changes
@@ -0,0 +1,161 @@
1
+ 'use strict';
2
+
3
+ const CMD_FAST_WRITE = 0x00;
4
+ const CMD_WRITE_DAC_EEPROM = 0x60;
5
+ const ADDR_GENERAL_CALL = 0x00;
6
+ const GC_RESET = 0x06;
7
+ const GC_WAKE = 0x09;
8
+
9
+ /**
10
+ * MCP4725 single-channel 12-bit voltage-output DAC — minimal interface.
11
+ *
12
+ * Provides simple voltage output as a fraction of V_DD with no configuration
13
+ * beyond the transport. Uses Fast Write (2-byte) for DAC register updates.
14
+ *
15
+ * @param {object} transport - Configured I²C transport pointing at the device (0x60–0x61).
16
+ */
17
+ class MCP4725Minimal {
18
+ /**
19
+ * @param {object} transport - Configured I²C transport pointing at the device.
20
+ */
21
+ constructor(transport) {
22
+ this._transport = transport;
23
+ }
24
+
25
+ /**
26
+ * Set the DAC output as a fraction of V_DD.
27
+ * @param {number} fraction - Output voltage as a fraction of V_DD (0.0–1.0).
28
+ */
29
+ set_voltage(fraction) {
30
+ if (fraction < 0.0) fraction = 0.0;
31
+ if (fraction > 1.0) fraction = 1.0;
32
+ const code = Math.round(fraction * 4095);
33
+ this._fast_write(code, 0);
34
+ }
35
+
36
+ /**
37
+ * Set the raw 12-bit DAC code directly.
38
+ * @param {number} code - Raw 12-bit DAC code (0–4095).
39
+ */
40
+ set_raw(code) {
41
+ if (code > 4095) code = 4095;
42
+ this._fast_write(code, 0);
43
+ }
44
+
45
+ _fast_write(code, pd_mode) {
46
+ const buf = Buffer.alloc(2);
47
+ buf[0] = ((pd_mode & 0x03) << 4) | ((code >> 8) & 0x0F);
48
+ buf[1] = code & 0xFF;
49
+ this._transport.write(buf);
50
+ }
51
+ }
52
+
53
+ /**
54
+ * MCP4725 full interface — extends MCP4725Minimal with EEPROM, power-down, and read-back.
55
+ *
56
+ * Adds write-with-EEPROM persistence, power-down modes, General Call reset/wake,
57
+ * and full register read-back of both DAC and EEPROM contents.
58
+ *
59
+ * @param {object} transport - Configured I²C transport pointing at the device (0x60–0x61).
60
+ */
61
+ class MCP4725Full extends MCP4725Minimal {
62
+ /**
63
+ * @param {object} transport - Configured I²C transport pointing at the device.
64
+ */
65
+ constructor(transport) {
66
+ super(transport);
67
+ }
68
+
69
+ /** @type {number} Normal operation (power-down mode 0). */
70
+ static PD_NORMAL = 0;
71
+ /** @type {number} Power-down with 1 kΩ to GND (mode 1). */
72
+ static PD_1K_GND = 1;
73
+ /** @type {number} Power-down with 100 kΩ to GND (mode 2). */
74
+ static PD_100K_GND = 2;
75
+ /** @type {number} Power-down with 500 kΩ to GND (mode 3). */
76
+ static PD_500K_GND = 3;
77
+
78
+ /**
79
+ * Set the DAC output and persist to EEPROM.
80
+ * @param {number} fraction - Output voltage as a fraction of V_DD (0.0–1.0).
81
+ */
82
+ set_voltage_eeprom(fraction) {
83
+ if (fraction < 0.0) fraction = 0.0;
84
+ if (fraction > 1.0) fraction = 1.0;
85
+ const code = Math.round(fraction * 4095);
86
+ this._write_dac_eeprom(code, 0);
87
+ }
88
+
89
+ /**
90
+ * Set the raw 12-bit DAC code and persist to EEPROM.
91
+ * @param {number} code - Raw 12-bit DAC code (0–4095).
92
+ */
93
+ set_raw_eeprom(code) {
94
+ if (code > 4095) code = 4095;
95
+ this._write_dac_eeprom(code, 0);
96
+ }
97
+
98
+ /**
99
+ * Read the current DAC register and EEPROM contents.
100
+ * @returns {{code: number, voltage_fraction: number, power_down: number, eeprom_code: number, eeprom_power_down: number, eeprom_ready: boolean}} Dictionary with code, voltage_fraction, power_down, eeprom_code, eeprom_power_down, eeprom_ready.
101
+ */
102
+ read() {
103
+ const buf = this._transport.writeRead(Buffer.from([0x00]), 5);
104
+ return {
105
+ code: ((buf[1] & 0x0F) << 8) | buf[2],
106
+ voltage_fraction: (((buf[1] & 0x0F) << 8) | buf[2]) / 4095.0,
107
+ power_down: (buf[0] >> 2) & 0x03,
108
+ eeprom_code: ((buf[3] & 0x0F) << 8) | buf[4],
109
+ eeprom_power_down: (buf[3] >> 6) & 0x03,
110
+ eeprom_ready: !!(buf[0] & 0x80),
111
+ };
112
+ }
113
+
114
+ /**
115
+ * Set the power-down mode and preserve the current DAC code.
116
+ * @param {number} mode - Power-down mode 0–3 (0 = normal, 1 = 1 kΩ to GND, 2 = 100 kΩ to GND, 3 = 500 kΩ to GND).
117
+ */
118
+ set_power_down(mode) {
119
+ if (mode > 3) mode = 3;
120
+ const code = this._read_dac_code();
121
+ this._fast_write(code, mode);
122
+ }
123
+
124
+ /**
125
+ * Send General Call Wake-Up (0x00, 0x09) to clear power-down bits.
126
+ */
127
+ wake_up() {
128
+ this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_WAKE]));
129
+ }
130
+
131
+ /**
132
+ * Send General Call Reset (0x00, 0x06) to trigger internal POR.
133
+ */
134
+ reset() {
135
+ this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_RESET]));
136
+ }
137
+
138
+ /**
139
+ * Check if the EEPROM write operation is complete.
140
+ * @returns {boolean} True when a pending EEPROM write has finished.
141
+ */
142
+ is_eeprom_ready() {
143
+ const buf = this._transport.writeRead(Buffer.from([0x00]), 1);
144
+ return !!(buf[0] & 0x80);
145
+ }
146
+
147
+ _write_dac_eeprom(code, pd_mode) {
148
+ const buf = Buffer.alloc(3);
149
+ buf[0] = CMD_WRITE_DAC_EEPROM | ((pd_mode & 0x03) << 1);
150
+ buf[1] = (code >> 4) & 0xFF;
151
+ buf[2] = (code & 0x0F) << 4;
152
+ this._transport.write(buf);
153
+ }
154
+
155
+ _read_dac_code() {
156
+ const buf = this._transport.writeRead(Buffer.from([0x00]), 2);
157
+ return ((buf[0] & 0x0F) << 8) | buf[1];
158
+ }
159
+ }
160
+
161
+ module.exports = { MCP4725Minimal, MCP4725Full };
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes