periph 1.1.0 → 1.2.1
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 +61 -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/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -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 +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/examples/rtc/ds3231/complete.js +77 -0
- package/examples/rtc/ds3231/demo.js +60 -0
- package/examples/rtc/ds3231/minimal.js +23 -0
- package/examples/rtc/pcf8523/complete.js +66 -0
- package/examples/rtc/pcf8523/demo.js +65 -0
- package/examples/rtc/pcf8523/minimal.js +20 -0
- package/examples/temperature/mcp9808/complete.js +89 -0
- package/examples/temperature/mcp9808/demo.js +62 -0
- package/examples/temperature/mcp9808/minimal.js +21 -0
- package/examples/temperature/tmp117/complete.js +90 -0
- package/examples/temperature/tmp117/demo.js +73 -0
- package/examples/temperature/tmp117/minimal.js +21 -0
- package/examples/tof/vl53l0x/complete.js +118 -0
- package/examples/tof/vl53l0x/demo.js +83 -0
- package/examples/tof/vl53l0x/minimal.js +25 -0
- package/examples/tof/vl53l1x/complete.js +151 -0
- package/examples/tof/vl53l1x/demo.js +100 -0
- package/examples/tof/vl53l1x/minimal.js +25 -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/motor/drv8830.js +239 -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 +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/chips/rtc/ds3231.js +504 -0
- package/src/chips/rtc/pcf8523.js +539 -0
- package/src/chips/temperature/mcp9808.js +488 -0
- package/src/chips/temperature/tmp117.js +471 -0
- package/src/chips/tof/_vl53_base.js +160 -0
- package/src/chips/tof/vl53l0x.js +816 -0
- package/src/chips/tof/vl53l1x.js +816 -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/examples/motor/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AD7705 2-channel, 16-bit sigma-delta ADC driver (SPI).
|
|
5
|
+
*
|
|
6
|
+
* Two-phase register-access protocol: write the Communication Register
|
|
7
|
+
* (selects target register + read/write direction + channel), then
|
|
8
|
+
* transfer the data bytes in a single CS-held transaction. DRDY is
|
|
9
|
+
* polled over SPI by inspecting bit 7 of the Communication Register,
|
|
10
|
+
* matching the datasheet's 3-wire microcontroller interface technique
|
|
11
|
+
* (no dedicated DRDY GPIO required).
|
|
12
|
+
*
|
|
13
|
+
* Minimal default configuration: gain 1, bipolar, unbuffered, 50 Hz
|
|
14
|
+
* output rate on a 2.4576/4.9152 MHz clock (or 20 Hz on 1/2 MHz), with
|
|
15
|
+
* Channel 1 self-calibrated once at construction.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const MCLK_1MHZ = 1000000;
|
|
19
|
+
const MCLK_2MHZ = 2000000;
|
|
20
|
+
const MCLK_2_4576MHZ = 2457600;
|
|
21
|
+
const MCLK_4_9152MHZ = 4915200;
|
|
22
|
+
const VALID_MCLK = new Set([MCLK_1MHZ, MCLK_2MHZ, MCLK_2_4576MHZ, MCLK_4_9152MHZ]);
|
|
23
|
+
|
|
24
|
+
const REG_COMM = 0x00;
|
|
25
|
+
const REG_SETUP = 0x10;
|
|
26
|
+
const REG_CLOCK = 0x20;
|
|
27
|
+
const REG_DATA = 0x30;
|
|
28
|
+
const REG_OFFSET = 0x60;
|
|
29
|
+
const REG_GAIN = 0x70;
|
|
30
|
+
|
|
31
|
+
const RW_WRITE = 0x00;
|
|
32
|
+
const RW_READ = 0x08;
|
|
33
|
+
|
|
34
|
+
const CH1 = 0x00;
|
|
35
|
+
const CH2 = 0x01;
|
|
36
|
+
|
|
37
|
+
const MODE_NORMAL = 0x00;
|
|
38
|
+
const MODE_SELF_CAL = 0x40;
|
|
39
|
+
const MODE_ZERO_SYS = 0x80;
|
|
40
|
+
const MODE_FULL_SYS = 0xC0;
|
|
41
|
+
|
|
42
|
+
const GAIN_BITS = [0x00, 0x08, 0x10, 0x18, 0x20, 0x28, 0x30, 0x38];
|
|
43
|
+
const GAIN_TO_IDX = { 1: 0, 2: 1, 4: 2, 8: 3, 16: 4, 32: 5, 64: 6, 128: 7 };
|
|
44
|
+
|
|
45
|
+
const BIPOLAR = 0x00;
|
|
46
|
+
const UNIPOLAR = 0x04;
|
|
47
|
+
const UNBUFFERED = 0x00;
|
|
48
|
+
const BUFFERED = 0x02;
|
|
49
|
+
const FSYNC_RUN = 0x00;
|
|
50
|
+
const STBY_RUN = 0x00;
|
|
51
|
+
const STBY_SLEEP = 0x04;
|
|
52
|
+
const DRDY_MASK = 0x80;
|
|
53
|
+
|
|
54
|
+
const FS_RATES_1MHZ = [20, 25, 100, 200];
|
|
55
|
+
const FS_RATES_2_4MHZ = [50, 60, 250, 500];
|
|
56
|
+
|
|
57
|
+
function commByte(reg, read, channel) {
|
|
58
|
+
return (reg | (read ? RW_READ : RW_WRITE) | (channel & 0x03)) & 0xFF;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function waitDrdy(conn) {
|
|
62
|
+
while (true) {
|
|
63
|
+
const r = await conn.writeRead(Buffer.from([commByte(REG_COMM, true, CH1)]), 1);
|
|
64
|
+
if (!(r[0] & DRDY_MASK)) return;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function writeRegChannel(conn, reg, value, channel, nBytes) {
|
|
69
|
+
const buf = Buffer.alloc(1 + nBytes);
|
|
70
|
+
buf[0] = commByte(reg, false, channel);
|
|
71
|
+
for (let i = nBytes - 1; i >= 0; i--) {
|
|
72
|
+
buf[1 + (nBytes - 1 - i)] = (value >> (8 * i)) & 0xFF;
|
|
73
|
+
}
|
|
74
|
+
await conn.write(buf);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function readRegChannel(conn, reg, channel, nBytes) {
|
|
78
|
+
const comm = Buffer.from([commByte(reg, true, channel)]);
|
|
79
|
+
const raw = await conn.writeRead(comm, nBytes);
|
|
80
|
+
let value = 0;
|
|
81
|
+
for (let i = 0; i < nBytes; i++) {
|
|
82
|
+
value = (value << 8) | raw[i];
|
|
83
|
+
}
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function configureClock(conn, mclkHz, outputRateHz) {
|
|
88
|
+
const clkBit = (mclkHz >= MCLK_2_4576MHZ) ? 0x04 : 0x00;
|
|
89
|
+
const clkDivBit = (mclkHz === MCLK_2MHZ || mclkHz === MCLK_4_9152MHZ) ? 0x08 : 0x00;
|
|
90
|
+
const rates = (mclkHz >= MCLK_2_4576MHZ) ? FS_RATES_2_4MHZ : FS_RATES_1MHZ;
|
|
91
|
+
const fsIdx = rates.indexOf(outputRateHz);
|
|
92
|
+
await writeRegChannel(conn, REG_CLOCK, clkDivBit | clkBit | fsIdx, CH1, 1);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function codeToVoltage(code, gain, bipolar, vref) {
|
|
96
|
+
if (bipolar) {
|
|
97
|
+
return ((code - 32768) / 32768) * (vref / gain);
|
|
98
|
+
}
|
|
99
|
+
return (code / 65536) * (vref / gain);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* AD7705 minimal driver — Channel 1 voltage read with sensible defaults.
|
|
104
|
+
*/
|
|
105
|
+
class AD7705Minimal {
|
|
106
|
+
/**
|
|
107
|
+
* @param {object} connection - SPI connection bound to the device.
|
|
108
|
+
* @param {number} vref - Externally-supplied reference voltage in V.
|
|
109
|
+
* @param {number} mclkHz - Master clock frequency in Hz.
|
|
110
|
+
* @param {object|null} [resetPin=null] - Optional OutputPin driving RESET.
|
|
111
|
+
*/
|
|
112
|
+
constructor(connection, vref, mclkHz, resetPin = null) {
|
|
113
|
+
if (!VALID_MCLK.has(mclkHz)) {
|
|
114
|
+
throw new Error('mclkHz must be 1000000, 2000000, 2457600, or 4915200');
|
|
115
|
+
}
|
|
116
|
+
this._conn = connection;
|
|
117
|
+
this._vref = Number(vref);
|
|
118
|
+
this._mclkHz = mclkHz;
|
|
119
|
+
this._resetPin = resetPin;
|
|
120
|
+
this._gain = 1;
|
|
121
|
+
this._bipolar = true;
|
|
122
|
+
this._buffered = false;
|
|
123
|
+
|
|
124
|
+
if (resetPin) {
|
|
125
|
+
resetPin.set(false);
|
|
126
|
+
resetPin.set(true);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const defaultRate = (mclkHz >= MCLK_2_4576MHZ) ? FS_RATES_2_4MHZ[0] : FS_RATES_1MHZ[0];
|
|
130
|
+
configureClock(connection, mclkHz, defaultRate);
|
|
131
|
+
|
|
132
|
+
const setup = MODE_SELF_CAL | GAIN_BITS[0] | BIPOLAR | UNBUFFERED | FSYNC_RUN;
|
|
133
|
+
writeRegChannel(connection, REG_SETUP, setup, CH1, 1);
|
|
134
|
+
return waitDrdy(connection);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Block until DRDY, then read and return the raw 16-bit Data Register code on Channel 1.
|
|
139
|
+
* @returns {Promise<number>} Raw 16-bit code (0–65535).
|
|
140
|
+
*/
|
|
141
|
+
async readRaw() {
|
|
142
|
+
await waitDrdy(this._conn);
|
|
143
|
+
return await readRegChannel(this._conn, REG_DATA, CH1, 2);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Block until DRDY, then return the input voltage on Channel 1 in V.
|
|
148
|
+
* @returns {Promise<number>} Voltage in V.
|
|
149
|
+
*/
|
|
150
|
+
async readVoltage() {
|
|
151
|
+
const code = await this.readRaw();
|
|
152
|
+
return codeToVoltage(code, this._gain, this._bipolar, this._vref);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* AD7705 full driver — adds per-channel configuration, calibration, and power control.
|
|
158
|
+
*/
|
|
159
|
+
class AD7705Full extends AD7705Minimal {
|
|
160
|
+
/**
|
|
161
|
+
* Write the Setup and Clock Registers for the given channel.
|
|
162
|
+
* Does not calibrate — call selfCalibrate() (or one of the
|
|
163
|
+
* system-calibration methods) afterward.
|
|
164
|
+
*
|
|
165
|
+
* @param {number} channel - 1 or 2.
|
|
166
|
+
* @param {number} gain - PGA gain: 1, 2, 4, 8, 16, 32, 64, or 128.
|
|
167
|
+
* @param {boolean} bipolar - true for bipolar, false for unipolar.
|
|
168
|
+
* @param {boolean} buffered - true to enable the analog input buffer.
|
|
169
|
+
* @param {number} outputRateHz - One of the four rates in the mclkHz family.
|
|
170
|
+
*/
|
|
171
|
+
async configure(channel, gain, bipolar, buffered, outputRateHz) {
|
|
172
|
+
if (channel !== 1 && channel !== 2) {
|
|
173
|
+
throw new Error('channel must be 1 or 2');
|
|
174
|
+
}
|
|
175
|
+
if (!(gain in GAIN_TO_IDX)) {
|
|
176
|
+
throw new Error('gain must be one of 1, 2, 4, 8, 16, 32, 64, 128');
|
|
177
|
+
}
|
|
178
|
+
const rates = (this._mclkHz >= MCLK_2_4576MHZ) ? FS_RATES_2_4MHZ : FS_RATES_1MHZ;
|
|
179
|
+
if (!rates.includes(outputRateHz)) {
|
|
180
|
+
throw new Error(`outputRateHz must be one of ${rates.join(', ')} Hz`);
|
|
181
|
+
}
|
|
182
|
+
const ch = (channel === 1) ? CH1 : CH2;
|
|
183
|
+
|
|
184
|
+
await configureClock(this._conn, this._mclkHz, outputRateHz);
|
|
185
|
+
|
|
186
|
+
const buBit = bipolar ? BIPOLAR : UNIPOLAR;
|
|
187
|
+
const bufBit = buffered ? BUFFERED : UNBUFFERED;
|
|
188
|
+
const setup = MODE_NORMAL | GAIN_BITS[GAIN_TO_IDX[gain]] | buBit | bufBit | FSYNC_RUN;
|
|
189
|
+
await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
|
|
190
|
+
|
|
191
|
+
if (channel === 1) {
|
|
192
|
+
this._gain = gain;
|
|
193
|
+
this._bipolar = bipolar;
|
|
194
|
+
this._buffered = buffered;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Block until DRDY, then read the raw 16-bit code for the channel.
|
|
200
|
+
* @param {number} channel - 1 or 2.
|
|
201
|
+
* @returns {Promise<number>} Raw 16-bit code (0–65535).
|
|
202
|
+
*/
|
|
203
|
+
async readRawChannel(channel) {
|
|
204
|
+
if (channel !== 1 && channel !== 2) {
|
|
205
|
+
throw new Error('channel must be 1 or 2');
|
|
206
|
+
}
|
|
207
|
+
const ch = (channel === 1) ? CH1 : CH2;
|
|
208
|
+
await waitDrdy(this._conn);
|
|
209
|
+
return await readRegChannel(this._conn, REG_DATA, ch, 2);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Block until DRDY, then return the input voltage on the channel in V.
|
|
214
|
+
* @param {number} channel - 1 or 2.
|
|
215
|
+
* @returns {Promise<number>} Voltage in V.
|
|
216
|
+
*/
|
|
217
|
+
async readVoltageChannel(channel) {
|
|
218
|
+
const code = await this.readRawChannel(channel);
|
|
219
|
+
return codeToVoltage(code, this._gain, this._bipolar, this._vref);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Run an internal self-calibration on the channel.
|
|
224
|
+
* @param {number} channel - 1 or 2.
|
|
225
|
+
*/
|
|
226
|
+
async selfCalibrate(channel) {
|
|
227
|
+
if (channel !== 1 && channel !== 2) {
|
|
228
|
+
throw new Error('channel must be 1 or 2');
|
|
229
|
+
}
|
|
230
|
+
const ch = (channel === 1) ? CH1 : CH2;
|
|
231
|
+
const setup = MODE_SELF_CAL | GAIN_BITS[GAIN_TO_IDX[this._gain]] | (this._bipolar ? BIPOLAR : UNIPOLAR) | (this._buffered ? BUFFERED : UNBUFFERED) | FSYNC_RUN;
|
|
232
|
+
await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
|
|
233
|
+
await waitDrdy(this._conn);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Run a zero-scale system calibration. The caller must present the zero-scale voltage at AIN first.
|
|
238
|
+
* @param {number} channel - 1 or 2.
|
|
239
|
+
*/
|
|
240
|
+
async systemCalibrateZero(channel) {
|
|
241
|
+
if (channel !== 1 && channel !== 2) {
|
|
242
|
+
throw new Error('channel must be 1 or 2');
|
|
243
|
+
}
|
|
244
|
+
const ch = (channel === 1) ? CH1 : CH2;
|
|
245
|
+
const setup = MODE_ZERO_SYS | GAIN_BITS[GAIN_TO_IDX[this._gain]] | (this._bipolar ? BIPOLAR : UNIPOLAR) | (this._buffered ? BUFFERED : UNBUFFERED) | FSYNC_RUN;
|
|
246
|
+
await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
|
|
247
|
+
await waitDrdy(this._conn);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Run a full-scale system calibration. The caller must present the full-scale voltage at AIN first.
|
|
252
|
+
* @param {number} channel - 1 or 2.
|
|
253
|
+
*/
|
|
254
|
+
async systemCalibrateFull(channel) {
|
|
255
|
+
if (channel !== 1 && channel !== 2) {
|
|
256
|
+
throw new Error('channel must be 1 or 2');
|
|
257
|
+
}
|
|
258
|
+
const ch = (channel === 1) ? CH1 : CH2;
|
|
259
|
+
const setup = MODE_FULL_SYS | GAIN_BITS[GAIN_TO_IDX[this._gain]] | (this._bipolar ? BIPOLAR : UNIPOLAR) | (this._buffered ? BUFFERED : UNBUFFERED) | FSYNC_RUN;
|
|
260
|
+
await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
|
|
261
|
+
await waitDrdy(this._conn);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Read the 24-bit Zero-Scale Calibration Register for the channel.
|
|
266
|
+
* @param {number} channel - 1 or 2.
|
|
267
|
+
* @returns {Promise<number>} 24-bit unsigned offset coefficient.
|
|
268
|
+
*/
|
|
269
|
+
async getOffsetCalibration(channel) {
|
|
270
|
+
if (channel !== 1 && channel !== 2) {
|
|
271
|
+
throw new Error('channel must be 1 or 2');
|
|
272
|
+
}
|
|
273
|
+
const ch = (channel === 1) ? CH1 : CH2;
|
|
274
|
+
return await readRegChannel(this._conn, REG_OFFSET, ch, 3);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Write a 24-bit Zero-Scale Calibration Register for the channel.
|
|
279
|
+
* @param {number} value - 24-bit unsigned offset coefficient.
|
|
280
|
+
* @param {number} channel - 1 or 2.
|
|
281
|
+
*/
|
|
282
|
+
async setOffsetCalibration(value, channel) {
|
|
283
|
+
if (channel !== 1 && channel !== 2) {
|
|
284
|
+
throw new Error('channel must be 1 or 2');
|
|
285
|
+
}
|
|
286
|
+
const ch = (channel === 1) ? CH1 : CH2;
|
|
287
|
+
await writeRegChannel(this._conn, REG_OFFSET, value & 0xFFFFFF, ch, 3);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Read the 24-bit Full-Scale Calibration Register for the channel.
|
|
292
|
+
* @param {number} channel - 1 or 2.
|
|
293
|
+
* @returns {Promise<number>} 24-bit unsigned gain coefficient.
|
|
294
|
+
*/
|
|
295
|
+
async getGainCalibration(channel) {
|
|
296
|
+
if (channel !== 1 && channel !== 2) {
|
|
297
|
+
throw new Error('channel must be 1 or 2');
|
|
298
|
+
}
|
|
299
|
+
const ch = (channel === 1) ? CH1 : CH2;
|
|
300
|
+
return await readRegChannel(this._conn, REG_GAIN, ch, 3);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Write a 24-bit Full-Scale Calibration Register for the channel.
|
|
305
|
+
* @param {number} value - 24-bit unsigned gain coefficient.
|
|
306
|
+
* @param {number} channel - 1 or 2.
|
|
307
|
+
*/
|
|
308
|
+
async setGainCalibration(value, channel) {
|
|
309
|
+
if (channel !== 1 && channel !== 2) {
|
|
310
|
+
throw new Error('channel must be 1 or 2');
|
|
311
|
+
}
|
|
312
|
+
const ch = (channel === 1) ? CH1 : CH2;
|
|
313
|
+
await writeRegChannel(this._conn, REG_GAIN, value & 0xFFFFFF, ch, 3);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Enter standby (~10 µA, registers retained).
|
|
318
|
+
*/
|
|
319
|
+
async standby() {
|
|
320
|
+
const comm = Buffer.from([(commByte(REG_COMM, false, CH1) | STBY_SLEEP) & 0xFF]);
|
|
321
|
+
await this._conn.write(comm);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Exit standby and block until a fresh conversion is available.
|
|
326
|
+
*/
|
|
327
|
+
async wakeup() {
|
|
328
|
+
const comm = Buffer.from([(commByte(REG_COMM, false, CH1) | STBY_RUN) & 0xFF]);
|
|
329
|
+
await this._conn.write(comm);
|
|
330
|
+
await waitDrdy(this._conn);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Pulse the hardware RESET line. Requires a resetPin to have been supplied.
|
|
335
|
+
*/
|
|
336
|
+
async reset() {
|
|
337
|
+
if (!this._resetPin) {
|
|
338
|
+
throw new Error('reset() requires a resetPin to have been supplied at construction');
|
|
339
|
+
}
|
|
340
|
+
this._resetPin.set(false);
|
|
341
|
+
this._resetPin.set(true);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
module.exports = { AD7705Minimal, AD7705Full };
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AD7706 3-channel, 16-bit sigma-delta ADC driver (SPI).
|
|
5
|
+
*
|
|
6
|
+
* Three pseudo-differential channels (AIN1, AIN2, AIN3), each measured
|
|
7
|
+
* relative to a single shared `COMMON` pin. Two-phase register-access
|
|
8
|
+
* protocol: write the Communication Register (selects target register +
|
|
9
|
+
* read/write direction + channel), then transfer the data bytes in a
|
|
10
|
+
* single CS-held transaction. DRDY is polled over SPI by inspecting bit 7
|
|
11
|
+
* of the Communication Register, matching the datasheet's 3-wire
|
|
12
|
+
* microcontroller interface technique (no dedicated DRDY GPIO required).
|
|
13
|
+
*
|
|
14
|
+
* Minimal default configuration: gain 1, bipolar, unbuffered, 50 Hz
|
|
15
|
+
* output rate on a 2.4576/4.9152 MHz clock (or 20 Hz on 1/2 MHz), with
|
|
16
|
+
* Channel 1 self-calibrated once at construction.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const MCLK_1MHZ = 1000000;
|
|
20
|
+
const MCLK_2MHZ = 2000000;
|
|
21
|
+
const MCLK_2_4576MHZ = 2457600;
|
|
22
|
+
const MCLK_4_9152MHZ = 4915200;
|
|
23
|
+
const VALID_MCLK = new Set([MCLK_1MHZ, MCLK_2MHZ, MCLK_2_4576MHZ, MCLK_4_9152MHZ]);
|
|
24
|
+
|
|
25
|
+
const REG_COMM = 0x00;
|
|
26
|
+
const REG_SETUP = 0x10;
|
|
27
|
+
const REG_CLOCK = 0x20;
|
|
28
|
+
const REG_DATA = 0x30;
|
|
29
|
+
const REG_OFFSET = 0x60;
|
|
30
|
+
const REG_GAIN = 0x70;
|
|
31
|
+
|
|
32
|
+
const RW_WRITE = 0x00;
|
|
33
|
+
const RW_READ = 0x08;
|
|
34
|
+
|
|
35
|
+
const CH1 = 0x00;
|
|
36
|
+
const CH2 = 0x01;
|
|
37
|
+
const CH3 = 0x03;
|
|
38
|
+
|
|
39
|
+
const MODE_NORMAL = 0x00;
|
|
40
|
+
const MODE_SELF_CAL = 0x40;
|
|
41
|
+
const MODE_ZERO_SYS = 0x80;
|
|
42
|
+
const MODE_FULL_SYS = 0xC0;
|
|
43
|
+
|
|
44
|
+
const GAIN_BITS = [0x00, 0x08, 0x10, 0x18, 0x20, 0x28, 0x30, 0x38];
|
|
45
|
+
const GAIN_TO_IDX = { 1: 0, 2: 1, 4: 2, 8: 3, 16: 4, 32: 5, 64: 6, 128: 7 };
|
|
46
|
+
|
|
47
|
+
const BIPOLAR = 0x00;
|
|
48
|
+
const UNIPOLAR = 0x04;
|
|
49
|
+
const UNBUFFERED = 0x00;
|
|
50
|
+
const BUFFERED = 0x02;
|
|
51
|
+
const FSYNC_RUN = 0x00;
|
|
52
|
+
const STBY_RUN = 0x00;
|
|
53
|
+
const STBY_SLEEP = 0x04;
|
|
54
|
+
const DRDY_MASK = 0x80;
|
|
55
|
+
|
|
56
|
+
const FS_RATES_1MHZ = [20, 25, 100, 200];
|
|
57
|
+
const FS_RATES_2_4MHZ = [50, 60, 250, 500];
|
|
58
|
+
|
|
59
|
+
function channelConst(c) {
|
|
60
|
+
if (c === 1) return CH1;
|
|
61
|
+
if (c === 2) return CH2;
|
|
62
|
+
if (c === 3) return CH3;
|
|
63
|
+
throw new Error('channel must be 1, 2, or 3');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function commByte(reg, read, channel) {
|
|
67
|
+
return (reg | (read ? RW_READ : RW_WRITE) | (channel & 0x03)) & 0xFF;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function waitDrdy(conn) {
|
|
71
|
+
while (true) {
|
|
72
|
+
const r = await conn.writeRead(Buffer.from([commByte(REG_COMM, true, CH1)]), 1);
|
|
73
|
+
if (!(r[0] & DRDY_MASK)) return;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function writeRegChannel(conn, reg, value, channel, nBytes) {
|
|
78
|
+
const buf = Buffer.alloc(1 + nBytes);
|
|
79
|
+
buf[0] = commByte(reg, false, channel);
|
|
80
|
+
for (let i = nBytes - 1; i >= 0; i--) {
|
|
81
|
+
buf[1 + (nBytes - 1 - i)] = (value >> (8 * i)) & 0xFF;
|
|
82
|
+
}
|
|
83
|
+
await conn.write(buf);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function readRegChannel(conn, reg, channel, nBytes) {
|
|
87
|
+
const comm = Buffer.from([commByte(reg, true, channel)]);
|
|
88
|
+
const raw = await conn.writeRead(comm, nBytes);
|
|
89
|
+
let value = 0;
|
|
90
|
+
for (let i = 0; i < nBytes; i++) {
|
|
91
|
+
value = (value << 8) | raw[i];
|
|
92
|
+
}
|
|
93
|
+
return value;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function configureClock(conn, mclkHz, outputRateHz) {
|
|
97
|
+
const clkBit = (mclkHz >= MCLK_2_4576MHZ) ? 0x04 : 0x00;
|
|
98
|
+
const clkDivBit = (mclkHz === MCLK_2MHZ || mclkHz === MCLK_4_9152MHZ) ? 0x08 : 0x00;
|
|
99
|
+
const rates = (mclkHz >= MCLK_2_4576MHZ) ? FS_RATES_2_4MHZ : FS_RATES_1MHZ;
|
|
100
|
+
const fsIdx = rates.indexOf(outputRateHz);
|
|
101
|
+
await writeRegChannel(conn, REG_CLOCK, clkDivBit | clkBit | fsIdx, CH1, 1);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function codeToVoltage(code, gain, bipolar, vref) {
|
|
105
|
+
if (bipolar) {
|
|
106
|
+
return ((code - 32768) / 32768) * (vref / gain);
|
|
107
|
+
}
|
|
108
|
+
return (code / 65536) * (vref / gain);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function calibrationSetup(mode, gain, bipolar, buffered) {
|
|
112
|
+
return mode | GAIN_BITS[GAIN_TO_IDX[gain]] | (bipolar ? BIPOLAR : UNIPOLAR) | (buffered ? BUFFERED : UNBUFFERED) | FSYNC_RUN;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* AD7706 minimal driver — Channel 1 voltage read with sensible defaults.
|
|
117
|
+
*/
|
|
118
|
+
class AD7706Minimal {
|
|
119
|
+
/**
|
|
120
|
+
* @param {object} connection - SPI connection bound to the device.
|
|
121
|
+
* @param {number} vref - Externally-supplied reference voltage in V.
|
|
122
|
+
* @param {number} mclkHz - Master clock frequency in Hz.
|
|
123
|
+
* @param {object|null} [resetPin=null] - Optional OutputPin driving RESET.
|
|
124
|
+
*/
|
|
125
|
+
constructor(connection, vref, mclkHz, resetPin = null) {
|
|
126
|
+
if (!VALID_MCLK.has(mclkHz)) {
|
|
127
|
+
throw new Error('mclkHz must be 1000000, 2000000, 2457600, or 4915200');
|
|
128
|
+
}
|
|
129
|
+
this._conn = connection;
|
|
130
|
+
this._vref = Number(vref);
|
|
131
|
+
this._mclkHz = mclkHz;
|
|
132
|
+
this._resetPin = resetPin;
|
|
133
|
+
this._gain = 1;
|
|
134
|
+
this._bipolar = true;
|
|
135
|
+
this._buffered = false;
|
|
136
|
+
|
|
137
|
+
if (resetPin) {
|
|
138
|
+
resetPin.set(false);
|
|
139
|
+
resetPin.set(true);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const defaultRate = (mclkHz >= MCLK_2_4576MHZ) ? FS_RATES_2_4MHZ[0] : FS_RATES_1MHZ[0];
|
|
143
|
+
configureClock(connection, mclkHz, defaultRate);
|
|
144
|
+
|
|
145
|
+
const setup = MODE_SELF_CAL | GAIN_BITS[0] | BIPOLAR | UNBUFFERED | FSYNC_RUN;
|
|
146
|
+
writeRegChannel(connection, REG_SETUP, setup, CH1, 1);
|
|
147
|
+
return waitDrdy(connection);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Block until DRDY, then read and return the raw 16-bit Data Register code on Channel 1.
|
|
152
|
+
* @returns {Promise<number>} Raw 16-bit code (0–65535).
|
|
153
|
+
*/
|
|
154
|
+
async readRaw() {
|
|
155
|
+
await waitDrdy(this._conn);
|
|
156
|
+
return await readRegChannel(this._conn, REG_DATA, CH1, 2);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Block until DRDY, then return the input voltage on Channel 1 in V.
|
|
161
|
+
* @returns {Promise<number>} Voltage in V.
|
|
162
|
+
*/
|
|
163
|
+
async readVoltage() {
|
|
164
|
+
const code = await this.readRaw();
|
|
165
|
+
return codeToVoltage(code, this._gain, this._bipolar, this._vref);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* AD7706 full driver — adds per-channel configuration, calibration, and power control.
|
|
171
|
+
*/
|
|
172
|
+
class AD7706Full extends AD7706Minimal {
|
|
173
|
+
/**
|
|
174
|
+
* Write the Setup and Clock Registers for the given channel.
|
|
175
|
+
* Does not calibrate — call selfCalibrate() (or one of the
|
|
176
|
+
* system-calibration methods) afterward.
|
|
177
|
+
*
|
|
178
|
+
* @param {number} channel - 1, 2, or 3 (AIN1, AIN2, AIN3 — all relative to COMMON).
|
|
179
|
+
* @param {number} gain - PGA gain: 1, 2, 4, 8, 16, 32, 64, or 128.
|
|
180
|
+
* @param {boolean} bipolar - true for bipolar, false for unipolar.
|
|
181
|
+
* @param {boolean} buffered - true to enable the analog input buffer.
|
|
182
|
+
* @param {number} outputRateHz - One of the four rates in the mclkHz family.
|
|
183
|
+
*/
|
|
184
|
+
async configure(channel, gain, bipolar, buffered, outputRateHz) {
|
|
185
|
+
const ch = channelConst(channel);
|
|
186
|
+
if (!(gain in GAIN_TO_IDX)) {
|
|
187
|
+
throw new Error('gain must be one of 1, 2, 4, 8, 16, 32, 64, 128');
|
|
188
|
+
}
|
|
189
|
+
const rates = (this._mclkHz >= MCLK_2_4576MHZ) ? FS_RATES_2_4MHZ : FS_RATES_1MHZ;
|
|
190
|
+
if (!rates.includes(outputRateHz)) {
|
|
191
|
+
throw new Error(`outputRateHz must be one of ${rates.join(', ')} Hz`);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
await configureClock(this._conn, this._mclkHz, outputRateHz);
|
|
195
|
+
|
|
196
|
+
const buBit = bipolar ? BIPOLAR : UNIPOLAR;
|
|
197
|
+
const bufBit = buffered ? BUFFERED : UNBUFFERED;
|
|
198
|
+
const setup = MODE_NORMAL | GAIN_BITS[GAIN_TO_IDX[gain]] | buBit | bufBit | FSYNC_RUN;
|
|
199
|
+
await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
|
|
200
|
+
|
|
201
|
+
if (channel === 1) {
|
|
202
|
+
this._gain = gain;
|
|
203
|
+
this._bipolar = bipolar;
|
|
204
|
+
this._buffered = buffered;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Block until DRDY, then read the raw 16-bit code for the channel.
|
|
210
|
+
* @param {number} channel - 1, 2, or 3.
|
|
211
|
+
* @returns {Promise<number>} Raw 16-bit code (0–65535).
|
|
212
|
+
*/
|
|
213
|
+
async readRawChannel(channel) {
|
|
214
|
+
const ch = channelConst(channel);
|
|
215
|
+
await waitDrdy(this._conn);
|
|
216
|
+
return await readRegChannel(this._conn, REG_DATA, ch, 2);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Block until DRDY, then return the input voltage on the channel in V.
|
|
221
|
+
* @param {number} channel - 1, 2, or 3.
|
|
222
|
+
* @returns {Promise<number>} Voltage in V.
|
|
223
|
+
*/
|
|
224
|
+
async readVoltageChannel(channel) {
|
|
225
|
+
const code = await this.readRawChannel(channel);
|
|
226
|
+
return codeToVoltage(code, this._gain, this._bipolar, this._vref);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Run an internal self-calibration on the channel.
|
|
231
|
+
* @param {number} channel - 1, 2, or 3.
|
|
232
|
+
*/
|
|
233
|
+
async selfCalibrate(channel) {
|
|
234
|
+
const ch = channelConst(channel);
|
|
235
|
+
const setup = calibrationSetup(MODE_SELF_CAL, this._gain, this._bipolar, this._buffered);
|
|
236
|
+
await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
|
|
237
|
+
await waitDrdy(this._conn);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Run a zero-scale system calibration. The caller must present the zero-scale voltage at AIN first.
|
|
242
|
+
* @param {number} channel - 1, 2, or 3.
|
|
243
|
+
*/
|
|
244
|
+
async systemCalibrateZero(channel) {
|
|
245
|
+
const ch = channelConst(channel);
|
|
246
|
+
const setup = calibrationSetup(MODE_ZERO_SYS, this._gain, this._bipolar, this._buffered);
|
|
247
|
+
await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
|
|
248
|
+
await waitDrdy(this._conn);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Run a full-scale system calibration. The caller must present the full-scale voltage at AIN first.
|
|
253
|
+
* @param {number} channel - 1, 2, or 3.
|
|
254
|
+
*/
|
|
255
|
+
async systemCalibrateFull(channel) {
|
|
256
|
+
const ch = channelConst(channel);
|
|
257
|
+
const setup = calibrationSetup(MODE_FULL_SYS, this._gain, this._bipolar, this._buffered);
|
|
258
|
+
await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
|
|
259
|
+
await waitDrdy(this._conn);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Read the 24-bit Zero-Scale Calibration Register for the channel.
|
|
264
|
+
* @param {number} channel - 1, 2, or 3.
|
|
265
|
+
* @returns {Promise<number>} 24-bit unsigned offset coefficient.
|
|
266
|
+
*/
|
|
267
|
+
async getOffsetCalibration(channel) {
|
|
268
|
+
const ch = channelConst(channel);
|
|
269
|
+
return await readRegChannel(this._conn, REG_OFFSET, ch, 3);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Write a 24-bit Zero-Scale Calibration Register for the channel.
|
|
274
|
+
* @param {number} value - 24-bit unsigned offset coefficient.
|
|
275
|
+
* @param {number} channel - 1, 2, or 3.
|
|
276
|
+
*/
|
|
277
|
+
async setOffsetCalibration(value, channel) {
|
|
278
|
+
const ch = channelConst(channel);
|
|
279
|
+
await writeRegChannel(this._conn, REG_OFFSET, value & 0xFFFFFF, ch, 3);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Read the 24-bit Full-Scale Calibration Register for the channel.
|
|
284
|
+
* @param {number} channel - 1, 2, or 3.
|
|
285
|
+
* @returns {Promise<number>} 24-bit unsigned gain coefficient.
|
|
286
|
+
*/
|
|
287
|
+
async getGainCalibration(channel) {
|
|
288
|
+
const ch = channelConst(channel);
|
|
289
|
+
return await readRegChannel(this._conn, REG_GAIN, ch, 3);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Write a 24-bit Full-Scale Calibration Register for the channel.
|
|
294
|
+
* @param {number} value - 24-bit unsigned gain coefficient.
|
|
295
|
+
* @param {number} channel - 1, 2, or 3.
|
|
296
|
+
*/
|
|
297
|
+
async setGainCalibration(value, channel) {
|
|
298
|
+
const ch = channelConst(channel);
|
|
299
|
+
await writeRegChannel(this._conn, REG_GAIN, value & 0xFFFFFF, ch, 3);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Enter standby (~10 µA, registers retained).
|
|
304
|
+
*/
|
|
305
|
+
async standby() {
|
|
306
|
+
const comm = Buffer.from([(commByte(REG_COMM, false, CH1) | STBY_SLEEP) & 0xFF]);
|
|
307
|
+
await this._conn.write(comm);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Exit standby and block until a fresh conversion is available.
|
|
312
|
+
*/
|
|
313
|
+
async wakeup() {
|
|
314
|
+
const comm = Buffer.from([(commByte(REG_COMM, false, CH1) | STBY_RUN) & 0xFF]);
|
|
315
|
+
await this._conn.write(comm);
|
|
316
|
+
await waitDrdy(this._conn);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Pulse the hardware RESET line. Requires a resetPin to have been supplied.
|
|
321
|
+
*/
|
|
322
|
+
async reset() {
|
|
323
|
+
if (!this._resetPin) {
|
|
324
|
+
throw new Error('reset() requires a resetPin to have been supplied at construction');
|
|
325
|
+
}
|
|
326
|
+
this._resetPin.set(false);
|
|
327
|
+
this._resetPin.set(true);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
module.exports = { AD7706Minimal, AD7706Full };
|