periph 1.0.0 → 1.1.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 +49 -0
- package/examples/adc_dac/hx711/complete.js +42 -0
- package/examples/adc_dac/hx711/demo.js +35 -0
- package/examples/adc_dac/hx711/minimal.js +16 -0
- package/examples/adc_dac/mcp4728/complete.js +41 -0
- package/examples/adc_dac/mcp4728/demo.js +41 -0
- package/examples/adc_dac/mcp4728/minimal.js +11 -0
- package/examples/display/pcf8576/complete.js +32 -0
- package/examples/display/pcf8576/demo.js +48 -0
- package/examples/display/pcf8576/minimal.js +18 -0
- package/examples/environmental/aht21/complete.js +30 -0
- package/examples/environmental/aht21/demo.js +44 -0
- package/examples/environmental/aht21/minimal.js +12 -0
- package/examples/environmental/bme280/complete.js +34 -0
- package/examples/environmental/bme280/demo.js +67 -0
- package/examples/environmental/bme280/minimal.js +18 -0
- package/examples/environmental/bme680/complete.js +37 -0
- package/examples/environmental/bme680/demo.js +52 -0
- package/examples/environmental/bme680/minimal.js +19 -0
- package/examples/gas/ens160/complete.js +52 -0
- package/examples/gas/ens160/demo.js +52 -0
- package/examples/gas/ens160/minimal.js +27 -0
- package/examples/imu/mpu6050/complete.js +56 -0
- package/examples/imu/mpu6050/demo.js +34 -0
- package/examples/imu/mpu6050/minimal.js +14 -0
- package/examples/light/apds9960/complete.js +62 -0
- package/examples/light/apds9960/demo.js +32 -0
- package/examples/light/apds9960/minimal.js +12 -0
- package/examples/memory/24aa02uid/complete.js +40 -0
- package/examples/memory/24aa02uid/demo.js +40 -0
- package/examples/memory/24aa02uid/minimal.js +12 -0
- package/examples/pressure/bmp280/complete.js +22 -18
- package/examples/pressure/bmp280/demo.js +34 -18
- package/examples/pressure/bmp280/minimal.js +4 -4
- package/package.json +1 -1
- package/src/chips/adc_dac/hx711.js +165 -0
- package/src/chips/adc_dac/mcp4728.js +284 -0
- package/src/chips/display/pcf8576.js +223 -0
- package/src/chips/environmental/aht21.js +170 -0
- package/src/chips/environmental/bme280.js +366 -0
- package/src/chips/environmental/bme680.js +489 -0
- package/src/chips/gas/ens160.js +280 -0
- package/src/chips/imu/mpu6050.js +266 -0
- package/src/chips/light/apds9960.js +464 -0
- package/src/chips/memory/_24aa02uid.js +179 -0
- package/src/chips/pressure/bmp280.js +154 -191
- package/src/transport/hx711.js +105 -0
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/pressure/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/pressure/.gitkeep +0 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
3
|
+
const { BME680Full } = require('../../../src/chips/environmental/bme680');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
|
|
7
|
+
|
|
8
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
9
|
+
|
|
10
|
+
// --- Room air quality probe: 4-in-1 sensor polling with VOC event ---
|
|
11
|
+
// Polls all four sensors once every 5 seconds for 5 minutes (60 ticks).
|
|
12
|
+
// At tick 30, the user is prompted to expose the sensor to a VOC source
|
|
13
|
+
// (isopropyl alcohol, marker pen). Gas resistance drops sharply on exposure
|
|
14
|
+
// and recovers over the remaining ticks, demonstrating raw VOC sensitivity
|
|
15
|
+
// without the closed-source BSEC library.
|
|
16
|
+
const bme = new BME680Full(transport); // Create BME680 driver, (transport)
|
|
17
|
+
bme.configure(BME680Full.OSRS_X2, BME680Full.OSRS_X16, BME680Full.OSRS_X1, BME680Full.MODE_FORCED, BME680Full.FILTER_15); // Configure chip, (osrsT=×2, osrsP=×16, osrsH=×1, mode=forced, filter=15) → undefined
|
|
18
|
+
bme.setHeater(320, 150); // Configure heater profile 0, (tempC=320, durationMs=150) → undefined
|
|
19
|
+
|
|
20
|
+
const temps = [], hums = [], pressures = [], gases = [];
|
|
21
|
+
for (let n = 0; n < 60; n++) {
|
|
22
|
+
if (n === 30) {
|
|
23
|
+
console.log('--- Expose sensor to VOC source now (alcohol/marker) ---');
|
|
24
|
+
}
|
|
25
|
+
const r = bme.readAll(); // Read all sensors in one cycle, () → object
|
|
26
|
+
temps.push(r.temperature);
|
|
27
|
+
pressures.push(r.pressure);
|
|
28
|
+
hums.push(r.humidity);
|
|
29
|
+
if (!isNaN(r.gasResistance)) gases.push(r.gasResistance);
|
|
30
|
+
console.log(`${n}: ${r.temperature.toFixed(1)} C, ${r.humidity.toFixed(1)} %RH, ${r.pressure.toFixed(1)} hPa, ${r.gasResistance.toFixed(0)} Ohm`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function stats(arr) {
|
|
34
|
+
if (!arr.length) return { min: 0, avg: 0, max: 0 };
|
|
35
|
+
const min = Math.min(...arr);
|
|
36
|
+
const max = Math.max(...arr);
|
|
37
|
+
const avg = arr.reduce((s, v) => s + v, 0) / arr.length;
|
|
38
|
+
return { min, avg, max };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const ts = stats(temps);
|
|
42
|
+
const hs = stats(hums);
|
|
43
|
+
const ps = stats(pressures);
|
|
44
|
+
const gs = stats(gases);
|
|
45
|
+
console.log(`T: ${ts.min.toFixed(1)}/${ts.avg.toFixed(1)}/${ts.max.toFixed(1)} C`);
|
|
46
|
+
console.log(`RH: ${hs.min.toFixed(1)}/${hs.avg.toFixed(1)}/${hs.max.toFixed(1)} %`);
|
|
47
|
+
console.log(`P: ${ps.min.toFixed(1)}/${ps.avg.toFixed(1)}/${ps.max.toFixed(1)} hPa`);
|
|
48
|
+
console.log(`R_gas: ${gs.min.toFixed(0)}/${gs.avg.toFixed(0)}/${gs.max.toFixed(0)} Ohm`);
|
|
49
|
+
if (gs.min > 0) console.log(`VOC response ratio: ${(gs.max / gs.min).toFixed(1)}x`);
|
|
50
|
+
|
|
51
|
+
transport.close();
|
|
52
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
3
|
+
const { BME680Minimal } = require('../../../src/chips/environmental/bme680');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
|
|
7
|
+
|
|
8
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
9
|
+
const bme = new BME680Minimal(transport); // Create BME680 driver, (transport)
|
|
10
|
+
|
|
11
|
+
for (let i = 0; i < 5; i++) {
|
|
12
|
+
const t = bme.temperature(); // Read temperature, () → number °C
|
|
13
|
+
const p = bme.pressure(); // Read pressure, () → number hPa
|
|
14
|
+
const h = bme.humidity(); // Read humidity, () → number %RH
|
|
15
|
+
const g = bme.gasResistance(); // Read gas resistance, () → number Ω
|
|
16
|
+
console.log(`${t.toFixed(1)} C, ${p.toFixed(1)} hPa, ${h.toFixed(1)} %RH, ${g.toFixed(0)} Ohm`);
|
|
17
|
+
}
|
|
18
|
+
transport.close();
|
|
19
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CTransport } = require('../../../packages/periph/src/transport/i2c');
|
|
3
|
+
const { ENS160Full } = require('../../../packages/periph/src/chips/gas/ens160');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x52', 16);
|
|
7
|
+
|
|
8
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
9
|
+
const sensor = new ENS160Full(transport); // Create ENS160 driver, (transport)
|
|
10
|
+
|
|
11
|
+
const fw = sensor.getFirmwareVersion(); // Get firmware version, () → object {major, minor, release}
|
|
12
|
+
// switches to IDLE, issues GET_APPVER, returns to STANDARD
|
|
13
|
+
console.log(`Firmware: ${fw.major}.${fw.minor}.${fw.release}`);
|
|
14
|
+
|
|
15
|
+
sensor.setCompensation(25.0, 50.0); // Set compensation, (tempCelsius, rhPercent) → undefined
|
|
16
|
+
// improves accuracy with external T/RH readings
|
|
17
|
+
|
|
18
|
+
sensor.configureInterrupt(true, false, false, true, false); // Configure interrupt, (enabled, activeHigh, pushPull, onData, onGpr) → undefined
|
|
19
|
+
// sets INTn pin behavior for new data notification
|
|
20
|
+
|
|
21
|
+
console.log('Waiting for warm-up...');
|
|
22
|
+
while (true) { // Wait for valid data, () → blocks until warm
|
|
23
|
+
try { sensor.readAirQuality(); break; } catch (e) { _delay(1000); }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const tvoc = sensor.readTvoc(); // Read TVOC, () → number ppb
|
|
27
|
+
const eco2 = sensor.readEco2(); // Read eCO2, () → number ppm
|
|
28
|
+
const aqi = sensor.readAqi(); // Read AQI, () → number 1–5
|
|
29
|
+
const ethanol = sensor.readEthanol(); // Read ethanol, () → number ppb
|
|
30
|
+
// alias of DATA_TVOC at 0x22
|
|
31
|
+
const r1 = sensor.readRawResistance(1); // Read raw resistance, (sensor=1 or 4) → number Ohms
|
|
32
|
+
const r4 = sensor.readRawResistance(4); // Read raw resistance, (sensor=1 or 4) → number Ohms
|
|
33
|
+
const actuals = sensor.readCompensationActuals(); // Read compensation actuals, () → object {tempCelsius, rhPercent}
|
|
34
|
+
// returns T/RH values used by sensor
|
|
35
|
+
|
|
36
|
+
console.log(`TVOC=${tvoc} ppb, eCO2=${eco2} ppm, AQI=${aqi}`);
|
|
37
|
+
console.log(`Ethanol=${ethanol} ppb, R1=${r1.toFixed(0)} Ohm, R4=${r4.toFixed(0)} Ohm`);
|
|
38
|
+
console.log(`Actual T=${actuals.tempCelsius.toFixed(1)} C, RH=${actuals.rhPercent.toFixed(1)} %`);
|
|
39
|
+
|
|
40
|
+
sensor.sleep(); // Enter deep sleep, () → undefined
|
|
41
|
+
// reduces current to ~10 uA
|
|
42
|
+
_delay(1000);
|
|
43
|
+
sensor.wake(); // Wake and resume sensing, () → undefined
|
|
44
|
+
// transitions IDLE then STANDARD
|
|
45
|
+
|
|
46
|
+
transport.close();
|
|
47
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
48
|
+
|
|
49
|
+
function _delay(ms) {
|
|
50
|
+
const start = Date.now();
|
|
51
|
+
while (Date.now() - start < ms) { /* spin */ }
|
|
52
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CTransport } = require('../../../packages/periph/src/transport/i2c');
|
|
3
|
+
const { ENS160Full } = require('../../../packages/periph/src/chips/gas/ens160');
|
|
4
|
+
|
|
5
|
+
const AQI_LABELS = {1: 'Excellent', 2: 'Good', 3: 'Moderate', 4: 'Poor', 5: 'Unhealthy'};
|
|
6
|
+
|
|
7
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
8
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x52', 16);
|
|
9
|
+
|
|
10
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
11
|
+
const sensor = new ENS160Full(transport); // Create ENS160 driver, (transport)
|
|
12
|
+
|
|
13
|
+
// --- Wait for sensor warm-up ---
|
|
14
|
+
// The ENS160 requires ~3 minutes after power-on or idle before VALIDITY_FLAG
|
|
15
|
+
// reaches 0. During warm-up, readings are unreliable. The driver surfaces the
|
|
16
|
+
// status so the application can display progress to the user.
|
|
17
|
+
console.log('Waiting for sensor warm-up...');
|
|
18
|
+
while (true) { // Wait for valid data, () → blocks until warm
|
|
19
|
+
try { sensor.readAirQuality(); break; } catch (e) {
|
|
20
|
+
const s = sensor.status();
|
|
21
|
+
if (s === 1) console.log('Warm-up in progress...');
|
|
22
|
+
else if (s === 2) console.log('Initial start-up (first power-on, up to 1 hour)...');
|
|
23
|
+
else console.log('No valid output');
|
|
24
|
+
_delay(1000);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
console.log('Sensor ready!');
|
|
28
|
+
|
|
29
|
+
// --- Set compensation from external sensor ---
|
|
30
|
+
// If an external temperature/humidity sensor is available, feeding its readings
|
|
31
|
+
// to the ENS160 improves accuracy outside the 20-80%RH range. Here we use a
|
|
32
|
+
// fixed 22C/45%RH as an example.
|
|
33
|
+
sensor.setCompensation(22.0, 45.0); // Set compensation, (tempCelsius=22.0, rhPercent=45.0) → undefined
|
|
34
|
+
|
|
35
|
+
// --- Indoor air quality monitoring loop ---
|
|
36
|
+
// Reads AQI, TVOC, and eCO2 every second and prints a human-readable label.
|
|
37
|
+
// AQI 1-2 is acceptable for occupied spaces; AQI 3+ suggests ventilation.
|
|
38
|
+
for (let n = 0; n < 60; n++) {
|
|
39
|
+
const data = sensor.readAirQuality(); // Read air quality, () → object {aqi, tvocPpb, eco2Ppm}
|
|
40
|
+
const aqi = data.aqi;
|
|
41
|
+
const label = AQI_LABELS[aqi] || 'Unknown';
|
|
42
|
+
console.log(`${n}s: AQI=${aqi} (${label}) TVOC=${data.tvocPpb} ppb eCO2=${data.eco2Ppm} ppm`);
|
|
43
|
+
_delay(1000);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
transport.close();
|
|
47
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
48
|
+
|
|
49
|
+
function _delay(ms) {
|
|
50
|
+
const start = Date.now();
|
|
51
|
+
while (Date.now() - start < ms) { /* spin */ }
|
|
52
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CTransport } = require('../../../packages/periph/src/transport/i2c');
|
|
3
|
+
const { ENS160Minimal } = require('../../../packages/periph/src/chips/gas/ens160');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x52', 16);
|
|
7
|
+
|
|
8
|
+
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
9
|
+
const sensor = new ENS160Minimal(transport); // Create ENS160 driver, (transport)
|
|
10
|
+
|
|
11
|
+
console.log('Waiting for sensor warm-up...');
|
|
12
|
+
while (true) { // Wait for valid data, () → blocks until warm
|
|
13
|
+
try { sensor.readAirQuality(); break; } catch (e) { _delay(1000); }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
for (let i = 0; i < 10; i++) {
|
|
17
|
+
const data = sensor.readAirQuality(); // Read air quality, () → object {aqi, tvocPpb, eco2Ppm}
|
|
18
|
+
console.log(`AQI=${data.aqi} TVOC=${data.tvocPpb} ppb eCO2=${data.eco2Ppm} ppm`);
|
|
19
|
+
_delay(1000);
|
|
20
|
+
}
|
|
21
|
+
transport.close();
|
|
22
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
23
|
+
|
|
24
|
+
function _delay(ms) {
|
|
25
|
+
const start = Date.now();
|
|
26
|
+
while (Date.now() - start < ms) { /* spin */ }
|
|
27
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { MPU6050Full } = require('../../../src/chips/imu/mpu6050');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x68);
|
|
7
|
+
const imu = new MPU6050Full(transport); // Create MPU6050 driver, (transport, addr=0x68) → None
|
|
8
|
+
|
|
9
|
+
const [ax, ay, az] = imu.accel(); // Read 3-axis acceleration, () → [float, float, float] m/s²
|
|
10
|
+
// converts raw accel register to m/s² (16384 LSB/g at ±2g)
|
|
11
|
+
const [gx, gy, gz] = imu.gyro(); // Read 3-axis angular rate, () → [float, float, float] rad/s
|
|
12
|
+
// converts raw gyro register to rad/s (131.0 LSB/(°/s) at ±250dps)
|
|
13
|
+
|
|
14
|
+
imu.configureGyro(1); // Configure gyro range, (fullScale=0) → None
|
|
15
|
+
// sets FS_SEL: 0=±250, 1=±500, 2=±1000, 3=±2000 dps
|
|
16
|
+
imu.configureAccel(1); // Configure accel range, (fullScale=0) → None
|
|
17
|
+
// sets AFS_SEL: 0=±2g, 1=±4g, 2=±8g, 3=±16g
|
|
18
|
+
imu.configureDlpf(3); // Configure DLPF bandwidth, (dlpf=3) → None
|
|
19
|
+
// sets DLPF_CFG: 0=260Hz … 6=5Hz (gyro/accel BW)
|
|
20
|
+
imu.configureSampleRate(4); // Configure sample rate, (divider=4) → None
|
|
21
|
+
// sets SMPLRT_DIV: output rate = 1kHz / (1 + divider)
|
|
22
|
+
|
|
23
|
+
console.log('temp:', imu.temperature()); // Read die temperature, () → float °C
|
|
24
|
+
// converts raw temp register: raw/340 + 36.53
|
|
25
|
+
|
|
26
|
+
const [rax, ray, raz] = imu.accelRaw(); // Read raw accel values, () → [int, int, int]
|
|
27
|
+
// returns raw 16-bit signed accelerometer register values
|
|
28
|
+
const [rgx, rgy, rgz] = imu.gyroRaw(); // Read raw gyro values, () → [int, int, int]
|
|
29
|
+
// returns raw 16-bit signed gyroscope register values
|
|
30
|
+
|
|
31
|
+
console.log('data_ready:', imu.dataReady()); // Check data ready flag, () → bool
|
|
32
|
+
// reads DATA_RDY_INT bit from INT_STATUS register
|
|
33
|
+
|
|
34
|
+
imu.setSleep(true); // Enter sleep mode, (sleep=true) → None
|
|
35
|
+
// sets SLEEP bit in PWR_MGMT_1
|
|
36
|
+
const end1 = Date.now() + 10;
|
|
37
|
+
while (Date.now() < end1) {}
|
|
38
|
+
imu.setSleep(false); // Wake from sleep, (sleep=true) → None
|
|
39
|
+
// clears SLEEP bit in PWR_MGMT_1
|
|
40
|
+
|
|
41
|
+
imu.setStandby(true, false, false, false, false, false); // Set axes standby, (xa, ya, za, xg, yg, zg) → None
|
|
42
|
+
// puts individual axes into low-power standby mode
|
|
43
|
+
imu.setStandby(); // Clear all standby, (xa=false, ...) → None
|
|
44
|
+
// restores all axes from standby
|
|
45
|
+
|
|
46
|
+
imu.enableFifo(true, true, false); // Enable FIFO sources, (gyro=true, accel=true, temp=false) → None
|
|
47
|
+
// configures FIFO_EN and sets FIFO_EN bit in USER_CTRL
|
|
48
|
+
imu.resetFifo(); // Reset FIFO buffer, () → None
|
|
49
|
+
// sets FIFO_RST bit in USER_CTRL to clear the buffer
|
|
50
|
+
console.log('fifo_count:', imu.fifoCount()); // Read FIFO byte count, () → int
|
|
51
|
+
// reads FIFO_COUNTH/L: number of bytes available
|
|
52
|
+
const data = imu.readFifo(); // Read FIFO data, () → Buffer
|
|
53
|
+
// reads all available bytes from FIFO_R_W register
|
|
54
|
+
console.log('fifo read:', data.length, 'bytes');
|
|
55
|
+
|
|
56
|
+
transport.close();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { MPU6050Full } = require('../../../src/chips/imu/mpu6050');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x68);
|
|
7
|
+
|
|
8
|
+
// --- Configure for motion logging with moderate dynamic range ---
|
|
9
|
+
// ±4g captures typical tilting and handling forces without clipping;
|
|
10
|
+
// ±500 dps covers fast rotations while retaining sub-degree resolution.
|
|
11
|
+
const imu = new MPU6050Full(transport); // Create MPU6050 driver, (transport, addr=0x68) → None
|
|
12
|
+
imu.configureAccel(1); // Configure accel range, (fullScale=0) → None
|
|
13
|
+
imu.configureGyro(1); // Configure gyro range, (fullScale=0) → None
|
|
14
|
+
|
|
15
|
+
console.log('roll pitch |accel| |gyro|');
|
|
16
|
+
|
|
17
|
+
setInterval(() => {
|
|
18
|
+
// gate reads on dataReady so each sample reflects a fresh conversion
|
|
19
|
+
while (!imu.dataReady()) {} // Check data ready flag, () → bool
|
|
20
|
+
|
|
21
|
+
const [ax, ay, az] = imu.accel(); // Read 3-axis acceleration, () → [float, float, float] m/s²
|
|
22
|
+
const [gx, gy, gz] = imu.gyro(); // Read 3-axis angular rate, () → [float, float, float] rad/s
|
|
23
|
+
|
|
24
|
+
// --- Compute tilt angles from the accelerometer gravity vector ---
|
|
25
|
+
// roll and pitch are reliable when the device is quasi-static;
|
|
26
|
+
// gyro magnitude indicates how fast the board is being rotated.
|
|
27
|
+
const roll = Math.atan2(ay, az) * 180.0 / Math.PI;
|
|
28
|
+
const pitch = Math.atan2(-ax, Math.sqrt(ay * ay + az * az)) * 180.0 / Math.PI;
|
|
29
|
+
const accelMag = Math.sqrt(ax * ax + ay * ay + az * az);
|
|
30
|
+
const gyroMag = Math.sqrt(gx * gx + gy * gy + gz * gz);
|
|
31
|
+
|
|
32
|
+
console.log(roll.toFixed(1).padEnd(9) + pitch.toFixed(1).padEnd(9) +
|
|
33
|
+
accelMag.toFixed(3).padEnd(11) + gyroMag.toFixed(3));
|
|
34
|
+
}, 100);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { MPU6050Minimal } = require('../../../src/chips/imu/mpu6050');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x68);
|
|
7
|
+
const imu = new MPU6050Minimal(transport); // Create MPU6050 driver, (transport, addr=0x68) → None
|
|
8
|
+
|
|
9
|
+
setInterval(() => {
|
|
10
|
+
const [ax, ay, az] = imu.accel(); // Read 3-axis acceleration, () → [float, float, float] m/s²
|
|
11
|
+
const [gx, gy, gz] = imu.gyro(); // Read 3-axis angular rate, () → [float, float, float] rad/s
|
|
12
|
+
console.log('accel:', ax.toFixed(2), ay.toFixed(2), az.toFixed(2),
|
|
13
|
+
' gyro:', gx.toFixed(2), gy.toFixed(2), gz.toFixed(2));
|
|
14
|
+
}, 100);
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { APDS9960Full } = require('../../../src/chips/light/apds9960');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x39);
|
|
7
|
+
const apds = new APDS9960Full(transport); // Create APDS9960 driver, (transport) → APDS9960Full
|
|
8
|
+
|
|
9
|
+
console.log('0x' + apds.chipId().toString(16)); // Read device ID, () → number
|
|
10
|
+
|
|
11
|
+
const { clear, red, green, blue } = apds.color(); // Read all RGBC channels, () → { clear, red, green, blue }
|
|
12
|
+
// burst read 0x94-0x9B latches all channels atomically
|
|
13
|
+
console.log(apds.colorClear()); // Read clear channel, () → number 0-65535
|
|
14
|
+
console.log(apds.colorRed()); // Read red channel, () → number 0-65535
|
|
15
|
+
console.log(apds.colorGreen()); // Read green channel, () → number 0-65535
|
|
16
|
+
console.log(apds.colorBlue()); // Read blue channel, () → number 0-65535
|
|
17
|
+
|
|
18
|
+
apds.configureAls(0xB6, 1); // Configure ALS, (atime 0-255, again 0-3) → void
|
|
19
|
+
// sets integration time and gain for the ALS/color engine
|
|
20
|
+
apds.configureWait(0xFF, false); // Configure wait, (wtime 0-255, wlong=false) → void
|
|
21
|
+
// sets idle period between measurement cycles
|
|
22
|
+
apds.enableWait(true); // Enable wait engine, (enabled) → void
|
|
23
|
+
|
|
24
|
+
apds.enableProximity(true); // Enable proximity engine, (enabled) → void
|
|
25
|
+
apds.configureProximityLed(0, 0, 0, 1); // Configure proximity LED, (ldrive 0-3, pgain 0-3, ppulse 0-63, pplen 0-3) → void
|
|
26
|
+
// sets LED drive strength, gain, pulse count and length
|
|
27
|
+
apds.setLedBoost(0); // Set LED boost, (boost 0-3) → void
|
|
28
|
+
// multiplies LED current: 0=100%, 1=150%, 2=200%, 3=300%
|
|
29
|
+
console.log(apds.proximity()); // Read proximity count, () → number 0-255
|
|
30
|
+
|
|
31
|
+
apds.alsThreshold(100, 60000); // Set ALS thresholds, (low 0-65535, high 0-65535) → void
|
|
32
|
+
apds.proximityThreshold(10, 200); // Set proximity thresholds, (low 0-255, high 0-255) → void
|
|
33
|
+
apds.setPersistence(0, 1); // Set persistence, (ppers 0-15, apers 0-15) → void
|
|
34
|
+
|
|
35
|
+
apds.enableAlsInterrupt(true); // Enable ALS interrupt, (enabled) → void
|
|
36
|
+
apds.enableProximityInterrupt(true); // Enable proximity interrupt, (enabled) → void
|
|
37
|
+
apds.clearAlsInterrupt(); // Clear ALS interrupt, () → void
|
|
38
|
+
apds.clearProximityInterrupt(); // Clear proximity interrupt, () → void
|
|
39
|
+
apds.clearAllInterrupts(); // Clear all interrupts, () → void
|
|
40
|
+
|
|
41
|
+
apds.setProximityOffset(10, -5); // Set proximity offset, (ur -127..127, dl -127..127) → void
|
|
42
|
+
// sign-magnitude encoding compensates for optical crosstalk
|
|
43
|
+
apds.setProximityMask(false, false, false, false); // Set proximity mask, (u, d, l, r) → void
|
|
44
|
+
|
|
45
|
+
apds.enableGesture(true); // Enable gesture engine, (enabled) → void
|
|
46
|
+
apds.configureGesture(1, 0, 0, 1, 1, 50, 20); // Configure gesture, (ggain, gldrive, gpulse, gplen, gwtime, gpenth, gexth) → void
|
|
47
|
+
// sets gain, LED drive, pulse, wait time, entry/exit thresholds
|
|
48
|
+
console.log(apds.gestureAvailable()); // Check gesture data, () → boolean
|
|
49
|
+
console.log(apds.gestureFifoLevel()); // Read FIFO level, () → number
|
|
50
|
+
console.log(apds.readGestureFifo()); // Read gesture FIFO, () → Array<{u,d,l,r}>
|
|
51
|
+
apds.clearGestureFifo(); // Clear gesture FIFO, () → void
|
|
52
|
+
apds.enableGestureInterrupt(false); // Enable gesture interrupt, (enabled) → void
|
|
53
|
+
apds.enableGesture(false); // Disable gesture engine, (enabled) → void
|
|
54
|
+
|
|
55
|
+
console.log(apds.status()); // Read STATUS register, () → number
|
|
56
|
+
console.log(apds.isAlsValid()); // Check ALS data valid, () → boolean
|
|
57
|
+
console.log(apds.isProximityValid()); // Check proximity valid, () → boolean
|
|
58
|
+
console.log(apds.isAlsSaturated()); // Check ALS saturated, () → boolean
|
|
59
|
+
console.log(apds.isProximitySaturated()); // Check proximity saturated, () → boolean
|
|
60
|
+
|
|
61
|
+
apds.enableProximity(false);
|
|
62
|
+
transport.close();
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { APDS9960Full } = require('../../../src/chips/light/apds9960');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x39);
|
|
7
|
+
const apds = new APDS9960Full(transport); // Create APDS9960 driver, (transport) → APDS9960Full
|
|
8
|
+
|
|
9
|
+
// --- Monitor ambient light with adaptive integration time ---
|
|
10
|
+
// Start with the default 200 ms integration (ATIME=0xB6). When the clear
|
|
11
|
+
// channel approaches saturation (>90% of max count), halve the integration
|
|
12
|
+
// time by doubling ATIME to prevent overflow.
|
|
13
|
+
let atime = 0xB6;
|
|
14
|
+
apds.configureAls(atime, 1); // Configure ALS, (atime 0-255, again 0-3) → void
|
|
15
|
+
|
|
16
|
+
setInterval(() => {
|
|
17
|
+
while (!apds.isAlsValid()) {} // Check ALS data valid, () → boolean
|
|
18
|
+
|
|
19
|
+
const { clear, red, green, blue } = apds.color(); // Read all RGBC channels, () → { clear, red, green, blue }
|
|
20
|
+
const lux = -0.32466 * red + 1.57837 * green + -0.73191 * blue;
|
|
21
|
+
console.log(`C=${clear} R=${red} G=${green} B=${blue} lux~${lux.toFixed(0)}`);
|
|
22
|
+
|
|
23
|
+
// --- Adaptive integration: reduce time when saturated ---
|
|
24
|
+
// At saturation the sensor clips; shortening integration recovers
|
|
25
|
+
// headroom at the cost of reduced sensitivity in low light.
|
|
26
|
+
if (apds.isAlsSaturated() && atime < 0xFE) { // Check ALS saturated, () → boolean
|
|
27
|
+
atime = atime + Math.floor((256 - atime) / 2);
|
|
28
|
+
if (atime > 0xFE) atime = 0xFE;
|
|
29
|
+
apds.configureAls(atime, 1); // Configure ALS, (atime 0-255, again 0-3) → void
|
|
30
|
+
console.log(`[SATURATED — reducing integration time, ATIME=0x${atime.toString(16)}]`);
|
|
31
|
+
}
|
|
32
|
+
}, 1000);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { APDS9960Minimal } = require('../../../src/chips/light/apds9960');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x39);
|
|
7
|
+
const apds = new APDS9960Minimal(transport); // Create APDS9960 driver, (transport) → APDS9960Minimal
|
|
8
|
+
|
|
9
|
+
setInterval(() => {
|
|
10
|
+
const { clear, red, green, blue } = apds.color(); // Read all RGBC channels, () → { clear, red, green, blue }
|
|
11
|
+
console.log(`C=${clear} R=${red} G=${green} B=${blue}`);
|
|
12
|
+
}, 1000);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { EEPROM24AA02UIDFull } = require('../../../src/chips/memory/_24aa02uid');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x50);
|
|
7
|
+
const eeprom = new EEPROM24AA02UIDFull(transport); // Create 24AA02UID driver, (transport, addr=0x50) → void
|
|
8
|
+
|
|
9
|
+
const uid = eeprom.readUid(); // Read 32-bit unique serial number, () → Buffer
|
|
10
|
+
// reads 4 bytes at 0xFC-0xFF
|
|
11
|
+
console.log('UID bytes:', uid.toString('hex').toUpperCase());
|
|
12
|
+
const uidInt = uid.readUInt32BE(0);
|
|
13
|
+
console.log('UID int: ', uidInt);
|
|
14
|
+
|
|
15
|
+
const mfr = eeprom.readManufacturerCode(); // Read manufacturer code, () → number
|
|
16
|
+
// reads 0xFA; expect 0x29 (Microchip)
|
|
17
|
+
const dev = eeprom.readDeviceCode(); // Read device code, () → number
|
|
18
|
+
// reads 0xFB; expect 0x41
|
|
19
|
+
console.log('MFR: 0x' + mfr.toString(16).toUpperCase() + ' DEV: 0x' + dev.toString(16).toUpperCase());
|
|
20
|
+
|
|
21
|
+
const first = eeprom.readByte(0x00); // Read a single byte, (address=0x00-0x7F) → number
|
|
22
|
+
// random read at user EEPROM address
|
|
23
|
+
console.log('First byte: 0x' + first.toString(16).toUpperCase());
|
|
24
|
+
|
|
25
|
+
eeprom.writeByte(0x10, 0xA5); // Write a single byte, (address, value) → void
|
|
26
|
+
// byte write + ACK-poll until complete (max 5 ms)
|
|
27
|
+
const verify = eeprom.readByte(0x10); // Read a single byte, (address=0x00-0x7F) → number
|
|
28
|
+
console.log('Wrote 0xA5, read back: 0x' + verify.toString(16).toUpperCase());
|
|
29
|
+
|
|
30
|
+
const block = eeprom.read(0x20, 8); // Sequential read, (address, length) → Buffer
|
|
31
|
+
// reads 8 bytes starting at address
|
|
32
|
+
console.log('Block @ 0x20: ' + block.toString('hex'));
|
|
33
|
+
|
|
34
|
+
eeprom.writePage(0x40, Buffer.from([0x01, 0x02, 0x03, 0x04])); // Page write, (address, data<=8 bytes) → void
|
|
35
|
+
// writes up to 8 bytes within one page; ACK-polls when done
|
|
36
|
+
eeprom.write(0x44, Buffer.from([0xAA, 0xBB, 0xCC, 0xDD, 0xEE])); // Arbitrary-length write, (address, data) → void
|
|
37
|
+
// splits at 8-byte page boundaries; ACK-polls each chunk
|
|
38
|
+
console.log('Multi-page write complete');
|
|
39
|
+
|
|
40
|
+
transport.close();
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { EEPROM24AA02UIDFull } = require('../../../src/chips/memory/_24aa02uid');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x50);
|
|
7
|
+
const eeprom = new EEPROM24AA02UIDFull(transport); // Create 24AA02UID driver, (transport, addr=0x50) → void
|
|
8
|
+
|
|
9
|
+
// --- Read the chip's factory-programmed 32-bit serial number ---
|
|
10
|
+
// The UID at 0xFC-0xFF never changes and identifies the device
|
|
11
|
+
// across the entire 256-byte address space.
|
|
12
|
+
const uid = eeprom.readUid(); // Read 32-bit unique serial number, () → Buffer
|
|
13
|
+
// reads 4 bytes at 0xFC-0xFF
|
|
14
|
+
console.log('Device UID: 0x' + uid.toString('hex').toUpperCase());
|
|
15
|
+
console.log('Device UID int:', uid.readUInt32BE(0));
|
|
16
|
+
|
|
17
|
+
// --- Maintain a 4-byte boot counter in user EEPROM at 0x00-0x03 ---
|
|
18
|
+
// Read the existing value (or zero on a fresh chip), increment,
|
|
19
|
+
// write back as 4 big-endian bytes. The user EEPROM is rewritable;
|
|
20
|
+
// the UID region above 0x80 is not, so the two stay independent.
|
|
21
|
+
const existing = eeprom.read(0x00, 4); // Sequential read, (address, length) → Buffer
|
|
22
|
+
// reads 4 bytes from user EEPROM
|
|
23
|
+
let counter = existing.readUInt32BE(0);
|
|
24
|
+
counter += 1;
|
|
25
|
+
const updated = Buffer.alloc(4);
|
|
26
|
+
updated.writeUInt32BE(counter, 0);
|
|
27
|
+
eeprom.write(0x00, updated); // Arbitrary-length write, (address, data) → void
|
|
28
|
+
// writes 4 bytes; ACK-polls when done
|
|
29
|
+
console.log('Boot count:', counter);
|
|
30
|
+
|
|
31
|
+
let n = 0;
|
|
32
|
+
setInterval(() => {
|
|
33
|
+
// --- Loop reading the UID only, showing it never changes ---
|
|
34
|
+
// The two distinct areas of the chip (immutable identification
|
|
35
|
+
// above 0x80, rewritable storage below 0x80) are exercised
|
|
36
|
+
// independently.
|
|
37
|
+
const uid = eeprom.readUid(); // Read 32-bit unique serial number, () → Buffer
|
|
38
|
+
console.log('[' + n + '] UID: 0x' + uid.toString('hex').toUpperCase() + ' (counter at user EEPROM 0x00-0x03)');
|
|
39
|
+
n++;
|
|
40
|
+
}, 2000);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CTransport } = require('../../../src/transport/i2c');
|
|
4
|
+
const { EEPROM24AA02UIDMinimal } = require('../../../src/chips/memory/_24aa02uid');
|
|
5
|
+
|
|
6
|
+
const transport = new I2CTransport(1, 0x50);
|
|
7
|
+
const eeprom = new EEPROM24AA02UIDMinimal(transport); // Create 24AA02UID driver, (transport, addr=0x50) → void
|
|
8
|
+
|
|
9
|
+
setInterval(() => {
|
|
10
|
+
const uid = eeprom.readUid(); // Read 32-bit unique serial number, () → Buffer
|
|
11
|
+
console.log('UID:', uid.toString('hex').toUpperCase());
|
|
12
|
+
}, 2000);
|
|
@@ -6,23 +6,27 @@ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
|
6
6
|
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x76', 16);
|
|
7
7
|
|
|
8
8
|
const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
|
|
9
|
-
const bmp = new BMP280Full(transport);
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
const bmp = new BMP280Full(transport); // Create BMP280 driver, (transport, busType='i2c')
|
|
10
|
+
const cid = bmp.chipId(); // Read chip ID, () → number
|
|
11
|
+
// returns 0x58 for BMP280
|
|
12
12
|
console.log('chip_id=' + cid.toString(16));
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
bmp.
|
|
21
|
-
|
|
22
|
-
bmp.
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
|
|
13
|
+
bmp.configure(BMP280Full.OSRS_X1, BMP280Full.OSRS_X1, BMP280Full.MODE_FORCED, BMP280Full.FILTER_OFF, BMP280Full.T_SB_0_5_MS); // Configure chip, (osrsT 0–5, osrsP 0–5, mode 0/1/3, filter 0–4, tSb 0–7) → undefined
|
|
14
|
+
// writes ctrl_meas and config registers
|
|
15
|
+
bmp.setOversampling(BMP280Full.OSRS_X4, BMP280Full.OSRS_X2); // Set oversampling, (osrsT 0–5, osrsP 0–5) → undefined
|
|
16
|
+
// changes conversion time vs resolution trade-off
|
|
17
|
+
bmp.setMode(BMP280Full.MODE_FORCED); // Set power mode, (mode 0/1/3) → undefined
|
|
18
|
+
bmp.setFilter(BMP280Full.FILTER_4); // Set IIR filter, (coeff 0–4) → undefined
|
|
19
|
+
// suppresses short-term pressure disturbances
|
|
20
|
+
bmp.setStandby(BMP280Full.T_SB_125_MS); // Set standby time, (tSb 0–7) → undefined
|
|
21
|
+
// only relevant in normal mode
|
|
22
|
+
const st = bmp.status(); // Read status register, () → number
|
|
23
|
+
const t = bmp.temperature(); // Read temperature, () → number °C
|
|
24
|
+
const p = bmp.pressure(); // Read pressure, () → number hPa
|
|
25
|
+
const alt = bmp.altitude(); // Compute altitude, (seaLevelHpa=1013.25) → number m
|
|
26
|
+
// uses barometric formula to convert pressure to metres
|
|
27
|
+
const slp = bmp.seaLevelPressure(alt); // Compute sea-level pressure, (altitudeM) → number hPa
|
|
28
|
+
bmp.reset(); // Soft reset chip, () → undefined
|
|
29
|
+
// re-reads calibration and re-applies configuration
|
|
30
|
+
console.log(`T=${t.toFixed(1)} C, P=${p.toFixed(1)} hPa, alt=${alt.toFixed(1)} m, slp=${slp.toFixed(1)} hPa`);
|
|
27
31
|
transport.close();
|
|
28
|
-
console.log('===DONE: 0 passed, 0 failed===');
|
|
32
|
+
console.log('===DONE: 0 passed, 0 failed===');
|