periph 1.3.3 → 1.5.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.
Files changed (150) hide show
  1. package/README.md +36 -0
  2. package/examples/adc_dac/ad5251/complete.js +66 -0
  3. package/examples/adc_dac/ad5251/demo.js +69 -0
  4. package/examples/adc_dac/ad5251/minimal.js +23 -0
  5. package/examples/adc_dac/ad5252/complete.js +66 -0
  6. package/examples/adc_dac/ad5252/demo.js +69 -0
  7. package/examples/adc_dac/ad5252/minimal.js +23 -0
  8. package/examples/adc_dac/ads7828/complete.js +46 -0
  9. package/examples/adc_dac/ads7828/demo.js +45 -0
  10. package/examples/adc_dac/ads7828/minimal.js +17 -0
  11. package/examples/adc_dac/cat5171/complete.js +52 -0
  12. package/examples/adc_dac/cat5171/demo.js +59 -0
  13. package/examples/adc_dac/cat5171/minimal.js +23 -0
  14. package/examples/adc_dac/cs43l22/complete.js +88 -0
  15. package/examples/adc_dac/cs43l22/demo.js +56 -0
  16. package/examples/adc_dac/cs43l22/minimal.js +20 -0
  17. package/examples/adc_dac/ds1881/complete.js +62 -0
  18. package/examples/adc_dac/ds1881/demo.js +61 -0
  19. package/examples/adc_dac/ds1881/minimal.js +25 -0
  20. package/examples/adc_dac/ds3502/complete.js +46 -0
  21. package/examples/adc_dac/ds3502/demo.js +54 -0
  22. package/examples/adc_dac/ds3502/minimal.js +28 -0
  23. package/examples/adc_dac/es8311/complete.js +80 -0
  24. package/examples/adc_dac/es8311/demo.js +59 -0
  25. package/examples/adc_dac/es8311/minimal.js +18 -0
  26. package/examples/adc_dac/mcp3422/complete.js +48 -0
  27. package/examples/adc_dac/mcp3422/demo.js +49 -0
  28. package/examples/adc_dac/mcp3422/minimal.js +25 -0
  29. package/examples/adc_dac/mcp4xxx/complete.js +97 -0
  30. package/examples/adc_dac/mcp4xxx/demo.js +76 -0
  31. package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
  32. package/examples/display/sh1106/complete.js +101 -0
  33. package/examples/display/sh1106/demo.js +68 -0
  34. package/examples/display/sh1106/minimal.js +25 -0
  35. package/examples/display/ssd1305/complete.js +129 -0
  36. package/examples/display/ssd1305/demo.js +71 -0
  37. package/examples/display/ssd1305/minimal.js +25 -0
  38. package/examples/display/ssd1306/complete.js +115 -0
  39. package/examples/display/ssd1306/demo.js +70 -0
  40. package/examples/display/ssd1306/minimal.js +25 -0
  41. package/examples/environmental/am2315/complete.js +34 -0
  42. package/examples/environmental/am2315/demo.js +46 -0
  43. package/examples/environmental/am2315/minimal.js +21 -0
  44. package/examples/humidity/htu21df/complete.js +49 -0
  45. package/examples/humidity/htu21df/demo.js +50 -0
  46. package/examples/humidity/htu21df/minimal.js +22 -0
  47. package/examples/humidity/sht31/complete.js +53 -0
  48. package/examples/humidity/sht31/demo.js +53 -0
  49. package/examples/humidity/sht31/minimal.js +21 -0
  50. package/examples/humidity/si7021/complete.js +55 -0
  51. package/examples/humidity/si7021/demo.js +58 -0
  52. package/examples/humidity/si7021/minimal.js +21 -0
  53. package/examples/imu/bno055/complete.js +85 -0
  54. package/examples/imu/bno055/demo.js +70 -0
  55. package/examples/imu/bno055/minimal.js +20 -0
  56. package/examples/imu/qmi8658a/complete.js +134 -0
  57. package/examples/imu/qmi8658a/demo.js +81 -0
  58. package/examples/imu/qmi8658a/minimal.js +22 -0
  59. package/examples/io_expander/pca6408a/complete.js +63 -0
  60. package/examples/io_expander/pca6408a/demo.js +58 -0
  61. package/examples/io_expander/pca6408a/minimal.js +20 -0
  62. package/examples/io_expander/pca6416a/complete.js +68 -0
  63. package/examples/io_expander/pca6416a/demo.js +60 -0
  64. package/examples/io_expander/pca6416a/minimal.js +20 -0
  65. package/examples/io_expander/pca9536/complete.js +63 -0
  66. package/examples/io_expander/pca9536/demo.js +57 -0
  67. package/examples/io_expander/pca9536/minimal.js +20 -0
  68. package/examples/io_expander/pca9539/complete.js +68 -0
  69. package/examples/io_expander/pca9539/demo.js +60 -0
  70. package/examples/io_expander/pca9539/minimal.js +20 -0
  71. package/examples/light/max44009/complete.js +56 -0
  72. package/examples/light/max44009/demo.js +49 -0
  73. package/examples/light/max44009/minimal.js +22 -0
  74. package/examples/memory/at24c02n/complete.js +39 -0
  75. package/examples/memory/at24c02n/demo.js +39 -0
  76. package/examples/memory/at24c02n/minimal.js +15 -0
  77. package/examples/memory/at24c64/complete.js +39 -0
  78. package/examples/memory/at24c64/demo.js +39 -0
  79. package/examples/memory/at24c64/minimal.js +15 -0
  80. package/examples/memory/cat24c512/complete.js +39 -0
  81. package/examples/memory/cat24c512/demo.js +39 -0
  82. package/examples/memory/cat24c512/minimal.js +15 -0
  83. package/examples/motor/tmc2130/complete.js +138 -0
  84. package/examples/motor/tmc2130/demo.js +96 -0
  85. package/examples/motor/tmc2130/minimal.js +30 -0
  86. package/examples/motor/tmc2208/complete.js +92 -0
  87. package/examples/motor/tmc2208/demo.js +85 -0
  88. package/examples/motor/tmc2208/minimal.js +25 -0
  89. package/examples/motor/tmc2209/complete.js +106 -0
  90. package/examples/motor/tmc2209/demo.js +97 -0
  91. package/examples/motor/tmc2209/minimal.js +25 -0
  92. package/examples/motor/tmc5160/complete.js +205 -0
  93. package/examples/motor/tmc5160/demo.js +104 -0
  94. package/examples/motor/tmc5160/minimal.js +35 -0
  95. package/examples/other/pca9541/complete.js +52 -0
  96. package/examples/other/pca9541/demo.js +61 -0
  97. package/examples/other/pca9541/minimal.js +23 -0
  98. package/examples/power/axp2101/complete.js +135 -0
  99. package/examples/power/axp2101/demo.js +68 -0
  100. package/examples/power/axp2101/minimal.js +24 -0
  101. package/examples/rtc/pcf85063a/complete.js +68 -0
  102. package/examples/rtc/pcf85063a/demo.js +76 -0
  103. package/examples/rtc/pcf85063a/minimal.js +20 -0
  104. package/examples/temperature/tmp102/complete.js +74 -0
  105. package/examples/temperature/tmp102/demo.js +71 -0
  106. package/examples/temperature/tmp102/minimal.js +20 -0
  107. package/examples/tof/vl6180x/complete.js +62 -0
  108. package/examples/tof/vl6180x/demo.js +77 -0
  109. package/examples/tof/vl6180x/minimal.js +23 -0
  110. package/package.json +1 -1
  111. package/src/chips/adc_dac/ad5251.js +345 -0
  112. package/src/chips/adc_dac/ad5252.js +345 -0
  113. package/src/chips/adc_dac/ads7828.js +225 -0
  114. package/src/chips/adc_dac/cat5171.js +213 -0
  115. package/src/chips/adc_dac/cs43l22.js +645 -0
  116. package/src/chips/adc_dac/ds1881.js +357 -0
  117. package/src/chips/adc_dac/ds3502.js +241 -0
  118. package/src/chips/adc_dac/es8311.js +621 -0
  119. package/src/chips/adc_dac/mcp3422.js +312 -0
  120. package/src/chips/adc_dac/mcp4xxx.js +982 -0
  121. package/src/chips/display/mono_oled.js +182 -0
  122. package/src/chips/display/sh1106.js +496 -0
  123. package/src/chips/display/ssd1305.js +655 -0
  124. package/src/chips/display/ssd1306.js +458 -0
  125. package/src/chips/environmental/am2315.js +237 -0
  126. package/src/chips/humidity/htu21df.js +222 -0
  127. package/src/chips/humidity/sht31.js +284 -0
  128. package/src/chips/humidity/si7021.js +282 -0
  129. package/src/chips/imu/bno055.js +496 -0
  130. package/src/chips/imu/qmi8658a.js +1011 -0
  131. package/src/chips/io_expander/pca6408a.js +407 -0
  132. package/src/chips/io_expander/pca6416a.js +455 -0
  133. package/src/chips/io_expander/pca9536.js +398 -0
  134. package/src/chips/io_expander/pca9539.js +457 -0
  135. package/src/chips/light/max44009.js +336 -0
  136. package/src/chips/memory/at24c02n.js +220 -0
  137. package/src/chips/memory/at24c64.js +220 -0
  138. package/src/chips/memory/cat24c512.js +221 -0
  139. package/src/chips/motor/tmc2130.js +537 -0
  140. package/src/chips/motor/tmc2208.js +490 -0
  141. package/src/chips/motor/tmc2209.js +565 -0
  142. package/src/chips/motor/tmc5160.js +900 -0
  143. package/src/chips/other/pca9541.js +315 -0
  144. package/src/chips/power/axp2101.js +968 -0
  145. package/src/chips/rtc/pcf85063a.js +482 -0
  146. package/src/chips/temperature/tmp102.js +361 -0
  147. package/src/chips/tof/vl6180x.js +580 -0
  148. package/src/connection/uart.js +9 -0
  149. package/src/connection/uart_mock.js +65 -0
  150. package/src/discovery/registry.js +85 -8
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { BNO055Minimal } = require('../../../src/chips/imu/bno055');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x29', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const imu = new BNO055Minimal(connection); // Create BNO055 driver, (connection) → void
11
+
12
+ async function main() {
13
+ while (true) {
14
+ const [h, r, p] = await imu.euler(); // Read fused Euler angles, () → [number, number, number] rad
15
+ console.log('heading %s rad, roll %s, pitch %s', h.toFixed(2), r.toFixed(2), p.toFixed(2));
16
+ await new Promise(res => setTimeout(res, 100));
17
+ }
18
+ }
19
+
20
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -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,63 @@
1
+ 'use strict';
2
+ const { I2CConnection } = require('periph/src/connection/i2c');
3
+ const { Pca6408aFull } = require('periph/src/chips/io_expander/pca6408a');
4
+
5
+ async function main() {
6
+ const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
7
+ const chip = new Pca6408aFull(connection); // Create PCA6408A full driver, (connection, addr=0x20)
8
+ // writes the reset values; 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
+ // P0 as output; P7 is a normal bidirectional pin
14
+ console.log('direction:', p0.direction); // direction property, () → string 'in'|'out'
15
+ await p0.write(0); // Write pin, (value=0|1) → Promise<void>
16
+ // clears the OUTPUT bit via the shadow
17
+ const v = await p0.read(); // Read pin, () → Promise<int 0|1>
18
+ // reads INPUT (actual pin level)
19
+ console.log('P0 level:', v);
20
+ await p0.setDirection('in'); // Set direction, (direction) → Promise<void>
21
+ // writes CONFIG; OUTPUT 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 OUTPUT write
26
+ const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
27
+ // INPUT register
28
+ console.log('PORT=0x' + port.toString(16).padStart(2, '0'));
29
+
30
+ // --- Configuration ---
31
+ await chip.configurePolarity(0x00); // Configure input polarity, (mask) → Promise<void>
32
+ // 1 bits invert the INPUT read
33
+ await chip.setDirection(0xF0); // Set whole-port direction, (mask) → Promise<void>
34
+ // P0–P3 outputs, P4–P7 inputs; OUTPUT is written first
35
+ const dir = chip.getDirection(); // Read direction shadow, () → int
36
+ // 1 = input, 0 = output
37
+ console.log('CONFIG=0x' + dir.toString(16).padStart(2, '0'));
38
+ const config = await chip.readRegister(0x03); // Read raw register, (reg) → Promise<int>
39
+ // escape hatch for debugging
40
+ console.log('CONFIG=0x' + config.toString(16).padStart(2, '0'));
41
+ await chip.writeRegister(0x02, 0x10); // Write raw register, (reg, value) → Promise<void>
42
+ // POLARITY: invert P4
43
+
44
+ // --- Interrupts ---
45
+ await chip.onInterrupt((changed) => { // Subscribe to INT, (callback, options) → Promise<void>
46
+ console.log('interrupt, changed=0x' + changed.toString(16));// INT line if wired, else 5 ms polling
47
+ });
48
+ const p4 = chip.pin(4, 'in');
49
+ await p4.watch((pin) => console.log('P4 changed'), 'change'); // Watch a pin, (handler, trigger='change') → Promise<void>
50
+ // edge filtered in software; all pins share INT
51
+ const changed = await chip.pollInterrupt(); // Read INPUT, get changed mask, () → Promise<int>
52
+ // XOR with the last INPUT value
53
+ console.log('changed=0x' + changed.toString(16));
54
+ await p4.unwatch(); // Stop watching, () → Promise<void>
55
+ // drops the handler
56
+ await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
57
+ // stops delivery
58
+ await chip.reset(); // Restore reset values, () → Promise<void>
59
+ // OUTPUT=0xFF, POLARITY=0x00, CONFIG=0xFF
60
+ await connection.close();
61
+ }
62
+
63
+ main();
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+ /**
3
+ * PCA6408A demo — LED scanner with interrupt-driven button input.
4
+ *
5
+ * Hardware:
6
+ * P0–P3: four active-low LEDs (anode → VDD(P) via 220 Ω, cathode → P; 0 lights the LED)
7
+ * P4–P7: four push buttons (pin → GND when pressed) with external pull-ups
8
+ * (the chip has no internal pull-ups)
9
+ *
10
+ * Runs a scanner pattern on the LEDs. Button presses raise an interrupt; the
11
+ * callback prints the changed-pin mask and the port state and holds the LED
12
+ * matching the pressed button (P4→P0, …) for one second.
13
+ */
14
+ const { I2CConnection } = require('periph/src/connection/i2c');
15
+ const { Pca6408aFull } = require('periph/src/chips/io_expander/pca6408a');
16
+
17
+ async function main() {
18
+ // --- Initialise bus and driver ---
19
+ const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
20
+ const chip = new Pca6408aFull(connection); // Create PCA6408A full driver, (connection, addr=0x20)
21
+ await chip.ready;
22
+
23
+ // --- Configure LEDs and buttons ---
24
+ // P0–P3 are outputs (all LEDs off = 1); P4–P7 stay inputs, a pressed button reads 0.
25
+ await chip.writePort(0, 0xFF); // Turn all LEDs off, (port=0, mask) → Promise<void>
26
+ await chip.setDirection(0xF0); // P0–P3 outputs, P4–P7 inputs, (mask) → Promise<void>
27
+
28
+ // --- Subscribe to input changes ---
29
+ // Any button edge raises INT (or the 5 ms poll sees it); the callback reports the changed
30
+ // mask, then holds the matching LED.
31
+ let holdUntil = 0;
32
+ let holdMask = 0x0F;
33
+ await chip.onInterrupt(async (changed) => { // Subscribe to INT, (callback, options) → Promise<void>
34
+ const port = await chip.readPort(); // Read all 8 pins, (port=0) → Promise<int bitmask>
35
+ console.log('button changed=0x' + changed.toString(16) + ' port=0x' + port.toString(16));
36
+ holdMask = ~((changed >> 4) & 0x0F) & 0x0F;
37
+ holdUntil = Date.now() + 1000;
38
+ });
39
+
40
+ // --- Scanner loop ---
41
+ let step = 0;
42
+ console.log('Running — press buttons P4–P7 to hold the matching LED');
43
+ const timer = setInterval(async () => {
44
+ const leds = Date.now() < holdUntil ? holdMask : (~(1 << (step % 4)) & 0x0F);
45
+ await chip.writePort(0, 0xF0 | 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, 0xFF); // Turn all LEDs off, (port=0, mask) → 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 { Pca6408aMinimal } = require('periph/src/chips/io_expander/pca6408a');
4
+
5
+ async function main() {
6
+ const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
7
+ const chip = new Pca6408aMinimal(connection); // Create PCA6408A 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('PORT=0x' + port.toString(16).padStart(2, '0') + ' P0=' + level);
17
+ }, 200);
18
+ }
19
+
20
+ main();
@@ -0,0 +1,68 @@
1
+ 'use strict';
2
+ const { I2CConnection } = require('periph/src/connection/i2c');
3
+ const { Pca6416aFull } = require('periph/src/chips/io_expander/pca6416a');
4
+
5
+ async function main() {
6
+ const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
7
+ const chip = new Pca6416aFull(connection); // Create PCA6416A full driver, (connection, addr=0x20)
8
+ // writes the reset values; all 16 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
+ // P0_0 as output; every pin is bidirectional
14
+ console.log('direction:', p0.direction); // direction property, () → string 'in'|'out'
15
+ await p0.write(0); // Write pin, (value=0|1) → Promise<void>
16
+ // clears the OUTPUT bit via the shadow
17
+ const v = await p0.read(); // Read pin, () → Promise<int 0|1>
18
+ // reads INPUT (actual pin level)
19
+ console.log('P0 level:', v);
20
+ await p0.setDirection('in'); // Set direction, (direction) → Promise<void>
21
+ // writes CONFIG; OUTPUT is latched first for outputs
22
+
23
+ // --- Port-level operations ---
24
+ await chip.writePort(0, 0b00001111); // Write port 0, (port, mask) → Promise<void>
25
+ // single-port OUTPUT write
26
+ await chip.writeAll(0xA50F); // Write all 16 pins, (value) → Promise<void>
27
+ // one two-byte write at OUTPUT0 (pair toggling)
28
+ const port = await chip.readPort(1); // Read port 1, (port) → Promise<int bitmask>
29
+ // INPUT1 register
30
+ console.log('PORT1=0x' + port.toString(16).padStart(2, '0'));
31
+ const all = await chip.readAll(); // Read all 16 pins, () → Promise<int>
32
+ // one two-byte read at INPUT0
33
+ console.log('ALL=0x' + all.toString(16).padStart(4, '0'));
34
+
35
+ // --- Configuration ---
36
+ await chip.configurePolarity(0x0000); // Configure input polarity, (mask) → Promise<void>
37
+ // 1 bits invert the INPUT read
38
+ await chip.setDirection(0xFF00); // Set whole-device direction, (mask) → Promise<void>
39
+ // port 0 outputs, port 1 inputs; OUTPUT is written first
40
+ const dir = chip.getDirection(); // Read direction shadow, () → int
41
+ // 1 = input, 0 = output
42
+ console.log('CONFIG=0x' + dir.toString(16).padStart(4, '0'));
43
+ const config = await chip.readRegister(0x07); // Read raw register, (reg) → Promise<int>
44
+ // escape hatch for debugging
45
+ console.log('CONFIG1=0x' + config.toString(16).padStart(2, '0'));
46
+ await chip.writeRegister(0x05, 0x10); // Write raw register, (reg, value) → Promise<void>
47
+ // POLARITY1: invert P1_4
48
+
49
+ // --- Interrupts ---
50
+ await chip.onInterrupt((changed) => { // Subscribe to INT, (callback, options) → Promise<void>
51
+ console.log('interrupt, changed=0x' + changed.toString(16));// INT line if wired, else 5 ms polling
52
+ });
53
+ const p12 = chip.pin(12, 'in');
54
+ await p12.watch((pin) => console.log('P1_4 changed'), 'change');// Watch a pin, (handler, trigger='change') → Promise<void>
55
+ // edge filtered in software; all pins share INT
56
+ const changed = await chip.pollInterrupt(); // Read INPUT0/1, get changed mask, () → Promise<int>
57
+ // XOR with the last INPUT values
58
+ console.log('changed=0x' + changed.toString(16));
59
+ await p12.unwatch(); // Stop watching, () → Promise<void>
60
+ // drops the handler
61
+ await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
62
+ // stops delivery
63
+ await chip.reset(); // Restore reset values, () → Promise<void>
64
+ // OUTPUT=0xFF, POLARITY=0x00, CONFIG=0xFF (both ports)
65
+ await connection.close();
66
+ }
67
+
68
+ main();
@@ -0,0 +1,60 @@
1
+ 'use strict';
2
+ /**
3
+ * PCA6416A demo — LED scanner with interrupt-driven button input.
4
+ *
5
+ * Hardware:
6
+ * P0_0–P0_7: eight active-low LEDs (anode → VDD(P) via 220 Ω, cathode → P; 0 lights the LED)
7
+ * P1_0–P1_7: eight push buttons (pin → GND when pressed) with external pull-ups
8
+ * (the chip has no internal pull-ups)
9
+ *
10
+ * Runs a scanner pattern on the LEDs. Button presses raise an interrupt; the
11
+ * callback prints the 16-bit changed-pin mask and the pin state and holds the
12
+ * LED matching the pressed button (P1_0→P0_0, …) for one second.
13
+ */
14
+ const { I2CConnection } = require('periph/src/connection/i2c');
15
+ const { Pca6416aFull } = require('periph/src/chips/io_expander/pca6416a');
16
+
17
+ async function main() {
18
+ // --- Initialise bus and driver ---
19
+ const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
20
+ const chip = new Pca6416aFull(connection); // Create PCA6416A full driver, (connection, addr=0x20)
21
+ await chip.ready;
22
+
23
+ // --- Configure LEDs and buttons ---
24
+ // Port 0 holds the LED outputs (all LEDs off = 1); port 1 stays input, a pressed button
25
+ // reads 0 — polarity inversion makes it read 1.
26
+ await chip.writeAll(0xFFFF); // Turn all LEDs off, (value) → Promise<void>
27
+ await chip.setDirection(0xFF00); // Port 0 outputs, port 1 inputs, (mask) → Promise<void>
28
+ await chip.configurePolarity(0xFF00); // Invert port 1 inputs, (mask) → Promise<void>
29
+
30
+ // --- Subscribe to input changes ---
31
+ // Any button edge raises INT (or the 5 ms poll sees it); the callback reports the changed
32
+ // mask, then holds the matching LED.
33
+ let holdUntil = 0;
34
+ let holdMask = 0xFF;
35
+ await chip.onInterrupt(async (changed) => { // Subscribe to INT, (callback, options) → Promise<void>
36
+ const all = await chip.readAll(); // Read all 16 pins, () → Promise<int>
37
+ console.log('button changed=0x' + changed.toString(16) + ' all=0x' + all.toString(16));
38
+ holdMask = ~((changed >> 8) & 0xFF) & 0xFF;
39
+ holdUntil = Date.now() + 1000;
40
+ });
41
+
42
+ // --- Scanner loop ---
43
+ let step = 0;
44
+ console.log('Running — press buttons P1_0–P1_7 to hold the matching LED');
45
+ const timer = setInterval(async () => {
46
+ const leds = Date.now() < holdUntil ? holdMask : (~(1 << (step % 8)) & 0xFF);
47
+ await chip.writeAll(0xFF00 | leds); // Write all 16 pins, (value) → Promise<void>
48
+ const all = await chip.readAll(); // Read all 16 pins, () → Promise<int>
49
+ console.log('pos=' + (step % 8) + ' all=0x' + all.toString(16).padStart(4, '0'));
50
+ step++;
51
+ if (step >= 80) {
52
+ clearInterval(timer);
53
+ await chip.offInterrupt(); // Unsubscribe, () → Promise<void>
54
+ await chip.writeAll(0xFFFF); // Turn all LEDs off, (value) → Promise<void>
55
+ await connection.close();
56
+ }
57
+ }, 100);
58
+ }
59
+
60
+ main();
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+ const { I2CConnection } = require('periph/src/connection/i2c');
3
+ const { Pca6416aMinimal } = require('periph/src/chips/io_expander/pca6416a');
4
+
5
+ async function main() {
6
+ const connection = new I2CConnection(1, 0x20); // Create I2C connection, (bus, addr=0x20)
7
+ const chip = new Pca6416aMinimal(connection); // Create PCA6416A driver, (connection, addr=0x20)
8
+
9
+ const p0 = chip.pin(0, 'in'); // Get pin proxy P0_0, (n, direction='in') → Pin
10
+ const p15 = chip.pin(15, 'out'); // Get pin proxy P1_7, (n, direction='out') → Pin
11
+
12
+ setInterval(async () => {
13
+ const port = await chip.readPort(1); // Read the 8 pins of port 1, (port) → Promise<int bitmask>
14
+ const level = await p0.read(); // Read pin, () → Promise<int 0|1>
15
+ await p15.write(level); // Write pin, (value=0|1) → Promise<void>
16
+ console.log('PORT1=0x' + port.toString(16).padStart(2, '0') + ' P0=' + level);
17
+ }, 200);
18
+ }
19
+
20
+ main();