periph 1.3.2 → 1.4.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 +27 -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/adc_dac/es8311/complete.js +80 -0
- package/examples/adc_dac/es8311/demo.js +59 -0
- package/examples/adc_dac/es8311/minimal.js +18 -0
- package/examples/adc_dac/mcp4xxx/complete.js +97 -0
- package/examples/adc_dac/mcp4xxx/demo.js +76 -0
- package/examples/adc_dac/mcp4xxx/minimal.js +22 -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/imu/qmi8658a/complete.js +134 -0
- package/examples/imu/qmi8658a/demo.js +81 -0
- package/examples/imu/qmi8658a/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/examples/motor/tmc2130/complete.js +138 -0
- package/examples/motor/tmc2130/demo.js +96 -0
- package/examples/motor/tmc2130/minimal.js +30 -0
- package/examples/motor/tmc2208/complete.js +92 -0
- package/examples/motor/tmc2208/demo.js +85 -0
- package/examples/motor/tmc2208/minimal.js +25 -0
- package/examples/motor/tmc2209/complete.js +106 -0
- package/examples/motor/tmc2209/demo.js +97 -0
- package/examples/motor/tmc2209/minimal.js +25 -0
- package/examples/motor/tmc5160/complete.js +205 -0
- package/examples/motor/tmc5160/demo.js +104 -0
- package/examples/motor/tmc5160/minimal.js +35 -0
- package/examples/power/axp2101/complete.js +135 -0
- package/examples/power/axp2101/demo.js +68 -0
- package/examples/power/axp2101/minimal.js +24 -0
- package/examples/rtc/pcf85063a/complete.js +68 -0
- package/examples/rtc/pcf85063a/demo.js +76 -0
- package/examples/rtc/pcf85063a/minimal.js +20 -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/adc_dac/es8311.js +621 -0
- package/src/chips/adc_dac/mcp4xxx.js +982 -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/imu/qmi8658a.js +1011 -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/chips/motor/tmc2130.js +537 -0
- package/src/chips/motor/tmc2208.js +490 -0
- package/src/chips/motor/tmc2209.js +565 -0
- package/src/chips/motor/tmc5160.js +900 -0
- package/src/chips/power/axp2101.js +968 -0
- package/src/chips/rtc/pcf85063a.js +482 -0
- package/src/connection/uart.js +9 -0
- package/src/connection/uart_mock.js +65 -0
- package/src/discovery/registry.js +45 -19
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Mcp4xxxFull } = require('../../../src/chips/adc_dac/mcp4xxx');
|
|
5
|
+
|
|
6
|
+
const PART = process.env.MCP4XXX_PART || 'MCP4532'; // any of MCP4531 ... MCP4662
|
|
7
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
8
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x58', 16);
|
|
9
|
+
|
|
10
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
11
|
+
const pot = new Mcp4xxxFull(connection, PART, 10000.0, 75.0); // Create MCP4XXX driver, (connection, part='MCP4532', rAb=10000 Ω, rWiper=75 Ω, hvc=null, hvcSettleMs=1) → Mcp4xxxFull
|
|
12
|
+
|
|
13
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
14
|
+
|
|
15
|
+
(async () => {
|
|
16
|
+
await pot.init(); // Bus check, () → void
|
|
17
|
+
|
|
18
|
+
// --- Identify the family member ---
|
|
19
|
+
// One driver covers sixteen parts; the descriptor tells resolution, networks and memory type.
|
|
20
|
+
console.log(PART, pot.channels(), 'network(s)', pot.steps(), 'steps', // Part properties, () → int
|
|
21
|
+
pot.isPotentiometer() ? 'pot' : 'rheostat', pot.isNonvolatile() ? 'EEPROM' : 'RAM');
|
|
22
|
+
console.log('power-up wiper', await pot.getWiper(), await pot.getResistance(), 'ohm'); // Read wiper, (channel=0) → int
|
|
23
|
+
|
|
24
|
+
// --- Set 2.2 kΩ and report the quantisation error ---
|
|
25
|
+
// The wiper snaps to the nearest step of RAB / max code.
|
|
26
|
+
await pot.setResistance(2200.0); // Set W-B resistance, (ohms, channel=0) → void
|
|
27
|
+
const actual = await pot.getResistance(); // Read W-B resistance, (channel=0) → float Ω
|
|
28
|
+
console.log('target=2200 ohm code=%d actual=%s ohm error=%s ohm', await pot.getWiper(), actual.toFixed(1), (actual - 2200).toFixed(1));
|
|
29
|
+
|
|
30
|
+
// --- Step the wiper up and down ---
|
|
31
|
+
// increment / decrement send one command byte per step in a single transaction and
|
|
32
|
+
// saturate at full scale and zero scale.
|
|
33
|
+
for (const [steps, up] of [[8, true], [8, true], [4, false], [10, true]]) {
|
|
34
|
+
const code = up ? await pot.increment(steps) : await pot.decrement(steps); // Step wiper, (steps=1, channel=0) → int
|
|
35
|
+
console.log('%s%d -> code=%d R_WB=%s ohm', up ? '+' : '-', steps, code, (await pot.getResistance()).toFixed(1));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// --- Ratios ---
|
|
39
|
+
// The ratio is the fraction of RAB between W and B (the unloaded divider ratio of a potentiometer).
|
|
40
|
+
for (const ratio of [0.0, 0.25, 0.5, 0.75, 1.0]) {
|
|
41
|
+
await pot.setRatio(ratio); // Set wiper ratio, (ratio 0.0-1.0, channel=0) → void
|
|
42
|
+
let line = `ratio=${ratio.toFixed(2)} code=${await pot.getWiper()} R_WB=${(await pot.getResistance()).toFixed(1)}`;
|
|
43
|
+
if (pot.isPotentiometer()) line += ` R_WA=${(await pot.getResistanceWa()).toFixed(1)}`; // Read W-A resistance, (channel=0) → float Ω
|
|
44
|
+
console.log(line);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// --- Dual parts: drive both networks ---
|
|
48
|
+
if (pot.channels() === 2) {
|
|
49
|
+
await pot.setBoth(32, 96); // Set both wipers, (code0, code1) → void
|
|
50
|
+
console.log('both:', await pot.getWiper(0), await pot.getWiper(1), '->', await pot.increment(5, 1), await pot.decrement(2, 1));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// --- Shutdown and terminal disconnect ---
|
|
54
|
+
// Shutdown ties W to B and opens A without losing the wiper; setTerminals(true, false, true) floats W.
|
|
55
|
+
await pot.shutdown(); // Shut network down, (channel=0) → void
|
|
56
|
+
console.log('shutdown=%s code=%d', await pot.isShutdown(), await pot.getWiper()); // Read state, (channel=0) → bool
|
|
57
|
+
await sleep(1000);
|
|
58
|
+
await pot.resume(); // Leave shutdown, (channel=0) → void
|
|
59
|
+
await pot.setTerminals(true, false, true); // Disconnect the wiper, (a=true, w=true, b=true, channel=0) → void
|
|
60
|
+
console.log('terminals', await pot.getTerminals()); // Read terminals, (channel=0) → {a, w, b}
|
|
61
|
+
await pot.setTerminals(true, true, true); // Reconnect all terminals, (a=true, w=true, b=true, channel=0) → void
|
|
62
|
+
|
|
63
|
+
// --- Non-volatile parts: EEPROM wiper and data ---
|
|
64
|
+
// storeWiper makes the setting survive power cycles; every EEPROM write uses one of the
|
|
65
|
+
// 1 000 000 write cycles, so do not do this in a loop.
|
|
66
|
+
if (pot.isNonvolatile()) {
|
|
67
|
+
console.log(await pot.readStatus()); // Read STATUS, () → {eepromBusy, wiperLock, writeProtect}
|
|
68
|
+
console.log('stored', await pot.storeWiper()); // Store the wiper in EEPROM, (channel=0) → int
|
|
69
|
+
await pot.writeData(0, 0x155); // Write calibration marker, (index 0-9, value 0-0x1FF) → void
|
|
70
|
+
console.log('data0', (await pot.readData(0)).toString(16), 'nv wiper', await pot.getNvWiper()); // Read back, (index) → int
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// --- Back to the power-up state ---
|
|
74
|
+
await pot.reset(); // Emulate power-on reset, () → void
|
|
75
|
+
console.log('after reset:', await pot.getWiper());
|
|
76
|
+
})();
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { Mcp4xxxMinimal } = require('../../../src/chips/adc_dac/mcp4xxx');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x58', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const pot = new Mcp4xxxMinimal(connection, 'MCP4532', 10000.0, 75.0); // Create MCP4XXX driver, (connection, part='MCP4532', rAb=10000 Ω, rWiper=75 Ω) → Mcp4xxxMinimal
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
await pot.init(); // Bus check, () → void
|
|
14
|
+
await pot.setWiper(32); // Set wiper, (code 0-128, channel=0) → void
|
|
15
|
+
await pot.setResistance(2200.0); // Set W-B resistance, (ohms, channel=0) → void
|
|
16
|
+
|
|
17
|
+
setInterval(async () => {
|
|
18
|
+
const code = await pot.getWiper(); // Read wiper, (channel=0) → int 0-128
|
|
19
|
+
const ohms = await pot.getResistance(); // Read W-B resistance, (channel=0) → float Ω
|
|
20
|
+
console.log('code=%d R_WB=%s ohm', code, ohms.toFixed(0));
|
|
21
|
+
}, 1000);
|
|
22
|
+
})();
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { PCF8578Full } = require('../../../src/chips/display/pcf8578');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x3C', 16);
|
|
7
|
+
|
|
8
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
9
|
+
const lcd = new PCF8578Full(connection, 16); // Create PCF8578 driver, (connection, rows=16 8/16/24/32)
|
|
10
|
+
// clears RAM and switches the display on in mixed mode
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
await lcd.ready(); // Wait for initialisation, () → void
|
|
14
|
+
// resolves once the RAM is cleared and the display is on
|
|
15
|
+
await lcd.clear(); // Clear display RAM, () → void
|
|
16
|
+
// zeroes all 4 banks x 40 columns and the shadow
|
|
17
|
+
await lcd.setPixel(1, 1); // Set one pixel, (x 0–23, y 0–15, on=true) → void
|
|
18
|
+
// writes a single RAM byte from the shadow
|
|
19
|
+
await lcd.writeColumns(4, 0, [0xFF, 0x81, 0xFF]); // Write column bytes, (x 0–23, bank 0–1, data bytes) → void
|
|
20
|
+
// character access mode, bit 0 = top row of the bank
|
|
21
|
+
const on = await lcd.getPixel(1, 1); // Read pixel from shadow, (x, y) → boolean
|
|
22
|
+
// no bus traffic
|
|
23
|
+
await lcd.fill(true); // Fill display, (on=true) → void
|
|
24
|
+
// every displayed pixel on
|
|
25
|
+
await lcd.fill(false); // Fill display, (on=true) → void
|
|
26
|
+
// every displayed pixel off
|
|
27
|
+
await lcd.drawLine(0, 0, 23, 15); // Draw line, (x0, y0, x1, y1, on=true) → void
|
|
28
|
+
// Bresenham, written as one batch of changed bytes
|
|
29
|
+
await lcd.drawRect(0, 0, 24, 16); // Draw rectangle outline, (x, y, w, h, on=true, fill=false) → void
|
|
30
|
+
// border around the whole panel
|
|
31
|
+
await lcd.drawRect(8, 4, 8, 8, true, true); // Draw filled rectangle, (x, y, w, h, on=true, fill=false) → void
|
|
32
|
+
// solid 8x8 block in the middle
|
|
33
|
+
await lcd.showBitmap(2, 8, [[0x18, 0x24, 0x42, 0x81]]); // Blit bitmap, (x, y multiple of 8, bands array of column bytes) → void
|
|
34
|
+
// one 8-pixel-high band at the lower bank
|
|
35
|
+
await lcd.setDisplay(PCF8578Full.DISPLAY_INVERSE); // Set display status, (status 0 blank/1 normal/2 all on/3 inverse) → void
|
|
36
|
+
// inverse video; RAM is preserved
|
|
37
|
+
await lcd.setDisplay(PCF8578Full.DISPLAY_NORMAL); // Set display status, (status 0–3) → void
|
|
38
|
+
// back to normal video
|
|
39
|
+
await lcd.setMode(24, true); // Set multiplex rate, (rows 8/16/24/32, mixed=true) → void
|
|
40
|
+
// 1:24 drive; mixed mode keeps RAM accessible
|
|
41
|
+
await lcd.setMode(16, true); // Set multiplex rate, (rows 8/16/24/32, mixed=true) → void
|
|
42
|
+
// back to 1:16
|
|
43
|
+
await lcd.setStartBank(1); // Set start bank, (bank 0–3) → void
|
|
44
|
+
// shows RAM bank 1 at the top (hardware scroll)
|
|
45
|
+
await lcd.scroll(-1); // Scroll by banks, (banks number) → void
|
|
46
|
+
// adds -1 to the start bank modulo banks in use
|
|
47
|
+
await lcd.writeRam(16, 0, [0xAA, 0x55]); // Write raw RAM, (x 0–39, bank 0–3, data bytes, mode=0) → void
|
|
48
|
+
// character order at RAM column 16
|
|
49
|
+
const data = await lcd.readRam(16, 0, 2); // Read raw RAM, (x 0–39, bank 0–3, n, mode=0) → Buffer
|
|
50
|
+
// address-set commands, repeated start, then a read
|
|
51
|
+
await lcd.deviceSelect(0); // Select device subaddress, (subaddress 0–15) → void
|
|
52
|
+
// 0 addresses this PCF8578; others target cascaded PCF8579s
|
|
53
|
+
await lcd.reset(); // Re-initialise, () → void
|
|
54
|
+
// clears RAM and restores normal video
|
|
55
|
+
|
|
56
|
+
await connection.close();
|
|
57
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
58
|
+
})();
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { PCF8578Full } = require('../../../src/chips/display/pcf8578');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x3C', 16);
|
|
7
|
+
|
|
8
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
9
|
+
|
|
10
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
11
|
+
|
|
12
|
+
// --- Initialise a 16 x 24 dot-matrix panel ---
|
|
13
|
+
// 1:16 multiplex gives 16 rows and 24 columns; the constructor clears the RAM
|
|
14
|
+
// before switching the display on so no random start-up pattern is shown.
|
|
15
|
+
const lcd = new PCF8578Full(connection, 16); // Create PCF8578 driver, (connection, rows=16 8/16/24/32)
|
|
16
|
+
|
|
17
|
+
(async () => {
|
|
18
|
+
await lcd.ready();
|
|
19
|
+
|
|
20
|
+
// --- Draw a border and both diagonals ---
|
|
21
|
+
// Lines and rectangles are rendered in the shadow buffer and sent as one
|
|
22
|
+
// batch of changed bytes per run of columns.
|
|
23
|
+
await lcd.drawRect(0, 0, 24, 16); // Draw rectangle outline, (x, y, w, h, on=true, fill=false) → void
|
|
24
|
+
await lcd.drawLine(0, 0, 23, 15); // Draw line, (x0, y0, x1, y1, on=true) → void
|
|
25
|
+
await lcd.drawLine(0, 15, 23, 0); // Draw line, (x0, y0, x1, y1, on=true) → void
|
|
26
|
+
|
|
27
|
+
// --- Flash the display twice with inverse video ---
|
|
28
|
+
// Inverse video only changes how the RAM is shown, so the picture is kept.
|
|
29
|
+
for (let i = 0; i < 2; i++) {
|
|
30
|
+
await lcd.setDisplay(PCF8578Full.DISPLAY_INVERSE); // Set display status, (status 0–3) → void
|
|
31
|
+
await sleep(400);
|
|
32
|
+
await lcd.setDisplay(PCF8578Full.DISPLAY_NORMAL); // Set display status, (status 0–3) → void
|
|
33
|
+
await sleep(400);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// --- Fill all four RAM banks with distinct patterns ---
|
|
37
|
+
// At 1:16 only banks 0 and 1 are visible; banks 2 and 3 act as off-screen
|
|
38
|
+
// buffers that the start bank can scroll into view.
|
|
39
|
+
const patterns = [0x55, 0x33, 0x0F, 0xFF];
|
|
40
|
+
for (let bank = 0; bank < 4; bank++) {
|
|
41
|
+
await lcd.writeRam(16, bank, new Array(24).fill(patterns[bank])); // Write raw RAM, (x 0–39, bank 0–3, data bytes, mode=0) → void
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// --- Scroll through the banks with the hardware start bank ---
|
|
45
|
+
// Stepping the start bank scrolls the display by 8 rows without rewriting RAM.
|
|
46
|
+
for (let bank = 0; bank < 4; bank++) {
|
|
47
|
+
await lcd.setStartBank(bank); // Set start bank, (bank 0–3) → void
|
|
48
|
+
const first = await lcd.readRam(16, bank, 1); // Read raw RAM, (x 0–39, bank 0–3, n, mode=0) → Buffer
|
|
49
|
+
console.log('start bank', bank, 'first column byte', '0x' + first[0].toString(16));
|
|
50
|
+
await sleep(1000);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// --- Read back the first 8 RAM columns of bank 0 ---
|
|
54
|
+
// RAM columns 0-7 are never displayed at 1:16, so they serve as scratch
|
|
55
|
+
// storage; the chip supports reads in mixed mode, so the bytes can be verified.
|
|
56
|
+
await lcd.setStartBank(0); // Set start bank, (bank 0–3) → void
|
|
57
|
+
await lcd.writeRam(0, 0, [1, 2, 3, 4, 5, 6, 7, 8]); // Write raw RAM, (x 0–39, bank 0–3, data bytes, mode=0) → void
|
|
58
|
+
console.log(Array.from(await lcd.readRam(0, 0, 8))); // Read raw RAM, (x 0–39, bank 0–3, n, mode=0) → Buffer
|
|
59
|
+
|
|
60
|
+
await connection.close();
|
|
61
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
62
|
+
})();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { PCF8578Minimal } = require('../../../src/chips/display/pcf8578');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x3C', 16);
|
|
7
|
+
|
|
8
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
9
|
+
const lcd = new PCF8578Minimal(connection, 16); // Create PCF8578 driver, (connection, rows=16 8/16/24/32)
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
await lcd.clear(); // Clear display RAM, () → void
|
|
13
|
+
await lcd.setPixel(0, 0); // Set one pixel, (x 0–23, y 0–15, on=true) → void
|
|
14
|
+
await lcd.setPixel(23, 15); // Set opposite corner, (x 0–23, y 0–15, on=true) → void
|
|
15
|
+
await lcd.writeColumns(4, 0, [0xFF, 0x81, 0x81, 0xFF]); // Write column bytes, (x 0–23, bank 0–1, data bytes) → void
|
|
16
|
+
|
|
17
|
+
await connection.close();
|
|
18
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
19
|
+
})();
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { PCF8579Full } = require('../../../src/chips/display/pcf8579');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x3C', 16);
|
|
7
|
+
|
|
8
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
9
|
+
const lcd = new PCF8579Full(connection, 0, 16); // Create PCF8579 driver, (connection, subaddress=0 0–15, rows=16 8/16/24/32, mixed=false)
|
|
10
|
+
// clears this device's RAM and switches the display on
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
await lcd.ready(); // Wait for initialisation, () → void
|
|
14
|
+
// resolves once the RAM is cleared and the display is on
|
|
15
|
+
await lcd.clear(); // Clear display RAM, () → void
|
|
16
|
+
// zeroes all 4 banks x 40 columns and the shadow
|
|
17
|
+
await lcd.setPixel(1, 1); // Set one pixel, (x 0–39, y 0–15, on=true) → void
|
|
18
|
+
// writes a single RAM byte from the shadow
|
|
19
|
+
await lcd.writeColumns(4, 0, [0xFF, 0x81, 0xFF]); // Write column bytes, (x 0–39, bank 0–1, data bytes) → void
|
|
20
|
+
// character access mode, bit 0 = top row of the bank
|
|
21
|
+
const on = await lcd.getPixel(1, 1); // Read pixel from shadow, (x, y) → boolean
|
|
22
|
+
// no bus traffic
|
|
23
|
+
await lcd.fill(true); // Fill display, (on=true) → void
|
|
24
|
+
// every displayed pixel on
|
|
25
|
+
await lcd.fill(false); // Fill display, (on=true) → void
|
|
26
|
+
// every displayed pixel off
|
|
27
|
+
await lcd.drawLine(0, 0, 39, 15); // Draw line, (x0, y0, x1, y1, on=true) → void
|
|
28
|
+
// Bresenham, written as one batch of changed bytes
|
|
29
|
+
await lcd.drawRect(0, 0, 40, 16); // Draw rectangle outline, (x, y, w, h, on=true, fill=false) → void
|
|
30
|
+
// border around the whole panel
|
|
31
|
+
await lcd.drawRect(8, 4, 8, 8, true, true); // Draw filled rectangle, (x, y, w, h, on=true, fill=false) → void
|
|
32
|
+
// solid 8x8 block in the middle
|
|
33
|
+
await lcd.showBitmap(2, 8, [0x18, 0x24, 0x42, 0x81]); // Blit bitmap, (x, y multiple of 8, columns array of bytes) → void
|
|
34
|
+
// one 8-pixel-high band at the lower bank
|
|
35
|
+
await lcd.setDisplay(PCF8579Full.DISPLAY_INVERSE); // Set display status, (status 0 blank/1 normal/2 all on/3 inverse) → void
|
|
36
|
+
// inverse video; RAM is preserved
|
|
37
|
+
await lcd.setDisplay(PCF8579Full.DISPLAY_NORMAL); // Set display status, (status 0–3) → void
|
|
38
|
+
// back to normal video
|
|
39
|
+
await lcd.setMode(24, false); // Set multiplex rate, (rows 8/16/24/32, mixed=false) → void
|
|
40
|
+
// 1:24 drive; mixed is the PCF8578 mode bit T
|
|
41
|
+
await lcd.setMode(16, false); // Set multiplex rate, (rows 8/16/24/32, mixed=false) → void
|
|
42
|
+
// back to 1:16
|
|
43
|
+
await lcd.setStartBank(1); // Set start bank, (bank 0–3) → void
|
|
44
|
+
// shows RAM bank 1 at the top (hardware scroll)
|
|
45
|
+
await lcd.scroll(-1); // Scroll by banks, (banks number) → void
|
|
46
|
+
// adds -1 to the start bank modulo banks in use
|
|
47
|
+
await lcd.writeRam(16, 0, [0xAA, 0x55]); // Write raw RAM, (x 0–39, bank 0–3, data bytes, mode=0) → void
|
|
48
|
+
// character order at RAM column 16
|
|
49
|
+
const data = await lcd.readRam(16, 0, 2); // Read raw RAM, (x 0–39, bank 0–3, n, mode=0) → Buffer
|
|
50
|
+
// address-set commands, repeated start, then a read
|
|
51
|
+
await lcd.writeRamCascade(new Array(160).fill(0)); // Write across devices, (data bytes, mode=2) → void
|
|
52
|
+
// runs on into the following subaddresses after column 39
|
|
53
|
+
await lcd.reset(); // Re-initialise, () → void
|
|
54
|
+
// clears RAM and restores normal video
|
|
55
|
+
|
|
56
|
+
await connection.close();
|
|
57
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
58
|
+
})();
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { PCF8579Full } = require('../../../src/chips/display/pcf8579');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x3C', 16);
|
|
7
|
+
|
|
8
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
9
|
+
|
|
10
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
11
|
+
|
|
12
|
+
// --- Initialise a 16 x 40 column driver ---
|
|
13
|
+
// 1:16 multiplex gives 16 rows and 40 columns; the constructor clears the RAM
|
|
14
|
+
// before switching the display on so no random start-up pattern is shown.
|
|
15
|
+
const lcd = new PCF8579Full(connection, 0, 16); // Create PCF8579 driver, (connection, subaddress=0 0–15, rows=16 8/16/24/32, mixed=false)
|
|
16
|
+
|
|
17
|
+
(async () => {
|
|
18
|
+
await lcd.ready();
|
|
19
|
+
|
|
20
|
+
// --- Draw a border and both diagonals ---
|
|
21
|
+
// Lines and rectangles are rendered in the shadow buffer and sent as one
|
|
22
|
+
// batch of changed bytes per run of columns.
|
|
23
|
+
await lcd.drawRect(0, 0, 40, 16); // Draw rectangle outline, (x, y, w, h, on=true, fill=false) → void
|
|
24
|
+
await lcd.drawLine(0, 0, 39, 15); // Draw line, (x0, y0, x1, y1, on=true) → void
|
|
25
|
+
await lcd.drawLine(0, 15, 39, 0); // Draw line, (x0, y0, x1, y1, on=true) → void
|
|
26
|
+
|
|
27
|
+
// --- Flash the display twice with inverse video ---
|
|
28
|
+
// Inverse video only changes how the RAM is shown, so the picture is kept.
|
|
29
|
+
for (let i = 0; i < 2; i++) {
|
|
30
|
+
await lcd.setDisplay(PCF8579Full.DISPLAY_INVERSE); // Set display status, (status 0–3) → void
|
|
31
|
+
await sleep(400);
|
|
32
|
+
await lcd.setDisplay(PCF8579Full.DISPLAY_NORMAL); // Set display status, (status 0–3) → void
|
|
33
|
+
await sleep(400);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// --- Fill all four RAM banks with distinct patterns ---
|
|
37
|
+
// At 1:16 only banks 0 and 1 are visible; banks 2 and 3 act as off-screen
|
|
38
|
+
// buffers that the start bank can scroll into view.
|
|
39
|
+
const patterns = [0x55, 0x33, 0x0F, 0xFF];
|
|
40
|
+
for (let bank = 0; bank < 4; bank++) {
|
|
41
|
+
await lcd.writeRam(0, bank, new Array(40).fill(patterns[bank])); // Write raw RAM, (x 0–39, bank 0–3, data bytes, mode=0) → void
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// --- Scroll through the banks with the hardware start bank ---
|
|
45
|
+
// Stepping the start bank scrolls the display by 8 rows without rewriting RAM.
|
|
46
|
+
for (let bank = 0; bank < 4; bank++) {
|
|
47
|
+
await lcd.setStartBank(bank); // Set start bank, (bank 0–3) → void
|
|
48
|
+
const first = await lcd.readRam(0, bank, 1); // Read raw RAM, (x 0–39, bank 0–3, n, mode=0) → Buffer
|
|
49
|
+
console.log('start bank', bank, 'first column byte', '0x' + first[0].toString(16));
|
|
50
|
+
await sleep(1000);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// --- Read back the first 8 RAM columns of bank 0 ---
|
|
54
|
+
// Writing a counting pattern and reading it back verifies the bus path.
|
|
55
|
+
await lcd.setStartBank(0); // Set start bank, (bank 0–3) → void
|
|
56
|
+
await lcd.writeRam(0, 0, [1, 2, 3, 4, 5, 6, 7, 8]); // Write raw RAM, (x 0–39, bank 0–3, data bytes, mode=0) → void
|
|
57
|
+
console.log(Array.from(await lcd.readRam(0, 0, 8))); // Read raw RAM, (x 0–39, bank 0–3, n, mode=0) → Buffer
|
|
58
|
+
|
|
59
|
+
await connection.close();
|
|
60
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
61
|
+
})();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { PCF8579Minimal } = require('../../../src/chips/display/pcf8579');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x3C', 16);
|
|
7
|
+
|
|
8
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
9
|
+
const lcd = new PCF8579Minimal(connection, 0, 16); // Create PCF8579 driver, (connection, subaddress=0 0–15, rows=16 8/16/24/32, mixed=false)
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
await lcd.clear(); // Clear display RAM, () → void
|
|
13
|
+
await lcd.setPixel(0, 0); // Set one pixel, (x 0–39, y 0–15, on=true) → void
|
|
14
|
+
await lcd.setPixel(39, 15); // Set opposite corner, (x 0–39, y 0–15, on=true) → void
|
|
15
|
+
await lcd.writeColumns(4, 0, [0xFF, 0x81, 0x81, 0xFF]); // Write column bytes, (x 0–39, bank 0–1, data bytes) → void
|
|
16
|
+
|
|
17
|
+
await connection.close();
|
|
18
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
19
|
+
})();
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { ICM20948Full } = require('../../../src/chips/imu/icm20948');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x68', 16);
|
|
8
|
+
|
|
9
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
10
|
+
|
|
11
|
+
async function main() {
|
|
12
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
13
|
+
const imu = new ICM20948Full(connection); // Create ICM20948 driver, (connection) → void
|
|
14
|
+
await imu.init(); // Wait for initialization, () → void
|
|
15
|
+
// runs the WHO_AM_I check and the default configuration
|
|
16
|
+
|
|
17
|
+
const [ax, ay, az] = await imu.accel(); // Read 3-axis acceleration, () → [number, number, number] m/s²
|
|
18
|
+
// converts raw accel registers to m/s² (16384 LSB/g at ±2g)
|
|
19
|
+
const [gx, gy, gz] = await imu.gyro(); // Read 3-axis angular rate, () → [number, number, number] rad/s
|
|
20
|
+
// converts raw gyro registers to rad/s (131 LSB/(°/s) at ±250dps)
|
|
21
|
+
const [accel, gyro, temp] = await imu.readAll(); // Read one coherent sample, () → [[number x3], [number x3], number] m/s², rad/s, °C
|
|
22
|
+
// burst-reads registers 0x2D…0x3A so all three belong to one sample
|
|
23
|
+
const t = await imu.temperature(); // Read die temperature, () → number °C
|
|
24
|
+
// converts raw temp register: raw/333.87 + 21.0
|
|
25
|
+
|
|
26
|
+
const [rax, ray, raz] = await imu.accelRaw(); // Read raw accel values, () → [number, number, number]
|
|
27
|
+
// returns signed 16-bit accelerometer counts
|
|
28
|
+
const [rgx, rgy, rgz] = await imu.gyroRaw(); // Read raw gyro values, () → [number, number, number]
|
|
29
|
+
// returns signed 16-bit gyroscope counts
|
|
30
|
+
|
|
31
|
+
await imu.configureGyro(1); // Configure gyro range, (fullScale=0) → void
|
|
32
|
+
// sets GYRO_FS_SEL: 0=±250, 1=±500, 2=±1000, 3=±2000 dps
|
|
33
|
+
await imu.configureAccel(1); // Configure accel range, (fullScale=0) → void
|
|
34
|
+
// sets ACCEL_FS_SEL: 0=±2g, 1=±4g, 2=±8g, 3=±16g
|
|
35
|
+
await imu.configureFilters(3, 3, true, true); // Configure low-pass filters, (gyroDlpf=3, accelDlpf=3, gyroFilter=true, accelFilter=true) → void
|
|
36
|
+
// sets *_DLPFCFG and *_FCHOICE; filter false bypasses the filter
|
|
37
|
+
await imu.configureSampleRate(10, 10); // Configure sample rate, (gyroDiv=10, accelDiv=10) → void
|
|
38
|
+
// sets the dividers; ODR = 1125 Hz / (1 + div)
|
|
39
|
+
await imu.setSensors(true, true); // Power sensors, (accel=true, gyro=true) → void
|
|
40
|
+
// writes PWR_MGMT_2 DISABLE_ACCEL / DISABLE_GYRO
|
|
41
|
+
await imu.setLowPower(false, false, 0, 0); // Configure duty-cycled mode, (accel=false, gyro=false, accelAvg=0, gyroAvg=0) → void
|
|
42
|
+
// writes LP_CONFIG and averaging; LP_EN is set last
|
|
43
|
+
await imu.setGyroOffset(0, 0, 0); // Set gyro offsets, (x, y, z) rad/s → void
|
|
44
|
+
// writes the 16-bit offset-cancellation registers (0.0305 dps/LSB)
|
|
45
|
+
const [ox, oy, oz] = await imu.getAccelOffset(); // Read accel offset codes, () → [number, number, number]
|
|
46
|
+
// reads the factory-trimmed 15-bit offsets (0.98 mg/LSB)
|
|
47
|
+
await imu.setAccelOffset(ox, oy, oz); // Write accel offset codes, (x, y, z) → void
|
|
48
|
+
// writes the 15-bit offsets back unchanged
|
|
49
|
+
|
|
50
|
+
await imu.enableMag(); // Enable magnetometer, () → void
|
|
51
|
+
// starts the I²C master and reads the AK09916 block continuously
|
|
52
|
+
const [mx, my, mz] = await imu.mag(); // Read 3-axis magnetic field, () → [number, number, number] µT
|
|
53
|
+
// converts raw counts at 0.15 µT/LSB; rejects on HOFL
|
|
54
|
+
const [rmx, rmy, rmz] = await imu.magRaw(); // Read raw mag values, () → [number, number, number]
|
|
55
|
+
// returns signed 16-bit magnetometer counts
|
|
56
|
+
const magReady = await imu.magDataReady(); // Check magnetometer data ready, () → boolean
|
|
57
|
+
// reads ST1.DRDY from the shadowed block
|
|
58
|
+
await imu.magSetMode(8); // Set magnetometer mode, (mode) → void
|
|
59
|
+
// writes CNTL2: 0=off, 1=single, 2/4/6/8=continuous 1-4
|
|
60
|
+
const who = await imu.auxReadByte(0x0C, 0x01); // Read auxiliary byte, (address, register) → number
|
|
61
|
+
// reads AK09916 WIA2 (0x09) through I²C-master slave 4
|
|
62
|
+
await imu.auxWriteByte(0x0C, 0x31, 0x08); // Write auxiliary byte, (address, register, value) → void
|
|
63
|
+
// writes AK09916 CNTL2 through slave 4
|
|
64
|
+
|
|
65
|
+
const ready = await imu.dataReady(); // Check data ready flag, () → boolean
|
|
66
|
+
// reads INT_STATUS_1.RAW_DATA_0_RDY_INT
|
|
67
|
+
|
|
68
|
+
await imu.enableFifo(true, true, false); // Enable FIFO sources, (accel=true, gyro=true, temp=false) → void
|
|
69
|
+
// sets FIFO_EN_2 and USER_CTRL.FIFO_EN, then resets the FIFO
|
|
70
|
+
await imu.setFifoMode(false); // Select FIFO mode, (snapshot=false) → void
|
|
71
|
+
// 0 = stream (overwrite oldest), 1 = snapshot (stop when full)
|
|
72
|
+
await sleep(50);
|
|
73
|
+
const count = await imu.fifoCount(); // Read FIFO byte count, () → number
|
|
74
|
+
// reads FIFO_COUNTH/L: number of bytes available
|
|
75
|
+
const data = await imu.readFifo(); // Read FIFO data, (count?) → Buffer
|
|
76
|
+
// reads available bytes from FIFO_R_W in chunks
|
|
77
|
+
await imu.resetFifo(); // Reset FIFO buffer, () → void
|
|
78
|
+
// asserts and de-asserts FIFO_RST
|
|
79
|
+
|
|
80
|
+
await imu.setWakeOnMotion(200, true); // Arm wake-on-motion, (thresholdMg, enabled=true) → void
|
|
81
|
+
// writes ACCEL_WOM_THR (4 mg/LSB) and ACCEL_INTEL_CTRL
|
|
82
|
+
await imu.setInterruptPin(false, false, true, false); // Configure INT1 pin, (activeLow=false, openDrain=false, latch=false, anyReadClears=false) → void
|
|
83
|
+
// writes INT_PIN_CFG
|
|
84
|
+
await imu.enableInterrupt(ICM20948Full.SOURCE_DATA_READY); // Enable interrupt source, (source) → void
|
|
85
|
+
// sets the source's enable bit in INT_ENABLE*
|
|
86
|
+
const status = await imu.pollInterrupt(); // Read interrupt status, () → number
|
|
87
|
+
// reads INT_STATUS, _1, _2 (clearing them) into SOURCE_* bits
|
|
88
|
+
await imu.disableInterrupt(ICM20948Full.SOURCE_DATA_READY); // Disable interrupt source, (source) → void
|
|
89
|
+
// clears the source's enable bit
|
|
90
|
+
await imu.onInterrupt((s) => console.log('INT', s)); // Subscribe to INT1, (callback) → void
|
|
91
|
+
// callback receives the status from pollInterrupt
|
|
92
|
+
await imu.offInterrupt(); // Unsubscribe, () → void
|
|
93
|
+
// removes the callback
|
|
94
|
+
|
|
95
|
+
const [stAccel, stGyro] = await imu.selfTest(); // Run self-test, () → [[number x3], [number x3]]
|
|
96
|
+
// raw response (enabled minus disabled) per axis; no pass/fail
|
|
97
|
+
const tbc = await imu.readTimebaseCorrection(); // Read PLL time-base correction, () → number %
|
|
98
|
+
// reads TIMEBASE_CORRECTION_PLL (0.079 %/LSB, signed)
|
|
99
|
+
|
|
100
|
+
await imu.setSleep(true); // Enter sleep mode, (sleep=true) → void
|
|
101
|
+
// sets SLEEP bit in PWR_MGMT_1
|
|
102
|
+
await sleep(10);
|
|
103
|
+
await imu.setSleep(false); // Wake from sleep, (sleep=true) → void
|
|
104
|
+
// clears SLEEP bit in PWR_MGMT_1
|
|
105
|
+
|
|
106
|
+
console.log('t=%s mag=%s,%s,%s ready=%s', t.toFixed(2), mx.toFixed(1), my.toFixed(1), mz.toFixed(1), ready);
|
|
107
|
+
void [ax, ay, az, gx, gy, gz, accel, gyro, temp, rax, ray, raz, rgx, rgy, rgz, rmx, rmy, rmz,
|
|
108
|
+
magReady, who, count, data, status, stAccel, stGyro, tbc];
|
|
109
|
+
await imu.reset(); // Reset the device, () → void
|
|
110
|
+
// DEVICE_RESET and wait; create a new driver afterwards
|
|
111
|
+
connection.close();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { ICM20948Full } = require('../../../src/chips/imu/icm20948');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x68', 16);
|
|
8
|
+
|
|
9
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
10
|
+
|
|
11
|
+
async function main() {
|
|
12
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
13
|
+
|
|
14
|
+
// --- Configure for motion and heading monitoring ---
|
|
15
|
+
// ±4g / ±500dps give headroom for sharper motion; DLPFCFG 3 with a divider of
|
|
16
|
+
// 10 keeps a ~100 Hz sample rate with a ~50 Hz noise bandwidth. The
|
|
17
|
+
// magnetometer runs through the chip's own I²C master, so one connection
|
|
18
|
+
// is enough.
|
|
19
|
+
const imu = new ICM20948Full(connection); // Create ICM20948 driver, (connection) → void
|
|
20
|
+
await imu.configureAccel(1); // Configure accel range, (fullScale=0) → void
|
|
21
|
+
await imu.configureGyro(1); // Configure gyro range, (fullScale=0) → void
|
|
22
|
+
await imu.configureFilters(3, 3); // Configure low-pass filters, (gyroDlpf=3, accelDlpf=3, gyroFilter=true, accelFilter=true) → void
|
|
23
|
+
await imu.configureSampleRate(10, 10); // Configure sample rate, (gyroDiv=10, accelDiv=10) → void
|
|
24
|
+
await imu.enableMag(); // Enable magnetometer, () → void
|
|
25
|
+
|
|
26
|
+
console.log('ICM-20948 die temperature: %s C', (await imu.temperature()).toFixed(1)); // Read die temperature, () → number °C
|
|
27
|
+
|
|
28
|
+
// --- Arm wake-on-motion and route it to the interrupt status ---
|
|
29
|
+
// A 200 mg threshold flags a bump; pollInterrupt() reports it as
|
|
30
|
+
// SOURCE_WAKE_ON_MOTION without needing the INT pin wired.
|
|
31
|
+
await imu.setWakeOnMotion(200); // Arm wake-on-motion, (thresholdMg, enabled=true) → void
|
|
32
|
+
await imu.enableInterrupt(ICM20948Full.SOURCE_WAKE_ON_MOTION); // Enable interrupt source, (source) → void
|
|
33
|
+
|
|
34
|
+
// --- Stream accelerometer + gyro into the FIFO ---
|
|
35
|
+
// Stream mode overwrites the oldest data; once per second the FIFO is
|
|
36
|
+
// drained and counted in 12-byte frames (accel X, Y, Z, gyro X, Y, Z).
|
|
37
|
+
await imu.enableFifo(true, true); // Enable FIFO sources, (accel=true, gyro=true, temp=false) → void
|
|
38
|
+
await imu.setFifoMode(false); // Select FIFO mode, (snapshot=false) → void
|
|
39
|
+
|
|
40
|
+
console.log('roll pitch heading |gyro|');
|
|
41
|
+
|
|
42
|
+
const start = Date.now();
|
|
43
|
+
let lastFifo = start;
|
|
44
|
+
while (Date.now() - start < 60000) {
|
|
45
|
+
const [[ax, ay, az], [gx, gy, gz]] = await imu.readAll(); // Read one coherent sample, () → [[number x3], [number x3], number] m/s², rad/s, °C
|
|
46
|
+
const [mx, my, mz] = await imu.mag(); // Read 3-axis magnetic field, () → [number, number, number] µT
|
|
47
|
+
|
|
48
|
+
// --- Compute roll, pitch and a tilt-compensated heading ---
|
|
49
|
+
// The magnetometer frame is rotated 180° about X relative to the
|
|
50
|
+
// inertial frame, so (mx, -my, -mz) maps it into the accel frame
|
|
51
|
+
// before the tilt compensation rotates it into the horizontal plane.
|
|
52
|
+
const roll = Math.atan2(ay, az);
|
|
53
|
+
const pitch = Math.atan2(-ax, Math.sqrt(ay * ay + az * az));
|
|
54
|
+
const bx = mx, by = -my, bz = -mz;
|
|
55
|
+
const xh = bx * Math.cos(pitch) + bz * Math.sin(pitch);
|
|
56
|
+
const yh = bx * Math.sin(roll) * Math.sin(pitch) + by * Math.cos(roll)
|
|
57
|
+
- bz * Math.sin(roll) * Math.cos(pitch);
|
|
58
|
+
const heading = (Math.atan2(-yh, xh) * 180 / Math.PI + 360) % 360;
|
|
59
|
+
const gyroMag = Math.sqrt(gx * gx + gy * gy + gz * gz);
|
|
60
|
+
|
|
61
|
+
console.log('%s %s %s %s',
|
|
62
|
+
(roll * 180 / Math.PI).toFixed(1), (pitch * 180 / Math.PI).toFixed(1),
|
|
63
|
+
heading.toFixed(1), gyroMag.toFixed(3));
|
|
64
|
+
|
|
65
|
+
const status = await imu.pollInterrupt(); // Read interrupt status, () → number
|
|
66
|
+
if (status & ICM20948Full.SOURCE_WAKE_ON_MOTION) console.log('MOTION');
|
|
67
|
+
|
|
68
|
+
if (Date.now() - lastFifo >= 1000) {
|
|
69
|
+
lastFifo = Date.now();
|
|
70
|
+
const count = await imu.fifoCount(); // Read FIFO byte count, () → number
|
|
71
|
+
const data = await imu.readFifo(); // Read FIFO data, (count?) → Buffer
|
|
72
|
+
console.log('FIFO: %d bytes (count %d), %d frames', data.length, count, Math.floor(data.length / 12));
|
|
73
|
+
}
|
|
74
|
+
await sleep(100);
|
|
75
|
+
}
|
|
76
|
+
connection.close();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
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 { ICM20948Minimal } = require('../../../src/chips/imu/icm20948');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x68', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const imu = new ICM20948Minimal(connection); // Create ICM20948 driver, (connection) → void
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
while (true) {
|
|
14
|
+
const [ax, ay, az] = await imu.accel(); // Read 3-axis acceleration, () → [number, number, number] m/s²
|
|
15
|
+
const [gx, gy, gz] = await imu.gyro(); // Read 3-axis angular rate, () → [number, number, number] rad/s
|
|
16
|
+
console.log('accel: %s %s %s gyro: %s %s %s',
|
|
17
|
+
ax.toFixed(2), ay.toFixed(2), az.toFixed(2), gx.toFixed(2), gy.toFixed(2), gz.toFixed(2));
|
|
18
|
+
await new Promise(r => setTimeout(r, 100));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
main().catch(err => { console.error(err); process.exit(1); });
|