periph 1.0.1 → 1.2.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.
- package/README.md +54 -12
- package/examples/accelerometer/adxl345/complete.js +52 -0
- package/examples/accelerometer/adxl345/demo.js +34 -0
- package/examples/accelerometer/adxl345/minimal.js +18 -0
- package/examples/accelerometer/adxl362/complete.js +57 -0
- package/examples/accelerometer/adxl362/demo.js +51 -0
- package/examples/accelerometer/adxl362/minimal.js +21 -0
- package/examples/adc_dac/ad7705/complete.js +32 -0
- package/examples/adc_dac/ad7705/demo.js +42 -0
- package/examples/adc_dac/ad7705/minimal.js +16 -0
- package/examples/adc_dac/ad7706/complete.js +35 -0
- package/examples/adc_dac/ad7706/demo.js +44 -0
- package/examples/adc_dac/ad7706/minimal.js +16 -0
- package/examples/adc_dac/hx710a/complete.js +45 -0
- package/examples/adc_dac/hx710a/demo.js +52 -0
- package/examples/adc_dac/hx710a/minimal.js +16 -0
- package/examples/adc_dac/hx710b/complete.js +45 -0
- package/examples/adc_dac/hx710b/demo.js +64 -0
- package/examples/adc_dac/hx710b/minimal.js +16 -0
- package/examples/adc_dac/hx711/complete.js +42 -0
- package/examples/adc_dac/hx711/demo.js +35 -0
- package/examples/adc_dac/hx711/minimal.js +16 -0
- package/examples/adc_dac/mcp4725/complete.js +19 -17
- package/examples/adc_dac/mcp4725/demo.js +5 -5
- package/examples/adc_dac/mcp4725/minimal.js +8 -6
- package/examples/adc_dac/mcp4728/complete.js +43 -0
- package/examples/adc_dac/mcp4728/demo.js +41 -0
- package/examples/adc_dac/mcp4728/minimal.js +13 -0
- package/examples/adc_dac/pcf8591/complete.js +41 -0
- package/examples/adc_dac/pcf8591/demo.js +39 -0
- package/examples/adc_dac/pcf8591/minimal.js +19 -0
- package/examples/comms/mcp2515/complete.js +75 -0
- package/examples/comms/mcp2515/demo.js +66 -0
- package/examples/comms/mcp2515/minimal.js +27 -0
- package/examples/comms/rda5807m/complete.js +55 -0
- package/examples/comms/rda5807m/demo.js +68 -0
- package/examples/comms/rda5807m/minimal.js +15 -0
- package/examples/comms/rfm9x/complete.js +50 -0
- package/examples/comms/rfm9x/demo.js +55 -0
- package/examples/comms/rfm9x/minimal.js +20 -0
- package/examples/display/pcf8576/complete.js +35 -0
- package/examples/display/pcf8576/demo.js +48 -0
- package/examples/display/pcf8576/minimal.js +20 -0
- package/examples/environmental/aht21/complete.js +32 -0
- package/examples/environmental/aht21/demo.js +46 -0
- package/examples/environmental/aht21/minimal.js +12 -0
- package/examples/environmental/bme280/complete.js +37 -0
- package/examples/environmental/bme280/demo.js +67 -0
- package/examples/environmental/bme280/minimal.js +20 -0
- package/examples/environmental/bme680/complete.js +40 -0
- package/examples/environmental/bme680/demo.js +54 -0
- package/examples/environmental/bme680/minimal.js +21 -0
- package/examples/gas/ens160/complete.js +54 -0
- package/examples/gas/ens160/demo.js +54 -0
- package/examples/gas/ens160/minimal.js +29 -0
- package/examples/gnss/neo6/complete.js +49 -0
- package/examples/gnss/neo6/demo.js +54 -0
- package/examples/gnss/neo6/minimal.js +30 -0
- package/examples/gyroscope/l3g4200d/complete.js +56 -0
- package/examples/gyroscope/l3g4200d/demo.js +49 -0
- package/examples/gyroscope/l3g4200d/minimal.js +19 -0
- package/examples/gyroscope/l3gd20h/complete.js +42 -0
- package/examples/gyroscope/l3gd20h/demo.js +29 -0
- package/examples/gyroscope/l3gd20h/minimal.js +13 -0
- package/examples/humidity/dht11/complete.js +22 -0
- package/examples/humidity/dht11/demo.js +38 -0
- package/examples/humidity/dht11/minimal.js +18 -0
- package/examples/imu/mpu6050/complete.js +58 -0
- package/examples/imu/mpu6050/demo.js +37 -0
- package/examples/imu/mpu6050/minimal.js +14 -0
- package/examples/imu/mpu9250/complete.js +65 -0
- package/examples/imu/mpu9250/demo.js +53 -0
- package/examples/imu/mpu9250/minimal.js +21 -0
- package/examples/imu/mpu9255/complete.js +73 -0
- package/examples/imu/mpu9255/demo.js +73 -0
- package/examples/imu/mpu9255/minimal.js +21 -0
- package/examples/io_expander/mcp23017/complete.js +70 -77
- package/examples/io_expander/mcp23017/demo.js +37 -33
- package/examples/io_expander/mcp23017/minimal.js +15 -11
- package/examples/io_expander/pcf8574/complete.js +60 -65
- package/examples/io_expander/pcf8574/demo.js +30 -25
- package/examples/io_expander/pcf8574/minimal.js +15 -11
- package/examples/io_expander/pcf8575/complete.js +24 -20
- package/examples/io_expander/pcf8575/demo.js +27 -23
- package/examples/io_expander/pcf8575/minimal.js +16 -12
- package/examples/io_expander/tpic6b595/complete.js +57 -0
- package/examples/io_expander/tpic6b595/demo.js +75 -0
- package/examples/io_expander/tpic6b595/minimal.js +26 -0
- package/examples/led/apa102/complete.js +72 -0
- package/examples/led/apa102/demo.js +92 -0
- package/examples/led/apa102/minimal.js +21 -0
- package/examples/led/sk6812rgbw/complete.js +42 -40
- package/examples/led/sk6812rgbw/demo.js +16 -16
- package/examples/led/sk6812rgbw/minimal.js +15 -17
- package/examples/led/ws2812b/complete.js +40 -38
- package/examples/led/ws2812b/demo.js +16 -16
- package/examples/led/ws2812b/minimal.js +13 -14
- package/examples/led/ws2814/complete.js +71 -0
- package/examples/led/ws2814/demo.js +82 -0
- package/examples/led/ws2814/minimal.js +23 -0
- package/examples/light/apds-9930/complete.js +46 -0
- package/examples/light/apds-9930/demo.js +31 -0
- package/examples/light/apds-9930/minimal.js +20 -0
- package/examples/light/apds9960/complete.js +64 -0
- package/examples/light/apds9960/demo.js +34 -0
- package/examples/light/apds9960/minimal.js +12 -0
- package/examples/magnetometer/as5600/complete.js +31 -29
- package/examples/magnetometer/as5600/demo.js +35 -33
- package/examples/magnetometer/as5600/minimal.js +7 -7
- package/examples/magnetometer/hmc5883l/complete.js +47 -0
- package/examples/magnetometer/hmc5883l/demo.js +50 -0
- package/examples/magnetometer/hmc5883l/minimal.js +15 -0
- package/examples/memory/24aa02uid/complete.js +42 -0
- package/examples/memory/24aa02uid/demo.js +42 -0
- package/examples/memory/24aa02uid/minimal.js +12 -0
- package/examples/other/mpr121/complete.js +50 -0
- package/examples/other/mpr121/demo.js +30 -0
- package/examples/other/mpr121/minimal.js +17 -0
- package/examples/power/ade7953/complete.js +44 -0
- package/examples/power/ade7953/demo.js +28 -0
- package/examples/power/ade7953/minimal.js +15 -0
- package/examples/power/ina219/complete.js +20 -18
- package/examples/power/ina219/demo.js +15 -13
- package/examples/power/ina219/minimal.js +8 -8
- package/examples/power/ina226/complete.js +24 -22
- package/examples/power/ina226/demo.js +22 -20
- package/examples/power/ina226/minimal.js +5 -5
- package/examples/power/ina3221/complete.js +51 -49
- package/examples/power/ina3221/demo.js +13 -13
- package/examples/power/ina3221/minimal.js +8 -8
- package/examples/pressure/bmp085/complete.js +29 -0
- package/examples/pressure/bmp085/demo.js +34 -0
- package/examples/pressure/bmp085/minimal.js +19 -0
- package/examples/pressure/bmp180/complete.js +20 -17
- package/examples/pressure/bmp180/demo.js +24 -22
- package/examples/pressure/bmp180/minimal.js +13 -11
- package/examples/pressure/bmp280/complete.js +29 -22
- package/examples/pressure/bmp280/demo.js +42 -23
- package/examples/pressure/bmp280/minimal.js +13 -11
- package/examples/pressure/bmp384/complete.js +29 -0
- package/examples/pressure/bmp384/demo.js +40 -0
- package/examples/pressure/bmp384/minimal.js +18 -0
- package/examples/pressure/bmp581/complete.js +34 -0
- package/examples/pressure/bmp581/demo.js +56 -0
- package/examples/pressure/bmp581/minimal.js +19 -0
- package/examples/pressure/lps22df/complete.js +50 -0
- package/examples/pressure/lps22df/demo.js +41 -0
- package/examples/pressure/lps22df/minimal.js +19 -0
- package/examples/pressure/lps28dfw/complete.js +37 -0
- package/examples/pressure/lps28dfw/demo.js +33 -0
- package/examples/pressure/lps28dfw/minimal.js +19 -0
- package/examples/pressure/lps33hw/complete.js +43 -0
- package/examples/pressure/lps33hw/demo.js +43 -0
- package/examples/pressure/lps33hw/minimal.js +19 -0
- package/examples/rfid/mfrc522/complete.js +44 -0
- package/examples/rfid/mfrc522/demo.js +70 -0
- package/examples/rfid/mfrc522/minimal.js +17 -0
- package/package.json +5 -1
- package/src/chips/accelerometer/adxl345.js +482 -0
- package/src/chips/accelerometer/adxl362.js +594 -0
- package/src/chips/adc_dac/ad7705.js +345 -0
- package/src/chips/adc_dac/ad7706.js +331 -0
- package/src/chips/adc_dac/hx710a.js +184 -0
- package/src/chips/adc_dac/hx710b.js +186 -0
- package/src/chips/adc_dac/hx711.js +165 -0
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +297 -0
- package/src/chips/adc_dac/pcf8591.js +196 -0
- package/src/chips/comms/mcp2515.js +659 -0
- package/src/chips/comms/rda5807m.js +393 -0
- package/src/chips/comms/rfm9x.js +590 -0
- package/src/chips/display/pcf8576.js +232 -0
- package/src/chips/environmental/aht21.js +185 -0
- package/src/chips/environmental/bme280.js +385 -0
- package/src/chips/environmental/bme680.js +512 -0
- package/src/chips/gas/ens160.js +298 -0
- package/src/chips/gnss/neo6.js +438 -0
- package/src/chips/gyroscope/l3g4200d.js +366 -0
- package/src/chips/gyroscope/l3gd20h.js +314 -0
- package/src/chips/humidity/dht11.js +135 -0
- package/src/chips/imu/mpu6050.js +289 -0
- package/src/chips/imu/mpu9250.js +394 -0
- package/src/chips/imu/mpu9255.js +447 -0
- package/src/chips/io_expander/mcp23017.js +240 -157
- package/src/chips/io_expander/pcf8574.js +149 -124
- package/src/chips/io_expander/pcf8575.js +164 -135
- package/src/chips/io_expander/tpic6b595.js +242 -0
- package/src/chips/led/_NeoPixelRGBBase.js +204 -0
- package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
- package/src/chips/led/apa102.js +236 -0
- package/src/chips/led/sk6812rgbw.js +15 -149
- package/src/chips/led/ws2812b.js +14 -139
- package/src/chips/led/ws2814.js +49 -0
- package/src/chips/light/apds-9930.js +342 -0
- package/src/chips/light/apds9960.js +499 -0
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +182 -0
- package/src/chips/other/mpr121.js +375 -0
- package/src/chips/power/ade7953.js +427 -0
- package/src/chips/power/ina219.js +45 -40
- package/src/chips/power/ina226.js +50 -45
- package/src/chips/power/ina3221.js +95 -76
- package/src/chips/pressure/bmp085.js +251 -0
- package/src/chips/pressure/bmp180.js +54 -37
- package/src/chips/pressure/bmp280.js +188 -206
- package/src/chips/pressure/bmp384.js +410 -0
- package/src/chips/pressure/bmp581.js +489 -0
- package/src/chips/pressure/lps22df.js +341 -0
- package/src/chips/pressure/lps28dfw.js +314 -0
- package/src/chips/pressure/lps33hw.js +255 -0
- package/src/chips/rfid/mfrc522.js +674 -0
- package/src/connection/connection.js +111 -0
- package/src/connection/dhtxx.js +138 -0
- package/src/connection/dhtxx_mock.js +39 -0
- package/src/connection/hx711.js +132 -0
- package/src/connection/hx711_mock.js +52 -0
- package/src/{transport → connection}/i2c.js +13 -9
- package/src/connection/i2c_mock.js +91 -0
- package/src/connection/input_pin.js +156 -0
- package/src/connection/neo6_mock.js +45 -0
- package/src/{transport → connection}/neopixel.js +23 -4
- package/src/connection/output_pin.js +41 -0
- package/src/connection/sipo.js +159 -0
- package/src/{transport → connection}/smbus.js +14 -10
- package/src/{transport → connection}/spi.js +12 -8
- package/src/connection/spi_mock.js +151 -0
- package/src/{transport → connection}/uart.js +33 -26
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/pressure/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/pressure/.gitkeep +0 -0
|
@@ -10,43 +10,45 @@ const GC_WAKE = 0x09;
|
|
|
10
10
|
* MCP4725 single-channel 12-bit voltage-output DAC — minimal interface.
|
|
11
11
|
*
|
|
12
12
|
* Provides simple voltage output as a fraction of V_DD with no configuration
|
|
13
|
-
* beyond the
|
|
13
|
+
* beyond the connection. Uses Fast Write (2-byte) for DAC register updates.
|
|
14
14
|
*
|
|
15
|
-
* @param {
|
|
15
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (0x60–0x61).
|
|
16
16
|
*/
|
|
17
17
|
class MCP4725Minimal {
|
|
18
18
|
/**
|
|
19
|
-
* @param {
|
|
19
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device.
|
|
20
20
|
*/
|
|
21
|
-
constructor(
|
|
22
|
-
this.
|
|
21
|
+
constructor(connection) {
|
|
22
|
+
this._conn = connection;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Set the DAC output as a fraction of V_DD.
|
|
27
27
|
* @param {number} fraction - Output voltage as a fraction of V_DD (0.0–1.0).
|
|
28
|
+
* @returns {Promise<void>}
|
|
28
29
|
*/
|
|
29
|
-
set_voltage(fraction) {
|
|
30
|
+
async set_voltage(fraction) {
|
|
30
31
|
if (fraction < 0.0) fraction = 0.0;
|
|
31
32
|
if (fraction > 1.0) fraction = 1.0;
|
|
32
33
|
const code = Math.round(fraction * 4095);
|
|
33
|
-
this._fast_write(code, 0);
|
|
34
|
+
await this._fast_write(code, 0);
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
/**
|
|
37
38
|
* Set the raw 12-bit DAC code directly.
|
|
38
39
|
* @param {number} code - Raw 12-bit DAC code (0–4095).
|
|
40
|
+
* @returns {Promise<void>}
|
|
39
41
|
*/
|
|
40
|
-
set_raw(code) {
|
|
42
|
+
async set_raw(code) {
|
|
41
43
|
if (code > 4095) code = 4095;
|
|
42
|
-
this._fast_write(code, 0);
|
|
44
|
+
await this._fast_write(code, 0);
|
|
43
45
|
}
|
|
44
46
|
|
|
45
|
-
_fast_write(code, pd_mode) {
|
|
47
|
+
async _fast_write(code, pd_mode) {
|
|
46
48
|
const buf = Buffer.alloc(2);
|
|
47
49
|
buf[0] = ((pd_mode & 0x03) << 4) | ((code >> 8) & 0x0F);
|
|
48
50
|
buf[1] = code & 0xFF;
|
|
49
|
-
this.
|
|
51
|
+
await this._conn.write(buf);
|
|
50
52
|
}
|
|
51
53
|
}
|
|
52
54
|
|
|
@@ -56,14 +58,14 @@ class MCP4725Minimal {
|
|
|
56
58
|
* Adds write-with-EEPROM persistence, power-down modes, General Call reset/wake,
|
|
57
59
|
* and full register read-back of both DAC and EEPROM contents.
|
|
58
60
|
*
|
|
59
|
-
* @param {
|
|
61
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (0x60–0x61).
|
|
60
62
|
*/
|
|
61
63
|
class MCP4725Full extends MCP4725Minimal {
|
|
62
64
|
/**
|
|
63
|
-
* @param {
|
|
65
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device.
|
|
64
66
|
*/
|
|
65
|
-
constructor(
|
|
66
|
-
super(
|
|
67
|
+
constructor(connection) {
|
|
68
|
+
super(connection);
|
|
67
69
|
}
|
|
68
70
|
|
|
69
71
|
/** @type {number} Normal operation (power-down mode 0). */
|
|
@@ -78,35 +80,38 @@ class MCP4725Full extends MCP4725Minimal {
|
|
|
78
80
|
/**
|
|
79
81
|
* Set the DAC output and persist to EEPROM.
|
|
80
82
|
* @param {number} fraction - Output voltage as a fraction of V_DD (0.0–1.0).
|
|
83
|
+
* @returns {Promise<void>}
|
|
81
84
|
*/
|
|
82
|
-
set_voltage_eeprom(fraction) {
|
|
85
|
+
async set_voltage_eeprom(fraction) {
|
|
83
86
|
if (fraction < 0.0) fraction = 0.0;
|
|
84
87
|
if (fraction > 1.0) fraction = 1.0;
|
|
85
88
|
const code = Math.round(fraction * 4095);
|
|
86
|
-
this._write_dac_eeprom(code, 0);
|
|
89
|
+
await this._write_dac_eeprom(code, 0);
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
/**
|
|
90
93
|
* Set the raw 12-bit DAC code and persist to EEPROM.
|
|
91
94
|
* @param {number} code - Raw 12-bit DAC code (0–4095).
|
|
95
|
+
* @returns {Promise<void>}
|
|
92
96
|
*/
|
|
93
|
-
set_raw_eeprom(code) {
|
|
97
|
+
async set_raw_eeprom(code) {
|
|
94
98
|
if (code > 4095) code = 4095;
|
|
95
|
-
this._write_dac_eeprom(code, 0);
|
|
99
|
+
await this._write_dac_eeprom(code, 0);
|
|
96
100
|
}
|
|
97
101
|
|
|
98
102
|
/**
|
|
99
103
|
* 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.
|
|
104
|
+
* @returns {Promise<{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
105
|
*/
|
|
102
|
-
read() {
|
|
103
|
-
const buf = this.
|
|
106
|
+
async read() {
|
|
107
|
+
const buf = await this._conn.writeRead(Buffer.from([0x00]), 5);
|
|
108
|
+
const code = (buf[1] << 4) | ((buf[2] >> 4) & 0x0F);
|
|
104
109
|
return {
|
|
105
|
-
code:
|
|
106
|
-
voltage_fraction:
|
|
110
|
+
code: code,
|
|
111
|
+
voltage_fraction: code / 4095.0,
|
|
107
112
|
power_down: (buf[0] >> 2) & 0x03,
|
|
108
113
|
eeprom_code: ((buf[3] & 0x0F) << 8) | buf[4],
|
|
109
|
-
eeprom_power_down: (buf[3] >>
|
|
114
|
+
eeprom_power_down: (buf[3] >> 5) & 0x03,
|
|
110
115
|
eeprom_ready: !!(buf[0] & 0x80),
|
|
111
116
|
};
|
|
112
117
|
}
|
|
@@ -114,48 +119,51 @@ class MCP4725Full extends MCP4725Minimal {
|
|
|
114
119
|
/**
|
|
115
120
|
* Set the power-down mode and preserve the current DAC code.
|
|
116
121
|
* @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).
|
|
122
|
+
* @returns {Promise<void>}
|
|
117
123
|
*/
|
|
118
|
-
set_power_down(mode) {
|
|
124
|
+
async set_power_down(mode) {
|
|
119
125
|
if (mode > 3) mode = 3;
|
|
120
|
-
const code = this._read_dac_code();
|
|
121
|
-
this._fast_write(code, mode);
|
|
126
|
+
const code = await this._read_dac_code();
|
|
127
|
+
await this._fast_write(code, mode);
|
|
122
128
|
}
|
|
123
129
|
|
|
124
130
|
/**
|
|
125
131
|
* Send General Call Wake-Up (0x00, 0x09) to clear power-down bits.
|
|
132
|
+
* @returns {Promise<void>}
|
|
126
133
|
*/
|
|
127
|
-
wake_up() {
|
|
128
|
-
this.
|
|
134
|
+
async wake_up() {
|
|
135
|
+
await this._conn.write(Buffer.from([ADDR_GENERAL_CALL, GC_WAKE]));
|
|
129
136
|
}
|
|
130
137
|
|
|
131
138
|
/**
|
|
132
139
|
* Send General Call Reset (0x00, 0x06) to trigger internal POR.
|
|
140
|
+
* @returns {Promise<void>}
|
|
133
141
|
*/
|
|
134
|
-
reset() {
|
|
135
|
-
this.
|
|
142
|
+
async reset() {
|
|
143
|
+
await this._conn.write(Buffer.from([ADDR_GENERAL_CALL, GC_RESET]));
|
|
136
144
|
}
|
|
137
145
|
|
|
138
146
|
/**
|
|
139
147
|
* Check if the EEPROM write operation is complete.
|
|
140
|
-
* @returns {boolean} True when a pending EEPROM write has finished.
|
|
148
|
+
* @returns {Promise<boolean>} True when a pending EEPROM write has finished.
|
|
141
149
|
*/
|
|
142
|
-
is_eeprom_ready() {
|
|
143
|
-
const buf = this.
|
|
150
|
+
async is_eeprom_ready() {
|
|
151
|
+
const buf = await this._conn.writeRead(Buffer.from([0x00]), 1);
|
|
144
152
|
return !!(buf[0] & 0x80);
|
|
145
153
|
}
|
|
146
154
|
|
|
147
|
-
_write_dac_eeprom(code, pd_mode) {
|
|
155
|
+
async _write_dac_eeprom(code, pd_mode) {
|
|
148
156
|
const buf = Buffer.alloc(3);
|
|
149
157
|
buf[0] = CMD_WRITE_DAC_EEPROM | ((pd_mode & 0x03) << 1);
|
|
150
158
|
buf[1] = (code >> 4) & 0xFF;
|
|
151
159
|
buf[2] = (code & 0x0F) << 4;
|
|
152
|
-
this.
|
|
160
|
+
await this._conn.write(buf);
|
|
153
161
|
}
|
|
154
162
|
|
|
155
|
-
_read_dac_code() {
|
|
156
|
-
const buf = this.
|
|
163
|
+
async _read_dac_code() {
|
|
164
|
+
const buf = await this._conn.writeRead(Buffer.from([0x00]), 2);
|
|
157
165
|
return ((buf[0] & 0x0F) << 8) | buf[1];
|
|
158
166
|
}
|
|
159
167
|
}
|
|
160
168
|
|
|
161
|
-
module.exports = { MCP4725Minimal, MCP4725Full };
|
|
169
|
+
module.exports = { MCP4725Minimal, MCP4725Full };
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const CMD_MULTI_WRITE_BASE = 0x40; // [0 1 0 0 0 DAC1 DAC0 UDAC]
|
|
4
|
+
const CMD_SINGLE_WRITE = 0x58; // [0 1 0 1 1 DAC1 DAC0 UDAC]
|
|
5
|
+
const CMD_SEQUENTIAL_BASE = 0x50; // [0 1 0 1 0 DAC1 DAC0 UDAC]
|
|
6
|
+
const CMD_WRITE_VREF = 0x80; // [1 0 0 X Vref_A Vref_B Vref_C Vref_D]
|
|
7
|
+
const CMD_WRITE_GAIN = 0xC0; // [1 1 0 X Gx_A Gx_B Gx_C Gx_D]
|
|
8
|
+
const CMD_WRITE_POWERDOWN = 0xA0; // [1 0 1 X ...]
|
|
9
|
+
const ADDR_GENERAL_CALL = 0x00;
|
|
10
|
+
const GC_RESET = 0x06;
|
|
11
|
+
const GC_SOFTWARE_UPD = 0x08;
|
|
12
|
+
const GC_WAKE = 0x09;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* MCP4728 quad-channel 12-bit voltage-output DAC — minimal interface.
|
|
16
|
+
*
|
|
17
|
+
* Provides simple voltage output as a fraction of V_DD for any of the four
|
|
18
|
+
* channels (A–D) plus a convenience method to update all four channels
|
|
19
|
+
* simultaneously. No configuration required beyond the connection. V_REF is
|
|
20
|
+
* fixed at external (V_DD), gain is fixed at ×1, and power-down is off.
|
|
21
|
+
* EEPROM is never written by this class.
|
|
22
|
+
*
|
|
23
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (0x60–0x67).
|
|
24
|
+
*/
|
|
25
|
+
class MCP4728Minimal {
|
|
26
|
+
/**
|
|
27
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device.
|
|
28
|
+
*/
|
|
29
|
+
constructor(connection) {
|
|
30
|
+
this._conn = connection;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Set one channel's DAC output as a fraction of V_DD.
|
|
35
|
+
* @param {number} channel - Channel index 0 (A) – 3 (D).
|
|
36
|
+
* @param {number} fraction - Output voltage as a fraction of V_DD (0.0–1.0).
|
|
37
|
+
* @returns {Promise<void>}
|
|
38
|
+
*/
|
|
39
|
+
async set_voltage(channel, fraction) {
|
|
40
|
+
let f = Math.max(0.0, Math.min(1.0, fraction));
|
|
41
|
+
const code = Math.round(f * 4095);
|
|
42
|
+
await this.set_raw(channel, code);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Set one channel's raw 12-bit DAC code.
|
|
47
|
+
* @param {number} channel - Channel index 0 (A) – 3 (D).
|
|
48
|
+
* @param {number} code - Raw 12-bit DAC code (0–4095).
|
|
49
|
+
* @returns {Promise<void>}
|
|
50
|
+
*/
|
|
51
|
+
async set_raw(channel, code) {
|
|
52
|
+
const ch = Math.max(0, Math.min(3, channel | 0));
|
|
53
|
+
const c = Math.max(0, Math.min(4095, code | 0));
|
|
54
|
+
await this._multi_write(ch, c, 0, 0, 0, 0);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Update all four channels simultaneously using Fast Write.
|
|
59
|
+
* @param {number[]} fractions - Array of 4 fractions (0.0–1.0), index 0 = A.
|
|
60
|
+
* @returns {Promise<void>}
|
|
61
|
+
*/
|
|
62
|
+
async set_all(fractions) {
|
|
63
|
+
if (!Array.isArray(fractions) || fractions.length !== 4) {
|
|
64
|
+
throw new Error('fractions must have exactly 4 elements');
|
|
65
|
+
}
|
|
66
|
+
const buf = Buffer.alloc(8);
|
|
67
|
+
for (let i = 0; i < 4; i++) {
|
|
68
|
+
let f = Math.max(0.0, Math.min(1.0, fractions[i]));
|
|
69
|
+
const code = Math.max(0, Math.min(4095, Math.round(f * 4095)));
|
|
70
|
+
// Fast Write: byte1=[0 0 PD1 PD0 D11-D8] (PD=00), byte2=[D7-D0]
|
|
71
|
+
buf[i * 2] = (code >> 8) & 0x0F;
|
|
72
|
+
buf[i * 2 + 1] = code & 0xFF;
|
|
73
|
+
}
|
|
74
|
+
await this._conn.write(buf);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async _multi_write(channel, code, vref, pd, gain, udac) {
|
|
78
|
+
const buf = Buffer.alloc(3);
|
|
79
|
+
buf[0] = CMD_MULTI_WRITE_BASE | ((channel & 0x03) << 1) | (udac & 0x01);
|
|
80
|
+
buf[1] = ((vref & 0x01) << 7) | ((pd & 0x03) << 5) |
|
|
81
|
+
((gain & 0x01) << 4) | ((code >> 8) & 0x0F);
|
|
82
|
+
buf[2] = code & 0xFF;
|
|
83
|
+
await this._conn.write(buf);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* MCP4728 full interface — extends MCP4728Minimal with EEPROM, V_REF, gain, power-down, and read-back.
|
|
89
|
+
*
|
|
90
|
+
* Adds per-channel V_REF and gain configuration, all-channel V_REF/gain/
|
|
91
|
+
* power-down commands, write-with-EEPROM persistence (Single and Sequential
|
|
92
|
+
* Write), General Call reset/wake-up/software-update, and full 24-byte
|
|
93
|
+
* read-back of all channel DAC input registers and EEPROM contents.
|
|
94
|
+
*
|
|
95
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (0x60–0x67).
|
|
96
|
+
*/
|
|
97
|
+
class MCP4728Full extends MCP4728Minimal {
|
|
98
|
+
/** @type {number} Normal operation (power-down mode 0). */
|
|
99
|
+
static PD_NORMAL = 0;
|
|
100
|
+
/** @type {number} Power-down with 1 kΩ to GND (mode 1). */
|
|
101
|
+
static PD_1K_GND = 1;
|
|
102
|
+
/** @type {number} Power-down with 100 kΩ to GND (mode 2). */
|
|
103
|
+
static PD_100K_GND = 2;
|
|
104
|
+
/** @type {number} Power-down with 500 kΩ to GND (mode 3). */
|
|
105
|
+
static PD_500K_GND = 3;
|
|
106
|
+
|
|
107
|
+
/** @type {number} External V_DD reference. */
|
|
108
|
+
static VREF_EXTERNAL = 0;
|
|
109
|
+
/** @type {number} Internal 2.048 V reference. */
|
|
110
|
+
static VREF_INTERNAL = 1;
|
|
111
|
+
|
|
112
|
+
/** @type {number} Gain ×1. */
|
|
113
|
+
static GAIN_X1 = 0;
|
|
114
|
+
/** @type {number} Gain ×2. */
|
|
115
|
+
static GAIN_X2 = 1;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device.
|
|
119
|
+
*/
|
|
120
|
+
constructor(connection) {
|
|
121
|
+
super(connection);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Set one channel's output and persist to EEPROM (Single Write).
|
|
126
|
+
* @param {number} channel - Channel index 0 (A) – 3 (D).
|
|
127
|
+
* @param {number} fraction - Output as a fraction of the configured full-scale.
|
|
128
|
+
* @param {number} vref - 0 = external (V_DD), 1 = internal (2.048 V).
|
|
129
|
+
* @param {number} gain - 1 = ×1, 2 = ×2 (ignored when vref = external).
|
|
130
|
+
* @returns {Promise<void>}
|
|
131
|
+
*/
|
|
132
|
+
async set_voltage_eeprom(channel, fraction, vref, gain) {
|
|
133
|
+
let f = Math.max(0.0, Math.min(1.0, fraction));
|
|
134
|
+
const code = Math.round(f * 4095);
|
|
135
|
+
await this._single_write(channel, code, vref, 0, gain, 0);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Set one channel's raw 12-bit code and persist to EEPROM.
|
|
140
|
+
* @param {number} channel - Channel index 0 (A) – 3 (D).
|
|
141
|
+
* @param {number} code - Raw 12-bit DAC code (0–4095).
|
|
142
|
+
* @param {number} vref - 0 = external (V_DD), 1 = internal (2.048 V).
|
|
143
|
+
* @param {number} gain - 1 = ×1, 2 = ×2 (ignored when vref = external).
|
|
144
|
+
* @returns {Promise<void>}
|
|
145
|
+
*/
|
|
146
|
+
async set_raw_eeprom(channel, code, vref, gain) {
|
|
147
|
+
const c = Math.max(0, Math.min(4095, code | 0));
|
|
148
|
+
await this._single_write(channel, c, vref, 0, gain, 0);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Update all four channels and EEPROM (Sequential Write from A to D).
|
|
153
|
+
* @param {number[]} fractions - Array of 4 fractions (0.0–1.0).
|
|
154
|
+
* @param {number[]} vrefs - Array of 4 V_REF values (0/1).
|
|
155
|
+
* @param {number[]} gains - Array of 4 gain values (1/2).
|
|
156
|
+
* @returns {Promise<void>}
|
|
157
|
+
*/
|
|
158
|
+
async set_all_eeprom(fractions, vrefs, gains) {
|
|
159
|
+
if (fractions.length !== 4 || vrefs.length !== 4 || gains.length !== 4) {
|
|
160
|
+
throw new Error('fractions, vrefs, gains must each have 4 elements');
|
|
161
|
+
}
|
|
162
|
+
const buf = Buffer.alloc(9);
|
|
163
|
+
// Sequential Write starting at channel 0 (A): [0 1 0 1 0 0 0 UDAC] = 0x50
|
|
164
|
+
buf[0] = CMD_SEQUENTIAL_BASE | 0x00;
|
|
165
|
+
for (let i = 0; i < 4; i++) {
|
|
166
|
+
let f = Math.max(0.0, Math.min(1.0, fractions[i]));
|
|
167
|
+
const code = Math.max(0, Math.min(4095, Math.round(f * 4095)));
|
|
168
|
+
const v = vrefs[i] ? 1 : 0;
|
|
169
|
+
const g = (gains[i] === 2) ? 1 : 0;
|
|
170
|
+
// Per-channel byte layout (Multi-Write format): [V_REF PD1 PD0 Gx D11-D8]
|
|
171
|
+
// PD bits are always 0 here (no power-down is set in this command).
|
|
172
|
+
buf[1 + i * 2] = ((v & 0x01) << 7) | ((g & 0x01) << 4) | ((code >> 8) & 0x0F);
|
|
173
|
+
buf[1 + i * 2 + 1] = code & 0xFF;
|
|
174
|
+
}
|
|
175
|
+
await this._conn.write(buf);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Set V_REF for all four channels (volatile register only).
|
|
180
|
+
* @param {number} vref_a - 0 = external V_DD, 1 = internal 2.048 V.
|
|
181
|
+
* @param {number} vref_b
|
|
182
|
+
* @param {number} vref_c
|
|
183
|
+
* @param {number} vref_d
|
|
184
|
+
* @returns {Promise<void>}
|
|
185
|
+
*/
|
|
186
|
+
async set_vref(vref_a, vref_b, vref_c, vref_d) {
|
|
187
|
+
const byte1 = CMD_WRITE_VREF |
|
|
188
|
+
((vref_a ? 1 : 0) << 3) | ((vref_b ? 1 : 0) << 2) |
|
|
189
|
+
((vref_c ? 1 : 0) << 1) | (vref_d ? 1 : 0);
|
|
190
|
+
await this._conn.write(Buffer.from([byte1]));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Set gain for all four channels (volatile register only).
|
|
195
|
+
* @param {number} gain_a - 1 = ×1, 2 = ×2.
|
|
196
|
+
* @param {number} gain_b
|
|
197
|
+
* @param {number} gain_c
|
|
198
|
+
* @param {number} gain_d
|
|
199
|
+
* @returns {Promise<void>}
|
|
200
|
+
*/
|
|
201
|
+
async set_gain(gain_a, gain_b, gain_c, gain_d) {
|
|
202
|
+
const byte1 = CMD_WRITE_GAIN |
|
|
203
|
+
((gain_a === 2 ? 1 : 0) << 3) | ((gain_b === 2 ? 1 : 0) << 2) |
|
|
204
|
+
((gain_c === 2 ? 1 : 0) << 1) | (gain_d === 2 ? 1 : 0);
|
|
205
|
+
await this._conn.write(Buffer.from([byte1]));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Set power-down mode for all four channels (volatile register only).
|
|
210
|
+
* @param {number} pd_a - 0–3.
|
|
211
|
+
* @param {number} pd_b
|
|
212
|
+
* @param {number} pd_c
|
|
213
|
+
* @param {number} pd_d
|
|
214
|
+
* @returns {Promise<void>}
|
|
215
|
+
*/
|
|
216
|
+
async set_power_down(pd_a, pd_b, pd_c, pd_d) {
|
|
217
|
+
const byte1 = CMD_WRITE_POWERDOWN |
|
|
218
|
+
(((pd_a >> 1) & 0x01) << 4) | ((pd_a & 0x01) << 3) |
|
|
219
|
+
(((pd_b >> 1) & 0x01) << 2) | ((pd_b & 0x01) << 1);
|
|
220
|
+
const byte2 = (((pd_c >> 1) & 0x01) << 6) | ((pd_c & 0x01) << 5) |
|
|
221
|
+
(((pd_d >> 1) & 0x01) << 4) | ((pd_d & 0x01) << 3);
|
|
222
|
+
await this._conn.write(Buffer.from([byte1, byte2]));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Read all four channels' DAC input registers and EEPROM contents.
|
|
227
|
+
* @returns {Promise<{channel: object[], eeprom_ready: boolean}>} Per-channel code, vref, gain, power_down, eeprom_*.
|
|
228
|
+
*/
|
|
229
|
+
async read() {
|
|
230
|
+
const buf = await this._conn.read(24);
|
|
231
|
+
const result = { channel: [], eeprom_ready: !!(buf[0] & 0x80) };
|
|
232
|
+
for (let i = 0; i < 4; i++) {
|
|
233
|
+
const b = i * 3;
|
|
234
|
+
result.channel.push({
|
|
235
|
+
code: ((buf[b + 1] & 0x0F) << 8) | buf[b + 2],
|
|
236
|
+
vref: (buf[b + 1] >> 7) & 0x01,
|
|
237
|
+
gain: ((buf[b + 1] >> 4) & 0x01) ? 2 : 1,
|
|
238
|
+
power_down: (buf[b + 1] >> 5) & 0x03,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
for (let i = 0; i < 4; i++) {
|
|
242
|
+
const b = 12 + i * 3;
|
|
243
|
+
result.channel[i].eeprom_code = ((buf[b + 1] & 0x0F) << 8) | buf[b + 2];
|
|
244
|
+
result.channel[i].eeprom_vref = (buf[b + 1] >> 7) & 0x01;
|
|
245
|
+
result.channel[i].eeprom_gain = ((buf[b + 1] >> 4) & 0x01) ? 2 : 1;
|
|
246
|
+
result.channel[i].eeprom_power_down = (buf[b + 1] >> 5) & 0x03;
|
|
247
|
+
}
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Check if the EEPROM write is complete (RDY/BSY = 1).
|
|
253
|
+
* @returns {Promise<boolean>}
|
|
254
|
+
*/
|
|
255
|
+
async is_eeprom_ready() {
|
|
256
|
+
const buf = await this._conn.read(1);
|
|
257
|
+
return !!(buf[0] & 0x80);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Send General Call Software Update (0x00, 0x08) to latch all V_OUT.
|
|
262
|
+
* @returns {Promise<void>}
|
|
263
|
+
*/
|
|
264
|
+
async software_update() {
|
|
265
|
+
await this._conn.write(Buffer.from([ADDR_GENERAL_CALL, GC_SOFTWARE_UPD]));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Send General Call Wake-Up (0x00, 0x09) to clear all PD bits.
|
|
270
|
+
* @returns {Promise<void>}
|
|
271
|
+
*/
|
|
272
|
+
async wake_up() {
|
|
273
|
+
await this._conn.write(Buffer.from([ADDR_GENERAL_CALL, GC_WAKE]));
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Send General Call Reset (0x00, 0x06) to reload EEPROM into all DAC registers.
|
|
278
|
+
* @returns {Promise<void>}
|
|
279
|
+
*/
|
|
280
|
+
async reset() {
|
|
281
|
+
await this._conn.write(Buffer.from([ADDR_GENERAL_CALL, GC_RESET]));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async _single_write(channel, code, vref, pd, gain, udac) {
|
|
285
|
+
const ch = Math.max(0, Math.min(3, channel | 0));
|
|
286
|
+
const c = Math.max(0, Math.min(4095, code | 0));
|
|
287
|
+
const g = (gain === 2) ? 1 : 0;
|
|
288
|
+
const buf = Buffer.alloc(3);
|
|
289
|
+
buf[0] = CMD_SINGLE_WRITE | ((ch & 0x03) << 1) | (udac & 0x01);
|
|
290
|
+
buf[1] = ((vref & 0x01) << 7) | ((pd & 0x03) << 5) |
|
|
291
|
+
(g << 4) | ((c >> 8) & 0x0F);
|
|
292
|
+
buf[2] = c & 0xFF;
|
|
293
|
+
await this._conn.write(buf);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
module.exports = { MCP4728Minimal, MCP4728Full };
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const NUM_CHANNELS = 4;
|
|
4
|
+
const CONTROL_DEFAULT = 0x00; // AIP=00, AOE=0, AI=0, CHN=0
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* PCF8591 8-bit quad ADC + DAC — minimal interface.
|
|
8
|
+
*
|
|
9
|
+
* Provides single-ended reads of the four analog inputs in 4 single-ended
|
|
10
|
+
* mode (AIP=00). No configuration beyond the connection is required. Each
|
|
11
|
+
* read transaction returns 5 bytes: the first is the previous conversion
|
|
12
|
+
* result and must be discarded; the next four are fresh channel samples.
|
|
13
|
+
*
|
|
14
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (0x48–0x4F).
|
|
15
|
+
*/
|
|
16
|
+
class PCF8591Minimal {
|
|
17
|
+
/**
|
|
18
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device.
|
|
19
|
+
*/
|
|
20
|
+
constructor(connection) {
|
|
21
|
+
this._conn = connection;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Read a single channel as an unsigned 8-bit value.
|
|
26
|
+
*
|
|
27
|
+
* Uses single-shot conversion: writes the control byte selecting the
|
|
28
|
+
* channel, then reads 2 bytes (discarding the stale first byte).
|
|
29
|
+
*
|
|
30
|
+
* @param {number} channel - Channel number 0–3. Clamped to the valid range.
|
|
31
|
+
* @returns {Promise<number>} Raw 8-bit value (0–255).
|
|
32
|
+
*/
|
|
33
|
+
async read_channel(channel) {
|
|
34
|
+
let ch = (channel >= 0 && channel < NUM_CHANNELS) ? channel : 0;
|
|
35
|
+
const ctrl = CONTROL_DEFAULT | (ch & 0x03);
|
|
36
|
+
await this._conn.write(Buffer.from([ctrl]));
|
|
37
|
+
const buf = await this._conn.read(2);
|
|
38
|
+
return buf[1];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Read all four channels as unsigned 8-bit values.
|
|
43
|
+
*
|
|
44
|
+
* Uses auto-increment (AI=1) to read all four channels in one
|
|
45
|
+
* transaction. Reads 5 bytes and discards the stale first byte.
|
|
46
|
+
*
|
|
47
|
+
* @returns {Promise<number[]>} Four raw 8-bit values [ch0, ch1, ch2, ch3].
|
|
48
|
+
*/
|
|
49
|
+
async read_all() {
|
|
50
|
+
const ctrl = CONTROL_DEFAULT | 0x04; // AI=1
|
|
51
|
+
await this._conn.write(Buffer.from([ctrl]));
|
|
52
|
+
const buf = await this._conn.read(NUM_CHANNELS + 1);
|
|
53
|
+
return [buf[1], buf[2], buf[3], buf[4]];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* PCF8591 full interface — extends PCF8591Minimal with differential, voltage, and DAC output.
|
|
59
|
+
*
|
|
60
|
+
* Adds analog input mode selection (single-ended, differential, mixed),
|
|
61
|
+
* auto-increment, DAC enable/disable, raw and voltage-calibrated ADC reads,
|
|
62
|
+
* and signed differential reads.
|
|
63
|
+
*
|
|
64
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (0x48–0x4F).
|
|
65
|
+
*/
|
|
66
|
+
class PCF8591Full extends PCF8591Minimal {
|
|
67
|
+
/** @type {number} 4 single-ended inputs (AIN0–AIN3). */
|
|
68
|
+
static MODE_4_SINGLE_ENDED = 0;
|
|
69
|
+
/** @type {number} 3 differential inputs (vs AIN3). */
|
|
70
|
+
static MODE_3_DIFFERENTIAL = 1;
|
|
71
|
+
/** @type {number} AIN0/1 single-ended, AIN2-AIN3 differential. */
|
|
72
|
+
static MODE_MIXED = 2;
|
|
73
|
+
/** @type {number} 2 differential inputs. */
|
|
74
|
+
static MODE_2_DIFFERENTIAL = 3;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device.
|
|
78
|
+
*/
|
|
79
|
+
constructor(connection) {
|
|
80
|
+
super(connection);
|
|
81
|
+
this._control = CONTROL_DEFAULT;
|
|
82
|
+
this._input_mode = PCF8591Full.MODE_4_SINGLE_ENDED;
|
|
83
|
+
this._dac_enabled = false;
|
|
84
|
+
this._auto_increment = false;
|
|
85
|
+
this._last_channel = 0;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Set the analog input mode, auto-increment, and DAC enable.
|
|
90
|
+
* @param {number} input_mode - Analog input programming 0–3 (see MODE_* constants).
|
|
91
|
+
* @param {boolean} auto_increment - If true, AI=1 — channel increments after each conversion.
|
|
92
|
+
* @param {boolean} dac_enabled - If true, AOE=1 — AOUT is active; AOUT returns to
|
|
93
|
+
* high-impedance when false.
|
|
94
|
+
* @returns {Promise<void>}
|
|
95
|
+
*/
|
|
96
|
+
async configure(input_mode, auto_increment, dac_enabled) {
|
|
97
|
+
const aip = input_mode & 0x03;
|
|
98
|
+
const ai = auto_increment ? 0x04 : 0x00;
|
|
99
|
+
const aoe = dac_enabled ? 0x40 : 0x00;
|
|
100
|
+
this._control = (aip << 4) | aoe | ai | (this._last_channel & 0x03);
|
|
101
|
+
this._input_mode = aip;
|
|
102
|
+
this._auto_increment = !!auto_increment;
|
|
103
|
+
this._dac_enabled = !!dac_enabled;
|
|
104
|
+
await this._conn.write(Buffer.from([this._control]));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Read a single channel and convert to voltage.
|
|
109
|
+
* @param {number} channel - Channel number 0–3.
|
|
110
|
+
* @param {number} vref - Reference voltage in volts.
|
|
111
|
+
* @param {number} vagnd - Analog ground voltage in volts.
|
|
112
|
+
* @returns {Promise<number>} Channel voltage in volts.
|
|
113
|
+
*/
|
|
114
|
+
async read_channel_voltage(channel, vref, vagnd) {
|
|
115
|
+
const raw = await this.read_channel(channel);
|
|
116
|
+
return vagnd + raw * (vref - vagnd) / 256.0;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Read all four channels and convert each to voltage.
|
|
121
|
+
* @param {number} vref - Reference voltage in volts.
|
|
122
|
+
* @param {number} vagnd - Analog ground voltage in volts.
|
|
123
|
+
* @returns {Promise<number[]>} Four channel voltages in volts [ch0, ch1, ch2, ch3].
|
|
124
|
+
*/
|
|
125
|
+
async read_all_voltage(vref, vagnd) {
|
|
126
|
+
const raws = await this.read_all();
|
|
127
|
+
const vfs = vref - vagnd;
|
|
128
|
+
return raws.map(r => vagnd + r * vfs / 256.0);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Read a differential channel as a signed value.
|
|
133
|
+
*
|
|
134
|
+
* The chip must be configured in a differential mode (input_mode 1, 2,
|
|
135
|
+
* or 3). The result is interpreted as a signed 8-bit two's complement
|
|
136
|
+
* number.
|
|
137
|
+
*
|
|
138
|
+
* @param {number} channel - Differential channel index (0–2 for 3-diff mode, 0–1
|
|
139
|
+
* for 2-diff and mixed modes).
|
|
140
|
+
* @returns {Promise<number>} Signed 8-bit value (-128 to 127).
|
|
141
|
+
*/
|
|
142
|
+
async read_differential(channel) {
|
|
143
|
+
const ch = channel & 0x03;
|
|
144
|
+
this._last_channel = ch;
|
|
145
|
+
const ctrl = this._control | (ch & 0x03);
|
|
146
|
+
await this._conn.write(Buffer.from([ctrl]));
|
|
147
|
+
const buf = await this._conn.read(2);
|
|
148
|
+
const raw = buf[1];
|
|
149
|
+
return raw >= 128 ? raw - 256 : raw;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Enable the DAC and write a raw 8-bit value.
|
|
154
|
+
*
|
|
155
|
+
* Sets the AOE bit so AOUT becomes active, then writes the DAC value
|
|
156
|
+
* in the byte following the control byte.
|
|
157
|
+
*
|
|
158
|
+
* @param {number} value - Raw 8-bit DAC value (0–255). Output voltage is
|
|
159
|
+
* V_AGND + value × (V_REF − V_AGND) / 256.
|
|
160
|
+
* @returns {Promise<void>}
|
|
161
|
+
*/
|
|
162
|
+
async set_dac(value) {
|
|
163
|
+
let v = Math.max(0, Math.min(255, value | 0));
|
|
164
|
+
const ctrl = (this._control | 0x40) & ~0x04; // AOE=1, AI=0
|
|
165
|
+
this._control = ctrl;
|
|
166
|
+
this._dac_enabled = true;
|
|
167
|
+
await this._conn.write(Buffer.from([ctrl, v]));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Enable the DAC and set the output as a fraction of (VREF−VAGND).
|
|
172
|
+
* @param {number} voltage_fraction - Output level as a fraction of (VREF−VAGND)
|
|
173
|
+
* (0.0 = V_AGND, 1.0 = V_REF). Clamped to [0.0, 1.0].
|
|
174
|
+
* @returns {Promise<void>}
|
|
175
|
+
*/
|
|
176
|
+
async set_dac_voltage(voltage_fraction) {
|
|
177
|
+
let f = voltage_fraction;
|
|
178
|
+
if (f < 0.0) f = 0.0;
|
|
179
|
+
if (f > 1.0) f = 1.0;
|
|
180
|
+
const value = Math.round(f * 255);
|
|
181
|
+
await this.set_dac(value);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Disable the DAC output; AOUT returns to high-impedance.
|
|
186
|
+
* @returns {Promise<void>}
|
|
187
|
+
*/
|
|
188
|
+
async disable_dac() {
|
|
189
|
+
const ctrl = this._control & ~0x40; // AOE=0
|
|
190
|
+
this._control = ctrl;
|
|
191
|
+
this._dac_enabled = false;
|
|
192
|
+
await this._conn.write(Buffer.from([ctrl]));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
module.exports = { PCF8591Minimal, PCF8591Full };
|