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
|
@@ -1,80 +1,73 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
const {
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
3
|
const { Mcp23017Minimal, Mcp23017Full } = require('periph/src/chips/io_expander/mcp23017');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
p0.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
//
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
chip.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
chip.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
p1.setActiveLow(false); // Set active-low inversion, (invert) → void
|
|
75
|
-
|
|
76
|
-
// --- Stop interrupt ---
|
|
77
|
-
chip.stopInterrupt(0); // Disable interrupt, (port) → void
|
|
78
|
-
// clears GPINTENA; stops polling
|
|
79
|
-
|
|
80
|
-
process.exit(0);
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
7
|
+
const chip = new Mcp23017Full(connection); // Create MCP23017 full driver, (connection, addr=0x20)
|
|
8
|
+
|
|
9
|
+
// --- Pin proxy — async API ---
|
|
10
|
+
const p0 = chip.pin(0, 'out'); // Get full pin proxy, (n, direction='out') → FullPin
|
|
11
|
+
// GPA0 as output (GPA7/GPB7 are output-only on hardware)
|
|
12
|
+
console.log('direction:', p0.direction); // direction property, () → string 'in'|'out'
|
|
13
|
+
|
|
14
|
+
await p0.write(1); // Write pin, (value=0|1) → Promise<void>
|
|
15
|
+
await p0.write(0); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
|
|
17
|
+
const v = await p0.read(); // Read pin, () → Promise<int 0|1>
|
|
18
|
+
// reads GPIOA, extracts bit 0
|
|
19
|
+
console.log('GPA0 level:', v);
|
|
20
|
+
|
|
21
|
+
// --- Direction change ---
|
|
22
|
+
await p0.setDirection('in'); // Set direction, (direction) → Promise<void>
|
|
23
|
+
console.log('now input');
|
|
24
|
+
|
|
25
|
+
// --- Port-level bulk operations ---
|
|
26
|
+
const porta = await chip.readPort(0); // Read all 8 pins, (port=0) → Promise<int bitmask>
|
|
27
|
+
// PORTA = GPIOA register
|
|
28
|
+
const portb = await chip.readPort(1); // Read all 8 pins, (port=1) → Promise<int bitmask>
|
|
29
|
+
// PORTB = GPIOB register
|
|
30
|
+
console.log('PORTA=0x' + porta.toString(16).padStart(2, '0') + ' PORTB=0x' + portb.toString(16).padStart(2, '0'));
|
|
31
|
+
await chip.writePort(0, 0b00001111); // Write all 8 pins, (port, mask) → Promise<void>
|
|
32
|
+
// GPA0–GPA3 as outputs low, GPA4–GPA6 inputs, GPA7 output low
|
|
33
|
+
await chip.writePort(1, 0b11110000); // Write all 8 pins, (port, mask) → Promise<void>
|
|
34
|
+
// GPB0–GPB3 outputs low, GPB4–GPB7 outputs high
|
|
35
|
+
|
|
36
|
+
// --- Pull-up configuration ---
|
|
37
|
+
await chip.configurePullup(0, 0b01111111); // Configure pull-ups, (port, mask) → Promise<void>
|
|
38
|
+
// enable pull-ups on GPA0–GPA6 (inputs); GPA7 is output, pull-up has no effect
|
|
39
|
+
|
|
40
|
+
// --- Polarity inversion ---
|
|
41
|
+
await chip.configurePolarity(1, 0x00); // Configure polarity, (port, mask) → Promise<void>
|
|
42
|
+
// normal polarity on PORTB
|
|
43
|
+
|
|
44
|
+
// --- Interrupt-on-change (single port) ---
|
|
45
|
+
await chip.onInterruptPort(0, (changed) => { // Subscribe to INT on one port, (port, callback, options) → Promise<void>
|
|
46
|
+
console.log('PORTA changed: 0x' + changed.toString(16).padStart(2, '0'));
|
|
47
|
+
});
|
|
48
|
+
// no options.intPin = 5 ms polling fallback; INTA fires on any GPA change
|
|
49
|
+
|
|
50
|
+
const changed = await chip.pollInterrupt(0); // Read and clear interrupt, (port) → Promise<int>
|
|
51
|
+
// INTFA flags; also reads INTCAPA to clear
|
|
52
|
+
console.log('changed on init:', changed);
|
|
53
|
+
|
|
54
|
+
const captured = await chip.readCapture(0); // Read INTCAP, (port) → Promise<int>
|
|
55
|
+
// captured PORTA state at moment of interrupt; also clears
|
|
56
|
+
console.log('captured PORTA: 0x' + captured.toString(16).padStart(2, '0'));
|
|
57
|
+
|
|
58
|
+
// --- Per-pin watch ---
|
|
59
|
+
const p1 = chip.pin(1, 'in'); // Get full pin proxy, (n, direction='in') → FullPin
|
|
60
|
+
const watcher = (pin) => console.log('GPA1 changed, pin:', pin);
|
|
61
|
+
p1.watch(watcher, 'change'); // Subscribe to pin edges, (handler, trigger) → void
|
|
62
|
+
// fires when GPA1 changes; needs onInterruptPort(0, ...) armed first
|
|
63
|
+
p1.unwatch(); // Unsubscribe pin handler, () → void
|
|
64
|
+
p1.setActiveLow(false); // Set active-low inversion, (invert) → void
|
|
65
|
+
|
|
66
|
+
// --- Stop interrupt ---
|
|
67
|
+
await chip.offInterruptPort(0); // Unsubscribe PORTA, (port) → Promise<void>
|
|
68
|
+
// clears GPINTENA; stops polling
|
|
69
|
+
|
|
70
|
+
process.exit(0);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
main();
|
|
@@ -12,48 +12,52 @@
|
|
|
12
12
|
* active-low buttons), ORs it with the scanner position unless a button is
|
|
13
13
|
* pressed, then writes to OLATA.
|
|
14
14
|
*/
|
|
15
|
-
const {
|
|
15
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
16
16
|
const { Mcp23017Full } = require('periph/src/chips/io_expander/mcp23017');
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
18
|
+
async function main() {
|
|
19
|
+
// --- Initialise bus and driver ---
|
|
20
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
21
|
+
const chip = new Mcp23017Full(connection); // Create MCP23017 full driver, (connection, addr=0x20)
|
|
21
22
|
|
|
22
|
-
// Enable pull-ups on PORTB inputs (GPB0–GPB6) so idle buttons read high.
|
|
23
|
-
chip.configurePullup(1, 0b01111111);
|
|
23
|
+
// Enable pull-ups on PORTB inputs (GPB0–GPB6) so idle buttons read high.
|
|
24
|
+
await chip.configurePullup(1, 0b01111111); // Configure pull-ups, (port=1, mask) → Promise<void>
|
|
24
25
|
|
|
25
|
-
// --- Wire interrupt for responsive button detection ---
|
|
26
|
-
chip.
|
|
27
|
-
|
|
28
|
-
});
|
|
26
|
+
// --- Wire interrupt for responsive button detection ---
|
|
27
|
+
await chip.onInterruptPort(1, (changed) => { // Subscribe to INT on one port, (port, callback, options) → Promise<void>
|
|
28
|
+
// handled in the polling loop below
|
|
29
|
+
});
|
|
29
30
|
|
|
30
|
-
let position = 0;
|
|
31
|
-
let direction = 1;
|
|
31
|
+
let position = 0;
|
|
32
|
+
let direction = 1;
|
|
32
33
|
|
|
33
|
-
// --- Main loop: Knight Rider with button override ---
|
|
34
|
-
console.log('Running — press buttons GPB0–GPB6 to light corresponding LEDs');
|
|
35
|
-
setInterval(() => {
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
// --- Main loop: Knight Rider with button override ---
|
|
35
|
+
console.log('Running — press buttons GPB0–GPB6 to light corresponding LEDs');
|
|
36
|
+
setInterval(async () => {
|
|
37
|
+
const portb = await chip.readPort(1); // Read all 8 pins, (port=1) → Promise<int bitmask>
|
|
38
|
+
// GPB0–GPB6 buttons: pressed = 0 (active-low pull-down)
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
const buttons = portb & 0x7F; // mask GPA7 (output-only)
|
|
41
|
+
const pressed = (~buttons) & 0x7F; // invert: pressed button = bit 1
|
|
41
42
|
|
|
42
|
-
|
|
43
|
+
const scanner = 1 << position;
|
|
43
44
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
let output;
|
|
46
|
+
if (pressed) {
|
|
47
|
+
output = pressed | (1 << 7); // keep GPA7 high (output-only)
|
|
48
|
+
} else {
|
|
49
|
+
output = scanner | (1 << 7);
|
|
50
|
+
}
|
|
50
51
|
|
|
51
|
-
|
|
52
|
+
await chip.writePort(0, output); // Write all 8 pins, (port=0, mask) → Promise<void>
|
|
52
53
|
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
const ledStr = [...Array(7)].map((_, i) => (output >> i) & 1 ? '*' : ' ').join('');
|
|
55
|
+
console.log('PORTA=0x' + output.toString(16).padStart(2, '0') + ' [' + ledStr + '] buttons=0x' + buttons.toString(16).padStart(2, '0'));
|
|
55
56
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}, 100);
|
|
57
|
+
position += direction;
|
|
58
|
+
if (position === 6) direction = -1;
|
|
59
|
+
if (position === 0) direction = 1;
|
|
60
|
+
}, 100);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
main();
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
const {
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
3
|
const { Mcp23017Minimal } = require('periph/src/chips/io_expander/mcp23017');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
const
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
7
|
+
const chip = new Mcp23017Minimal(connection); // Create MCP23017 driver, (connection, addr=0x20)
|
|
7
8
|
|
|
8
|
-
const p0 = chip.pin(0, 'out'); // Get pin proxy, (n, direction='out') → Pin
|
|
9
|
-
const p8 = chip.pin(8, 'in'); // Get pin proxy, (n, direction='in') → Pin
|
|
9
|
+
const p0 = chip.pin(0, 'out'); // Get pin proxy, (n, direction='out') → Pin
|
|
10
|
+
const p8 = chip.pin(8, 'in'); // Get pin proxy, (n, direction='in') → Pin
|
|
10
11
|
|
|
11
|
-
setInterval(() => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}, 200);
|
|
12
|
+
setInterval(async () => {
|
|
13
|
+
const porta = await chip.readPort(0); // Read all 8 pins, (port=0) → Promise<int bitmask>
|
|
14
|
+
const portb = await chip.readPort(1); // Read all 8 pins, (port=1) → Promise<int bitmask>
|
|
15
|
+
await p0.write((porta >> 1) & 1); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
console.log('PORTA=0x' + porta.toString(16).padStart(2, '0') + ' PORTB=0x' + portb.toString(16).padStart(2, '0'));
|
|
17
|
+
}, 200);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main();
|
|
@@ -1,68 +1,63 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
const {
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
3
|
const { Pcf8574Minimal, Pcf8574Full } = require('periph/src/chips/io_expander/pcf8574');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
//
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
console.log('
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// fires whenever P5 changes; needs configureInterrupt
|
|
65
|
-
p5.unwatch(watcher); // Remove change handler, (handler) → void
|
|
66
|
-
p5.unexport(); // Release pin (no-op), () → void
|
|
67
|
-
|
|
68
|
-
process.exit(0);
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
7
|
+
const chip = new Pcf8574Full(connection); // Create PCF8574 full driver, (connection, addr=0x20)
|
|
8
|
+
// initialises all pins as inputs; shadow = 0xFF
|
|
9
|
+
|
|
10
|
+
// --- Pin proxy — async API ---
|
|
11
|
+
const p0 = chip.pin(0, 'out'); // Get full pin proxy, (n, direction='out') → FullPin
|
|
12
|
+
// drives P0 low initially; returns FullPin
|
|
13
|
+
console.log('direction:', p0.direction); // direction property, () → string 'in'|'out'
|
|
14
|
+
// reflects the direction set at pin() call time
|
|
15
|
+
|
|
16
|
+
await p0.write(1); // Write pin, (value=0|1) → Promise<void>
|
|
17
|
+
// shadow |= (1<<0); quasi-high (input mode)
|
|
18
|
+
await p0.write(0); // Write pin, (value=0|1) → Promise<void>
|
|
19
|
+
// shadow &= ~(1<<0); drives low
|
|
20
|
+
|
|
21
|
+
const v = await p0.read(); // Read pin, () → Promise<int 0|1>
|
|
22
|
+
// reads full port byte, extracts bit 0
|
|
23
|
+
console.log('P0 level:', v);
|
|
24
|
+
|
|
25
|
+
// --- Direction change ---
|
|
26
|
+
await p0.setDirection('in'); // Set direction, (direction) → Promise<void>
|
|
27
|
+
// updates shadow bit to 1 (input mode)
|
|
28
|
+
console.log('now input');
|
|
29
|
+
|
|
30
|
+
// --- Port-level bulk operations ---
|
|
31
|
+
const mask = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
|
|
32
|
+
// bit 0 = P0, bit 7 = P7
|
|
33
|
+
console.log('port:', mask.toString(16));
|
|
34
|
+
await chip.writePort(0, 0b00001111); // Write all 8 pins, (port, mask) → Promise<void>
|
|
35
|
+
// P0–P3 low (outputs), P4–P7 high (inputs)
|
|
36
|
+
|
|
37
|
+
// --- Input pin ---
|
|
38
|
+
const p4 = chip.pin(4, 'in'); // Get full pin proxy, (n, direction='in') → FullPin
|
|
39
|
+
const state = await p4.read(); // Read pin, () → Promise<int>
|
|
40
|
+
// 0 if button pulls P4 low, 1 if floating
|
|
41
|
+
console.log('P4:', state);
|
|
42
|
+
|
|
43
|
+
// --- Interrupt-on-change ---
|
|
44
|
+
await chip.onInterrupt((changed) => { // Subscribe to INT line, (callback, options) → Promise<void>
|
|
45
|
+
console.log('changed:', changed.toString(2).padStart(8, '0')); // no options.intPin = 5 ms polling fallback
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const changed = await chip.pollInterrupt(); // Read and return changed bitmask, () → Promise<int>
|
|
49
|
+
// compares current byte to previous; clears INT
|
|
50
|
+
console.log('changed on init:', changed);
|
|
51
|
+
|
|
52
|
+
// --- Per-pin watch ---
|
|
53
|
+
const p5 = chip.pin(5, 'in'); // Get full pin proxy, (n, direction='in') → FullPin
|
|
54
|
+
const watcher = (pin) => console.log('P5 changed, pin:', pin);
|
|
55
|
+
p5.watch(watcher, 'change'); // Subscribe to pin edges, (handler, trigger) → void
|
|
56
|
+
// fires whenever P5 changes; needs onInterrupt armed
|
|
57
|
+
p5.unwatch(); // Unsubscribe pin handler, () → void
|
|
58
|
+
p5.unexport(); // Release pin (no-op), () → void
|
|
59
|
+
|
|
60
|
+
process.exit(0);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
main();
|
|
@@ -8,34 +8,39 @@
|
|
|
8
8
|
*
|
|
9
9
|
* Reads button nibble every 200 ms and mirrors it inverted to the LED nibble.
|
|
10
10
|
*/
|
|
11
|
-
const {
|
|
12
|
-
const { Pcf8574Full }
|
|
11
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
12
|
+
const { Pcf8574Full } = require('periph/src/chips/io_expander/pcf8574');
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
const
|
|
14
|
+
async function main() {
|
|
15
|
+
// --- Initialise bus and driver ---
|
|
16
|
+
// All pins default to inputs (shadow = 0xFF). We immediately write 0xF0
|
|
17
|
+
// to drive P0–P3 low (LEDs on) and keep P4–P7 as inputs (buttons).
|
|
18
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
19
|
+
const chip = new Pcf8574Full(connection); // Create PCF8574 full driver, (connection, addr=0x20)
|
|
19
20
|
|
|
20
|
-
chip.writePort(0, 0xF0);
|
|
21
|
+
await chip.writePort(0, 0xF0); // Write all 8 pins, (port, mask) → Promise<void>
|
|
21
22
|
|
|
22
|
-
// --- Wire interrupt for responsive button detection ---
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
//
|
|
27
|
-
|
|
23
|
+
// --- Wire interrupt for responsive button detection ---
|
|
24
|
+
// No options.intPin wired in this example, so onInterrupt() falls back
|
|
25
|
+
// to a 5 ms polling loop automatically. Pass { intPin } with a real
|
|
26
|
+
// InputPin (e.g. EpollInputPin) for edge-based delivery instead.
|
|
27
|
+
await chip.onInterrupt((changed) => { // Subscribe to INT line, (callback, options) → Promise<void>
|
|
28
|
+
// flag is handled in the polling interval below
|
|
29
|
+
});
|
|
28
30
|
|
|
29
|
-
// --- Main loop: read buttons → mirror to LEDs ---
|
|
30
|
-
console.log('Running — press buttons on P4–P7 to mirror to LEDs on P0–P3');
|
|
31
|
-
setInterval(() => {
|
|
32
|
-
|
|
31
|
+
// --- Main loop: read buttons → mirror to LEDs ---
|
|
32
|
+
console.log('Running — press buttons on P4–P7 to mirror to LEDs on P0–P3');
|
|
33
|
+
setInterval(async () => {
|
|
34
|
+
const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
|
|
33
35
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
const buttons = (port >> 4) & 0x0F; // bits 4–7: pressed = 0
|
|
37
|
+
const leds = (~buttons) & 0x0F; // active-low: pressed → LED on (bit = 0)
|
|
38
|
+
await chip.writePort(0, 0xF0 | leds); // Write all 8 pins, (port, mask) → Promise<void>
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}, 200);
|
|
40
|
+
const btnStr = [...Array(4)].map((_, i) => (buttons >> i) & 1 ? '.' : 'X').join('');
|
|
41
|
+
const ledStr = [...Array(4)].map((_, i) => (leds >> i) & 1 ? '*' : ' ').join('');
|
|
42
|
+
console.log(`port=0x${port.toString(16).padStart(2, '0')} buttons=[${btnStr}] LEDs=[${ledStr}]`);
|
|
43
|
+
}, 200);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
main();
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
const {
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
3
|
const { Pcf8574Minimal } = require('periph/src/chips/io_expander/pcf8574');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
const
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
7
|
+
const chip = new Pcf8574Minimal(connection); // Create PCF8574 driver, (connection, addr=0x20)
|
|
7
8
|
|
|
8
|
-
const p0 = chip.pin(0, 'out'); // Get pin proxy, (n, direction='out') → Pin
|
|
9
|
-
const p4 = chip.pin(4, 'in'); // Get pin proxy, (n, direction='in') → Pin
|
|
9
|
+
const p0 = chip.pin(0, 'out'); // Get pin proxy, (n, direction='out') → Pin
|
|
10
|
+
const p4 = chip.pin(4, 'in'); // Get pin proxy, (n, direction='in') → Pin
|
|
10
11
|
|
|
11
|
-
setInterval(() => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}, 200);
|
|
12
|
+
setInterval(async () => {
|
|
13
|
+
const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
|
|
14
|
+
const btn = (port >> 4) & 1;
|
|
15
|
+
await p0.write(btn ? 1 : 0); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
console.log('port=0x' + port.toString(16).padStart(2, '0'));
|
|
17
|
+
}, 200);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main();
|
|
@@ -1,31 +1,35 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
const { Pcf8575Minimal, Pcf8575Full } = require('../../src/chips/io_expander/pcf8575');
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
write
|
|
6
|
-
read
|
|
4
|
+
const connection = {
|
|
5
|
+
async write(buf) { console.log('write', buf); },
|
|
6
|
+
async read(len) { return Buffer.from([0xFF, 0xFF]); }
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
async function main() {
|
|
10
|
+
const chip = new Pcf8575Full(connection, 0x20); // Create PCF8575 full driver, (connection, addr=0x20)
|
|
10
11
|
|
|
11
|
-
const p0 = chip.pin(0);
|
|
12
|
-
p0.
|
|
13
|
-
p0.
|
|
14
|
-
const v = p0.
|
|
12
|
+
const p0 = chip.pin(0); // Get pin proxy, (n=0) → Pin
|
|
13
|
+
await p0.write(0); // Write pin, (value=0|1) → Promise<void>
|
|
14
|
+
await p0.write(1); // Write pin, (value=0|1) → Promise<void>
|
|
15
|
+
const v = await p0.read(); // Read pin, () → Promise<0|1>
|
|
15
16
|
|
|
16
|
-
const port0 = chip.readPort(0);
|
|
17
|
-
const port1 = chip.readPort(1);
|
|
17
|
+
const port0 = await chip.readPort(0); // Read Port 0, (port=0) → Promise<int bitmask>
|
|
18
|
+
const port1 = await chip.readPort(1); // Read Port 1, (port=1) → Promise<int bitmask>
|
|
18
19
|
|
|
19
|
-
chip.writePort(0, 0b00001111);
|
|
20
|
-
chip.writePort(1, 0b00001111);
|
|
20
|
+
await chip.writePort(0, 0b00001111); // Write Port 0, (port=0, mask=int) → Promise<void>
|
|
21
|
+
await chip.writePort(1, 0b00001111); // Write Port 1, (port=1, mask=int) → Promise<void>
|
|
21
22
|
|
|
22
|
-
const p8 = chip.pin(8);
|
|
23
|
-
p8.setDirection('in');
|
|
24
|
-
const state = p8.
|
|
23
|
+
const p8 = chip.pin(8); // Get pin proxy, (n=8) → Pin
|
|
24
|
+
await p8.setDirection('in'); // Set direction, (direction='in'|'out') → Promise<void>
|
|
25
|
+
const state = await p8.read(); // Read pin, () → Promise<0|1>
|
|
25
26
|
|
|
26
|
-
chip.
|
|
27
|
-
|
|
28
|
-
});
|
|
27
|
+
await chip.onInterrupt((changed) => { // Subscribe to INT line, (callback, options) → Promise<void>
|
|
28
|
+
console.log('changed:', changed);
|
|
29
|
+
});
|
|
29
30
|
|
|
30
|
-
const changed = chip.
|
|
31
|
-
console.log('v:', v, 'port0:', port0, 'port1:', port1, 'state:', state, 'changed:', changed);
|
|
31
|
+
const changed = await chip.pollInterrupt(); // Read port and return 16-bit changed bitmask, () → Promise<int>
|
|
32
|
+
console.log('v:', v, 'port0:', port0, 'port1:', port1, 'state:', state, 'changed:', changed);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
main();
|