periph 1.0.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/examples/accelerometer/.gitkeep +0 -0
- package/examples/adc_dac/.gitkeep +0 -0
- package/examples/adc_dac/mcp4725/complete.js +34 -0
- package/examples/adc_dac/mcp4725/demo.js +43 -0
- package/examples/adc_dac/mcp4725/minimal.js +14 -0
- package/examples/color/.gitkeep +0 -0
- package/examples/comms/.gitkeep +0 -0
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gas/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/gpio/.gitkeep +0 -0
- package/examples/gyroscope/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/io_expander/mcp23017/complete.js +80 -0
- package/examples/io_expander/mcp23017/demo.js +59 -0
- package/examples/io_expander/mcp23017/minimal.js +16 -0
- package/examples/io_expander/pcf8574/complete.js +68 -0
- package/examples/io_expander/pcf8574/demo.js +41 -0
- package/examples/io_expander/pcf8574/minimal.js +16 -0
- package/examples/io_expander/pcf8575/complete.js +31 -0
- package/examples/io_expander/pcf8575/demo.js +33 -0
- package/examples/io_expander/pcf8575/minimal.js +20 -0
- package/examples/led/.gitkeep +0 -0
- package/examples/led/sk6812rgbw/complete.js +69 -0
- package/examples/led/sk6812rgbw/demo.js +85 -0
- package/examples/led/sk6812rgbw/minimal.js +25 -0
- package/examples/led/ws2812b/complete.js +64 -0
- package/examples/led/ws2812b/demo.js +101 -0
- package/examples/led/ws2812b/minimal.js +22 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/magnetometer/.gitkeep +0 -0
- package/examples/magnetometer/as5600/complete.js +43 -0
- package/examples/magnetometer/as5600/demo.js +49 -0
- package/examples/magnetometer/as5600/minimal.js +16 -0
- package/examples/memory/.gitkeep +0 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/other/.gitkeep +0 -0
- package/examples/power/ina219/complete.js +30 -0
- package/examples/power/ina219/demo.js +26 -0
- package/examples/power/ina219/minimal.js +17 -0
- package/examples/power/ina226/complete.js +33 -0
- package/examples/power/ina226/demo.js +30 -0
- package/examples/power/ina226/minimal.js +11 -0
- package/examples/power/ina3221/complete.js +65 -0
- package/examples/power/ina3221/demo.js +56 -0
- package/examples/power/ina3221/minimal.js +16 -0
- package/examples/pressure/.gitkeep +0 -0
- package/examples/pressure/bmp180/complete.js +26 -0
- package/examples/pressure/bmp180/demo.js +32 -0
- package/examples/pressure/bmp180/minimal.js +17 -0
- package/examples/pressure/bmp280/complete.js +28 -0
- package/examples/pressure/bmp280/demo.js +34 -0
- package/examples/pressure/bmp280/minimal.js +17 -0
- package/examples/rfid/.gitkeep +0 -0
- package/examples/rtc/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/package.json +20 -0
- package/src/chips/accelerometer/.gitkeep +0 -0
- package/src/chips/adc_dac/.gitkeep +0 -0
- package/src/chips/adc_dac/mcp4725.js +161 -0
- package/src/chips/color/.gitkeep +0 -0
- package/src/chips/comms/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/gas/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/gpio/.gitkeep +0 -0
- package/src/chips/gyroscope/.gitkeep +0 -0
- package/src/chips/humidity/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/io_expander/mcp23017.js +396 -0
- package/src/chips/io_expander/pcf8574.js +298 -0
- package/src/chips/io_expander/pcf8575.js +295 -0
- package/src/chips/led/.gitkeep +0 -0
- package/src/chips/led/_color.js +25 -0
- package/src/chips/led/sk6812rgbw.js +183 -0
- package/src/chips/led/ws2812b.js +171 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/magnetometer/as5600.js +293 -0
- package/src/chips/memory/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/other/.gitkeep +0 -0
- package/src/chips/power/ina219.js +154 -0
- package/src/chips/power/ina226.js +208 -0
- package/src/chips/power/ina3221.js +341 -0
- package/src/chips/pressure/.gitkeep +0 -0
- package/src/chips/pressure/bmp180.js +234 -0
- package/src/chips/pressure/bmp280.js +328 -0
- package/src/chips/rfid/.gitkeep +0 -0
- package/src/chips/rtc/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
- package/src/index.js +2 -0
- package/src/transport/i2c.js +67 -0
- package/src/transport/neopixel.js +37 -0
- package/src/transport/smbus.js +117 -0
- package/src/transport/spi.js +72 -0
- package/src/transport/uart.js +145 -0
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { MCP4725Full } = require('../../../src/chips/adc_dac/mcp4725');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 16);
|
|
8
|
+
|
|
9
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const dac = new MCP4725Full(transport);
|
|
11
|
+
|
|
12
|
+
dac.set_voltage(0.75); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
|
|
13
|
+
// converts fraction to 12-bit code and issues Fast Write
|
|
14
|
+
dac.set_raw(3000); // Set raw 12-bit code, (code=0–4095) → None
|
|
15
|
+
// clamps to [0, 4095] and writes DAC register only
|
|
16
|
+
dac.set_voltage_eeprom(0.5); // Set output and persist to EEPROM, (fraction=0.0–1.0) → None
|
|
17
|
+
// writes both DAC register and EEPROM for power-cycle persistence
|
|
18
|
+
dac.set_raw_eeprom(2048); // Set raw code and persist to EEPROM, (code=0–4095) → None
|
|
19
|
+
// writes both DAC register and EEPROM for power-cycle persistence
|
|
20
|
+
const state = dac.read(); // Read DAC and EEPROM registers, () → dict
|
|
21
|
+
// returns code, voltage_fraction, power_down, eeprom_code, eeprom_power_down, eeprom_ready
|
|
22
|
+
console.log('code:', state.code);
|
|
23
|
+
console.log('voltage_fraction:', state.voltage_fraction);
|
|
24
|
+
console.log('eeprom_ready:', state.eeprom_ready);
|
|
25
|
+
dac.set_power_down(MCP4725Full.PD_100K_GND); // Set power-down mode with code preserved, (mode=0–3) → None
|
|
26
|
+
// enters power-down; output stage disconnects with 100k to GND
|
|
27
|
+
dac.wake_up(); // Send General Call Wake-Up to clear power-down, () → None
|
|
28
|
+
// sends 0x00, 0x09 to address 0x00; clears PD bits in DAC register
|
|
29
|
+
dac.reset(); // Send General Call Reset and reload EEPROM, () → None
|
|
30
|
+
// sends 0x00, 0x06; triggers internal POR and reloads DAC from EEPROM
|
|
31
|
+
const ready = dac.is_eeprom_ready(); // Check if EEPROM write is complete, () → bool
|
|
32
|
+
// returns True when any pending EEPROM write has finished
|
|
33
|
+
console.log('eeprom_ready:', ready);
|
|
34
|
+
console.log('MCP4725 complete running');
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { MCP4725Full } = require('../../../src/chips/adc_dac/mcp4725');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 16);
|
|
8
|
+
|
|
9
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const dac = new MCP4725Full(transport);
|
|
11
|
+
|
|
12
|
+
const STEP = 1.0 / 20.0;
|
|
13
|
+
const DELAY_MS = 100;
|
|
14
|
+
|
|
15
|
+
function delay(ms) {
|
|
16
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function run_triangle_wave() {
|
|
20
|
+
console.log('MCP4725 demo: triangle wave');
|
|
21
|
+
// Up sweep: 0 to full scale in 21 steps
|
|
22
|
+
for (let n = 0; n <= 20; n++) {
|
|
23
|
+
const fraction = n * STEP;
|
|
24
|
+
dac.set_voltage(fraction); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
|
|
25
|
+
const code = Math.round(fraction * 4095);
|
|
26
|
+
const approx_v = code * 3.3 / 4096;
|
|
27
|
+
console.log(`n=${String(n).padStart(2)} fraction=${fraction.toFixed(2)} code=${String(code).padStart(4)} approx_v=${approx_v.toFixed(3)}V`);
|
|
28
|
+
await delay(DELAY_MS);
|
|
29
|
+
}
|
|
30
|
+
// Down sweep: full scale back to 0 in 20 steps
|
|
31
|
+
for (let n = 20; n >= 0; n--) {
|
|
32
|
+
const fraction = n * STEP;
|
|
33
|
+
dac.set_voltage(fraction); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
|
|
34
|
+
const code = Math.round(fraction * 4095);
|
|
35
|
+
const approx_v = code * 3.3 / 4096;
|
|
36
|
+
console.log(`n=${String(n).padStart(2)} fraction=${fraction.toFixed(2)} code=${String(code).padStart(4)} approx_v=${approx_v.toFixed(3)}V`);
|
|
37
|
+
await delay(DELAY_MS);
|
|
38
|
+
}
|
|
39
|
+
console.log('Demo complete');
|
|
40
|
+
process.exit(0);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
run_triangle_wave();
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { MCP4725Minimal } = require('../../../src/chips/adc_dac/mcp4725');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 16);
|
|
8
|
+
|
|
9
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const dac = new MCP4725Minimal(transport);
|
|
11
|
+
|
|
12
|
+
dac.set_voltage(0.5); // Set output as fraction of V_DD, (fraction=0.0–1.0) → None
|
|
13
|
+
dac.set_raw(2048); // Set raw 12-bit code, (code=0–4095) → None
|
|
14
|
+
console.log('MCP4725 minimal running');
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CTransport } = require('periph/src/transport/i2c');
|
|
3
|
+
const { Mcp23017Minimal, Mcp23017Full } = require('periph/src/chips/io_expander/mcp23017');
|
|
4
|
+
|
|
5
|
+
const transport = new I2CTransport(1, 0x20); // Create I2C transport, (bus, addr=0x20)
|
|
6
|
+
const chip = new Mcp23017Full(transport); // Create MCP23017 full driver, (transport, addr=0x20)
|
|
7
|
+
|
|
8
|
+
// --- Pin proxy — synchronous API ---
|
|
9
|
+
const p0 = chip.pin(0, 'out'); // Get full pin proxy, (n, direction='out') → FullPin
|
|
10
|
+
// GPA0 as output (GPA7/GPB7 are output-only on hardware)
|
|
11
|
+
console.log('direction:', p0.direction); // direction property, () → string 'in'|'out'
|
|
12
|
+
|
|
13
|
+
p0.writeSync(1); // Write pin synchronously, (value=0|1) → void
|
|
14
|
+
p0.writeSync(0); // Write pin synchronously, (value=0|1) → void
|
|
15
|
+
|
|
16
|
+
const v = p0.readSync(); // Read pin synchronously, () → int 0|1
|
|
17
|
+
// reads GPIOA, extracts bit 0
|
|
18
|
+
console.log('GPA0 level:', v);
|
|
19
|
+
|
|
20
|
+
// --- Async API ---
|
|
21
|
+
p0.read((err, val) => { // Read pin async, (callback(err,val)) → void
|
|
22
|
+
if (!err) console.log('async read:', val);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
p0.write(1, (err) => { // Write pin async, (value, callback(err)) → void
|
|
26
|
+
if (!err) console.log('async write done');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// --- Direction change ---
|
|
30
|
+
p0.setDirection('in', (err) => { // Set direction, (direction, callback(err)) → void
|
|
31
|
+
if (!err) console.log('now input');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// --- Port-level bulk operations ---
|
|
35
|
+
const porta = chip.readPort(0); // Read all 8 pins, (port=0) → int bitmask
|
|
36
|
+
// PORTA = GPIOA register
|
|
37
|
+
const portb = chip.readPort(1); // Read all 8 pins, (port=1) → int bitmask
|
|
38
|
+
// PORTB = GPIOB register
|
|
39
|
+
console.log('PORTA=0x' + porta.toString(16).padStart(2,'0') + ' PORTB=0x' + portb.toString(16).padStart(2,'0'));
|
|
40
|
+
chip.writePort(0, 0b00001111); // Write all 8 pins, (port, mask) → void
|
|
41
|
+
// GPA0–GPA3 as outputs low, GPA4–GPA6 inputs, GPA7 output low
|
|
42
|
+
chip.writePort(1, 0b11110000); // Write all 8 pins, (port, mask) → void
|
|
43
|
+
// GPB0–GPB3 outputs low, GPB4–GPB7 outputs high
|
|
44
|
+
|
|
45
|
+
// --- Pull-up configuration ---
|
|
46
|
+
chip.configurePullup(0, 0b01111111); // Configure pull-ups, (port, mask) → void
|
|
47
|
+
// enable pull-ups on GPA0–GPA6 (inputs); GPA7 is output, pull-up has no effect
|
|
48
|
+
|
|
49
|
+
// --- Polarity inversion ---
|
|
50
|
+
chip.configurePolarity(1, 0x00); // Configure polarity, (port, mask) → void
|
|
51
|
+
// normal polarity on PORTB
|
|
52
|
+
|
|
53
|
+
// --- Interrupt-on-change ---
|
|
54
|
+
chip.configureInterrupt(0, null, (port, changed) => { // Attach interrupt, (port, intGpioPath, callback) → void
|
|
55
|
+
console.log('PORTA changed: 0x' + changed.toString(16).padStart(2,'0'));
|
|
56
|
+
});
|
|
57
|
+
// null = use 5 ms polling; INTA fires on any GPA change
|
|
58
|
+
|
|
59
|
+
const flags = chip.readInterruptFlags(0); // Read interrupt flags, (port) → int
|
|
60
|
+
// INTFA register; 1 = corresponding pin caused interrupt
|
|
61
|
+
console.log('INT flags PORTA: 0x' + flags.toString(16).padStart(2,'0'));
|
|
62
|
+
|
|
63
|
+
const changed = chip.clearInterrupt(0); // Read and clear interrupt, (port) → int
|
|
64
|
+
// INTCAPA captured state; also returns changed bitmask
|
|
65
|
+
console.log('changed on init:', changed);
|
|
66
|
+
|
|
67
|
+
// --- Per-pin watch ---
|
|
68
|
+
const p1 = chip.pin(1, 'in'); // Get full pin proxy, (n, direction='in') → FullPin
|
|
69
|
+
const watcher = (err, val) => console.log('GPA1 changed:', val);
|
|
70
|
+
p1.watch(watcher); // Attach change handler, (handler(err,val)) → void
|
|
71
|
+
// fires when GPA1 changes; needs configureInterrupt first
|
|
72
|
+
p1.unwatch(watcher); // Remove change handler, (handler) → void
|
|
73
|
+
p1.unwatchAll(); // Remove all handlers for pin, () → void
|
|
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);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* MCP23017 demo — Knight Rider scanner with button override.
|
|
4
|
+
*
|
|
5
|
+
* Hardware:
|
|
6
|
+
* GPA0–GPA6: seven LEDs (anode → VCC via 220Ω, cathode → pin; active-high)
|
|
7
|
+
* GPB0–GPB6: seven push buttons (pin → GND when pressed; internal pull-up = high when idle)
|
|
8
|
+
*
|
|
9
|
+
* Runs a Knight Rider scanning pattern on the PORTA LEDs. Pressing a button
|
|
10
|
+
* overrides the scanner and lights the matching LED. The loop reads GPIOB
|
|
11
|
+
* every 100 ms, builds the output mask from the button state (inverted, since
|
|
12
|
+
* active-low buttons), ORs it with the scanner position unless a button is
|
|
13
|
+
* pressed, then writes to OLATA.
|
|
14
|
+
*/
|
|
15
|
+
const { I2CTransport } = require('periph/src/transport/i2c');
|
|
16
|
+
const { Mcp23017Full } = require('periph/src/chips/io_expander/mcp23017');
|
|
17
|
+
|
|
18
|
+
// --- Initialise bus and driver ---
|
|
19
|
+
const transport = new I2CTransport(1, 0x20); // Create I2C transport, (bus, addr=0x20)
|
|
20
|
+
const chip = new Mcp23017Full(transport); // Create MCP23017 full driver, (transport, addr=0x20)
|
|
21
|
+
|
|
22
|
+
// Enable pull-ups on PORTB inputs (GPB0–GPB6) so idle buttons read high.
|
|
23
|
+
chip.configurePullup(1, 0b01111111); // Configure pull-ups, (port=1, mask) → void
|
|
24
|
+
|
|
25
|
+
// --- Wire interrupt for responsive button detection ---
|
|
26
|
+
chip.configureInterrupt(1, null, (port, changed) => { // Attach interrupt, (port, intGpioPath, callback) → void
|
|
27
|
+
// handled in the polling loop below
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
let position = 0;
|
|
31
|
+
let direction = 1;
|
|
32
|
+
|
|
33
|
+
// --- Main loop: Knight Rider with button override ---
|
|
34
|
+
console.log('Running — press buttons GPB0–GPB6 to light corresponding LEDs');
|
|
35
|
+
setInterval(() => {
|
|
36
|
+
const portb = chip.readPort(1); // Read all 8 pins, (port=1) → int bitmask
|
|
37
|
+
// GPB0–GPB6 buttons: pressed = 0 (active-low pull-down)
|
|
38
|
+
|
|
39
|
+
const buttons = portb & 0x7F; // mask GPA7 (output-only)
|
|
40
|
+
const pressed = (~buttons) & 0x7F; // invert: pressed button = bit 1
|
|
41
|
+
|
|
42
|
+
const scanner = 1 << position;
|
|
43
|
+
|
|
44
|
+
let output;
|
|
45
|
+
if (pressed) {
|
|
46
|
+
output = pressed | (1 << 7); // keep GPA7 high (output-only)
|
|
47
|
+
} else {
|
|
48
|
+
output = scanner | (1 << 7);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
chip.writePort(0, output); // Write all 8 pins, (port=0, mask) → void
|
|
52
|
+
|
|
53
|
+
const ledStr = [...Array(7)].map((_, i) => (output >> i) & 1 ? '*' : ' ').join('');
|
|
54
|
+
console.log('PORTA=0x' + output.toString(16).padStart(2,'0') + ' [' + ledStr + '] buttons=0x' + buttons.toString(16).padStart(2,'0'));
|
|
55
|
+
|
|
56
|
+
position += direction;
|
|
57
|
+
if (position === 6) direction = -1;
|
|
58
|
+
if (position === 0) direction = 1;
|
|
59
|
+
}, 100);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CTransport } = require('periph/src/transport/i2c');
|
|
3
|
+
const { Mcp23017Minimal } = require('periph/src/chips/io_expander/mcp23017');
|
|
4
|
+
|
|
5
|
+
const transport = new I2CTransport(1, 0x20); // Create I2C transport, (bus, addr=0x20)
|
|
6
|
+
const chip = new Mcp23017Minimal(transport); // Create MCP23017 driver, (transport, addr=0x20)
|
|
7
|
+
|
|
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
|
|
10
|
+
|
|
11
|
+
setInterval(() => {
|
|
12
|
+
const porta = chip.readPort(0); // Read all 8 pins, (port=0) → int bitmask
|
|
13
|
+
const portb = chip.readPort(1); // Read all 8 pins, (port=1) → int bitmask
|
|
14
|
+
p0.writeSync((porta >> 1) & 1); // Write pin, (value=0|1) → void
|
|
15
|
+
console.log('PORTA=0x' + porta.toString(16).padStart(2,'0') + ' PORTB=0x' + portb.toString(16).padStart(2,'0'));
|
|
16
|
+
}, 200);
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CTransport } = require('periph/src/transport/i2c');
|
|
3
|
+
const { Pcf8574Minimal, Pcf8574Full } = require('periph/src/chips/io_expander/pcf8574');
|
|
4
|
+
|
|
5
|
+
const transport = new I2CTransport(1, 0x20); // Create I2C transport, (bus, addr=0x20)
|
|
6
|
+
const chip = new Pcf8574Full(transport); // Create PCF8574 full driver, (transport, addr=0x20)
|
|
7
|
+
// initialises all pins as inputs; shadow = 0xFF
|
|
8
|
+
|
|
9
|
+
// --- Pin proxy — synchronous API ---
|
|
10
|
+
const p0 = chip.pin(0, 'out'); // Get full pin proxy, (n, direction='out') → FullPin
|
|
11
|
+
// drives P0 low initially; returns FullPin
|
|
12
|
+
console.log('direction:', p0.direction); // direction property, () → string 'in'|'out'
|
|
13
|
+
// reflects the direction set at pin() call time
|
|
14
|
+
|
|
15
|
+
p0.writeSync(1); // Write pin synchronously, (value=0|1) → void
|
|
16
|
+
// shadow |= (1<<0); quasi-high (input mode)
|
|
17
|
+
p0.writeSync(0); // Write pin synchronously, (value=0|1) → void
|
|
18
|
+
// shadow &= ~(1<<0); drives low
|
|
19
|
+
|
|
20
|
+
const v = p0.readSync(); // Read pin synchronously, () → int 0|1
|
|
21
|
+
// reads full port byte, extracts bit 0
|
|
22
|
+
console.log('P0 level:', v);
|
|
23
|
+
|
|
24
|
+
// --- Async API ---
|
|
25
|
+
p0.read((err, val) => { // Read pin async, (callback(err,val)) → void
|
|
26
|
+
if (!err) console.log('async read:', val); // wraps readSync; fires callback immediately
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
p0.write(1, (err) => { // Write pin async, (value, callback(err)) → void
|
|
30
|
+
if (!err) console.log('async write done'); // wraps writeSync; fires callback immediately
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// --- Direction change ---
|
|
34
|
+
p0.setDirection('in', (err) => { // Set direction, (direction, callback(err)) → void
|
|
35
|
+
if (!err) console.log('now input'); // updates shadow bit to 1 (input mode)
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// --- Port-level bulk operations ---
|
|
39
|
+
const mask = chip.readPort(); // Read all 8 pins, (port=0) → int bitmask
|
|
40
|
+
// bit 0 = P0, bit 7 = P7
|
|
41
|
+
console.log('port:', mask.toString(16));
|
|
42
|
+
chip.writePort(0, 0b00001111); // Write all 8 pins, (port, mask) → void
|
|
43
|
+
// P0–P3 low (outputs), P4–P7 high (inputs)
|
|
44
|
+
|
|
45
|
+
// --- Input pin ---
|
|
46
|
+
const p4 = chip.pin(4, 'in'); // Get full pin proxy, (n, direction='in') → FullPin
|
|
47
|
+
const state = p4.readSync(); // Read pin synchronously, () → int
|
|
48
|
+
// 0 if button pulls P4 low, 1 if floating
|
|
49
|
+
console.log('P4:', state);
|
|
50
|
+
|
|
51
|
+
// --- Interrupt-on-change ---
|
|
52
|
+
chip.configureInterrupt(null, (changed) => { // Attach interrupt, (intGpioPath, callback) → void
|
|
53
|
+
console.log('changed:', changed.toString(2).padStart(8,'0')); // null = use 5 ms polling
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const changed = chip.clearInterrupt(); // Read and return changed bitmask, () → int
|
|
57
|
+
// compares current byte to previous; clears INT
|
|
58
|
+
console.log('changed on init:', changed);
|
|
59
|
+
|
|
60
|
+
// --- Per-pin watch ---
|
|
61
|
+
const p5 = chip.pin(5, 'in'); // Get full pin proxy, (n, direction='in') → FullPin
|
|
62
|
+
const watcher = (err, val) => console.log('P5 changed:', val);
|
|
63
|
+
p5.watch(watcher); // Attach change handler, (handler(err,val)) → void
|
|
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);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* PCF8574 demo — button-controlled LED mirror.
|
|
4
|
+
*
|
|
5
|
+
* Hardware:
|
|
6
|
+
* P0–P3: LEDs (anode → VCC, cathode → pin; active-low)
|
|
7
|
+
* P4–P7: push buttons (pin → GND when pressed; internal pull-up = high when idle)
|
|
8
|
+
*
|
|
9
|
+
* Reads button nibble every 200 ms and mirrors it inverted to the LED nibble.
|
|
10
|
+
*/
|
|
11
|
+
const { I2CTransport } = require('periph/src/transport/i2c');
|
|
12
|
+
const { Pcf8574Full } = require('periph/src/chips/io_expander/pcf8574');
|
|
13
|
+
|
|
14
|
+
// --- Initialise bus and driver ---
|
|
15
|
+
// All pins default to inputs (shadow = 0xFF). We immediately write 0xF0
|
|
16
|
+
// to drive P0–P3 low (LEDs on) and keep P4–P7 as inputs (buttons).
|
|
17
|
+
const transport = new I2CTransport(1, 0x20); // Create I2C transport, (bus, addr=0x20)
|
|
18
|
+
const chip = new Pcf8574Full(transport); // Create PCF8574 full driver, (transport, addr=0x20)
|
|
19
|
+
|
|
20
|
+
chip.writePort(0, 0xF0); // Write all 8 pins, (port, mask) → void
|
|
21
|
+
|
|
22
|
+
// --- Wire interrupt for responsive button detection ---
|
|
23
|
+
// Using polling (null) since no GPIO-mapped INT line in this example.
|
|
24
|
+
// Replace null with '/sys/class/gpio/gpio5/value' for edge-based delivery.
|
|
25
|
+
chip.configureInterrupt(null, (changed) => { // Attach interrupt, (intGpioPath, callback) → void
|
|
26
|
+
// flag is handled in the polling interval below
|
|
27
|
+
});
|
|
28
|
+
|
|
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
|
+
const port = chip.readPort(); // Read all 8 pins, (port=0) → int bitmask
|
|
33
|
+
|
|
34
|
+
const buttons = (port >> 4) & 0x0F; // bits 4–7: pressed = 0
|
|
35
|
+
const leds = (~buttons) & 0x0F; // active-low: pressed → LED on (bit = 0)
|
|
36
|
+
chip.writePort(0, 0xF0 | leds); // Write all 8 pins, (port, mask) → void
|
|
37
|
+
|
|
38
|
+
const btnStr = [...Array(4)].map((_, i) => (buttons >> i) & 1 ? '.' : 'X').join('');
|
|
39
|
+
const ledStr = [...Array(4)].map((_, i) => (leds >> i) & 1 ? '*' : ' ').join('');
|
|
40
|
+
console.log(`port=0x${port.toString(16).padStart(2,'0')} buttons=[${btnStr}] LEDs=[${ledStr}]`);
|
|
41
|
+
}, 200);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CTransport } = require('periph/src/transport/i2c');
|
|
3
|
+
const { Pcf8574Minimal } = require('periph/src/chips/io_expander/pcf8574');
|
|
4
|
+
|
|
5
|
+
const transport = new I2CTransport(1, 0x20); // Create I2C transport, (bus, addr=0x20)
|
|
6
|
+
const chip = new Pcf8574Minimal(transport); // Create PCF8574 driver, (transport, addr=0x20)
|
|
7
|
+
|
|
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
|
|
10
|
+
|
|
11
|
+
setInterval(() => {
|
|
12
|
+
const port = chip.readPort(); // Read all 8 pins, (port=0) → int bitmask
|
|
13
|
+
const btn = (port >> 4) & 1;
|
|
14
|
+
p0.writeSync(btn ? 1 : 0); // Write pin, (value=0|1) → void
|
|
15
|
+
console.log('port=0x' + port.toString(16).padStart(2, '0'));
|
|
16
|
+
}, 200);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { Pcf8575Minimal, Pcf8575Full } = require('../../src/chips/io_expander/pcf8575');
|
|
3
|
+
|
|
4
|
+
const transport = {
|
|
5
|
+
write: (buf) => { console.log('write', buf); },
|
|
6
|
+
read: (len) => { return Buffer.from([0xFF, 0xFF]); }
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const chip = new Pcf8575Full(transport, 0x20); // Create PCF8575 full driver, (transport, addr=0x20)
|
|
10
|
+
|
|
11
|
+
const p0 = chip.pin(0); // Get pin proxy, (n=0) → Pin
|
|
12
|
+
p0.writeSync(0); // Write pin, (value=0|1) → void
|
|
13
|
+
p0.writeSync(1); // Write pin, (value=0|1) → void
|
|
14
|
+
const v = p0.readSync(); // Read pin, () → 0|1
|
|
15
|
+
|
|
16
|
+
const port0 = chip.readPort(0); // Read Port 0, (port=0) → int bitmask
|
|
17
|
+
const port1 = chip.readPort(1); // Read Port 1, (port=1) → int bitmask
|
|
18
|
+
|
|
19
|
+
chip.writePort(0, 0b00001111); // Write Port 0, (port=0, mask=int) → void
|
|
20
|
+
chip.writePort(1, 0b00001111); // Write Port 1, (port=1, mask=int) → void
|
|
21
|
+
|
|
22
|
+
const p8 = chip.pin(8); // Get pin proxy, (n=8) → Pin
|
|
23
|
+
p8.setDirection('in'); // Set direction, (direction='in'|'out', cb) → void
|
|
24
|
+
const state = p8.readSync(); // Read pin, () → 0|1
|
|
25
|
+
|
|
26
|
+
chip.configureInterrupt(null, (changed) => { // Attach interrupt, (int_gpio_path|null, callback) → void
|
|
27
|
+
console.log('changed:', changed);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const changed = chip.clearInterrupt(); // Read port and return 16-bit changed bitmask, () → int
|
|
31
|
+
console.log('v:', v, 'port0:', port0, 'port1:', port1, 'state:', state, 'changed:', changed);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { Pcf8575Full } = require('../../src/chips/io_expander/pcf8575');
|
|
3
|
+
|
|
4
|
+
const transport = {
|
|
5
|
+
write: (buf) => { /* LED write */ },
|
|
6
|
+
read: (len) => { return Buffer.from([0xFF, 0xFF]); }
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const chip = new Pcf8575Full(transport, 0x20); // Create PCF8575 full driver, (transport, addr=0x20)
|
|
10
|
+
|
|
11
|
+
chip.writePort(0, 0xFF); // Write Port 0, (port=0, mask=int) → void
|
|
12
|
+
chip.writePort(1, 0xFF); // Write Port 1, (port=1, mask=int) → void
|
|
13
|
+
|
|
14
|
+
chip.configureInterrupt(null, (changed) => { // Attach interrupt, (int_gpio_path|null, callback) → void
|
|
15
|
+
console.log('changed:', changed);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
console.log('Running — buttons on P10–P17 mirror to LEDs on P00–P07');
|
|
19
|
+
setInterval(() => {
|
|
20
|
+
const port0 = chip.readPort(0); // Read Port 0, (port=0) → int bitmask
|
|
21
|
+
const port1 = chip.readPort(1); // Read Port 1, (port=1) → int bitmask
|
|
22
|
+
|
|
23
|
+
const buttons = port1 & 0xFF; // P10–P17 (pressed = 0)
|
|
24
|
+
const led_bits = (~buttons) & 0xFF; // invert: pressed → LED on (0)
|
|
25
|
+
chip.writePort(0, led_bits); // Write Port 0, (port=0, mask=int) → void
|
|
26
|
+
|
|
27
|
+
console.log(
|
|
28
|
+
'P0=0x' + port0.toString(16).padStart(2, '0') +
|
|
29
|
+
' P1=0x' + port1.toString(16).padStart(2, '0') +
|
|
30
|
+
' buttons=' + Array.from({length: 8}, (_, i) => (buttons >> (7 - i)) & 1 ? '.' : 'X').join('') +
|
|
31
|
+
' LEDs=' + Array.from({length: 8}, (_, i) => (led_bits >> (7 - i)) & 1 ? ' ' : '*').join('')
|
|
32
|
+
);
|
|
33
|
+
}, 200);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { Pcf8575Minimal } = require('../../src/chips/io_expander/pcf8575');
|
|
3
|
+
|
|
4
|
+
const transport = { // Mock transport for example
|
|
5
|
+
write: (buf) => { console.log('write', buf); },
|
|
6
|
+
read: (len) => { return Buffer.from([0xFF, 0xFF]); }
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const chip = new Pcf8575Minimal(transport, 0x20); // Create PCF8575 driver, (transport, addr=0x20)
|
|
10
|
+
|
|
11
|
+
const p0 = chip.pin(0); // Get pin proxy, (n=0) → Pin
|
|
12
|
+
const p8 = chip.pin(8); // Get pin proxy, (n=8) → Pin
|
|
13
|
+
|
|
14
|
+
p0.writeSync(0); // Write pin, (value=0|1) → void
|
|
15
|
+
const v = p0.readSync(); // Read pin, () → 0|1
|
|
16
|
+
console.log('pin0 value:', v);
|
|
17
|
+
|
|
18
|
+
const port0 = chip.readPort(0); // Read Port 0, (port=0) → int bitmask
|
|
19
|
+
const port1 = chip.readPort(1); // Read Port 1, (port=1) → int bitmask
|
|
20
|
+
console.log('port0:', port0, 'port1:', port1);
|
|
File without changes
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { NeoPixelTransport } = require('../../../src/transport/neopixel');
|
|
4
|
+
const { SK6812RGBWFull } = require('../../../src/chips/led/sk6812rgbw');
|
|
5
|
+
|
|
6
|
+
const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
|
|
7
|
+
const SPI_DEVICE = parseInt(process.env.SPI_DEVICE || '0', 10);
|
|
8
|
+
|
|
9
|
+
const transport = new NeoPixelTransport(SPI_BUS, SPI_DEVICE); // Create NeoPixel transport, (busNumber, deviceNumber)
|
|
10
|
+
const strip = new SK6812RGBWFull(transport, 8); // Create SK6812RGBW full driver, (transport, n=8 pixels)
|
|
11
|
+
|
|
12
|
+
// fill — set all pixels and send immediately
|
|
13
|
+
strip.fill(255, 0, 0); // Fill all pixels with one colour, (r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
14
|
+
// stores GRBW in buffer and calls transport.write()
|
|
15
|
+
|
|
16
|
+
// fill with white channel
|
|
17
|
+
strip.fill(0, 0, 0, 255); // Fill all pixels using W channel, (r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
18
|
+
// w=255 activates dedicated white LED element; RGB channels stay dark
|
|
19
|
+
|
|
20
|
+
// set individual pixels then show
|
|
21
|
+
strip.set_pixel(0, 255, 0, 0); // Set pixel 0 in buffer (no send), (index=0–n-1, r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
22
|
+
// writes G,R,B,W bytes into internal buffer at position index*4
|
|
23
|
+
strip.set_pixel(1, 0, 255, 0); // Set pixel 1 in buffer (no send), (index=0–n-1, r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
24
|
+
// writes G,R,B,W bytes into internal buffer at position index*4
|
|
25
|
+
strip.set_pixel(2, 0, 0, 255); // Set pixel 2 in buffer (no send), (index=0–n-1, r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
26
|
+
// writes G,R,B,W bytes into internal buffer at position index*4
|
|
27
|
+
strip.set_pixel(3, 0, 0, 0, 255); // Set pixel 3 to white in buffer (no send), (index=0–n-1, r=0–255, g=0–255, b=0–255, w=0–255) → void
|
|
28
|
+
// w=255 lights the dedicated white element; RGB remain off
|
|
29
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
30
|
+
// applies brightness scaling then calls transport.write()
|
|
31
|
+
|
|
32
|
+
// set_pixels — write multiple pixels at once
|
|
33
|
+
strip.set_pixels([[255,0,0,0],[0,255,0,0],[0,0,255,0], // Set pixels from array of [r,g,b,w], (colors=Array<[r,g,b,w?]>) → void
|
|
34
|
+
[0,0,0,255],[255,200,150,255]]);
|
|
35
|
+
// handles 3- or 4-element tuples; writes entries sequentially from pixel 0
|
|
36
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
37
|
+
// applies brightness scaling then calls transport.write()
|
|
38
|
+
|
|
39
|
+
// brightness — global scale applied at show() time
|
|
40
|
+
strip.brightness = 64; // Set global brightness, (value=0–255)
|
|
41
|
+
// stored value is scaled: sent = stored * brightness / 255
|
|
42
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
43
|
+
// applies brightness scaling then calls transport.write()
|
|
44
|
+
strip.brightness = 255; // Set global brightness, (value=0–255)
|
|
45
|
+
// stored value is scaled: sent = stored * brightness / 255
|
|
46
|
+
|
|
47
|
+
// fill_hsv — fill all pixels from HSV colour (white=0)
|
|
48
|
+
strip.fill_hsv(0.0, 1.0, 1.0); // Fill all pixels with HSV colour and send, (h=0.0–1.0, s=0.0–1.0, v=0.0–1.0) → void
|
|
49
|
+
// converts HSV to RGB (w=0) then calls fill(); hue 0.0 = red
|
|
50
|
+
strip.fill_hsv(0.333, 1.0, 1.0); // Fill all pixels with HSV colour and send, (h=0.0–1.0, s=0.0–1.0, v=0.0–1.0) → void
|
|
51
|
+
// converts HSV to RGB (w=0) then calls fill(); hue 0.333 = green
|
|
52
|
+
strip.fill_hsv(0.667, 1.0, 1.0); // Fill all pixels with HSV colour and send, (h=0.0–1.0, s=0.0–1.0, v=0.0–1.0) → void
|
|
53
|
+
// converts HSV to RGB (w=0) then calls fill(); hue 0.667 = blue
|
|
54
|
+
|
|
55
|
+
// rotate — shift pixel buffer left, then show
|
|
56
|
+
strip.set_pixels([[255,0,0,0], ...Array(7).fill([0,0,0,0])]); // Set pixels from array of [r,g,b,w], (colors=Array<[r,g,b,w?]>) → void
|
|
57
|
+
// handles 3- or 4-element tuples; writes entries sequentially from pixel 0
|
|
58
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
59
|
+
// applies brightness scaling then calls transport.write()
|
|
60
|
+
for (let i = 0; i < 7; i++) {
|
|
61
|
+
strip.rotate(1); // Rotate pixel buffer left, (steps=1) → void
|
|
62
|
+
// shifts buffer by steps pixel positions; wraps around; does not send
|
|
63
|
+
strip.show(); // Transmit buffer to strip, () → void
|
|
64
|
+
// applies brightness scaling then calls transport.write()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
strip.off(); // Turn off all pixels, () → void
|
|
68
|
+
// equivalent to fill(0, 0, 0, 0)
|
|
69
|
+
transport.close();
|