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,134 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { QMI8658AFull } = require('../../../src/chips/imu/qmi8658a');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x6B', 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 QMI8658AFull(connection); // Create QMI8658A driver, (connection) → void
|
|
14
|
+
const [ax, ay, az] = await imu.accel(); // Read 3-axis acceleration, () → [number, number, number] m/s²
|
|
15
|
+
// converts raw counts at 16384 LSB/g (±2 g)
|
|
16
|
+
const [gx, gy, gz] = await imu.gyro(); // Read 3-axis angular rate, () → [number, number, number] rad/s
|
|
17
|
+
// converts raw counts at 128 LSB/dps (±256 dps)
|
|
18
|
+
const [rax, ray, raz] = await imu.accelRaw(); // Read raw accel counts, () → [number, number, number]
|
|
19
|
+
// one 6-byte burst, signed 16-bit
|
|
20
|
+
const [rgx, rgy, rgz] = await imu.gyroRaw(); // Read raw gyro counts, () → [number, number, number]
|
|
21
|
+
// one 6-byte burst, signed 16-bit
|
|
22
|
+
const [accel, gyro, temp] = await imu.readAll(); // Read one coherent sample, () → [[number, number, number], [number, number, number], number] m/s², rad/s, °C
|
|
23
|
+
// one 14-byte burst of registers 0x33…0x40
|
|
24
|
+
const t = await imu.temperature(); // Read die temperature, () → number °C
|
|
25
|
+
// signed 8.8 fixed point, raw / 256
|
|
26
|
+
const ts = await imu.timestamp(); // Read sample counter, () → number
|
|
27
|
+
// 24-bit counter, wraps at 0xFFFFFF
|
|
28
|
+
const ready = await imu.dataReady(); // Check for new data, () → boolean
|
|
29
|
+
// STATUS0 aDA / gDA
|
|
30
|
+
await imu.configureAccel(1, 5); // Configure accelerometer, (fullScale=0, odr=5) → void
|
|
31
|
+
// CTRL2: 0=±2, 1=±4, 2=±8, 3=±16 g; ODR code 5 = 224.2 Hz
|
|
32
|
+
await imu.configureGyro(5, 5); // Configure gyroscope, (fullScale=4, odr=5) → void
|
|
33
|
+
// CTRL3: 0=±16 … 7=±2048 dps
|
|
34
|
+
await imu.configureFilters(true, 3, true, 3); // Configure low-pass filters, (accelFilter=true, accelMode=3, gyroFilter=true, gyroMode=3) → void
|
|
35
|
+
// CTRL5: mode 3 = 13.37 % of ODR
|
|
36
|
+
await imu.setSensors(true, true, false); // Enable sensors, (accel=true, gyro=true, gyroSnooze=false) → void
|
|
37
|
+
// CTRL7 aEN / gEN / gSN
|
|
38
|
+
await imu.configureFifo(2, 1, 16); // Configure FIFO, (mode=2, size=3, watermark=0) → void
|
|
39
|
+
// stream mode, 32 samples, watermark 16
|
|
40
|
+
await sleep(500);
|
|
41
|
+
const fifoFlags = await imu.fifoStatus(); // Read FIFO flags, () → number
|
|
42
|
+
// raw FIFO_STATUS byte
|
|
43
|
+
const fifoBytes = await imu.fifoCount(); // Read FIFO fill, () → number bytes
|
|
44
|
+
// two bytes per sample word
|
|
45
|
+
const data = await imu.readFifo(); // Drain the FIFO, (count?) → Buffer
|
|
46
|
+
// REQ_FIFO, chunked FIFO_DATA reads, FIFO_RD_MODE cleared
|
|
47
|
+
await imu.resetFifo(); // Reset the FIFO, () → void
|
|
48
|
+
// CTRL9 RST_FIFO
|
|
49
|
+
await imu.configureFifo(0); // Back to bypass, (mode=2, size=3, watermark=0) → void
|
|
50
|
+
// bypass re-enables data-ready
|
|
51
|
+
await imu.configureAnyMotion(0.25, 3, 0b111, false); // Configure any-motion, (thresholdG=0.25, window=3, axes=0b111, logicAnd=false) → void
|
|
52
|
+
// two CTRL9 CONFIGURE_MOTION sets
|
|
53
|
+
await imu.configureNoMotion(0.125, 30, 0b111, true); // Configure no-motion, (thresholdG=0.125, window=30, axes=0b111, logicAnd=true) → void
|
|
54
|
+
// same command, no-motion fields
|
|
55
|
+
await imu.configureSignificantMotion(150, 600); // Configure significant motion, (waitWindow=150, confirmWindow=600) → void
|
|
56
|
+
// wait and confirm windows in samples
|
|
57
|
+
await imu.enableMotion(true, true, true); // Enable motion engines, (anyMotion=false, noMotion=false, significant=false) → void
|
|
58
|
+
// CTRL8 bits 1–3
|
|
59
|
+
await imu.enableMotion(); // Disable motion engines, (anyMotion=false, noMotion=false, significant=false) → void
|
|
60
|
+
// clears CTRL8 bits 1–3
|
|
61
|
+
await imu.configureTap(); // Configure tap engine, (priority=0, peakWindow=20, tapWindow=50, doubleTapWindow=250, alpha=0.0625, gamma=0.25, peakThreshold=0.8, quietThreshold=0.4) → void
|
|
62
|
+
// two CTRL9 CONFIGURE_TAP sets
|
|
63
|
+
await imu.enableTap(true); // Enable tap engine, (enabled=true) → void
|
|
64
|
+
// CTRL8.Tap_EN
|
|
65
|
+
const [taps, axis, negative] = await imu.tapStatus(); // Read tap event, () → [number, number, boolean]
|
|
66
|
+
// count 0/1/2, axis 0–3, polarity
|
|
67
|
+
await imu.enableTap(false); // Disable tap engine, (enabled=true) → void
|
|
68
|
+
// clears CTRL8.Tap_EN
|
|
69
|
+
await imu.configurePedometer(); // Configure pedometer, (sampleCount=50, peakToPeakMg=200, peakMg=100, timeUp=200, timeLow=20, entryCount=10, precision=0, reportEvery=4) → void
|
|
70
|
+
// two CTRL9 CONFIGURE_PEDOMETER sets (50 Hz values)
|
|
71
|
+
await imu.enablePedometer(true); // Enable pedometer, (enabled=true) → void
|
|
72
|
+
// CTRL8.Pedo_EN; 0→1 resets the count
|
|
73
|
+
const steps = await imu.stepCount(); // Read step count, () → number
|
|
74
|
+
// 24-bit count
|
|
75
|
+
await imu.resetStepCount(); // Clear step count, () → void
|
|
76
|
+
// CTRL9 RESET_PEDOMETER
|
|
77
|
+
await imu.enablePedometer(false); // Disable pedometer, (enabled=true) → void
|
|
78
|
+
// clears CTRL8.Pedo_EN
|
|
79
|
+
await imu.setAccelOffset(0, 0, 0); // Set accel offset, (x, y, z) m/s² → void
|
|
80
|
+
// signed 4.12 g via CTRL9
|
|
81
|
+
await imu.setGyroOffset(0, 0, 0); // Set gyro offset, (x, y, z) rad/s → void
|
|
82
|
+
// signed 11.5 dps via CTRL9
|
|
83
|
+
const [calOk, codStatus] = await imu.calibrateGyro(); // Calibrate gyroscope, () → [boolean, number]
|
|
84
|
+
// calibration-on-demand, ~1.5 s, keep still
|
|
85
|
+
const [gainX, gainY, gainZ] = await imu.getGyroGains(); // Read gyro gains, () → [number, number, number]
|
|
86
|
+
// raw 16-bit gains from dVX…dVZ
|
|
87
|
+
await imu.setGyroGains(gainX, gainY, gainZ); // Restore gyro gains, (x, y, z) → void
|
|
88
|
+
// CTRL9 APPLY_GYRO_GAINS
|
|
89
|
+
const [accelOk, accelSelf] = await imu.selfTestAccel(); // Run accel self-test, () → [boolean, [number, number, number]] m/s²
|
|
90
|
+
// passes when every axis exceeds 200 mg
|
|
91
|
+
const [gyroOk, gyroSelf] = await imu.selfTestGyro(); // Run gyro self-test, () → [boolean, [number, number, number]] rad/s
|
|
92
|
+
// passes when every axis exceeds 300 dps
|
|
93
|
+
const info = await imu.deviceInfo(); // Read device info, () → {revision, firmware, usid}
|
|
94
|
+
// CTRL9 COPY_USID: revision, firmware, usid
|
|
95
|
+
await imu.setPullups(true, true, true, true); // Control pull-ups, (aux=true, sdx=true, cs=true, i2c=true) → void
|
|
96
|
+
// CTRL9 SET_RPU; false disables a group
|
|
97
|
+
await imu.setWakeOnMotion(100, 1, 0, 0); // Arm wake-on-motion, (thresholdMg, interruptPin=1, initialLevel=0, blankingSamples=0) → void
|
|
98
|
+
// accel 128 Hz ±2 g, CTRL9 WRITE_WOM_SETTING
|
|
99
|
+
await imu.setWakeOnMotion(0); // Leave wake-on-motion, (thresholdMg, interruptPin=1, initialLevel=0, blankingSamples=0) → void
|
|
100
|
+
// threshold 0 exits WoM mode
|
|
101
|
+
await imu.setSensors(true, true); // Re-enable sensors, (accel=true, gyro=true, gyroSnooze=false) → void
|
|
102
|
+
// restores normal operation
|
|
103
|
+
await imu.setInterruptPins(2, 2); // Route interrupts, (fifoPin=2, activityPin=2) → void
|
|
104
|
+
// CTRL1.FIFO_INT_SEL and CTRL8.ACTIVITY_INT_SEL
|
|
105
|
+
await imu.enableInterrupt(QMI8658AFull.SOURCE_DATA_READY); // Enable interrupt source, (source) → void
|
|
106
|
+
// clears DRDY_DIS, enables INT2
|
|
107
|
+
const status = await imu.pollInterrupt(); // Poll interrupt status, () → number
|
|
108
|
+
// STATUS0, STATUS1 and FIFO_STATUS as SOURCE_* bits
|
|
109
|
+
await imu.onInterrupt((s) => console.log('INT status 0x%s', s.toString(16))); // Subscribe to interrupts, (callback) → void
|
|
110
|
+
// uses the connection's interrupt pin or a 5 ms polling timer
|
|
111
|
+
await sleep(200);
|
|
112
|
+
await imu.offInterrupt(); // Unsubscribe, () → void
|
|
113
|
+
// stops the callback
|
|
114
|
+
await imu.disableInterrupt(QMI8658AFull.SOURCE_DATA_READY); // Disable interrupt source, (source) → void
|
|
115
|
+
// sets DRDY_DIS, releases INT2
|
|
116
|
+
await imu.setSyncSample(true); // Enable SyncSample, (enabled) → void
|
|
117
|
+
// locks each sample; reads wait for Avail and Locked
|
|
118
|
+
await imu.setSyncSample(false); // Disable SyncSample, (enabled) → void
|
|
119
|
+
// restores the AHB clock gating
|
|
120
|
+
await imu.setPowerDown(true); // Power down, (down=true) → void
|
|
121
|
+
// CTRL1.SensorDisable after disabling the sensors
|
|
122
|
+
await imu.setPowerDown(false); // Wake, (down=true) → void
|
|
123
|
+
// clears SensorDisable
|
|
124
|
+
await imu.setSensors(true, true); // Re-enable sensors, (accel=true, gyro=true, gyroSnooze=false) → void
|
|
125
|
+
// needs 150 ms gyroscope turn-on
|
|
126
|
+
await sleep(200);
|
|
127
|
+
await imu.reset(); // Soft reset, () → void
|
|
128
|
+
// RESET register, waits 15 ms
|
|
129
|
+
console.log(accel, gyro, temp, t, ts, ready, rax, ray, raz, rgx, rgy, rgz, fifoFlags, fifoBytes, data.length,
|
|
130
|
+
taps, axis, negative, steps, calOk, codStatus, accelOk, gyroOk, accelSelf, gyroSelf, info, status);
|
|
131
|
+
console.log('Complete example done');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { QMI8658AFull } = require('../../../src/chips/imu/qmi8658a');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x6B', 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 QMI8658AFull(connection); // Create QMI8658A driver, (connection) → void
|
|
14
|
+
|
|
15
|
+
// --- Identify the part and run the built-in checks ---
|
|
16
|
+
// The revision, firmware version and USID identify the silicon; the
|
|
17
|
+
// self-tests need the board lying still and the gyro calibration trims the
|
|
18
|
+
// X/Y gains before real use.
|
|
19
|
+
const info = await imu.deviceInfo(); // Read device info, () → {revision, firmware, usid}
|
|
20
|
+
console.log('revision 0x%s firmware %s usid %s', info.revision.toString(16), info.firmware.toString('hex'), info.usid.toString('hex'));
|
|
21
|
+
const [accelOk] = await imu.selfTestAccel(); // Run accel self-test, () → [boolean, [number, number, number]] m/s²
|
|
22
|
+
console.log('accel self-test:', accelOk ? 'PASS' : 'FAIL');
|
|
23
|
+
const [gyroOk] = await imu.selfTestGyro(); // Run gyro self-test, () → [boolean, [number, number, number]] rad/s
|
|
24
|
+
console.log('gyro self-test:', gyroOk ? 'PASS' : 'FAIL');
|
|
25
|
+
const [calOk, codStatus] = await imu.calibrateGyro(); // Calibrate gyroscope, () → [boolean, number]
|
|
26
|
+
console.log('gyro calibration:', calOk ? 'OK' : 'FAILED 0x' + codStatus.toString(16));
|
|
27
|
+
|
|
28
|
+
// --- Configure ±4 g / ±512 dps at 112.1 Hz ---
|
|
29
|
+
// ODR code 6 gives 112.1 Hz for both sensors; mode 3 filters keep the
|
|
30
|
+
// noise bandwidth near 15 Hz.
|
|
31
|
+
await imu.configureAccel(1, 6); // Configure accelerometer, (fullScale=0, odr=5) → void
|
|
32
|
+
await imu.configureGyro(5, 6); // Configure gyroscope, (fullScale=4, odr=5) → void
|
|
33
|
+
await imu.configureFilters(true, 3, true, 3); // Configure low-pass filters, (accelFilter=true, accelMode=3, gyroFilter=true, gyroMode=3) → void
|
|
34
|
+
|
|
35
|
+
// --- Step counting and tap detection ---
|
|
36
|
+
// The pedometer defaults suit ~50 Hz, so the window parameters are scaled
|
|
37
|
+
// for 112 Hz; the tap engine runs best above 200 Hz and is only a coarse
|
|
38
|
+
// detector at this rate.
|
|
39
|
+
await imu.configurePedometer(112, 200, 100, 450, 45, 10, 0, 1); // Configure pedometer, (sampleCount=50, …) → void
|
|
40
|
+
await imu.enablePedometer(true); // Enable pedometer, (enabled=true) → void
|
|
41
|
+
await imu.configureTap(); // Configure tap engine, (priority=0, …) → void
|
|
42
|
+
await imu.enableTap(true); // Enable tap engine, (enabled=true) → void
|
|
43
|
+
|
|
44
|
+
// --- Stream into the FIFO ---
|
|
45
|
+
// Stream mode with 32 samples and a watermark of 16 is drained once per
|
|
46
|
+
// second; each sample is a 12-byte frame (accel XYZ then gyro XYZ).
|
|
47
|
+
await imu.configureFifo(2, 1, 16); // Configure FIFO, (mode=2, size=3, watermark=0) → void
|
|
48
|
+
await sleep(200);
|
|
49
|
+
|
|
50
|
+
const start = Date.now();
|
|
51
|
+
let lastFifo = start;
|
|
52
|
+
while (Date.now() - start < 60000) {
|
|
53
|
+
if (await imu.dataReady()) { // Check for new data, () → boolean
|
|
54
|
+
const [[ax, ay, az], [gx, gy, gz], temp] = await imu.readAll(); // Read one coherent sample, () → [[number, number, number], [number, number, number], number] m/s², rad/s, °C
|
|
55
|
+
const roll = Math.atan2(ay, az) * 180 / Math.PI;
|
|
56
|
+
const pitch = Math.atan2(-ax, Math.sqrt(ay * ay + az * az)) * 180 / Math.PI;
|
|
57
|
+
const mag = Math.sqrt(gx * gx + gy * gy + gz * gz);
|
|
58
|
+
console.log('roll %s pitch %s |gyro| %s rad/s %s C steps %d', roll.toFixed(1), pitch.toFixed(1),
|
|
59
|
+
mag.toFixed(3), temp.toFixed(1), await imu.stepCount()); // Read step count, () → number
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// --- Report taps ---
|
|
63
|
+
// pollInterrupt() flags a tap without the INT pin wired.
|
|
64
|
+
const status = await imu.pollInterrupt(); // Poll interrupt status, () → number
|
|
65
|
+
if (status & QMI8658AFull.SOURCE_TAP) {
|
|
66
|
+
const [taps, axis] = await imu.tapStatus(); // Read tap event, () → [number, number, boolean]
|
|
67
|
+
console.log('TAP %s on axis %s', taps === 2 ? 'double' : 'single', axis ? 'XYZ'[axis - 1] : '?');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// --- Drain the FIFO once per second ---
|
|
71
|
+
if (Date.now() - lastFifo >= 1000) {
|
|
72
|
+
lastFifo = Date.now();
|
|
73
|
+
const n = await imu.fifoCount(); // Read FIFO fill, () → number bytes
|
|
74
|
+
const data = await imu.readFifo(); // Drain the FIFO, (count?) → Buffer
|
|
75
|
+
console.log('FIFO: %d bytes (%d), %d frames', n, data.length, Math.floor(data.length / 12));
|
|
76
|
+
}
|
|
77
|
+
await sleep(100);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
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 { QMI8658AMinimal } = require('../../../src/chips/imu/qmi8658a');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x6B', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const imu = new QMI8658AMinimal(connection); // Create QMI8658A 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); });
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
|
+
const { Mcp23008Full } = require('periph/src/chips/io_expander/mcp23008');
|
|
4
|
+
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
7
|
+
const chip = new Mcp23008Full(connection); // Create MCP23008 full driver, (connection, addr=0x20)
|
|
8
|
+
// clears OLAT; all pins start as inputs
|
|
9
|
+
await chip.ready;
|
|
10
|
+
|
|
11
|
+
// --- Pin proxy — async API ---
|
|
12
|
+
const p0 = chip.pin(0, 'out'); // Get full pin proxy, (n, direction='in') → FullPin
|
|
13
|
+
// GP0 as output; GP7 is a normal bidirectional pin
|
|
14
|
+
console.log('direction:', p0.direction); // direction property, () → string 'in'|'out'
|
|
15
|
+
await p0.write(1); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
// sets the OLAT bit via the shadow
|
|
17
|
+
const v = await p0.read(); // Read pin, () → Promise<int 0|1>
|
|
18
|
+
// reads GPIO (actual pin level)
|
|
19
|
+
console.log('GP0 level:', v);
|
|
20
|
+
await p0.setDirection('in'); // Set direction, (direction) → Promise<void>
|
|
21
|
+
// writes IODIR; OLAT is latched first for outputs
|
|
22
|
+
|
|
23
|
+
// --- Port-level operations ---
|
|
24
|
+
await chip.writePort(0, 0b00001111); // Write all 8 pins, (port=0, mask) → Promise<void>
|
|
25
|
+
// whole-port OLAT write
|
|
26
|
+
const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
|
|
27
|
+
// GPIO register
|
|
28
|
+
console.log('GPIO=0x' + port.toString(16).padStart(2, '0'));
|
|
29
|
+
|
|
30
|
+
// --- Configuration ---
|
|
31
|
+
await chip.configurePullup(0xF0); // Configure pull-ups, (mask) → Promise<void>
|
|
32
|
+
// 100 kΩ pull-ups on GP4–GP7 (inputs only)
|
|
33
|
+
await chip.configurePolarity(0x00); // Configure input polarity, (mask) → Promise<void>
|
|
34
|
+
// 1 bits invert the GPIO read
|
|
35
|
+
await chip.setDefaultValue(0x00); // Set DEFVAL, (mask) → Promise<void>
|
|
36
|
+
// reference for default-compare interrupts
|
|
37
|
+
await chip.configureIntPin({ openDrain: false, activeHigh: false }); // Configure INT output, (options) → Promise<void>
|
|
38
|
+
// push-pull, active-low (IOCON ODR/INTPOL)
|
|
39
|
+
await chip.setSequential(true); // Sequential access, (enabled) → Promise<void>
|
|
40
|
+
// IOCON SEQOP = 0 (driver default)
|
|
41
|
+
const iodir = await chip.readRegister(0x00); // Read raw register, (reg) → Promise<int>
|
|
42
|
+
// escape hatch for debugging
|
|
43
|
+
console.log('IODIR=0x' + iodir.toString(16).padStart(2, '0'));
|
|
44
|
+
await chip.writeRegister(0x02, 0x10); // Write raw register, (reg, value) → Promise<void>
|
|
45
|
+
// GPINTEN: arm GP4
|
|
46
|
+
|
|
47
|
+
// --- Interrupts ---
|
|
48
|
+
await chip.enablePinInterrupt(0xF0); // Arm interrupt-on-change, (mask) → Promise<void>
|
|
49
|
+
// GP4–GP7
|
|
50
|
+
await chip.onInterrupt((status) => { // Subscribe to INT, (callback, options) → Promise<void>
|
|
51
|
+
console.log('interrupt, INTF=0x' + status.toString(16)); // INT line if wired, else 5 ms polling
|
|
52
|
+
}, { mode: 'default' });
|
|
53
|
+
const p4 = chip.pin(4, 'in');
|
|
54
|
+
await p4.watch((pin) => console.log('GP4 changed'), 'change'); // Watch a pin, (handler, trigger='change') → Promise<void>
|
|
55
|
+
// arms GP4 in GPINTEN, edge filtered in software
|
|
56
|
+
const flags = await chip.pollInterrupt(); // Read INTF and clear, () → Promise<int>
|
|
57
|
+
// INTCAP is read and discarded
|
|
58
|
+
const cap = await chip.readCapture(); // Read captured port, () → Promise<int>
|
|
59
|
+
// port snapshot at the interrupt
|
|
60
|
+
console.log('INTF=0x' + flags.toString(16) + ' INTCAP=0x' + cap.toString(16));
|
|
61
|
+
await p4.unwatch(); // Stop watching, () → Promise<void>
|
|
62
|
+
// clears its GPINTEN bit
|
|
63
|
+
await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
|
|
64
|
+
// stops delivery
|
|
65
|
+
await chip.reset(); // Restore reset values, () → Promise<void>
|
|
66
|
+
// rewrites all registers, clears shadows
|
|
67
|
+
await connection.close();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
main();
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* MCP23008 demo — LED scanner with interrupt-driven button input.
|
|
4
|
+
*
|
|
5
|
+
* Hardware:
|
|
6
|
+
* GP0–GP3: four LEDs (anode → GP via 220 Ω, cathode → GND; active-high)
|
|
7
|
+
* GP4–GP7: four push buttons (pin → GND when pressed; 100 kΩ pull-up enabled)
|
|
8
|
+
*
|
|
9
|
+
* Runs a scanner pattern on the LEDs. Button presses raise an interrupt-on-change;
|
|
10
|
+
* the callback prints the INTF mask and INTCAP snapshot and holds the LED
|
|
11
|
+
* matching the pressed button (GP4→GP0, …) for one second.
|
|
12
|
+
*/
|
|
13
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
14
|
+
const { Mcp23008Full } = require('periph/src/chips/io_expander/mcp23008');
|
|
15
|
+
|
|
16
|
+
async function main() {
|
|
17
|
+
// --- Initialise bus and driver ---
|
|
18
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
19
|
+
const chip = new Mcp23008Full(connection); // Create MCP23008 full driver, (connection, addr=0x20)
|
|
20
|
+
await chip.ready;
|
|
21
|
+
|
|
22
|
+
// --- Configure LEDs and buttons ---
|
|
23
|
+
// GP0–GP3 are outputs; GP4–GP7 stay inputs with pull-ups, so a pressed button reads 0.
|
|
24
|
+
for (let n = 0; n < 4; n++) await chip.pin(n, 'out').setDirection('out'); // Set LED direction, (direction) → Promise<void>
|
|
25
|
+
await chip.configurePullup(0xF0); // Configure pull-ups, (mask) → Promise<void>
|
|
26
|
+
|
|
27
|
+
// --- Arm interrupt-on-change on the buttons ---
|
|
28
|
+
// Any button edge raises INT (or the 5 ms poll sees it); the callback reports INTF and
|
|
29
|
+
// INTCAP, then holds the matching LED.
|
|
30
|
+
let holdUntil = 0;
|
|
31
|
+
let holdMask = 0;
|
|
32
|
+
await chip.enablePinInterrupt(0xF0); // Arm GP4–GP7, (mask) → Promise<void>
|
|
33
|
+
await chip.onInterrupt(async (status) => { // Subscribe to INT, (callback, options) → Promise<void>
|
|
34
|
+
const captured = await chip.readCapture(); // Read port snapshot, () → Promise<int>
|
|
35
|
+
console.log('button INTF=0x' + status.toString(16) + ' INTCAP=0x' + captured.toString(16));
|
|
36
|
+
holdMask = (status >> 4) & 0x0F;
|
|
37
|
+
holdUntil = Date.now() + 1000;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// --- Scanner loop ---
|
|
41
|
+
let step = 0;
|
|
42
|
+
console.log('Running — press buttons GP4–GP7 to hold the matching LED');
|
|
43
|
+
const timer = setInterval(async () => {
|
|
44
|
+
const leds = Date.now() < holdUntil ? holdMask : (1 << (step % 4));
|
|
45
|
+
await chip.writePort(0, leds); // Write all 8 pins, (port=0, mask) → Promise<void>
|
|
46
|
+
const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
|
|
47
|
+
console.log('pos=' + (step % 4) + ' port=0x' + port.toString(16).padStart(2, '0'));
|
|
48
|
+
step++;
|
|
49
|
+
if (step >= 40) {
|
|
50
|
+
clearInterval(timer);
|
|
51
|
+
await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
|
|
52
|
+
await chip.writePort(0, 0x00); // Clear all outputs, (port=0, mask=0x00) → Promise<void>
|
|
53
|
+
await connection.close();
|
|
54
|
+
}
|
|
55
|
+
}, 100);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
main();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
3
|
+
const { Mcp23008Minimal } = require('periph/src/chips/io_expander/mcp23008');
|
|
4
|
+
|
|
5
|
+
async function main() {
|
|
6
|
+
const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
|
|
7
|
+
const chip = new Mcp23008Minimal(connection); // Create MCP23008 driver, (connection, addr=0x20)
|
|
8
|
+
|
|
9
|
+
const p0 = chip.pin(0, 'in'); // Get pin proxy, (n, direction='in') → Pin
|
|
10
|
+
const p7 = chip.pin(7, 'out'); // Get pin proxy, (n, direction='out') → Pin
|
|
11
|
+
|
|
12
|
+
setInterval(async () => {
|
|
13
|
+
const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
|
|
14
|
+
const level = await p0.read(); // Read pin, () → Promise<int 0|1>
|
|
15
|
+
await p7.write(level); // Write pin, (value=0|1) → Promise<void>
|
|
16
|
+
console.log('GPIO=0x' + port.toString(16).padStart(2, '0') + ' GP0=' + level);
|
|
17
|
+
}, 200);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main();
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { MB85RCFull } = require('../../../src/chips/memory/mb85rc');
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const connection = new I2CConnection(1, 0x50, null, null, 2); // Create I2C connection, (bus=1, addr=0x50, intPin=null, enPin=null, regBytes=2) → Connection
|
|
8
|
+
const fram = new MB85RCFull(connection); // Create MB85RC driver, (connection) → void
|
|
9
|
+
|
|
10
|
+
console.log('Size:', fram.size(), 'bytes'); // Get array size, () → number
|
|
11
|
+
// always 32768 for the MB85RC256V
|
|
12
|
+
fram.maxChunk = 16; // Set max bytes per bus transaction, (maxChunk=30) → void
|
|
13
|
+
console.log('maxChunk:', fram.maxChunk); // Get max bytes per bus transaction, () → number
|
|
14
|
+
|
|
15
|
+
await fram.writeByte(0x0010, 0xA5); // Write a single byte, (address 0–32767, value 0–255) → void
|
|
16
|
+
// no write cycle, no polling
|
|
17
|
+
console.log('Byte:', (await fram.readByte(0x0010)).toString(16)); // Read a single byte, (address 0–32767) → number
|
|
18
|
+
// random read
|
|
19
|
+
|
|
20
|
+
await fram.write(0x0100, Buffer.from(Array.from({ length: 40 }, (_, i) => i))); // Sequential write, (address, data) → void
|
|
21
|
+
// split into maxChunk-byte transactions
|
|
22
|
+
const data = await fram.read(0x0100, 40); // Sequential read, (address, length) → Buffer
|
|
23
|
+
// split into maxChunk-byte transactions
|
|
24
|
+
console.log('Block:', data.toString('hex'));
|
|
25
|
+
|
|
26
|
+
await fram.fill(0x0100, 40, 0xFF); // Fill a range, (address, length, value=0) → void
|
|
27
|
+
// writes value over the range in chunks
|
|
28
|
+
console.log('After fill:', (await fram.read(0x0100, 8)).toString('hex'));
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
const idConnection = new I2CConnection(1, 0x7C, null, null, 1); // Create Device ID connection, (bus=1, addr=0x7C, intPin=null, enPin=null, regBytes=1) → Connection
|
|
32
|
+
const id = await fram.readDeviceId(idConnection, 0x50); // Read Device ID, (idConnection, address=0x50) → {manufacturer, density, product}
|
|
33
|
+
// reads 3 bytes via reserved slave address 0x7C
|
|
34
|
+
console.log('Device ID:', id);
|
|
35
|
+
} catch (e) {
|
|
36
|
+
console.log('Device ID not readable on this bus');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { MB85RCFull } = require('../../../src/chips/memory/mb85rc');
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const connection = new I2CConnection(1, 0x50, null, null, 2); // Create I2C connection, (bus=1, addr=0x50, intPin=null, enPin=null, regBytes=2) → Connection
|
|
8
|
+
const fram = new MB85RCFull(connection); // Create MB85RC driver, (connection) → void
|
|
9
|
+
|
|
10
|
+
// --- Persistent boot counter ---
|
|
11
|
+
// A 4-byte big-endian counter at 0x0000 survives power loss. FRAM has no
|
|
12
|
+
// write cycle, so the increment is committed the moment the write ends.
|
|
13
|
+
const counter = (await fram.read(0x0000, 4)).readUInt32BE(0) + 1; // Sequential read, (address, length) → Buffer
|
|
14
|
+
const cbuf = Buffer.alloc(4);
|
|
15
|
+
cbuf.writeUInt32BE(counter, 0);
|
|
16
|
+
await fram.write(0x0000, cbuf); // Sequential write, (address, data) → void
|
|
17
|
+
console.log('Boot count:', counter);
|
|
18
|
+
|
|
19
|
+
// --- Ring-buffer logger, back-to-back with no delays ---
|
|
20
|
+
// 1,000 records of 16 bytes (index + counter + padding) are written one
|
|
21
|
+
// after the other without any pause — an EEPROM would need ACK polling
|
|
22
|
+
// after every page, FRAM does not.
|
|
23
|
+
const LOG_BASE = 0x0100, RECORD = 16, COUNT = 1000, RING = 200;
|
|
24
|
+
const t0 = Date.now();
|
|
25
|
+
for (let i = 0; i < COUNT; i++) {
|
|
26
|
+
const rec = Buffer.alloc(RECORD);
|
|
27
|
+
rec.writeUInt32BE(i, 0);
|
|
28
|
+
rec.writeUInt32BE(counter, 4);
|
|
29
|
+
await fram.write(LOG_BASE + (i % RING) * RECORD, rec); // Sequential write, (address, data) → void
|
|
30
|
+
}
|
|
31
|
+
const dt = (Date.now() - t0) / 1000;
|
|
32
|
+
console.log('Logged', COUNT * RECORD, 'bytes in', dt.toFixed(3), 's =', Math.round(COUNT * RECORD / dt), 'B/s');
|
|
33
|
+
|
|
34
|
+
// --- Verify the last five records ---
|
|
35
|
+
// Read back the newest records from the ring and check their contents.
|
|
36
|
+
let ok = true;
|
|
37
|
+
for (let i = COUNT - 5; i < COUNT; i++) {
|
|
38
|
+
const rec = await fram.read(LOG_BASE + (i % RING) * RECORD, RECORD); // Sequential read, (address, length) → Buffer
|
|
39
|
+
if (rec.readUInt32BE(0) !== i || rec.readUInt32BE(4) !== counter) ok = false;
|
|
40
|
+
}
|
|
41
|
+
console.log('Last 5 records verified:', ok);
|
|
42
|
+
|
|
43
|
+
// --- Clear the log area ---
|
|
44
|
+
// fill() zeroes the whole ring in chunked transactions.
|
|
45
|
+
await fram.fill(LOG_BASE, RING * RECORD, 0); // Fill a range, (address, length, value=0) → void
|
|
46
|
+
console.log('Log cleared; array size:', fram.size(), 'bytes'); // Get array size, () → number
|
|
47
|
+
|
|
48
|
+
// --- Device ID (needs the reserved address 0x7C) ---
|
|
49
|
+
// Some hosts (Linux i2c-dev) refuse addresses 0x78-0x7F; skip then.
|
|
50
|
+
try {
|
|
51
|
+
const idConnection = new I2CConnection(1, 0x7C, null, null, 1); // Create Device ID connection, (bus=1, addr=0x7C, intPin=null, enPin=null, regBytes=1) → Connection
|
|
52
|
+
console.log('Device ID:', await fram.readDeviceId(idConnection, 0x50)); // Read Device ID, (idConnection, address=0x50) → {manufacturer, density, product}
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.log('Device ID not available');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { MB85RCMinimal } = require('../../../src/chips/memory/mb85rc');
|
|
5
|
+
|
|
6
|
+
const connection = new I2CConnection(1, 0x50, null, null, 2); // Create I2C connection, (bus=1, addr=0x50, intPin=null, enPin=null, regBytes=2) → Connection
|
|
7
|
+
const fram = new MB85RCMinimal(connection); // Create MB85RC driver, (connection) → void
|
|
8
|
+
|
|
9
|
+
let counter = 0;
|
|
10
|
+
setInterval(async () => {
|
|
11
|
+
await fram.writeByte(0x0000, counter); // Write a single byte, (address 0–32767, value 0–255) → void
|
|
12
|
+
const value = await fram.readByte(0x0000); // Read a single byte, (address 0–32767) → number
|
|
13
|
+
console.log('Stored:', value);
|
|
14
|
+
counter = (counter + 1) & 0xFF;
|
|
15
|
+
}, 1000);
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { DRV2605Full } = require('periph/src/chips/motor/drv2605');
|
|
5
|
+
|
|
6
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
7
|
+
|
|
8
|
+
(async () => {
|
|
9
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
10
|
+
const connection = new I2CConnection(bus, DRV2605Full.I2C_ADDRESS);
|
|
11
|
+
const haptic = new DRV2605Full(connection, false); // Create DRV2605 Full driver, (connection, lra=false)
|
|
12
|
+
await haptic.init(); // Reset, verify and configure the chip, () → None
|
|
13
|
+
// DEV_RESET, DEVICE_ID check, leave standby, select ERM/LRA
|
|
14
|
+
|
|
15
|
+
console.log('device id', await haptic.readDeviceId()); // Read DEVICE_ID, () → number
|
|
16
|
+
// bits 7:5 of STATUS; 3 for the DRV2605
|
|
17
|
+
await haptic.playEffect(DRV2605Full.EFFECT_STRONG_CLICK); // Play a ROM effect, (effectId 1–123) → None
|
|
18
|
+
// writes slot 1 and the end marker, then sets GO
|
|
19
|
+
await haptic.waitDone(); // Wait for playback, (timeoutMs=5000) → boolean
|
|
20
|
+
// polls GO until the sequence finishes
|
|
21
|
+
await haptic.playSequence([DRV2605Full.EFFECT_STRONG_CLICK, ['wait', 100], DRV2605Full.EFFECT_BUZZ]); // Play a sequence, (items ≤ 8) → None
|
|
22
|
+
// number = effect, ['wait', ms] = delay in 10 ms steps
|
|
23
|
+
await haptic.waitDone(); // Wait for playback, (timeoutMs=5000) → boolean
|
|
24
|
+
console.log('playing', await haptic.isPlaying()); // Check playback, () → boolean
|
|
25
|
+
// reads the GO bit
|
|
26
|
+
await haptic.stop(); // Cancel playback, () → None
|
|
27
|
+
// clears GO; also leaves RTP mode
|
|
28
|
+
|
|
29
|
+
await haptic.setLibrary(1); // Select ROM library, (library 0–6) → None
|
|
30
|
+
// 0 empty, 1–5 ERM libraries A–E, 6 LRA library
|
|
31
|
+
await haptic.setActuator(false); // Select actuator type, (lra) → None
|
|
32
|
+
// sets N_ERM_LRA; recalibrate after changing it
|
|
33
|
+
await haptic.setOpenLoop(true); // Select open-loop drive, (enabled) → None
|
|
34
|
+
// ERM_OPEN_LOOP for an ERM, LRA_OPEN_LOOP for an LRA
|
|
35
|
+
await haptic.setBidirectional(true); // Select bidirectional input, (enabled) → None
|
|
36
|
+
// unidirectional is only valid closed-loop
|
|
37
|
+
await haptic.setRatedVoltage({ volts: 3.0 }); // Set rated voltage, ({volts V | code 0–255, lraFreqHz Hz}) → None
|
|
38
|
+
// code = V / 21.33 mV for an ERM
|
|
39
|
+
await haptic.setOverdriveClamp({ volts: 3.0 }); // Set overdrive clamp, ({volts V | code 0–255, lraFreqHz Hz}) → None
|
|
40
|
+
// code = V / 21.96 mV in open loop
|
|
41
|
+
await haptic.setDriveTime(2.0); // Set drive time, (ms) → None
|
|
42
|
+
// ERM back-EMF sample period (1 ms + 0.2 ms × code)
|
|
43
|
+
await haptic.setFeedback(3, 1, 2); // Set feedback control, (brakeFactor 0–7, loopGain 0–3, bemfGain 0–3) → None
|
|
44
|
+
// keeps the actuator-type bit
|
|
45
|
+
await haptic.setAdvanced(); // Set CONTROL1–3 fields, ({startupBoost, brakeStabilizer, sampleTime, blankingTime, idissTime, lraDriveTwice, supplyComp, noiseGate}) → None
|
|
46
|
+
// defaults match the chip's reset values
|
|
47
|
+
await haptic.setTimeOffsets({ overdriveMs: 0, sustainPosMs: 0, sustainNegMs: 0, brakeMs: 0 }); // Set library time offsets, ({overdriveMs, sustainPosMs, sustainNegMs, brakeMs}) → None
|
|
48
|
+
// ODT/SPT/SNT/BRT for open-loop ROM playback
|
|
49
|
+
|
|
50
|
+
await haptic.rtpLevel(0.5); // Stream an RTP amplitude, (level 0.0–1.0) → None
|
|
51
|
+
// enters RTP mode with signed data; 0.5 → 64
|
|
52
|
+
await sleep(500);
|
|
53
|
+
await haptic.setRtpFormat(false); // Select RTP data format, (unsigned) → None
|
|
54
|
+
// false = signed (default)
|
|
55
|
+
await haptic.rtpWrite(0x3F); // Write a raw RTP byte, (value 0–255) → None
|
|
56
|
+
// 0x3F = half scale in signed bidirectional mode
|
|
57
|
+
await sleep(500);
|
|
58
|
+
await haptic.stop(); // Cancel playback, () → None
|
|
59
|
+
// zeroes RTP_INPUT and returns to internal trigger
|
|
60
|
+
|
|
61
|
+
await haptic.setMode(DRV2605Full.MODE_INTERNAL_TRIGGER); // Select operating mode, (mode 0–7) → None
|
|
62
|
+
// 0 internal trigger … 7 auto-calibration
|
|
63
|
+
await haptic.setInput(false); // Select PWM/analog input, (analog) → None
|
|
64
|
+
// N_PWM_ANALOG; used with mode 3
|
|
65
|
+
await haptic.setHiZ(false); // Set high-impedance outputs, (enabled) → None
|
|
66
|
+
// true floats OUT+/OUT−
|
|
67
|
+
const cal = await haptic.calibrate(false, 3.0, 3.0); // Run auto-calibration, (lra, ratedVoltage V, overdriveVoltage V, lraFreqHz Hz) → {ok, comp, bemf, bemfGain}
|
|
68
|
+
// takes up to 1.2 s
|
|
69
|
+
console.log('calibrated', cal);
|
|
70
|
+
const stored = await haptic.getCalibration(); // Read calibration results, () → {comp, bemf, bemfGain}
|
|
71
|
+
// A_CAL_COMP, A_CAL_BEMF, BEMF_GAIN
|
|
72
|
+
await haptic.setCalibration(stored.comp, stored.bemf, stored.bemfGain); // Restore calibration, (comp, bemf, bemfGain) → None
|
|
73
|
+
// avoids recalibrating after a reset
|
|
74
|
+
console.log('actuator ok', await haptic.diagnose()); // Run actuator diagnostics, () → boolean
|
|
75
|
+
// true if DIAG_RESULT = 0
|
|
76
|
+
await haptic.setAudioToVibe({ acCouple: true, peakTimeMs: 20, filterHz: 125, minInputV: 0.1, maxInputV: 1.8, minDrive: 0.1, maxDrive: 1.0 }); // Configure audio-to-vibe, ({acCouple, peakTimeMs, filterHz, minInputV, maxInputV, minDrive, maxDrive}) → None
|
|
77
|
+
// enters audio-to-vibe mode
|
|
78
|
+
await haptic.setMode(DRV2605Full.MODE_INTERNAL_TRIGGER); // Select operating mode, (mode 0–7) → None
|
|
79
|
+
// back to internal trigger
|
|
80
|
+
|
|
81
|
+
await haptic.playEffect(DRV2605Full.EFFECT_BUZZ); // Play a ROM effect, (effectId 1–123) → None
|
|
82
|
+
// a buzz, so the supply and LRA readings are valid
|
|
83
|
+
await sleep(200);
|
|
84
|
+
console.log('vdd', await haptic.readSupplyVoltage()); // Read supply voltage, () → number V
|
|
85
|
+
// VBAT × 5.6 / 255; valid during playback
|
|
86
|
+
console.log('lra', await haptic.readLraFrequency()); // Read LRA frequency, () → number Hz | null
|
|
87
|
+
// null when LRA_PERIOD is 0
|
|
88
|
+
await haptic.stop(); // Cancel playback, () → None
|
|
89
|
+
console.log(await haptic.readStatus()); // Read status flags, () → {diagFailed, fbTimeout, overTemp, overCurrent}
|
|
90
|
+
// clears the latched flags
|
|
91
|
+
console.log('otp', await haptic.otpProgrammed()); // Read OTP status, () → boolean
|
|
92
|
+
// true once the OTP has been burned
|
|
93
|
+
// await haptic.programOtp(true); // IRREVERSIBLE one-time OTP burn, (confirm) → None
|
|
94
|
+
await haptic.standby(); // Enter software standby, () → None
|
|
95
|
+
// also stops playback
|
|
96
|
+
await haptic.wake(); // Leave software standby, () → None
|
|
97
|
+
await haptic.reset(); // Reset all registers, () → None
|
|
98
|
+
// re-run init() afterwards
|
|
99
|
+
await connection.close();
|
|
100
|
+
})();
|