periph 1.0.1 → 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 +42 -0
- package/examples/adc_dac/hx711/demo.js +35 -0
- package/examples/adc_dac/hx711/minimal.js +16 -0
- 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 +43 -0
- package/examples/adc_dac/mcp4728/demo.js +41 -0
- package/examples/adc_dac/mcp4728/minimal.js +13 -0
- 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 +35 -0
- package/examples/display/pcf8576/demo.js +48 -0
- package/examples/display/pcf8576/minimal.js +20 -0
- package/examples/environmental/aht21/complete.js +32 -0
- package/examples/environmental/aht21/demo.js +46 -0
- package/examples/environmental/aht21/minimal.js +12 -0
- package/examples/environmental/bme280/complete.js +37 -0
- package/examples/environmental/bme280/demo.js +67 -0
- package/examples/environmental/bme280/minimal.js +20 -0
- package/examples/environmental/bme680/complete.js +40 -0
- package/examples/environmental/bme680/demo.js +54 -0
- package/examples/environmental/bme680/minimal.js +21 -0
- package/examples/gas/ens160/complete.js +54 -0
- package/examples/gas/ens160/demo.js +54 -0
- package/examples/gas/ens160/minimal.js +29 -0
- 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 +58 -0
- package/examples/imu/mpu6050/demo.js +37 -0
- package/examples/imu/mpu6050/minimal.js +14 -0
- 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 +64 -0
- package/examples/light/apds9960/demo.js +34 -0
- package/examples/light/apds9960/minimal.js +12 -0
- 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 +42 -0
- package/examples/memory/24aa02uid/demo.js +42 -0
- package/examples/memory/24aa02uid/minimal.js +12 -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 +29 -22
- package/examples/pressure/bmp280/demo.js +42 -23
- 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 +165 -0
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +297 -0
- 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 +232 -0
- package/src/chips/environmental/aht21.js +185 -0
- package/src/chips/environmental/bme280.js +385 -0
- package/src/chips/environmental/bme680.js +512 -0
- package/src/chips/gas/ens160.js +298 -0
- 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 +289 -0
- 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 +499 -0
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +182 -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 +188 -206
- 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/connection/hx711.js +132 -0
- 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/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/pressure/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/pressure/.gitkeep +0 -0
|
@@ -4,226 +4,265 @@
|
|
|
4
4
|
* PCF8575 16-bit quasi-bidirectional I/O port expander — minimal interface.
|
|
5
5
|
*
|
|
6
6
|
* Exposes all 16 pins (P00–P07, P10–P17) as GPIO objects via the pin() factory.
|
|
7
|
-
*
|
|
8
|
-
* writeSync(
|
|
7
|
+
* Pin objects expose async read()/write() (not onoff's synchronous
|
|
8
|
+
* readSync()/writeSync() — Connection is async everywhere, so a
|
|
9
|
+
* synchronous pin proxy can no longer correctly return a value). Call
|
|
10
|
+
* pin.asGpio() for a synchronous opengpio-shaped facade when a pin needs
|
|
11
|
+
* to be passed somewhere a real opengpio Input/Output is expected — see
|
|
12
|
+
* _Pin.asGpio() below.
|
|
9
13
|
*
|
|
10
|
-
*
|
|
14
|
+
* Direction is implicit: write(1) puts a pin in input mode (weak pull-up);
|
|
15
|
+
* write(0) drives it low. Two shadow registers track the output latches.
|
|
11
16
|
*
|
|
12
|
-
*
|
|
13
|
-
* @param {number} [addr=0x20] - 7-bit I²C device address.
|
|
17
|
+
* Initialises all pins to input mode (shadow = [0xFF, 0xFF]) at construction.
|
|
14
18
|
*/
|
|
15
19
|
class Pcf8575Minimal {
|
|
16
20
|
/**
|
|
17
|
-
* @param {
|
|
21
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
|
|
18
22
|
* @param {number} [addr=0x20] - 7-bit I²C device address.
|
|
19
23
|
*/
|
|
20
|
-
constructor(
|
|
21
|
-
this.
|
|
24
|
+
constructor(connection, addr = 0x20) {
|
|
25
|
+
this._conn = connection;
|
|
22
26
|
this._addr = addr;
|
|
23
27
|
this._shadow = [0xFF, 0xFF];
|
|
24
|
-
this._writeBoth();
|
|
28
|
+
this._writeBoth(); // fire-and-forget: underlying I2C write is synchronous
|
|
25
29
|
}
|
|
26
30
|
|
|
27
|
-
_writeBoth() {
|
|
28
|
-
this.
|
|
31
|
+
async _writeBoth() {
|
|
32
|
+
await this._conn.write(Buffer.from([this._shadow[0], this._shadow[1]]));
|
|
29
33
|
}
|
|
30
34
|
|
|
31
|
-
_readPort() {
|
|
32
|
-
return this.
|
|
35
|
+
async _readPort() {
|
|
36
|
+
return this._conn.read(2);
|
|
33
37
|
}
|
|
34
38
|
|
|
35
|
-
_setPin(n, value) {
|
|
39
|
+
async _setPin(n, value) {
|
|
36
40
|
const portIdx = Math.floor(n / 8);
|
|
37
41
|
const bit = n % 8;
|
|
38
42
|
if (value) this._shadow[portIdx] |= (1 << bit);
|
|
39
43
|
else this._shadow[portIdx] &= ~(1 << bit);
|
|
40
|
-
this._writeBoth();
|
|
44
|
+
await this._writeBoth();
|
|
41
45
|
}
|
|
42
46
|
|
|
43
47
|
/**
|
|
44
48
|
* Return a Pin proxy object for pin n (0–15).
|
|
45
49
|
* @param {number} n - Pin index (0 = P00, 15 = P17).
|
|
46
50
|
* @param {string} [direction='in'] - Initial direction: 'in' or 'out'.
|
|
47
|
-
* @returns {_Pin} Pin proxy
|
|
51
|
+
* @returns {_Pin} Pin proxy.
|
|
48
52
|
*/
|
|
49
53
|
pin(n, direction = 'in') {
|
|
50
54
|
const p = new _Pin(this, n, direction);
|
|
51
|
-
|
|
52
|
-
else this._setPin(n, 1);
|
|
55
|
+
this._setPin(n, direction === 'out' ? 0 : 1); // fire-and-forget
|
|
53
56
|
return p;
|
|
54
57
|
}
|
|
55
58
|
|
|
56
59
|
/**
|
|
57
60
|
* Read all 8 pins of the given port as a bitmask.
|
|
58
61
|
* @param {number} [port=0] - Port index (0 = P00–P07, 1 = P10–P17).
|
|
59
|
-
* @returns {number} 8-bit bitmask of actual pin logic levels.
|
|
62
|
+
* @returns {Promise<number>} 8-bit bitmask of actual pin logic levels.
|
|
60
63
|
*/
|
|
61
|
-
readPort(port = 0) {
|
|
62
|
-
|
|
64
|
+
async readPort(port = 0) {
|
|
65
|
+
const raw = await this._readPort();
|
|
66
|
+
return raw[port];
|
|
63
67
|
}
|
|
64
68
|
|
|
65
69
|
/**
|
|
66
70
|
* Write all 8 pins of the given port at once and update the shadow register.
|
|
67
71
|
* @param {number} [port=0] - Port index (0 or 1).
|
|
68
72
|
* @param {number} mask - 8-bit output mask; 1 = input mode, 0 = drive low.
|
|
73
|
+
* @returns {Promise<void>}
|
|
69
74
|
*/
|
|
70
|
-
writePort(port = 0, mask = 0xFF) {
|
|
75
|
+
async writePort(port = 0, mask = 0xFF) {
|
|
71
76
|
this._shadow[port] = mask & 0xFF;
|
|
72
|
-
this._writeBoth();
|
|
77
|
+
await this._writeBoth();
|
|
73
78
|
}
|
|
74
79
|
}
|
|
75
80
|
|
|
76
81
|
/**
|
|
77
|
-
* GPIO proxy for a single PCF8575 pin
|
|
82
|
+
* GPIO proxy for a single PCF8575 pin.
|
|
78
83
|
*
|
|
79
84
|
* Obtain via Pcf8575Minimal.pin(n). Do not instantiate directly.
|
|
80
|
-
*
|
|
81
|
-
* @param {Pcf8575Minimal} chip - Parent driver instance.
|
|
82
|
-
* @param {number} n - Pin index (0–15).
|
|
83
|
-
* @param {string} direction - 'in' or 'out'.
|
|
84
85
|
*/
|
|
85
86
|
class _Pin {
|
|
87
|
+
/**
|
|
88
|
+
* @param {Pcf8575Minimal} chip - Parent driver instance.
|
|
89
|
+
* @param {number} n - Pin index (0–15).
|
|
90
|
+
* @param {string} direction - 'in' or 'out'.
|
|
91
|
+
*/
|
|
86
92
|
constructor(chip, n, direction) {
|
|
87
93
|
this._chip = chip;
|
|
88
94
|
this._n = n;
|
|
89
95
|
this._direction = direction;
|
|
90
96
|
}
|
|
91
97
|
|
|
92
|
-
/** @type {string}
|
|
98
|
+
/** @type {string} Pin direction ('in' or 'out'). */
|
|
93
99
|
get direction() { return this._direction; }
|
|
94
100
|
|
|
95
101
|
/**
|
|
96
|
-
* Read pin
|
|
97
|
-
* @returns {number} 0 or 1.
|
|
102
|
+
* Read pin.
|
|
103
|
+
* @returns {Promise<number>} 0 or 1.
|
|
98
104
|
*/
|
|
99
|
-
|
|
105
|
+
async read() {
|
|
100
106
|
const port = Math.floor(this._n / 8);
|
|
101
107
|
const bit = this._n % 8;
|
|
102
|
-
|
|
108
|
+
const raw = await this._chip._readPort();
|
|
109
|
+
return (raw[port] >> bit) & 1;
|
|
103
110
|
}
|
|
104
111
|
|
|
105
112
|
/**
|
|
106
|
-
* Write pin
|
|
113
|
+
* Write pin.
|
|
107
114
|
* @param {number} value - 0 (drive low) or 1 (release to quasi-high input).
|
|
115
|
+
* @returns {Promise<void>}
|
|
108
116
|
*/
|
|
109
|
-
|
|
110
|
-
this._chip._setPin(this._n, value ? 1 : 0);
|
|
117
|
+
async write(value) {
|
|
118
|
+
await this._chip._setPin(this._n, value ? 1 : 0);
|
|
111
119
|
}
|
|
112
120
|
|
|
113
121
|
/**
|
|
114
|
-
*
|
|
115
|
-
* @param {
|
|
116
|
-
|
|
117
|
-
read(callback) {
|
|
118
|
-
try { callback(null, this.readSync()); } catch (e) { callback(e); }
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Write pin asynchronously.
|
|
123
|
-
* @param {number} value - 0 or 1.
|
|
124
|
-
* @param {function} callback - Node-style callback(err).
|
|
122
|
+
* Set pin direction.
|
|
123
|
+
* @param {string} direction - 'in' or 'out'.
|
|
124
|
+
* @returns {Promise<void>}
|
|
125
125
|
*/
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
async setDirection(direction) {
|
|
127
|
+
this._direction = direction;
|
|
128
|
+
await this._chip._setPin(this._n, direction === 'in' ? 1 : 0);
|
|
128
129
|
}
|
|
129
130
|
|
|
130
131
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
132
|
+
* Return a synchronous opengpio-shaped Input/Output facade for this
|
|
133
|
+
* pin — a boolean `.value` getter/setter, fixed `direction`, and
|
|
134
|
+
* `stop()` — so it can be passed anywhere code expects a real
|
|
135
|
+
* opengpio GPIO line.
|
|
136
|
+
*
|
|
137
|
+
* Output-direction facades read back the shadow register directly
|
|
138
|
+
* (authoritative) and write through fire-and-forget. Input-direction
|
|
139
|
+
* facades are backed by a 5 ms background poll that refreshes a
|
|
140
|
+
* cached value — `.value` is therefore eventually consistent, not a
|
|
141
|
+
* live bus read on every access like this pin's own async read().
|
|
142
|
+
* Call stop() to release the background poll when done.
|
|
143
|
+
*
|
|
144
|
+
* @returns {{direction: string, value: boolean, stop: function}} opengpio-shaped facade.
|
|
134
145
|
*/
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
146
|
+
asGpio() {
|
|
147
|
+
const pin = this;
|
|
148
|
+
if (this._direction === 'out') {
|
|
149
|
+
const port = Math.floor(this._n / 8);
|
|
150
|
+
const bit = this._n % 8;
|
|
151
|
+
return {
|
|
152
|
+
get direction() { return 'out'; },
|
|
153
|
+
get value() { return ((pin._chip._shadow[port] >> bit) & 1) === 1; },
|
|
154
|
+
set value(v) { pin._chip._setPin(pin._n, v ? 1 : 0); }, // fire-and-forget
|
|
155
|
+
stop() {},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
let cached = false;
|
|
159
|
+
const timer = setInterval(async () => {
|
|
160
|
+
cached = (await pin.read()) === 1;
|
|
161
|
+
}, 5);
|
|
162
|
+
return {
|
|
163
|
+
get direction() { return 'in'; },
|
|
164
|
+
get value() { return cached; },
|
|
165
|
+
set value(_v) { throw new Error('cannot set value on an input pin'); },
|
|
166
|
+
stop() { clearInterval(timer); },
|
|
167
|
+
};
|
|
141
168
|
}
|
|
142
169
|
|
|
143
170
|
/** Release the pin (no-op; shadow state preserved). */
|
|
144
|
-
|
|
171
|
+
stop() {}
|
|
145
172
|
}
|
|
146
173
|
|
|
147
174
|
/**
|
|
148
175
|
* PCF8575 full interface — extends Pcf8575Minimal with interrupt support.
|
|
149
176
|
*
|
|
150
|
-
* Adds
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
* @param {object} transport - Configured I²C transport.
|
|
155
|
-
* @param {number} [addr=0x20] - 7-bit I²C device address.
|
|
177
|
+
* Adds onInterrupt()/offInterrupt() to subscribe to the chip's INT line and
|
|
178
|
+
* pollInterrupt() to read and clear the 16-bit changed-pin bitmask. Pin
|
|
179
|
+
* objects gain watch()/unwatch() for per-pin interrupt handlers.
|
|
156
180
|
*/
|
|
157
181
|
class Pcf8575Full extends Pcf8575Minimal {
|
|
158
182
|
/**
|
|
159
|
-
* @param {
|
|
183
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
|
|
160
184
|
* @param {number} [addr=0x20] - 7-bit I²C device address.
|
|
161
185
|
*/
|
|
162
|
-
constructor(
|
|
163
|
-
super(
|
|
164
|
-
|
|
165
|
-
this._prev = [raw[0], raw[1]];
|
|
166
|
-
this._callback
|
|
186
|
+
constructor(connection, addr = 0x20) {
|
|
187
|
+
super(connection, addr);
|
|
188
|
+
this._prev = [0xFF, 0xFF];
|
|
189
|
+
this._readPort().then(raw => { this._prev = [raw[0], raw[1]]; }); // fire-and-forget refresh
|
|
190
|
+
this._callback = null;
|
|
191
|
+
this._watchers = {}; // { [pin]: { handler, trigger, lastState } }
|
|
192
|
+
this._intPin = null;
|
|
167
193
|
this._pollTimer = null;
|
|
168
|
-
this.
|
|
194
|
+
this._edgeHandler = () => { this._handleEdge(); };
|
|
169
195
|
}
|
|
170
196
|
|
|
171
197
|
/**
|
|
172
198
|
* Return a Full pin proxy for pin n (0–15).
|
|
173
199
|
* @param {number} n - Pin index.
|
|
174
200
|
* @param {string} [direction='in'] - Initial direction.
|
|
175
|
-
* @returns {_FullPin} Full pin proxy with watch
|
|
201
|
+
* @returns {_FullPin} Full pin proxy with watch() support.
|
|
176
202
|
*/
|
|
177
203
|
pin(n, direction = 'in') {
|
|
178
204
|
const p = new _FullPin(this, n, direction);
|
|
179
|
-
|
|
180
|
-
else this._setPin(n, 1);
|
|
205
|
+
this._setPin(n, direction === 'out' ? 0 : 1); // fire-and-forget
|
|
181
206
|
return p;
|
|
182
207
|
}
|
|
183
208
|
|
|
184
209
|
/**
|
|
185
|
-
*
|
|
210
|
+
* Subscribe to INT assertions.
|
|
186
211
|
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
212
|
+
* Wires options.intPin (or connection.intPin if omitted) to deliver
|
|
213
|
+
* edges. If neither is available, falls back to a 5 ms polling loop
|
|
214
|
+
* reading both ports directly (no InputPin involved).
|
|
189
215
|
*
|
|
190
|
-
* @param {
|
|
191
|
-
*
|
|
216
|
+
* @param {function} callback - Called with the 16-bit changed-pin bitmask
|
|
217
|
+
* (bits 0–7 = Port 0, bits 8–15 = Port 1) on any input change.
|
|
218
|
+
* @param {object} [options]
|
|
219
|
+
* @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
|
|
220
|
+
* @returns {Promise<void>}
|
|
192
221
|
*/
|
|
193
|
-
|
|
222
|
+
async onInterrupt(callback, options = {}) {
|
|
194
223
|
this._callback = callback;
|
|
195
|
-
|
|
196
|
-
if (
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
const ep = require('epoll').Epoll;
|
|
200
|
-
const fd = fs.openSync(intGpioPath, 'r');
|
|
201
|
-
const poll = new ep((err, fd2) => {
|
|
202
|
-
fs.readSync(fd2, Buffer.alloc(1), 0, 1, 0);
|
|
203
|
-
const changed = this.clearInterrupt();
|
|
204
|
-
if (changed) this._dispatch(changed);
|
|
205
|
-
});
|
|
206
|
-
poll.add(fd, ep.EPOLLPRI);
|
|
207
|
-
} catch (_) {
|
|
208
|
-
this._startPolling();
|
|
209
|
-
}
|
|
224
|
+
const pin = options.intPin ?? this._conn.intPin ?? null;
|
|
225
|
+
if (pin) {
|
|
226
|
+
this._intPin = pin;
|
|
227
|
+
await pin.onEdge(this._edgeHandler, 'falling');
|
|
210
228
|
} else {
|
|
211
229
|
this._startPolling();
|
|
212
230
|
}
|
|
213
231
|
}
|
|
214
232
|
|
|
233
|
+
/**
|
|
234
|
+
* Unsubscribe and stop delivery.
|
|
235
|
+
* @returns {Promise<void>}
|
|
236
|
+
*/
|
|
237
|
+
async offInterrupt() {
|
|
238
|
+
this._callback = null;
|
|
239
|
+
if (this._intPin) {
|
|
240
|
+
await this._intPin.offEdge(this._edgeHandler);
|
|
241
|
+
this._intPin = null;
|
|
242
|
+
}
|
|
243
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
244
|
+
}
|
|
245
|
+
|
|
215
246
|
_startPolling() {
|
|
216
|
-
this._pollTimer
|
|
217
|
-
|
|
218
|
-
if (changed) this._dispatch(changed);
|
|
219
|
-
}, 5);
|
|
247
|
+
if (this._pollTimer) return;
|
|
248
|
+
this._pollTimer = setInterval(() => { this._handleEdge(); }, 5);
|
|
220
249
|
}
|
|
221
250
|
|
|
222
|
-
|
|
251
|
+
async _handleEdge() {
|
|
252
|
+
const changed = await this.pollInterrupt();
|
|
253
|
+
if (!changed) return;
|
|
223
254
|
if (this._callback) this._callback(changed);
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
255
|
+
const raw = await this._readPort();
|
|
256
|
+
for (const [n, w] of Object.entries(this._watchers)) {
|
|
257
|
+
if (!((changed >> n) & 1)) continue;
|
|
258
|
+
const port = Math.floor(n / 8);
|
|
259
|
+
const bit = n % 8;
|
|
260
|
+
const current = (raw[port] >> bit) & 1;
|
|
261
|
+
const rising = current === 1 && w.lastState === 0;
|
|
262
|
+
const falling = current === 0 && w.lastState === 1;
|
|
263
|
+
w.lastState = current;
|
|
264
|
+
if (w.trigger === 'change' || (w.trigger === 'rising' && rising) || (w.trigger === 'falling' && falling)) {
|
|
265
|
+
w.handler(this.pin(Number(n)));
|
|
227
266
|
}
|
|
228
267
|
}
|
|
229
268
|
}
|
|
@@ -231,10 +270,10 @@ class Pcf8575Full extends Pcf8575Minimal {
|
|
|
231
270
|
/**
|
|
232
271
|
* Read current pin states and return 16-bit bitmask of pins that changed.
|
|
233
272
|
* Also clears the chip's INT line.
|
|
234
|
-
* @returns {number} 16-bit changed-pin bitmask (bits 0–7 = Port 0, bits 8–15 = Port 1).
|
|
273
|
+
* @returns {Promise<number>} 16-bit changed-pin bitmask (bits 0–7 = Port 0, bits 8–15 = Port 1).
|
|
235
274
|
*/
|
|
236
|
-
|
|
237
|
-
const current = this._readPort();
|
|
275
|
+
async pollInterrupt() {
|
|
276
|
+
const current = await this._readPort();
|
|
238
277
|
const changed0 = current[0] ^ this._prev[0];
|
|
239
278
|
const changed1 = current[1] ^ this._prev[1];
|
|
240
279
|
this._prev = [current[0], current[1]];
|
|
@@ -244,43 +283,33 @@ class Pcf8575Full extends Pcf8575Minimal {
|
|
|
244
283
|
|
|
245
284
|
/**
|
|
246
285
|
* Full GPIO proxy — adds watch/unwatch for interrupt-driven input.
|
|
247
|
-
*
|
|
248
|
-
* @param {Pcf8575Full} chip - Parent full driver instance.
|
|
249
|
-
* @param {number} n - Pin index.
|
|
250
|
-
* @param {string} direction - 'in' or 'out'.
|
|
251
286
|
*/
|
|
252
287
|
class _FullPin extends _Pin {
|
|
253
|
-
constructor(chip, n, direction) {
|
|
254
|
-
super(chip, n, direction);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
288
|
/**
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
* @param {
|
|
289
|
+
* @param {Pcf8575Full} chip - Parent full driver instance.
|
|
290
|
+
* @param {number} n - Pin index.
|
|
291
|
+
* @param {string} direction - 'in' or 'out'.
|
|
261
292
|
*/
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
if (!this._chip._watchers[n]) this._chip._watchers[n] = [];
|
|
265
|
-
this._chip._watchers[n].push((err) => {
|
|
266
|
-
handler(err, this.readSync());
|
|
267
|
-
});
|
|
293
|
+
constructor(chip, n, direction) {
|
|
294
|
+
super(chip, n, direction);
|
|
268
295
|
}
|
|
269
296
|
|
|
270
297
|
/**
|
|
271
|
-
*
|
|
272
|
-
*
|
|
298
|
+
* Subscribe to this pin's edge events.
|
|
299
|
+
*
|
|
300
|
+
* At most one handler per pin at a time; a second watch() call replaces
|
|
301
|
+
* the first. Call the chip's onInterrupt() first to arm delivery.
|
|
302
|
+
*
|
|
303
|
+
* @param {function} handler - Called with this pin when its state matches trigger.
|
|
304
|
+
* @param {string} [trigger='change'] - 'rising', 'falling', or 'change'.
|
|
273
305
|
*/
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
if (this._chip._watchers[n]) {
|
|
277
|
-
this._chip._watchers[n] = this._chip._watchers[n].filter(h => h !== handler);
|
|
278
|
-
}
|
|
306
|
+
watch(handler, trigger = 'change') {
|
|
307
|
+
this._chip._watchers[this._n] = { handler, trigger, lastState: 1 };
|
|
279
308
|
}
|
|
280
309
|
|
|
281
|
-
/**
|
|
282
|
-
|
|
283
|
-
this._chip._watchers[this._n]
|
|
310
|
+
/** Unsubscribe this pin's handler. No-op if not registered. */
|
|
311
|
+
unwatch() {
|
|
312
|
+
delete this._chip._watchers[this._n];
|
|
284
313
|
}
|
|
285
314
|
|
|
286
315
|
/**
|
|
@@ -292,4 +321,4 @@ class _FullPin extends _Pin {
|
|
|
292
321
|
}
|
|
293
322
|
}
|
|
294
323
|
|
|
295
|
-
module.exports = { Pcf8575Minimal, Pcf8575Full };
|
|
324
|
+
module.exports = { Pcf8575Minimal, Pcf8575Full };
|