periph 1.0.0 → 1.1.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 +49 -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/mcp4728/complete.js +41 -0
- package/examples/adc_dac/mcp4728/demo.js +41 -0
- package/examples/adc_dac/mcp4728/minimal.js +11 -0
- package/examples/display/pcf8576/complete.js +32 -0
- package/examples/display/pcf8576/demo.js +48 -0
- package/examples/display/pcf8576/minimal.js +18 -0
- package/examples/environmental/aht21/complete.js +30 -0
- package/examples/environmental/aht21/demo.js +44 -0
- package/examples/environmental/aht21/minimal.js +12 -0
- package/examples/environmental/bme280/complete.js +34 -0
- package/examples/environmental/bme280/demo.js +67 -0
- package/examples/environmental/bme280/minimal.js +18 -0
- package/examples/environmental/bme680/complete.js +37 -0
- package/examples/environmental/bme680/demo.js +52 -0
- package/examples/environmental/bme680/minimal.js +19 -0
- package/examples/gas/ens160/complete.js +52 -0
- package/examples/gas/ens160/demo.js +52 -0
- package/examples/gas/ens160/minimal.js +27 -0
- package/examples/imu/mpu6050/complete.js +56 -0
- package/examples/imu/mpu6050/demo.js +34 -0
- package/examples/imu/mpu6050/minimal.js +14 -0
- package/examples/light/apds9960/complete.js +62 -0
- package/examples/light/apds9960/demo.js +32 -0
- package/examples/light/apds9960/minimal.js +12 -0
- package/examples/memory/24aa02uid/complete.js +40 -0
- package/examples/memory/24aa02uid/demo.js +40 -0
- package/examples/memory/24aa02uid/minimal.js +12 -0
- package/examples/pressure/bmp280/complete.js +22 -18
- package/examples/pressure/bmp280/demo.js +34 -18
- package/examples/pressure/bmp280/minimal.js +4 -4
- package/package.json +1 -1
- package/src/chips/adc_dac/hx711.js +165 -0
- package/src/chips/adc_dac/mcp4728.js +284 -0
- package/src/chips/display/pcf8576.js +223 -0
- package/src/chips/environmental/aht21.js +170 -0
- package/src/chips/environmental/bme280.js +366 -0
- package/src/chips/environmental/bme680.js +489 -0
- package/src/chips/gas/ens160.js +280 -0
- package/src/chips/imu/mpu6050.js +266 -0
- package/src/chips/light/apds9960.js +464 -0
- package/src/chips/memory/_24aa02uid.js +179 -0
- package/src/chips/pressure/bmp280.js +154 -191
- package/src/transport/hx711.js +105 -0
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.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/imu/.gitkeep +0 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/pressure/.gitkeep +0 -0
|
@@ -6,29 +6,45 @@ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
|
6
6
|
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
|
|
7
7
|
|
|
8
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
9
|
|
|
11
|
-
|
|
10
|
+
// --- Weather monitoring preset: lowest power, forced mode ---
|
|
11
|
+
// BMP280 datasheet Table 7: ×1/×1, filter off, forced mode.
|
|
12
|
+
// One sample per second for 30 seconds.
|
|
13
|
+
const bmp = new BMP280Full(transport); // Create BMP280 driver, (transport, busType='i2c')
|
|
14
|
+
bmp.configure(BMP280Full.OSRS_X1, BMP280Full.OSRS_X1, BMP280Full.MODE_FORCED, BMP280Full.FILTER_OFF, BMP280Full.T_SB_0_5_MS); // Configure chip, (osrsT=×1, osrsP=×1, mode=forced, filter=off, tSb=0) → undefined
|
|
12
15
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
bmp.
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
const temps = [], pressures = [], alts = [];
|
|
17
|
+
for (let n = 0; n < 30; n++) {
|
|
18
|
+
const t = bmp.temperature(); // Read temperature, () → number °C
|
|
19
|
+
const p = bmp.pressure(); // Read pressure, () → number hPa
|
|
20
|
+
const a = bmp.altitude(); // Compute altitude, (seaLevelHpa=1013.25) → number m
|
|
21
|
+
temps.push(t);
|
|
22
|
+
pressures.push(p);
|
|
23
|
+
alts.push(a);
|
|
24
|
+
console.log(`${n}s: ${t.toFixed(1)} C, ${p.toFixed(1)} hPa, alt=${a.toFixed(1)} m`);
|
|
25
|
+
}
|
|
18
26
|
|
|
19
|
-
|
|
27
|
+
const avgT = temps.reduce((s, v) => s + v, 0) / temps.length;
|
|
28
|
+
const avgP = pressures.reduce((s, v) => s + v, 0) / pressures.length;
|
|
29
|
+
console.log(`Weather: T=${Math.min(...temps).toFixed(1)}/${avgT.toFixed(1)}/${Math.max(...temps).toFixed(1)} C, P=${Math.min(...pressures).toFixed(1)}/${avgP.toFixed(1)}/${Math.max(...pressures).toFixed(1)} hPa`);
|
|
20
30
|
|
|
31
|
+
// --- Indoor navigation preset: high resolution with IIR filter ---
|
|
32
|
+
// ×16/×2, filter coefficient 16, normal mode at ~26 Hz.
|
|
33
|
+
bmp.configure(BMP280Full.OSRS_X2, BMP280Full.OSRS_X16, BMP280Full.MODE_NORMAL, BMP280Full.FILTER_16, BMP280Full.T_SB_0_5_MS); // Configure chip, (osrsT=×2, osrsP=×16, mode=normal, filter=16, tSb=0.5ms) → undefined
|
|
34
|
+
|
|
35
|
+
const alts2 = [];
|
|
21
36
|
for (let n = 0; n < 30; n++) {
|
|
22
|
-
const t = bmp.temperature();
|
|
23
|
-
const p = bmp.pressure();
|
|
24
|
-
const a = bmp.altitude();
|
|
25
|
-
|
|
26
|
-
console.log(`${
|
|
27
|
-
if (n < 29) {
|
|
28
|
-
const waitMs = 1000 - (Date.now() - startMs) % 1000;
|
|
29
|
-
if (waitMs > 0) setTimeout(() => {}, waitMs);
|
|
30
|
-
}
|
|
37
|
+
const t = bmp.temperature(); // Read temperature, () → number °C
|
|
38
|
+
const p = bmp.pressure(); // Read pressure, () → number hPa
|
|
39
|
+
const a = bmp.altitude(); // Compute altitude, (seaLevelHpa=1013.25) → number m
|
|
40
|
+
alts2.push(a);
|
|
41
|
+
console.log(`${n}s: alt=${a.toFixed(4)} m`);
|
|
31
42
|
}
|
|
32
43
|
|
|
44
|
+
const meanAlt = alts2.reduce((s, v) => s + v, 0) / alts2.length;
|
|
45
|
+
const variance = alts2.reduce((s, v) => s + (v - meanAlt) ** 2, 0) / alts2.length;
|
|
46
|
+
const std = Math.sqrt(variance);
|
|
47
|
+
console.log(`Navigation: alt min=${Math.min(...alts2).toFixed(4)} max=${Math.max(...alts2).toFixed(4)} std=${std.toFixed(4)} m`);
|
|
48
|
+
|
|
33
49
|
transport.close();
|
|
34
|
-
console.log('===DONE: 0 passed, 0 failed===');
|
|
50
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
@@ -6,12 +6,12 @@ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
|
6
6
|
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
|
|
7
7
|
|
|
8
8
|
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
9
|
-
const bmp = new BMP280Minimal(transport);
|
|
9
|
+
const bmp = new BMP280Minimal(transport); // Create BMP280 driver, (transport, busType='i2c')
|
|
10
10
|
|
|
11
11
|
for (let i = 0; i < 5; i++) {
|
|
12
|
-
const t = bmp.temperature();
|
|
13
|
-
const p = bmp.pressure();
|
|
12
|
+
const t = bmp.temperature(); // Read temperature, () → number °C
|
|
13
|
+
const p = bmp.pressure(); // Read pressure, () → number hPa
|
|
14
14
|
console.log(`${t.toFixed(1)} C, ${p.toFixed(1)} hPa`);
|
|
15
15
|
}
|
|
16
16
|
transport.close();
|
|
17
|
-
console.log('===DONE: 0 passed, 0 failed===');
|
|
17
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
package/package.json
CHANGED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const GAIN_TO_PULSES = { 128: 25, 32: 26, 64: 27 };
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* HX711 24-bit ADC — minimal interface.
|
|
7
|
+
*
|
|
8
|
+
* Reads signed 24-bit ADC values using Channel A, Gain 128. No configuration
|
|
9
|
+
* beyond the transport is required. The first post-power-up conversion is
|
|
10
|
+
* discarded during construction.
|
|
11
|
+
*/
|
|
12
|
+
class HX711Minimal {
|
|
13
|
+
/**
|
|
14
|
+
* @param {object} transport - Configured HX711 transport (HX711Transport).
|
|
15
|
+
*/
|
|
16
|
+
constructor(transport) {
|
|
17
|
+
this._transport = transport;
|
|
18
|
+
this._transport.readRaw(25);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Return true if a conversion result is available (DOUT is LOW).
|
|
23
|
+
*
|
|
24
|
+
* Non-blocking.
|
|
25
|
+
*
|
|
26
|
+
* @returns {boolean} True when DOUT is LOW (data ready).
|
|
27
|
+
*/
|
|
28
|
+
isReady() {
|
|
29
|
+
return this._transport.isReady();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Block until data is ready and return a signed 24-bit ADC value.
|
|
34
|
+
*
|
|
35
|
+
* Reads Channel A at Gain 128.
|
|
36
|
+
*
|
|
37
|
+
* @returns {number} Signed 24-bit ADC value (-8 388 608 to +8 388 607).
|
|
38
|
+
*/
|
|
39
|
+
readRaw() {
|
|
40
|
+
return this._transport.readRaw(25);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* HX711 full interface — extends HX711Minimal with gain, tare, and calibration.
|
|
46
|
+
*
|
|
47
|
+
* Adds gain selection (Channel A Gain 128/64, Channel B Gain 32), multi-sample
|
|
48
|
+
* averaging, tare offset capture, scale factor calibration, and power management.
|
|
49
|
+
*/
|
|
50
|
+
class HX711Full extends HX711Minimal {
|
|
51
|
+
/**
|
|
52
|
+
* @param {object} transport - Configured HX711 transport (HX711Transport).
|
|
53
|
+
*/
|
|
54
|
+
constructor(transport) {
|
|
55
|
+
super(transport);
|
|
56
|
+
this._pulses = 25;
|
|
57
|
+
this._offset = 0;
|
|
58
|
+
this._scale = 1.0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Block until data is ready and return a signed 24-bit ADC value.
|
|
63
|
+
*
|
|
64
|
+
* Uses the currently selected channel and gain.
|
|
65
|
+
*
|
|
66
|
+
* @returns {number} Signed 24-bit ADC value.
|
|
67
|
+
*/
|
|
68
|
+
readRaw() {
|
|
69
|
+
return this._transport.readRaw(this._pulses);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Select the input channel and gain.
|
|
74
|
+
*
|
|
75
|
+
* Issues one dummy read to apply the new gain before returning.
|
|
76
|
+
*
|
|
77
|
+
* @param {number} gain - 128 (Channel A), 64 (Channel A), or 32 (Channel B).
|
|
78
|
+
* @throws {Error} If gain is not 128, 64, or 32.
|
|
79
|
+
*/
|
|
80
|
+
setGain(gain) {
|
|
81
|
+
if (!(gain in GAIN_TO_PULSES))
|
|
82
|
+
throw new Error('gain must be 128, 64, or 32');
|
|
83
|
+
this._pulses = GAIN_TO_PULSES[gain];
|
|
84
|
+
this._transport.readRaw(this._pulses);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Return the average of multiple raw ADC readings.
|
|
89
|
+
* @param {number} times - Number of readings to average (default 10).
|
|
90
|
+
* @returns {number} Average signed 24-bit ADC value.
|
|
91
|
+
*/
|
|
92
|
+
readAverage(times = 10) {
|
|
93
|
+
let total = 0;
|
|
94
|
+
for (let i = 0; i < times; i++)
|
|
95
|
+
total += this.readRaw();
|
|
96
|
+
return Math.trunc(total / times);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Capture the current average reading as the zero offset.
|
|
101
|
+
* @param {number} times - Number of readings to average (default 10).
|
|
102
|
+
*/
|
|
103
|
+
tare(times = 10) {
|
|
104
|
+
this._offset = this.readAverage(times);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Return the stored tare offset.
|
|
109
|
+
* @returns {number} Offset captured by the last tare() call.
|
|
110
|
+
*/
|
|
111
|
+
getOffset() {
|
|
112
|
+
return this._offset;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Set the calibration scale factor.
|
|
117
|
+
*
|
|
118
|
+
* Calibrate: factor = (readAverage() - getOffset()) / known_weight.
|
|
119
|
+
*
|
|
120
|
+
* @param {number} factor - Scale factor (ADC counts per unit weight).
|
|
121
|
+
*/
|
|
122
|
+
setScale(factor) {
|
|
123
|
+
this._scale = factor;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Return the current calibration scale factor.
|
|
128
|
+
* @returns {number} Scale factor set by the last setScale() call.
|
|
129
|
+
*/
|
|
130
|
+
getScale() {
|
|
131
|
+
return this._scale;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Return the calibrated weight in the units defined by the scale factor.
|
|
136
|
+
*
|
|
137
|
+
* Computes (readAverage(times) - offset) / scale.
|
|
138
|
+
*
|
|
139
|
+
* @param {number} times - Number of readings to average (default 1).
|
|
140
|
+
* @returns {number} Calibrated weight value.
|
|
141
|
+
*/
|
|
142
|
+
readWeight(times = 1) {
|
|
143
|
+
return (this.readAverage(times) - this._offset) / this._scale;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Enter power-down mode (PD_SCK held HIGH for >60 µs).
|
|
148
|
+
*/
|
|
149
|
+
powerDown() {
|
|
150
|
+
this._transport.powerDown();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Exit power-down, reset chip, discard settling conversion.
|
|
155
|
+
*
|
|
156
|
+
* Resets to Channel A, Gain 128 and discards the first post-reset conversion.
|
|
157
|
+
*/
|
|
158
|
+
powerUp() {
|
|
159
|
+
this._transport.powerUp();
|
|
160
|
+
this._pulses = 25;
|
|
161
|
+
this._transport.readRaw(25);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
module.exports = { HX711Minimal, HX711Full };
|
|
@@ -0,0 +1,284 @@
|
|
|
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 transport. 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 {object} transport - Configured I²C transport pointing at the device (0x60–0x67).
|
|
24
|
+
*/
|
|
25
|
+
class MCP4728Minimal {
|
|
26
|
+
/**
|
|
27
|
+
* @param {object} transport - Configured I²C transport pointing at the device.
|
|
28
|
+
*/
|
|
29
|
+
constructor(transport) {
|
|
30
|
+
this._transport = transport;
|
|
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
|
+
*/
|
|
38
|
+
set_voltage(channel, fraction) {
|
|
39
|
+
let f = Math.max(0.0, Math.min(1.0, fraction));
|
|
40
|
+
const code = Math.round(f * 4095);
|
|
41
|
+
this.set_raw(channel, code);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Set one channel's raw 12-bit DAC code.
|
|
46
|
+
* @param {number} channel - Channel index 0 (A) – 3 (D).
|
|
47
|
+
* @param {number} code - Raw 12-bit DAC code (0–4095).
|
|
48
|
+
*/
|
|
49
|
+
set_raw(channel, code) {
|
|
50
|
+
const ch = Math.max(0, Math.min(3, channel | 0));
|
|
51
|
+
const c = Math.max(0, Math.min(4095, code | 0));
|
|
52
|
+
this._multi_write(ch, c, 0, 0, 0, 0);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Update all four channels simultaneously using Fast Write.
|
|
57
|
+
* @param {number[]} fractions - Array of 4 fractions (0.0–1.0), index 0 = A.
|
|
58
|
+
*/
|
|
59
|
+
set_all(fractions) {
|
|
60
|
+
if (!Array.isArray(fractions) || fractions.length !== 4) {
|
|
61
|
+
throw new Error('fractions must have exactly 4 elements');
|
|
62
|
+
}
|
|
63
|
+
const buf = Buffer.alloc(8);
|
|
64
|
+
for (let i = 0; i < 4; i++) {
|
|
65
|
+
let f = Math.max(0.0, Math.min(1.0, fractions[i]));
|
|
66
|
+
const code = Math.max(0, Math.min(4095, Math.round(f * 4095)));
|
|
67
|
+
// Fast Write: byte1=[0 0 PD1 PD0 D11-D8] (PD=00), byte2=[D7-D0]
|
|
68
|
+
buf[i * 2] = (code >> 8) & 0x0F;
|
|
69
|
+
buf[i * 2 + 1] = code & 0xFF;
|
|
70
|
+
}
|
|
71
|
+
this._transport.write(buf);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
_multi_write(channel, code, vref, pd, gain, udac) {
|
|
75
|
+
const buf = Buffer.alloc(3);
|
|
76
|
+
buf[0] = CMD_MULTI_WRITE_BASE | ((channel & 0x03) << 1) | (udac & 0x01);
|
|
77
|
+
buf[1] = ((vref & 0x01) << 7) | ((pd & 0x03) << 5) |
|
|
78
|
+
((gain & 0x01) << 4) | ((code >> 8) & 0x0F);
|
|
79
|
+
buf[2] = code & 0xFF;
|
|
80
|
+
this._transport.write(buf);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* MCP4728 full interface — extends MCP4728Minimal with EEPROM, V_REF, gain, power-down, and read-back.
|
|
86
|
+
*
|
|
87
|
+
* Adds per-channel V_REF and gain configuration, all-channel V_REF/gain/
|
|
88
|
+
* power-down commands, write-with-EEPROM persistence (Single and Sequential
|
|
89
|
+
* Write), General Call reset/wake-up/software-update, and full 24-byte
|
|
90
|
+
* read-back of all channel DAC input registers and EEPROM contents.
|
|
91
|
+
*
|
|
92
|
+
* @param {object} transport - Configured I²C transport pointing at the device (0x60–0x67).
|
|
93
|
+
*/
|
|
94
|
+
class MCP4728Full extends MCP4728Minimal {
|
|
95
|
+
/** @type {number} Normal operation (power-down mode 0). */
|
|
96
|
+
static PD_NORMAL = 0;
|
|
97
|
+
/** @type {number} Power-down with 1 kΩ to GND (mode 1). */
|
|
98
|
+
static PD_1K_GND = 1;
|
|
99
|
+
/** @type {number} Power-down with 100 kΩ to GND (mode 2). */
|
|
100
|
+
static PD_100K_GND = 2;
|
|
101
|
+
/** @type {number} Power-down with 500 kΩ to GND (mode 3). */
|
|
102
|
+
static PD_500K_GND = 3;
|
|
103
|
+
|
|
104
|
+
/** @type {number} External V_DD reference. */
|
|
105
|
+
static VREF_EXTERNAL = 0;
|
|
106
|
+
/** @type {number} Internal 2.048 V reference. */
|
|
107
|
+
static VREF_INTERNAL = 1;
|
|
108
|
+
|
|
109
|
+
/** @type {number} Gain ×1. */
|
|
110
|
+
static GAIN_X1 = 0;
|
|
111
|
+
/** @type {number} Gain ×2. */
|
|
112
|
+
static GAIN_X2 = 1;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @param {object} transport - Configured I²C transport pointing at the device.
|
|
116
|
+
*/
|
|
117
|
+
constructor(transport) {
|
|
118
|
+
super(transport);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Set one channel's output and persist to EEPROM (Single Write).
|
|
123
|
+
* @param {number} channel - Channel index 0 (A) – 3 (D).
|
|
124
|
+
* @param {number} fraction - Output as a fraction of the configured full-scale.
|
|
125
|
+
* @param {number} vref - 0 = external (V_DD), 1 = internal (2.048 V).
|
|
126
|
+
* @param {number} gain - 1 = ×1, 2 = ×2 (ignored when vref = external).
|
|
127
|
+
*/
|
|
128
|
+
set_voltage_eeprom(channel, fraction, vref, gain) {
|
|
129
|
+
let f = Math.max(0.0, Math.min(1.0, fraction));
|
|
130
|
+
const code = Math.round(f * 4095);
|
|
131
|
+
this._single_write(channel, code, vref, 0, gain, 0);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Set one channel's raw 12-bit code and persist to EEPROM.
|
|
136
|
+
* @param {number} channel - Channel index 0 (A) – 3 (D).
|
|
137
|
+
* @param {number} code - Raw 12-bit DAC code (0–4095).
|
|
138
|
+
* @param {number} vref - 0 = external (V_DD), 1 = internal (2.048 V).
|
|
139
|
+
* @param {number} gain - 1 = ×1, 2 = ×2 (ignored when vref = external).
|
|
140
|
+
*/
|
|
141
|
+
set_raw_eeprom(channel, code, vref, gain) {
|
|
142
|
+
const c = Math.max(0, Math.min(4095, code | 0));
|
|
143
|
+
this._single_write(channel, c, vref, 0, gain, 0);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Update all four channels and EEPROM (Sequential Write from A to D).
|
|
148
|
+
* @param {number[]} fractions - Array of 4 fractions (0.0–1.0).
|
|
149
|
+
* @param {number[]} vrefs - Array of 4 V_REF values (0/1).
|
|
150
|
+
* @param {number[]} gains - Array of 4 gain values (1/2).
|
|
151
|
+
*/
|
|
152
|
+
set_all_eeprom(fractions, vrefs, gains) {
|
|
153
|
+
if (fractions.length !== 4 || vrefs.length !== 4 || gains.length !== 4) {
|
|
154
|
+
throw new Error('fractions, vrefs, gains must each have 4 elements');
|
|
155
|
+
}
|
|
156
|
+
const buf = Buffer.alloc(9);
|
|
157
|
+
// Sequential Write starting at channel 0 (A): [0 1 0 1 0 0 0 UDAC] = 0x50
|
|
158
|
+
buf[0] = CMD_SEQUENTIAL_BASE | 0x00;
|
|
159
|
+
for (let i = 0; i < 4; i++) {
|
|
160
|
+
let f = Math.max(0.0, Math.min(1.0, fractions[i]));
|
|
161
|
+
const code = Math.max(0, Math.min(4095, Math.round(f * 4095)));
|
|
162
|
+
const v = vrefs[i] ? 1 : 0;
|
|
163
|
+
const g = (gains[i] === 2) ? 1 : 0;
|
|
164
|
+
// Per-channel byte layout (Multi-Write format): [V_REF PD1 PD0 Gx D11-D8]
|
|
165
|
+
// PD bits are always 0 here (no power-down is set in this command).
|
|
166
|
+
buf[1 + i * 2] = ((v & 0x01) << 7) | ((g & 0x01) << 4) | ((code >> 8) & 0x0F);
|
|
167
|
+
buf[1 + i * 2 + 1] = code & 0xFF;
|
|
168
|
+
}
|
|
169
|
+
this._transport.write(buf);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Set V_REF for all four channels (volatile register only).
|
|
174
|
+
* @param {number} vref_a - 0 = external V_DD, 1 = internal 2.048 V.
|
|
175
|
+
* @param {number} vref_b
|
|
176
|
+
* @param {number} vref_c
|
|
177
|
+
* @param {number} vref_d
|
|
178
|
+
*/
|
|
179
|
+
set_vref(vref_a, vref_b, vref_c, vref_d) {
|
|
180
|
+
const byte1 = CMD_WRITE_VREF |
|
|
181
|
+
((vref_a ? 1 : 0) << 3) | ((vref_b ? 1 : 0) << 2) |
|
|
182
|
+
((vref_c ? 1 : 0) << 1) | (vref_d ? 1 : 0);
|
|
183
|
+
this._transport.write(Buffer.from([byte1]));
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Set gain for all four channels (volatile register only).
|
|
188
|
+
* @param {number} gain_a - 1 = ×1, 2 = ×2.
|
|
189
|
+
* @param {number} gain_b
|
|
190
|
+
* @param {number} gain_c
|
|
191
|
+
* @param {number} gain_d
|
|
192
|
+
*/
|
|
193
|
+
set_gain(gain_a, gain_b, gain_c, gain_d) {
|
|
194
|
+
const byte1 = CMD_WRITE_GAIN |
|
|
195
|
+
((gain_a === 2 ? 1 : 0) << 3) | ((gain_b === 2 ? 1 : 0) << 2) |
|
|
196
|
+
((gain_c === 2 ? 1 : 0) << 1) | (gain_d === 2 ? 1 : 0);
|
|
197
|
+
this._transport.write(Buffer.from([byte1]));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Set power-down mode for all four channels (volatile register only).
|
|
202
|
+
* @param {number} pd_a - 0–3.
|
|
203
|
+
* @param {number} pd_b
|
|
204
|
+
* @param {number} pd_c
|
|
205
|
+
* @param {number} pd_d
|
|
206
|
+
*/
|
|
207
|
+
set_power_down(pd_a, pd_b, pd_c, pd_d) {
|
|
208
|
+
const byte1 = CMD_WRITE_POWERDOWN |
|
|
209
|
+
(((pd_a >> 1) & 0x01) << 4) | ((pd_a & 0x01) << 3) |
|
|
210
|
+
(((pd_b >> 1) & 0x01) << 2) | ((pd_b & 0x01) << 1);
|
|
211
|
+
const byte2 = (((pd_c >> 1) & 0x01) << 6) | ((pd_c & 0x01) << 5) |
|
|
212
|
+
(((pd_d >> 1) & 0x01) << 4) | ((pd_d & 0x01) << 3);
|
|
213
|
+
this._transport.write(Buffer.from([byte1, byte2]));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Read all four channels' DAC input registers and EEPROM contents.
|
|
218
|
+
* @returns {{channel: object[], eeprom_ready: boolean}} Per-channel code, vref, gain, power_down, eeprom_*.
|
|
219
|
+
*/
|
|
220
|
+
read() {
|
|
221
|
+
const buf = this._transport.read(24);
|
|
222
|
+
const result = { channel: [], eeprom_ready: !!(buf[0] & 0x80) };
|
|
223
|
+
for (let i = 0; i < 4; i++) {
|
|
224
|
+
const b = i * 3;
|
|
225
|
+
result.channel.push({
|
|
226
|
+
code: ((buf[b + 1] & 0x0F) << 8) | buf[b + 2],
|
|
227
|
+
vref: (buf[b + 1] >> 7) & 0x01,
|
|
228
|
+
gain: ((buf[b + 1] >> 4) & 0x01) ? 2 : 1,
|
|
229
|
+
power_down: (buf[b + 1] >> 5) & 0x03,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
for (let i = 0; i < 4; i++) {
|
|
233
|
+
const b = 12 + i * 3;
|
|
234
|
+
result.channel[i].eeprom_code = ((buf[b + 1] & 0x0F) << 8) | buf[b + 2];
|
|
235
|
+
result.channel[i].eeprom_vref = (buf[b + 1] >> 7) & 0x01;
|
|
236
|
+
result.channel[i].eeprom_gain = ((buf[b + 1] >> 4) & 0x01) ? 2 : 1;
|
|
237
|
+
result.channel[i].eeprom_power_down = (buf[b + 1] >> 5) & 0x03;
|
|
238
|
+
}
|
|
239
|
+
return result;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Check if the EEPROM write is complete (RDY/BSY = 1).
|
|
244
|
+
* @returns {boolean}
|
|
245
|
+
*/
|
|
246
|
+
is_eeprom_ready() {
|
|
247
|
+
const buf = this._transport.read(1);
|
|
248
|
+
return !!(buf[0] & 0x80);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Send General Call Software Update (0x00, 0x08) to latch all V_OUT.
|
|
253
|
+
*/
|
|
254
|
+
software_update() {
|
|
255
|
+
this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_SOFTWARE_UPD]));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Send General Call Wake-Up (0x00, 0x09) to clear all PD bits.
|
|
260
|
+
*/
|
|
261
|
+
wake_up() {
|
|
262
|
+
this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_WAKE]));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Send General Call Reset (0x00, 0x06) to reload EEPROM into all DAC registers.
|
|
267
|
+
*/
|
|
268
|
+
reset() {
|
|
269
|
+
this._transport.write(Buffer.from([ADDR_GENERAL_CALL, GC_RESET]));
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
_single_write(channel, code, vref, pd, gain, udac) {
|
|
273
|
+
const ch = Math.max(0, Math.min(3, channel | 0));
|
|
274
|
+
const c = Math.max(0, Math.min(4095, code | 0));
|
|
275
|
+
const buf = Buffer.alloc(3);
|
|
276
|
+
buf[0] = CMD_SINGLE_WRITE | ((ch & 0x03) << 1) | (udac & 0x01);
|
|
277
|
+
buf[1] = ((vref & 0x01) << 7) | ((pd & 0x03) << 5) |
|
|
278
|
+
((gain & 0x01) << 4) | ((c >> 8) & 0x0F);
|
|
279
|
+
buf[2] = c & 0xFF;
|
|
280
|
+
this._transport.write(buf);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
module.exports = { MCP4728Minimal, MCP4728Full };
|