periph 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/examples/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -0
- package/examples/rtc/ds3231/complete.js +77 -0
- package/examples/rtc/ds3231/demo.js +60 -0
- package/examples/rtc/ds3231/minimal.js +23 -0
- package/examples/rtc/pcf8523/complete.js +66 -0
- package/examples/rtc/pcf8523/demo.js +65 -0
- package/examples/rtc/pcf8523/minimal.js +20 -0
- package/examples/temperature/mcp9808/complete.js +89 -0
- package/examples/temperature/mcp9808/demo.js +62 -0
- package/examples/temperature/mcp9808/minimal.js +21 -0
- package/examples/temperature/tmp117/complete.js +90 -0
- package/examples/temperature/tmp117/demo.js +73 -0
- package/examples/temperature/tmp117/minimal.js +21 -0
- package/examples/tof/vl53l0x/complete.js +118 -0
- package/examples/tof/vl53l0x/demo.js +83 -0
- package/examples/tof/vl53l0x/minimal.js +25 -0
- package/examples/tof/vl53l1x/complete.js +151 -0
- package/examples/tof/vl53l1x/demo.js +100 -0
- package/examples/tof/vl53l1x/minimal.js +25 -0
- package/package.json +1 -1
- package/src/chips/imu/mpu9250.js +4 -4
- package/src/chips/imu/mpu9255.js +5 -5
- package/src/chips/motor/drv8830.js +239 -0
- package/src/chips/rtc/ds3231.js +504 -0
- package/src/chips/rtc/pcf8523.js +539 -0
- package/src/chips/temperature/mcp9808.js +488 -0
- package/src/chips/temperature/tmp117.js +471 -0
- package/src/chips/tof/_vl53_base.js +160 -0
- package/src/chips/tof/vl53l0x.js +816 -0
- package/src/chips/tof/vl53l1x.js +816 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Industrial freezer monitor: the healthy range is -25 °C to -15 °C, and
|
|
4
|
+
// -5 °C means the door has been left open too long. The Alert output fires
|
|
5
|
+
// in interrupt mode each time the temperature leaves or re-enters the
|
|
6
|
+
// window; the callback reports which boundary tripped. After a fixed number
|
|
7
|
+
// of alerts the monitor disables the Alert output and unsubscribes.
|
|
8
|
+
|
|
9
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
10
|
+
const { MCP9808Full } = require('periph/src/chips/temperature/mcp9808');
|
|
11
|
+
|
|
12
|
+
const MAX_ALERTS = 10;
|
|
13
|
+
|
|
14
|
+
(async () => {
|
|
15
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
16
|
+
const connection = new I2CConnection(bus, MCP9808Full.I2C_ADDRESS);
|
|
17
|
+
const sensor = new MCP9808Full(connection); // Create MCP9808 Full driver, (connection)
|
|
18
|
+
await sensor.init(); // Confirm device identity, () → None
|
|
19
|
+
|
|
20
|
+
// --- Trade resolution for faster sampling ---
|
|
21
|
+
// 0.25 °C is plenty for a freezer and converts in ~65 ms instead of 250 ms,
|
|
22
|
+
// so a door opening shows up in the next reading almost immediately.
|
|
23
|
+
await sensor.setResolution(0.25); // Set resolution, (celsius 0.5|0.25|0.125|0.0625) → None
|
|
24
|
+
|
|
25
|
+
// --- Program the healthy window and the door-open threshold ---
|
|
26
|
+
// TLOWER/TUPPER bracket normal operation; TCRIT flags a door left open.
|
|
27
|
+
// 3 °C of hysteresis stops the Alert chattering while the compressor cycles.
|
|
28
|
+
await sensor.setLowerLimit(-25.0); // Set TLOWER, (celsius °C) → None
|
|
29
|
+
await sensor.setUpperLimit(-15.0); // Set TUPPER, (celsius °C) → None
|
|
30
|
+
await sensor.setCriticalLimit(-5.0); // Set TCRIT, (celsius °C) → None
|
|
31
|
+
await sensor.setHysteresis(3.0); // Set hysteresis, (celsius 0|1.5|3.0|6.0) → None
|
|
32
|
+
|
|
33
|
+
// --- Route every boundary to the Alert pin as a latched interrupt ---
|
|
34
|
+
// Interrupt mode latches each crossing until clearInterrupt(), so a short
|
|
35
|
+
// excursion is never missed between two reads.
|
|
36
|
+
await sensor.configureAlert('all', 'interrupt', 'active_low'); // Configure Alert, (mode='all', output='comparator', polarity='active_low') → None
|
|
37
|
+
await sensor.enableAlert(); // Enable Alert output, () → None
|
|
38
|
+
|
|
39
|
+
let alerts = 0;
|
|
40
|
+
let done;
|
|
41
|
+
const finished = new Promise((r) => { done = r; });
|
|
42
|
+
|
|
43
|
+
// --- Report which boundary tripped, then re-arm ---
|
|
44
|
+
// The status mask is a live read of TA's boundary bits; an empty mask
|
|
45
|
+
// means the temperature has come back inside the healthy window.
|
|
46
|
+
await sensor.onInterrupt(async (status) => { // Subscribe to Alert, (callback) → None
|
|
47
|
+
const t = await sensor.readTemperature(); // Read ambient temperature, () → number °C
|
|
48
|
+
if (status & MCP9808Full.SOURCE_CRITICAL) console.log(`${t.toFixed(2)} °C CRITICAL — door open?`);
|
|
49
|
+
else if (status & MCP9808Full.SOURCE_UPPER) console.log(`${t.toFixed(2)} °C too warm`);
|
|
50
|
+
else if (status & MCP9808Full.SOURCE_LOWER) console.log(`${t.toFixed(2)} °C too cold`);
|
|
51
|
+
else console.log(`${t.toFixed(2)} °C back in range`);
|
|
52
|
+
await sensor.clearInterrupt(); // Clear interrupt-mode Alert, () → None
|
|
53
|
+
if (++alerts >= MAX_ALERTS) done();
|
|
54
|
+
});
|
|
55
|
+
console.log(`monitoring, ${(await sensor.readTemperature()).toFixed(2)} °C now`); // Read ambient temperature, () → number °C
|
|
56
|
+
await finished;
|
|
57
|
+
|
|
58
|
+
// --- Shut down cleanly after the demo run ---
|
|
59
|
+
await sensor.disableAlert(); // Disable Alert output, () → None
|
|
60
|
+
await sensor.offInterrupt(); // Unsubscribe, () → None
|
|
61
|
+
await connection.close();
|
|
62
|
+
})();
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { MCP9808Minimal } = require('periph/src/chips/temperature/mcp9808');
|
|
5
|
+
|
|
6
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
7
|
+
|
|
8
|
+
(async () => {
|
|
9
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
10
|
+
const connection = new I2CConnection(bus, MCP9808Minimal.I2C_ADDRESS);
|
|
11
|
+
const sensor = new MCP9808Minimal(connection); // Create MCP9808 driver, (connection)
|
|
12
|
+
await sensor.init(); // Confirm device identity, () → None
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < 10; i++) {
|
|
15
|
+
const t = await sensor.readTemperature(); // Read ambient temperature, () → number °C
|
|
16
|
+
console.log(`${t.toFixed(4)} °C`);
|
|
17
|
+
await sleep(1000);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
await connection.close();
|
|
21
|
+
})();
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Exercises every method in the TMP117 Full API. EEPROM unlock/lock is shown
|
|
4
|
+
// without any write in between, so no power-on default is changed.
|
|
5
|
+
|
|
6
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
7
|
+
const { TMP117Full } = require('periph/src/chips/temperature/tmp117');
|
|
8
|
+
|
|
9
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
13
|
+
const connection = new I2CConnection(bus, TMP117Full.I2C_ADDRESS);
|
|
14
|
+
const sensor = new TMP117Full(connection); // Create TMP117 Full driver, (connection)
|
|
15
|
+
await sensor.init(); // Confirm device identity, () → None
|
|
16
|
+
// checks DEVICE_ID bits 11:0 == 0x117
|
|
17
|
+
|
|
18
|
+
const t = await sensor.readTemperature(); // Read temperature, () → number °C
|
|
19
|
+
// decodes TEMP_RESULT, 0.0078125 °C two's complement
|
|
20
|
+
console.log(`temperature ${t.toFixed(4)} °C`);
|
|
21
|
+
|
|
22
|
+
await sensor.configure('continuous', 32, 0.5); // Configure conversion, (mode='continuous', averaging=8, cycleSeconds=1.0 s) → None
|
|
23
|
+
// writes MOD/AVG/CONV; cycle snaps to the nearest CONV step
|
|
24
|
+
console.log('config', await sensor.getConfig()); // Read conversion config, () → {mode, averaging, cycleSeconds s}
|
|
25
|
+
// decodes MOD, AVG and CONV from CONFIGURATION
|
|
26
|
+
|
|
27
|
+
await sensor.configure('shutdown'); // Configure conversion, (mode='continuous', averaging=8, cycleSeconds=1.0 s) → None
|
|
28
|
+
// MOD=01 stops conversions; TEMP_RESULT keeps its last value
|
|
29
|
+
console.log('shutdown', await sensor.isShutdown()); // Check Shutdown mode, () → boolean
|
|
30
|
+
// reads MOD[1:0] == 01
|
|
31
|
+
await sensor.triggerOneShot(); // Start one conversion, () → None
|
|
32
|
+
// MOD=11; returns to Shutdown when done
|
|
33
|
+
while (!(await sensor.isDataReady())) { // Check for a fresh result, () → boolean
|
|
34
|
+
await sleep(10); // reading Data_Ready clears it
|
|
35
|
+
}
|
|
36
|
+
console.log(`one-shot ${(await sensor.readTemperature()).toFixed(4)} °C`); // Read temperature, () → number °C
|
|
37
|
+
// the one-shot result
|
|
38
|
+
await sensor.configure(); // Configure conversion, (mode='continuous', averaging=8, cycleSeconds=1.0 s) → None
|
|
39
|
+
// back to the POR default
|
|
40
|
+
|
|
41
|
+
await sensor.setHighLimit(30.0); // Set THIGH_LIMIT, (celsius °C) → None
|
|
42
|
+
// rounded to the nearest 0.0078125 °C step
|
|
43
|
+
await sensor.setLowLimit(10.0); // Set TLOW_LIMIT, (celsius °C) → None
|
|
44
|
+
// rounded to the nearest 0.0078125 °C step
|
|
45
|
+
console.log('high', await sensor.getHighLimit()); // Read THIGH_LIMIT, () → number °C
|
|
46
|
+
// same format as TEMP_RESULT
|
|
47
|
+
console.log('low', await sensor.getLowLimit()); // Read TLOW_LIMIT, () → number °C
|
|
48
|
+
// same format as TEMP_RESULT
|
|
49
|
+
|
|
50
|
+
await sensor.setTemperatureOffset(0.25); // Set calibration offset, (celsius °C) → None
|
|
51
|
+
// added to every result after linearization
|
|
52
|
+
console.log('offset', await sensor.getTemperatureOffset()); // Read calibration offset, () → number °C
|
|
53
|
+
// decodes TEMP_OFFSET
|
|
54
|
+
await sensor.setTemperatureOffset(0.0); // Set calibration offset, (celsius °C) → None
|
|
55
|
+
// remove the offset again
|
|
56
|
+
|
|
57
|
+
await sensor.unlockEeprom(); // Unlock EEPROM, () → None
|
|
58
|
+
// EUN=1: EEPROM-backed writes now persist
|
|
59
|
+
console.log('eeprom busy', await sensor.isEepromBusy()); // Check EEPROM busy, () → boolean
|
|
60
|
+
// reads EEPROM_UL.EEPROM_Busy
|
|
61
|
+
await sensor.lockEeprom(); // Lock EEPROM, () → None
|
|
62
|
+
// EUN=0: writes are volatile again
|
|
63
|
+
const eeprom1 = await sensor.readEepromScratch(1); // Read EEPROM scratch, (slot 1|2|3) → number
|
|
64
|
+
// slot 1 holds part of the factory unique ID
|
|
65
|
+
console.log(`eeprom1 0x${eeprom1.toString(16).padStart(4, '0')}`);
|
|
66
|
+
await sensor.writeEepromScratch(2, 0x1234); // Write EEPROM scratch, (slot 2, value 16-bit) → None
|
|
67
|
+
// only EEPROM2 is writable; volatile while locked
|
|
68
|
+
const eeprom2 = await sensor.readEepromScratch(2); // Read EEPROM scratch, (slot 1|2|3) → number
|
|
69
|
+
// reads back EEPROM2
|
|
70
|
+
console.log(`eeprom2 0x${eeprom2.toString(16).padStart(4, '0')}`);
|
|
71
|
+
|
|
72
|
+
await sensor.configureAlert('alert', 'active_low', 'alert'); // Configure ALERT, (mode='alert', polarity='active_low', pinFunction='alert') → None
|
|
73
|
+
// sets T/nA, POL and DR/Alert together
|
|
74
|
+
|
|
75
|
+
const status = await sensor.pollInterrupt(); // Read alert flags, () → number mask
|
|
76
|
+
// HIGH_Alert/LOW_Alert; the read clears them in Alert mode
|
|
77
|
+
console.log('above high', (status & TMP117Full.SOURCE_HIGH) !== 0,
|
|
78
|
+
'below low', (status & TMP117Full.SOURCE_LOW) !== 0);
|
|
79
|
+
|
|
80
|
+
await sensor.onInterrupt((s) => console.log('alert, status mask', s)); // Subscribe to ALERT, (callback) → None
|
|
81
|
+
// callback receives the pollInterrupt() mask
|
|
82
|
+
await sleep(5000);
|
|
83
|
+
await sensor.offInterrupt(); // Unsubscribe, () → None
|
|
84
|
+
// detaches the edge handler or stops the polling timer
|
|
85
|
+
|
|
86
|
+
await sensor.reset(); // Software reset, () → None
|
|
87
|
+
// reloads CONFIGURATION/limits/offset from EEPROM, 2 ms
|
|
88
|
+
|
|
89
|
+
await connection.close();
|
|
90
|
+
})();
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// PT100-replacement cold-chain container thermometer: maximum averaging gives
|
|
4
|
+
// the lowest-noise reading, and the ALERT output fires when the cargo leaves
|
|
5
|
+
// the -25 °C to 8 °C safe transport range; the callback reports which
|
|
6
|
+
// boundary tripped. Set REFERENCE_C to a reference thermometer reading to
|
|
7
|
+
// calibrate once and persist the offset to EEPROM. After a fixed number of
|
|
8
|
+
// alerts the monitor unsubscribes.
|
|
9
|
+
|
|
10
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
11
|
+
const { TMP117Full } = require('periph/src/chips/temperature/tmp117');
|
|
12
|
+
|
|
13
|
+
const MAX_ALERTS = 10;
|
|
14
|
+
const REFERENCE_C = process.env.REFERENCE_C ? parseFloat(process.env.REFERENCE_C) : null;
|
|
15
|
+
|
|
16
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
17
|
+
|
|
18
|
+
(async () => {
|
|
19
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
20
|
+
const connection = new I2CConnection(bus, TMP117Full.I2C_ADDRESS);
|
|
21
|
+
const sensor = new TMP117Full(connection); // Create TMP117 Full driver, (connection)
|
|
22
|
+
await sensor.init(); // Confirm device identity, () → None
|
|
23
|
+
|
|
24
|
+
// --- Lowest-noise continuous conversion ---
|
|
25
|
+
// 64-conversion averaging with a 1 s cycle gives the quietest result the
|
|
26
|
+
// chip can deliver — a cold-chain log needs stability, not speed.
|
|
27
|
+
await sensor.configure('continuous', 64, 1.0); // Configure conversion, (mode='continuous', averaging=8, cycleSeconds=1.0 s) → None
|
|
28
|
+
|
|
29
|
+
// --- One-time calibration against a reference thermometer ---
|
|
30
|
+
// The observed error is written to TEMP_OFFSET with the EEPROM unlocked, so
|
|
31
|
+
// the correction survives power cycles. EEPROM endurance is limited — this
|
|
32
|
+
// is a once-per-deployment step, not a loop.
|
|
33
|
+
if (REFERENCE_C !== null) {
|
|
34
|
+
await sleep(1100);
|
|
35
|
+
const offset = REFERENCE_C - (await sensor.readTemperature()) // Read temperature, () → number °C
|
|
36
|
+
+ (await sensor.getTemperatureOffset()); // Read calibration offset, () → number °C
|
|
37
|
+
await sensor.unlockEeprom(); // Unlock EEPROM, () → None
|
|
38
|
+
await sensor.setTemperatureOffset(offset); // Set calibration offset, (celsius °C) → None
|
|
39
|
+
while (await sensor.isEepromBusy()) { // Check EEPROM busy, () → boolean
|
|
40
|
+
await sleep(1);
|
|
41
|
+
}
|
|
42
|
+
await sensor.lockEeprom(); // Lock EEPROM, () → None
|
|
43
|
+
console.log(`calibrated, offset ${offset.toFixed(4)} °C`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// --- Program the safe transport range ---
|
|
47
|
+
// Alert mode flags either side of the window independently; ALERT is
|
|
48
|
+
// active-low open-drain, pulled up on the board.
|
|
49
|
+
await sensor.setHighLimit(8.0); // Set THIGH_LIMIT, (celsius °C) → None
|
|
50
|
+
await sensor.setLowLimit(-25.0); // Set TLOW_LIMIT, (celsius °C) → None
|
|
51
|
+
await sensor.configureAlert('alert', 'active_low'); // Configure ALERT, (mode='alert', polarity='active_low', pinFunction='alert') → None
|
|
52
|
+
|
|
53
|
+
let alerts = 0;
|
|
54
|
+
let done;
|
|
55
|
+
const finished = new Promise((r) => { done = r; });
|
|
56
|
+
|
|
57
|
+
// --- Report which boundary tripped ---
|
|
58
|
+
// The status mask comes from CONFIGURATION's alert flags; reading them
|
|
59
|
+
// clears them in Alert mode, re-arming the pin for the next excursion.
|
|
60
|
+
await sensor.onInterrupt(async (status) => { // Subscribe to ALERT, (callback) → None
|
|
61
|
+
if (!status) return;
|
|
62
|
+
const t = await sensor.readTemperature(); // Read temperature, () → number °C
|
|
63
|
+
if (status & TMP117Full.SOURCE_HIGH) console.log(`${t.toFixed(2)} °C too warm — cargo above 8 °C`);
|
|
64
|
+
else if (status & TMP117Full.SOURCE_LOW) console.log(`${t.toFixed(2)} °C too cold — cargo below -25 °C`);
|
|
65
|
+
if (++alerts >= MAX_ALERTS) done();
|
|
66
|
+
});
|
|
67
|
+
console.log(`monitoring, ${(await sensor.readTemperature()).toFixed(2)} °C now`); // Read temperature, () → number °C
|
|
68
|
+
await finished;
|
|
69
|
+
|
|
70
|
+
// --- Stop monitoring after the demo run ---
|
|
71
|
+
await sensor.offInterrupt(); // Unsubscribe, () → None
|
|
72
|
+
await connection.close();
|
|
73
|
+
})();
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { TMP117Minimal } = require('periph/src/chips/temperature/tmp117');
|
|
5
|
+
|
|
6
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
7
|
+
|
|
8
|
+
(async () => {
|
|
9
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
10
|
+
const connection = new I2CConnection(bus, TMP117Minimal.I2C_ADDRESS);
|
|
11
|
+
const sensor = new TMP117Minimal(connection); // Create TMP117 driver, (connection)
|
|
12
|
+
await sensor.init(); // Confirm device identity, () → None
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < 10; i++) {
|
|
15
|
+
const t = await sensor.readTemperature(); // Read temperature, () → number °C
|
|
16
|
+
console.log(`${t.toFixed(4)} °C`);
|
|
17
|
+
await sleep(1000);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
await connection.close();
|
|
21
|
+
})();
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Exercises every method in the VL53L0X Full API, and finally moves the
|
|
4
|
+
// sensor to another I²C address and back to 0x29.
|
|
5
|
+
|
|
6
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
7
|
+
const { VL53L0XFull } = require('periph/src/chips/tof/vl53l0x');
|
|
8
|
+
|
|
9
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
13
|
+
const connection = new I2CConnection(bus, VL53L0XFull.I2C_ADDRESS);
|
|
14
|
+
const sensor = new VL53L0XFull(connection); // Create VL53L0X Full driver, (connection)
|
|
15
|
+
await sensor.init(); // Wait for init sequence, () → None
|
|
16
|
+
// ID check, tuning, SPADs, VHV + phase calibration
|
|
17
|
+
|
|
18
|
+
console.log('model', (await sensor.modelId()).toString(16)); // Read model ID, () → number
|
|
19
|
+
// IDENTIFICATION_MODEL_ID, always 0xEE
|
|
20
|
+
console.log('revision', (await sensor.revisionId()).toString(16)); // Read revision ID, () → number
|
|
21
|
+
// IDENTIFICATION_REVISION_ID, 0x10 on current silicon
|
|
22
|
+
|
|
23
|
+
const d = await sensor.distance(); // Measure distance, () → number mm
|
|
24
|
+
// single shot; blocks for about one timing budget
|
|
25
|
+
console.log('distance', d, 'mm, valid', await sensor.rangeValid()); // Check last measurement, () → boolean
|
|
26
|
+
// device range status == 11 (range complete)
|
|
27
|
+
console.log('range status', await sensor.rangeStatus()); // Read last range status, () → number 0–15
|
|
28
|
+
// 11 = valid, 4 = no target
|
|
29
|
+
const m = await sensor.readMeasurement(); // Read result block, () → {distanceMm, rangeStatus, signalRateMcps, ambientRateMcps, effectiveSpadCount}
|
|
30
|
+
// distance, status, signal/ambient MCPS, SPAD count
|
|
31
|
+
console.log(`signal ${m.signalRateMcps.toFixed(2)} MCPS, ambient ${m.ambientRateMcps.toFixed(2)} MCPS`);
|
|
32
|
+
|
|
33
|
+
await sensor.startContinuous(); // Start continuous ranging, (periodMs=0 ms) → None
|
|
34
|
+
// 0 = back-to-back measurements
|
|
35
|
+
for (let i = 0; i < 5; i++) {
|
|
36
|
+
console.log('continuous', await sensor.readContinuous(), 'mm'); // Read next continuous result, () → number mm
|
|
37
|
+
// waits for a fresh data-ready, then clears it
|
|
38
|
+
}
|
|
39
|
+
await sensor.stopContinuous(); // Stop continuous ranging, () → None
|
|
40
|
+
// does not wait for a running measurement
|
|
41
|
+
|
|
42
|
+
await sensor.startContinuous(100); // Start continuous ranging, (periodMs=0 ms) → None
|
|
43
|
+
// timed mode: one measurement every 100 ms
|
|
44
|
+
while (!(await sensor.dataReady())) { // Check for a result, () → boolean
|
|
45
|
+
await sleep(10); // RESULT_INTERRUPT_STATUS bits 2:0 non-zero
|
|
46
|
+
}
|
|
47
|
+
console.log('timed', (await sensor.readMeasurement()).distanceMm, 'mm'); // Read result block, () → {distanceMm, …}
|
|
48
|
+
// non-blocking; clears the interrupt
|
|
49
|
+
await sensor.stopContinuous(); // Stop continuous ranging, () → None
|
|
50
|
+
// back to software standby
|
|
51
|
+
|
|
52
|
+
console.log('budget', await sensor.timingBudget(), 'us'); // Read timing budget, () → number µs
|
|
53
|
+
// computed from the sequence-step timeouts
|
|
54
|
+
await sensor.setTimingBudget(50000); // Set timing budget, (budgetUs µs) → None
|
|
55
|
+
// longer budget = lower noise, ≥ 20000 µs
|
|
56
|
+
console.log('signal limit', await sensor.signalRateLimit(), 'MCPS'); // Read signal-rate limit, () → number MCPS
|
|
57
|
+
// 9.7 fixed point
|
|
58
|
+
await sensor.setSignalRateLimit(0.1); // Set signal-rate limit, (limitMcps MCPS) → None
|
|
59
|
+
// lower = longer range, more noise
|
|
60
|
+
await sensor.setVcselPulsePeriod('pre_range', 18); // Set VCSEL period, (periodType, pclks) → None
|
|
61
|
+
// pre-range 12/14/16/18; redoes phase calibration
|
|
62
|
+
await sensor.setVcselPulsePeriod('final_range', 14); // Set VCSEL period, (periodType, pclks) → None
|
|
63
|
+
// final-range 8/10/12/14
|
|
64
|
+
console.log('vcsel', await sensor.vcselPulsePeriod('pre_range'), // Read VCSEL period, (periodType) → number PCLKs
|
|
65
|
+
await sensor.vcselPulsePeriod('final_range')); // (reg + 1) × 2
|
|
66
|
+
await sensor.setProfile('default'); // Apply ranging profile, (profile) → None
|
|
67
|
+
// 0.25 MCPS, 14/10 PCLKs, 33 ms
|
|
68
|
+
|
|
69
|
+
const original = await sensor.offset(); // Read range offset, () → number mm
|
|
70
|
+
// NVM factory value, 0.25 mm steps
|
|
71
|
+
await sensor.setOffset(original - 5); // Set range offset, (offsetMm mm) → None
|
|
72
|
+
// volatile override, −512.0 to 511.75 mm
|
|
73
|
+
console.log('offset', await sensor.offset(), 'mm'); // Read range offset, () → number mm
|
|
74
|
+
// 12-bit two's complement × 0.25
|
|
75
|
+
await sensor.setOffset(original); // Set range offset, (offsetMm mm) → None
|
|
76
|
+
// restore the factory value
|
|
77
|
+
await sensor.setCrosstalkCompensation(0); // Set crosstalk compensation, (rateMcps MCPS) → None
|
|
78
|
+
// 0 = compensation off
|
|
79
|
+
|
|
80
|
+
await sensor.recalibrate(); // Rerun reference calibration, () → None
|
|
81
|
+
// VHV + phase; needed after a > 8 °C change
|
|
82
|
+
|
|
83
|
+
await sensor.setInterruptThresholds(100, 800); // Set distance thresholds, (lowMm mm, highMm mm) → None
|
|
84
|
+
// 2 mm resolution
|
|
85
|
+
console.log('thresholds', await sensor.interruptThresholds()); // Read distance thresholds, () → {lowMm, highMm} mm
|
|
86
|
+
// decoded from SYSTEM_THRESH_LOW/HIGH
|
|
87
|
+
await sensor.enableInterrupt(VL53L0XFull.SOURCE_OUT_OF_WINDOW); // Select interrupt source, (source) → None
|
|
88
|
+
// replaces the active source (mutually exclusive)
|
|
89
|
+
await sensor.disableInterrupt(VL53L0XFull.SOURCE_OUT_OF_WINDOW); // Disable interrupt source, (source) → None
|
|
90
|
+
// only if it is the active one
|
|
91
|
+
await sensor.enableInterrupt(VL53L0XFull.SOURCE_NEW_SAMPLE_READY); // Select interrupt source, (source) → None
|
|
92
|
+
// back to the default data-ready source
|
|
93
|
+
|
|
94
|
+
await sensor.onInterrupt((status) => console.log('interrupt, source', status)); // Subscribe to GPIO1, (callback) → None
|
|
95
|
+
// status is read and cleared before the callback
|
|
96
|
+
await sensor.startContinuous(200); // Start continuous ranging, (periodMs=0 ms) → None
|
|
97
|
+
// timed mode feeds the subscription
|
|
98
|
+
await sleep(1000);
|
|
99
|
+
await sensor.stopContinuous(); // Stop continuous ranging, () → None
|
|
100
|
+
// no more samples
|
|
101
|
+
await sensor.offInterrupt(); // Unsubscribe, () → None
|
|
102
|
+
// detaches the pin handler or stops the polling timer
|
|
103
|
+
console.log('pending', await sensor.pollInterrupt()); // Read and clear status, () → number
|
|
104
|
+
// SOURCE_* value that fired, 0 = nothing pending
|
|
105
|
+
|
|
106
|
+
await sensor.setAddress(0x30); // Change I²C address, (address) → None
|
|
107
|
+
// volatile; this driver instance is now unusable
|
|
108
|
+
await connection.close();
|
|
109
|
+
const movedConnection = new I2CConnection(bus, 0x30);
|
|
110
|
+
const moved = new VL53L0XFull(movedConnection); // Create VL53L0X Full driver, (connection)
|
|
111
|
+
await moved.init(); // Wait for init sequence, () → None
|
|
112
|
+
// re-init at the new address is safe
|
|
113
|
+
console.log('at 0x30', await moved.distance(), 'mm'); // Measure distance, () → number mm
|
|
114
|
+
// same sensor, new address
|
|
115
|
+
await moved.setAddress(VL53L0XFull.I2C_ADDRESS); // Change I²C address, (address) → None
|
|
116
|
+
// back to the power-on 0x29
|
|
117
|
+
await movedConnection.close();
|
|
118
|
+
})();
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Touchless presence gate with multi-rate ranging: a first measurement picks
|
|
4
|
+
// the profile (long range in a dark room, default otherwise), then timed
|
|
5
|
+
// continuous ranging at 100 ms feeds an out-of-window interrupt — closer than
|
|
6
|
+
// 10 cm is an ENTER event, the scene clearing beyond 80 cm a LEAVE event.
|
|
7
|
+
// After 20 events or 60 s, it prints statistics over 10 fresh samples, stops
|
|
8
|
+
// ranging and recalibrates. Without a GPIO1 pin on the connection the
|
|
9
|
+
// driver's polling fallback delivers the events.
|
|
10
|
+
|
|
11
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
12
|
+
const { VL53L0XFull } = require('periph/src/chips/tof/vl53l0x');
|
|
13
|
+
|
|
14
|
+
const MAX_EVENTS = 20;
|
|
15
|
+
const MAX_MS = 60000;
|
|
16
|
+
|
|
17
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
18
|
+
|
|
19
|
+
(async () => {
|
|
20
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
21
|
+
const connection = new I2CConnection(bus, VL53L0XFull.I2C_ADDRESS);
|
|
22
|
+
const sensor = new VL53L0XFull(connection); // Create VL53L0X Full driver, (connection)
|
|
23
|
+
await sensor.init(); // Wait for init sequence, () → None
|
|
24
|
+
|
|
25
|
+
// --- Pick a profile from the ambient light level ---
|
|
26
|
+
// The long-range profile (0.1 MCPS limit, 18/14 PCLK VCSEL periods) reaches
|
|
27
|
+
// ~2 m, but only without IR background; in daylight it mostly adds invalid
|
|
28
|
+
// readings. One single-shot measurement tells us how bright the scene is.
|
|
29
|
+
await sensor.distance(); // Measure distance, () → number mm
|
|
30
|
+
const first = await sensor.readMeasurement(); // Read result block, () → {distanceMm, …}
|
|
31
|
+
if (first.ambientRateMcps < 0.5) {
|
|
32
|
+
await sensor.setProfile('long_range'); // Apply ranging profile, (profile) → None
|
|
33
|
+
console.log(`dark scene (${first.ambientRateMcps.toFixed(2)} MCPS ambient): long_range profile`);
|
|
34
|
+
} else {
|
|
35
|
+
await sensor.setProfile('default'); // Apply ranging profile, (profile) → None
|
|
36
|
+
console.log(`bright scene (${first.ambientRateMcps.toFixed(2)} MCPS ambient): default profile`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// --- Arm the presence gate ---
|
|
40
|
+
// Timed ranging every 100 ms keeps the laser mostly idle. The firmware
|
|
41
|
+
// compares each result with the 100 mm / 800 mm window itself and only
|
|
42
|
+
// raises GPIO1 when a reading falls outside it.
|
|
43
|
+
await sensor.setInterruptThresholds(100, 800); // Set distance thresholds, (lowMm mm, highMm mm) → None
|
|
44
|
+
await sensor.enableInterrupt(VL53L0XFull.SOURCE_OUT_OF_WINDOW); // Select interrupt source, (source) → None
|
|
45
|
+
await sensor.startContinuous(100); // Start continuous ranging, (periodMs=0 ms) → None
|
|
46
|
+
|
|
47
|
+
// --- Classify each event ---
|
|
48
|
+
// The status is already cleared; the result block still holds the
|
|
49
|
+
// measurement that triggered it.
|
|
50
|
+
let events = 0;
|
|
51
|
+
await sensor.onInterrupt(async () => { // Subscribe to GPIO1, (callback) → None
|
|
52
|
+
const d = (await sensor.readMeasurement()).distanceMm; // Read result block, () → {distanceMm, …}
|
|
53
|
+
events++;
|
|
54
|
+
console.log(d < 100 ? 'ENTER' : 'LEAVE', d, 'mm');
|
|
55
|
+
});
|
|
56
|
+
const start = Date.now();
|
|
57
|
+
while (events < MAX_EVENTS && Date.now() - start < MAX_MS) await sleep(50);
|
|
58
|
+
|
|
59
|
+
// --- Statistics over fresh samples ---
|
|
60
|
+
// Threshold sources hide ordinary samples from dataReady(), so switch back
|
|
61
|
+
// to new-sample-ready before using the blocking continuous reads.
|
|
62
|
+
await sensor.offInterrupt(); // Unsubscribe, () → None
|
|
63
|
+
await sensor.enableInterrupt(VL53L0XFull.SOURCE_NEW_SAMPLE_READY); // Select interrupt source, (source) → None
|
|
64
|
+
await sensor.pollInterrupt(); // Read and clear status, () → number
|
|
65
|
+
const samples = [];
|
|
66
|
+
let rate = 0;
|
|
67
|
+
for (let i = 0; i < 10; i++) {
|
|
68
|
+
samples.push(await sensor.readContinuous()); // Read next continuous result, () → number mm
|
|
69
|
+
rate += (await sensor.readMeasurement()).signalRateMcps; // Read result block, () → {signalRateMcps, …}
|
|
70
|
+
}
|
|
71
|
+
const mean = samples.reduce((a, b) => a + b, 0) / samples.length;
|
|
72
|
+
console.log(`mean ${mean.toFixed(0)} mm, min ${Math.min(...samples)} mm, max ${Math.max(...samples)} mm, ` +
|
|
73
|
+
`signal ${(rate / samples.length).toFixed(2)} MCPS`);
|
|
74
|
+
|
|
75
|
+
// --- Shut down and recalibrate ---
|
|
76
|
+
// Reference calibration must run in software standby. Repeat it whenever
|
|
77
|
+
// the sensor's temperature has drifted more than 8 °C.
|
|
78
|
+
await sensor.stopContinuous(); // Stop continuous ranging, () → None
|
|
79
|
+
await sleep(200);
|
|
80
|
+
await sensor.recalibrate(); // Rerun reference calibration, () → None
|
|
81
|
+
console.log('done');
|
|
82
|
+
await connection.close();
|
|
83
|
+
})();
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { VL53L0XMinimal } = require('periph/src/chips/tof/vl53l0x');
|
|
5
|
+
|
|
6
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
7
|
+
|
|
8
|
+
(async () => {
|
|
9
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
10
|
+
const connection = new I2CConnection(bus, VL53L0XMinimal.I2C_ADDRESS);
|
|
11
|
+
const sensor = new VL53L0XMinimal(connection); // Create VL53L0X driver, (connection)
|
|
12
|
+
await sensor.init(); // Wait for init sequence, () → None
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < 50; i++) {
|
|
15
|
+
const d = await sensor.distance(); // Measure distance, () → number mm
|
|
16
|
+
if (await sensor.rangeValid()) { // Check last measurement, () → boolean
|
|
17
|
+
console.log(`${d} mm`);
|
|
18
|
+
} else {
|
|
19
|
+
console.log('out of range');
|
|
20
|
+
}
|
|
21
|
+
await sleep(100);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
await connection.close();
|
|
25
|
+
})();
|