periph 1.5.4 → 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.
@@ -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,81 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { SPL06Full } = require('../../../src/chips/pressure/spl06');
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
+ // Default address 0x77 (SDO high or open); 0x76 with SDO low.
12
+ const sensor = new SPL06Full(connection); // Create SPL06 driver, (connection) → void
13
+ // Stores the connection only; no bus traffic yet.
14
+
15
+ async function main() {
16
+ await sensor.init(); // Check ID, read coefficients, apply defaults, () → void
17
+ // ID 0x10, waits for COEF_RDY, 16x pressure / 1x external temperature.
18
+ console.log(sensor.coefficients()); // Decoded calibration, () → {c0, c1, c00, c10, c01, c11, c20, c21, c30}
19
+ // The nine factory coefficients used by every compensation.
20
+ console.log(await sensor.read()); // Temperature and pressure, () → {temperature_c °C, pressure_pa Pa}
21
+ // Command mode: one temperature, then one pressure measurement.
22
+ console.log(await sensor.readPressure()); // Pressure, () → number Pa
23
+ // Measures temperature first, because the compensation needs it.
24
+ console.log(await sensor.readTemperature()); // Temperature, () → number °C
25
+ // One temperature measurement only.
26
+ const [praw, traw] = await sensor.readRaw(); // Raw results, () → [praw, traw]
27
+ // Signed 24-bit values from the result registers.
28
+ console.log(sensor.compensate(praw, traw, 16, 1)); // Compensate raw values, (praw, traw, pOversampling, tOversampling) → {temperature_c, pressure_pa}
29
+ // Pure computation with the stored coefficients; no bus access.
30
+ console.log(await sensor.altitude(101325)); // Altitude, (seaLevelPa=101325) → number m
31
+ // International barometric formula against the given sea-level pressure.
32
+
33
+ await sensor.setPressureConfig(64, 4); // Pressure oversampling and rate, (oversampling=16, rate=1) → void
34
+ // 64x oversampling, 4 background readings per second; sets P_SHIFT.
35
+ await sensor.setTemperatureConfig(2, 4); // Temperature oversampling and rate, (oversampling=1, rate=1) → void
36
+ // 2x oversampling, 4 per second; always keeps the external sensor.
37
+ await sensor.startContinuous(true, true); // Background mode, (pressure=true, temperature=true) → void
38
+ // MEAS_CTRL = 111: the chip measures both on its own.
39
+ console.log(await sensor.readContinuous()); // Latest background result, () → {temperature_c, pressure_pa}
40
+ // Waits for both ready flags; does not start a measurement.
41
+ console.log(await sensor.dataReady()); // Ready flags, () → [pressureReady, temperatureReady]
42
+ // MEAS_CFG.PRS_RDY and TMP_RDY.
43
+ await sensor.stop(); // Standby, () → void
44
+ // MEAS_CTRL = 000.
45
+
46
+ await sensor.enableFifo(); // FIFO on, () → void
47
+ // Background results now queue in the 32-entry FIFO.
48
+ await sensor.startContinuous(); // Background mode, (pressure=true, temperature=true) → void
49
+ // Fill the FIFO for a second.
50
+ await sleep(1000);
51
+ await sensor.stop(); // Standby, () → void
52
+ // Stop adding entries.
53
+ console.log(await sensor.fifoStatus()); // FIFO state, () → [empty, full]
54
+ // FIFO_STS flags.
55
+ console.log(await sensor.readFifo()); // Drain FIFO, () → [{kind, value}]
56
+ // "pressure" in Pa or "temperature" in °C, in measured order.
57
+ await sensor.flushFifo(); // Flush FIFO, () → void
58
+ // Discards any remaining entries.
59
+ await sensor.disableFifo(); // FIFO off, () → void
60
+ // Results go to the result registers again.
61
+
62
+ await sensor.enableInterrupt(SPL06Full.SOURCE_PRESSURE); // Enable interrupt source, (source) → void
63
+ // Pressure-ready interrupt on SDO (active low).
64
+ console.log(await sensor.pollInterrupt()); // Read and clear INT_STS, () → number
65
+ // Bitmask of SOURCE_PRESSURE / SOURCE_TEMPERATURE / SOURCE_FIFO_FULL.
66
+ await sensor.onInterrupt((status) => console.log('INT', status)); // Subscribe, (callback, intPin=null) → void
67
+ // Uses the connection's INT pin, or a 5 ms polling timer without one.
68
+ await sensor.read(); // Trigger a measurement, () → {temperature_c, pressure_pa}
69
+ // The pressure result raises the interrupt.
70
+ await sleep(100);
71
+ await sensor.offInterrupt(); // Unsubscribe, () → void
72
+ // Removes this driver's handler only.
73
+ await sensor.disableInterrupt(SPL06Full.SOURCE_PRESSURE); // Disable interrupt source, (source) → void
74
+ // Clears CFG_REG.INT_PRS.
75
+
76
+ await sensor.reset(); // Soft reset, () → void
77
+ // RESET = 0x09, re-reads the coefficients and restores the init defaults.
78
+ await connection.close();
79
+ }
80
+
81
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,106 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { SPL06Full } = require('../../../src/chips/pressure/spl06');
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 SPL06Full(connection); // Create SPL06 driver, (connection) → void
12
+
13
+ async function main() {
14
+ // --- Identity and calibration ---
15
+ // init() checks ID = 0x10; the nine coefficients are factory-trimmed per
16
+ // part and drive every compensated value below.
17
+ await sensor.init(); // Check ID, read coefficients, apply defaults, () → void
18
+ console.log('ID 0x10 verified', sensor.coefficients()); // Decoded calibration, () → {c0 … c30}
19
+
20
+ // --- Altimeter: 20 s at 1 Hz, altitude relative to the first reading ---
21
+ // The first pressure becomes the reference level, so the altitude column
22
+ // shows how far the sensor moved since start (about 12 Pa per metre).
23
+ const p0 = await sensor.readPressure(); // Reference pressure, () → number Pa
24
+ for (let i = 0; i < 20; i++) {
25
+ const m = await sensor.read(); // Temperature and pressure, () → {temperature_c °C, pressure_pa Pa}
26
+ const t = await sensor.readTemperature(); // Temperature only, () → number °C
27
+ const h = await sensor.altitude(p0); // Altitude vs. reference, (seaLevelPa) → number m
28
+ console.log(`${i} s ${m.temperature_c.toFixed(2)} C (${t.toFixed(2)} C) ${m.pressure_pa.toFixed(2)} Pa ${h.toFixed(2)} m`);
29
+ await sleep(1000);
30
+ }
31
+
32
+ // --- Oversampling: noise and measurement time at 1x, 16x and 128x ---
33
+ // More oversampling means less noise but longer measurements; above 8x
34
+ // the driver sets P_SHIFT automatically.
35
+ for (const osr of [1, 16, 128]) {
36
+ await sensor.setPressureConfig(osr, 1); // Pressure oversampling, (oversampling, rate) → void
37
+ const values = [];
38
+ const start = Date.now();
39
+ for (let j = 0; j < 10; j++) values.push(await sensor.readPressure()); // Pressure, () → number Pa
40
+ const per = (Date.now() - start) / 10;
41
+ const mean = values.reduce((a, b) => a + b, 0) / values.length;
42
+ const sd = Math.sqrt(values.reduce((a, v) => a + (v - mean) ** 2, 0) / values.length);
43
+ console.log(`${osr}x: mean ${mean.toFixed(2)} Pa sd ${sd.toFixed(2)} Pa ${per.toFixed(1)} ms per reading`);
44
+ }
45
+
46
+ // --- Variometer: background mode at 8 Hz ---
47
+ // The chip measures on its own; readContinuous only fetches the latest
48
+ // results, so the host never waits for a conversion it started itself.
49
+ await sensor.setPressureConfig(16, 8); // 16x, 8 Hz, (oversampling, rate) → void
50
+ await sensor.setTemperatureConfig(1, 8); // 1x, 8 Hz, (oversampling, rate) → void
51
+ await sensor.startContinuous(true, true); // Background mode, (pressure, temperature) → void
52
+ let last = null;
53
+ for (let i = 0; i < 16; i++) {
54
+ const c = await sensor.readContinuous(); // Latest results, () → {temperature_c, pressure_pa}
55
+ if (last !== null) console.log(`${c.pressure_pa.toFixed(2)} Pa climb ${(-(c.pressure_pa - last) / 12 * 8).toFixed(2)} m/s`);
56
+ last = c.pressure_pa;
57
+ }
58
+ console.log('ready flags', await sensor.dataReady()); // Ready flags, () → [pressureReady, temperatureReady]
59
+ await sensor.stop(); // Standby, () → void
60
+
61
+ // --- FIFO: let the chip collect 32 results, then read them in one go ---
62
+ // Each entry's LSB marks it as pressure or temperature; the host can sleep
63
+ // while the chip fills the FIFO (32 entries = 2 s at 8 Hz for both).
64
+ await sensor.flushFifo(); // Empty the FIFO, () → void
65
+ await sensor.enableFifo(); // FIFO on, () → void
66
+ await sensor.startContinuous(); // Background mode, (pressure=true, temperature=true) → void
67
+ for (let i = 0; i < 50 && !(await sensor.fifoStatus())[1]; i++) { // Wait for full, () → [empty, full]
68
+ await sleep(100);
69
+ }
70
+ await sensor.stop(); // Standby, () → void
71
+ const entries = await sensor.readFifo(); // Drain FIFO, () → [{kind, value}]
72
+ const pressures = entries.filter((e) => e.kind === 'pressure').map((e) => e.value);
73
+ const pmean = pressures.reduce((a, b) => a + b, 0) / Math.max(pressures.length, 1);
74
+ console.log(`${entries.length} FIFO entries, ${pressures.length} pressure, mean ${pmean.toFixed(2)} Pa`);
75
+ await sensor.disableFifo(); // FIFO off, () → void
76
+
77
+ // --- Data-ready interrupt ---
78
+ // SDO doubles as the interrupt line; the callback receives the INT_STS bits.
79
+ // Without an INT pin on the connection the driver polls INT_STS instead.
80
+ await sensor.enableInterrupt(SPL06Full.SOURCE_PRESSURE); // Pressure-ready interrupt, (source) → void
81
+ await sensor.pollInterrupt(); // Clear stale status, () → number
82
+ await sensor.onInterrupt((status) => console.log(`interrupt 0x${status.toString(16)}`)); // Subscribe, (callback, intPin=null) → void
83
+ await sensor.startContinuous(true, true); // Background mode, (pressure, temperature) → void
84
+ await sleep(2000);
85
+ await sensor.offInterrupt(); // Unsubscribe, () → void
86
+ await sensor.disableInterrupt(SPL06Full.SOURCE_PRESSURE); // Disable source, (source) → void
87
+ await sensor.stop(); // Standby, () → void
88
+
89
+ // --- Compensation by hand ---
90
+ // compensate() turns raw register values into SI units with the stored
91
+ // coefficients; useful for logging raw data and converting it later.
92
+ await sensor.setPressureConfig(16, 1); // Back to 16x, (oversampling, rate) → void
93
+ await sensor.setTemperatureConfig(1, 1); // Back to 1x, (oversampling, rate) → void
94
+ const [praw, traw] = await sensor.readRaw(); // Raw results, () → [praw, traw]
95
+ console.log('raw', praw, traw, '->', sensor.compensate(praw, traw, 16, 1)); // Compensate, (praw, traw, pOversampling, tOversampling) → {temperature_c, pressure_pa}
96
+
97
+ // --- Wrap up ---
98
+ // Standby, empty FIFO and a soft reset leave the chip as after power-up
99
+ // (plus the driver defaults).
100
+ await sensor.stop(); // Standby, () → void
101
+ await sensor.flushFifo(); // Empty the FIFO, () → void
102
+ await sensor.reset(); // Soft reset and defaults, () → void
103
+ await connection.close();
104
+ }
105
+
106
+ 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 { SPL06Minimal } = require('../../../src/chips/pressure/spl06');
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 SPL06Minimal(connection); // Create SPL06 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Check ID, read coefficients, apply defaults, () → void
15
+ while (true) {
16
+ const m = await sensor.read(); // Measure temperature and pressure, () → {temperature_c: number °C, pressure_pa: number Pa}
17
+ console.log('%s Pa, %s C', m.pressure_pa.toFixed(2), m.temperature_c.toFixed(2));
18
+ await sleep(1000);
19
+ }
20
+ }
21
+
22
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,99 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { PN532Full } = require('../../../src/chips/rfid/pn532');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x24', 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=0x24) → Connection
11
+ // The PN532 address is fixed; I0 low and I1 high select the I²C interface.
12
+ const reader = new PN532Full(connection); // Create PN532 driver, (connection) → void
13
+ // Constructor stores the connection only; no bus traffic yet.
14
+
15
+ async function main() {
16
+ await reader.init(); // Wake, check firmware, SAM normal mode, finite retries, () → void
17
+ // GetFirmwareVersion (IC 0x32), SAMConfiguration normal mode, MxRtyPassiveActivation = 2.
18
+
19
+ console.log(await reader.firmwareVersion()); // GetFirmwareVersion, () → {ic, version, revision, support, iso14443a, iso14443b, iso18092}
20
+ // IC 0x32, firmware 1.6 on current parts; the support bits list the protocols.
21
+ console.log(await reader.generalStatus()); // GetGeneralStatus, () → {error, field, targets, samStatus}
22
+ // Last error code, external field detected, active targets, SAM status.
23
+ console.log(await reader.selfTest()); // Diagnose, (test=0x00) → boolean
24
+ // Communication line test: the chip echoes an 8-byte pattern.
25
+ console.log(await reader.selfTest(0x01)); // Diagnose, (test=0x01) → boolean
26
+ // ROM checksum test.
27
+
28
+ console.log('UID', await reader.readUid()); // Detect a type A card, (timeout=1.0 s) → Buffer | null
29
+ // InListPassiveTarget at 106 kbps type A, then InRelease.
30
+
31
+ const targets = await reader.listPassiveTargets({ baud: PN532Full.BAUD_106_A, maxTargets: 1 }); // InListPassiveTarget, ({baud=BAUD_106_A, maxTargets=1, initiatorData=[], timeout=1.0 s}) → Target[]
32
+ // Keeps the target active; target.tg is the logical number for the next commands.
33
+ if (targets.length) {
34
+ const t = targets[0];
35
+ console.log(t);
36
+ await reader.deselect(t.tg); // InDeselect, (target=0) → void
37
+ // Puts the card to sleep but keeps its information in the PN532.
38
+ await reader.select(t.tg); // InSelect, (target=0) → void
39
+ // Wakes and reselects the deselected card.
40
+ if (t.uid.length === 7) {
41
+ console.log(await reader.ntagReadPage(t.tg, 4)); // NTAG READ, (target, page) → Buffer (16)
42
+ // Returns pages 4..7 (16 bytes).
43
+ await reader.ntagWritePage(t.tg, 4, [1, 2, 3, 4]); // NTAG WRITE, (target, page, data 4 bytes) → void
44
+ // Writes one 4-byte user page.
45
+ } else {
46
+ await reader.mifareAuthenticate(t.tg, 4, Buffer.alloc(6, 0xFF), t.uid); // MIFARE auth, (target, block, key 6 bytes, uid, keyB=false) → void
47
+ // Key A authentication of sector 1 with the transport key.
48
+ const data = await reader.mifareReadBlock(t.tg, 4); // MIFARE READ, (target, block) → Buffer (16)
49
+ // Reads block 4.
50
+ await reader.mifareWriteBlock(t.tg, 4, data); // MIFARE WRITE, (target, block, data 16 bytes) → void
51
+ // Writes the same 16 bytes back.
52
+ }
53
+ console.log(await reader.dataExchange(t.tg, [0x30, 0x00])); // InDataExchange, (target, data) → Buffer
54
+ // Raw READ of block/page 0 with PN532 framing and CRC.
55
+ await reader.release(t.tg); // InRelease, (target=0) → void
56
+ // Ends the session with the card.
57
+ }
58
+
59
+ await reader.setRfField(false); // RFConfiguration item 1, (on, autoRfca=false) → void
60
+ // RF field off; a card in the field loses power.
61
+ await reader.setRfField(true); // RFConfiguration item 1, (on, autoRfca=false) → void
62
+ // RF field back on.
63
+ await reader.setMaxRetries({ passiveActivation: 2 }); // RFConfiguration item 5, ({passiveActivation=2, atr=0xFF, psl=1}) → void
64
+ // 0xFF would retry forever.
65
+ const txMode = await reader.readRegister(0x6302); // ReadRegister, (address) → number
66
+ // 0x6302 is CIU_TxMode in the PN532's SFR space.
67
+ await reader.writeRegister(0x6302, txMode); // WriteRegister, (address, value) → void
68
+ // Writes the value back unchanged.
69
+ console.log(await reader.sendCommand(0x02)); // Raw command, (command, data=[], timeout=1.0 s) → Buffer
70
+ // GetFirmwareVersion through the raw frame interface.
71
+ try {
72
+ console.log(await reader.communicateThru([0x26])); // InCommunicateThru, (data) → Buffer
73
+ // Raw RF exchange with the current target; fails without one.
74
+ } catch (e) {
75
+ console.log('communicateThru:', e.message);
76
+ }
77
+
78
+ await reader.startCommand(0x02); // Command without waiting, (command, data=[]) → void
79
+ // Writes the frame and reads the ACK only.
80
+ console.log(await reader.waitReady(1.0)); // Wait for a frame, (timeout s) → boolean
81
+ // Polls the status byte every 1 ms.
82
+ console.log(await reader.pollInterrupt()); // Frame ready?, () → boolean
83
+ // A single status-byte read.
84
+ console.log(await reader.readResponse(0x02)); // Read the pending response, (command, timeout=1.0 s) → Buffer
85
+ // Header, NACK, full frame, checksums.
86
+
87
+ await reader.onInterrupt(() => console.log('frame ready')); // Subscribe to IRQ, (callback, intPin=null) → void
88
+ // Falling edge on IRQ, or a 10 ms status-byte poll without an IRQ pin.
89
+ await reader.sendCommand(0x02); // Raw command, (command, data=[], timeout=1.0 s) → Buffer
90
+ await sleep(100);
91
+ await reader.offInterrupt(); // Unsubscribe, () → void
92
+ // Stops the edge handler or the polling timer.
93
+
94
+ await reader.powerDown(); // PowerDown, (wakeupSources=0x20) → void
95
+ // Sleeps until the next I²C access; the driver retries the wake-up write.
96
+ connection.close();
97
+ }
98
+
99
+ main().catch(err => { console.error(err); process.exit(1); });