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,46 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { AM2315Full } = require('../../../src/chips/environmental/am2315');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x5C', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x5C) → Connection
11
+ const sensor = new AM2315Full(connection); // Create AM2315 driver, (connection) → void
12
+
13
+ // Magnus formula
14
+ function dewPoint(t, rh) {
15
+ const g = Math.log(Math.max(rh, 0.1) / 100.0) + 17.62 * t / (243.12 + t);
16
+ return 243.12 * g / (17.62 - g);
17
+ }
18
+
19
+ async function main() {
20
+ const info = await sensor.readDeviceInfo(); // Read device information, () → {model, version, device_id}
21
+ console.log('model %d, version %d, device id 0x%s', info.model, info.version, info.device_id.toString(16).toUpperCase());
22
+
23
+ // --- Weather-station logger: a sample every 5 s, min/max tracking ---
24
+ let tMin = 1000, tMax = -1000, hMin = 1000, hMax = -1000;
25
+ for (let i = 1; i <= 30; i++) {
26
+ // The sensor measures after a communication and returns that result on the next
27
+ // one, so every sample is the previous measurement; the driver enforces the >= 2 s
28
+ // spacing and the CRC-16 check.
29
+ const r = (i % 10 === 0)
30
+ ? await sensor.readFresh() // Real-time value, () → {temperature_c, humidity_pct}
31
+ : await sensor.read(); // Read temperature and humidity, () → {temperature_c, humidity_pct}
32
+ const t = r.temperature_c, rh = r.humidity_pct;
33
+ tMin = Math.min(tMin, t); tMax = Math.max(tMax, t);
34
+ hMin = Math.min(hMin, rh); hMax = Math.max(hMax, rh);
35
+ console.log('%s %s C %s %%RH dew point %s C (T %s..%s, RH %s..%s)', new Date().toISOString(),
36
+ t.toFixed(1), rh.toFixed(1), dewPoint(t, rh).toFixed(1), tMin.toFixed(1), tMax.toFixed(1), hMin.toFixed(1), hMax.toFixed(1));
37
+ await sleep(5000);
38
+ }
39
+
40
+ // --- User registers as a persistent counter ---
41
+ await sensor.writeUserRegisters(30, 0xBEEF); // Write user registers, (user1, user2) → void
42
+ console.log('user registers:', await sensor.readUserRegisters()); // Read user registers, () → [number, number]
43
+ connection.close();
44
+ }
45
+
46
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { AM2315Minimal } = require('../../../src/chips/environmental/am2315');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x5C', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x5C) → Connection
11
+ const sensor = new AM2315Minimal(connection); // Create AM2315 driver, (connection) → void
12
+
13
+ async function main() {
14
+ while (true) {
15
+ const r = await sensor.read(); // Read temperature and humidity, () → {temperature_c, humidity_pct}
16
+ console.log('%s C, %s %%RH', r.temperature_c.toFixed(1), r.humidity_pct.toFixed(1));
17
+ await sleep(2000);
18
+ }
19
+ }
20
+
21
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HTU21DFFull } = require('../../../src/chips/humidity/htu21df');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
11
+ const sensor = new HTU21DFFull(connection); // Create HTU21D-F driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset, () → void
15
+ // 15 ms power-up wait, soft reset, 15 ms
16
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number °C, number %RH]
17
+ // no hold master, CRC checked, one retry on a mismatch
18
+ console.log(t, rh);
19
+ console.log(await sensor.readHumidity()); // Read humidity only, () → number %RH
20
+ // not clamped, ASIC range -6 ... 118 %RH
21
+ console.log(await sensor.readTemperature()); // Read temperature only, () → number °C
22
+ console.log(await sensor.readHumidityCompensated()); // Temperature-compensated humidity, () → number %RH
23
+ // -0.15 %RH/°C relative to 25 °C
24
+ console.log(await sensor.readRaw()); // Read raw codes, () → [number, number]
25
+ // [temperature, humidity], status bits cleared
26
+
27
+ await sensor.setResolution(3); // Set resolution, (res) → void
28
+ // 0 = 12/14 bit, 1 = 8/12, 2 = 10/13, 3 = 11/11
29
+ console.log(await sensor.getResolution()); // Get resolution, () → number
30
+ await sensor.setResolution(0);
31
+
32
+ await sensor.setHeater(true); // Switch the heater, (on) → void
33
+ // about 5.5 mW; readings drift while on
34
+ console.log(await sensor.heater()); // Heater state, () → boolean
35
+ await sensor.setHeater(false);
36
+ console.log(await sensor.batteryLow()); // End-of-battery flag, () → boolean
37
+ // true when VDD is below about 2.25 V
38
+ console.log((await sensor.readUserRegister()).toString(16)); // Read the user register, () → number
39
+ // 0x02 after reset
40
+ sensor.setHoldMaster(false); // Use hold-master commands, (on) → void
41
+ // needs bus-layer clock stretching; off polls the NACKed read header
42
+ console.log(await sensor.dewPoint()); // Compute dew point, () → number °C
43
+ // one measurement
44
+ await sensor.softReset(); // Soft reset, () → void
45
+ // 0xFE, waits 15 ms; the heater bit survives
46
+ connection.close();
47
+ }
48
+
49
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HTU21DFFull } = require('../../../src/chips/humidity/htu21df');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
11
+ const sensor = new HTU21DFFull(connection); // Create HTU21D-F driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset, () → void
15
+ await sensor.softReset(); // Soft reset, () → void
16
+ console.log('user register 0x' + (await sensor.readUserRegister()).toString(16)); // Read the user register, () → number
17
+ console.log('resolution', await sensor.getResolution()); // Get resolution, () → number
18
+
19
+ // --- Five readings at 12/14 bit ---
20
+ for (let i = 0; i < 5; i++) {
21
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number, number]
22
+ console.log('%s C %s %%RH (compensated %s) dew point %s C', t.toFixed(2), rh.toFixed(1),
23
+ (await sensor.readHumidityCompensated()).toFixed(1), (await sensor.dewPoint()).toFixed(2)); // () → number %RH ; () → number °C
24
+ await sleep(2000);
25
+ }
26
+ console.log('raw', await sensor.readRaw()); // Read raw codes, () → [number, number]
27
+
28
+ // --- 8/12 bit: shorter conversion, coarser resolution ---
29
+ await sensor.setResolution(1); // Set resolution, (res) → void
30
+ const start = Date.now();
31
+ const t812 = await sensor.readTemperature(); // () → number °C
32
+ const rh812 = await sensor.readHumidity(); // () → number %RH
33
+ console.log('8/12 bit: %s C %s %%RH in %d ms', t812.toFixed(2), rh812.toFixed(1), Date.now() - start);
34
+ await sensor.setResolution(0);
35
+ sensor.setHoldMaster(false); // Use hold-master commands, (on) → void
36
+
37
+ // --- Heater self-check: 10 s ---
38
+ const rhBefore = await sensor.readHumidity();
39
+ const tBefore = await sensor.readTemperature();
40
+ await sensor.setHeater(true); // Switch the heater, (on) → void
41
+ await sleep(10000);
42
+ console.log('heater on:', await sensor.heater()); // Heater state, () → boolean
43
+ console.log('RH drop %s %%RH, temperature rise %s K',
44
+ (rhBefore - await sensor.readHumidity()).toFixed(1), ((await sensor.readTemperature()) - tBefore).toFixed(2));
45
+ await sensor.setHeater(false);
46
+ console.log('battery low:', await sensor.batteryLow()); // End-of-battery flag, () → boolean
47
+ connection.close();
48
+ }
49
+
50
+ 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 { HTU21DFMinimal } = require('../../../src/chips/humidity/htu21df');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
11
+ const sensor = new HTU21DFMinimal(connection); // Create HTU21D-F driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Soft reset, () → void
15
+ while (true) {
16
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number °C, number %RH]
17
+ console.log('%s C, %s %%RH', t.toFixed(2), rh.toFixed(1));
18
+ await sleep(2000);
19
+ }
20
+ }
21
+
22
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { SHT31Full } = require('../../../src/chips/humidity/sht31');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x44', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x44) → Connection
11
+ const sensor = new SHT31Full(connection); // Create SHT31 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Wait for the soft reset, () → void
15
+
16
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number, number]
17
+ // single shot, high repeatability, CRC checked
18
+ console.log(t, rh);
19
+ console.log(await sensor.readTemperature()); // Read temperature only, () → number °C
20
+ console.log(await sensor.readHumidity()); // Read humidity only, () → number %RH
21
+ console.log(await sensor.readRaw()); // Read raw words, () → [number, number]
22
+ // 16-bit temperature and humidity ticks
23
+
24
+ sensor.setRepeatability('low'); // Select repeatability, (level) → void
25
+ // 'low' / 'medium' / 'high' (4 / 6 / 15 ms)
26
+ console.log(await sensor.read());
27
+ sensor.setRepeatability('high');
28
+ sensor.setClockStretching(false); // Use clock-stretching commands, (on) → void
29
+ // needs bus-layer support; off polls the NACKed read header
30
+
31
+ await sensor.startPeriodic(2, 'high'); // Start periodic mode, (rate, repeatability='high', art=false) → void
32
+ // rate 0.5 / 1 / 2 / 4 / 10 mps; art=true selects the 4 mps ART mode
33
+ await sleep(1000);
34
+ console.log(await sensor.fetch()); // Fetch the latest periodic result, () → [number, number] | null
35
+ // null while the read header is NACKed
36
+ await sensor.stopPeriodic(); // Stop periodic mode, () → void
37
+ // break command 0x3093, waits 1 ms
38
+
39
+ await sensor.setHeater(true); // Switch the heater, (on) → void
40
+ // about 5-25 mW; readings are unusable while on
41
+ console.log((await sensor.readStatus()).toString(16)); // Read status register, () → number
42
+ // CRC verified; bit 13 = heater on
43
+ await sensor.setHeater(false);
44
+ await sensor.clearStatus(); // Clear status flags, () → void
45
+ // command 0x3041
46
+ console.log(await sensor.dewPoint()); // Compute dew point, () → number °C
47
+ // one measurement, Magnus formula
48
+ await sensor.softReset(); // Soft reset, () → void
49
+ // 0x30A2, waits 1 ms, back to single shot
50
+ connection.close();
51
+ }
52
+
53
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { SHT31Full } = require('../../../src/chips/humidity/sht31');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x44', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x44) → Connection
11
+ const sensor = new SHT31Full(connection); // Create SHT31 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.init(); // Wait for the soft reset, () → void
15
+ await sensor.softReset(); // Soft reset, () → void
16
+ console.log('status 0x' + (await sensor.readStatus()).toString(16)); // Read status register, () → number
17
+
18
+ // --- Single shots at all three repeatability levels ---
19
+ for (const level of ['low', 'medium', 'high']) {
20
+ sensor.setRepeatability(level); // Select repeatability, (level) → void
21
+ const start = process.hrtime.bigint();
22
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number, number]
23
+ const ms = Number(process.hrtime.bigint() - start) / 1e6;
24
+ console.log('%s %s C %s %%RH (%s ms)', level.padEnd(6), t.toFixed(2), rh.toFixed(1), ms.toFixed(1));
25
+ }
26
+ sensor.setClockStretching(false); // Use clock-stretching commands, (on) → void
27
+ console.log('raw', await sensor.readRaw()); // Read raw words, () → [number, number]
28
+ console.log('T', await sensor.readTemperature(), 'RH', await sensor.readHumidity()); // Read temperature only, () → number ; Read humidity only, () → number
29
+
30
+ // --- Periodic mode at 2 mps: ten samples ---
31
+ await sensor.startPeriodic(2, 'high'); // Start periodic mode, (rate, repeatability='high', art=false) → void
32
+ for (let got = 0; got < 10;) {
33
+ const res = await sensor.fetch(); // Fetch the latest periodic result, () → [number, number] | null
34
+ if (res === null) { await sleep(50); continue; }
35
+ got++;
36
+ console.log('periodic %d: %s C %s %%RH', got, res[0].toFixed(2), res[1].toFixed(1));
37
+ }
38
+ await sensor.stopPeriodic(); // Stop periodic mode, () → void
39
+
40
+ // --- Heater test: 10 s ---
41
+ const before = await sensor.readTemperature();
42
+ await sensor.setHeater(true); // Switch the heater, (on) → void
43
+ await sleep(10000);
44
+ console.log('heater status bit:', !!((await sensor.readStatus()) & 0x2000)); // Read status register, () → number
45
+ console.log('temperature rise: %s K', ((await sensor.readTemperature()) - before).toFixed(2));
46
+ await sensor.setHeater(false);
47
+ await sensor.clearStatus(); // Clear status flags, () → void
48
+ console.log('status 0x' + (await sensor.readStatus()).toString(16));
49
+ console.log('dew point %s C', (await sensor.dewPoint()).toFixed(2)); // Compute dew point, () → number °C
50
+ connection.close();
51
+ }
52
+
53
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { SHT31Minimal } = require('../../../src/chips/humidity/sht31');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x44', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x44) → Connection
11
+ const sensor = new SHT31Minimal(connection); // Create SHT31 driver, (connection) → void
12
+
13
+ async function main() {
14
+ while (true) {
15
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number °C, number %RH]
16
+ console.log('%s C, %s %%RH', t.toFixed(2), rh.toFixed(1));
17
+ await sleep(2000);
18
+ }
19
+ }
20
+
21
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,55 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { Si7021Full } = require('../../../src/chips/humidity/si7021');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
11
+ const sensor = new Si7021Full(connection); // Create Si7021 driver, (connection) → void
12
+
13
+ async function main() {
14
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number °C, number %RH]
15
+ // RH without hold master, temperature of the same conversion
16
+ console.log(t, rh);
17
+ console.log(await sensor.readHumidity()); // Read humidity only, () → number %RH
18
+ console.log(await sensor.readTemperature()); // Read temperature (new conversion), () → number °C
19
+ console.log(await sensor.readTemperaturePrevious()); // Temperature of the last RH measurement, () → number °C
20
+ // command 0xE0, no new conversion
21
+ console.log(await sensor.readRawHumidity(), await sensor.readRawTemperature()); // Read raw codes, () → number ; () → number
22
+ // 16-bit codes, status bits included
23
+
24
+ await sensor.setResolution(11, 11); // Set resolution, (rhBits, temperatureBits) → void
25
+ // (12, 14) / (8, 12) / (10, 13) / (11, 11)
26
+ console.log(await sensor.getResolution()); // Get resolution, () → [number, number]
27
+ console.log(await sensor.conversionTime()); // Typical conversion time, () → number s
28
+ await sensor.setResolution(12, 14);
29
+
30
+ await sensor.setHeater(true); // Switch the heater, (on) → void
31
+ // about 3.1 mA; readings drift while on
32
+ console.log(await sensor.isHeaterOn()); // Heater state, () → boolean
33
+ await sensor.setHeater(false);
34
+ console.log(await sensor.vddLow()); // Supply low flag, () → boolean
35
+ // true when VDD is below about 1.9 V
36
+ const reg = await sensor.readUserRegister(); // Read USER_REG_1, () → number
37
+ // 0x3A after reset
38
+ await sensor.writeUserRegister(reg); // Write USER_REG_1, (value) → void
39
+ // reserved bits are preserved
40
+ sensor.setClockStretching(false); // Use hold-master commands, (on) → void
41
+ // needs bus-layer support; off polls the NACKed read header
42
+
43
+ console.log((await sensor.serialNumber()).toString(16)); // Electronic serial number, () → bigint
44
+ console.log((await sensor.deviceType()).toString(16)); // Device type, () → number
45
+ // 0x15 = Si7021
46
+ console.log((await sensor.firmwareRevision()).toString(16)); // Firmware revision, () → number
47
+ // 0xFF = firmware 1.0
48
+ console.log(await sensor.dewPoint()); // Compute dew point, () → number °C
49
+ // one measurement, Magnus formula
50
+ await sensor.softReset(); // Software reset, () → void
51
+ // 0xFE, waits 15 ms
52
+ connection.close();
53
+ }
54
+
55
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { Si7021Full } = require('../../../src/chips/humidity/si7021');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
11
+ const sensor = new Si7021Full(connection); // Create Si7021 driver, (connection) → void
12
+
13
+ async function main() {
14
+ await sensor.softReset(); // Software reset, () → void
15
+ console.log('device type 0x' + (await sensor.deviceType()).toString(16)); // Device type, () → number
16
+ console.log('serial number 0x' + (await sensor.serialNumber()).toString(16)); // Electronic serial number, () → bigint
17
+ console.log('firmware 0x' + (await sensor.firmwareRevision()).toString(16)); // Firmware revision, () → number
18
+ console.log('VDD low:', await sensor.vddLow()); // Supply low flag, () → boolean
19
+
20
+ // --- Condensation-aware hygrometer: 15 readings, 2 s apart ---
21
+ for (let i = 0; i < 15; i++) {
22
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number, number]
23
+ console.log('%s C %s %%RH dew point %s C', t.toFixed(2), rh.toFixed(1), (await sensor.dewPoint()).toFixed(2)); // Compute dew point, () → number °C
24
+ await sleep(2000);
25
+ }
26
+
27
+ // --- Value accessors and raw codes ---
28
+ console.log('RH', await sensor.readHumidity(), 'T', await sensor.readTemperature()); // () → number ; () → number
29
+ console.log('T of last RH', await sensor.readTemperaturePrevious()); // () → number
30
+ console.log('raw', await sensor.readRawHumidity(), await sensor.readRawTemperature()); // () → number ; () → number
31
+ sensor.setClockStretching(false); // Use hold-master commands, (on) → void
32
+ const reg = await sensor.readUserRegister(); // Read USER_REG_1, () → number
33
+ await sensor.writeUserRegister(reg); // Write USER_REG_1, (value) → void
34
+
35
+ // --- Resolution comparison ---
36
+ for (const [rhBits, tBits] of [[12, 14], [10, 13], [11, 11], [8, 12]]) {
37
+ await sensor.setResolution(rhBits, tBits); // Set resolution, (rhBits, temperatureBits) → void
38
+ const start = process.hrtime.bigint();
39
+ const [t, rh] = await sensor.read();
40
+ const ms = Number(process.hrtime.bigint() - start) / 1e6;
41
+ console.log('%d/%d bit: %s C %s %%RH, %s ms (typ. %s ms), resolution %j', rhBits, tBits, t.toFixed(2), rh.toFixed(1),
42
+ ms.toFixed(1), ((await sensor.conversionTime()) * 1000).toFixed(1), await sensor.getResolution()); // () → number s ; () → [number, number]
43
+ }
44
+ await sensor.setResolution(12, 14);
45
+
46
+ // --- Heater test: 10 s ---
47
+ const rhBefore = await sensor.readHumidity();
48
+ const tBefore = await sensor.readTemperature();
49
+ await sensor.setHeater(true); // Switch the heater, (on) → void
50
+ await sleep(10000);
51
+ console.log('heater on:', await sensor.isHeaterOn()); // Heater state, () → boolean
52
+ console.log('RH drop %s %%RH, temperature rise %s K', (rhBefore - await sensor.readHumidity()).toFixed(1),
53
+ ((await sensor.readTemperature()) - tBefore).toFixed(2));
54
+ await sensor.setHeater(false);
55
+ connection.close();
56
+ }
57
+
58
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { Si7021Minimal } = require('../../../src/chips/humidity/si7021');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x40', 16);
8
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
9
+
10
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x40) → Connection
11
+ const sensor = new Si7021Minimal(connection); // Create Si7021 driver, (connection) → void
12
+
13
+ async function main() {
14
+ while (true) {
15
+ const [t, rh] = await sensor.read(); // Read temperature and humidity, () → [number °C, number %RH]
16
+ console.log('%s C, %s %%RH', t.toFixed(2), rh.toFixed(1));
17
+ await sleep(2000);
18
+ }
19
+ }
20
+
21
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,85 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { BNO055Full, BNO055Mode, BNO055Source } = 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 BNO055Full(connection); // Create BNO055 driver, (connection) → void
11
+
12
+ async function main() {
13
+ await imu.init(); // Wait for boot and NDOF, () → void
14
+ // rejects if CHIP_ID is not 0xA0
15
+
16
+ const [h, r, p] = await imu.euler(); // Read fused Euler angles, () → [number, number, number] rad
17
+ // burst read of 0x1A…0x1F, raw / 900
18
+ const [w, x, y, z] = await imu.quaternion(); // Read orientation quaternion, () → [number, number, number, number]
19
+ // raw / 16384
20
+ const lin = await imu.linearAcceleration(); // Read gravity-free acceleration, () → [number, number, number] m/s²
21
+ // raw / 100
22
+ const grav = await imu.gravity(); // Read gravity vector, () → [number, number, number] m/s²
23
+ // raw / 100
24
+ const acc = await imu.acceleration(); // Read raw accelerometer, () → [number, number, number] m/s²
25
+ // raw / 100
26
+ const mag = await imu.magneticField(); // Read raw magnetometer, () → [number, number, number] µT
27
+ // raw / 16
28
+ const gyr = await imu.angularRate(); // Read raw gyroscope, () → [number, number, number] rad/s
29
+ // raw / 900
30
+ const t = await imu.temperature(); // Read temperature, () → number °C
31
+ // signed 8 bit, 1 LSB = 1 °C
32
+ const [sys, gy, ac, mg] = await imu.calibrationStatus(); // Read calibration status, () → [number, number, number, number]
33
+ // each 0 (uncalibrated) … 3 (calibrated)
34
+ const done = await imu.isCalibrated(); // Check full calibration, () → boolean
35
+ // true when all four values are 3
36
+ console.log(h, r, p, w, x, y, z, lin, grav, acc, mag, gyr, t, sys, gy, ac, mg, done);
37
+
38
+ const mode = await imu.getMode(); // Read operation mode, () → number
39
+ // 0x0C = NDOF
40
+ await imu.setMode(BNO055Mode.IMU); // Select operation mode, (mode) → void
41
+ // via CONFIGMODE, 19 ms + 7 ms waits
42
+ await imu.setMode(BNO055Mode.NDOF); // Select operation mode, (mode) → void
43
+ // back to 9-DOF fusion
44
+
45
+ const blob = await imu.getCalibration(); // Read offsets and radii, () → Buffer
46
+ // 22 bytes, 0x55…0x6A, read in CONFIGMODE
47
+ await imu.setCalibration(blob); // Restore offsets and radii, (data) → void
48
+ // 22 bytes, written in CONFIGMODE
49
+ await imu.setAxisRemap(0x24, 0x00); // Remap axes, (config, sign) → void
50
+ // 0x24/0x00 = identity placement
51
+ await imu.setPowerMode('normal'); // Select power mode, (mode) → void
52
+ // 'normal', 'low_power' or 'suspend'
53
+ await imu.setTemperatureSource('accelerometer'); // Select temperature sensor, (source) → void
54
+ // 'accelerometer' or 'gyroscope'
55
+ await imu.useExternalCrystal(false); // Select clock source, (on) → void
56
+ // internal oscillator unless a crystal is fitted
57
+
58
+ const result = await imu.selfTest(); // Run self-test, () → {acc, mag, gyr, mcu}
59
+ // booleans, true = passed
60
+ const [status, error] = await imu.systemStatus(); // Read system status, () → [number, number]
61
+ // SYS_STATUS 5 = fusion running, SYS_ERR 0 = none
62
+ const info = await imu.revisions(); // Read revisions, () → object
63
+ // chip/acc/mag/gyr IDs, SW revision, bootloader
64
+ const uid = await imu.uniqueId(); // Read unique ID, () → Buffer
65
+ // 16 bytes from page 1
66
+ console.log(mode, result, status, error, info, uid.toString('hex'));
67
+
68
+ await imu.enableInterrupt(BNO055Source.ACC_BSX_DRDY); // Enable interrupt source, (source) → void
69
+ // INT_EN and INT_MSK on page 1
70
+ const ints = await imu.pollInterrupt(); // Poll interrupt status, () → number
71
+ // INT_STA, cleared on read
72
+ await imu.onInterrupt((s) => console.log('INT status 0x' + s.toString(16))); // Subscribe to interrupts, (callback, intPin?) → void
73
+ // uses an INT pin or a 100 ms polling timer
74
+ await new Promise(res => setTimeout(res, 500));
75
+ await imu.offInterrupt(); // Unsubscribe, () → void
76
+ // stops the callback
77
+ await imu.disableInterrupt(BNO055Source.ACC_BSX_DRDY); // Disable interrupt source, (source) → void
78
+ // clears the INT_EN and INT_MSK bits
79
+ console.log(ints);
80
+
81
+ await imu.reset(); // Reset the chip, () → void
82
+ // RST_SYS, waits 650 ms, re-enters NDOF
83
+ }
84
+
85
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { BNO055Full, BNO055Mode, BNO055Source } = 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 BNO055Full(connection); // Create BNO055 driver, (connection) → void
11
+ const sleep = (ms) => new Promise(res => setTimeout(res, ms));
12
+
13
+ async function main() {
14
+ await imu.init(); // Wait for boot and NDOF, () → void
15
+
16
+ // --- Identify the part ---
17
+ const info = await imu.revisions(); // Read revisions, () → object
18
+ console.log('chip 0x%s, SW %d.%d, bootloader %d', info.chipId.toString(16), info.swRevision >> 8, info.swRevision & 0xFF, info.bootloader);
19
+ console.log('unique id', (await imu.uniqueId()).toString('hex')); // Read unique ID, () → Buffer
20
+
21
+ // --- Orientation monitor until fully calibrated (move the board in a figure eight) ---
22
+ for (;;) {
23
+ const [h, r, p] = await imu.euler(); // Read fused Euler angles, () → [number, number, number] rad
24
+ const [w, x, y, z] = await imu.quaternion(); // Read orientation quaternion, () → [number, number, number, number]
25
+ const lin = await imu.linearAcceleration(); // Read gravity-free acceleration, () → [number, number, number] m/s²
26
+ const grav = await imu.gravity(); // Read gravity vector, () → [number, number, number] m/s²
27
+ const cal = await imu.calibrationStatus(); // Read calibration status, () → [number, number, number, number]
28
+ console.log('euler %s %s %s quat %s %s %s %s lin %s grav %s cal %s',
29
+ h.toFixed(2), r.toFixed(2), p.toFixed(2), w.toFixed(3), x.toFixed(3), y.toFixed(3), z.toFixed(3),
30
+ lin.map(v => v.toFixed(2)).join(' '), grav.map(v => v.toFixed(2)).join(' '), cal.join(''));
31
+ if (await imu.isCalibrated()) break; // Check full calibration, () → boolean
32
+ await sleep(100);
33
+ }
34
+
35
+ // --- Save the calibration, relative mode for 5 s, restore it ---
36
+ const blob = await imu.getCalibration(); // Read offsets and radii, () → Buffer
37
+ console.log('calibration', blob.toString('hex'));
38
+ await imu.setMode(BNO055Mode.IMU); // Select operation mode, (mode) → void
39
+ console.log('mode', await imu.getMode()); // Read operation mode, () → number
40
+ await sleep(5000);
41
+ await imu.setMode(BNO055Mode.NDOF); // Select operation mode, (mode) → void
42
+ await imu.setCalibration(blob); // Restore offsets and radii, (data) → void
43
+
44
+ // --- Self-test, temperature and configuration ---
45
+ console.log('self-test', await imu.selfTest()); // Run self-test, () → object
46
+ console.log('temperature', await imu.temperature(), 'C'); // Read temperature, () → number °C
47
+ await imu.setTemperatureSource('gyroscope'); // Select temperature sensor, (source) → void
48
+ await imu.setAxisRemap(0x24, 0x00); // Remap axes, (config, sign) → void
49
+ await imu.setPowerMode('normal'); // Select power mode, (mode) → void
50
+ await imu.useExternalCrystal(false); // Select clock source, (on) → void
51
+
52
+ // --- Raw sensors in AMG mode ---
53
+ await imu.setMode(BNO055Mode.AMG); // Select operation mode, (mode) → void
54
+ console.log('acc', await imu.acceleration()); // Read raw accelerometer, () → [number, number, number] m/s²
55
+ console.log('mag', await imu.magneticField()); // Read raw magnetometer, () → [number, number, number] µT
56
+ console.log('gyr', await imu.angularRate()); // Read raw gyroscope, () → [number, number, number] rad/s
57
+
58
+ // --- Interrupts: accelerometer/fusion data ready ---
59
+ await imu.setMode(BNO055Mode.NDOF); // Select operation mode, (mode) → void
60
+ await imu.enableInterrupt(BNO055Source.ACC_BSX_DRDY); // Enable interrupt source, (source) → void
61
+ await imu.onInterrupt((s) => console.log('INT 0x' + s.toString(16))); // Subscribe to interrupts, (callback, intPin?) → void
62
+ await sleep(500);
63
+ await imu.offInterrupt(); // Unsubscribe, () → void
64
+ await imu.disableInterrupt(BNO055Source.ACC_BSX_DRDY); // Disable interrupt source, (source) → void
65
+ console.log('interrupt status', await imu.pollInterrupt()); // Poll interrupt status, () → number
66
+ console.log('system status', await imu.systemStatus()); // Read system status, () → [number, number]
67
+ await imu.reset(); // Reset the chip, () → void
68
+ }
69
+
70
+ main().catch(err => { console.error(err); process.exit(1); });