periph 1.3.2 → 1.3.3
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/accelerometer/bma180/complete.js +83 -0
- package/examples/accelerometer/bma180/demo.js +47 -0
- package/examples/accelerometer/bma180/minimal.js +21 -0
- package/examples/accelerometer/fxos8700/complete.js +111 -0
- package/examples/accelerometer/fxos8700/demo.js +80 -0
- package/examples/accelerometer/fxos8700/minimal.js +20 -0
- package/examples/accelerometer/lis3dh/complete.js +123 -0
- package/examples/accelerometer/lis3dh/demo.js +99 -0
- package/examples/accelerometer/lis3dh/minimal.js +19 -0
- package/examples/accelerometer/lsm303/complete.js +82 -0
- package/examples/accelerometer/lsm303/demo.js +96 -0
- package/examples/accelerometer/lsm303/minimal.js +23 -0
- package/examples/accelerometer/mma8451q/complete.js +96 -0
- package/examples/accelerometer/mma8451q/demo.js +120 -0
- package/examples/accelerometer/mma8451q/minimal.js +20 -0
- package/examples/adc_dac/ad5243/complete.js +52 -0
- package/examples/adc_dac/ad5243/demo.js +50 -0
- package/examples/adc_dac/ad5243/minimal.js +23 -0
- package/examples/adc_dac/ad5248/complete.js +49 -0
- package/examples/adc_dac/ad5248/demo.js +42 -0
- package/examples/adc_dac/ad5248/minimal.js +21 -0
- package/examples/adc_dac/ads1013/complete.js +51 -0
- package/examples/adc_dac/ads1013/demo.js +51 -0
- package/examples/adc_dac/ads1013/minimal.js +21 -0
- package/examples/adc_dac/ads1014/complete.js +88 -0
- package/examples/adc_dac/ads1014/demo.js +52 -0
- package/examples/adc_dac/ads1014/minimal.js +21 -0
- package/examples/adc_dac/ads1113/complete.js +47 -0
- package/examples/adc_dac/ads1113/demo.js +39 -0
- package/examples/adc_dac/ads1113/minimal.js +20 -0
- package/examples/adc_dac/ads1114/complete.js +77 -0
- package/examples/adc_dac/ads1114/demo.js +47 -0
- package/examples/adc_dac/ads1114/minimal.js +20 -0
- package/examples/adc_dac/ads1115/complete.js +82 -0
- package/examples/adc_dac/ads1115/demo.js +55 -0
- package/examples/adc_dac/ads1115/minimal.js +20 -0
- package/examples/display/pcf8578/complete.js +58 -0
- package/examples/display/pcf8578/demo.js +62 -0
- package/examples/display/pcf8578/minimal.js +19 -0
- package/examples/display/pcf8579/complete.js +58 -0
- package/examples/display/pcf8579/demo.js +61 -0
- package/examples/display/pcf8579/minimal.js +19 -0
- package/examples/imu/icm20948/complete.js +114 -0
- package/examples/imu/icm20948/demo.js +79 -0
- package/examples/imu/icm20948/minimal.js +22 -0
- package/examples/io_expander/mcp23008/complete.js +70 -0
- package/examples/io_expander/mcp23008/demo.js +58 -0
- package/examples/io_expander/mcp23008/minimal.js +20 -0
- package/examples/memory/mb85rc/complete.js +40 -0
- package/examples/memory/mb85rc/demo.js +58 -0
- package/examples/memory/mb85rc/minimal.js +15 -0
- package/examples/motor/drv2605/complete.js +100 -0
- package/examples/motor/drv2605/demo.js +55 -0
- package/examples/motor/drv2605/minimal.js +24 -0
- package/package.json +1 -1
- package/src/chips/accelerometer/bma180.js +573 -0
- package/src/chips/accelerometer/fxos8700.js +1600 -0
- package/src/chips/accelerometer/lis3dh.js +1341 -0
- package/src/chips/accelerometer/lsm303.js +1471 -0
- package/src/chips/accelerometer/mma8451q.js +1406 -0
- package/src/chips/adc_dac/_ads111x_base.js +272 -0
- package/src/chips/adc_dac/ad5243.js +231 -0
- package/src/chips/adc_dac/ad5248.js +194 -0
- package/src/chips/adc_dac/ads1013.js +219 -0
- package/src/chips/adc_dac/ads1014.js +509 -0
- package/src/chips/adc_dac/ads1113.js +112 -0
- package/src/chips/adc_dac/ads1114.js +201 -0
- package/src/chips/adc_dac/ads1115.js +232 -0
- package/src/chips/display/pcf8578.js +452 -0
- package/src/chips/display/pcf8579.js +451 -0
- package/src/chips/imu/icm20948.js +849 -0
- package/src/chips/io_expander/mcp23008.js +558 -0
- package/src/chips/memory/mb85rc.js +166 -0
- package/src/chips/motor/drv2605.js +592 -0
- package/src/discovery/registry.js +20 -19
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { LIS3DHFull, LIS3DHSource } = require('periph/src/chips/accelerometer/lis3dh');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x18', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const accel = new LIS3DHFull(connection);
|
|
11
|
+
|
|
12
|
+
// --- Configure the sensor for tilt, double-tap and free-fall detection ---
|
|
13
|
+
// ±2 g in high-resolution mode gives 1 mg/LSB, enough resolution for the
|
|
14
|
+
// pitch/roll estimate. 50 Hz halves the power budget compared to the 100 Hz
|
|
15
|
+
// default and still samples a double tap reliably. The self-test is run
|
|
16
|
+
// once at start-up to verify the MEMS elements and the sign of the axes.
|
|
17
|
+
async function setup() {
|
|
18
|
+
await accel.setRange(2); // Set full-scale range, (rangeG) → None
|
|
19
|
+
// ±2 g, i.e. 1 mg/LSB in high-resolution mode
|
|
20
|
+
await accel.setMode(2); // Set operating mode, (mode) → None
|
|
21
|
+
// 2 = high-resolution (12-bit)
|
|
22
|
+
await accel.setDataRate(50); // Set output data rate, (rateHz) → None
|
|
23
|
+
// 50 Hz
|
|
24
|
+
const st = await accel.selfTest(1); // Run self-test, (mode) → bool
|
|
25
|
+
// electrostatic self-test 0; the result is printed once
|
|
26
|
+
console.log(`self-test: ${st ? 'PASS' : 'FAIL'}`);
|
|
27
|
+
|
|
28
|
+
// A double tap on Z is detected with a 0.8 g threshold; the 200 ms
|
|
29
|
+
// window is wide enough for two separate taps but rejects a shake.
|
|
30
|
+
await accel.setClick(0, 0x04, 0.8, 20, 40, 200, true);
|
|
31
|
+
// Configure click detection, (single, double, thresholdG, timeLimitMs, latencyMs, windowMs, latch) → None
|
|
32
|
+
// double click on Z, latched until CLICK_SRC is read
|
|
33
|
+
// Free fall: all three axes below 0.35 g for 60 ms.
|
|
34
|
+
await accel.setFreeFall(1, 0.35, 60); // Configure free fall, (generator, thresholdG, durationMs) → None
|
|
35
|
+
// generator 1, 0.35 g threshold, 60 ms duration
|
|
36
|
+
await accel.setLatch(1, true); // Latch generator interrupt, (generator, enabled) → None
|
|
37
|
+
// the free-fall event stays latched until INT1_SRC is read
|
|
38
|
+
await accel.enableInterrupt(LIS3DHSource.SOURCE_CLICK, 1);
|
|
39
|
+
// Route interrupt source, (source, pin) → None
|
|
40
|
+
// click events on INT1
|
|
41
|
+
await accel.enableInterrupt(LIS3DHSource.SOURCE_IA1, 1);
|
|
42
|
+
// Route interrupt source, (source, pin) → None
|
|
43
|
+
// generator 1 (free fall) on INT1
|
|
44
|
+
// Stream mode keeps the last 32 samples; the watermark is 16, so the
|
|
45
|
+
// buffer is drained twice per second at 50 Hz.
|
|
46
|
+
await accel.setFifo(2, 16, false); // Configure FIFO, (mode, watermark, triggerInt2) → None
|
|
47
|
+
// mode 2 = stream, 16-sample watermark
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// --- 60-second tilt / double-tap / free-fall monitor ---
|
|
51
|
+
// Every 100 ms the FIFO is drained and the samples are averaged, which
|
|
52
|
+
// removes most of the quantization noise. The pitch and roll are derived
|
|
53
|
+
// from the averaged vector with atan2(): the pitch is the rotation in the
|
|
54
|
+
// X-Z plane, the roll the rotation around the forward axis. The vector
|
|
55
|
+
// magnitude stays near 1 g while the board is tilted, and drops towards
|
|
56
|
+
// 0 g during a free fall. The user is expected to tilt the board, double
|
|
57
|
+
// tap it and shake it during the 60 s window.
|
|
58
|
+
(async () => {
|
|
59
|
+
await setup();
|
|
60
|
+
const start = Date.now();
|
|
61
|
+
let lastLoop = 0;
|
|
62
|
+
|
|
63
|
+
while (Date.now() - start < 60000) {
|
|
64
|
+
const now = Date.now();
|
|
65
|
+
if (now - lastLoop >= 100) {
|
|
66
|
+
lastLoop = now;
|
|
67
|
+
const level = await accel.fifoLevel(); // Read FIFO level, () → count
|
|
68
|
+
// number of buffered sample sets
|
|
69
|
+
const samples = await accel.readFifo(); // Drain the FIFO, (count) → [{x, y, z}]
|
|
70
|
+
// reads every buffered sample set
|
|
71
|
+
if (samples.length > 0) {
|
|
72
|
+
let sx = 0, sy = 0, sz = 0;
|
|
73
|
+
for (const s of samples) { sx += s.x; sy += s.y; sz += s.z; }
|
|
74
|
+
const x = sx / samples.length;
|
|
75
|
+
const y = sy / samples.length;
|
|
76
|
+
const z = sz / samples.length;
|
|
77
|
+
const pitch = Math.atan2(x, z) * 180.0 / Math.PI;
|
|
78
|
+
const roll = Math.atan2(y, Math.hypot(x, z)) * 180.0 / Math.PI;
|
|
79
|
+
const mag = Math.hypot(x, y, z);
|
|
80
|
+
const temp = await accel.readTemperatureDelta();
|
|
81
|
+
// Read relative temperature, () → °C
|
|
82
|
+
// 1 digit/°C relative to the power-on reference
|
|
83
|
+
console.log(`${String(Math.floor((now - start) / 1000)).padStart(5)} n=${samples.length} (level ${level}) pitch=${pitch.toFixed(1).padStart(7)}° roll=${roll.toFixed(1).padStart(7)}° |a|=${mag.toFixed(3)} g T=${temp} °C`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const status = await accel.pollInterrupt(); // Poll interrupt status, () → int
|
|
87
|
+
// combined status of every enabled source
|
|
88
|
+
if (status & LIS3DHSource.SOURCE_CLICK) {
|
|
89
|
+
console.log(`${String(Math.floor((now - start) / 1000)).padStart(5)} DOUBLE TAP`);
|
|
90
|
+
}
|
|
91
|
+
if (status & LIS3DHSource.SOURCE_IA1) {
|
|
92
|
+
console.log(`${String(Math.floor((now - start) / 1000)).padStart(5)} FREE FALL`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
await new Promise(r => setTimeout(r, 10));
|
|
96
|
+
}
|
|
97
|
+
console.log('done');
|
|
98
|
+
await connection.close();
|
|
99
|
+
})();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { LIS3DHMinimal } = require('periph/src/chips/accelerometer/lis3dh');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x18', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const accel = new LIS3DHMinimal(connection); // Create LIS3DH driver, (connection)
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
for (let i = 0; i < 10; i++) {
|
|
14
|
+
const { x, y, z } = await accel.read(); // Read 3-axis acceleration, () → {x, y, z} g
|
|
15
|
+
console.log(`x=${x.toFixed(3)} y=${y.toFixed(3)} z=${z.toFixed(3)} g`);
|
|
16
|
+
await new Promise(r => setTimeout(r, 100));
|
|
17
|
+
}
|
|
18
|
+
await connection.close();
|
|
19
|
+
})();
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { LSM303Full } = require('periph/src/chips/accelerometer/lsm303');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
|
|
8
|
+
const accelConnection = new I2CConnection(I2C_BUS, 0x19); // Open I²C bus, (busNumber, addr=0x19) → I2CConnection
|
|
9
|
+
const magConnection = new I2CConnection(I2C_BUS, 0x1E); // Open I²C bus, (busNumber, addr=0x1E) → I2CConnection
|
|
10
|
+
const compass = new LSM303Full(accelConnection, magConnection); // Create LSM303 driver, (accelConnection, magConnection) → LSM303Full
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
// --- Accelerometer readings ---
|
|
14
|
+
const accel = await compass.readAcceleration(); // Read acceleration, () → Promise<{x:float, y:float, z:float}> g
|
|
15
|
+
console.log(`accel x=${accel.x.toFixed(4)} y=${accel.y.toFixed(4)} z=${accel.z.toFixed(4)} g`);
|
|
16
|
+
|
|
17
|
+
const rawAccel = await compass.readAccelerationRaw(); // Read raw acceleration, () → Promise<{x:int, y:int, z:int}> counts
|
|
18
|
+
console.log(`raw accel ${rawAccel.x} ${rawAccel.y} ${rawAccel.z}`);
|
|
19
|
+
|
|
20
|
+
const status = await compass.readStatus(); // Read status register, () → Promise<number> byte
|
|
21
|
+
console.log(`status 0x${status.toString(16)}`);
|
|
22
|
+
|
|
23
|
+
console.log(`new data ${await compass.newDataAvailable() ? 1 : 0}`); // Check new data, () → Promise<boolean>
|
|
24
|
+
|
|
25
|
+
// --- Accelerometer configuration ---
|
|
26
|
+
await compass.setRange(4); // Set full-scale range, (rangeG=4) → Promise<boolean>
|
|
27
|
+
await compass.setDataRate(50); // Set output data rate, (rateHz=50) → Promise<boolean>
|
|
28
|
+
await compass.setMode(LSM303Full.MODE_HIGH_RESOLUTION); // Set operating mode, (mode=MODE_HIGH_RESOLUTION=2) → Promise<boolean>
|
|
29
|
+
await compass.setAxes(true, true, true); // Enable axes, (x=true, y=true, z=true) → Promise<void>
|
|
30
|
+
|
|
31
|
+
// --- High-pass filter ---
|
|
32
|
+
await compass.setHighPass(0, 0, false, true, false, false); // Configure high-pass filter, (mode=0, cutoff=0, filterOutput=false, onIa1=true, onIa2=false, onClick=false) → Promise<boolean>
|
|
33
|
+
console.log(`reference ${await compass.resetHighPass()}`); // Reset high-pass filter, () → Promise<number>
|
|
34
|
+
await compass.setReference(0); // Set reference value, (value=0) → Promise<void>
|
|
35
|
+
|
|
36
|
+
// --- FIFO ---
|
|
37
|
+
await compass.setFifo(LSM303Full.FIFO_STREAM, 16); // Configure FIFO, (mode=FIFO_STREAM=2, watermark=16, triggerInt2=false) → Promise<boolean>
|
|
38
|
+
console.log(`fifo level ${await compass.fifoLevel()}`); // Read FIFO level, () → Promise<number>
|
|
39
|
+
const fifo = await compass.readFifoStatus(); // Read FIFO status, () → Promise<{watermark:bool, overrun:bool, empty:bool, level:int}>
|
|
40
|
+
console.log(`fifo wm=${fifo.watermark} ov=${fifo.overrun} empty=${fifo.empty} level=${fifo.level}`);
|
|
41
|
+
const samples = await compass.readFifo(8); // Read FIFO, (count=8) → Promise<Array<{x,y,z}>>
|
|
42
|
+
console.log(`fifo samples ${samples.length}`);
|
|
43
|
+
|
|
44
|
+
// --- Inertial interrupt generators ---
|
|
45
|
+
await compass.setMotionInterrupt(1, 0x02, 0.5, 10, false); // Configure motion interrupt, (generator=1, events=0x02, thresholdG=0.5, durationMs=10, andMode=false) → Promise<boolean>
|
|
46
|
+
await compass.setFreeFall(1, 0.35, 30); // Arm free-fall, (generator=1, thresholdG=0.35, durationMs=30) → Promise<boolean>
|
|
47
|
+
await compass.setOrientation(1, false, false); // Configure orientation, (generator=1, position=false, fourD=false) → Promise<boolean>
|
|
48
|
+
await compass.setLatch(1, true); // Latch the interrupt, (generator=1, enabled=true) → Promise<boolean>
|
|
49
|
+
console.log(`int1 source 0x${(await compass.readInterruptSource(1)).toString(16)}`); // Read interrupt source, (generator=1) → Promise<number>
|
|
50
|
+
|
|
51
|
+
// --- Click detection ---
|
|
52
|
+
await compass.setClick(0, 0x04, 0.8, 20, 40, 200); // Configure click detector, (single=0, double=0x04, thresholdG=0.8, timeLimitMs=20, latencyMs=40, windowMs=200) → Promise<boolean>
|
|
53
|
+
console.log(`click source 0x${(await compass.readClickSource()).toString(16)}`); // Read click source, () → Promise<number>
|
|
54
|
+
|
|
55
|
+
// --- Interrupt routing ---
|
|
56
|
+
await compass.setInterruptPolarity(false); // Set interrupt polarity, (activeLow=false) → Promise<void>
|
|
57
|
+
await compass.enableInterrupt(LSM303Full.SOURCE_CLICK, 1); // Route an interrupt, (source=SOURCE_CLICK=0x20, pin=1) → Promise<boolean>
|
|
58
|
+
await compass.disableInterrupt(LSM303Full.SOURCE_CLICK, 1); // Remove an interrupt, (source=SOURCE_CLICK=0x20, pin=1) → Promise<boolean>
|
|
59
|
+
|
|
60
|
+
// --- Magnetometer ---
|
|
61
|
+
await compass.setMagRange(250); // Set magnetometer range, (rangeUt=250) → Promise<boolean>
|
|
62
|
+
await compass.setMagDataRate(30); // Set magnetometer data rate, (rateHz=30) → Promise<boolean>
|
|
63
|
+
await compass.setMagMode(LSM303Full.MAG_MODE_CONTINUOUS); // Set magnetometer mode, (mode=MAG_MODE_CONTINUOUS=0) → Promise<boolean>
|
|
64
|
+
const rawMag = await compass.readMagneticRaw(); // Read raw magnetic field, () → Promise<{x:int, y:int, z:int}> counts
|
|
65
|
+
console.log(`raw mag ${rawMag.x} ${rawMag.y} ${rawMag.z}`);
|
|
66
|
+
const magStatus = await compass.readMagStatus(); // Read magnetometer status, () → Promise<{drdy:bool, lock:bool}>
|
|
67
|
+
console.log(`mag drdy=${magStatus.drdy} lock=${magStatus.lock}`);
|
|
68
|
+
const identity = await compass.readMagIdentity(); // Read magnetometer identity, () → Promise<Buffer>
|
|
69
|
+
console.log(`identity 0x${identity.toString('hex')}`);
|
|
70
|
+
await compass.enableTemperature(true); // Enable the temperature sensor, (enabled=true) → Promise<void>
|
|
71
|
+
console.log(`temperature ${(await compass.readTemperatureDelta()).toFixed(2)} C`); // Read relative temperature, () → Promise<number>
|
|
72
|
+
|
|
73
|
+
// --- Compass heading ---
|
|
74
|
+
console.log(`heading ${(await compass.heading(true)).toFixed(1)} deg`); // Read the compass heading, (tiltCompensate=true) → Promise<number>
|
|
75
|
+
console.log(`flat heading ${(await compass.heading(false)).toFixed(1)} deg`); // Read the compass heading, (tiltCompensate=false) → Promise<number>
|
|
76
|
+
|
|
77
|
+
// --- Miscellaneous ---
|
|
78
|
+
await compass.reboot(); // Reboot the accelerometer, () → Promise<void>
|
|
79
|
+
|
|
80
|
+
await accelConnection.close();
|
|
81
|
+
await magConnection.close();
|
|
82
|
+
})();
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { LSM303Full, LSM303Source } = require('periph/src/chips/accelerometer/lsm303');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
|
|
8
|
+
const accelConnection = new I2CConnection(I2C_BUS, 0x19); // Open I²C bus, (busNumber, addr=0x19) → I2CConnection
|
|
9
|
+
const magConnection = new I2CConnection(I2C_BUS, 0x1E); // Open I²C bus, (busNumber, addr=0x1E) → I2CConnection
|
|
10
|
+
const compass = new LSM303Full(accelConnection, magConnection); // Create LSM303 driver, (accelConnection, magConnection) → LSM303Full
|
|
11
|
+
|
|
12
|
+
const DIRECTIONS = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE',
|
|
13
|
+
'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'];
|
|
14
|
+
|
|
15
|
+
// --- Configure the sensor for a responsive compass ---
|
|
16
|
+
// 50 Hz gives a fresh sample every 20 ms without flooding the FIFO, and
|
|
17
|
+
// the high-resolution mode keeps 1 mg/LSB so the tilt compensation stays
|
|
18
|
+
// precise.
|
|
19
|
+
async function setup() {
|
|
20
|
+
await compass.setDataRate(50); // Set output data rate, (rateHz=50) → Promise<boolean>
|
|
21
|
+
await compass.setMode(LSM303Full.MODE_HIGH_RESOLUTION); // Set operating mode, (mode=MODE_HIGH_RESOLUTION=2) → Promise<boolean>
|
|
22
|
+
await compass.setMagRange(130); // Set magnetometer range, (rangeUt=130) → Promise<boolean>
|
|
23
|
+
await compass.setMagDataRate(15); // Set magnetometer data rate, (rateHz=15) → Promise<boolean>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// --- Arm the gestures ---
|
|
27
|
+
// A double tap on the Z axis and a free-fall event both end up on INT1.
|
|
28
|
+
// The latch keeps the line asserted until the source register is read, so
|
|
29
|
+
// a short tap is never missed between two polling rounds.
|
|
30
|
+
async function armGestures() {
|
|
31
|
+
await compass.setClick(0, 0x04, 0.8, 20, 40, 200); // Configure click detector, (single=0, double=0x04, thresholdG=0.8, timeLimitMs=20, latencyMs=40, windowMs=200) → Promise<boolean>
|
|
32
|
+
await compass.setFreeFall(1, 0.35, 60); // Arm free-fall, (generator=1, thresholdG=0.35, durationMs=60) → Promise<boolean>
|
|
33
|
+
await compass.setLatch(1, true); // Latch the interrupt, (generator=1, enabled=true) → Promise<boolean>
|
|
34
|
+
await compass.enableInterrupt(LSM303Source.SOURCE_CLICK, 1); // Route an interrupt, (source=SOURCE_CLICK=0x20, pin=1) → Promise<boolean>
|
|
35
|
+
await compass.enableInterrupt(LSM303Source.SOURCE_IA1, 1); // Route an interrupt, (source=SOURCE_IA1=0x08, pin=1) → Promise<boolean>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// --- Collect the samples in the FIFO ---
|
|
39
|
+
// Stream mode keeps the newest 32 sample sets; the watermark interrupts as
|
|
40
|
+
// soon as 16 of them have piled up.
|
|
41
|
+
async function setupFifo() {
|
|
42
|
+
await compass.setFifo(LSM303Full.FIFO_STREAM, 16); // Configure FIFO, (mode=FIFO_STREAM=2, watermark=16, triggerInt2=false) → Promise<boolean>
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
(async () => {
|
|
46
|
+
await setup();
|
|
47
|
+
await armGestures();
|
|
48
|
+
await setupFifo();
|
|
49
|
+
|
|
50
|
+
for (let round = 0; round < 60; round++) {
|
|
51
|
+
// --- Average the samples the FIFO collected ---
|
|
52
|
+
// Averaging over the whole burst cancels most of the vibration noise a
|
|
53
|
+
// handheld compass would otherwise show.
|
|
54
|
+
let ax = 0, ay = 0, az = 0;
|
|
55
|
+
const samples = await compass.readFifo(8); // Read FIFO, (count=8) → Promise<Array<{x,y,z}>>
|
|
56
|
+
if (samples.length > 0) {
|
|
57
|
+
for (const s of samples) {
|
|
58
|
+
ax += s.x;
|
|
59
|
+
ay += s.y;
|
|
60
|
+
az += s.z;
|
|
61
|
+
}
|
|
62
|
+
ax /= samples.length;
|
|
63
|
+
ay /= samples.length;
|
|
64
|
+
az /= samples.length;
|
|
65
|
+
} else {
|
|
66
|
+
const a = await compass.readAcceleration(); // Read acceleration, () → Promise<{x:float, y:float, z:float}> g
|
|
67
|
+
ax = a.x; ay = a.y; az = a.z;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// --- Compute the heading and the tilt ---
|
|
71
|
+
const heading = await compass.heading(true); // Read the compass heading, (tiltCompensate=true) → Promise<number>
|
|
72
|
+
const roll = Math.atan2(ay, az) * 180.0 / Math.PI;
|
|
73
|
+
const pitch = Math.atan2(-ax, Math.sqrt(ay * ay + az * az)) * 180.0 / Math.PI;
|
|
74
|
+
const mag = await compass.readMagnetic(); // Read magnetic field, () → Promise<{x:float, y:float, z:float}> uT
|
|
75
|
+
const magnitude = Math.sqrt(mag.x * mag.x + mag.y * mag.y + mag.z * mag.z);
|
|
76
|
+
const temperature = await compass.readTemperatureDelta(); // Read relative temperature, () → Promise<number>
|
|
77
|
+
let index = Math.floor((heading + 11.25) / 22.5) % 16;
|
|
78
|
+
if (index < 0) index += 16;
|
|
79
|
+
|
|
80
|
+
console.log(`heading ${heading.toFixed(1)} deg (${DIRECTIONS[index]}) pitch ${pitch.toFixed(1)} deg roll ${roll.toFixed(1)} deg field ${magnitude.toFixed(1)} uT temp ${temperature.toFixed(1)} C`);
|
|
81
|
+
|
|
82
|
+
// --- Report the gestures ---
|
|
83
|
+
const status = await compass.pollInterrupt(); // Read interrupt status, () → Promise<number>
|
|
84
|
+
if (status & LSM303Source.SOURCE_CLICK) {
|
|
85
|
+
console.log('DOUBLE TAP');
|
|
86
|
+
}
|
|
87
|
+
if (status & LSM303Source.SOURCE_IA1) {
|
|
88
|
+
console.log('FREE FALL');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
await accelConnection.close();
|
|
95
|
+
await magConnection.close();
|
|
96
|
+
})();
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { LSM303Minimal } = require('periph/src/chips/accelerometer/lsm303');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
|
|
8
|
+
const accelConnection = new I2CConnection(I2C_BUS, 0x19); // Open I²C bus, (busNumber, addr=0x19) → I2CConnection
|
|
9
|
+
const magConnection = new I2CConnection(I2C_BUS, 0x1E); // Open I²C bus, (busNumber, addr=0x1E) → I2CConnection
|
|
10
|
+
const compass = new LSM303Minimal(accelConnection, magConnection); // Create LSM303 driver, (accelConnection, magConnection) → LSM303Minimal
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
while (true) {
|
|
14
|
+
const accel = await compass.readAcceleration(); // Read acceleration, () → Promise<{x:float, y:float, z:float}> g
|
|
15
|
+
const mag = await compass.readMagnetic(); // Read magnetic field, () → Promise<{x:float, y:float, z:float}> uT
|
|
16
|
+
console.log(`accel x=${accel.x.toFixed(4)} y=${accel.y.toFixed(4)} z=${accel.z.toFixed(4)} g`);
|
|
17
|
+
console.log(`mag x=${mag.x.toFixed(2)} y=${mag.y.toFixed(2)} z=${mag.z.toFixed(2)} uT`);
|
|
18
|
+
await new Promise(r => setTimeout(r, 1000));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
await accelConnection.close();
|
|
22
|
+
await magConnection.close();
|
|
23
|
+
})();
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { MMA8451QFull } = require('periph/src/chips/accelerometer/mma8451q');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x1C', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const accel = new MMA8451QFull(connection); // Create MMA8451Q Full driver, (connection)
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
await accel.init(); // Run init sequence, () → None
|
|
14
|
+
// verifies WHO_AM_I, resets and sets ±2 g / 100 Hz / 14-bit
|
|
15
|
+
|
|
16
|
+
await accel.setRange(4); // Set measurement range, (range_g) → g
|
|
17
|
+
// selects ±4 g; 2048 LSB/g instead of 4096 LSB/g
|
|
18
|
+
await accel.setDataRate(200); // Set output data rate, (rate_hz) → Hz
|
|
19
|
+
// DR code 010; the turn-on time shrinks to 2/ODR + 2 ms
|
|
20
|
+
await accel.setOversampling(0); // Set oversampling mode, (mode, sleep_mode=None) → None
|
|
21
|
+
// mode 0 = normal; mode 2 = high resolution, mode 3 = low power
|
|
22
|
+
await accel.setLowNoise(false); // Set reduced-noise mode, (enabled) → None
|
|
23
|
+
// LNOISE caps the range at ±4 g, so it is left off here
|
|
24
|
+
await accel.setResolution(14); // Set output resolution, (bits) → bits
|
|
25
|
+
// 14 = 14-bit; 8 = fast read, MSB registers only
|
|
26
|
+
await accel.setHighPassOutput(false, 0); // Set high-pass output, (enabled, cutoff=0) → None
|
|
27
|
+
// HPF_OUT off; cutoff is the SEL code 0..3
|
|
28
|
+
|
|
29
|
+
const [x, y, z] = await accel.read(); // Read 3-axis acceleration, () → [number, number, number] g
|
|
30
|
+
const raw = await accel.readRaw(); // Read raw counts, () → [int, int, int]
|
|
31
|
+
// signed counts at the current resolution (14-bit right-aligned)
|
|
32
|
+
const status = await accel.readStatus(); // Read STATUS register, () → bitmask
|
|
33
|
+
// ZYXDR / ZYXOW data-ready and overwrite flags
|
|
34
|
+
const ready = await accel.newDataAvailable(); // Check new data, () → bool
|
|
35
|
+
// True once ZYXDR is set; invalid while the FIFO is on
|
|
36
|
+
const sysmod = await accel.readSystemMode(); // Read system mode, () → int
|
|
37
|
+
// SYSMOD bits 1:0 — 0 standby, 1 wake, 2 sleep
|
|
38
|
+
|
|
39
|
+
await accel.setOffset(0.01, -0.01, 0.0); // Set offsets, (x_g, y_g, z_g) → g
|
|
40
|
+
// OFF_x = round(offset / 0.002 g), clamped to ±128 counts (±0.256 g)
|
|
41
|
+
const offsets = await accel.getOffset(); // Get offsets, () → [number, number, number] g
|
|
42
|
+
// current per-axis offset correction
|
|
43
|
+
|
|
44
|
+
await accel.setFifo(1, 16); // Configure FIFO, (mode, watermark=0) → None
|
|
45
|
+
// mode 1 = circular, watermark 16 samples
|
|
46
|
+
await accel.setFifoTrigger(false, true, false, false); // Set FIFO triggers, (transient, orientation, pulse, motion) → None
|
|
47
|
+
// trigger the FIFO on an orientation change
|
|
48
|
+
const level = await accel.fifoLevel(); // Read FIFO level, () → int
|
|
49
|
+
// F_CNT, 0..32 samples
|
|
50
|
+
const fstatus = await accel.readFifoStatus(); // Read FIFO status, () → {overflow, watermark, count}
|
|
51
|
+
// reading F_STATUS clears the FIFO interrupt
|
|
52
|
+
|
|
53
|
+
await accel.setFreefallMotion(false, 7, 0.2, 60, true); // Configure freefall, (motion, axes, threshold_g, debounce_ms, latch) → g, ms
|
|
54
|
+
// OAE = 0 (AND of all axes); 0.2 g threshold, 60 ms debounce, latched
|
|
55
|
+
await accel.setTransient(7, 0.25, 0, false, true); // Configure transient, (axes, threshold_g, debounce_ms, bypass_hpf, latch) → g, ms
|
|
56
|
+
// 0.25 g threshold on all axes, latched
|
|
57
|
+
await accel.setPulse(0, 4, [0.5, 0.5, 1.0], 40, 100, 300, true);
|
|
58
|
+
// Configure tap, (single, double, threshold_g, time_limit_ms, latency_ms, window_ms, latch) → g, ms
|
|
59
|
+
// double tap on Z with a 1.0 g Z threshold
|
|
60
|
+
|
|
61
|
+
await accel.setOrientation(true, 100, 45, 4, 4, 1); // Configure orientation, (enabled, debounce_ms, threshold_deg, hysteresis, z_lock, back_front) → ms, deg
|
|
62
|
+
// 45° trip angle, ±14° hysteresis, 100 ms debounce
|
|
63
|
+
const orient = await accel.readOrientation(); // Read orientation, () → {changed, lockout, portraitLandscape, back}
|
|
64
|
+
// LAPO 0 portrait up, 1 portrait down, 2 landscape right, 3 landscape left
|
|
65
|
+
|
|
66
|
+
await accel.setAutoSleep(true, 12.5, 10, 0); // Configure Auto-SLEEP, (enabled, sleep_rate_hz, timeout_s, wake_sources) → Hz, s
|
|
67
|
+
// sleeps after 10 s of inactivity at 12.5 Hz
|
|
68
|
+
await accel.setFifoGate(false); // Set FIFO gate, (enabled) → None
|
|
69
|
+
// block FIFO input around WAKE/SLEEP transitions
|
|
70
|
+
await accel.setInterruptPins(false, false); // Set interrupt pins, (active_high, open_drain) → None
|
|
71
|
+
// IPOL = 0 (active low), PP_OD = 0 (push-pull)
|
|
72
|
+
|
|
73
|
+
await accel.enableInterrupt(0x04, 1); // Enable interrupt, (source, pin=1) → None
|
|
74
|
+
// enables the freefall/motion source and routes it to INT1
|
|
75
|
+
await accel.disableInterrupt(0x04); // Disable interrupt, (source) → None
|
|
76
|
+
// clears the INT_EN bit in CTRL_REG4
|
|
77
|
+
|
|
78
|
+
const polled = await accel.pollInterrupt(); // Poll interrupts, () → bitmask
|
|
79
|
+
// INT_SOURCE; also clears the latched source flags
|
|
80
|
+
await accel.onInterrupt(() => {}); // Subscribe to interrupts, (callback, pin=1) → None
|
|
81
|
+
// fires on every INT edge; the callback receives the status mask
|
|
82
|
+
await accel.offInterrupt(); // Unsubscribe from interrupts, (pin=1) → None
|
|
83
|
+
// stops interrupt delivery
|
|
84
|
+
|
|
85
|
+
const st = await accel.selfTest(); // Run self-test, () → {ok, dx, dy, dz}
|
|
86
|
+
// electrostatic force; compares the count deltas against the typical values
|
|
87
|
+
await accel.standby(); // Enter standby, () → None
|
|
88
|
+
// ACTIVE = 0; configuration registers may now be written
|
|
89
|
+
await accel.activate(); // Leave standby, () → None
|
|
90
|
+
// ACTIVE = 1, then waits 2/ODR + 2 ms
|
|
91
|
+
await accel.reset(); // Software reset, () → None
|
|
92
|
+
// RST bit; the chip returns to STANDBY with reset values
|
|
93
|
+
|
|
94
|
+
console.log(`xyz=[${[x, y, z].map(v => v.toFixed(3)).join(', ')}] raw=${JSON.stringify(raw)} status=0x${status.toString(16)} ready=${ready} sysmod=${sysmod} offsets=${JSON.stringify(offsets)} fifoLevel=${level} fifo=${JSON.stringify(fstatus)} orient=${JSON.stringify(orient)} polled=0x${polled.toString(16)} selftest=${JSON.stringify(st)}`);
|
|
95
|
+
await connection.close();
|
|
96
|
+
})();
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const {
|
|
5
|
+
MMA8451QFull,
|
|
6
|
+
SOURCE_ORIENTATION,
|
|
7
|
+
SOURCE_PULSE,
|
|
8
|
+
SOURCE_FF_MT,
|
|
9
|
+
} = require('periph/src/chips/accelerometer/mma8451q');
|
|
10
|
+
|
|
11
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
12
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x1C', 16);
|
|
13
|
+
|
|
14
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
15
|
+
const accel = new MMA8451QFull(connection); // Create MMA8451Q driver, (connection)
|
|
16
|
+
|
|
17
|
+
// --- Configure ±4 g, 100 Hz, high-resolution oversampling and run the self-test ---
|
|
18
|
+
// ±4 g gives 2048 LSB/g, enough headroom for the freefall and tap
|
|
19
|
+
// thresholds while keeping 14-bit resolution. High-resolution
|
|
20
|
+
// oversampling trades current for noise performance, which matters when
|
|
21
|
+
// the chip spends most of its time in Auto-SLEEP.
|
|
22
|
+
async function setup() {
|
|
23
|
+
await accel.init(); // Run init sequence, () → None
|
|
24
|
+
// verifies WHO_AM_I, resets and sets ±2 g / 100 Hz / 14-bit
|
|
25
|
+
await accel.setRange(4); // Set measurement range, (range_g) → g
|
|
26
|
+
// selects ±4 g
|
|
27
|
+
await accel.setOversampling(2); // Set oversampling mode, (mode, sleep_mode=None) → None
|
|
28
|
+
// mode 2 = high resolution
|
|
29
|
+
await accel.setDataRate(100); // Set output data rate, (rate_hz) → Hz
|
|
30
|
+
// DR code 011
|
|
31
|
+
|
|
32
|
+
const st = await accel.selfTest(); // Run self-test, () → {ok, dx, dy, dz}
|
|
33
|
+
console.log(`self-test: ok=${st.ok} dx=${st.dx} dy=${st.dy} dz=${st.dz}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// --- Arm the orientation, tap and freefall detectors ---
|
|
37
|
+
// The orientation detector reports portrait/landscape and back/front
|
|
38
|
+
// changes; the tap detector fires on a double tap along Z; the freefall
|
|
39
|
+
// detector fires when all three axes drop below the threshold.
|
|
40
|
+
async function armDetectors() {
|
|
41
|
+
await accel.setOrientation(true, 100); // Configure orientation, (enabled, debounce_ms=0, threshold_deg=45, hysteresis=4, z_lock=4, back_front=1) → ms
|
|
42
|
+
// 100 ms debounce suppresses the bounces of a hand-held board
|
|
43
|
+
await accel.setPulse(0, 0x04, [0.5, 0.5, 1.0], 40, 100, 300, true);
|
|
44
|
+
// Configure tap, (single, double, threshold_g, time_limit_ms, latency_ms, window_ms, latch=False) → g, ms
|
|
45
|
+
// double tap on Z, latched until PULSE_SRC is read
|
|
46
|
+
await accel.setFreefallMotion(false, 7, 0.2, 60, true); // Configure freefall, (motion, axes, threshold_g, debounce_ms, latch) → g, ms
|
|
47
|
+
// 0.2 g threshold on all axes, 60 ms debounce, latched
|
|
48
|
+
|
|
49
|
+
// --- Auto-SLEEP after 10 s of inactivity, woken by a tap or freefall ---
|
|
50
|
+
// The chip drops to 12.5 Hz after 10 s without motion; a tap or a
|
|
51
|
+
// freefall event brings it straight back to 100 Hz.
|
|
52
|
+
await accel.setAutoSleep(true, 12.5, 10, SOURCE_PULSE | SOURCE_FF_MT);
|
|
53
|
+
// Configure Auto-SLEEP, (enabled, sleep_rate_hz=50, timeout_s=10, wake_sources=0) → Hz, s
|
|
54
|
+
// sleep rate 12.5 Hz, timeout 10 s, wake on tap or freefall
|
|
55
|
+
|
|
56
|
+
// --- Route the three detector interrupts to INT1 and fill the FIFO ---
|
|
57
|
+
// All three sources share INT1; the driver reports the cause through
|
|
58
|
+
// the status mask, so one pin is enough.
|
|
59
|
+
await accel.enableInterrupt(SOURCE_ORIENTATION, 1); // Enable interrupt, (source, pin=1) → None
|
|
60
|
+
// orientation change → INT1
|
|
61
|
+
await accel.enableInterrupt(SOURCE_PULSE, 1); // Enable interrupt, (source, pin=1) → None
|
|
62
|
+
// tap → INT1
|
|
63
|
+
await accel.enableInterrupt(SOURCE_FF_MT, 1); // Enable interrupt, (source, pin=1) → None
|
|
64
|
+
// freefall/motion → INT1
|
|
65
|
+
|
|
66
|
+
await accel.setFifo(1, 16); // Configure FIFO, (mode, watermark=0) → None
|
|
67
|
+
// circular FIFO with a 16-sample watermark
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// --- 60-second orientation / tap / freefall monitor ---
|
|
71
|
+
// The user is expected to tilt, tap and drop the board during the 60 s
|
|
72
|
+
// window. Every 250 ms the acceleration and the system mode are printed,
|
|
73
|
+
// so the transition from WAKE to SLEEP becomes visible as well.
|
|
74
|
+
(async () => {
|
|
75
|
+
await setup();
|
|
76
|
+
await armDetectors();
|
|
77
|
+
|
|
78
|
+
const start = Date.now();
|
|
79
|
+
while (Date.now() - start < 60000) {
|
|
80
|
+
const [x, y, z] = await accel.read(); // Read 3-axis acceleration, () → [number, number, number] g
|
|
81
|
+
const mode = await accel.readSystemMode(); // Read system mode, () → int
|
|
82
|
+
const modeName = mode === 2 ? 'SLEEP' : (mode === 1 ? 'WAKE' : 'STANDBY');
|
|
83
|
+
console.log(`${String(Math.floor((Date.now() - start) / 1000)).padStart(5)} x=${x.toFixed(3).padStart(7)} y=${y.toFixed(3).padStart(7)} z=${z.toFixed(3).padStart(7)} ${modeName}`);
|
|
84
|
+
|
|
85
|
+
const status = await accel.pollInterrupt(); // Poll interrupts, () → bitmask
|
|
86
|
+
if (status & SOURCE_ORIENTATION) {
|
|
87
|
+
const o = await accel.readOrientation(); // Read orientation, () → {changed, lockout, portraitLandscape, back}
|
|
88
|
+
// LAPO 0 portrait up, 1 portrait down, 2 landscape right, 3 landscape left
|
|
89
|
+
const lapo = ['portrait up', 'portrait down', 'landscape right', 'landscape left'][o.portraitLandscape];
|
|
90
|
+
console.log(` ORIENTATION: ${lapo}, ${o.back ? 'back' : 'front'}`);
|
|
91
|
+
}
|
|
92
|
+
if (status & SOURCE_PULSE) {
|
|
93
|
+
await accel.readPulseSource(); // Read tap source, () → int
|
|
94
|
+
// reading PULSE_SRC clears the latched tap flag
|
|
95
|
+
console.log(' DOUBLE TAP');
|
|
96
|
+
}
|
|
97
|
+
if (status & SOURCE_FF_MT) {
|
|
98
|
+
await accel.readMotionSource(); // Read motion source, () → int
|
|
99
|
+
// reading FF_MT_SRC clears the latched freefall flag
|
|
100
|
+
console.log(' FREE FALL');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
await new Promise(r => setTimeout(r, 250));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// --- Drain the circular FIFO and report the mean acceleration ---
|
|
107
|
+
// The FIFO holds the last 32 samples; the mean smooths the 100 Hz
|
|
108
|
+
// noise down to a stable tilt angle.
|
|
109
|
+
const samples = await accel.readFifo(); // Read FIFO, (count=None) → [[number, number, number]] g
|
|
110
|
+
// reads up to 32 samples, one per transaction
|
|
111
|
+
const n = samples.length;
|
|
112
|
+
if (n > 0) {
|
|
113
|
+
const mean = [0, 1, 2].map(i => samples.reduce((s, v) => s + v[i], 0) / n);
|
|
114
|
+
console.log(`FIFO: ${n} samples, mean x=${mean[0].toFixed(3)} y=${mean[1].toFixed(3)} z=${mean[2].toFixed(3)} g`);
|
|
115
|
+
} else {
|
|
116
|
+
console.log('FIFO: empty');
|
|
117
|
+
}
|
|
118
|
+
console.log('done');
|
|
119
|
+
await connection.close();
|
|
120
|
+
})();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { MMA8451QMinimal } = require('periph/src/chips/accelerometer/mma8451q');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x1C', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const accel = new MMA8451QMinimal(connection); // Create MMA8451Q driver, (connection)
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
await accel.init(); // Run init sequence, () → None
|
|
14
|
+
// verifies WHO_AM_I, resets and sets ±2 g / 100 Hz / 14-bit
|
|
15
|
+
for (let i = 0; i < 10; i++) {
|
|
16
|
+
const [x, y, z] = await accel.read(); // Read 3-axis acceleration, () → [number, number, number] g
|
|
17
|
+
console.log(`x=${x.toFixed(3)} y=${y.toFixed(3)} z=${z.toFixed(3)} g`);
|
|
18
|
+
}
|
|
19
|
+
await connection.close();
|
|
20
|
+
})();
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Complete example: exercises every method of AD5243Full.
|
|
4
|
+
|
|
5
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
6
|
+
const { AD5243Full } = require('periph/src/chips/adc_dac/ad5243');
|
|
7
|
+
|
|
8
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
9
|
+
|
|
10
|
+
(async () => {
|
|
11
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
12
|
+
const connection = new I2CConnection(bus, AD5243Full.I2C_ADDRESS);
|
|
13
|
+
const pot = new AD5243Full(connection); // Create AD5243 Full driver, (connection, rAb=10000 Ω, rWiper=160 Ω)
|
|
14
|
+
await pot.init(); // Prepare driver, () → None
|
|
15
|
+
// no I²C traffic; the chip needs no initialisation
|
|
16
|
+
|
|
17
|
+
await pot.setWiper(0, 100); // Set wiper position, (channel 0–1, code 0–255) → None
|
|
18
|
+
// writes [instruction, code]; the instruction byte selects the channel
|
|
19
|
+
console.log(await pot.getWiper(0)); // Read wiper position, (channel 0–1) → int 0–255
|
|
20
|
+
// sends the instruction alone, then reads one byte
|
|
21
|
+
await pot.setResistance(0, 5000.0); // Set W–B resistance, (channel 0–1, ohms Ω) → None
|
|
22
|
+
// inverts R = D/256·RAB + 2·RW to the nearest code
|
|
23
|
+
console.log(await pot.getResistance(0)); // Read W–B resistance, (channel 0–1) → float Ω
|
|
24
|
+
// evaluates D/256·RAB + 2·RW for the current code
|
|
25
|
+
|
|
26
|
+
await pot.setBoth(32, 224); // Write both wipers, (code1 0–255, code2 0–255) → None
|
|
27
|
+
// two transactions, channel 0 then channel 1
|
|
28
|
+
await pot.setRatio(0, 0.25); // Set divider ratio, (channel 0–1, ratio 0.0–1.0) → None
|
|
29
|
+
// code = round(ratio × 256), clamped to 255
|
|
30
|
+
console.log(await pot.getRatio(0)); // Read divider ratio, (channel 0–1) → float
|
|
31
|
+
// code / 256
|
|
32
|
+
await pot.setResistanceWa(1, 3000.0); // Set W–A resistance, (channel 0–1, ohms Ω) → None
|
|
33
|
+
// rheostat between W and A with B open
|
|
34
|
+
console.log(await pot.getResistanceWa(1)); // Read W–A resistance, (channel 0–1) → float Ω
|
|
35
|
+
// (256 − D)/256·RAB + 2·RW
|
|
36
|
+
console.log(await pot.increment(0, 5)); // Step the wiper, (channel 0–1, steps=1) → int 0–255
|
|
37
|
+
// read, add, clamp, write; returns the new code
|
|
38
|
+
|
|
39
|
+
await pot.shutdown(1); // Shut channel down, (channel 0–1) → None
|
|
40
|
+
// SD = 1: A opens, wiper shorts to B, RDAC kept
|
|
41
|
+
console.log(pot.isShutdown(1)); // Query shutdown state, (channel 0–1) → bool
|
|
42
|
+
// driver-tracked; the chip cannot report it
|
|
43
|
+
await sleep(1000);
|
|
44
|
+
await pot.resume(1); // Leave shutdown, (channel 0–1) → None
|
|
45
|
+
// rewrites the kept code with SD = 0
|
|
46
|
+
|
|
47
|
+
pot.setRab(50000.0, 120.0); // Change resistance model, (rAb Ω, rWiper Ω) → None
|
|
48
|
+
// affects conversions only; no I²C traffic
|
|
49
|
+
await pot.reset(); // Restore power-up preset, () → None
|
|
50
|
+
// writes 0x80 to both channels with SD = 0
|
|
51
|
+
await connection.close();
|
|
52
|
+
})();
|