periph 1.5.3 → 1.6.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 +18 -0
- package/examples/color/isl29125/complete.js +57 -0
- package/examples/color/isl29125/demo.js +77 -0
- package/examples/color/isl29125/minimal.js +23 -0
- package/examples/environmental/bme688/complete.js +48 -0
- package/examples/environmental/bme688/demo.js +47 -0
- package/examples/environmental/bme688/minimal.js +21 -0
- package/examples/gas/ccs811/complete.js +87 -0
- package/examples/gas/ccs811/demo.js +108 -0
- package/examples/gas/ccs811/minimal.js +20 -0
- package/examples/gas/rrh46410/complete.js +78 -0
- package/examples/gas/rrh46410/demo.js +86 -0
- package/examples/gas/rrh46410/minimal.js +21 -0
- package/examples/humidity/hts221/complete.js +83 -0
- package/examples/humidity/hts221/demo.js +94 -0
- package/examples/humidity/hts221/minimal.js +22 -0
- package/examples/humidity/htu31d/complete.js +50 -0
- package/examples/humidity/htu31d/demo.js +65 -0
- package/examples/humidity/htu31d/minimal.js +22 -0
- package/examples/humidity/shtc3/complete.js +48 -0
- package/examples/humidity/shtc3/demo.js +45 -0
- package/examples/humidity/shtc3/minimal.js +22 -0
- package/examples/led/saa1064/complete.js +51 -0
- package/examples/led/saa1064/demo.js +54 -0
- package/examples/led/saa1064/minimal.js +23 -0
- package/examples/light/apds9250/complete.js +71 -0
- package/examples/light/apds9250/demo.js +79 -0
- package/examples/light/apds9250/minimal.js +23 -0
- package/examples/light/bh1750/complete.js +46 -0
- package/examples/light/bh1750/demo.js +52 -0
- package/examples/light/bh1750/minimal.js +22 -0
- package/examples/light/si1132/complete.js +70 -0
- package/examples/light/si1132/demo.js +73 -0
- package/examples/light/si1132/minimal.js +22 -0
- package/examples/light/si1133/complete.js +89 -0
- package/examples/light/si1133/demo.js +93 -0
- package/examples/light/si1133/minimal.js +22 -0
- package/examples/light/si1145/complete.js +93 -0
- package/examples/light/si1145/demo.js +98 -0
- package/examples/light/si1145/minimal.js +22 -0
- package/examples/pressure/mpl3115a2/complete.js +49 -0
- package/examples/pressure/mpl3115a2/demo.js +48 -0
- package/examples/pressure/mpl3115a2/minimal.js +22 -0
- package/examples/pressure/ms5607/complete.js +49 -0
- package/examples/pressure/ms5607/demo.js +65 -0
- package/examples/pressure/ms5607/minimal.js +22 -0
- package/examples/pressure/ms5611/complete.js +49 -0
- package/examples/pressure/ms5611/demo.js +65 -0
- package/examples/pressure/ms5611/minimal.js +22 -0
- package/examples/pressure/spl06/complete.js +81 -0
- package/examples/pressure/spl06/demo.js +106 -0
- package/examples/pressure/spl06/minimal.js +22 -0
- package/examples/rfid/pn532/complete.js +99 -0
- package/examples/rfid/pn532/demo.js +121 -0
- package/examples/rfid/pn532/minimal.js +22 -0
- package/package.json +1 -1
- package/src/chips/color/isl29125.js +379 -0
- package/src/chips/environmental/bme688.js +573 -0
- package/src/chips/gas/ccs811.js +400 -0
- package/src/chips/gas/rrh46410.js +534 -0
- package/src/chips/humidity/hts221.js +369 -0
- package/src/chips/humidity/htu31d.js +242 -0
- package/src/chips/humidity/shtc3.js +239 -0
- package/src/chips/led/saa1064.js +241 -0
- package/src/chips/light/apds9250.js +431 -0
- package/src/chips/light/bh1750.js +196 -0
- package/src/chips/light/si1132.js +416 -0
- package/src/chips/light/si1133.js +583 -0
- package/src/chips/light/si1145.js +630 -0
- package/src/chips/pressure/mpl3115a2.js +459 -0
- package/src/chips/pressure/ms5607.js +290 -0
- package/src/chips/pressure/ms5611.js +290 -0
- package/src/chips/pressure/spl06.js +461 -0
- package/src/chips/rfid/pn532.js +513 -0
- package/src/connection/i2c.js +46 -13
- package/src/connection/smbus.js +21 -8
- package/src/discovery/registry.js +18 -18
- package/examples/gpio/.gitkeep +0 -0
- package/src/chips/gpio/.gitkeep +0 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Si1133Full } = require('../../../src/chips/light/si1133');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x55', 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=0x55) → Connection
|
|
11
|
+
// Default address 0x55 (AD pulled up); 0x52 with AD pulled down.
|
|
12
|
+
const sensor = new Si1133Full(connection); // Create Si1133 driver, (connection) → void
|
|
13
|
+
// Constructor stores the connection only; no bus traffic yet.
|
|
14
|
+
|
|
15
|
+
async function main() {
|
|
16
|
+
await sensor.init(); // Check PART_ID, reset, UV + white channels, () → void
|
|
17
|
+
// Waits 25 ms, verifies PART_ID 0x33, RESET_SW, channel 0 = UV, channel 1 = white, 24-bit outputs.
|
|
18
|
+
|
|
19
|
+
console.log(await sensor.readId()); // Identity registers, () → {partId, hwId, revId}
|
|
20
|
+
// PART_ID is 0x33; HW_ID bits 4:0 are 0x03 for the Si1133-AA00.
|
|
21
|
+
console.log(await sensor.readResponse()); // RESPONSE0 decoded, () → {state, error, counter}
|
|
22
|
+
// State bits RUNNING/SUSPEND/SLEEP, the CMD_ERR flag and the 4-bit command counter.
|
|
23
|
+
|
|
24
|
+
console.log(await sensor.read()); // Forced measurement, () → {uv: number counts, white: number counts}
|
|
25
|
+
// FORCE, wait for both channels in IRQ_STATUS, decode two signed 24-bit counts.
|
|
26
|
+
console.log(await sensor.readUv()); // UV photodiode once, () → number counts
|
|
27
|
+
// Uses channel 5 as a scratch channel and restores CHAN_LIST afterwards.
|
|
28
|
+
console.log(await sensor.readWhite()); // White photodiode once, () → number counts
|
|
29
|
+
// Same scratch channel, HW_GAIN 7.
|
|
30
|
+
console.log(await sensor.readIr(Si1133Full.MUX_LARGE_IR)); // IR photodiode once, (photodiode) → number counts
|
|
31
|
+
// Small, medium or large IR photodiode; the small one is the default.
|
|
32
|
+
|
|
33
|
+
await sensor.configureChannel(2, Si1133Full.MUX_LARGE_WHITE, { hwGain: 4, swGain: 1, decimRate: 0,
|
|
34
|
+
highSignal: false, bits24: false, postshift: 0 }); // Channel setup, (channel, adcmux, {hwGain, swGain, decimRate, highSignal, bits24, postshift}) → void
|
|
35
|
+
// Writes ADCCONFIG2, ADCSENS2 and ADCPOST2; this channel reports 16-bit unsigned counts.
|
|
36
|
+
await sensor.enableChannels(0x07); // CHAN_LIST, (mask) → void
|
|
37
|
+
// Channels 0, 1 and 2 now run on every FORCE.
|
|
38
|
+
console.log(await sensor.readChannels()); // Forced read of all channels, (force=true) → number[]
|
|
39
|
+
// Results in channel order: 24-bit signed for channels 0/1, 16-bit for channel 2.
|
|
40
|
+
await sensor.force(); // FORCE without waiting, () → void
|
|
41
|
+
// Starts a measurement; the results arrive in HOSTOUT when IRQ_STATUS flags the channels.
|
|
42
|
+
await sleep(100);
|
|
43
|
+
console.log(await sensor.dataReady()); // IRQ_STATUS mask, () → number
|
|
44
|
+
// Bit c set when channel c finished; this read clears the flags.
|
|
45
|
+
console.log(await sensor.readChannels(false)); // Decode current results, (force=false) → number[]
|
|
46
|
+
// No new measurement, only HOSTOUT decoded.
|
|
47
|
+
|
|
48
|
+
await sensor.setThreshold(0, 2000); // Threshold value, (index, value) → void
|
|
49
|
+
// THRESHOLD0 = 2000 counts (16-bit).
|
|
50
|
+
await sensor.setChannelThreshold(0, 1); // Threshold select, (channel, index) → void
|
|
51
|
+
// Channel 0 interrupts only when crossing THRESHOLD0 (index 1); 0 turns it off.
|
|
52
|
+
await sensor.setChannelThreshold(0, 0);
|
|
53
|
+
await sensor.setChannelCounter(2, 2); // Counter for a channel, (channel, index) → void
|
|
54
|
+
// Channel 2 uses MEASCOUNT1 in autonomous mode; 0 keeps a channel forced-only.
|
|
55
|
+
await sensor.startAutonomous(0.1, [10, 5, 1]); // Autonomous mode, (rate s, counters) → void
|
|
56
|
+
// MEASRATE = 0.1 s; channels 0/1 run every 1 s (MEASCOUNT0 = 10), channel 2 every 0.5 s.
|
|
57
|
+
await sleep(1500);
|
|
58
|
+
console.log(await sensor.pollInterrupt()); // IRQ_STATUS mask, () → number
|
|
59
|
+
// Same as dataReady; also releases the INT pin.
|
|
60
|
+
await sensor.pause(); // PAUSE, () → void
|
|
61
|
+
// Stops the autonomous measurements.
|
|
62
|
+
await sensor.setBurst(4); // Burst mode, (count) → void
|
|
63
|
+
// Four readings per burst at MEASRATE; 0 disables burst mode.
|
|
64
|
+
await sensor.setBurst(0);
|
|
65
|
+
|
|
66
|
+
await sensor.writeParameter(0x1E, 3); // PARAM_SET, (address, value) → void
|
|
67
|
+
// Direct parameter-table access: MEASCOUNT2 = 3.
|
|
68
|
+
console.log(await sensor.readParameter(0x1E)); // PARAM_QUERY, (address) → number
|
|
69
|
+
// Reads it back through RESPONSE1.
|
|
70
|
+
|
|
71
|
+
await sensor.disableInterrupt(Si1133Full.SOURCE_CHANNEL2); // IRQ_ENABLE clear, (source) → void
|
|
72
|
+
// Channel 2 no longer drives INT.
|
|
73
|
+
await sensor.enableInterrupt(Si1133Full.SOURCE_CHANNEL2); // IRQ_ENABLE set, (source) → void
|
|
74
|
+
// Channel 2 drives INT again.
|
|
75
|
+
await sensor.onInterrupt((status) => console.log('irq 0x%s', status.toString(16))); // Subscribe, (callback, intPin=null) → void
|
|
76
|
+
// INT falling edge when a pin is wired, otherwise a 10 ms IRQ_STATUS poll.
|
|
77
|
+
await sensor.readChannels();
|
|
78
|
+
await sleep(100);
|
|
79
|
+
await sensor.offInterrupt(); // Unsubscribe, () → void
|
|
80
|
+
// Stops the edge handler or polling timer.
|
|
81
|
+
|
|
82
|
+
await sensor.reset(); // RESET_SW + defaults, () → void
|
|
83
|
+
// Parameter table back to power-up values, then channel 0 = UV, channel 1 = white again.
|
|
84
|
+
// await sensor.setI2cAddress(0x56); // Change address, (address) → void
|
|
85
|
+
// Volatile; the chip answers on the new address at once, so a new connection is needed.
|
|
86
|
+
await connection.close();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Si1133Full } = require('../../../src/chips/light/si1133');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x55', 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=0x55) → Connection
|
|
11
|
+
const sensor = new Si1133Full(connection); // Create Si1133 driver, (connection) → void
|
|
12
|
+
const hex = (v) => '0x' + v.toString(16).padStart(2, '0');
|
|
13
|
+
|
|
14
|
+
async function main() {
|
|
15
|
+
await sensor.init(); // Check PART_ID, reset, UV + white channels, () → void
|
|
16
|
+
|
|
17
|
+
// --- Identity ---
|
|
18
|
+
// PART_ID 0x33 identifies the Si1133; HW_ID and REV_ID give silicon and
|
|
19
|
+
// product revision. RESPONSE0 shows the sequencer state and command counter.
|
|
20
|
+
const id = await sensor.readId(); // Identity registers, () → {partId, hwId, revId}
|
|
21
|
+
console.log('PART_ID %s HW_ID %s REV_ID %s', hex(id.partId), hex(id.hwId), hex(id.revId));
|
|
22
|
+
console.log('response', await sensor.readResponse()); // RESPONSE0 decoded, () → {state, error, counter}
|
|
23
|
+
|
|
24
|
+
// --- UV and daylight monitor: 20 s at 1 Hz ---
|
|
25
|
+
// The counts are raw ADC values; a UV index needs Silicon Labs' calibration
|
|
26
|
+
// for the product's window, so watch the trend.
|
|
27
|
+
for (let i = 0; i < 20; i++) {
|
|
28
|
+
const m = await sensor.read(); // Forced measurement, () → {uv: number counts, white: number counts}
|
|
29
|
+
console.log('%d s UV %d white %d', i, m.uv, m.white);
|
|
30
|
+
await sleep(1000);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// --- Gain settings on a constant light level ---
|
|
34
|
+
// HW_GAIN doubles the integration time per step, SW_GAIN accumulates 2^n
|
|
35
|
+
// samples, HIGH_SIGNAL divides the sensitivity by 14.5 for direct sunlight.
|
|
36
|
+
await sensor.enableChannels(0x04); // CHAN_LIST, (mask) → void
|
|
37
|
+
for (const [hwGain, swGain, highSignal] of [[2, 0, false], [6, 0, false], [6, 2, false], [6, 0, true]]) {
|
|
38
|
+
await sensor.configureChannel(2, Si1133Full.MUX_WHITE, { hwGain, swGain, highSignal }); // Channel setup, (channel, adcmux, {hwGain, swGain, highSignal}) → void
|
|
39
|
+
const [value] = await sensor.readChannels(); // Forced read, () → number[]
|
|
40
|
+
console.log('hwGain %d swGain %d highSignal %s -> %d', hwGain, swGain, highSignal, value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// --- IR photodiodes ---
|
|
44
|
+
for (const [name, mux] of [['small', Si1133Full.MUX_SMALL_IR], ['medium', Si1133Full.MUX_MEDIUM_IR],
|
|
45
|
+
['large', Si1133Full.MUX_LARGE_IR]]) {
|
|
46
|
+
console.log('IR %s %d', name, await sensor.readIr(mux)); // IR photodiode once, (photodiode) → number counts
|
|
47
|
+
}
|
|
48
|
+
console.log('UV %d, white %d', await sensor.readUv(), await sensor.readWhite()); // Scratch-channel reads, () → number counts
|
|
49
|
+
|
|
50
|
+
// --- Autonomous mode with a UV threshold ---
|
|
51
|
+
// Channels 0 and 1 run every second; channel 0 only interrupts when crossing
|
|
52
|
+
// THRESHOLD0, channel 1 on every result.
|
|
53
|
+
await sensor.configureChannel(0, Si1133Full.MUX_UV, { hwGain: 9 }); // Channel setup, (channel, adcmux, {hwGain}) → void
|
|
54
|
+
await sensor.enableChannels(0x03); // CHAN_LIST, (mask) → void
|
|
55
|
+
await sensor.setThreshold(0, 500); // Threshold value, (index, value) → void
|
|
56
|
+
await sensor.setChannelThreshold(0, 1); // Threshold select, (channel, index) → void
|
|
57
|
+
await sensor.setChannelCounter(1, 1); // Counter for a channel, (channel, index) → void
|
|
58
|
+
await sensor.disableInterrupt(Si1133Full.SOURCE_CHANNEL1); // IRQ_ENABLE clear, (source) → void
|
|
59
|
+
await sensor.enableInterrupt(Si1133Full.SOURCE_CHANNEL0); // IRQ_ENABLE set, (source) → void
|
|
60
|
+
await sensor.onInterrupt(async (status) => { // Subscribe, (callback, intPin=null) → void
|
|
61
|
+
console.log('interrupt %s values %j', hex(status), await sensor.readChannels(false));
|
|
62
|
+
});
|
|
63
|
+
await sensor.startAutonomous(0.1, [10, 10, 10]); // Autonomous mode, (rate s, counters) → void
|
|
64
|
+
await sleep(5000);
|
|
65
|
+
await sensor.offInterrupt(); // Unsubscribe, () → void
|
|
66
|
+
await sensor.pause(); // PAUSE, () → void
|
|
67
|
+
await sensor.enableInterrupt(Si1133Full.SOURCE_CHANNEL1); // IRQ_ENABLE set, (source) → void
|
|
68
|
+
|
|
69
|
+
// --- Burst mode: four readings in a row ---
|
|
70
|
+
await sensor.setBurst(4); // Burst mode, (count) → void
|
|
71
|
+
await sensor.force(); // FORCE without waiting, () → void
|
|
72
|
+
await sleep(500);
|
|
73
|
+
console.log('burst ready %s', hex(await sensor.dataReady())); // IRQ_STATUS mask, () → number
|
|
74
|
+
console.log('burst values %j', await sensor.readChannels(false)); // Decode current results, (force=false) → number[]
|
|
75
|
+
await sensor.setBurst(0);
|
|
76
|
+
|
|
77
|
+
// --- Parameter table ---
|
|
78
|
+
await sensor.writeParameter(0x1E, 7); // PARAM_SET, (address, value) → void
|
|
79
|
+
const chan = await sensor.readParameter(0x01); // PARAM_QUERY, (address) → number
|
|
80
|
+
const rate = ((await sensor.readParameter(0x1A)) << 8) | (await sensor.readParameter(0x1B));
|
|
81
|
+
console.log('CHAN_LIST %s MEASRATE %d MEASCOUNT2 %d', hex(chan), rate, await sensor.readParameter(0x1E));
|
|
82
|
+
console.log('poll', await sensor.pollInterrupt()); // IRQ_STATUS mask, () → number
|
|
83
|
+
|
|
84
|
+
// --- Finish ---
|
|
85
|
+
// setI2cAddress would move the chip to another address until the next
|
|
86
|
+
// reset; the demo keeps 0x55 and only shows the call.
|
|
87
|
+
// await sensor.setI2cAddress(0x56); // Change address, (address) → void
|
|
88
|
+
await sensor.reset(); // RESET_SW + defaults, () → void
|
|
89
|
+
console.log('after reset', await sensor.read()); // Forced measurement, () → {uv, white}
|
|
90
|
+
await connection.close();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Si1133Minimal } = require('../../../src/chips/light/si1133');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x55', 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=0x55) → Connection
|
|
11
|
+
const sensor = new Si1133Minimal(connection); // Create Si1133 driver, (connection) → void
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
await sensor.init(); // Check PART_ID, reset, UV + white channels, () → void
|
|
15
|
+
while (true) {
|
|
16
|
+
const m = await sensor.read(); // Forced measurement, () → {uv: number counts, white: number counts}
|
|
17
|
+
console.log('UV %d, white %d', m.uv, m.white);
|
|
18
|
+
await sleep(1000);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Si1145Full } = require('../../../src/chips/light/si1145');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 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=0x60) → Connection
|
|
11
|
+
// The Si1145 has the fixed address 0x60 (older silicon: 0x5A).
|
|
12
|
+
const sensor = new Si1145Full(connection); // Create Si1145 driver, (connection) → void
|
|
13
|
+
// Constructor stores the connection only; no bus traffic yet.
|
|
14
|
+
|
|
15
|
+
async function main() {
|
|
16
|
+
await sensor.init(); // Check PART_ID, HW_KEY, UV + ALS + PS1, () → void
|
|
17
|
+
// Waits 25 ms, verifies PART_ID 0x45/0x46/0x47, writes HW_KEY 0x17, UCOEF, CHLIST 0xB1, LED1 level 3.
|
|
18
|
+
|
|
19
|
+
console.log(await sensor.readId()); // Identity registers, () → {partId, revId, seqId}
|
|
20
|
+
// PART_ID 0x45 for the Si1145; SEQ_ID 0x01 parts swap the MEAS_RATE bytes.
|
|
21
|
+
console.log(await sensor.readResponse()); // RESPONSE decoded, () → {counter, error}
|
|
22
|
+
// 4-bit command counter and the error code (0 = none, 0x8x = ADC overflow / invalid setting).
|
|
23
|
+
|
|
24
|
+
console.log(await sensor.read()); // Forced PS + ALS, () → {proximity, uv_index, visible, infrared}
|
|
25
|
+
// PSALS_FORCE, waits for ALS_INT and PS1_INT; raw counts and the UV index (AUX_DATA / 100).
|
|
26
|
+
console.log(await sensor.readProximity(1)); // Forced PS, (channel=1) → number counts
|
|
27
|
+
// PS1 only; PS2/PS3 exist on the Si1146/47.
|
|
28
|
+
console.log(await sensor.readUvIndex()); // Forced ALS, () → number UV index
|
|
29
|
+
// UV index with the default UV coefficients 7B 6B 01 00.
|
|
30
|
+
console.log(await sensor.readVisible()); // Forced ALS, () → number counts
|
|
31
|
+
// Raw visible ambient light.
|
|
32
|
+
console.log(await sensor.readInfrared()); // Forced ALS, () → number counts
|
|
33
|
+
// Raw infrared ambient light.
|
|
34
|
+
console.log(await sensor.readRaw()); // Data registers, () → {visible, infrared, ps1, ps2, ps3, aux}
|
|
35
|
+
// No new measurement; the latest values of 0x22–0x2D.
|
|
36
|
+
console.log(await sensor.readTemperatureRaw()); // AUX temperature, () → number counts
|
|
37
|
+
// Relative die temperature; CHLIST is switched to EN_AUX and restored.
|
|
38
|
+
console.log(await sensor.readVddRaw()); // AUX VDD, () → number counts
|
|
39
|
+
// Supply voltage on the auxiliary channel.
|
|
40
|
+
|
|
41
|
+
await sensor.setChannels(true, true, true, true); // CHLIST, (uv, visible, infrared, ps1, ps2=false, ps3=false, aux=false) → void
|
|
42
|
+
// UV and AUX share AUX_DATA, so only one of them can be enabled.
|
|
43
|
+
await sensor.setLedCurrent(1, 8); // LED drive, (led, level) → void
|
|
44
|
+
// Level 0 = off … 15 = maximum (~359 mA); keep the gain low at high currents.
|
|
45
|
+
await sensor.setPsLed(1, 0x01); // PS LED select, (channel, leds) → void
|
|
46
|
+
// PS1 drives LED1; the Si1145 has no LED2/LED3.
|
|
47
|
+
await sensor.setPsPhotodiode(1, true); // PS photodiode, (channel, large) → void
|
|
48
|
+
// Large IR photodiode (0x03); false selects the small one.
|
|
49
|
+
await sensor.setGain(0, 0, 0); // ADC gains, (visible=0, infrared=0, proximity=0) → void
|
|
50
|
+
// Integration time × 2^n, recovery counts at the one's complement of the gain.
|
|
51
|
+
await sensor.setHighRange(false, false, false); // Signal range, (visible, infrared, proximity) → void
|
|
52
|
+
// true divides the sensitivity by 14.5 (direct sunlight).
|
|
53
|
+
await sensor.setAlignment(); // Result alignment, ({visible, infrared, ps1, ps2, ps3}) → void
|
|
54
|
+
// true reports the 16 LSBs instead of the 16 MSBs of the ADC result.
|
|
55
|
+
await sensor.setUvCoefficients(Si1145Full.UCOEF_DEFAULT); // UCOEF0..3, (ucoef) → void
|
|
56
|
+
// 7B 6B 01 00 make AUX_DATA the UV index × 100.
|
|
57
|
+
console.log(await sensor.readCalibration()); // GET_CAL, () → number[12]
|
|
58
|
+
// Factory calibration bytes; overwrites the data registers.
|
|
59
|
+
|
|
60
|
+
await sensor.pollInterrupt(); // IRQ_STATUS, () → number sources
|
|
61
|
+
// Reads and clears pending flags.
|
|
62
|
+
await sensor.startAutonomous(0.1); // Autonomous mode, (interval s, proximity=true, als=true) → void
|
|
63
|
+
// MEAS_RATE = 0.1 s / 31.25 µs = 3200, then PSALS_AUTO.
|
|
64
|
+
await sleep(500);
|
|
65
|
+
console.log(await sensor.pollInterrupt()); // IRQ_STATUS, () → number sources
|
|
66
|
+
// SOURCE_ALS | SOURCE_PS1 when both finished at least once.
|
|
67
|
+
await sensor.pause(); // PSALS_PAUSE, () → void
|
|
68
|
+
// Stops the autonomous loop.
|
|
69
|
+
await sensor.force(true, false); // PS_FORCE without waiting, (proximity=true, als=true) → void
|
|
70
|
+
// The result arrives when IRQ_STATUS flags PS1.
|
|
71
|
+
await sleep(50);
|
|
72
|
+
console.log((await sensor.readRaw()).ps1);
|
|
73
|
+
|
|
74
|
+
const events = [];
|
|
75
|
+
await sensor.enableInterrupt(Si1145Full.SOURCE_ALS | Si1145Full.SOURCE_PS1); // INT_CFG + IRQ_ENABLE, (source) → void
|
|
76
|
+
// INT pin (active low) driven for ALS and PS1 completions.
|
|
77
|
+
await sensor.onInterrupt((s) => events.push(s)); // Subscribe, (callback, intPin=null) → void
|
|
78
|
+
// Falling edge on the connection's intPin, otherwise a 10 ms polling timer.
|
|
79
|
+
await sensor.force(); // PSALS_FORCE without waiting, () → void
|
|
80
|
+
await sleep(200);
|
|
81
|
+
await sensor.offInterrupt(); // Unsubscribe, () → void
|
|
82
|
+
console.log(events);
|
|
83
|
+
await sensor.disableInterrupt(Si1145Full.SOURCE_CMD); // IRQ_ENABLE clear, (source) → void
|
|
84
|
+
// CMD_INT is not used by default.
|
|
85
|
+
|
|
86
|
+
await sensor.setI2cAddress(0x60); // I2C_ADDR + BUSADDR, (address) → void
|
|
87
|
+
// Volatile; here the address stays 0x60, otherwise reconnect on the new address.
|
|
88
|
+
await sensor.reset(); // RESET + HW_KEY + defaults, () → void
|
|
89
|
+
// Software reset, 10 ms wait, then the Minimal configuration again.
|
|
90
|
+
await connection.close();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Si1145Full } = require('../../../src/chips/light/si1145');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 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=0x60) → Connection
|
|
11
|
+
const sensor = new Si1145Full(connection); // Create Si1145 driver, (connection) → void
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
await sensor.init(); // Check PART_ID, HW_KEY, UV + ALS + PS1, () → void
|
|
15
|
+
|
|
16
|
+
// --- Identity and calibration ---
|
|
17
|
+
// PART_ID tells the Si1145/46/47 apart (one, two or three LEDs); GET_CAL returns the factory
|
|
18
|
+
// calibration bytes.
|
|
19
|
+
const id = await sensor.readId(); // Identity registers, () → {partId, revId, seqId}
|
|
20
|
+
console.log('PART_ID 0x%s REV_ID 0x%s SEQ_ID 0x%s', id.partId.toString(16), id.revId.toString(16), id.seqId.toString(16));
|
|
21
|
+
console.log('calibration', await sensor.readCalibration()); // GET_CAL, () → number[12]
|
|
22
|
+
console.log('response', await sensor.readResponse()); // RESPONSE, () → {counter, error}
|
|
23
|
+
|
|
24
|
+
// --- Proximity, UV index and light for 10 s ---
|
|
25
|
+
// Proximity counts rise as a hand approaches the LED/sensor window.
|
|
26
|
+
for (let i = 0; i < 10; i++) {
|
|
27
|
+
const m = await sensor.read(); // Forced PS + ALS, () → {proximity, uv_index, visible, infrared}
|
|
28
|
+
console.log('%d s prox %d UV %s vis %d ir %d', i, m.proximity, m.uv_index.toFixed(2), m.visible, m.infrared);
|
|
29
|
+
await sleep(1000);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// --- LED current and photodiode ---
|
|
33
|
+
// Hold a hand ~5 cm above the sensor: higher LED current gives more counts, the small
|
|
34
|
+
// photodiode a fraction of the large one.
|
|
35
|
+
console.log('Hold a hand above the sensor');
|
|
36
|
+
await sleep(3000);
|
|
37
|
+
for (const level of [1, 4, 8, 15]) {
|
|
38
|
+
await sensor.setLedCurrent(1, level); // LED drive, (led, level) → void
|
|
39
|
+
console.log('LED level %d -> %d', level, await sensor.readProximity()); // Forced PS, (channel=1) → number counts
|
|
40
|
+
}
|
|
41
|
+
await sensor.setLedCurrent(1, 3);
|
|
42
|
+
for (const large of [false, true]) {
|
|
43
|
+
await sensor.setPsPhotodiode(1, large); // PS photodiode, (channel, large) → void
|
|
44
|
+
console.log('%s photodiode -> %d', large ? 'large' : 'small', await sensor.readProximity());
|
|
45
|
+
}
|
|
46
|
+
await sensor.setPsLed(1, 0x01); // PS LED select, (channel, leds) → void
|
|
47
|
+
|
|
48
|
+
// --- Gain and high range ---
|
|
49
|
+
// Each gain step doubles the integration time; the high range divides the sensitivity by 14.5
|
|
50
|
+
// for direct sunlight.
|
|
51
|
+
for (const gain of [0, 2, 4]) {
|
|
52
|
+
await sensor.setGain(gain, gain, 0); // ADC gains, (visible, infrared, proximity) → void
|
|
53
|
+
console.log('gain %d vis %d ir %d', gain, await sensor.readVisible(), await sensor.readInfrared()); // Forced ALS, () → number counts
|
|
54
|
+
}
|
|
55
|
+
await sensor.setGain();
|
|
56
|
+
await sensor.setHighRange(true, true, false); // Signal range, (visible, infrared, proximity) → void
|
|
57
|
+
console.log('high range vis %d UV %s', await sensor.readVisible(), (await sensor.readUvIndex()).toFixed(2)); // Forced ALS, () → number
|
|
58
|
+
await sensor.setHighRange(false, false, false);
|
|
59
|
+
await sensor.setAlignment(); // Result alignment, ({...}) → void
|
|
60
|
+
await sensor.setUvCoefficients(Si1145Full.UCOEF_DEFAULT); // UCOEF0..3, (ucoef) → void
|
|
61
|
+
|
|
62
|
+
// --- Autonomous mode with interrupt events and a proximity switch ---
|
|
63
|
+
// PS + ALS every 100 ms; each finished PS1 measurement raises INT (or is polled).
|
|
64
|
+
const THRESHOLD = 2000;
|
|
65
|
+
await sensor.setChannels(true, true, true, true); // CHLIST, (uv, visible, infrared, ps1, ...) → void
|
|
66
|
+
const events = [];
|
|
67
|
+
await sensor.enableInterrupt(Si1145Full.SOURCE_PS1); // INT_CFG + IRQ_ENABLE, (source) → void
|
|
68
|
+
await sensor.onInterrupt((s) => events.push(s)); // Subscribe, (callback, intPin=null) → void
|
|
69
|
+
await sensor.startAutonomous(0.1); // Autonomous mode, (interval s, proximity, als) → void
|
|
70
|
+
let near = false;
|
|
71
|
+
for (let i = 0; i < 50; i++) {
|
|
72
|
+
const ps1 = (await sensor.readRaw()).ps1; // Data registers, () → {...}
|
|
73
|
+
if ((ps1 > THRESHOLD) !== near) {
|
|
74
|
+
near = ps1 > THRESHOLD;
|
|
75
|
+
console.log('output %s (prox %d)', near ? 'ON' : 'OFF', ps1);
|
|
76
|
+
}
|
|
77
|
+
await sleep(100);
|
|
78
|
+
}
|
|
79
|
+
await sensor.pause(); // PSALS_PAUSE, () → void
|
|
80
|
+
await sensor.offInterrupt(); // Unsubscribe, () → void
|
|
81
|
+
console.log('%d interrupt events', events.length);
|
|
82
|
+
await sensor.disableInterrupt(Si1145Full.SOURCE_PS1); // IRQ_ENABLE clear, (source) → void
|
|
83
|
+
await sensor.pollInterrupt(); // IRQ_STATUS, () → number sources
|
|
84
|
+
|
|
85
|
+
// --- Auxiliary channels ---
|
|
86
|
+
console.log('temperature raw %d VDD raw %d', await sensor.readTemperatureRaw(), await sensor.readVddRaw()); // AUX channel, () → number counts
|
|
87
|
+
await sensor.force(false, true); // ALS_FORCE without waiting, (proximity, als) → void
|
|
88
|
+
await sleep(50);
|
|
89
|
+
console.log('raw', await sensor.readRaw()); // Data registers, () → {...}
|
|
90
|
+
|
|
91
|
+
// --- Address and reset ---
|
|
92
|
+
await sensor.setI2cAddress(0x60); // I2C_ADDR + BUSADDR, (address) → void
|
|
93
|
+
await sensor.reset(); // RESET + HW_KEY + defaults, () → void
|
|
94
|
+
console.log('after reset', await sensor.read());
|
|
95
|
+
await connection.close();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Si1145Minimal } = require('../../../src/chips/light/si1145');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 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=0x60) → Connection
|
|
11
|
+
const sensor = new Si1145Minimal(connection); // Create Si1145 driver, (connection) → void
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
await sensor.init(); // Check PART_ID, HW_KEY, UV + ALS + PS1, () → void
|
|
15
|
+
while (true) {
|
|
16
|
+
const m = await sensor.read(); // Forced PS + ALS, () → {proximity, uv_index, visible, infrared}
|
|
17
|
+
console.log('prox %d UV %s vis %d ir %d', m.proximity, m.uv_index.toFixed(2), m.visible, m.infrared);
|
|
18
|
+
await sleep(1000);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { MPL3115A2Full } = require('../../../src/chips/pressure/mpl3115a2');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 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=0x60) → Connection
|
|
11
|
+
const sensor = new MPL3115A2Full(connection); // Create MPL3115A2 driver, (connection) → void
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
await sensor.init(); // Reset and apply defaults, () → void
|
|
15
|
+
console.log('who_am_i 0x%s', (await sensor.whoAmI()).toString(16)); // Read WHO_AM_I, () → number (0xC4)
|
|
16
|
+
await sensor.setOversample(32); // Oversample ratio, (ratio 1/2/4/…/128) → void
|
|
17
|
+
console.log('oversample', sensor.getOversample()); // Current ratio, () → number
|
|
18
|
+
await sensor.setSeaLevelPressure(101325); // Sea-level reference, (pa) → void
|
|
19
|
+
await sensor.setOffsets(0, 0, 0); // Offset trims, (pressurePa, temperatureC, altitudeM) → void
|
|
20
|
+
console.log(await sensor.readPressure(), await sensor.readTemperature()); // One-shot values, () → number Pa / number °C
|
|
21
|
+
console.log(await sensor.read()); // Both together, () → {pressure_pa, temperature_c}
|
|
22
|
+
console.log(await sensor.readAltitude()); // Altimeter reading, () → number m
|
|
23
|
+
await sensor.setMode(false); // Back to barometer, (altimeter) → void
|
|
24
|
+
await sensor.startContinuous(0); // Periodic acquisition, (stepExp 0-15) → void
|
|
25
|
+
console.log('active', await sensor.isActive()); // Check ACTIVE mode, () → boolean
|
|
26
|
+
console.log(await sensor.readContinuous()); // Wait for PTDR and read, () → {value, temperature_c}
|
|
27
|
+
console.log(await sensor.dataReady(), await sensor.readDelta()); // Data ready / change since previous, () → boolean / [number, number]
|
|
28
|
+
await sensor.stopContinuous(); // Back to STANDBY, () → void
|
|
29
|
+
await sensor.setTarget(100000, 200, -5, 3); // Threshold targets, (value, window, temperatureC, temperatureWindow) → void
|
|
30
|
+
await sensor.loadCurrentAsTarget(); // Current output as target, () → void
|
|
31
|
+
console.log(await sensor.readMinMax()); // Captured extremes, () → {p_min, p_max, t_min, t_max}
|
|
32
|
+
await sensor.clearMinMax(); // Clear extremes, () → void
|
|
33
|
+
await sensor.fifoSetup(MPL3115A2Full.FIFO_CIRCULAR, 16); // Configure the FIFO, (mode 0/1/2, watermark 0-63) → void
|
|
34
|
+
console.log(await sensor.fifoStatus()); // FIFO state, () → [overflow, watermark, count]
|
|
35
|
+
console.log(await sensor.fifoRead()); // Drain the FIFO, () → [[value, temperature_c], …]
|
|
36
|
+
await sensor.fifoSetup(MPL3115A2Full.FIFO_OFF); // FIFO off, (mode, watermark = 0) → void
|
|
37
|
+
await sensor.setInterruptPins(false, false, false, false); // INT polarity / drive, (polarity1, openDrain1, polarity2, openDrain2) → void
|
|
38
|
+
await sensor.enableInterrupt(MPL3115A2Full.SOURCE_DRDY, 1); // Route a source to INT1, (source, pin 1/2) → void
|
|
39
|
+
console.log('INT_SOURCE 0x%s', (await sensor.pollInterrupt()).toString(16)); // Read INT_SOURCE, () → number
|
|
40
|
+
await sensor.onInterrupt((status) => console.log('INT 0x%s', status.toString(16)), { pin: 1 }); // Subscribe, (callback, {pin, intPin}) → void
|
|
41
|
+
await sleep(1000);
|
|
42
|
+
await sensor.offInterrupt(); // Unsubscribe, () → void
|
|
43
|
+
await sensor.disableInterrupt(MPL3115A2Full.SOURCE_DRDY); // Disable the source, (source) → void
|
|
44
|
+
await sensor.reset(); // Software reset and defaults, () → void
|
|
45
|
+
await connection.close();
|
|
46
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { MPL3115A2Full } = require('../../../src/chips/pressure/mpl3115a2');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 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=0x60) → Connection
|
|
11
|
+
const sensor = new MPL3115A2Full(connection); // Create MPL3115A2 driver, (connection) → void
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
await sensor.init(); // Reset and apply defaults, () → void
|
|
15
|
+
|
|
16
|
+
// --- Weather and altitude station ---
|
|
17
|
+
// Sets the local sea-level pressure, takes three barometer readings, switches to
|
|
18
|
+
// altimeter mode for three altitude readings, collects 16 samples in the circular
|
|
19
|
+
// FIFO and prints their minimum, maximum and mean, then arms a temperature
|
|
20
|
+
// threshold on INT1 and reports the captured extremes.
|
|
21
|
+
await sensor.setSeaLevelPressure(101325); // Sea-level reference, (pa) → void
|
|
22
|
+
for (let i = 0; i < 3; i++) {
|
|
23
|
+
const m = await sensor.read(); // Read pressure and temperature, () → {pressure_pa, temperature_c}
|
|
24
|
+
console.log('%s Pa %s C', m.pressure_pa.toFixed(2), m.temperature_c.toFixed(2));
|
|
25
|
+
await sleep(5000);
|
|
26
|
+
}
|
|
27
|
+
for (let i = 0; i < 3; i++) {
|
|
28
|
+
console.log('altitude %s m', (await sensor.readAltitude()).toFixed(2)); // Read altitude, () → number m
|
|
29
|
+
}
|
|
30
|
+
await sensor.setMode(false); // Back to barometer, (altimeter) → void
|
|
31
|
+
await sensor.setOversample(16); // Faster conversions, (ratio) → void
|
|
32
|
+
await sensor.fifoSetup(MPL3115A2Full.FIFO_CIRCULAR, 16); // Circular FIFO, watermark 16, (mode, watermark) → void
|
|
33
|
+
await sensor.startContinuous(0); // One sample per second, (stepExp 0-15) → void
|
|
34
|
+
while (!(await sensor.fifoStatus())[1]) await sleep(1000); // Wait for the watermark flag, () → [overflow, watermark, count]
|
|
35
|
+
const samples = await sensor.fifoRead(); // Drain the FIFO, () → [[value Pa, temperature_c °C], …]
|
|
36
|
+
const values = samples.map((s) => s[0]);
|
|
37
|
+
const mean = values.reduce((a, b) => a + b, 0) / values.length;
|
|
38
|
+
console.log('n=%d min %s max %s mean %s Pa', values.length, Math.min(...values).toFixed(2), Math.max(...values).toFixed(2), mean.toFixed(2));
|
|
39
|
+
await sensor.setTarget(0, 0, Math.trunc(samples[samples.length - 1][1]) + 1, 0); // Temperature target, (value, window, temperatureC, temperatureWindow) → void
|
|
40
|
+
await sensor.enableInterrupt(MPL3115A2Full.SOURCE_TTH, 1); // Route temperature threshold to INT1, (source, pin) → void
|
|
41
|
+
console.log('INT_SOURCE 0x%s', (await sensor.pollInterrupt()).toString(16)); // Read INT_SOURCE, () → number
|
|
42
|
+
await sensor.stopContinuous(); // Back to STANDBY, () → void
|
|
43
|
+
await sensor.fifoSetup(MPL3115A2Full.FIFO_OFF); // FIFO off, (mode) → void
|
|
44
|
+
console.log(await sensor.readMinMax()); // Captured extremes, () → {p_min, p_max, t_min, t_max}
|
|
45
|
+
await connection.close();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { MPL3115A2Minimal } = require('../../../src/chips/pressure/mpl3115a2');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x60', 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=0x60) → Connection
|
|
11
|
+
const sensor = new MPL3115A2Minimal(connection); // Create MPL3115A2 driver, (connection) → void
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
await sensor.init(); // Reset and apply defaults, () → void
|
|
15
|
+
while (true) {
|
|
16
|
+
const m = await sensor.read(); // Read pressure and temperature, () → {pressure_pa: number Pa, temperature_c: number °C}
|
|
17
|
+
console.log('%s Pa, %s C', m.pressure_pa.toFixed(2), m.temperature_c.toFixed(2));
|
|
18
|
+
await sleep(2000);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { MS5607Full } = require('../../../src/chips/pressure/ms5607');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x77', 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=0x77) → Connection
|
|
11
|
+
const sensor = new MS5607Full(connection); // Create MS5607 driver, (connection) → void
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
await sensor.init(); // Reset, read PROM, verify CRC-4, () → void
|
|
15
|
+
|
|
16
|
+
console.log(await sensor.read()); // Read temperature and pressure, () → {temperature_c: number °C, pressure_pa: number Pa}
|
|
17
|
+
// OSR 4096, second-order compensation applied
|
|
18
|
+
console.log(await sensor.readPressure()); // Read pressure only, () → number Pa
|
|
19
|
+
console.log(await sensor.readTemperature()); // Read temperature only, () → number °C
|
|
20
|
+
// converts D2 only
|
|
21
|
+
const [d1, d2] = await sensor.readRaw(); // Read raw ADC codes, () → [number, number]
|
|
22
|
+
// [d1 pressure, d2 temperature], 24-bit
|
|
23
|
+
console.log(d1, d2);
|
|
24
|
+
console.log(sensor.compensate(d1, d2)); // Compensate raw codes, (d1, d2, secondOrder=true) → {temperature_c, pressure_pa}
|
|
25
|
+
// pure computation, no bus access
|
|
26
|
+
console.log(sensor.compensate(d1, d2, false));
|
|
27
|
+
sensor.setOversampling(1024); // Select oversampling ratio, (osr) → void
|
|
28
|
+
// 256, 512, 1024, 2048 or 4096
|
|
29
|
+
console.log(sensor.getOversampling()); // Current oversampling ratio, () → number
|
|
30
|
+
console.log(sensor.conversionTime()); // Max conversion time, (osr?) → number s
|
|
31
|
+
await sensor.startConversion(MS5607Full.TEMPERATURE); // Start a conversion, (kind, osr?) → void
|
|
32
|
+
// PRESSURE = D1, TEMPERATURE = D2; non-blocking
|
|
33
|
+
await sleep(sensor.conversionTime() * 1000 + 1);
|
|
34
|
+
console.log(await sensor.readAdc()); // Read the ADC result, () → number
|
|
35
|
+
// only after the conversion time, 24-bit
|
|
36
|
+
sensor.setOversampling(4096);
|
|
37
|
+
console.log(await sensor.readProm()); // Read the PROM words, () → number[8]
|
|
38
|
+
console.log(sensor.coefficients()); // Calibration coefficients, () → [c1 .. c6]
|
|
39
|
+
console.log(sensor.promCrcOk()); // Verify the PROM CRC-4, () → boolean
|
|
40
|
+
console.log(sensor.serialCode()); // Serial code, () → number
|
|
41
|
+
// PROM word 7 bits 15:4
|
|
42
|
+
console.log(await sensor.altitude()); // Altitude from pressure, (seaLevelPa=101325) → number m
|
|
43
|
+
// international barometric formula
|
|
44
|
+
await sensor.reset(); // Reset and reload PROM, () → void
|
|
45
|
+
// 0x1E, 3 ms wait
|
|
46
|
+
connection.close();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
main().catch(err => { console.error(err); process.exit(1); });
|