periph 1.4.0 → 1.5.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 +27 -0
- package/examples/adc_dac/ad5251/complete.js +66 -0
- package/examples/adc_dac/ad5251/demo.js +69 -0
- package/examples/adc_dac/ad5251/minimal.js +23 -0
- package/examples/adc_dac/ad5252/complete.js +66 -0
- package/examples/adc_dac/ad5252/demo.js +69 -0
- package/examples/adc_dac/ad5252/minimal.js +23 -0
- package/examples/adc_dac/ads7828/complete.js +46 -0
- package/examples/adc_dac/ads7828/demo.js +45 -0
- package/examples/adc_dac/ads7828/minimal.js +17 -0
- package/examples/adc_dac/cat5171/complete.js +52 -0
- package/examples/adc_dac/cat5171/demo.js +59 -0
- package/examples/adc_dac/cat5171/minimal.js +23 -0
- package/examples/adc_dac/cs43l22/complete.js +88 -0
- package/examples/adc_dac/cs43l22/demo.js +56 -0
- package/examples/adc_dac/cs43l22/minimal.js +20 -0
- package/examples/adc_dac/ds1881/complete.js +62 -0
- package/examples/adc_dac/ds1881/demo.js +61 -0
- package/examples/adc_dac/ds1881/minimal.js +25 -0
- package/examples/adc_dac/ds3502/complete.js +46 -0
- package/examples/adc_dac/ds3502/demo.js +54 -0
- package/examples/adc_dac/ds3502/minimal.js +28 -0
- package/examples/adc_dac/mcp3422/complete.js +48 -0
- package/examples/adc_dac/mcp3422/demo.js +49 -0
- package/examples/adc_dac/mcp3422/minimal.js +25 -0
- package/examples/display/sh1106/complete.js +101 -0
- package/examples/display/sh1106/demo.js +68 -0
- package/examples/display/sh1106/minimal.js +25 -0
- package/examples/display/ssd1305/complete.js +129 -0
- package/examples/display/ssd1305/demo.js +71 -0
- package/examples/display/ssd1305/minimal.js +25 -0
- package/examples/display/ssd1306/complete.js +115 -0
- package/examples/display/ssd1306/demo.js +70 -0
- package/examples/display/ssd1306/minimal.js +25 -0
- package/examples/environmental/am2315/complete.js +34 -0
- package/examples/environmental/am2315/demo.js +46 -0
- package/examples/environmental/am2315/minimal.js +21 -0
- package/examples/humidity/htu21df/complete.js +49 -0
- package/examples/humidity/htu21df/demo.js +50 -0
- package/examples/humidity/htu21df/minimal.js +22 -0
- package/examples/humidity/sht31/complete.js +53 -0
- package/examples/humidity/sht31/demo.js +53 -0
- package/examples/humidity/sht31/minimal.js +21 -0
- package/examples/humidity/si7021/complete.js +55 -0
- package/examples/humidity/si7021/demo.js +58 -0
- package/examples/humidity/si7021/minimal.js +21 -0
- package/examples/imu/bno055/complete.js +85 -0
- package/examples/imu/bno055/demo.js +70 -0
- package/examples/imu/bno055/minimal.js +20 -0
- package/examples/io_expander/pca6408a/complete.js +63 -0
- package/examples/io_expander/pca6408a/demo.js +58 -0
- package/examples/io_expander/pca6408a/minimal.js +20 -0
- package/examples/io_expander/pca6416a/complete.js +68 -0
- package/examples/io_expander/pca6416a/demo.js +60 -0
- package/examples/io_expander/pca6416a/minimal.js +20 -0
- package/examples/io_expander/pca9536/complete.js +63 -0
- package/examples/io_expander/pca9536/demo.js +57 -0
- package/examples/io_expander/pca9536/minimal.js +20 -0
- package/examples/io_expander/pca9539/complete.js +68 -0
- package/examples/io_expander/pca9539/demo.js +60 -0
- package/examples/io_expander/pca9539/minimal.js +20 -0
- package/examples/light/max44009/complete.js +56 -0
- package/examples/light/max44009/demo.js +49 -0
- package/examples/light/max44009/minimal.js +22 -0
- package/examples/memory/at24c02n/complete.js +39 -0
- package/examples/memory/at24c02n/demo.js +39 -0
- package/examples/memory/at24c02n/minimal.js +15 -0
- package/examples/memory/at24c64/complete.js +39 -0
- package/examples/memory/at24c64/demo.js +39 -0
- package/examples/memory/at24c64/minimal.js +15 -0
- package/examples/memory/cat24c512/complete.js +39 -0
- package/examples/memory/cat24c512/demo.js +39 -0
- package/examples/memory/cat24c512/minimal.js +15 -0
- package/examples/other/pca9541/complete.js +52 -0
- package/examples/other/pca9541/demo.js +61 -0
- package/examples/other/pca9541/minimal.js +23 -0
- package/examples/temperature/tmp102/complete.js +74 -0
- package/examples/temperature/tmp102/demo.js +71 -0
- package/examples/temperature/tmp102/minimal.js +20 -0
- package/examples/tof/vl6180x/complete.js +62 -0
- package/examples/tof/vl6180x/demo.js +77 -0
- package/examples/tof/vl6180x/minimal.js +23 -0
- package/package.json +1 -1
- package/src/chips/adc_dac/ad5251.js +345 -0
- package/src/chips/adc_dac/ad5252.js +345 -0
- package/src/chips/adc_dac/ads7828.js +225 -0
- package/src/chips/adc_dac/cat5171.js +213 -0
- package/src/chips/adc_dac/cs43l22.js +645 -0
- package/src/chips/adc_dac/ds1881.js +357 -0
- package/src/chips/adc_dac/ds3502.js +241 -0
- package/src/chips/adc_dac/mcp3422.js +312 -0
- package/src/chips/display/mono_oled.js +182 -0
- package/src/chips/display/sh1106.js +496 -0
- package/src/chips/display/ssd1305.js +655 -0
- package/src/chips/display/ssd1306.js +458 -0
- package/src/chips/environmental/am2315.js +237 -0
- package/src/chips/humidity/htu21df.js +222 -0
- package/src/chips/humidity/sht31.js +284 -0
- package/src/chips/humidity/si7021.js +282 -0
- package/src/chips/imu/bno055.js +496 -0
- package/src/chips/io_expander/pca6408a.js +407 -0
- package/src/chips/io_expander/pca6416a.js +455 -0
- package/src/chips/io_expander/pca9536.js +398 -0
- package/src/chips/io_expander/pca9539.js +457 -0
- package/src/chips/light/max44009.js +336 -0
- package/src/chips/memory/at24c02n.js +220 -0
- package/src/chips/memory/at24c64.js +220 -0
- package/src/chips/memory/cat24c512.js +221 -0
- package/src/chips/other/pca9541.js +315 -0
- package/src/chips/temperature/tmp102.js +361 -0
- package/src/chips/tof/vl6180x.js +580 -0
- package/src/discovery/registry.js +65 -13
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Si7021Full } = require('../../../src/chips/humidity/si7021');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
|
|
8
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
9
|
+
|
|
10
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
|
|
11
|
+
const sensor = new Si7021Full(connection); // Create Si7021 driver, (connection) → void
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number °C, number %RH]
|
|
15
|
+
// RH without hold master, temperature of the same conversion
|
|
16
|
+
console.log(t, rh);
|
|
17
|
+
console.log(await sensor.readHumidity()); // Read humidity only, () → number %RH
|
|
18
|
+
console.log(await sensor.readTemperature()); // Read temperature (new conversion), () → number °C
|
|
19
|
+
console.log(await sensor.readTemperaturePrevious()); // Temperature of the last RH measurement, () → number °C
|
|
20
|
+
// command 0xE0, no new conversion
|
|
21
|
+
console.log(await sensor.readRawHumidity(), await sensor.readRawTemperature()); // Read raw codes, () → number ; () → number
|
|
22
|
+
// 16-bit codes, status bits included
|
|
23
|
+
|
|
24
|
+
await sensor.setResolution(11, 11); // Set resolution, (rhBits, temperatureBits) → void
|
|
25
|
+
// (12, 14) / (8, 12) / (10, 13) / (11, 11)
|
|
26
|
+
console.log(await sensor.getResolution()); // Get resolution, () → [number, number]
|
|
27
|
+
console.log(await sensor.conversionTime()); // Typical conversion time, () → number s
|
|
28
|
+
await sensor.setResolution(12, 14);
|
|
29
|
+
|
|
30
|
+
await sensor.setHeater(true); // Switch the heater, (on) → void
|
|
31
|
+
// about 3.1 mA; readings drift while on
|
|
32
|
+
console.log(await sensor.isHeaterOn()); // Heater state, () → boolean
|
|
33
|
+
await sensor.setHeater(false);
|
|
34
|
+
console.log(await sensor.vddLow()); // Supply low flag, () → boolean
|
|
35
|
+
// true when VDD is below about 1.9 V
|
|
36
|
+
const reg = await sensor.readUserRegister(); // Read USER_REG_1, () → number
|
|
37
|
+
// 0x3A after reset
|
|
38
|
+
await sensor.writeUserRegister(reg); // Write USER_REG_1, (value) → void
|
|
39
|
+
// reserved bits are preserved
|
|
40
|
+
sensor.setClockStretching(false); // Use hold-master commands, (on) → void
|
|
41
|
+
// needs bus-layer support; off polls the NACKed read header
|
|
42
|
+
|
|
43
|
+
console.log((await sensor.serialNumber()).toString(16)); // Electronic serial number, () → bigint
|
|
44
|
+
console.log((await sensor.deviceType()).toString(16)); // Device type, () → number
|
|
45
|
+
// 0x15 = Si7021
|
|
46
|
+
console.log((await sensor.firmwareRevision()).toString(16)); // Firmware revision, () → number
|
|
47
|
+
// 0xFF = firmware 1.0
|
|
48
|
+
console.log(await sensor.dewPoint()); // Compute dew point, () → number °C
|
|
49
|
+
// one measurement, Magnus formula
|
|
50
|
+
await sensor.softReset(); // Software reset, () → void
|
|
51
|
+
// 0xFE, waits 15 ms
|
|
52
|
+
connection.close();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Si7021Full } = require('../../../src/chips/humidity/si7021');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
|
|
8
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
9
|
+
|
|
10
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
|
|
11
|
+
const sensor = new Si7021Full(connection); // Create Si7021 driver, (connection) → void
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
await sensor.softReset(); // Software reset, () → void
|
|
15
|
+
console.log('device type 0x' + (await sensor.deviceType()).toString(16)); // Device type, () → number
|
|
16
|
+
console.log('serial number 0x' + (await sensor.serialNumber()).toString(16)); // Electronic serial number, () → bigint
|
|
17
|
+
console.log('firmware 0x' + (await sensor.firmwareRevision()).toString(16)); // Firmware revision, () → number
|
|
18
|
+
console.log('VDD low:', await sensor.vddLow()); // Supply low flag, () → boolean
|
|
19
|
+
|
|
20
|
+
// --- Condensation-aware hygrometer: 15 readings, 2 s apart ---
|
|
21
|
+
for (let i = 0; i < 15; i++) {
|
|
22
|
+
const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number, number]
|
|
23
|
+
console.log('%s C %s %%RH dew point %s C', t.toFixed(2), rh.toFixed(1), (await sensor.dewPoint()).toFixed(2)); // Compute dew point, () → number °C
|
|
24
|
+
await sleep(2000);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// --- Value accessors and raw codes ---
|
|
28
|
+
console.log('RH', await sensor.readHumidity(), 'T', await sensor.readTemperature()); // () → number ; () → number
|
|
29
|
+
console.log('T of last RH', await sensor.readTemperaturePrevious()); // () → number
|
|
30
|
+
console.log('raw', await sensor.readRawHumidity(), await sensor.readRawTemperature()); // () → number ; () → number
|
|
31
|
+
sensor.setClockStretching(false); // Use hold-master commands, (on) → void
|
|
32
|
+
const reg = await sensor.readUserRegister(); // Read USER_REG_1, () → number
|
|
33
|
+
await sensor.writeUserRegister(reg); // Write USER_REG_1, (value) → void
|
|
34
|
+
|
|
35
|
+
// --- Resolution comparison ---
|
|
36
|
+
for (const [rhBits, tBits] of [[12, 14], [10, 13], [11, 11], [8, 12]]) {
|
|
37
|
+
await sensor.setResolution(rhBits, tBits); // Set resolution, (rhBits, temperatureBits) → void
|
|
38
|
+
const start = process.hrtime.bigint();
|
|
39
|
+
const [t, rh] = await sensor.read();
|
|
40
|
+
const ms = Number(process.hrtime.bigint() - start) / 1e6;
|
|
41
|
+
console.log('%d/%d bit: %s C %s %%RH, %s ms (typ. %s ms), resolution %j', rhBits, tBits, t.toFixed(2), rh.toFixed(1),
|
|
42
|
+
ms.toFixed(1), ((await sensor.conversionTime()) * 1000).toFixed(1), await sensor.getResolution()); // () → number s ; () → [number, number]
|
|
43
|
+
}
|
|
44
|
+
await sensor.setResolution(12, 14);
|
|
45
|
+
|
|
46
|
+
// --- Heater test: 10 s ---
|
|
47
|
+
const rhBefore = await sensor.readHumidity();
|
|
48
|
+
const tBefore = await sensor.readTemperature();
|
|
49
|
+
await sensor.setHeater(true); // Switch the heater, (on) → void
|
|
50
|
+
await sleep(10000);
|
|
51
|
+
console.log('heater on:', await sensor.isHeaterOn()); // Heater state, () → boolean
|
|
52
|
+
console.log('RH drop %s %%RH, temperature rise %s K', (rhBefore - await sensor.readHumidity()).toFixed(1),
|
|
53
|
+
((await sensor.readTemperature()) - tBefore).toFixed(2));
|
|
54
|
+
await sensor.setHeater(false);
|
|
55
|
+
connection.close();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Si7021Minimal } = require('../../../src/chips/humidity/si7021');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
|
|
8
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
9
|
+
|
|
10
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
|
|
11
|
+
const sensor = new Si7021Minimal(connection); // Create Si7021 driver, (connection) → void
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
while (true) {
|
|
15
|
+
const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number °C, number %RH]
|
|
16
|
+
console.log('%s C, %s %%RH', t.toFixed(2), rh.toFixed(1));
|
|
17
|
+
await sleep(2000);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { BNO055Full, BNO055Mode, BNO055Source } = require('../../../src/chips/imu/bno055');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x29', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const imu = new BNO055Full(connection); // Create BNO055 driver, (connection) → void
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
await imu.init(); // Wait for boot and NDOF, () → void
|
|
14
|
+
// rejects if CHIP_ID is not 0xA0
|
|
15
|
+
|
|
16
|
+
const [h, r, p] = await imu.euler(); // Read fused Euler angles, () → [number, number, number] rad
|
|
17
|
+
// burst read of 0x1A…0x1F, raw / 900
|
|
18
|
+
const [w, x, y, z] = await imu.quaternion(); // Read orientation quaternion, () → [number, number, number, number]
|
|
19
|
+
// raw / 16384
|
|
20
|
+
const lin = await imu.linearAcceleration(); // Read gravity-free acceleration, () → [number, number, number] m/s²
|
|
21
|
+
// raw / 100
|
|
22
|
+
const grav = await imu.gravity(); // Read gravity vector, () → [number, number, number] m/s²
|
|
23
|
+
// raw / 100
|
|
24
|
+
const acc = await imu.acceleration(); // Read raw accelerometer, () → [number, number, number] m/s²
|
|
25
|
+
// raw / 100
|
|
26
|
+
const mag = await imu.magneticField(); // Read raw magnetometer, () → [number, number, number] µT
|
|
27
|
+
// raw / 16
|
|
28
|
+
const gyr = await imu.angularRate(); // Read raw gyroscope, () → [number, number, number] rad/s
|
|
29
|
+
// raw / 900
|
|
30
|
+
const t = await imu.temperature(); // Read temperature, () → number °C
|
|
31
|
+
// signed 8 bit, 1 LSB = 1 °C
|
|
32
|
+
const [sys, gy, ac, mg] = await imu.calibrationStatus(); // Read calibration status, () → [number, number, number, number]
|
|
33
|
+
// each 0 (uncalibrated) … 3 (calibrated)
|
|
34
|
+
const done = await imu.isCalibrated(); // Check full calibration, () → boolean
|
|
35
|
+
// true when all four values are 3
|
|
36
|
+
console.log(h, r, p, w, x, y, z, lin, grav, acc, mag, gyr, t, sys, gy, ac, mg, done);
|
|
37
|
+
|
|
38
|
+
const mode = await imu.getMode(); // Read operation mode, () → number
|
|
39
|
+
// 0x0C = NDOF
|
|
40
|
+
await imu.setMode(BNO055Mode.IMU); // Select operation mode, (mode) → void
|
|
41
|
+
// via CONFIGMODE, 19 ms + 7 ms waits
|
|
42
|
+
await imu.setMode(BNO055Mode.NDOF); // Select operation mode, (mode) → void
|
|
43
|
+
// back to 9-DOF fusion
|
|
44
|
+
|
|
45
|
+
const blob = await imu.getCalibration(); // Read offsets and radii, () → Buffer
|
|
46
|
+
// 22 bytes, 0x55…0x6A, read in CONFIGMODE
|
|
47
|
+
await imu.setCalibration(blob); // Restore offsets and radii, (data) → void
|
|
48
|
+
// 22 bytes, written in CONFIGMODE
|
|
49
|
+
await imu.setAxisRemap(0x24, 0x00); // Remap axes, (config, sign) → void
|
|
50
|
+
// 0x24/0x00 = identity placement
|
|
51
|
+
await imu.setPowerMode('normal'); // Select power mode, (mode) → void
|
|
52
|
+
// 'normal', 'low_power' or 'suspend'
|
|
53
|
+
await imu.setTemperatureSource('accelerometer'); // Select temperature sensor, (source) → void
|
|
54
|
+
// 'accelerometer' or 'gyroscope'
|
|
55
|
+
await imu.useExternalCrystal(false); // Select clock source, (on) → void
|
|
56
|
+
// internal oscillator unless a crystal is fitted
|
|
57
|
+
|
|
58
|
+
const result = await imu.selfTest(); // Run self-test, () → {acc, mag, gyr, mcu}
|
|
59
|
+
// booleans, true = passed
|
|
60
|
+
const [status, error] = await imu.systemStatus(); // Read system status, () → [number, number]
|
|
61
|
+
// SYS_STATUS 5 = fusion running, SYS_ERR 0 = none
|
|
62
|
+
const info = await imu.revisions(); // Read revisions, () → object
|
|
63
|
+
// chip/acc/mag/gyr IDs, SW revision, bootloader
|
|
64
|
+
const uid = await imu.uniqueId(); // Read unique ID, () → Buffer
|
|
65
|
+
// 16 bytes from page 1
|
|
66
|
+
console.log(mode, result, status, error, info, uid.toString('hex'));
|
|
67
|
+
|
|
68
|
+
await imu.enableInterrupt(BNO055Source.ACC_BSX_DRDY); // Enable interrupt source, (source) → void
|
|
69
|
+
// INT_EN and INT_MSK on page 1
|
|
70
|
+
const ints = await imu.pollInterrupt(); // Poll interrupt status, () → number
|
|
71
|
+
// INT_STA, cleared on read
|
|
72
|
+
await imu.onInterrupt((s) => console.log('INT status 0x' + s.toString(16))); // Subscribe to interrupts, (callback, intPin?) → void
|
|
73
|
+
// uses an INT pin or a 100 ms polling timer
|
|
74
|
+
await new Promise(res => setTimeout(res, 500));
|
|
75
|
+
await imu.offInterrupt(); // Unsubscribe, () → void
|
|
76
|
+
// stops the callback
|
|
77
|
+
await imu.disableInterrupt(BNO055Source.ACC_BSX_DRDY); // Disable interrupt source, (source) → void
|
|
78
|
+
// clears the INT_EN and INT_MSK bits
|
|
79
|
+
console.log(ints);
|
|
80
|
+
|
|
81
|
+
await imu.reset(); // Reset the chip, () → void
|
|
82
|
+
// RST_SYS, waits 650 ms, re-enters NDOF
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { BNO055Full, BNO055Mode, BNO055Source } = require('../../../src/chips/imu/bno055');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x29', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const imu = new BNO055Full(connection); // Create BNO055 driver, (connection) → void
|
|
11
|
+
const sleep = (ms) => new Promise(res => setTimeout(res, ms));
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
await imu.init(); // Wait for boot and NDOF, () → void
|
|
15
|
+
|
|
16
|
+
// --- Identify the part ---
|
|
17
|
+
const info = await imu.revisions(); // Read revisions, () → object
|
|
18
|
+
console.log('chip 0x%s, SW %d.%d, bootloader %d', info.chipId.toString(16), info.swRevision >> 8, info.swRevision & 0xFF, info.bootloader);
|
|
19
|
+
console.log('unique id', (await imu.uniqueId()).toString('hex')); // Read unique ID, () → Buffer
|
|
20
|
+
|
|
21
|
+
// --- Orientation monitor until fully calibrated (move the board in a figure eight) ---
|
|
22
|
+
for (;;) {
|
|
23
|
+
const [h, r, p] = await imu.euler(); // Read fused Euler angles, () → [number, number, number] rad
|
|
24
|
+
const [w, x, y, z] = await imu.quaternion(); // Read orientation quaternion, () → [number, number, number, number]
|
|
25
|
+
const lin = await imu.linearAcceleration(); // Read gravity-free acceleration, () → [number, number, number] m/s²
|
|
26
|
+
const grav = await imu.gravity(); // Read gravity vector, () → [number, number, number] m/s²
|
|
27
|
+
const cal = await imu.calibrationStatus(); // Read calibration status, () → [number, number, number, number]
|
|
28
|
+
console.log('euler %s %s %s quat %s %s %s %s lin %s grav %s cal %s',
|
|
29
|
+
h.toFixed(2), r.toFixed(2), p.toFixed(2), w.toFixed(3), x.toFixed(3), y.toFixed(3), z.toFixed(3),
|
|
30
|
+
lin.map(v => v.toFixed(2)).join(' '), grav.map(v => v.toFixed(2)).join(' '), cal.join(''));
|
|
31
|
+
if (await imu.isCalibrated()) break; // Check full calibration, () → boolean
|
|
32
|
+
await sleep(100);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// --- Save the calibration, relative mode for 5 s, restore it ---
|
|
36
|
+
const blob = await imu.getCalibration(); // Read offsets and radii, () → Buffer
|
|
37
|
+
console.log('calibration', blob.toString('hex'));
|
|
38
|
+
await imu.setMode(BNO055Mode.IMU); // Select operation mode, (mode) → void
|
|
39
|
+
console.log('mode', await imu.getMode()); // Read operation mode, () → number
|
|
40
|
+
await sleep(5000);
|
|
41
|
+
await imu.setMode(BNO055Mode.NDOF); // Select operation mode, (mode) → void
|
|
42
|
+
await imu.setCalibration(blob); // Restore offsets and radii, (data) → void
|
|
43
|
+
|
|
44
|
+
// --- Self-test, temperature and configuration ---
|
|
45
|
+
console.log('self-test', await imu.selfTest()); // Run self-test, () → object
|
|
46
|
+
console.log('temperature', await imu.temperature(), 'C'); // Read temperature, () → number °C
|
|
47
|
+
await imu.setTemperatureSource('gyroscope'); // Select temperature sensor, (source) → void
|
|
48
|
+
await imu.setAxisRemap(0x24, 0x00); // Remap axes, (config, sign) → void
|
|
49
|
+
await imu.setPowerMode('normal'); // Select power mode, (mode) → void
|
|
50
|
+
await imu.useExternalCrystal(false); // Select clock source, (on) → void
|
|
51
|
+
|
|
52
|
+
// --- Raw sensors in AMG mode ---
|
|
53
|
+
await imu.setMode(BNO055Mode.AMG); // Select operation mode, (mode) → void
|
|
54
|
+
console.log('acc', await imu.acceleration()); // Read raw accelerometer, () → [number, number, number] m/s²
|
|
55
|
+
console.log('mag', await imu.magneticField()); // Read raw magnetometer, () → [number, number, number] µT
|
|
56
|
+
console.log('gyr', await imu.angularRate()); // Read raw gyroscope, () → [number, number, number] rad/s
|
|
57
|
+
|
|
58
|
+
// --- Interrupts: accelerometer/fusion data ready ---
|
|
59
|
+
await imu.setMode(BNO055Mode.NDOF); // Select operation mode, (mode) → void
|
|
60
|
+
await imu.enableInterrupt(BNO055Source.ACC_BSX_DRDY); // Enable interrupt source, (source) → void
|
|
61
|
+
await imu.onInterrupt((s) => console.log('INT 0x' + s.toString(16))); // Subscribe to interrupts, (callback, intPin?) → void
|
|
62
|
+
await sleep(500);
|
|
63
|
+
await imu.offInterrupt(); // Unsubscribe, () → void
|
|
64
|
+
await imu.disableInterrupt(BNO055Source.ACC_BSX_DRDY); // Disable interrupt source, (source) → void
|
|
65
|
+
console.log('interrupt status', await imu.pollInterrupt()); // Poll interrupt status, () → number
|
|
66
|
+
console.log('system status', await imu.systemStatus()); // Read system status, () → [number, number]
|
|
67
|
+
await imu.reset(); // Reset the chip, () → void
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { BNO055Minimal } = require('../../../src/chips/imu/bno055');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x29', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const imu = new BNO055Minimal(connection); // Create BNO055 driver, (connection) → void
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
while (true) {
|
|
14
|
+
const [h, r, p] = await imu.euler(); // Read fused Euler angles, () → [number, number, number] rad
|
|
15
|
+
console.log('heading %s rad, roll %s, pitch %s', h.toFixed(2), r.toFixed(2), p.toFixed(2));
|
|
16
|
+
await new Promise(res => setTimeout(res, 100));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
|
+
const { Pca6408aFull } = require('periph/src/chips/io_expander/pca6408a');
|
|
4
|
+
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
7
|
+
const chip = new Pca6408aFull(connection); // Create PCA6408A full driver, (connection, addr=0x20)
|
|
8
|
+
// writes the reset values; all pins start as inputs
|
|
9
|
+
await chip.ready;
|
|
10
|
+
|
|
11
|
+
// --- Pin proxy — async API ---
|
|
12
|
+
const p0 = chip.pin(0, 'out'); // Get full pin proxy, (n, direction='in') → FullPin
|
|
13
|
+
// P0 as output; P7 is a normal bidirectional pin
|
|
14
|
+
console.log('direction:', p0.direction); // direction property, () → string 'in'|'out'
|
|
15
|
+
await p0.write(0); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
// clears the OUTPUT bit via the shadow
|
|
17
|
+
const v = await p0.read(); // Read pin, () → Promise<int 0|1>
|
|
18
|
+
// reads INPUT (actual pin level)
|
|
19
|
+
console.log('P0 level:', v);
|
|
20
|
+
await p0.setDirection('in'); // Set direction, (direction) → Promise<void>
|
|
21
|
+
// writes CONFIG; OUTPUT is latched first for outputs
|
|
22
|
+
|
|
23
|
+
// --- Port-level operations ---
|
|
24
|
+
await chip.writePort(0, 0b00001111); // Write all 8 pins, (port=0, mask) → Promise<void>
|
|
25
|
+
// whole-port OUTPUT write
|
|
26
|
+
const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
|
|
27
|
+
// INPUT register
|
|
28
|
+
console.log('PORT=0x' + port.toString(16).padStart(2, '0'));
|
|
29
|
+
|
|
30
|
+
// --- Configuration ---
|
|
31
|
+
await chip.configurePolarity(0x00); // Configure input polarity, (mask) → Promise<void>
|
|
32
|
+
// 1 bits invert the INPUT read
|
|
33
|
+
await chip.setDirection(0xF0); // Set whole-port direction, (mask) → Promise<void>
|
|
34
|
+
// P0–P3 outputs, P4–P7 inputs; OUTPUT is written first
|
|
35
|
+
const dir = chip.getDirection(); // Read direction shadow, () → int
|
|
36
|
+
// 1 = input, 0 = output
|
|
37
|
+
console.log('CONFIG=0x' + dir.toString(16).padStart(2, '0'));
|
|
38
|
+
const config = await chip.readRegister(0x03); // Read raw register, (reg) → Promise<int>
|
|
39
|
+
// escape hatch for debugging
|
|
40
|
+
console.log('CONFIG=0x' + config.toString(16).padStart(2, '0'));
|
|
41
|
+
await chip.writeRegister(0x02, 0x10); // Write raw register, (reg, value) → Promise<void>
|
|
42
|
+
// POLARITY: invert P4
|
|
43
|
+
|
|
44
|
+
// --- Interrupts ---
|
|
45
|
+
await chip.onInterrupt((changed) => { // Subscribe to INT, (callback, options) → Promise<void>
|
|
46
|
+
console.log('interrupt, changed=0x' + changed.toString(16));// INT line if wired, else 5 ms polling
|
|
47
|
+
});
|
|
48
|
+
const p4 = chip.pin(4, 'in');
|
|
49
|
+
await p4.watch((pin) => console.log('P4 changed'), 'change'); // Watch a pin, (handler, trigger='change') → Promise<void>
|
|
50
|
+
// edge filtered in software; all pins share INT
|
|
51
|
+
const changed = await chip.pollInterrupt(); // Read INPUT, get changed mask, () → Promise<int>
|
|
52
|
+
// XOR with the last INPUT value
|
|
53
|
+
console.log('changed=0x' + changed.toString(16));
|
|
54
|
+
await p4.unwatch(); // Stop watching, () → Promise<void>
|
|
55
|
+
// drops the handler
|
|
56
|
+
await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
|
|
57
|
+
// stops delivery
|
|
58
|
+
await chip.reset(); // Restore reset values, () → Promise<void>
|
|
59
|
+
// OUTPUT=0xFF, POLARITY=0x00, CONFIG=0xFF
|
|
60
|
+
await connection.close();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
main();
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* PCA6408A demo — LED scanner with interrupt-driven button input.
|
|
4
|
+
*
|
|
5
|
+
* Hardware:
|
|
6
|
+
* P0–P3: four active-low LEDs (anode → VDD(P) via 220 Ω, cathode → P; 0 lights the LED)
|
|
7
|
+
* P4–P7: four push buttons (pin → GND when pressed) with external pull-ups
|
|
8
|
+
* (the chip has no internal pull-ups)
|
|
9
|
+
*
|
|
10
|
+
* Runs a scanner pattern on the LEDs. Button presses raise an interrupt; the
|
|
11
|
+
* callback prints the changed-pin mask and the port state and holds the LED
|
|
12
|
+
* matching the pressed button (P4→P0, …) for one second.
|
|
13
|
+
*/
|
|
14
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
15
|
+
const { Pca6408aFull } = require('periph/src/chips/io_expander/pca6408a');
|
|
16
|
+
|
|
17
|
+
async function main() {
|
|
18
|
+
// --- Initialise bus and driver ---
|
|
19
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
20
|
+
const chip = new Pca6408aFull(connection); // Create PCA6408A full driver, (connection, addr=0x20)
|
|
21
|
+
await chip.ready;
|
|
22
|
+
|
|
23
|
+
// --- Configure LEDs and buttons ---
|
|
24
|
+
// P0–P3 are outputs (all LEDs off = 1); P4–P7 stay inputs, a pressed button reads 0.
|
|
25
|
+
await chip.writePort(0, 0xFF); // Turn all LEDs off, (port=0, mask) → Promise<void>
|
|
26
|
+
await chip.setDirection(0xF0); // P0–P3 outputs, P4–P7 inputs, (mask) → Promise<void>
|
|
27
|
+
|
|
28
|
+
// --- Subscribe to input changes ---
|
|
29
|
+
// Any button edge raises INT (or the 5 ms poll sees it); the callback reports the changed
|
|
30
|
+
// mask, then holds the matching LED.
|
|
31
|
+
let holdUntil = 0;
|
|
32
|
+
let holdMask = 0x0F;
|
|
33
|
+
await chip.onInterrupt(async (changed) => { // Subscribe to INT, (callback, options) → Promise<void>
|
|
34
|
+
const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
|
|
35
|
+
console.log('button changed=0x' + changed.toString(16) + ' port=0x' + port.toString(16));
|
|
36
|
+
holdMask = ~((changed >> 4) & 0x0F) & 0x0F;
|
|
37
|
+
holdUntil = Date.now() + 1000;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// --- Scanner loop ---
|
|
41
|
+
let step = 0;
|
|
42
|
+
console.log('Running — press buttons P4–P7 to hold the matching LED');
|
|
43
|
+
const timer = setInterval(async () => {
|
|
44
|
+
const leds = Date.now() < holdUntil ? holdMask : (~(1 << (step % 4)) & 0x0F);
|
|
45
|
+
await chip.writePort(0, 0xF0 | leds); // Write all 8 pins, (port=0, mask) → Promise<void>
|
|
46
|
+
const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
|
|
47
|
+
console.log('pos=' + (step % 4) + ' port=0x' + port.toString(16).padStart(2, '0'));
|
|
48
|
+
step++;
|
|
49
|
+
if (step >= 40) {
|
|
50
|
+
clearInterval(timer);
|
|
51
|
+
await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
|
|
52
|
+
await chip.writePort(0, 0xFF); // Turn all LEDs off, (port=0, mask) → Promise<void>
|
|
53
|
+
await connection.close();
|
|
54
|
+
}
|
|
55
|
+
}, 100);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
main();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
|
+
const { Pca6408aMinimal } = require('periph/src/chips/io_expander/pca6408a');
|
|
4
|
+
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
7
|
+
const chip = new Pca6408aMinimal(connection); // Create PCA6408A driver, (connection, addr=0x20)
|
|
8
|
+
|
|
9
|
+
const p0 = chip.pin(0, 'in'); // Get pin proxy, (n, direction='in') → Pin
|
|
10
|
+
const p7 = chip.pin(7, 'out'); // Get pin proxy, (n, direction='out') → Pin
|
|
11
|
+
|
|
12
|
+
setInterval(async () => {
|
|
13
|
+
const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
|
|
14
|
+
const level = await p0.read(); // Read pin, () → Promise<int 0|1>
|
|
15
|
+
await p7.write(level); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
console.log('PORT=0x' + port.toString(16).padStart(2, '0') + ' P0=' + level);
|
|
17
|
+
}, 200);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main();
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
|
+
const { Pca6416aFull } = require('periph/src/chips/io_expander/pca6416a');
|
|
4
|
+
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
7
|
+
const chip = new Pca6416aFull(connection); // Create PCA6416A full driver, (connection, addr=0x20)
|
|
8
|
+
// writes the reset values; all 16 pins start as inputs
|
|
9
|
+
await chip.ready;
|
|
10
|
+
|
|
11
|
+
// --- Pin proxy — async API ---
|
|
12
|
+
const p0 = chip.pin(0, 'out'); // Get full pin proxy, (n, direction='in') → FullPin
|
|
13
|
+
// P0_0 as output; every pin is bidirectional
|
|
14
|
+
console.log('direction:', p0.direction); // direction property, () → string 'in'|'out'
|
|
15
|
+
await p0.write(0); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
// clears the OUTPUT bit via the shadow
|
|
17
|
+
const v = await p0.read(); // Read pin, () → Promise<int 0|1>
|
|
18
|
+
// reads INPUT (actual pin level)
|
|
19
|
+
console.log('P0 level:', v);
|
|
20
|
+
await p0.setDirection('in'); // Set direction, (direction) → Promise<void>
|
|
21
|
+
// writes CONFIG; OUTPUT is latched first for outputs
|
|
22
|
+
|
|
23
|
+
// --- Port-level operations ---
|
|
24
|
+
await chip.writePort(0, 0b00001111); // Write port 0, (port, mask) → Promise<void>
|
|
25
|
+
// single-port OUTPUT write
|
|
26
|
+
await chip.writeAll(0xA50F); // Write all 16 pins, (value) → Promise<void>
|
|
27
|
+
// one two-byte write at OUTPUT0 (pair toggling)
|
|
28
|
+
const port = await chip.readPort(1); // Read port 1, (port) → Promise<int bitmask>
|
|
29
|
+
// INPUT1 register
|
|
30
|
+
console.log('PORT1=0x' + port.toString(16).padStart(2, '0'));
|
|
31
|
+
const all = await chip.readAll(); // Read all 16 pins, () → Promise<int>
|
|
32
|
+
// one two-byte read at INPUT0
|
|
33
|
+
console.log('ALL=0x' + all.toString(16).padStart(4, '0'));
|
|
34
|
+
|
|
35
|
+
// --- Configuration ---
|
|
36
|
+
await chip.configurePolarity(0x0000); // Configure input polarity, (mask) → Promise<void>
|
|
37
|
+
// 1 bits invert the INPUT read
|
|
38
|
+
await chip.setDirection(0xFF00); // Set whole-device direction, (mask) → Promise<void>
|
|
39
|
+
// port 0 outputs, port 1 inputs; OUTPUT is written first
|
|
40
|
+
const dir = chip.getDirection(); // Read direction shadow, () → int
|
|
41
|
+
// 1 = input, 0 = output
|
|
42
|
+
console.log('CONFIG=0x' + dir.toString(16).padStart(4, '0'));
|
|
43
|
+
const config = await chip.readRegister(0x07); // Read raw register, (reg) → Promise<int>
|
|
44
|
+
// escape hatch for debugging
|
|
45
|
+
console.log('CONFIG1=0x' + config.toString(16).padStart(2, '0'));
|
|
46
|
+
await chip.writeRegister(0x05, 0x10); // Write raw register, (reg, value) → Promise<void>
|
|
47
|
+
// POLARITY1: invert P1_4
|
|
48
|
+
|
|
49
|
+
// --- Interrupts ---
|
|
50
|
+
await chip.onInterrupt((changed) => { // Subscribe to INT, (callback, options) → Promise<void>
|
|
51
|
+
console.log('interrupt, changed=0x' + changed.toString(16));// INT line if wired, else 5 ms polling
|
|
52
|
+
});
|
|
53
|
+
const p12 = chip.pin(12, 'in');
|
|
54
|
+
await p12.watch((pin) => console.log('P1_4 changed'), 'change');// Watch a pin, (handler, trigger='change') → Promise<void>
|
|
55
|
+
// edge filtered in software; all pins share INT
|
|
56
|
+
const changed = await chip.pollInterrupt(); // Read INPUT0/1, get changed mask, () → Promise<int>
|
|
57
|
+
// XOR with the last INPUT values
|
|
58
|
+
console.log('changed=0x' + changed.toString(16));
|
|
59
|
+
await p12.unwatch(); // Stop watching, () → Promise<void>
|
|
60
|
+
// drops the handler
|
|
61
|
+
await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
|
|
62
|
+
// stops delivery
|
|
63
|
+
await chip.reset(); // Restore reset values, () → Promise<void>
|
|
64
|
+
// OUTPUT=0xFF, POLARITY=0x00, CONFIG=0xFF (both ports)
|
|
65
|
+
await connection.close();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
main();
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* PCA6416A demo — LED scanner with interrupt-driven button input.
|
|
4
|
+
*
|
|
5
|
+
* Hardware:
|
|
6
|
+
* P0_0–P0_7: eight active-low LEDs (anode → VDD(P) via 220 Ω, cathode → P; 0 lights the LED)
|
|
7
|
+
* P1_0–P1_7: eight push buttons (pin → GND when pressed) with external pull-ups
|
|
8
|
+
* (the chip has no internal pull-ups)
|
|
9
|
+
*
|
|
10
|
+
* Runs a scanner pattern on the LEDs. Button presses raise an interrupt; the
|
|
11
|
+
* callback prints the 16-bit changed-pin mask and the pin state and holds the
|
|
12
|
+
* LED matching the pressed button (P1_0→P0_0, …) for one second.
|
|
13
|
+
*/
|
|
14
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
15
|
+
const { Pca6416aFull } = require('periph/src/chips/io_expander/pca6416a');
|
|
16
|
+
|
|
17
|
+
async function main() {
|
|
18
|
+
// --- Initialise bus and driver ---
|
|
19
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
20
|
+
const chip = new Pca6416aFull(connection); // Create PCA6416A full driver, (connection, addr=0x20)
|
|
21
|
+
await chip.ready;
|
|
22
|
+
|
|
23
|
+
// --- Configure LEDs and buttons ---
|
|
24
|
+
// Port 0 holds the LED outputs (all LEDs off = 1); port 1 stays input, a pressed button
|
|
25
|
+
// reads 0 — polarity inversion makes it read 1.
|
|
26
|
+
await chip.writeAll(0xFFFF); // Turn all LEDs off, (value) → Promise<void>
|
|
27
|
+
await chip.setDirection(0xFF00); // Port 0 outputs, port 1 inputs, (mask) → Promise<void>
|
|
28
|
+
await chip.configurePolarity(0xFF00); // Invert port 1 inputs, (mask) → Promise<void>
|
|
29
|
+
|
|
30
|
+
// --- Subscribe to input changes ---
|
|
31
|
+
// Any button edge raises INT (or the 5 ms poll sees it); the callback reports the changed
|
|
32
|
+
// mask, then holds the matching LED.
|
|
33
|
+
let holdUntil = 0;
|
|
34
|
+
let holdMask = 0xFF;
|
|
35
|
+
await chip.onInterrupt(async (changed) => { // Subscribe to INT, (callback, options) → Promise<void>
|
|
36
|
+
const all = await chip.readAll(); // Read all 16 pins, () → Promise<int>
|
|
37
|
+
console.log('button changed=0x' + changed.toString(16) + ' all=0x' + all.toString(16));
|
|
38
|
+
holdMask = ~((changed >> 8) & 0xFF) & 0xFF;
|
|
39
|
+
holdUntil = Date.now() + 1000;
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// --- Scanner loop ---
|
|
43
|
+
let step = 0;
|
|
44
|
+
console.log('Running — press buttons P1_0–P1_7 to hold the matching LED');
|
|
45
|
+
const timer = setInterval(async () => {
|
|
46
|
+
const leds = Date.now() < holdUntil ? holdMask : (~(1 << (step % 8)) & 0xFF);
|
|
47
|
+
await chip.writeAll(0xFF00 | leds); // Write all 16 pins, (value) → Promise<void>
|
|
48
|
+
const all = await chip.readAll(); // Read all 16 pins, () → Promise<int>
|
|
49
|
+
console.log('pos=' + (step % 8) + ' all=0x' + all.toString(16).padStart(4, '0'));
|
|
50
|
+
step++;
|
|
51
|
+
if (step >= 80) {
|
|
52
|
+
clearInterval(timer);
|
|
53
|
+
await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
|
|
54
|
+
await chip.writeAll(0xFFFF); // Turn all LEDs off, (value) → Promise<void>
|
|
55
|
+
await connection.close();
|
|
56
|
+
}
|
|
57
|
+
}, 100);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
main();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
|
+
const { Pca6416aMinimal } = require('periph/src/chips/io_expander/pca6416a');
|
|
4
|
+
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
7
|
+
const chip = new Pca6416aMinimal(connection); // Create PCA6416A driver, (connection, addr=0x20)
|
|
8
|
+
|
|
9
|
+
const p0 = chip.pin(0, 'in'); // Get pin proxy P0_0, (n, direction='in') → Pin
|
|
10
|
+
const p15 = chip.pin(15, 'out'); // Get pin proxy P1_7, (n, direction='out') → Pin
|
|
11
|
+
|
|
12
|
+
setInterval(async () => {
|
|
13
|
+
const port = await chip.readPort(1); // Read the 8 pins of port 1, (port) → Promise<int bitmask>
|
|
14
|
+
const level = await p0.read(); // Read pin, () → Promise<int 0|1>
|
|
15
|
+
await p15.write(level); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
console.log('PORT1=0x' + port.toString(16).padStart(2, '0') + ' P0=' + level);
|
|
17
|
+
}, 200);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main();
|