periph 1.1.0 → 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 +7 -7
- package/examples/adc_dac/hx711/demo.js +6 -6
- package/examples/adc_dac/hx711/minimal.js +7 -7
- 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 +24 -22
- package/examples/adc_dac/mcp4728/demo.js +22 -22
- package/examples/adc_dac/mcp4728/minimal.js +8 -6
- 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 +18 -15
- package/examples/display/pcf8576/demo.js +6 -6
- package/examples/display/pcf8576/minimal.js +12 -10
- package/examples/environmental/aht21/complete.js +15 -13
- package/examples/environmental/aht21/demo.js +8 -6
- package/examples/environmental/aht21/minimal.js +5 -5
- package/examples/environmental/bme280/complete.js +23 -20
- package/examples/environmental/bme280/demo.js +14 -14
- package/examples/environmental/bme280/minimal.js +13 -11
- package/examples/environmental/bme680/complete.js +25 -22
- package/examples/environmental/bme680/demo.js +38 -36
- package/examples/environmental/bme680/minimal.js +14 -12
- package/examples/gas/ens160/complete.js +29 -27
- package/examples/gas/ens160/demo.js +39 -37
- package/examples/gas/ens160/minimal.js +17 -15
- 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 +27 -25
- package/examples/imu/mpu6050/demo.js +29 -26
- package/examples/imu/mpu6050/minimal.js +6 -6
- 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 +45 -43
- package/examples/light/apds9960/demo.js +11 -9
- package/examples/light/apds9960/minimal.js +5 -5
- 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 +23 -21
- package/examples/memory/24aa02uid/demo.js +32 -30
- package/examples/memory/24aa02uid/minimal.js +5 -5
- 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 +23 -20
- package/examples/pressure/bmp280/demo.js +44 -41
- 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 +15 -15
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +57 -44
- 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 +42 -33
- package/src/chips/environmental/aht21.js +52 -37
- package/src/chips/environmental/bme280.js +85 -66
- package/src/chips/environmental/bme680.js +110 -87
- package/src/chips/gas/ens160.js +82 -64
- 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 +89 -66
- 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 +158 -123
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +32 -29
- 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 +72 -53
- 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/{transport → connection}/hx711.js +49 -22
- 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/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Default } = require('opengpio');
|
|
4
|
+
const { HX711Connection } = require('../../../src/connection/hx711');
|
|
5
|
+
const { HX710AFull } = require('../../../src/chips/adc_dac/hx710a');
|
|
6
|
+
|
|
7
|
+
// Temperature-monitored load cell demo: tare at startup, then print weight
|
|
8
|
+
// continuously, sampling the on-chip temperature sensor periodically.
|
|
9
|
+
// Replace SCALE_FACTOR with the value calibrated for your load cell and V_DD.
|
|
10
|
+
// Calibration: (1) call tare() with nothing on the scale; (2) place a known
|
|
11
|
+
// 100 g reference weight; (3) SCALE_FACTOR = (readAverage() - getOffset()) / 100.
|
|
12
|
+
const SCALE_FACTOR = 420.0;
|
|
13
|
+
|
|
14
|
+
const dout = Default.input({ chip: 0, line: 5 });
|
|
15
|
+
const pd_sck = Default.output({ chip: 0, line: 6 });
|
|
16
|
+
const connection = new HX711Connection(dout, pd_sck); // Create HX711 transport connection, (dout, pd_sck)
|
|
17
|
+
const chip = new HX710AFull(connection); // Create HX710A driver — discards first conversion, (connection)
|
|
18
|
+
|
|
19
|
+
// --- Tare the scale before use ---
|
|
20
|
+
// Averaging 10 readings with nothing on the scale suppresses noise in the
|
|
21
|
+
// zero-offset capture, so later weight readings aren't skewed by drift.
|
|
22
|
+
console.log('Taring — keep scale empty...');
|
|
23
|
+
chip.tare(10); // Capture zero offset from 10-reading average, (times=10) → undefined
|
|
24
|
+
chip.setScale(SCALE_FACTOR); // Set calibration scale factor, (factor: number) → undefined
|
|
25
|
+
console.log('Tare done. Place weight on scale.');
|
|
26
|
+
|
|
27
|
+
let prevWeight = null;
|
|
28
|
+
let iteration = 0;
|
|
29
|
+
|
|
30
|
+
function poll() {
|
|
31
|
+
const weight = chip.readWeight(3); // Return calibrated weight, (times=3) → number
|
|
32
|
+
const rounded = Math.round(weight * 10) / 10;
|
|
33
|
+
if (prevWeight === null || Math.abs(rounded - prevWeight) > 1.0) {
|
|
34
|
+
console.log(`-> ${rounded.toFixed(1)} g`);
|
|
35
|
+
prevWeight = rounded;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (iteration % 10 === 0) {
|
|
39
|
+
// --- Sample the on-chip temperature sensor every ~5 s ---
|
|
40
|
+
// This is an uncalibrated raw ADC code (~20.4 LSB/°C, chip-to-chip
|
|
41
|
+
// offset/gain vary per the datasheet), intended only for the
|
|
42
|
+
// datasheet's stated purpose of relative drift compensation of the
|
|
43
|
+
// weight reading — not as an absolute °C measurement.
|
|
44
|
+
const tempRaw = chip.readTemperatureRaw(); // Read raw on-chip temperature code, () → number
|
|
45
|
+
console.log(`-> ${rounded.toFixed(1)} g (temp raw=${tempRaw})`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
iteration++;
|
|
49
|
+
setTimeout(poll, 500);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
poll();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Default } = require('opengpio');
|
|
4
|
+
const { HX711Connection } = require('../../../src/connection/hx711');
|
|
5
|
+
const { HX710AMinimal } = require('../../../src/chips/adc_dac/hx710a');
|
|
6
|
+
|
|
7
|
+
const dout = Default.input({ chip: 0, line: 5 }); // Configure DOUT as input, ({chip, line}) → Input
|
|
8
|
+
const pd_sck = Default.output({ chip: 0, line: 6 }); // Configure PD_SCK as output, ({chip, line}) → Output
|
|
9
|
+
const connection = new HX711Connection(dout, pd_sck); // Create HX711 transport connection, (dout, pd_sck)
|
|
10
|
+
const chip = new HX710AMinimal(connection); // Create HX710A driver — discards first conversion, (connection)
|
|
11
|
+
|
|
12
|
+
const ready = chip.isReady(); // Check if conversion is ready (non-blocking), () → boolean
|
|
13
|
+
const raw = chip.readRaw(); // Read signed 24-bit differential-input value, () → number
|
|
14
|
+
console.log(raw);
|
|
15
|
+
|
|
16
|
+
connection.close();
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Default } = require('opengpio');
|
|
4
|
+
const { HX711Connection } = require('../../../src/connection/hx711');
|
|
5
|
+
const { HX710BFull } = require('../../../src/chips/adc_dac/hx710b');
|
|
6
|
+
|
|
7
|
+
const dout = Default.input({ chip: 0, line: 5 }); // Configure DOUT as input, ({chip, line}) → Input
|
|
8
|
+
const pd_sck = Default.output({ chip: 0, line: 6 }); // Configure PD_SCK as output, ({chip, line}) → Output
|
|
9
|
+
const connection = new HX711Connection(dout, pd_sck); // Create HX711 transport connection, (dout, pd_sck)
|
|
10
|
+
const chip = new HX710BFull(connection); // Create HX710B driver — discards first conversion, (connection)
|
|
11
|
+
|
|
12
|
+
const ready = chip.isReady(); // Check if conversion is ready (non-blocking), () → boolean
|
|
13
|
+
// returns true when DOUT is LOW
|
|
14
|
+
const raw = chip.readRaw(); // Read signed 24-bit differential-input value, () → number
|
|
15
|
+
// blocks until DOUT goes LOW, then clocks out 24 bits
|
|
16
|
+
|
|
17
|
+
chip.setRate(40); // Select differential-input output rate, (rate: 10|40) → undefined
|
|
18
|
+
// takes effect after next read; issues dummy read to apply
|
|
19
|
+
chip.setRate(10); // (restores default 10 SPS)
|
|
20
|
+
|
|
21
|
+
const avg = chip.readAverage(10); // Average multiple raw readings, (times=10) → number
|
|
22
|
+
// blocks for `times` complete conversions
|
|
23
|
+
|
|
24
|
+
chip.tare(10); // Capture zero offset from 10-reading average, (times=10) → undefined
|
|
25
|
+
// stores result in internal _offset; call with nothing on the scale
|
|
26
|
+
const offset = chip.getOffset(); // Return stored tare offset, () → number
|
|
27
|
+
|
|
28
|
+
chip.setScale(420.0); // Set calibration scale factor, (factor: number) → undefined
|
|
29
|
+
// factor = (readAverage() - offset) / known_weight_in_target_unit
|
|
30
|
+
const scale = chip.getScale(); // Return current scale factor, () → number
|
|
31
|
+
|
|
32
|
+
const weight = chip.readWeight(5); // Return calibrated weight, (times=1) → number
|
|
33
|
+
// computes (readAverage(times) - offset) / scale
|
|
34
|
+
console.log('weight:', weight);
|
|
35
|
+
|
|
36
|
+
const suppRaw = chip.readSupplyDiffRaw(); // Read raw DVDD−AVDD supply-difference code, () → number
|
|
37
|
+
// uncalibrated ADC code, no absolute LSB-to-volts scale
|
|
38
|
+
console.log('supply diff raw:', suppRaw);
|
|
39
|
+
|
|
40
|
+
chip.powerDown(); // Enter power-down mode, () → undefined
|
|
41
|
+
// holds PD_SCK HIGH for >60 µs
|
|
42
|
+
chip.powerUp(); // Exit power-down, reset chip, discard settling conversion, () → undefined
|
|
43
|
+
// resets to differential input, gain 128, 10 SPS
|
|
44
|
+
|
|
45
|
+
connection.close();
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Default } = require('opengpio');
|
|
4
|
+
const { HX711Connection } = require('../../../src/connection/hx711');
|
|
5
|
+
const { HX710BFull } = require('../../../src/chips/adc_dac/hx710b');
|
|
6
|
+
|
|
7
|
+
// Battery-powered load cell demo: tare at startup, then print weight
|
|
8
|
+
// continuously, watching the DVDD−AVDD supply-difference reading for
|
|
9
|
+
// drift that signals a low battery. Replace SCALE_FACTOR with the value
|
|
10
|
+
// calibrated for your load cell and wiring topology. Calibration: (1)
|
|
11
|
+
// call tare() with nothing on the scale; (2) place a known 100 g reference
|
|
12
|
+
// weight; (3) SCALE_FACTOR = (readAverage() - getOffset()) / 100.
|
|
13
|
+
const SCALE_FACTOR = 420.0;
|
|
14
|
+
const LOW_BATT_DELTA = 50000; // supply-diff drift threshold from baseline
|
|
15
|
+
|
|
16
|
+
const dout = Default.input({ chip: 0, line: 5 });
|
|
17
|
+
const pd_sck = Default.output({ chip: 0, line: 6 });
|
|
18
|
+
const connection = new HX711Connection(dout, pd_sck); // Create HX711 transport connection, (dout, pd_sck)
|
|
19
|
+
const chip = new HX710BFull(connection); // Create HX710B driver — discards first conversion, (connection)
|
|
20
|
+
|
|
21
|
+
// --- Tare the scale before use ---
|
|
22
|
+
// Averaging 10 readings with nothing on the scale suppresses noise in the
|
|
23
|
+
// zero-offset capture, so later weight readings aren't skewed by drift.
|
|
24
|
+
console.log('Taring — keep scale empty...');
|
|
25
|
+
chip.tare(10); // Capture zero offset from 10-reading average, (times=10) → undefined
|
|
26
|
+
chip.setScale(SCALE_FACTOR); // Set calibration scale factor, (factor: number) → undefined
|
|
27
|
+
console.log('Tare done. Place weight on scale.');
|
|
28
|
+
|
|
29
|
+
// --- Capture the supply-difference baseline at full charge ---
|
|
30
|
+
// The datasheet gives no absolute LSB-to-volts conversion for this
|
|
31
|
+
// channel — it is only useful for relative drift tracking. Capture one
|
|
32
|
+
// reading at startup as a "known-good battery" baseline, then compare
|
|
33
|
+
// later readings against it to detect discharge.
|
|
34
|
+
const baselineSuppDiff = chip.readSupplyDiffRaw(); // Read raw DVDD−AVDD supply-difference code, () → number
|
|
35
|
+
|
|
36
|
+
let prevWeight = null;
|
|
37
|
+
let iteration = 0;
|
|
38
|
+
|
|
39
|
+
function poll() {
|
|
40
|
+
const weight = chip.readWeight(3); // Return calibrated weight, (times=3) → number
|
|
41
|
+
const rounded = Math.round(weight * 10) / 10;
|
|
42
|
+
if (prevWeight === null || Math.abs(rounded - prevWeight) > 1.0) {
|
|
43
|
+
console.log(`-> ${rounded.toFixed(1)} g`);
|
|
44
|
+
prevWeight = rounded;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (iteration % 20 === 0) {
|
|
48
|
+
// --- Sample the DVDD−AVDD supply-difference channel every ~10 s ---
|
|
49
|
+
// Uncalibrated ADC code, intended only for relative drift tracking
|
|
50
|
+
// against a known-good baseline (the datasheet's stated purpose for
|
|
51
|
+
// this channel in battery-powered weigh-scale applications) — not
|
|
52
|
+
// an absolute voltage reading.
|
|
53
|
+
const suppDiff = chip.readSupplyDiffRaw(); // Read raw DVDD−AVDD supply-difference code, () → number
|
|
54
|
+
const drift = suppDiff - baselineSuppDiff;
|
|
55
|
+
if (Math.abs(drift) > LOW_BATT_DELTA) {
|
|
56
|
+
console.log(`LOW BATTERY (supply_diff=${suppDiff}, drift=${drift})`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
iteration++;
|
|
61
|
+
setTimeout(poll, 500);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
poll();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { Default } = require('opengpio');
|
|
4
|
+
const { HX711Connection } = require('../../../src/connection/hx711');
|
|
5
|
+
const { HX710BMinimal } = require('../../../src/chips/adc_dac/hx710b');
|
|
6
|
+
|
|
7
|
+
const dout = Default.input({ chip: 0, line: 5 }); // Configure DOUT as input, ({chip, line}) → Input
|
|
8
|
+
const pd_sck = Default.output({ chip: 0, line: 6 }); // Configure PD_SCK as output, ({chip, line}) → Output
|
|
9
|
+
const connection = new HX711Connection(dout, pd_sck); // Create HX711 transport connection, (dout, pd_sck)
|
|
10
|
+
const chip = new HX710BMinimal(connection); // Create HX710B driver — discards first conversion, (connection)
|
|
11
|
+
|
|
12
|
+
const ready = chip.isReady(); // Check if conversion is ready (non-blocking), () → boolean
|
|
13
|
+
const raw = chip.readRaw(); // Read signed 24-bit differential-input value, () → number
|
|
14
|
+
console.log(raw);
|
|
15
|
+
|
|
16
|
+
connection.close();
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const {
|
|
3
|
+
const { Default } = require('opengpio');
|
|
4
|
+
const { HX711Connection } = require('../../../src/connection/hx711');
|
|
5
5
|
const { HX711Full } = require('../../../src/chips/adc_dac/hx711');
|
|
6
6
|
|
|
7
|
-
const dout =
|
|
8
|
-
const pd_sck =
|
|
9
|
-
const
|
|
10
|
-
const chip = new HX711Full(
|
|
7
|
+
const dout = Default.input({ chip: 0, line: 5 }); // Configure DOUT as input, ({chip, line}) → Input
|
|
8
|
+
const pd_sck = Default.output({ chip: 0, line: 6 }); // Configure PD_SCK as output, ({chip, line}) → Output
|
|
9
|
+
const connection = new HX711Connection(dout, pd_sck); // Create HX711 connection, (dout, pd_sck)
|
|
10
|
+
const chip = new HX711Full(connection); // Create HX711 driver — discards first conversion, (connection)
|
|
11
11
|
|
|
12
12
|
const ready = chip.isReady(); // Check if conversion is ready (non-blocking), () → boolean
|
|
13
13
|
// returns true when DOUT is LOW
|
|
@@ -39,4 +39,4 @@ chip.powerDown(); // Enter power-down m
|
|
|
39
39
|
chip.powerUp(); // Exit power-down, reset chip, discard settling conversion, () → undefined
|
|
40
40
|
// resets to Channel A Gain 128; first post-reset conversion discarded internally
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
connection.close();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const {
|
|
3
|
+
const { Default } = require('opengpio');
|
|
4
|
+
const { HX711Connection } = require('../../../src/connection/hx711');
|
|
5
5
|
const { HX711Full } = require('../../../src/chips/adc_dac/hx711');
|
|
6
6
|
|
|
7
7
|
// Kitchen scale demo: tare at startup, then print weight continuously.
|
|
@@ -10,10 +10,10 @@ const { HX711Full } = require('../../../src/chips/adc_dac/hx711');
|
|
|
10
10
|
// weight W grams; (3) SCALE_FACTOR = (readAverage() - getOffset()) / W.
|
|
11
11
|
const SCALE_FACTOR = 420.0;
|
|
12
12
|
|
|
13
|
-
const dout =
|
|
14
|
-
const pd_sck =
|
|
15
|
-
const
|
|
16
|
-
const chip = new HX711Full(
|
|
13
|
+
const dout = Default.input({ chip: 0, line: 5 });
|
|
14
|
+
const pd_sck = Default.output({ chip: 0, line: 6 });
|
|
15
|
+
const connection = new HX711Connection(dout, pd_sck); // Create HX711 connection, (dout, pd_sck)
|
|
16
|
+
const chip = new HX711Full(connection); // Create HX711 driver — discards first conversion, (connection)
|
|
17
17
|
|
|
18
18
|
console.log('Taring — keep scale empty...');
|
|
19
19
|
chip.tare(10); // Capture zero offset from 10-reading average, (times=10) → undefined
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const {
|
|
3
|
+
const { Default } = require('opengpio');
|
|
4
|
+
const { HX711Connection } = require('../../../src/connection/hx711');
|
|
5
5
|
const { HX711Minimal } = require('../../../src/chips/adc_dac/hx711');
|
|
6
6
|
|
|
7
|
-
const dout =
|
|
8
|
-
const pd_sck =
|
|
9
|
-
const
|
|
10
|
-
const chip = new HX711Minimal(
|
|
7
|
+
const dout = Default.input({ chip: 0, line: 5 }); // Configure DOUT as input, ({chip, line}) → Input
|
|
8
|
+
const pd_sck = Default.output({ chip: 0, line: 6 }); // Configure PD_SCK as output, ({chip, line}) → Output
|
|
9
|
+
const connection = new HX711Connection(dout, pd_sck); // Create HX711 connection, (dout, pd_sck)
|
|
10
|
+
const chip = new HX711Minimal(connection); // Create HX711 driver — discards first conversion, (connection)
|
|
11
11
|
|
|
12
12
|
const ready = chip.isReady(); // Check if conversion is ready (non-blocking), () → boolean
|
|
13
13
|
const raw = chip.readRaw(); // Read signed 24-bit ADC value, () → number
|
|
14
14
|
console.log(raw);
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
connection.close();
|
|
@@ -1,34 +1,36 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
4
|
const { MCP4725Full } = require('../../../src/chips/adc_dac/mcp4725');
|
|
5
5
|
|
|
6
6
|
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
7
|
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 16);
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
const dac = new MCP4725Full(
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const dac = new MCP4725Full(connection);
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
(async () => {
|
|
13
|
+
await dac.set_voltage(0.75); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
|
|
13
14
|
// converts fraction to 12-bit code and issues Fast Write
|
|
14
|
-
dac.set_raw(3000);
|
|
15
|
+
await dac.set_raw(3000); // Set raw 12-bit code, (code=0–4095) → None
|
|
15
16
|
// clamps to [0, 4095] and writes DAC register only
|
|
16
|
-
dac.set_voltage_eeprom(0.5);
|
|
17
|
+
await dac.set_voltage_eeprom(0.5); // Set output and persist to EEPROM, (fraction=0.0–1.0) → None
|
|
17
18
|
// writes both DAC register and EEPROM for power-cycle persistence
|
|
18
|
-
dac.set_raw_eeprom(2048);
|
|
19
|
+
await dac.set_raw_eeprom(2048); // Set raw code and persist to EEPROM, (code=0–4095) → None
|
|
19
20
|
// writes both DAC register and EEPROM for power-cycle persistence
|
|
20
|
-
const state = dac.read();
|
|
21
|
+
const state = await dac.read(); // Read DAC and EEPROM registers, () → dict
|
|
21
22
|
// returns code, voltage_fraction, power_down, eeprom_code, eeprom_power_down, eeprom_ready
|
|
22
|
-
console.log('code:', state.code);
|
|
23
|
-
console.log('voltage_fraction:', state.voltage_fraction);
|
|
24
|
-
console.log('eeprom_ready:', state.eeprom_ready);
|
|
25
|
-
dac.set_power_down(MCP4725Full.PD_100K_GND); // Set power-down mode with code preserved, (mode=0–3) → None
|
|
23
|
+
console.log('code:', state.code);
|
|
24
|
+
console.log('voltage_fraction:', state.voltage_fraction);
|
|
25
|
+
console.log('eeprom_ready:', state.eeprom_ready);
|
|
26
|
+
await dac.set_power_down(MCP4725Full.PD_100K_GND); // Set power-down mode with code preserved, (mode=0–3) → None
|
|
26
27
|
// enters power-down; output stage disconnects with 100k to GND
|
|
27
|
-
dac.wake_up();
|
|
28
|
+
await dac.wake_up(); // Send General Call Wake-Up to clear power-down, () → None
|
|
28
29
|
// sends 0x00, 0x09 to address 0x00; clears PD bits in DAC register
|
|
29
|
-
dac.reset();
|
|
30
|
+
await dac.reset(); // Send General Call Reset and reload EEPROM, () → None
|
|
30
31
|
// sends 0x00, 0x06; triggers internal POR and reloads DAC from EEPROM
|
|
31
|
-
const ready = dac.is_eeprom_ready();
|
|
32
|
+
const ready = await dac.is_eeprom_ready(); // Check if EEPROM write is complete, () → bool
|
|
32
33
|
// returns True when any pending EEPROM write has finished
|
|
33
|
-
console.log('eeprom_ready:', ready);
|
|
34
|
-
console.log('MCP4725 complete running');
|
|
34
|
+
console.log('eeprom_ready:', ready);
|
|
35
|
+
console.log('MCP4725 complete running');
|
|
36
|
+
})();
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
4
|
const { MCP4725Full } = require('../../../src/chips/adc_dac/mcp4725');
|
|
5
5
|
|
|
6
6
|
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
7
|
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 16);
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
const dac = new MCP4725Full(
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const dac = new MCP4725Full(connection);
|
|
11
11
|
|
|
12
12
|
const STEP = 1.0 / 20.0;
|
|
13
13
|
const DELAY_MS = 100;
|
|
@@ -21,7 +21,7 @@ async function run_triangle_wave() {
|
|
|
21
21
|
// Up sweep: 0 to full scale in 21 steps
|
|
22
22
|
for (let n = 0; n <= 20; n++) {
|
|
23
23
|
const fraction = n * STEP;
|
|
24
|
-
dac.set_voltage(fraction);
|
|
24
|
+
await dac.set_voltage(fraction); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
|
|
25
25
|
const code = Math.round(fraction * 4095);
|
|
26
26
|
const approx_v = code * 3.3 / 4096;
|
|
27
27
|
console.log(`n=${String(n).padStart(2)} fraction=${fraction.toFixed(2)} code=${String(code).padStart(4)} approx_v=${approx_v.toFixed(3)}V`);
|
|
@@ -30,7 +30,7 @@ async function run_triangle_wave() {
|
|
|
30
30
|
// Down sweep: full scale back to 0 in 20 steps
|
|
31
31
|
for (let n = 20; n >= 0; n--) {
|
|
32
32
|
const fraction = n * STEP;
|
|
33
|
-
dac.set_voltage(fraction);
|
|
33
|
+
await dac.set_voltage(fraction); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
|
|
34
34
|
const code = Math.round(fraction * 4095);
|
|
35
35
|
const approx_v = code * 3.3 / 4096;
|
|
36
36
|
console.log(`n=${String(n).padStart(2)} fraction=${fraction.toFixed(2)} code=${String(code).padStart(4)} approx_v=${approx_v.toFixed(3)}V`);
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
4
|
const { MCP4725Minimal } = require('../../../src/chips/adc_dac/mcp4725');
|
|
5
5
|
|
|
6
6
|
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
7
|
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 16);
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
const dac = new MCP4725Minimal(
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const dac = new MCP4725Minimal(connection);
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
dac.
|
|
14
|
-
|
|
12
|
+
(async () => {
|
|
13
|
+
await dac.set_voltage(0.5); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
|
|
14
|
+
await dac.set_raw(2048); // Set raw 12-bit code, (code=0–4095) → None
|
|
15
|
+
console.log('MCP4725 minimal running');
|
|
16
|
+
})();
|
|
@@ -1,41 +1,43 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
4
|
const { MCP4728Full } = require('periph/src/chips/adc_dac/mcp4728');
|
|
5
5
|
|
|
6
|
-
const
|
|
7
|
-
const dac = new MCP4728Full(
|
|
6
|
+
const connection = new I2CConnection(1, 0x60); // Create I2C connection, (bus=1, addr=0x60)
|
|
7
|
+
const dac = new MCP4728Full(connection); // Create MCP4728 driver, (connection)
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
(async () => {
|
|
10
|
+
await dac.set_voltage(0, 0.75); // Set channel A as fraction of V_DD, (channel=0–3, fraction=0.0–1.0) → None
|
|
10
11
|
// Multi-Write, V_REF=external, gain=×1, PD=00
|
|
11
|
-
dac.set_raw(2, 3000);
|
|
12
|
+
await dac.set_raw(2, 3000); // Set channel C raw code, (channel=0–3, code=0–4095) → None
|
|
12
13
|
// clamps to [0, 4095]; writes channel C only
|
|
13
|
-
dac.set_all([0.1, 0.2, 0.3, 0.4]);
|
|
14
|
+
await dac.set_all([0.1, 0.2, 0.3, 0.4]); // Update all four channels simultaneously, (fractions[4]) → None
|
|
14
15
|
// single 8-byte Fast Write transaction; EEPROM unaffected
|
|
15
|
-
dac.set_voltage_eeprom(0, 0.5, 0, 1);
|
|
16
|
+
await dac.set_voltage_eeprom(0, 0.5, 0, 1); // Set channel A and persist to EEPROM, (channel=0–3, fraction, vref=0/1, gain=1/2) → None
|
|
16
17
|
// Single Write updates DAC register and nonvolatile EEPROM
|
|
17
|
-
dac.set_raw_eeprom(1, 2048, 0, 1);
|
|
18
|
+
await dac.set_raw_eeprom(1, 2048, 0, 1); // Set channel B raw code and persist, (channel=0–3, code, vref=0/1, gain=1/2) → None
|
|
18
19
|
// Single Write; takes up to 50 ms for EEPROM write
|
|
19
|
-
dac.set_all_eeprom([0.0, 0.25, 0.5, 0.75],
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
await dac.set_all_eeprom([0.0, 0.25, 0.5, 0.75], // Update all four channels + EEPROM, (fractions[4], vrefs[4], gains[4]) → None
|
|
21
|
+
[0, 0, 0, 0], // each channel uses external V_DD
|
|
22
|
+
[1, 1, 1, 1]); // and gain ×1
|
|
22
23
|
// Sequential Write from A to D; persists all four at the end
|
|
23
|
-
dac.set_vref(0, 0, 0, 0);
|
|
24
|
+
await dac.set_vref(0, 0, 0, 0); // Set V_REF for all four channels, (vref_a, vref_b, vref_c, vref_d) → None
|
|
24
25
|
// 0 = external V_DD; volatile register only
|
|
25
|
-
dac.set_gain(1, 1, 1, 1);
|
|
26
|
+
await dac.set_gain(1, 1, 1, 1); // Set gain for all four channels, (gain_a, gain_b, gain_c, gain_d) → None
|
|
26
27
|
// 1 = ×1, 2 = ×2; volatile register only
|
|
27
|
-
dac.set_power_down(0, 0, 0, 0);
|
|
28
|
+
await dac.set_power_down(0, 0, 0, 0); // Set power-down for all four channels, (pd_a, pd_b, pd_c, pd_d) → None
|
|
28
29
|
// 0 = normal, 1 = 1 kΩ, 2 = 100 kΩ, 3 = 500 kΩ to GND
|
|
29
|
-
const state = dac.read();
|
|
30
|
+
const state = await dac.read(); // Read all four channels' DAC and EEPROM state, () → {channel[], eeprom_ready}
|
|
30
31
|
// 4 channel objects with code, vref, gain, power_down, eeprom_*
|
|
31
|
-
console.log('ch0 code:', state.channel[0].code);
|
|
32
|
-
console.log('eeprom_ready:', state.eeprom_ready);
|
|
33
|
-
dac.software_update();
|
|
32
|
+
console.log('ch0 code:', state.channel[0].code);
|
|
33
|
+
console.log('eeprom_ready:', state.eeprom_ready);
|
|
34
|
+
await dac.software_update(); // Latch all V_OUT simultaneously, () → None
|
|
34
35
|
// General Call 0x00, 0x08; equivalent to LDAC pin pulse
|
|
35
|
-
dac.wake_up();
|
|
36
|
+
await dac.wake_up(); // Clear all PD bits via General Call Wake-Up, () → None
|
|
36
37
|
// sends 0x00, 0x09; clears power-down on all four channels
|
|
37
|
-
dac.reset();
|
|
38
|
+
await dac.reset(); // Reload EEPROM into all DAC registers, () → None
|
|
38
39
|
// General Call 0x00, 0x06; triggers internal POR
|
|
39
|
-
const ready = dac.is_eeprom_ready();
|
|
40
|
+
const ready = await dac.is_eeprom_ready(); // Check if EEPROM write is complete, () → bool
|
|
40
41
|
// True when any pending EEPROM write has finished
|
|
41
|
-
console.log('eeprom ready:', ready);
|
|
42
|
+
console.log('eeprom ready:', ready);
|
|
43
|
+
})();
|
|
@@ -1,41 +1,41 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
4
|
const { MCP4728Full } = require('periph/src/chips/adc_dac/mcp4728');
|
|
5
5
|
|
|
6
6
|
const VDD = 3.3;
|
|
7
7
|
|
|
8
|
-
const
|
|
9
|
-
const dac = new MCP4728Full(
|
|
8
|
+
const connection = new I2CConnection(1, 0x60); // Create I2C connection, (bus=1, addr=0x60)
|
|
9
|
+
const dac = new MCP4728Full(connection); // Create MCP4728 driver, (connection)
|
|
10
10
|
|
|
11
|
-
// --- Apply four-point calibration voltages to channels A–D ---
|
|
12
|
-
// A 4-channel DAC is the canonical way to bias a 4-point sensor bridge
|
|
13
|
-
// (load cell, RTD conditioning, strain gauge). Each channel gets a
|
|
14
|
-
// different fraction of full scale to demonstrate independent outputs.
|
|
15
|
-
// Voltages printed below assume a 3.3 V supply.
|
|
16
|
-
const calibration = [0.0, 1.0 / 3, 2.0 / 3, 1.0];
|
|
17
|
-
dac.set_all(calibration); // Update all four channels simultaneously, (fractions[4]) → None
|
|
18
|
-
for (let ch = 0; ch < 4; ch++) {
|
|
19
|
-
const code = Math.round(calibration[ch] * 4095);
|
|
20
|
-
console.log(`ch=${ch} fraction=${calibration[ch].toFixed(4)} code=${code} approx_v=${(code * VDD / 4096).toFixed(3)}V`);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// --- Synchronous staircase from 0 to full scale on all four channels ---
|
|
24
|
-
// Using set_all with the same fraction across channels keeps them in lock-step
|
|
25
|
-
// and demonstrates simultaneous V_OUT update via Fast Write. A 50 ms pause
|
|
26
|
-
// between steps lets the host controller observe each level on the scope.
|
|
27
11
|
async function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
|
|
28
12
|
(async () => {
|
|
13
|
+
// --- Apply four-point calibration voltages to channels A–D ---
|
|
14
|
+
// A 4-channel DAC is the canonical way to bias a 4-point sensor bridge
|
|
15
|
+
// (load cell, RTD conditioning, strain gauge). Each channel gets a
|
|
16
|
+
// different fraction of full scale to demonstrate independent outputs.
|
|
17
|
+
// Voltages printed below assume a 3.3 V supply.
|
|
18
|
+
const calibration = [0.0, 1.0 / 3, 2.0 / 3, 1.0];
|
|
19
|
+
await dac.set_all(calibration); // Update all four channels simultaneously, (fractions[4]) → None
|
|
20
|
+
for (let ch = 0; ch < 4; ch++) {
|
|
21
|
+
const code = Math.round(calibration[ch] * 4095);
|
|
22
|
+
console.log(`ch=${ch} fraction=${calibration[ch].toFixed(4)} code=${code} approx_v=${(code * VDD / 4096).toFixed(3)}V`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// --- Synchronous staircase from 0 to full scale on all four channels ---
|
|
26
|
+
// Using set_all with the same fraction across channels keeps them in lock-step
|
|
27
|
+
// and demonstrates simultaneous V_OUT update via Fast Write. A 50 ms pause
|
|
28
|
+
// between steps lets the host controller observe each level on the scope.
|
|
29
29
|
const STEP = 1.0 / 16;
|
|
30
30
|
for (let n = 0; n <= 16; n++) {
|
|
31
31
|
const f = n * STEP;
|
|
32
|
-
dac.set_all([f, f, f, f]);
|
|
32
|
+
await dac.set_all([f, f, f, f]); // Update all four channels simultaneously, (fractions[4]) → None
|
|
33
33
|
const code = Math.round(f * 4095);
|
|
34
34
|
console.log(`step=${n.toString().padStart(2)} fraction=${f.toFixed(4)} code=${code} approx_v=${(code * VDD / 4096).toFixed(3)}V`);
|
|
35
35
|
await sleep(50);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
// --- Reset all channels to 0 V before exit ---
|
|
39
|
-
dac.set_all([0.0, 0.0, 0.0, 0.0]);
|
|
40
|
-
|
|
39
|
+
await dac.set_all([0.0, 0.0, 0.0, 0.0]); // Update all four channels simultaneously, (fractions[4]) → None
|
|
40
|
+
await connection.close();
|
|
41
41
|
})();
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
4
|
const { MCP4728Minimal } = require('periph/src/chips/adc_dac/mcp4728');
|
|
5
5
|
|
|
6
|
-
const
|
|
7
|
-
const dac = new MCP4728Minimal(
|
|
6
|
+
const connection = new I2CConnection(1, 0x60); // Create I2C connection, (bus=1, addr=0x60)
|
|
7
|
+
const dac = new MCP4728Minimal(connection); // Create MCP4728 driver, (connection)
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
dac.
|
|
11
|
-
dac.
|
|
9
|
+
(async () => {
|
|
10
|
+
await dac.set_voltage(0, 0.5); // Set channel A as fraction of V_DD, (channel=0–3, fraction=0.0–1.0) → None
|
|
11
|
+
await dac.set_raw(1, 2048); // Set channel B raw 12-bit code, (channel=0–3, code=0–4095) → None
|
|
12
|
+
await dac.set_all([0.0, 0.25, 0.5, 1.0]); // Update all four channels simultaneously, (fractions[4]) → None
|
|
13
|
+
})();
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { PCF8591Full } = require('../../../src/chips/adc_dac/pcf8591');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x48', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const adc = new PCF8591Full(connection);
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
const ch0_raw = await adc.read_channel(0); // Read single channel, (channel=0–3) → number
|
|
14
|
+
// discards the stale first conversion byte; returns 0–255
|
|
15
|
+
const ch1_raw = await adc.read_channel(1); // Read single channel, (channel=0–3) → number
|
|
16
|
+
// selects channel 1 via the control byte, returns 0–255
|
|
17
|
+
const all_raw = await adc.read_all(); // Read all four channels, () → number[]
|
|
18
|
+
// sets AI=1 and reads 5 bytes; discards stale byte 0
|
|
19
|
+
|
|
20
|
+
const v0 = await adc.read_channel_voltage(0, 3.3, 0.0); // Read channel as voltage, (channel, vref=3.3 V, vagnd=0.0 V) → number V
|
|
21
|
+
// converts raw to voltage using V_AGND + raw × (V_REF−V_AGND) / 256
|
|
22
|
+
const v_all = await adc.read_all_voltage(3.3, 0.0); // Read all channels as voltages, (vref=3.3 V, vagnd=0.0 V) → number[] V
|
|
23
|
+
// returns four voltages using the same conversion
|
|
24
|
+
|
|
25
|
+
await adc.configure(PCF8591Full.MODE_3_DIFFERENTIAL, false, false); // Configure input mode, (input_mode=0–3, auto_increment=bool, dac_enabled=bool) → None
|
|
26
|
+
// sets AIP=01 (3 differential channels vs AIN3) and clears AOE/AI
|
|
27
|
+
const diff = await adc.read_differential(0); // Read differential channel, (channel=0–2) → number
|
|
28
|
+
// returns signed 8-bit two's complement (-128 to 127)
|
|
29
|
+
await adc.configure(PCF8591Full.MODE_4_SINGLE_ENDED, false, true); // Configure input mode, (input_mode=0–3, auto_increment=bool, dac_enabled=bool) → None
|
|
30
|
+
// restores 4 single-ended mode and enables the DAC output
|
|
31
|
+
await adc.set_dac(128); // Enable DAC and set raw value, (value=0–255) → None
|
|
32
|
+
// sets AOE=1 and writes 128 to the DAC register; V_AOUT ≈ V_REF/2
|
|
33
|
+
await adc.set_dac_voltage(0.25); // Set DAC as fraction of (VREF−VAGND), (fraction=0.0–1.0) → None
|
|
34
|
+
// maps fraction to 0–255 and writes the DAC; AOUT follows
|
|
35
|
+
await adc.disable_dac(); // Disable DAC output, () → None
|
|
36
|
+
// clears AOE; AOUT returns to high-impedance
|
|
37
|
+
console.log('ch0_raw=' + ch0_raw);
|
|
38
|
+
console.log('v0=' + v0.toFixed(3) + 'V');
|
|
39
|
+
console.log('diff=' + diff);
|
|
40
|
+
console.log('PCF8591 complete running');
|
|
41
|
+
})();
|