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,427 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// 8-bit registers
|
|
4
|
+
const REG_DISNOLOAD = 0x001;
|
|
5
|
+
const REG_PGA_V = 0x007;
|
|
6
|
+
const REG_PGA_IA = 0x008;
|
|
7
|
+
const REG_PGA_IB = 0x009;
|
|
8
|
+
const REG_WRITE_PROTECT = 0x040;
|
|
9
|
+
const REG_VERSION = 0x702;
|
|
10
|
+
const REG_EX_REF = 0x800;
|
|
11
|
+
|
|
12
|
+
// 16-bit registers
|
|
13
|
+
const REG_CONFIG = 0x102;
|
|
14
|
+
const REG_CF1DEN = 0x103;
|
|
15
|
+
const REG_CF2DEN = 0x104;
|
|
16
|
+
const REG_CFMODE = 0x107;
|
|
17
|
+
const REG_PHCALA = 0x108;
|
|
18
|
+
const REG_PFA = 0x10A;
|
|
19
|
+
const REG_PERIOD = 0x10E;
|
|
20
|
+
const REG_ALT_OUTPUT = 0x110;
|
|
21
|
+
const REG_INTERNAL_RES = 0x120;
|
|
22
|
+
|
|
23
|
+
// 24-bit / 32-bit registers
|
|
24
|
+
const REG_SAGLVL = 0x200;
|
|
25
|
+
const REG_ACCMODE = 0x201;
|
|
26
|
+
const REG_AP_NOLOAD = 0x203;
|
|
27
|
+
const REG_VAR_NOLOAD = 0x204;
|
|
28
|
+
const REG_VA_NOLOAD = 0x205;
|
|
29
|
+
const REG_AWATT = 0x212;
|
|
30
|
+
const REG_VRMS = 0x21C;
|
|
31
|
+
const REG_AENERGYA = 0x21E;
|
|
32
|
+
const REG_OVLVL = 0x224;
|
|
33
|
+
const REG_OILVL = 0x225;
|
|
34
|
+
const REG_IRQENA = 0x22C;
|
|
35
|
+
const REG_RSTIRQSTATA = 0x22E;
|
|
36
|
+
const REG_IRQENB = 0x22F;
|
|
37
|
+
const REG_RSTIRQSTATB = 0x231;
|
|
38
|
+
const REG_CRC = 0x37F;
|
|
39
|
+
const REG_AWGAIN = 0x282;
|
|
40
|
+
const REG_AWATTOS = 0x289;
|
|
41
|
+
|
|
42
|
+
const REG_120_UNLOCK_ADDR = 0x0FE;
|
|
43
|
+
const REG_120_UNLOCK = 0xAD;
|
|
44
|
+
const REG_120_VALUE = 0x30;
|
|
45
|
+
|
|
46
|
+
const ADC_FS_VOLTS = 0.5 / Math.sqrt(2); // 0.353553
|
|
47
|
+
const ADC_FS_CODE = 9032007;
|
|
48
|
+
const POWER_FS_CODE = 4862401;
|
|
49
|
+
const T_SAMPLE = 1.0 / 206900.0;
|
|
50
|
+
const PF_LSB = 1.0 / 32768.0;
|
|
51
|
+
const ANGLE_LSB = 1.0 / 223750.0;
|
|
52
|
+
const PHASE_LSB = 1.0 / 895000.0;
|
|
53
|
+
|
|
54
|
+
const BUS_I2C = 'i2c';
|
|
55
|
+
const BUS_SPI = 'spi';
|
|
56
|
+
const BUS_UART = 'uart';
|
|
57
|
+
|
|
58
|
+
const _delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
59
|
+
|
|
60
|
+
function _twos(value, width) {
|
|
61
|
+
const mask = (1 << width) - 1;
|
|
62
|
+
const sign = 1 << (width - 1);
|
|
63
|
+
value &= mask;
|
|
64
|
+
if (value & sign) value -= mask + 1;
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function _u16(data, offset = 0) {
|
|
69
|
+
return (data[offset] << 8) | data[offset + 1];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function _s16(data, offset = 0) {
|
|
73
|
+
return _twos(_u16(data, offset), 16);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function _u24(data, offset = 0) {
|
|
77
|
+
return (data[offset] << 16) | (data[offset + 1] << 8) | data[offset + 2];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function _s24(data, offset = 0) {
|
|
81
|
+
return _twos(_u24(data, offset), 24);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function _u32(data, offset = 0) {
|
|
85
|
+
return ((data[offset] << 24) | (data[offset + 1] << 16) |
|
|
86
|
+
(data[offset + 2] << 8) | data[offset + 3]) >>> 0;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class ADE7953Minimal {
|
|
91
|
+
/**
|
|
92
|
+
* ADE7953 single-phase multifunction metering IC — minimal interface.
|
|
93
|
+
*
|
|
94
|
+
* Reads voltage, current and active power/energy for Current Channel A.
|
|
95
|
+
* No register configuration beyond the mandatory power-up sequence and
|
|
96
|
+
* the caller's sensor scaling is performed.
|
|
97
|
+
*
|
|
98
|
+
* @param {import('../../connection/connection').Connection} connection
|
|
99
|
+
* Configured I²C, SPI or UART connection bound to the device.
|
|
100
|
+
* @param {number} voltageGain Real volts at the mains per volt at VP–VN.
|
|
101
|
+
* @param {number} currentGain Real amperes per volt at IAP–IAN.
|
|
102
|
+
* @param {string} [busType='i2c'] One of 'i2c', 'spi' or 'uart'.
|
|
103
|
+
*/
|
|
104
|
+
constructor(connection, voltageGain, currentGain, busType = 'i2c') {
|
|
105
|
+
this._conn = connection;
|
|
106
|
+
this._busType = busType;
|
|
107
|
+
this._voltageGain = Number(voltageGain);
|
|
108
|
+
this._currentGainA = Number(currentGain);
|
|
109
|
+
this._currentGainB = Number(currentGain);
|
|
110
|
+
this._pgaA = 1;
|
|
111
|
+
this._pgaB = 1;
|
|
112
|
+
this._pgaV = 1;
|
|
113
|
+
this._initChip();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async _initChip() {
|
|
117
|
+
await _delay(110);
|
|
118
|
+
await this._writeU8(REG_120_UNLOCK_ADDR, REG_120_UNLOCK);
|
|
119
|
+
await this._writeU16(REG_INTERNAL_RES, REG_120_VALUE);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
_addrForRead(reg, n) {
|
|
123
|
+
if (this._busType === BUS_SPI) {
|
|
124
|
+
return Buffer.from([
|
|
125
|
+
(reg >> 8) & 0x7F,
|
|
126
|
+
(reg & 0xFF) | 0x80,
|
|
127
|
+
0x00, 0x00,
|
|
128
|
+
]);
|
|
129
|
+
}
|
|
130
|
+
if (this._busType === BUS_UART) {
|
|
131
|
+
return Buffer.from([0x35, (reg >> 8) & 0xFF, reg & 0xFF]);
|
|
132
|
+
}
|
|
133
|
+
return Buffer.from([(reg >> 8) & 0xFF, reg & 0xFF]);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
_addrForWrite(reg, payload) {
|
|
137
|
+
if (this._busType === BUS_UART) {
|
|
138
|
+
const reversed = Buffer.from(payload).reverse();
|
|
139
|
+
return Buffer.concat([Buffer.from([0xCA, (reg >> 8) & 0xFF, reg & 0xFF]), reversed]);
|
|
140
|
+
}
|
|
141
|
+
return Buffer.concat([Buffer.from([(reg >> 8) & 0xFF, reg & 0xFF]), Buffer.from(payload)]);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async _read(reg, n) {
|
|
145
|
+
if (this._busType === BUS_SPI) {
|
|
146
|
+
const tx = this._addrForRead(reg, n);
|
|
147
|
+
const raw = await this._conn.writeRead(tx, n + 2);
|
|
148
|
+
return raw.slice(2);
|
|
149
|
+
}
|
|
150
|
+
if (this._busType === BUS_UART) {
|
|
151
|
+
await this._conn.write(this._addrForRead(reg, n));
|
|
152
|
+
await _delay(1);
|
|
153
|
+
const raw = await this._conn.read(n);
|
|
154
|
+
return Buffer.from(raw).reverse();
|
|
155
|
+
}
|
|
156
|
+
return await this._conn.writeRead(this._addrForRead(reg, n), n);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async _writePayload(reg, payload) {
|
|
160
|
+
await this._conn.write(this._addrForWrite(reg, payload));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async _writeU8(reg, value) {
|
|
164
|
+
await this._writePayload(reg, [(value & 0xFF)]);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async _writeU16(reg, value) {
|
|
168
|
+
await this._writePayload(reg, [(value >> 8) & 0xFF, value & 0xFF]);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async _writeU24(reg, value) {
|
|
172
|
+
await this._writePayload(reg, [(value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF]);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async _readU24(reg) {
|
|
176
|
+
const raw = await this._read(reg, 3);
|
|
177
|
+
return _u24(raw);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async _readS24(reg) {
|
|
181
|
+
const raw = await this._read(reg, 3);
|
|
182
|
+
return _s24(raw);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async _readU32(reg) {
|
|
186
|
+
const raw = await this._read(reg, 4);
|
|
187
|
+
return _u32(raw);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async _readU16(reg) {
|
|
191
|
+
const raw = await this._read(reg, 2);
|
|
192
|
+
return _u16(raw);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async _readS16(reg) {
|
|
196
|
+
const raw = await this._read(reg, 2);
|
|
197
|
+
return _s16(raw);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
_voltageScale() {
|
|
201
|
+
return (ADC_FS_VOLTS * this._voltageGain) / (ADC_FS_CODE * this._pgaV);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
_currentScale(gain) {
|
|
205
|
+
return (ADC_FS_VOLTS * gain) / ADC_FS_CODE;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
_powerScale(gain) {
|
|
209
|
+
return ((ADC_FS_VOLTS * ADC_FS_VOLTS) * this._voltageGain * gain) / POWER_FS_CODE;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
_energyScale(gain) {
|
|
213
|
+
return ((ADC_FS_VOLTS * ADC_FS_VOLTS) * this._voltageGain * gain * T_SAMPLE) / 3600.0;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Read the RMS voltage on the voltage channel.
|
|
218
|
+
* @returns {Promise<number>} Voltage in volts.
|
|
219
|
+
*/
|
|
220
|
+
async voltage() {
|
|
221
|
+
const raw = await this._readU24(REG_VRMS);
|
|
222
|
+
return raw * this._voltageScale();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Read the RMS current on Current Channel A.
|
|
227
|
+
* @returns {Promise<number>} Current in amperes.
|
|
228
|
+
*/
|
|
229
|
+
async current() {
|
|
230
|
+
const raw = await this._readU24(0x21A);
|
|
231
|
+
return raw * this._currentScale(this._currentGainA);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Read instantaneous active power on Current Channel A.
|
|
236
|
+
* @returns {Promise<number>} Active power in watts (signed).
|
|
237
|
+
*/
|
|
238
|
+
async activePower() {
|
|
239
|
+
const raw = await this._readS24(REG_AWATT);
|
|
240
|
+
return raw * this._powerScale(this._currentGainA);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Read the active-energy accumulator for Current Channel A.
|
|
245
|
+
* @returns {Promise<number>} Active energy in watt-hours accumulated since the previous call.
|
|
246
|
+
*/
|
|
247
|
+
async activeEnergy() {
|
|
248
|
+
const raw = await this._readS24(REG_AENERGYA);
|
|
249
|
+
return raw * this._energyScale(this._currentGainA);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
class ADE7953Full extends ADE7953Minimal {
|
|
255
|
+
/**
|
|
256
|
+
* ADE7953 full interface — extends ADE7953Minimal with Channel B,
|
|
257
|
+
* reactive/apparent measurements, calibration, accumulation modes,
|
|
258
|
+
* power-quality features (no-load, sag, peak, overcurrent/overvoltage),
|
|
259
|
+
* zero-crossing, REVP, alternate outputs, CF pulses, interrupts,
|
|
260
|
+
* checksum, write protection, reset and last-operation diagnostics.
|
|
261
|
+
*
|
|
262
|
+
* @param {import('../../connection/connection').Connection} connection
|
|
263
|
+
* @param {number} voltageGain
|
|
264
|
+
* @param {number} currentGain
|
|
265
|
+
* @param {string} [busType='i2c']
|
|
266
|
+
*/
|
|
267
|
+
constructor(connection, voltageGain, currentGain, busType = 'i2c') {
|
|
268
|
+
super(connection, voltageGain, currentGain, busType);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async configureChannelB(currentGainB) {
|
|
272
|
+
this._currentGainB = Number(currentGainB);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async currentB() {
|
|
276
|
+
const raw = await this._readU24(0x21B);
|
|
277
|
+
return raw * this._currentScale(this._currentGainB);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async activePowerB() {
|
|
281
|
+
const raw = await this._readS24(0x213);
|
|
282
|
+
return raw * this._powerScale(this._currentGainB);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async activeEnergyB() {
|
|
286
|
+
const raw = await this._readS24(0x21F);
|
|
287
|
+
return raw * this._energyScale(this._currentGainB);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async reactivePower() {
|
|
291
|
+
const raw = await this._readS24(0x214);
|
|
292
|
+
return raw * this._powerScale(this._currentGainA);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async reactivePowerB() {
|
|
296
|
+
const raw = await this._readS24(0x215);
|
|
297
|
+
return raw * this._powerScale(this._currentGainB);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
async reactiveEnergy() {
|
|
301
|
+
const raw = await this._readS24(0x220);
|
|
302
|
+
return raw * this._energyScale(this._currentGainA);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
async reactiveEnergyB() {
|
|
306
|
+
const raw = await this._readS24(0x221);
|
|
307
|
+
return raw * this._energyScale(this._currentGainB);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async apparentPower() {
|
|
311
|
+
const raw = await this._readS24(0x210);
|
|
312
|
+
return raw * this._powerScale(this._currentGainA);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async apparentPowerB() {
|
|
316
|
+
const raw = await this._readS24(0x211);
|
|
317
|
+
return raw * this._powerScale(this._currentGainB);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
async apparentEnergy() {
|
|
321
|
+
const raw = await this._readS24(0x222);
|
|
322
|
+
return raw * this._energyScale(this._currentGainA);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
async apparentEnergyB() {
|
|
326
|
+
const raw = await this._readS24(0x223);
|
|
327
|
+
return raw * this._energyScale(this._currentGainB);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
async powerFactor() {
|
|
331
|
+
const raw = await this._readS16(0x10A);
|
|
332
|
+
return raw * PF_LSB;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async linePeriod() {
|
|
336
|
+
const raw = await this._readU16(REG_PERIOD);
|
|
337
|
+
return (raw + 1) * ANGLE_LSB;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
async lineFrequency() {
|
|
341
|
+
return 1.0 / (await this.linePeriod());
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async setPga(channel, gain) {
|
|
345
|
+
const bits = {1:0, 2:1, 4:2, 8:3, 16:4, 22:5}[gain];
|
|
346
|
+
if (bits === undefined) return;
|
|
347
|
+
if (channel === 'a') { await this._writeU8(REG_PGA_IA, bits); this._pgaA = gain; }
|
|
348
|
+
else if (channel === 'b') { await this._writeU8(REG_PGA_IB, bits); this._pgaB = gain; }
|
|
349
|
+
else if (channel === 'v') { await this._writeU8(REG_PGA_V, bits); this._pgaV = gain; }
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
async setPhaseCalibration(channel, delayS) {
|
|
353
|
+
let mag = Math.round(Math.abs(delayS) / PHASE_LSB);
|
|
354
|
+
if (mag > 0x1FF) mag = 0x1FF;
|
|
355
|
+
let raw = mag & 0x1FF;
|
|
356
|
+
if (delayS >= 0) raw |= 0x200;
|
|
357
|
+
if (channel === 'a') await this._writeU16(REG_PHCALA, raw);
|
|
358
|
+
else if (channel === 'b') await this._writeU16(0x109, raw);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
async setGainCalibration(reg, value) {
|
|
362
|
+
await this._writeU24(reg, value & 0xFFFFFF);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async gainCalibration(reg) {
|
|
366
|
+
return await this._readU24(reg);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
async setOffsetCalibration(reg, value) {
|
|
370
|
+
await this._writeU24(reg, value & 0xFFFFFF);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
async offsetCalibration(reg) {
|
|
374
|
+
return await this._readS24(reg);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
async checksum() {
|
|
378
|
+
return await this._readU32(REG_CRC);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
async enableChecksum(enabled) {
|
|
382
|
+
let cfg = await this._readU16(REG_CONFIG);
|
|
383
|
+
if (enabled) cfg |= (1 << 8);
|
|
384
|
+
else cfg &= ~(1 << 8);
|
|
385
|
+
await this._writeU16(REG_CONFIG, cfg);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
async configureOvervoltage(threshold) {
|
|
389
|
+
let raw = (threshold * (ADC_FS_CODE * this._pgaV)) / (ADC_FS_VOLTS * this._voltageGain);
|
|
390
|
+
if (raw < 0) raw = 0;
|
|
391
|
+
if (raw > 0xFFFFFF) raw = 0xFFFFFF;
|
|
392
|
+
await this._writeU24(REG_OVLVL, raw);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
async configureOvercurrent(threshold) {
|
|
396
|
+
let raw = (threshold * ADC_FS_CODE) / ADC_FS_VOLTS;
|
|
397
|
+
if (raw < 0) raw = 0;
|
|
398
|
+
if (raw > 0xFFFFFF) raw = 0xFFFFFF;
|
|
399
|
+
await this._writeU24(REG_OILVL, raw);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
async reset() {
|
|
403
|
+
let cfg = await this._readU16(REG_CONFIG);
|
|
404
|
+
cfg |= (1 << 7);
|
|
405
|
+
await this._writeU16(REG_CONFIG, cfg);
|
|
406
|
+
await _delay(110);
|
|
407
|
+
await this._writeU8(REG_120_UNLOCK_ADDR, REG_120_UNLOCK);
|
|
408
|
+
await this._writeU16(REG_INTERNAL_RES, REG_120_VALUE);
|
|
409
|
+
this._pgaA = 1;
|
|
410
|
+
this._pgaB = 1;
|
|
411
|
+
this._pgaV = 1;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
async version() {
|
|
415
|
+
const raw = await this._read(REG_VERSION, 1);
|
|
416
|
+
return raw[0];
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
module.exports = {
|
|
422
|
+
ADE7953Minimal,
|
|
423
|
+
ADE7953Full,
|
|
424
|
+
REG_VERSION,
|
|
425
|
+
REG_AWGAIN,
|
|
426
|
+
REG_AWATTOS,
|
|
427
|
+
};
|
|
@@ -11,7 +11,7 @@ const _REG_CAL = 0x05;
|
|
|
11
11
|
* INA219 26V, 12-bit current/voltage/power monitor — minimal interface.
|
|
12
12
|
*
|
|
13
13
|
* Provides bus voltage, shunt voltage, current, and power readings with no
|
|
14
|
-
* configuration beyond the
|
|
14
|
+
* configuration beyond the connection and shunt resistor. Writes the
|
|
15
15
|
* Calibration Register automatically at construction.
|
|
16
16
|
*
|
|
17
17
|
* Default chip configuration (power-on defaults, not rewritten):
|
|
@@ -23,43 +23,43 @@ const _REG_CAL = 0x05;
|
|
|
23
23
|
*/
|
|
24
24
|
class INA219Minimal {
|
|
25
25
|
/**
|
|
26
|
-
* @param {
|
|
26
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SMBus connection (writeRead, write).
|
|
27
27
|
* @param {number} [rShunt=0.1] - Shunt resistor value in ohms.
|
|
28
28
|
* @param {number} [maxCurrent=2.0] - Maximum expected current in amperes.
|
|
29
29
|
*/
|
|
30
|
-
constructor(
|
|
31
|
-
this.
|
|
30
|
+
constructor(connection, rShunt = 0.1, maxCurrent = 2.0) {
|
|
31
|
+
this._conn = connection;
|
|
32
32
|
this._currentLsb = maxCurrent / 32768;
|
|
33
33
|
this._cal = Math.floor(0.04096 / (this._currentLsb * rShunt)) & 0xFFFE;
|
|
34
34
|
this._writeReg(_REG_CAL, this._cal);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
_writeReg(reg, value) {
|
|
37
|
+
async _writeReg(reg, value) {
|
|
38
38
|
const buf = Buffer.alloc(3);
|
|
39
39
|
buf[0] = reg;
|
|
40
40
|
buf.writeUInt16BE(value, 1);
|
|
41
|
-
this.
|
|
41
|
+
await this._conn.write(buf);
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
_readReg(reg) {
|
|
45
|
-
return this.
|
|
44
|
+
async _readReg(reg) {
|
|
45
|
+
return (await this._conn.writeRead(Buffer.from([reg]), 2)).readUInt16BE(0);
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
_readRegSigned(reg) {
|
|
49
|
-
return this.
|
|
48
|
+
async _readRegSigned(reg) {
|
|
49
|
+
return (await this._conn.writeRead(Buffer.from([reg]), 2)).readInt16BE(0);
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
/** @return {number} Bus voltage in volts ((raw >> 3) × 4 mV LSB). */
|
|
53
|
-
voltage() { return (this._readReg(_REG_BUS) >> 3) * 4e-3; }
|
|
52
|
+
/** @return {Promise<number>} Bus voltage in volts ((raw >> 3) × 4 mV LSB). */
|
|
53
|
+
async voltage() { return ((await this._readReg(_REG_BUS)) >> 3) * 4e-3; }
|
|
54
54
|
|
|
55
|
-
/** @return {number} Shunt voltage in volts, signed (raw × 10 µV LSB). */
|
|
56
|
-
shuntVoltage() { return this._readRegSigned(_REG_SHUNT) * 10e-6; }
|
|
55
|
+
/** @return {Promise<number>} Shunt voltage in volts, signed (raw × 10 µV LSB). */
|
|
56
|
+
async shuntVoltage() { return (await this._readRegSigned(_REG_SHUNT)) * 10e-6; }
|
|
57
57
|
|
|
58
|
-
/** @return {number} Current in amperes, signed. */
|
|
59
|
-
current() { return this._readRegSigned(_REG_CURRENT) * this._currentLsb; }
|
|
58
|
+
/** @return {Promise<number>} Current in amperes, signed. */
|
|
59
|
+
async current() { return (await this._readRegSigned(_REG_CURRENT)) * this._currentLsb; }
|
|
60
60
|
|
|
61
|
-
/** @return {number} Power in watts (raw × 20 × current LSB). */
|
|
62
|
-
power() { return this._readReg(_REG_POWER) * 20 * this._currentLsb; }
|
|
61
|
+
/** @return {Promise<number>} Power in watts (raw × 20 × current LSB). */
|
|
62
|
+
async power() { return (await this._readReg(_REG_POWER)) * 20 * this._currentLsb; }
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
/**
|
|
@@ -99,12 +99,12 @@ class INA219Full extends INA219Minimal {
|
|
|
99
99
|
static MODE_SHUNT_BUS_CONT = 7;
|
|
100
100
|
|
|
101
101
|
/**
|
|
102
|
-
* @param {
|
|
102
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SMBus connection.
|
|
103
103
|
* @param {number} [rShunt=0.1] - Shunt resistor value in ohms.
|
|
104
104
|
* @param {number} [maxCurrent=2.0] - Maximum expected current in amperes.
|
|
105
105
|
*/
|
|
106
|
-
constructor(
|
|
107
|
-
super(
|
|
106
|
+
constructor(connection, rShunt = 0.1, maxCurrent = 2.0) {
|
|
107
|
+
super(connection, rShunt, maxCurrent);
|
|
108
108
|
this._savedMode = INA219Full.MODE_SHUNT_BUS_CONT;
|
|
109
109
|
}
|
|
110
110
|
|
|
@@ -115,39 +115,44 @@ class INA219Full extends INA219Minimal {
|
|
|
115
115
|
* @param {number} [badc=0x03] - Bus ADC resolution/averaging: 0x00–0x0F.
|
|
116
116
|
* @param {number} [sadc=0x03] - Shunt ADC resolution/averaging: 0x00–0x0F.
|
|
117
117
|
* @param {number} [mode=7] - Operating mode 0–7.
|
|
118
|
+
* @returns {Promise<void>}
|
|
118
119
|
*/
|
|
119
|
-
configure(brng = 1, pga = 3, badc = 0x03, sadc = 0x03, mode = 7) {
|
|
120
|
+
async configure(brng = 1, pga = 3, badc = 0x03, sadc = 0x03, mode = 7) {
|
|
120
121
|
const config = ((brng & 1) << 13) | ((pga & 3) << 11) | ((badc & 0x0F) << 7) | ((sadc & 0x0F) << 3) | (mode & 7);
|
|
121
122
|
this._savedMode = mode & 7;
|
|
122
|
-
this._writeReg(_REG_CONFIG, config);
|
|
123
|
-
this._writeReg(_REG_CAL, this._cal);
|
|
123
|
+
await this._writeReg(_REG_CONFIG, config);
|
|
124
|
+
await this._writeReg(_REG_CAL, this._cal);
|
|
124
125
|
}
|
|
125
126
|
|
|
126
|
-
/** @return {boolean} True if a conversion completed since the last read. */
|
|
127
|
-
conversionReady() { return !!(this._readReg(_REG_BUS) & 0x02); }
|
|
127
|
+
/** @return {Promise<boolean>} True if a conversion completed since the last read. */
|
|
128
|
+
async conversionReady() { return !!((await this._readReg(_REG_BUS)) & 0x02); }
|
|
128
129
|
|
|
129
|
-
/** @return {boolean} True if an arithmetic overflow occurred. */
|
|
130
|
-
overflow() { return !!(this._readReg(_REG_BUS) & 0x01); }
|
|
130
|
+
/** @return {Promise<boolean>} True if an arithmetic overflow occurred. */
|
|
131
|
+
async overflow() { return !!((await this._readReg(_REG_BUS)) & 0x01); }
|
|
131
132
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
this._writeReg(
|
|
133
|
+
/** @returns {Promise<void>} */
|
|
134
|
+
async reset() {
|
|
135
|
+
await this._writeReg(_REG_CONFIG, 0x8000);
|
|
136
|
+
await this._writeReg(_REG_CAL, this._cal);
|
|
135
137
|
}
|
|
136
138
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
+
/** @returns {Promise<void>} */
|
|
140
|
+
async shutdown() {
|
|
141
|
+
const config = await this._readReg(_REG_CONFIG);
|
|
139
142
|
this._savedMode = config & 7;
|
|
140
|
-
this._writeReg(_REG_CONFIG, config & 0xFFF8);
|
|
143
|
+
await this._writeReg(_REG_CONFIG, config & 0xFFF8);
|
|
141
144
|
}
|
|
142
145
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
+
/** @returns {Promise<void>} */
|
|
147
|
+
async wake() {
|
|
148
|
+
const config = await this._readReg(_REG_CONFIG);
|
|
149
|
+
await this._writeReg(_REG_CONFIG, (config & 0xFFF8) | this._savedMode);
|
|
146
150
|
}
|
|
147
151
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
this.
|
|
152
|
+
/** @returns {Promise<void>} */
|
|
153
|
+
async trigger() {
|
|
154
|
+
const config = await this._readReg(_REG_CONFIG);
|
|
155
|
+
await this._writeReg(_REG_CONFIG, config);
|
|
151
156
|
}
|
|
152
157
|
}
|
|
153
158
|
|