periph 1.4.0 → 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 (112) hide show
  1. package/README.md +27 -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/mcp3422/complete.js +48 -0
  24. package/examples/adc_dac/mcp3422/demo.js +49 -0
  25. package/examples/adc_dac/mcp3422/minimal.js +25 -0
  26. package/examples/display/sh1106/complete.js +101 -0
  27. package/examples/display/sh1106/demo.js +68 -0
  28. package/examples/display/sh1106/minimal.js +25 -0
  29. package/examples/display/ssd1305/complete.js +129 -0
  30. package/examples/display/ssd1305/demo.js +71 -0
  31. package/examples/display/ssd1305/minimal.js +25 -0
  32. package/examples/display/ssd1306/complete.js +115 -0
  33. package/examples/display/ssd1306/demo.js +70 -0
  34. package/examples/display/ssd1306/minimal.js +25 -0
  35. package/examples/environmental/am2315/complete.js +34 -0
  36. package/examples/environmental/am2315/demo.js +46 -0
  37. package/examples/environmental/am2315/minimal.js +21 -0
  38. package/examples/humidity/htu21df/complete.js +49 -0
  39. package/examples/humidity/htu21df/demo.js +50 -0
  40. package/examples/humidity/htu21df/minimal.js +22 -0
  41. package/examples/humidity/sht31/complete.js +53 -0
  42. package/examples/humidity/sht31/demo.js +53 -0
  43. package/examples/humidity/sht31/minimal.js +21 -0
  44. package/examples/humidity/si7021/complete.js +55 -0
  45. package/examples/humidity/si7021/demo.js +58 -0
  46. package/examples/humidity/si7021/minimal.js +21 -0
  47. package/examples/imu/bno055/complete.js +85 -0
  48. package/examples/imu/bno055/demo.js +70 -0
  49. package/examples/imu/bno055/minimal.js +20 -0
  50. package/examples/io_expander/pca6408a/complete.js +63 -0
  51. package/examples/io_expander/pca6408a/demo.js +58 -0
  52. package/examples/io_expander/pca6408a/minimal.js +20 -0
  53. package/examples/io_expander/pca6416a/complete.js +68 -0
  54. package/examples/io_expander/pca6416a/demo.js +60 -0
  55. package/examples/io_expander/pca6416a/minimal.js +20 -0
  56. package/examples/io_expander/pca9536/complete.js +63 -0
  57. package/examples/io_expander/pca9536/demo.js +57 -0
  58. package/examples/io_expander/pca9536/minimal.js +20 -0
  59. package/examples/io_expander/pca9539/complete.js +68 -0
  60. package/examples/io_expander/pca9539/demo.js +60 -0
  61. package/examples/io_expander/pca9539/minimal.js +20 -0
  62. package/examples/light/max44009/complete.js +56 -0
  63. package/examples/light/max44009/demo.js +49 -0
  64. package/examples/light/max44009/minimal.js +22 -0
  65. package/examples/memory/at24c02n/complete.js +39 -0
  66. package/examples/memory/at24c02n/demo.js +39 -0
  67. package/examples/memory/at24c02n/minimal.js +15 -0
  68. package/examples/memory/at24c64/complete.js +39 -0
  69. package/examples/memory/at24c64/demo.js +39 -0
  70. package/examples/memory/at24c64/minimal.js +15 -0
  71. package/examples/memory/cat24c512/complete.js +39 -0
  72. package/examples/memory/cat24c512/demo.js +39 -0
  73. package/examples/memory/cat24c512/minimal.js +15 -0
  74. package/examples/other/pca9541/complete.js +52 -0
  75. package/examples/other/pca9541/demo.js +61 -0
  76. package/examples/other/pca9541/minimal.js +23 -0
  77. package/examples/temperature/tmp102/complete.js +74 -0
  78. package/examples/temperature/tmp102/demo.js +71 -0
  79. package/examples/temperature/tmp102/minimal.js +20 -0
  80. package/examples/tof/vl6180x/complete.js +62 -0
  81. package/examples/tof/vl6180x/demo.js +77 -0
  82. package/examples/tof/vl6180x/minimal.js +23 -0
  83. package/package.json +1 -1
  84. package/src/chips/adc_dac/ad5251.js +345 -0
  85. package/src/chips/adc_dac/ad5252.js +345 -0
  86. package/src/chips/adc_dac/ads7828.js +225 -0
  87. package/src/chips/adc_dac/cat5171.js +213 -0
  88. package/src/chips/adc_dac/cs43l22.js +645 -0
  89. package/src/chips/adc_dac/ds1881.js +357 -0
  90. package/src/chips/adc_dac/ds3502.js +241 -0
  91. package/src/chips/adc_dac/mcp3422.js +312 -0
  92. package/src/chips/display/mono_oled.js +182 -0
  93. package/src/chips/display/sh1106.js +496 -0
  94. package/src/chips/display/ssd1305.js +655 -0
  95. package/src/chips/display/ssd1306.js +458 -0
  96. package/src/chips/environmental/am2315.js +237 -0
  97. package/src/chips/humidity/htu21df.js +222 -0
  98. package/src/chips/humidity/sht31.js +284 -0
  99. package/src/chips/humidity/si7021.js +282 -0
  100. package/src/chips/imu/bno055.js +496 -0
  101. package/src/chips/io_expander/pca6408a.js +407 -0
  102. package/src/chips/io_expander/pca6416a.js +455 -0
  103. package/src/chips/io_expander/pca9536.js +398 -0
  104. package/src/chips/io_expander/pca9539.js +457 -0
  105. package/src/chips/light/max44009.js +336 -0
  106. package/src/chips/memory/at24c02n.js +220 -0
  107. package/src/chips/memory/at24c64.js +220 -0
  108. package/src/chips/memory/cat24c512.js +221 -0
  109. package/src/chips/other/pca9541.js +315 -0
  110. package/src/chips/temperature/tmp102.js +361 -0
  111. package/src/chips/tof/vl6180x.js +580 -0
  112. package/src/discovery/registry.js +65 -13
@@ -0,0 +1,115 @@
1
+ // SSD1306 complete example: 0.96" 128 x 64 OLED module (SSD1306) over I2C on a Raspberry Pi (Node.js).
2
+ // Wiring: SDA=GPIO2, SCL=GPIO3 (/dev/i2c-1), VCC = 3V3 (the module generates the OLED supply with the SSD1306 charge pump); address 0x3C (SA0 = low) or 0x3D (SA0 = high). Complete: every Full method - drawing, contrast, invert, flip, scroll, drive tuning, reads, sleep.
3
+ 'use strict';
4
+ const { I2CConnection } = require('../../../src/connection/i2c');
5
+ const { SSD1306Full } = require('../../../src/chips/display/ssd1306');
6
+
7
+ const connection = new I2CConnection(1, 0x3C); // Create I2C connection, (busNumber=1, addr=0x3C)
8
+ const lcd = new SSD1306Full(connection); // Create SSD1306 driver, (connection, width=128, height=64, externalVcc=false)
9
+
10
+ (async () => {
11
+ await lcd.ready();
12
+ // Framebuffer drawing: everything is composed in RAM and sent with show().
13
+ lcd.clear(); // Clear framebuffer, ()
14
+ // zeroes every pixel of the framebuffer; the panel is unchanged until show()
15
+ lcd.fill(true); // Fill framebuffer, (on=true)
16
+ // sets (or clears) every pixel
17
+ lcd.fill(false); // Fill framebuffer, (on=true)
18
+ lcd.drawRect(0, 0, 128, 64); // Draw rectangle, (x, y, w, h, on=true, fill=false)
19
+ // one-pixel outline around the whole panel
20
+ lcd.drawLine(0, 0, 127, 63); // Draw line, (x0, y0, x1, y1, on=true)
21
+ // Bresenham line into the framebuffer
22
+ lcd.drawLine(0, 63, 127, 0); // Draw line, (x0, y0, x1, y1, on=true)
23
+ lcd.drawRect(10, 10, 30, 20, true, true); // Draw rectangle, (x, y, w, h, on=true, fill=false)
24
+ // filled rectangle
25
+ lcd.drawRect(18, 16, 14, 8, false, true); // Draw rectangle, (x, y, w, h, on=true, fill=false)
26
+ // on=false clears pixels: a hole in the filled rectangle
27
+ lcd.setPixel(64, 32, true); // Set pixel, (x, y, on=true)
28
+ const lit = lcd.getPixel(64, 32); // Read framebuffer pixel, (x, y) → boolean
29
+ // reads the framebuffer, not the panel
30
+ const icon = Buffer.from([0x3C, 0x42, 0x81, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C, 0x00, 0x7E, 0x81, 0x81, 0x81, 0x7E, 0x00]);
31
+ lcd.blit(100, 16, icon, 2); // Copy bitmap, (x, y multiple of 8, columns, heightPages=1)
32
+ // two page rows of 8 columns, bit 0 = top pixel
33
+ await lcd.show(); // Send framebuffer to the display, () → Promise<void>
34
+ // per page: page + column command, then 128 data bytes in 31-byte chunks
35
+ await new Promise((r) => setTimeout(r, 2000));
36
+ await lcd.showPage(2); // Send one framebuffer page, (page 0..7) → Promise<void>
37
+ // cheaper partial update of 8 rows
38
+
39
+ // Contrast and display modes.
40
+ await lcd.setContrast(0x20); // Set contrast, (value 0..255) → Promise<void>
41
+ // segment drive current; reset value is 0x7F
42
+ await new Promise((r) => setTimeout(r, 800));
43
+ await lcd.setContrast(0x7F); // Set contrast, (value 0..255) → Promise<void>
44
+ await lcd.setInvert(true); // Invert display, (on) → Promise<void>
45
+ // RAM 0 = pixel on; RAM is unchanged
46
+ await new Promise((r) => setTimeout(r, 800));
47
+ await lcd.setInvert(false); // Invert display, (on) → Promise<void>
48
+ await lcd.setAllOn(true); // Force all pixels on, (on) → Promise<void>
49
+ // test mode, RAM is kept
50
+ await new Promise((r) => setTimeout(r, 800));
51
+ await lcd.setAllOn(false); // Force all pixels on, (on) → Promise<void>
52
+ await lcd.setFlip(true, true); // Mirror display, (horizontal, vertical) → Promise<void>
53
+ // segment re-map and COM scan direction; framebuffer unchanged
54
+ await new Promise((r) => setTimeout(r, 1500));
55
+ await lcd.setFlip(false, false); // Mirror display, (horizontal, vertical) → Promise<void>
56
+
57
+ // Hardware vertical scroll and drive tuning.
58
+ for (let line = 0; line < 64; line += 2) {
59
+ await lcd.setStartLine(line); // Scroll the image up, (line 0..63) → Promise<void>
60
+ // RAM line shown at COM0; the image wraps around
61
+ await new Promise((r) => setTimeout(r, 30));
62
+ }
63
+ await lcd.setStartLine(0); // Scroll the image up, (line 0..63) → Promise<void>
64
+ await lcd.setDisplayOffset(0); // Set COM offset, (offset 0..63) → Promise<void>
65
+ // shifts the COM mapping
66
+ await lcd.setMultiplex(64); // Set multiplex ratio, (ratio 16..64) → Promise<void>
67
+ // number of driven rows
68
+ await lcd.setClock(1, 8); // Set clock, (divide 1..16, osc 0..15) → Promise<void>
69
+ // divide ratio 1 at the reset oscillator setting
70
+ await lcd.setPrecharge(2, 2); // Set pre-charge, (phase1 1..15, phase2 1..15) → Promise<void>
71
+ // pre-charge and discharge periods in display clocks
72
+ await lcd.setComConfig(true); // Set COM pin configuration, (alternative, remap=false) → Promise<void>
73
+ // alternative is the reset value and right for 128 x 64 modules
74
+ await lcd.setVcomh(0x20); // Set VCOMH deselect level, (value 0x00..0x70) → Promise<void>
75
+ // 0x20 (about 0.77 x VCC) is the reset value
76
+ await lcd.setChargePump(true); // Enable the charge pump, (enabled) → Promise<void>
77
+ // 0x8D 0x14; takes effect with the display on
78
+
79
+ // Addressing modes.
80
+ await lcd.setAddressingMode('horizontal'); // Select addressing mode, (mode "page"|"horizontal"|"vertical") → Promise<void>
81
+ // show() then streams the frame in one transfer
82
+ await lcd.show(); // Update display, () → Promise<void>
83
+ await lcd.setAddressingMode('page'); // Select addressing mode, (mode "page"|"horizontal"|"vertical") → Promise<void>
84
+
85
+ // Hardware scrolling (RAM access is refused while a scroll is active).
86
+ await lcd.setScrollHorizontal(true, 0, 7, 7); // Set horizontal scroll, (right, startPage, endPage, interval 0..7) → Promise<void>
87
+ // interval index 7 = 2 frames per step
88
+ await lcd.scrollStart(); // Start scrolling, () → Promise<void>
89
+ await new Promise((r) => setTimeout(r, 2000));
90
+ await lcd.scrollStop(); // Stop scrolling, () → Promise<void>
91
+ // deactivates and rewrites the RAM from the framebuffer
92
+ await lcd.setScrollArea(0, 64); // Set vertical scroll area, (topFixed 0..63, scrollRows 1..64) → Promise<void>
93
+ await lcd.setScrollVertical(true, 0, 7, 7, 1); // Set diagonal scroll, (right, startPage, endPage, interval 0..7, rows 1..63) → Promise<void>
94
+ await lcd.scrollStart(); // Start scrolling, () → Promise<void>
95
+ await new Promise((r) => setTimeout(r, 2000));
96
+ await lcd.scrollStop(); // Stop scrolling, () → Promise<void>
97
+
98
+ // Raw commands.
99
+ await lcd.sendCommand(0xE3); // Send raw command stream, (...bytes) → Promise<void>
100
+ // NOP
101
+
102
+ // Power.
103
+ await lcd.setDisplay(false); // Display on/off, (on) → Promise<void>
104
+ // RAM and settings are kept
105
+ await new Promise((r) => setTimeout(r, 1000));
106
+ await lcd.setDisplay(true); // Display on/off, (on) → Promise<void>
107
+ await lcd.sleep(); // Enter sleep, () → Promise<void>
108
+ // display off; switch VCC off afterwards
109
+ await new Promise((r) => setTimeout(r, 1000));
110
+ await lcd.wake(); // Leave sleep, () → Promise<void>
111
+ // display on and a 100 ms wait (tAF)
112
+ console.log(`lit ${lit}`);
113
+ connection.close();
114
+ process.exit(0);
115
+ })().catch((e) => { console.error(e); process.exit(1); });
@@ -0,0 +1,70 @@
1
+ // SSD1306 demo example: 0.96" 128 x 64 OLED module (SSD1306) over I2C on a Raspberry Pi (Node.js).
2
+ // Wiring: SDA=GPIO2, SCL=GPIO3 (/dev/i2c-1), VCC = 3V3 (the module generates the OLED supply with the SSD1306 charge pump); address 0x3C (SA0 = low) or 0x3D (SA0 = high). Demo: a bouncing ball inside a border, a contrast fade, inverse flashes, hardware scroll and sleep.
3
+ 'use strict';
4
+ const { I2CConnection } = require('../../../src/connection/i2c');
5
+ const { SSD1306Full } = require('../../../src/chips/display/ssd1306');
6
+
7
+ const connection = new I2CConnection(1, 0x3C); // Create I2C connection, (busNumber=1, addr=0x3C)
8
+ const lcd = new SSD1306Full(connection); // Create SSD1306 driver, (connection, width=128, height=64, externalVcc=false)
9
+
10
+ (async () => {
11
+ await lcd.ready();
12
+ // --- Draw the border once ---
13
+ // A one-pixel border around the 128 x 64 panel; the ball bounces inside it.
14
+ lcd.clear(); // Clear framebuffer, ()
15
+ lcd.drawRect(0, 0, 128, 64); // Draw rectangle, (x, y, w, h, on=true, fill=false)
16
+ await lcd.show(); // Send framebuffer to the display, () → Promise<void>
17
+
18
+ // --- Bounce a 6 x 6 ball at about 20 frames per second ---
19
+ // The ball is erased and redrawn in the framebuffer, then the frame is sent with show().
20
+ let bx = 20, by = 12, dx = 3, dy = 2;
21
+ for (let frame = 0; frame < 200; frame++) {
22
+ lcd.drawRect(bx, by, 6, 6, false, true); // Draw rectangle, (x, y, w, h, on=true, fill=false)
23
+ bx += dx;
24
+ by += dy;
25
+ if (bx < 2 || bx > 120) { dx = -dx; bx += 2 * dx; }
26
+ if (by < 2 || by > 56) { dy = -dy; by += 2 * dy; }
27
+ lcd.drawRect(bx, by, 6, 6, true, true); // Draw rectangle, (x, y, w, h, on=true, fill=false)
28
+ await lcd.show(); // Send framebuffer to the display, () → Promise<void>
29
+ await new Promise((r) => setTimeout(r, 50));
30
+ }
31
+
32
+ // --- Fade the contrast down and back up ---
33
+ // 255 -> 0 -> 255 in steps of 5 shows the full range of the segment drive current.
34
+ for (let v = 255; v >= 0; v -= 5) {
35
+ await lcd.setContrast(v); // Set contrast, (value 0..255) → Promise<void>
36
+ await new Promise((r) => setTimeout(r, 20));
37
+ }
38
+ for (let v = 0; v <= 255; v += 5) {
39
+ await lcd.setContrast(v); // Set contrast, (value 0..255) → Promise<void>
40
+ await new Promise((r) => setTimeout(r, 20));
41
+ }
42
+
43
+ // --- Flash the inverse display twice ---
44
+ for (let i = 0; i < 2; i++) {
45
+ await lcd.setInvert(true); // Invert display, (on) → Promise<void>
46
+ await new Promise((r) => setTimeout(r, 400));
47
+ await lcd.setInvert(false); // Invert display, (on) → Promise<void>
48
+ await new Promise((r) => setTimeout(r, 400));
49
+ }
50
+
51
+ // --- Hardware scroll ---
52
+ // The scroll engine moves all eight pages right in the controller without re-sending any data;
53
+ // RAM access is prohibited while it runs, so it is stopped before the next show().
54
+ await lcd.setScrollHorizontal(true, 0, 7, 7); // Set horizontal scroll, (right, startPage, endPage, interval 0..7) → Promise<void>
55
+ await lcd.scrollStart(); // Start scrolling, () → Promise<void>
56
+ await new Promise((r) => setTimeout(r, 5000));
57
+ await lcd.scrollStop(); // Stop scrolling, () → Promise<void>
58
+
59
+ // A diagonal scroll moves one row vertically per step in addition.
60
+ await lcd.setScrollVertical(true, 0, 7, 7, 1); // Set diagonal scroll, (right, startPage, endPage, interval 0..7, rows 1..63) → Promise<void>
61
+ await lcd.scrollStart(); // Start scrolling, () → Promise<void>
62
+ await new Promise((r) => setTimeout(r, 5000));
63
+ await lcd.scrollStop(); // Stop scrolling, () → Promise<void>
64
+
65
+ // --- Power down ---
66
+ // Sleep turns the display off; RAM and registers are kept.
67
+ await lcd.sleep(); // Enter sleep, () → Promise<void>
68
+ connection.close();
69
+ process.exit(0);
70
+ })().catch((e) => { console.error(e); process.exit(1); });
@@ -0,0 +1,25 @@
1
+ // SSD1306 minimal example: 0.96" 128 x 64 OLED module (SSD1306) over I2C on a Raspberry Pi (Node.js).
2
+ // Wiring: SDA=GPIO2, SCL=GPIO3 (/dev/i2c-1), VCC = 3V3 (the module generates the OLED supply with the SSD1306 charge pump); address 0x3C (SA0 = low) or 0x3D (SA0 = high). Minimal use: a border and a diagonal on the 128 x 64 panel.
3
+ 'use strict';
4
+ const { I2CConnection } = require('../../../src/connection/i2c');
5
+ const { SSD1306Minimal } = require('../../../src/chips/display/ssd1306');
6
+
7
+ const connection = new I2CConnection(1, 0x3C); // Create I2C connection, (busNumber=1, addr=0x3C)
8
+ const lcd = new SSD1306Minimal(connection); // Create SSD1306 driver, (connection, width=128, height=64, externalVcc=false)
9
+
10
+ (async () => {
11
+ await lcd.ready();
12
+ lcd.clear(); // Clear framebuffer, ()
13
+ for (let x = 0; x < 128; x++) {
14
+ lcd.setPixel(x, 0); // Set pixel, (x 0..127, y 0..63, on=true)
15
+ lcd.setPixel(x, 63); // Set pixel, (x 0..127, y 0..63, on=true)
16
+ lcd.setPixel(x, Math.floor(x / 2)); // Set pixel, (x 0..127, y 0..63, on=true)
17
+ }
18
+ for (let y = 0; y < 64; y++) {
19
+ lcd.setPixel(0, y); // Set pixel, (x 0..127, y 0..63, on=true)
20
+ lcd.setPixel(127, y); // Set pixel, (x 0..127, y 0..63, on=true)
21
+ }
22
+ await lcd.show(); // Send framebuffer to the display, () → Promise<void>
23
+ connection.close();
24
+ process.exit(0);
25
+ })().catch((e) => { console.error(e); process.exit(1); });
@@ -0,0 +1,34 @@
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
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR); // Create I²C connection, (bus=1, addr=0x5C) → Connection
10
+ const sensor = new AM2315Full(connection); // Create AM2315 driver, (connection) → void
11
+
12
+ async function main() {
13
+ const r = await sensor.read(); // Read temperature and humidity, () → {temperature_c, humidity_pct}
14
+ // previous measurement; reads are kept >= 2 s apart, CRC-16 checked
15
+ console.log(r.temperature_c, r.humidity_pct);
16
+ console.log(await sensor.readTemperature()); // Read temperature only, () → number °C
17
+ console.log(await sensor.readHumidity()); // Read humidity only, () → number %RH
18
+ console.log(await sensor.readRaw()); // Read raw words, () → [number, number]
19
+ // [humidity x10, temperature x10 as sign + magnitude]
20
+ console.log(await sensor.readFresh()); // Real-time value, () → {temperature_c, humidity_pct}
21
+ // two reads >= 2 s apart; returns the second
22
+ console.log(await sensor.readRegisters(0x08, 7)); // Read registers, (start, count) → Buffer
23
+ // function 0x03, 1-10 registers, CRC verified
24
+ console.log(await sensor.readDeviceInfo()); // Read device information, () → {model, version, device_id}
25
+ console.log(await sensor.readUserRegisters()); // Read user registers, () → [number, number]
26
+ await sensor.writeUserRegisters(0x1234, 0xABCD); // Write user registers, (user1, user2) → void
27
+ await sensor.writeRegisters(0x10, [0x00, 0x01]); // Write registers, (start, data) → void
28
+ // only the user registers 0x10-0x13 are allowed
29
+ await sensor.wake(); // Wake the sensor, () → void
30
+ // dummy write, the NACK is ignored
31
+ connection.close();
32
+ }
33
+
34
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -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); });