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
|
@@ -26,7 +26,12 @@ const REG_OLATB = 0x15;
|
|
|
26
26
|
* MCP23017 16-bit bidirectional I/O port expander — minimal interface.
|
|
27
27
|
*
|
|
28
28
|
* Provides access to 16 GPIO pins (GPA0–GPA7 and GPB0–GPB7) via a pin()
|
|
29
|
-
* factory. Each returned pin
|
|
29
|
+
* factory. Each returned pin exposes async read()/write() (not onoff's
|
|
30
|
+
* synchronous readSync()/writeSync() — Connection is async everywhere, so
|
|
31
|
+
* a synchronous pin proxy can no longer correctly return a value). Call
|
|
32
|
+
* pin.asGpio() for a synchronous opengpio-shaped facade when a pin needs
|
|
33
|
+
* to be passed somewhere a real opengpio Input/Output is expected — see
|
|
34
|
+
* _Pin.asGpio() below.
|
|
30
35
|
*
|
|
31
36
|
* At construction, all pins initialise as inputs except GPA7 and GPB7
|
|
32
37
|
* which are output-only on the hardware and are forced to output mode.
|
|
@@ -37,14 +42,15 @@ const REG_OLATB = 0x15;
|
|
|
37
42
|
*/
|
|
38
43
|
class Mcp23017Minimal {
|
|
39
44
|
/**
|
|
40
|
-
* @param {
|
|
45
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
|
|
41
46
|
* @param {number} [addr=0x20] - 7-bit I²C address (default 0x20, range 0x20–0x27).
|
|
42
47
|
*/
|
|
43
|
-
constructor(
|
|
44
|
-
this.
|
|
45
|
-
this._addr
|
|
46
|
-
this._shadow
|
|
48
|
+
constructor(connection, addr = 0x20) {
|
|
49
|
+
this._conn = connection;
|
|
50
|
+
this._addr = addr;
|
|
51
|
+
this._shadow = [0, 0];
|
|
47
52
|
|
|
53
|
+
// fire-and-forget: underlying I2C write is synchronous
|
|
48
54
|
this._writeReg(REG_OLATA, 0x00);
|
|
49
55
|
this._writeReg(REG_OLATB, 0x00);
|
|
50
56
|
this._writeReg(REG_IODIRA, 0x7F);
|
|
@@ -55,28 +61,29 @@ class Mcp23017Minimal {
|
|
|
55
61
|
this._writeReg(REG_GPPUB, 0x00);
|
|
56
62
|
}
|
|
57
63
|
|
|
58
|
-
_writeReg(reg, value) {
|
|
59
|
-
this.
|
|
64
|
+
async _writeReg(reg, value) {
|
|
65
|
+
await this._conn.write(Buffer.from([reg, value & 0xFF]));
|
|
60
66
|
}
|
|
61
67
|
|
|
62
|
-
_readReg(reg) {
|
|
63
|
-
|
|
68
|
+
async _readReg(reg) {
|
|
69
|
+
const buf = await this._conn.writeRead(Buffer.from([reg]), 1);
|
|
70
|
+
return buf[0];
|
|
64
71
|
}
|
|
65
72
|
|
|
66
|
-
_writePort(port, mask) {
|
|
67
|
-
this._writeReg(REG_OLATA + port, mask & 0xFF);
|
|
73
|
+
async _writePort(port, mask) {
|
|
74
|
+
await this._writeReg(REG_OLATA + port, mask & 0xFF);
|
|
68
75
|
}
|
|
69
76
|
|
|
70
|
-
_readPort(port) {
|
|
77
|
+
async _readPort(port) {
|
|
71
78
|
return this._readReg(REG_GPIOA + port);
|
|
72
79
|
}
|
|
73
80
|
|
|
74
|
-
_setPin(n, value) {
|
|
81
|
+
async _setPin(n, value) {
|
|
75
82
|
const port = n >> 3;
|
|
76
83
|
const bit = n & 7;
|
|
77
84
|
if (value) this._shadow[port] |= (1 << bit);
|
|
78
85
|
else this._shadow[port] &= ~(1 << bit);
|
|
79
|
-
this._writePort(port, this._shadow[port]);
|
|
86
|
+
await this._writePort(port, this._shadow[port]);
|
|
80
87
|
}
|
|
81
88
|
|
|
82
89
|
/**
|
|
@@ -89,7 +96,7 @@ class Mcp23017Minimal {
|
|
|
89
96
|
*
|
|
90
97
|
* @param {number} n - Pin index (0–15).
|
|
91
98
|
* @param {string} [direction='in'] - Initial direction: 'in' or 'out'.
|
|
92
|
-
* @returns {_Pin} Pin proxy
|
|
99
|
+
* @returns {_Pin} Pin proxy.
|
|
93
100
|
*/
|
|
94
101
|
pin(n, direction = 'in') {
|
|
95
102
|
return new _Pin(this, n, direction);
|
|
@@ -99,9 +106,9 @@ class Mcp23017Minimal {
|
|
|
99
106
|
* Read all 8 pins of a port as a bitmask.
|
|
100
107
|
*
|
|
101
108
|
* @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
|
|
102
|
-
* @returns {number} 8-bit bitmask (bit 0 = pin 0 of the port).
|
|
109
|
+
* @returns {Promise<number>} 8-bit bitmask (bit 0 = pin 0 of the port).
|
|
103
110
|
*/
|
|
104
|
-
readPort(port = 0) {
|
|
111
|
+
async readPort(port = 0) {
|
|
105
112
|
return this._readPort(port);
|
|
106
113
|
}
|
|
107
114
|
|
|
@@ -111,24 +118,26 @@ class Mcp23017Minimal {
|
|
|
111
118
|
*
|
|
112
119
|
* @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
|
|
113
120
|
* @param {number} mask - 8-bit output mask.
|
|
121
|
+
* @returns {Promise<void>}
|
|
114
122
|
*/
|
|
115
|
-
writePort(port = 0, mask = 0x00) {
|
|
123
|
+
async writePort(port = 0, mask = 0x00) {
|
|
116
124
|
this._shadow[port] = mask & 0xFF;
|
|
117
|
-
this._writePort(port, this._shadow[port]);
|
|
125
|
+
await this._writePort(port, this._shadow[port]);
|
|
118
126
|
}
|
|
119
127
|
|
|
120
128
|
/**
|
|
121
129
|
* Configure the direction (IODIR) of a full port.
|
|
122
130
|
* @param {number} port - 0 = PORTA (IODIRA), 1 = PORTB (IODIRB).
|
|
123
131
|
* @param {number} mask - 8-bit mask; bit = 1 → input, 0 → output.
|
|
132
|
+
* @returns {Promise<void>}
|
|
124
133
|
*/
|
|
125
|
-
configureDirection(port, mask) {
|
|
126
|
-
this._writeReg(REG_IODIRA + (port & 1), mask & 0xFF);
|
|
134
|
+
async configureDirection(port, mask) {
|
|
135
|
+
await this._writeReg(REG_IODIRA + (port & 1), mask & 0xFF);
|
|
127
136
|
}
|
|
128
137
|
}
|
|
129
138
|
|
|
130
139
|
/**
|
|
131
|
-
* GPIO proxy for a single MCP23017 pin
|
|
140
|
+
* GPIO proxy for a single MCP23017 pin.
|
|
132
141
|
*
|
|
133
142
|
* Obtain via Mcp23017Minimal.pin(n). Do not instantiate directly.
|
|
134
143
|
*/
|
|
@@ -136,7 +145,7 @@ class _Pin {
|
|
|
136
145
|
/**
|
|
137
146
|
* @param {Mcp23017Minimal} chip - Parent driver instance.
|
|
138
147
|
* @param {number} n - Pin index (0–15).
|
|
139
|
-
* @param {string} direction - 'in' or 'out'.
|
|
148
|
+
* @param {string} direction - 'in' or 'out'; fixed for the pin's lifetime.
|
|
140
149
|
*/
|
|
141
150
|
constructor(chip, n, direction) {
|
|
142
151
|
this._chip = chip;
|
|
@@ -144,78 +153,104 @@ class _Pin {
|
|
|
144
153
|
this._direction = direction;
|
|
145
154
|
}
|
|
146
155
|
|
|
147
|
-
/** @type {string}
|
|
156
|
+
/** @type {string} Pin direction ('in' or 'out'). */
|
|
148
157
|
get direction() { return this._direction; }
|
|
149
158
|
|
|
150
159
|
/**
|
|
151
|
-
* Read pin
|
|
152
|
-
* @returns {number} 0 or 1.
|
|
160
|
+
* Read pin.
|
|
161
|
+
* @returns {Promise<number>} 0 or 1.
|
|
153
162
|
*/
|
|
154
|
-
|
|
155
|
-
return (this._chip._readPort(this._n >> 3) >> (this._n & 7)) & 1;
|
|
163
|
+
async read() {
|
|
164
|
+
return (await this._chip._readPort(this._n >> 3) >> (this._n & 7)) & 1;
|
|
156
165
|
}
|
|
157
166
|
|
|
158
167
|
/**
|
|
159
|
-
* Write pin
|
|
168
|
+
* Write pin.
|
|
160
169
|
* @param {number} value - 0 or 1.
|
|
170
|
+
* @returns {Promise<void>}
|
|
161
171
|
*/
|
|
162
|
-
|
|
163
|
-
this._chip._setPin(this._n, value ? 1 : 0);
|
|
172
|
+
async write(value) {
|
|
173
|
+
await this._chip._setPin(this._n, value ? 1 : 0);
|
|
164
174
|
}
|
|
165
175
|
|
|
166
176
|
/**
|
|
167
|
-
*
|
|
168
|
-
* @param {
|
|
169
|
-
|
|
170
|
-
read(callback) {
|
|
171
|
-
try { callback(null, this.readSync()); } catch (e) { callback(e); }
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Write pin asynchronously.
|
|
176
|
-
* @param {number} value - 0 or 1.
|
|
177
|
-
* @param {function} callback - Node-style callback(err).
|
|
177
|
+
* Set pin direction.
|
|
178
|
+
* @param {string} direction - 'in' or 'out'.
|
|
179
|
+
* @returns {Promise<void>}
|
|
178
180
|
*/
|
|
179
|
-
|
|
180
|
-
|
|
181
|
+
async setDirection(direction) {
|
|
182
|
+
this._direction = direction;
|
|
181
183
|
}
|
|
182
184
|
|
|
183
185
|
/**
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
186
|
+
* Return a synchronous opengpio-shaped Input/Output facade for this
|
|
187
|
+
* pin — a boolean `.value` getter/setter, fixed `direction`, and
|
|
188
|
+
* `stop()` — so it can be passed anywhere code expects a real
|
|
189
|
+
* opengpio GPIO line.
|
|
190
|
+
*
|
|
191
|
+
* Output-direction facades read back the shadow register directly
|
|
192
|
+
* (authoritative) and write through fire-and-forget. Input-direction
|
|
193
|
+
* facades are backed by a 5 ms background poll that refreshes a
|
|
194
|
+
* cached value — `.value` is therefore eventually consistent, not a
|
|
195
|
+
* live bus read on every access like this pin's own async read().
|
|
196
|
+
* Call stop() to release the background poll when done.
|
|
197
|
+
*
|
|
198
|
+
* @returns {{direction: string, value: boolean, stop: function}} opengpio-shaped facade.
|
|
187
199
|
*/
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
200
|
+
asGpio() {
|
|
201
|
+
const pin = this;
|
|
202
|
+
if (this._direction === 'out') {
|
|
203
|
+
const port = this._n >> 3;
|
|
204
|
+
const bit = this._n & 7;
|
|
205
|
+
return {
|
|
206
|
+
get direction() { return 'out'; },
|
|
207
|
+
get value() { return ((pin._chip._shadow[port] >> bit) & 1) === 1; },
|
|
208
|
+
set value(v) { pin._chip._setPin(pin._n, v ? 1 : 0); }, // fire-and-forget
|
|
209
|
+
stop() {},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
let cached = false;
|
|
213
|
+
const timer = setInterval(async () => {
|
|
214
|
+
cached = (await pin.read()) === 1;
|
|
215
|
+
}, 5);
|
|
216
|
+
return {
|
|
217
|
+
get direction() { return 'in'; },
|
|
218
|
+
get value() { return cached; },
|
|
219
|
+
set value(_v) { throw new Error('cannot set value on an input pin'); },
|
|
220
|
+
stop() { clearInterval(timer); },
|
|
221
|
+
};
|
|
193
222
|
}
|
|
194
223
|
|
|
195
224
|
/** Release the pin (no-op; shadow state preserved). */
|
|
196
|
-
|
|
225
|
+
stop() {}
|
|
197
226
|
}
|
|
198
227
|
|
|
199
228
|
/**
|
|
200
229
|
* MCP23017 full interface — extends Minimal with interrupt support,
|
|
201
230
|
* per-pin pull-ups, and interrupt-on-change configuration.
|
|
202
231
|
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
232
|
+
* Level 3: the chip has two independent INT lines (INTA/INTB), one per
|
|
233
|
+
* port. onInterrupt()/offInterrupt() subscribe/unsubscribe both ports at
|
|
234
|
+
* once (delivered via one shared InputPin — wire IOCON.MIRROR or external
|
|
235
|
+
* wiring so a single physical line carries both); onInterruptPort()/
|
|
236
|
+
* offInterruptPort() target one port with its own InputPin. pollInterrupt()
|
|
237
|
+
* reads and clears INTF; readCapture() reads INTCAP (the latched pin state)
|
|
238
|
+
* separately. Per-pin watch()/unwatch() are also available.
|
|
207
239
|
*/
|
|
208
240
|
class Mcp23017Full extends Mcp23017Minimal {
|
|
209
241
|
/**
|
|
210
|
-
* @param {
|
|
242
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
|
|
211
243
|
* @param {number} [addr=0x20] - 7-bit I²C address.
|
|
212
244
|
*/
|
|
213
|
-
constructor(
|
|
214
|
-
super(
|
|
215
|
-
this.
|
|
216
|
-
this.
|
|
217
|
-
this.
|
|
218
|
-
this.
|
|
245
|
+
constructor(connection, addr = 0x20) {
|
|
246
|
+
super(connection, addr);
|
|
247
|
+
this._callbackBoth = null;
|
|
248
|
+
this._callbackPort = [null, null];
|
|
249
|
+
this._intPinUsed = [null, null];
|
|
250
|
+
this._pollTimer = [null, null];
|
|
251
|
+
this._watchers = [{}, {}]; // per port: { [bit]: { handler, trigger, lastState } }
|
|
252
|
+
this._edgeHandlerA = () => { this._handleEdge(0); };
|
|
253
|
+
this._edgeHandlerB = () => { this._handleEdge(1); };
|
|
219
254
|
}
|
|
220
255
|
|
|
221
256
|
/**
|
|
@@ -237,9 +272,10 @@ class Mcp23017Full extends Mcp23017Minimal {
|
|
|
237
272
|
*
|
|
238
273
|
* @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
|
|
239
274
|
* @param {number} mask - 8-bit mask: 1 = enable 100 kΩ pull-up.
|
|
275
|
+
* @returns {Promise<void>}
|
|
240
276
|
*/
|
|
241
|
-
configurePullup(port = 0, mask = 0x00) {
|
|
242
|
-
this._writeReg(REG_GPPUA + port, mask & 0xFF);
|
|
277
|
+
async configurePullup(port = 0, mask = 0x00) {
|
|
278
|
+
await this._writeReg(REG_GPPUA + port, mask & 0xFF);
|
|
243
279
|
}
|
|
244
280
|
|
|
245
281
|
/**
|
|
@@ -247,105 +283,156 @@ class Mcp23017Full extends Mcp23017Minimal {
|
|
|
247
283
|
*
|
|
248
284
|
* @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
|
|
249
285
|
* @param {number} mask - 8-bit mask: 1 = invert GPIO read.
|
|
286
|
+
* @returns {Promise<void>}
|
|
250
287
|
*/
|
|
251
|
-
configurePolarity(port = 0, mask = 0x00) {
|
|
252
|
-
this._writeReg(REG_IPOLA + port, mask & 0xFF);
|
|
288
|
+
async configurePolarity(port = 0, mask = 0x00) {
|
|
289
|
+
await this._writeReg(REG_IPOLA + port, mask & 0xFF);
|
|
253
290
|
}
|
|
254
291
|
|
|
255
292
|
/**
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
* delivery. The callback receives the changed-pin bitmask for the port.
|
|
261
|
-
*
|
|
262
|
-
* @param {number} [port=0] - Port index: 0 = PORTA (INTA), 1 = PORTB (INTB).
|
|
263
|
-
* @param {string|null} intGpioPath - Sysfs GPIO value file for edge delivery, or null for polling.
|
|
264
|
-
* @param {function} callback - Called with (changedMask) on any input change.
|
|
293
|
+
* Set DEFVAL register for default-compare interrupt mode.
|
|
294
|
+
* @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
|
|
295
|
+
* @param {number} mask - 8-bit default compare value.
|
|
296
|
+
* @returns {Promise<void>}
|
|
265
297
|
*/
|
|
266
|
-
|
|
267
|
-
this.
|
|
268
|
-
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
269
|
-
|
|
270
|
-
this._writeReg(REG_GPINTENA + port, 0xFF);
|
|
271
|
-
this._writeReg(REG_INTCONA + port, 0x00);
|
|
272
|
-
|
|
273
|
-
if (intGpioPath) {
|
|
274
|
-
try {
|
|
275
|
-
const fs = require('fs');
|
|
276
|
-
const ep = require('epoll').Epoll;
|
|
277
|
-
const fd = fs.openSync(intGpioPath, 'r');
|
|
278
|
-
const poll = new ep((err, fd2) => {
|
|
279
|
-
fs.readSync(fd2, Buffer.alloc(1), 0, 1, 0);
|
|
280
|
-
const changed = this.clearInterrupt(port);
|
|
281
|
-
if (changed) this._dispatch(port, changed);
|
|
282
|
-
});
|
|
283
|
-
poll.add(fd, ep.EPOLLPRI);
|
|
284
|
-
} catch (_) {
|
|
285
|
-
this._startPolling();
|
|
286
|
-
}
|
|
287
|
-
} else {
|
|
288
|
-
this._startPolling();
|
|
289
|
-
}
|
|
298
|
+
async setDefaultValue(port = 0, mask = 0x00) {
|
|
299
|
+
await this._writeReg(REG_DEFVALA + (port & 1), mask & 0xFF);
|
|
290
300
|
}
|
|
291
301
|
|
|
292
|
-
|
|
293
|
-
this.
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
302
|
+
async _armPort(port, intPin) {
|
|
303
|
+
await this._writeReg(REG_INTCONA + port, 0x00); // interrupt-on-change mode
|
|
304
|
+
await this._writeReg(REG_GPINTENA + port, 0xFF);
|
|
305
|
+
this._intPinUsed[port] = intPin;
|
|
306
|
+
if (this._pollTimer[port]) { clearInterval(this._pollTimer[port]); this._pollTimer[port] = null; }
|
|
307
|
+
if (intPin) {
|
|
308
|
+
await intPin.onEdge(port === 0 ? this._edgeHandlerA : this._edgeHandlerB, 'falling');
|
|
309
|
+
} else {
|
|
310
|
+
this._pollTimer[port] = setInterval(() => { this._handleEdge(port); }, 5);
|
|
311
|
+
}
|
|
298
312
|
}
|
|
299
313
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
314
|
+
/**
|
|
315
|
+
* Subscribe to INT assertions on both ports.
|
|
316
|
+
*
|
|
317
|
+
* The common case: both INT lines share one physical GPIO, either via
|
|
318
|
+
* IOCON.MIRROR (options.mirror = true) or external wiring. Wires
|
|
319
|
+
* options.intPin (or connection.intPin if omitted) to poll both ports
|
|
320
|
+
* on every edge. Falls back to two independent 5 ms polling loops if no
|
|
321
|
+
* InputPin is available.
|
|
322
|
+
*
|
|
323
|
+
* @param {function} callback - Called with (port, status) on any input change.
|
|
324
|
+
* @param {object} [options]
|
|
325
|
+
* @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
|
|
326
|
+
* @param {boolean} [options.mirror=false] - Set IOCON.MIRROR so either port's interrupt activates both INTA and INTB.
|
|
327
|
+
* @returns {Promise<void>}
|
|
328
|
+
*/
|
|
329
|
+
async onInterrupt(callback, options = {}) {
|
|
330
|
+
this._callbackBoth = callback;
|
|
331
|
+
const iocon = await this._readReg(REG_IOCON);
|
|
332
|
+
await this._writeReg(REG_IOCON, options.mirror ? (iocon | (1 << 6)) : iocon);
|
|
333
|
+
const pin = options.intPin ?? this._conn.intPin ?? null;
|
|
334
|
+
await this._armPort(0, pin);
|
|
335
|
+
await this._armPort(1, pin);
|
|
308
336
|
}
|
|
309
337
|
|
|
310
338
|
/**
|
|
311
|
-
*
|
|
339
|
+
* Subscribe to INT assertions on a single port.
|
|
312
340
|
*
|
|
313
|
-
*
|
|
341
|
+
* Use this (with a dedicated InputPin per call) when INTA and INTB are
|
|
342
|
+
* wired to two separate GPIOs.
|
|
314
343
|
*
|
|
315
|
-
* @param {number}
|
|
316
|
-
* @
|
|
344
|
+
* @param {number} port - Port index: 0 = PORTA (INTA), 1 = PORTB (INTB).
|
|
345
|
+
* @param {function} callback - Called with (status) on any input change.
|
|
346
|
+
* @param {object} [options]
|
|
347
|
+
* @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
|
|
348
|
+
* @returns {Promise<void>}
|
|
317
349
|
*/
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
350
|
+
async onInterruptPort(port, callback, options = {}) {
|
|
351
|
+
port &= 1;
|
|
352
|
+
this._callbackPort[port] = callback;
|
|
353
|
+
await this._armPort(port, options.intPin ?? this._conn.intPin ?? null);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Unsubscribe and stop delivery on both ports.
|
|
358
|
+
* @returns {Promise<void>}
|
|
359
|
+
*/
|
|
360
|
+
async offInterrupt() {
|
|
361
|
+
await this.offInterruptPort(0);
|
|
362
|
+
await this.offInterruptPort(1);
|
|
363
|
+
this._callbackBoth = null;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Unsubscribe and stop delivery on a single port.
|
|
368
|
+
* @param {number} port - Port index: 0 = PORTA, 1 = PORTB.
|
|
369
|
+
* @returns {Promise<void>}
|
|
370
|
+
*/
|
|
371
|
+
async offInterruptPort(port) {
|
|
372
|
+
port &= 1;
|
|
373
|
+
await this._writeReg(REG_GPINTENA + port, 0x00);
|
|
374
|
+
if (this._intPinUsed[port]) {
|
|
375
|
+
await this._intPinUsed[port].offEdge(port === 0 ? this._edgeHandlerA : this._edgeHandlerB);
|
|
376
|
+
this._intPinUsed[port] = null;
|
|
377
|
+
}
|
|
378
|
+
if (this._pollTimer[port]) { clearInterval(this._pollTimer[port]); this._pollTimer[port] = null; }
|
|
379
|
+
this._callbackPort[port] = null;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
async _handleEdge(port) {
|
|
383
|
+
const status = await this.pollInterrupt(port);
|
|
384
|
+
if (!status) return;
|
|
385
|
+
if (this._callbackBoth) this._callbackBoth(port, status);
|
|
386
|
+
if (this._callbackPort[port]) this._callbackPort[port](status);
|
|
387
|
+
|
|
388
|
+
const raw = await this._readPort(port);
|
|
389
|
+
for (const [bit, w] of Object.entries(this._watchers[port])) {
|
|
390
|
+
if (!((status >> bit) & 1)) continue;
|
|
391
|
+
const current = (raw >> bit) & 1;
|
|
392
|
+
const rising = current === 1 && w.lastState === 0;
|
|
393
|
+
const falling = current === 0 && w.lastState === 1;
|
|
394
|
+
w.lastState = current;
|
|
395
|
+
if (w.trigger === 'change' || (w.trigger === 'rising' && rising) || (w.trigger === 'falling' && falling)) {
|
|
396
|
+
w.handler(this.pin(port * 8 + Number(bit)));
|
|
397
|
+
}
|
|
398
|
+
}
|
|
324
399
|
}
|
|
325
400
|
|
|
326
401
|
/**
|
|
327
|
-
* Read interrupt
|
|
402
|
+
* Read & clear interrupt status; returns the raw INTF flag register.
|
|
403
|
+
*
|
|
404
|
+
* Reads INTFA/INTFB (which pins triggered) then INTCAPA/INTCAPB (which
|
|
405
|
+
* clears the interrupt latch and re-arms it). Note that reading either
|
|
406
|
+
* INTCAP or GPIO clears the interrupt condition on this chip — call
|
|
407
|
+
* pollInterrupt() or readCapture() per event, not both.
|
|
328
408
|
*
|
|
329
409
|
* @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
|
|
330
|
-
* @returns {number} 8-bit interrupt
|
|
410
|
+
* @returns {Promise<number>} 8-bit interrupt flag mask.
|
|
331
411
|
*/
|
|
332
|
-
|
|
333
|
-
|
|
412
|
+
async pollInterrupt(port = 0) {
|
|
413
|
+
port &= 1;
|
|
414
|
+
const flags = await this._readReg(REG_INTFA + port);
|
|
415
|
+
await this._readReg(REG_INTCAPA + port); // clears and re-arms the interrupt; value discarded
|
|
416
|
+
return flags;
|
|
334
417
|
}
|
|
335
418
|
|
|
336
419
|
/**
|
|
337
|
-
*
|
|
420
|
+
* Read INTCAP: the port state latched at the moment of the interrupt.
|
|
421
|
+
*
|
|
422
|
+
* Also clears the interrupt latch (see pollInterrupt() doc) — call this
|
|
423
|
+
* instead of pollInterrupt() when the captured pin state is wanted
|
|
424
|
+
* rather than the flag register.
|
|
338
425
|
*
|
|
339
426
|
* @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
|
|
427
|
+
* @returns {Promise<number>} 8-bit captured port bitmask at the moment of interrupt.
|
|
340
428
|
*/
|
|
341
|
-
|
|
342
|
-
this.
|
|
343
|
-
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
429
|
+
async readCapture(port = 0) {
|
|
430
|
+
return this._readReg(REG_INTCAPA + (port & 1));
|
|
344
431
|
}
|
|
345
432
|
}
|
|
346
433
|
|
|
347
434
|
/**
|
|
348
|
-
* Full GPIO proxy — adds watch
|
|
435
|
+
* Full GPIO proxy — adds watch() for interrupt-driven input.
|
|
349
436
|
*/
|
|
350
437
|
class _FullPin extends _Pin {
|
|
351
438
|
/**
|
|
@@ -358,30 +445,26 @@ class _FullPin extends _Pin {
|
|
|
358
445
|
}
|
|
359
446
|
|
|
360
447
|
/**
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* Remove a previously registered handler.
|
|
373
|
-
* @param {function} handler - The handler to remove.
|
|
448
|
+
* Subscribe to this pin's edge events.
|
|
449
|
+
*
|
|
450
|
+
* At most one handler per pin at a time; a second watch() call replaces
|
|
451
|
+
* the first. Call the chip's onInterrupt()/onInterruptPort() (for this
|
|
452
|
+
* pin's port) first to arm delivery.
|
|
453
|
+
*
|
|
454
|
+
* @param {function} handler - Called with this pin when its state matches trigger.
|
|
455
|
+
* @param {string} [trigger='change'] - 'rising', 'falling', or 'change'.
|
|
374
456
|
*/
|
|
375
|
-
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
}
|
|
457
|
+
watch(handler, trigger = 'change') {
|
|
458
|
+
const port = this._n >> 3;
|
|
459
|
+
const bit = this._n & 7;
|
|
460
|
+
this._chip._watchers[port][bit] = { handler, trigger, lastState: 1 };
|
|
380
461
|
}
|
|
381
462
|
|
|
382
|
-
/**
|
|
383
|
-
|
|
384
|
-
this.
|
|
463
|
+
/** Unsubscribe this pin's handler. No-op if not registered. */
|
|
464
|
+
unwatch() {
|
|
465
|
+
const port = this._n >> 3;
|
|
466
|
+
const bit = this._n & 7;
|
|
467
|
+
delete this._chip._watchers[port][bit];
|
|
385
468
|
}
|
|
386
469
|
|
|
387
470
|
/**
|
|
@@ -393,4 +476,4 @@ class _FullPin extends _Pin {
|
|
|
393
476
|
}
|
|
394
477
|
}
|
|
395
478
|
|
|
396
|
-
module.exports = { Mcp23017Minimal, Mcp23017Full };
|
|
479
|
+
module.exports = { Mcp23017Minimal, Mcp23017Full };
|