periph 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -12
- package/examples/accelerometer/adxl345/complete.js +52 -0
- package/examples/accelerometer/adxl345/demo.js +34 -0
- package/examples/accelerometer/adxl345/minimal.js +18 -0
- package/examples/accelerometer/adxl362/complete.js +57 -0
- package/examples/accelerometer/adxl362/demo.js +51 -0
- package/examples/accelerometer/adxl362/minimal.js +21 -0
- package/examples/adc_dac/ad7705/complete.js +32 -0
- package/examples/adc_dac/ad7705/demo.js +42 -0
- package/examples/adc_dac/ad7705/minimal.js +16 -0
- package/examples/adc_dac/ad7706/complete.js +35 -0
- package/examples/adc_dac/ad7706/demo.js +44 -0
- package/examples/adc_dac/ad7706/minimal.js +16 -0
- package/examples/adc_dac/hx710a/complete.js +45 -0
- package/examples/adc_dac/hx710a/demo.js +52 -0
- package/examples/adc_dac/hx710a/minimal.js +16 -0
- package/examples/adc_dac/hx710b/complete.js +45 -0
- package/examples/adc_dac/hx710b/demo.js +64 -0
- package/examples/adc_dac/hx710b/minimal.js +16 -0
- package/examples/adc_dac/hx711/complete.js +7 -7
- package/examples/adc_dac/hx711/demo.js +6 -6
- package/examples/adc_dac/hx711/minimal.js +7 -7
- package/examples/adc_dac/mcp4725/complete.js +19 -17
- package/examples/adc_dac/mcp4725/demo.js +5 -5
- package/examples/adc_dac/mcp4725/minimal.js +8 -6
- package/examples/adc_dac/mcp4728/complete.js +24 -22
- package/examples/adc_dac/mcp4728/demo.js +22 -22
- package/examples/adc_dac/mcp4728/minimal.js +8 -6
- package/examples/adc_dac/pcf8591/complete.js +41 -0
- package/examples/adc_dac/pcf8591/demo.js +39 -0
- package/examples/adc_dac/pcf8591/minimal.js +19 -0
- package/examples/comms/mcp2515/complete.js +75 -0
- package/examples/comms/mcp2515/demo.js +66 -0
- package/examples/comms/mcp2515/minimal.js +27 -0
- package/examples/comms/rda5807m/complete.js +55 -0
- package/examples/comms/rda5807m/demo.js +68 -0
- package/examples/comms/rda5807m/minimal.js +15 -0
- package/examples/comms/rfm9x/complete.js +50 -0
- package/examples/comms/rfm9x/demo.js +55 -0
- package/examples/comms/rfm9x/minimal.js +20 -0
- package/examples/display/pcf8576/complete.js +18 -15
- package/examples/display/pcf8576/demo.js +6 -6
- package/examples/display/pcf8576/minimal.js +12 -10
- package/examples/environmental/aht21/complete.js +15 -13
- package/examples/environmental/aht21/demo.js +8 -6
- package/examples/environmental/aht21/minimal.js +5 -5
- package/examples/environmental/bme280/complete.js +23 -20
- package/examples/environmental/bme280/demo.js +14 -14
- package/examples/environmental/bme280/minimal.js +13 -11
- package/examples/environmental/bme680/complete.js +25 -22
- package/examples/environmental/bme680/demo.js +38 -36
- package/examples/environmental/bme680/minimal.js +14 -12
- package/examples/gas/ens160/complete.js +29 -27
- package/examples/gas/ens160/demo.js +39 -37
- package/examples/gas/ens160/minimal.js +17 -15
- package/examples/gnss/neo6/complete.js +49 -0
- package/examples/gnss/neo6/demo.js +54 -0
- package/examples/gnss/neo6/minimal.js +30 -0
- package/examples/gyroscope/l3g4200d/complete.js +56 -0
- package/examples/gyroscope/l3g4200d/demo.js +49 -0
- package/examples/gyroscope/l3g4200d/minimal.js +19 -0
- package/examples/gyroscope/l3gd20h/complete.js +42 -0
- package/examples/gyroscope/l3gd20h/demo.js +29 -0
- package/examples/gyroscope/l3gd20h/minimal.js +13 -0
- package/examples/humidity/dht11/complete.js +22 -0
- package/examples/humidity/dht11/demo.js +38 -0
- package/examples/humidity/dht11/minimal.js +18 -0
- package/examples/imu/mpu6050/complete.js +27 -25
- package/examples/imu/mpu6050/demo.js +29 -26
- package/examples/imu/mpu6050/minimal.js +6 -6
- package/examples/imu/mpu9250/complete.js +65 -0
- package/examples/imu/mpu9250/demo.js +53 -0
- package/examples/imu/mpu9250/minimal.js +21 -0
- package/examples/imu/mpu9255/complete.js +73 -0
- package/examples/imu/mpu9255/demo.js +73 -0
- package/examples/imu/mpu9255/minimal.js +21 -0
- package/examples/io_expander/mcp23017/complete.js +70 -77
- package/examples/io_expander/mcp23017/demo.js +37 -33
- package/examples/io_expander/mcp23017/minimal.js +15 -11
- package/examples/io_expander/pcf8574/complete.js +60 -65
- package/examples/io_expander/pcf8574/demo.js +30 -25
- package/examples/io_expander/pcf8574/minimal.js +15 -11
- package/examples/io_expander/pcf8575/complete.js +24 -20
- package/examples/io_expander/pcf8575/demo.js +27 -23
- package/examples/io_expander/pcf8575/minimal.js +16 -12
- package/examples/io_expander/tpic6b595/complete.js +57 -0
- package/examples/io_expander/tpic6b595/demo.js +75 -0
- package/examples/io_expander/tpic6b595/minimal.js +26 -0
- package/examples/led/apa102/complete.js +72 -0
- package/examples/led/apa102/demo.js +92 -0
- package/examples/led/apa102/minimal.js +21 -0
- package/examples/led/sk6812rgbw/complete.js +42 -40
- package/examples/led/sk6812rgbw/demo.js +16 -16
- package/examples/led/sk6812rgbw/minimal.js +15 -17
- package/examples/led/ws2812b/complete.js +40 -38
- package/examples/led/ws2812b/demo.js +16 -16
- package/examples/led/ws2812b/minimal.js +13 -14
- package/examples/led/ws2814/complete.js +71 -0
- package/examples/led/ws2814/demo.js +82 -0
- package/examples/led/ws2814/minimal.js +23 -0
- package/examples/light/apds-9930/complete.js +46 -0
- package/examples/light/apds-9930/demo.js +31 -0
- package/examples/light/apds-9930/minimal.js +20 -0
- package/examples/light/apds9960/complete.js +45 -43
- package/examples/light/apds9960/demo.js +11 -9
- package/examples/light/apds9960/minimal.js +5 -5
- package/examples/magnetometer/as5600/complete.js +31 -29
- package/examples/magnetometer/as5600/demo.js +35 -33
- package/examples/magnetometer/as5600/minimal.js +7 -7
- package/examples/magnetometer/hmc5883l/complete.js +47 -0
- package/examples/magnetometer/hmc5883l/demo.js +50 -0
- package/examples/magnetometer/hmc5883l/minimal.js +15 -0
- package/examples/memory/24aa02uid/complete.js +23 -21
- package/examples/memory/24aa02uid/demo.js +32 -30
- package/examples/memory/24aa02uid/minimal.js +5 -5
- package/examples/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -0
- package/examples/other/mpr121/complete.js +50 -0
- package/examples/other/mpr121/demo.js +30 -0
- package/examples/other/mpr121/minimal.js +17 -0
- package/examples/power/ade7953/complete.js +44 -0
- package/examples/power/ade7953/demo.js +28 -0
- package/examples/power/ade7953/minimal.js +15 -0
- package/examples/power/ina219/complete.js +20 -18
- package/examples/power/ina219/demo.js +15 -13
- package/examples/power/ina219/minimal.js +8 -8
- package/examples/power/ina226/complete.js +24 -22
- package/examples/power/ina226/demo.js +22 -20
- package/examples/power/ina226/minimal.js +5 -5
- package/examples/power/ina3221/complete.js +51 -49
- package/examples/power/ina3221/demo.js +13 -13
- package/examples/power/ina3221/minimal.js +8 -8
- package/examples/pressure/bmp085/complete.js +29 -0
- package/examples/pressure/bmp085/demo.js +34 -0
- package/examples/pressure/bmp085/minimal.js +19 -0
- package/examples/pressure/bmp180/complete.js +20 -17
- package/examples/pressure/bmp180/demo.js +24 -22
- package/examples/pressure/bmp180/minimal.js +13 -11
- package/examples/pressure/bmp280/complete.js +23 -20
- package/examples/pressure/bmp280/demo.js +44 -41
- package/examples/pressure/bmp280/minimal.js +13 -11
- package/examples/pressure/bmp384/complete.js +29 -0
- package/examples/pressure/bmp384/demo.js +40 -0
- package/examples/pressure/bmp384/minimal.js +18 -0
- package/examples/pressure/bmp581/complete.js +34 -0
- package/examples/pressure/bmp581/demo.js +56 -0
- package/examples/pressure/bmp581/minimal.js +19 -0
- package/examples/pressure/lps22df/complete.js +50 -0
- package/examples/pressure/lps22df/demo.js +41 -0
- package/examples/pressure/lps22df/minimal.js +19 -0
- package/examples/pressure/lps28dfw/complete.js +37 -0
- package/examples/pressure/lps28dfw/demo.js +33 -0
- package/examples/pressure/lps28dfw/minimal.js +19 -0
- package/examples/pressure/lps33hw/complete.js +43 -0
- package/examples/pressure/lps33hw/demo.js +43 -0
- package/examples/pressure/lps33hw/minimal.js +19 -0
- package/examples/rfid/mfrc522/complete.js +44 -0
- package/examples/rfid/mfrc522/demo.js +70 -0
- package/examples/rfid/mfrc522/minimal.js +17 -0
- package/examples/rtc/ds3231/complete.js +77 -0
- package/examples/rtc/ds3231/demo.js +60 -0
- package/examples/rtc/ds3231/minimal.js +23 -0
- package/examples/rtc/pcf8523/complete.js +66 -0
- package/examples/rtc/pcf8523/demo.js +65 -0
- package/examples/rtc/pcf8523/minimal.js +20 -0
- package/examples/temperature/mcp9808/complete.js +89 -0
- package/examples/temperature/mcp9808/demo.js +62 -0
- package/examples/temperature/mcp9808/minimal.js +21 -0
- package/examples/temperature/tmp117/complete.js +90 -0
- package/examples/temperature/tmp117/demo.js +73 -0
- package/examples/temperature/tmp117/minimal.js +21 -0
- package/examples/tof/vl53l0x/complete.js +118 -0
- package/examples/tof/vl53l0x/demo.js +83 -0
- package/examples/tof/vl53l0x/minimal.js +25 -0
- package/examples/tof/vl53l1x/complete.js +151 -0
- package/examples/tof/vl53l1x/demo.js +100 -0
- package/examples/tof/vl53l1x/minimal.js +25 -0
- package/package.json +5 -1
- package/src/chips/accelerometer/adxl345.js +482 -0
- package/src/chips/accelerometer/adxl362.js +594 -0
- package/src/chips/adc_dac/ad7705.js +345 -0
- package/src/chips/adc_dac/ad7706.js +331 -0
- package/src/chips/adc_dac/hx710a.js +184 -0
- package/src/chips/adc_dac/hx710b.js +186 -0
- package/src/chips/adc_dac/hx711.js +15 -15
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +57 -44
- package/src/chips/adc_dac/pcf8591.js +196 -0
- package/src/chips/comms/mcp2515.js +659 -0
- package/src/chips/comms/rda5807m.js +393 -0
- package/src/chips/comms/rfm9x.js +590 -0
- package/src/chips/display/pcf8576.js +42 -33
- package/src/chips/environmental/aht21.js +52 -37
- package/src/chips/environmental/bme280.js +85 -66
- package/src/chips/environmental/bme680.js +110 -87
- package/src/chips/gas/ens160.js +82 -64
- package/src/chips/gnss/neo6.js +438 -0
- package/src/chips/gyroscope/l3g4200d.js +366 -0
- package/src/chips/gyroscope/l3gd20h.js +314 -0
- package/src/chips/humidity/dht11.js +135 -0
- package/src/chips/imu/mpu6050.js +89 -66
- package/src/chips/imu/mpu9250.js +394 -0
- package/src/chips/imu/mpu9255.js +447 -0
- package/src/chips/io_expander/mcp23017.js +240 -157
- package/src/chips/io_expander/pcf8574.js +149 -124
- package/src/chips/io_expander/pcf8575.js +164 -135
- package/src/chips/io_expander/tpic6b595.js +242 -0
- package/src/chips/led/_NeoPixelRGBBase.js +204 -0
- package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
- package/src/chips/led/apa102.js +236 -0
- package/src/chips/led/sk6812rgbw.js +15 -149
- package/src/chips/led/ws2812b.js +14 -139
- package/src/chips/led/ws2814.js +49 -0
- package/src/chips/light/apds-9930.js +342 -0
- package/src/chips/light/apds9960.js +158 -123
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +32 -29
- package/src/chips/motor/drv8830.js +239 -0
- package/src/chips/other/mpr121.js +375 -0
- package/src/chips/power/ade7953.js +427 -0
- package/src/chips/power/ina219.js +45 -40
- package/src/chips/power/ina226.js +50 -45
- package/src/chips/power/ina3221.js +95 -76
- package/src/chips/pressure/bmp085.js +251 -0
- package/src/chips/pressure/bmp180.js +54 -37
- package/src/chips/pressure/bmp280.js +72 -53
- package/src/chips/pressure/bmp384.js +410 -0
- package/src/chips/pressure/bmp581.js +489 -0
- package/src/chips/pressure/lps22df.js +341 -0
- package/src/chips/pressure/lps28dfw.js +314 -0
- package/src/chips/pressure/lps33hw.js +255 -0
- package/src/chips/rfid/mfrc522.js +674 -0
- package/src/chips/rtc/ds3231.js +504 -0
- package/src/chips/rtc/pcf8523.js +539 -0
- package/src/chips/temperature/mcp9808.js +488 -0
- package/src/chips/temperature/tmp117.js +471 -0
- package/src/chips/tof/_vl53_base.js +160 -0
- package/src/chips/tof/vl53l0x.js +816 -0
- package/src/chips/tof/vl53l1x.js +816 -0
- package/src/connection/connection.js +111 -0
- package/src/connection/dhtxx.js +138 -0
- package/src/connection/dhtxx_mock.js +39 -0
- package/src/{transport → connection}/hx711.js +49 -22
- package/src/connection/hx711_mock.js +52 -0
- package/src/{transport → connection}/i2c.js +13 -9
- package/src/connection/i2c_mock.js +91 -0
- package/src/connection/input_pin.js +156 -0
- package/src/connection/neo6_mock.js +45 -0
- package/src/{transport → connection}/neopixel.js +23 -4
- package/src/connection/output_pin.js +41 -0
- package/src/connection/sipo.js +159 -0
- package/src/{transport → connection}/smbus.js +14 -10
- package/src/{transport → connection}/spi.js +12 -8
- package/src/connection/spi_mock.js +151 -0
- package/src/{transport → connection}/uart.js +33 -26
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
|
@@ -4,44 +4,50 @@
|
|
|
4
4
|
* PCF8574 8-bit quasi-bidirectional I/O port expander — minimal interface.
|
|
5
5
|
*
|
|
6
6
|
* Exposes all eight pins (P0–P7) as GPIO objects via the pin() factory.
|
|
7
|
-
* Pin objects
|
|
8
|
-
*
|
|
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 (e.g.
|
|
12
|
+
* SiPoConnection's SER/SRCK/RCK, HX711Connection's DOUT/PD_SCK,
|
|
13
|
+
* UARTConnection's DE pin) — see _Pin.asGpio() below.
|
|
9
14
|
*
|
|
10
|
-
* Direction is implicit:
|
|
11
|
-
*
|
|
15
|
+
* Direction is implicit: write(1) puts a pin in input mode (weak pull-up);
|
|
16
|
+
* write(0) drives it low. A shadow register tracks the output latch.
|
|
12
17
|
*
|
|
13
18
|
* Initialises all pins to input mode (shadow = 0xFF) at construction.
|
|
14
19
|
*/
|
|
15
20
|
class Pcf8574Minimal {
|
|
16
21
|
/**
|
|
17
|
-
* @param {
|
|
22
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
|
|
18
23
|
* @param {number} [addr=0x20] - 7-bit I²C device address.
|
|
19
24
|
* PCF8574 default 0x20; PCF8574A default 0x38.
|
|
20
25
|
*/
|
|
21
|
-
constructor(
|
|
22
|
-
this.
|
|
23
|
-
this._addr
|
|
24
|
-
this._shadow
|
|
25
|
-
this._writePort(0xFF);
|
|
26
|
+
constructor(connection, addr = 0x20) {
|
|
27
|
+
this._conn = connection;
|
|
28
|
+
this._addr = addr;
|
|
29
|
+
this._shadow = 0xFF;
|
|
30
|
+
this._writePort(0xFF); // fire-and-forget: underlying I2C write is synchronous
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
// ------------------------------------------------------------------
|
|
29
34
|
// Internal helpers
|
|
30
35
|
// ------------------------------------------------------------------
|
|
31
36
|
|
|
32
|
-
_writePort(mask) {
|
|
33
|
-
this.
|
|
37
|
+
async _writePort(mask) {
|
|
38
|
+
await this._conn.write(Buffer.from([mask & 0xFF]));
|
|
34
39
|
}
|
|
35
40
|
|
|
36
|
-
_readPort() {
|
|
37
|
-
|
|
41
|
+
async _readPort() {
|
|
42
|
+
const buf = await this._conn.read(1);
|
|
43
|
+
return buf[0];
|
|
38
44
|
}
|
|
39
45
|
|
|
40
|
-
_setPin(n, value) {
|
|
46
|
+
async _setPin(n, value) {
|
|
41
47
|
if (value) this._shadow |= (1 << n);
|
|
42
48
|
else this._shadow &= ~(1 << n);
|
|
43
49
|
this._shadow &= 0xFF;
|
|
44
|
-
this._writePort(this._shadow);
|
|
50
|
+
await this._writePort(this._shadow);
|
|
45
51
|
}
|
|
46
52
|
|
|
47
53
|
// ------------------------------------------------------------------
|
|
@@ -52,35 +58,35 @@ class Pcf8574Minimal {
|
|
|
52
58
|
* Return a Pin proxy object for pin n (0–7).
|
|
53
59
|
* @param {number} n - Pin index (0 = P0, 7 = P7).
|
|
54
60
|
* @param {string} [direction='in'] - Initial direction: 'in' or 'out'.
|
|
55
|
-
* @returns {_Pin} Pin proxy
|
|
61
|
+
* @returns {_Pin} Pin proxy.
|
|
56
62
|
*/
|
|
57
63
|
pin(n, direction = 'in') {
|
|
58
64
|
const p = new _Pin(this, n, direction);
|
|
59
|
-
|
|
60
|
-
else this._setPin(n, 1);
|
|
65
|
+
this._setPin(n, direction === 'out' ? 0 : 1); // fire-and-forget
|
|
61
66
|
return p;
|
|
62
67
|
}
|
|
63
68
|
|
|
64
69
|
/**
|
|
65
70
|
* Read all 8 pins as a bitmask.
|
|
66
71
|
* @param {number} [port=0] - Port index (ignored; PCF8574 has one port).
|
|
67
|
-
* @returns {number} 8-bit bitmask of actual pin logic levels (bit 0 = P0).
|
|
72
|
+
* @returns {Promise<number>} 8-bit bitmask of actual pin logic levels (bit 0 = P0).
|
|
68
73
|
*/
|
|
69
|
-
readPort(port = 0) { return this._readPort(); }
|
|
74
|
+
async readPort(port = 0) { return this._readPort(); }
|
|
70
75
|
|
|
71
76
|
/**
|
|
72
77
|
* Write all 8 pins at once and update the shadow register.
|
|
73
78
|
* @param {number} [port=0] - Port index (ignored).
|
|
74
79
|
* @param {number} mask - 8-bit output mask; 1 = input mode, 0 = drive low.
|
|
80
|
+
* @returns {Promise<void>}
|
|
75
81
|
*/
|
|
76
|
-
writePort(port = 0, mask = 0xFF) {
|
|
82
|
+
async writePort(port = 0, mask = 0xFF) {
|
|
77
83
|
this._shadow = mask & 0xFF;
|
|
78
|
-
this._writePort(this._shadow);
|
|
84
|
+
await this._writePort(this._shadow);
|
|
79
85
|
}
|
|
80
86
|
}
|
|
81
87
|
|
|
82
88
|
/**
|
|
83
|
-
* GPIO proxy for a single PCF8574 pin
|
|
89
|
+
* GPIO proxy for a single PCF8574 pin.
|
|
84
90
|
*
|
|
85
91
|
* Obtain via Pcf8574Minimal.pin(n). Do not instantiate directly.
|
|
86
92
|
*/
|
|
@@ -88,7 +94,7 @@ class _Pin {
|
|
|
88
94
|
/**
|
|
89
95
|
* @param {Pcf8574Minimal} chip - Parent driver instance.
|
|
90
96
|
* @param {number} n - Pin index (0–7).
|
|
91
|
-
* @param {string} direction - 'in' or 'out'.
|
|
97
|
+
* @param {string} direction - 'in' or 'out'; fixed for the pin's lifetime.
|
|
92
98
|
*/
|
|
93
99
|
constructor(chip, n, direction) {
|
|
94
100
|
this._chip = chip;
|
|
@@ -96,77 +102,100 @@ class _Pin {
|
|
|
96
102
|
this._direction = direction;
|
|
97
103
|
}
|
|
98
104
|
|
|
99
|
-
/** @type {string}
|
|
105
|
+
/** @type {string} Pin direction ('in' or 'out'). */
|
|
100
106
|
get direction() { return this._direction; }
|
|
101
107
|
|
|
102
108
|
/**
|
|
103
|
-
* Read pin
|
|
104
|
-
* @returns {number} 0 or 1.
|
|
109
|
+
* Read pin.
|
|
110
|
+
* @returns {Promise<number>} 0 or 1.
|
|
105
111
|
*/
|
|
106
|
-
|
|
107
|
-
return (this._chip._readPort() >> this._n) & 1;
|
|
112
|
+
async read() {
|
|
113
|
+
return (await this._chip._readPort() >> this._n) & 1;
|
|
108
114
|
}
|
|
109
115
|
|
|
110
116
|
/**
|
|
111
|
-
* Write pin
|
|
117
|
+
* Write pin.
|
|
112
118
|
* @param {number} value - 0 (drive low) or 1 (release to quasi-high input).
|
|
119
|
+
* @returns {Promise<void>}
|
|
113
120
|
*/
|
|
114
|
-
|
|
115
|
-
this._chip._setPin(this._n, value ? 1 : 0);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Read pin asynchronously.
|
|
120
|
-
* @param {function} callback - Node-style callback(err, value).
|
|
121
|
-
*/
|
|
122
|
-
read(callback) {
|
|
123
|
-
try { callback(null, this.readSync()); } catch (e) { callback(e); }
|
|
121
|
+
async write(value) {
|
|
122
|
+
await this._chip._setPin(this._n, value ? 1 : 0);
|
|
124
123
|
}
|
|
125
124
|
|
|
126
125
|
/**
|
|
127
|
-
*
|
|
128
|
-
* @param {
|
|
129
|
-
* @
|
|
126
|
+
* Set pin direction.
|
|
127
|
+
* @param {string} direction - 'in' or 'out'.
|
|
128
|
+
* @returns {Promise<void>}
|
|
130
129
|
*/
|
|
131
|
-
|
|
132
|
-
|
|
130
|
+
async setDirection(direction) {
|
|
131
|
+
this._direction = direction;
|
|
132
|
+
await this._chip._setPin(this._n, direction === 'in' ? 1 : 0);
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
/**
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
136
|
+
* Return a synchronous opengpio-shaped Input/Output facade for this
|
|
137
|
+
* pin — a boolean `.value` getter/setter, fixed `direction`, and
|
|
138
|
+
* `stop()` — so it can be passed anywhere code expects a real
|
|
139
|
+
* opengpio GPIO line.
|
|
140
|
+
*
|
|
141
|
+
* Output-direction facades read back the shadow register directly
|
|
142
|
+
* (authoritative — nothing else can change an output pin's logical
|
|
143
|
+
* state) and write through fire-and-forget, matching this class's own
|
|
144
|
+
* fire-and-forget writes elsewhere. Input-direction facades are
|
|
145
|
+
* backed by a 5 ms background poll that refreshes a cached value —
|
|
146
|
+
* `.value` is therefore eventually consistent, not a live bus read on
|
|
147
|
+
* every access like this pin's own async read(). Call stop() to
|
|
148
|
+
* release the background poll when the facade is no longer needed.
|
|
149
|
+
*
|
|
150
|
+
* @returns {{direction: string, value: boolean, stop: function}} opengpio-shaped facade.
|
|
139
151
|
*/
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
152
|
+
asGpio() {
|
|
153
|
+
const pin = this;
|
|
154
|
+
if (this._direction === 'out') {
|
|
155
|
+
return {
|
|
156
|
+
get direction() { return 'out'; },
|
|
157
|
+
get value() { return ((pin._chip._shadow >> pin._n) & 1) === 1; },
|
|
158
|
+
set value(v) { pin._chip._setPin(pin._n, v ? 1 : 0); }, // fire-and-forget
|
|
159
|
+
stop() {},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
let cached = false;
|
|
163
|
+
const timer = setInterval(async () => {
|
|
164
|
+
cached = (await pin.read()) === 1;
|
|
165
|
+
}, 5);
|
|
166
|
+
return {
|
|
167
|
+
get direction() { return 'in'; },
|
|
168
|
+
get value() { return cached; },
|
|
169
|
+
set value(_v) { throw new Error('cannot set value on an input pin'); },
|
|
170
|
+
stop() { clearInterval(timer); },
|
|
171
|
+
};
|
|
146
172
|
}
|
|
147
173
|
|
|
148
174
|
/** Release the pin (no-op; shadow state preserved). */
|
|
149
|
-
|
|
175
|
+
stop() {}
|
|
150
176
|
}
|
|
151
177
|
|
|
152
178
|
/**
|
|
153
179
|
* PCF8574 full interface — extends Pcf8574Minimal with interrupt support.
|
|
154
180
|
*
|
|
155
|
-
* Adds
|
|
156
|
-
*
|
|
157
|
-
*
|
|
181
|
+
* Adds onInterrupt()/offInterrupt() to subscribe to the chip's INT line and
|
|
182
|
+
* pollInterrupt() to read and clear the changed-pin bitmask. Pin objects
|
|
183
|
+
* gain watch()/unwatch() for per-pin interrupt handlers.
|
|
158
184
|
*/
|
|
159
185
|
class Pcf8574Full extends Pcf8574Minimal {
|
|
160
186
|
/**
|
|
161
|
-
* @param {
|
|
187
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
|
|
162
188
|
* @param {number} [addr=0x20] - 7-bit I²C device address.
|
|
163
189
|
*/
|
|
164
|
-
constructor(
|
|
165
|
-
super(
|
|
166
|
-
this._prev
|
|
190
|
+
constructor(connection, addr = 0x20) {
|
|
191
|
+
super(connection, addr);
|
|
192
|
+
this._prev = 0xFF;
|
|
193
|
+
this._readPort().then(v => { this._prev = v; }); // fire-and-forget refresh
|
|
167
194
|
this._callback = null;
|
|
195
|
+
this._watchers = {}; // { [pin]: { handler, trigger, lastState } }
|
|
196
|
+
this._intPin = null;
|
|
168
197
|
this._pollTimer = null;
|
|
169
|
-
this.
|
|
198
|
+
this._edgeHandler = () => { this._handleEdge(); };
|
|
170
199
|
}
|
|
171
200
|
|
|
172
201
|
/**
|
|
@@ -177,55 +206,63 @@ class Pcf8574Full extends Pcf8574Minimal {
|
|
|
177
206
|
*/
|
|
178
207
|
pin(n, direction = 'in') {
|
|
179
208
|
const p = new _FullPin(this, n, direction);
|
|
180
|
-
|
|
181
|
-
else this._setPin(n, 1);
|
|
209
|
+
this._setPin(n, direction === 'out' ? 0 : 1); // fire-and-forget
|
|
182
210
|
return p;
|
|
183
211
|
}
|
|
184
212
|
|
|
185
213
|
/**
|
|
186
|
-
*
|
|
214
|
+
* Subscribe to INT assertions.
|
|
187
215
|
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
216
|
+
* Wires options.intPin (or connection.intPin if omitted) to deliver
|
|
217
|
+
* edges. If neither is available, falls back to a 5 ms polling loop
|
|
218
|
+
* reading the port directly (no InputPin involved).
|
|
191
219
|
*
|
|
192
|
-
* @param {
|
|
193
|
-
* @param {
|
|
220
|
+
* @param {function} callback - Called with the changed-pin bitmask on any input change.
|
|
221
|
+
* @param {object} [options]
|
|
222
|
+
* @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
|
|
223
|
+
* @returns {Promise<void>}
|
|
194
224
|
*/
|
|
195
|
-
|
|
225
|
+
async onInterrupt(callback, options = {}) {
|
|
196
226
|
this._callback = callback;
|
|
197
|
-
|
|
198
|
-
if (
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
const ep = require('epoll').Epoll;
|
|
202
|
-
const fd = fs.openSync(intGpioPath, 'r');
|
|
203
|
-
const poll = new ep((err, fd2) => {
|
|
204
|
-
fs.readSync(fd2, Buffer.alloc(1), 0, 1, 0);
|
|
205
|
-
const changed = this.clearInterrupt();
|
|
206
|
-
if (changed) this._dispatch(changed);
|
|
207
|
-
});
|
|
208
|
-
poll.add(fd, ep.EPOLLPRI);
|
|
209
|
-
} catch (_) {
|
|
210
|
-
this._startPolling();
|
|
211
|
-
}
|
|
227
|
+
const pin = options.intPin ?? this._conn.intPin ?? null;
|
|
228
|
+
if (pin) {
|
|
229
|
+
this._intPin = pin;
|
|
230
|
+
await pin.onEdge(this._edgeHandler, 'falling');
|
|
212
231
|
} else {
|
|
213
232
|
this._startPolling();
|
|
214
233
|
}
|
|
215
234
|
}
|
|
216
235
|
|
|
236
|
+
/**
|
|
237
|
+
* Unsubscribe and stop delivery.
|
|
238
|
+
* @returns {Promise<void>}
|
|
239
|
+
*/
|
|
240
|
+
async offInterrupt() {
|
|
241
|
+
this._callback = null;
|
|
242
|
+
if (this._intPin) {
|
|
243
|
+
await this._intPin.offEdge(this._edgeHandler);
|
|
244
|
+
this._intPin = null;
|
|
245
|
+
}
|
|
246
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
247
|
+
}
|
|
248
|
+
|
|
217
249
|
_startPolling() {
|
|
218
|
-
this._pollTimer
|
|
219
|
-
|
|
220
|
-
if (changed) this._dispatch(changed);
|
|
221
|
-
}, 5);
|
|
250
|
+
if (this._pollTimer) return;
|
|
251
|
+
this._pollTimer = setInterval(() => { this._handleEdge(); }, 5);
|
|
222
252
|
}
|
|
223
253
|
|
|
224
|
-
|
|
254
|
+
async _handleEdge() {
|
|
255
|
+
const changed = await this.pollInterrupt();
|
|
256
|
+
if (!changed) return;
|
|
225
257
|
if (this._callback) this._callback(changed);
|
|
226
|
-
for (const [n,
|
|
227
|
-
if ((changed >> n) & 1)
|
|
228
|
-
|
|
258
|
+
for (const [n, w] of Object.entries(this._watchers)) {
|
|
259
|
+
if (!((changed >> n) & 1)) continue;
|
|
260
|
+
const current = (await this._readPort() >> n) & 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)));
|
|
229
266
|
}
|
|
230
267
|
}
|
|
231
268
|
}
|
|
@@ -233,12 +270,12 @@ class Pcf8574Full extends Pcf8574Minimal {
|
|
|
233
270
|
/**
|
|
234
271
|
* Read current pin states and return bitmask of pins that changed.
|
|
235
272
|
* Also clears the chip's INT line.
|
|
236
|
-
* @returns {number} 8-bit changed-pin bitmask.
|
|
273
|
+
* @returns {Promise<number>} 8-bit changed-pin bitmask.
|
|
237
274
|
*/
|
|
238
|
-
|
|
239
|
-
const current = this._readPort();
|
|
275
|
+
async pollInterrupt() {
|
|
276
|
+
const current = await this._readPort();
|
|
240
277
|
const changed = (current ^ this._prev) & 0xFF;
|
|
241
|
-
this._prev
|
|
278
|
+
this._prev = current;
|
|
242
279
|
return changed;
|
|
243
280
|
}
|
|
244
281
|
}
|
|
@@ -257,33 +294,21 @@ class _FullPin extends _Pin {
|
|
|
257
294
|
}
|
|
258
295
|
|
|
259
296
|
/**
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
const current = this.readSync();
|
|
268
|
-
this._chip._watchers[n].push((err) => {
|
|
269
|
-
handler(err, this.readSync());
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Remove a previously registered handler.
|
|
275
|
-
* @param {function} handler - The handler to remove.
|
|
297
|
+
* Subscribe to this pin's edge events.
|
|
298
|
+
*
|
|
299
|
+
* At most one handler per pin at a time; a second watch() call replaces
|
|
300
|
+
* the first. Call the chip's onInterrupt() first to arm delivery.
|
|
301
|
+
*
|
|
302
|
+
* @param {function} handler - Called with this pin when its state matches trigger.
|
|
303
|
+
* @param {string} [trigger='change'] - 'rising', 'falling', or 'change'.
|
|
276
304
|
*/
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if (this._chip._watchers[n]) {
|
|
280
|
-
this._chip._watchers[n] = this._chip._watchers[n].filter(h => h !== handler);
|
|
281
|
-
}
|
|
305
|
+
watch(handler, trigger = 'change') {
|
|
306
|
+
this._chip._watchers[this._n] = { handler, trigger, lastState: 1 };
|
|
282
307
|
}
|
|
283
308
|
|
|
284
|
-
/**
|
|
285
|
-
|
|
286
|
-
this._chip._watchers[this._n]
|
|
309
|
+
/** Unsubscribe this pin's handler. No-op if not registered. */
|
|
310
|
+
unwatch() {
|
|
311
|
+
delete this._chip._watchers[this._n];
|
|
287
312
|
}
|
|
288
313
|
|
|
289
314
|
/**
|