periph 1.0.1 → 1.2.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 (241) hide show
  1. package/README.md +54 -12
  2. package/examples/accelerometer/adxl345/complete.js +52 -0
  3. package/examples/accelerometer/adxl345/demo.js +34 -0
  4. package/examples/accelerometer/adxl345/minimal.js +18 -0
  5. package/examples/accelerometer/adxl362/complete.js +57 -0
  6. package/examples/accelerometer/adxl362/demo.js +51 -0
  7. package/examples/accelerometer/adxl362/minimal.js +21 -0
  8. package/examples/adc_dac/ad7705/complete.js +32 -0
  9. package/examples/adc_dac/ad7705/demo.js +42 -0
  10. package/examples/adc_dac/ad7705/minimal.js +16 -0
  11. package/examples/adc_dac/ad7706/complete.js +35 -0
  12. package/examples/adc_dac/ad7706/demo.js +44 -0
  13. package/examples/adc_dac/ad7706/minimal.js +16 -0
  14. package/examples/adc_dac/hx710a/complete.js +45 -0
  15. package/examples/adc_dac/hx710a/demo.js +52 -0
  16. package/examples/adc_dac/hx710a/minimal.js +16 -0
  17. package/examples/adc_dac/hx710b/complete.js +45 -0
  18. package/examples/adc_dac/hx710b/demo.js +64 -0
  19. package/examples/adc_dac/hx710b/minimal.js +16 -0
  20. package/examples/adc_dac/hx711/complete.js +42 -0
  21. package/examples/adc_dac/hx711/demo.js +35 -0
  22. package/examples/adc_dac/hx711/minimal.js +16 -0
  23. package/examples/adc_dac/mcp4725/complete.js +19 -17
  24. package/examples/adc_dac/mcp4725/demo.js +5 -5
  25. package/examples/adc_dac/mcp4725/minimal.js +8 -6
  26. package/examples/adc_dac/mcp4728/complete.js +43 -0
  27. package/examples/adc_dac/mcp4728/demo.js +41 -0
  28. package/examples/adc_dac/mcp4728/minimal.js +13 -0
  29. package/examples/adc_dac/pcf8591/complete.js +41 -0
  30. package/examples/adc_dac/pcf8591/demo.js +39 -0
  31. package/examples/adc_dac/pcf8591/minimal.js +19 -0
  32. package/examples/comms/mcp2515/complete.js +75 -0
  33. package/examples/comms/mcp2515/demo.js +66 -0
  34. package/examples/comms/mcp2515/minimal.js +27 -0
  35. package/examples/comms/rda5807m/complete.js +55 -0
  36. package/examples/comms/rda5807m/demo.js +68 -0
  37. package/examples/comms/rda5807m/minimal.js +15 -0
  38. package/examples/comms/rfm9x/complete.js +50 -0
  39. package/examples/comms/rfm9x/demo.js +55 -0
  40. package/examples/comms/rfm9x/minimal.js +20 -0
  41. package/examples/display/pcf8576/complete.js +35 -0
  42. package/examples/display/pcf8576/demo.js +48 -0
  43. package/examples/display/pcf8576/minimal.js +20 -0
  44. package/examples/environmental/aht21/complete.js +32 -0
  45. package/examples/environmental/aht21/demo.js +46 -0
  46. package/examples/environmental/aht21/minimal.js +12 -0
  47. package/examples/environmental/bme280/complete.js +37 -0
  48. package/examples/environmental/bme280/demo.js +67 -0
  49. package/examples/environmental/bme280/minimal.js +20 -0
  50. package/examples/environmental/bme680/complete.js +40 -0
  51. package/examples/environmental/bme680/demo.js +54 -0
  52. package/examples/environmental/bme680/minimal.js +21 -0
  53. package/examples/gas/ens160/complete.js +54 -0
  54. package/examples/gas/ens160/demo.js +54 -0
  55. package/examples/gas/ens160/minimal.js +29 -0
  56. package/examples/gnss/neo6/complete.js +49 -0
  57. package/examples/gnss/neo6/demo.js +54 -0
  58. package/examples/gnss/neo6/minimal.js +30 -0
  59. package/examples/gyroscope/l3g4200d/complete.js +56 -0
  60. package/examples/gyroscope/l3g4200d/demo.js +49 -0
  61. package/examples/gyroscope/l3g4200d/minimal.js +19 -0
  62. package/examples/gyroscope/l3gd20h/complete.js +42 -0
  63. package/examples/gyroscope/l3gd20h/demo.js +29 -0
  64. package/examples/gyroscope/l3gd20h/minimal.js +13 -0
  65. package/examples/humidity/dht11/complete.js +22 -0
  66. package/examples/humidity/dht11/demo.js +38 -0
  67. package/examples/humidity/dht11/minimal.js +18 -0
  68. package/examples/imu/mpu6050/complete.js +58 -0
  69. package/examples/imu/mpu6050/demo.js +37 -0
  70. package/examples/imu/mpu6050/minimal.js +14 -0
  71. package/examples/imu/mpu9250/complete.js +65 -0
  72. package/examples/imu/mpu9250/demo.js +53 -0
  73. package/examples/imu/mpu9250/minimal.js +21 -0
  74. package/examples/imu/mpu9255/complete.js +73 -0
  75. package/examples/imu/mpu9255/demo.js +73 -0
  76. package/examples/imu/mpu9255/minimal.js +21 -0
  77. package/examples/io_expander/mcp23017/complete.js +70 -77
  78. package/examples/io_expander/mcp23017/demo.js +37 -33
  79. package/examples/io_expander/mcp23017/minimal.js +15 -11
  80. package/examples/io_expander/pcf8574/complete.js +60 -65
  81. package/examples/io_expander/pcf8574/demo.js +30 -25
  82. package/examples/io_expander/pcf8574/minimal.js +15 -11
  83. package/examples/io_expander/pcf8575/complete.js +24 -20
  84. package/examples/io_expander/pcf8575/demo.js +27 -23
  85. package/examples/io_expander/pcf8575/minimal.js +16 -12
  86. package/examples/io_expander/tpic6b595/complete.js +57 -0
  87. package/examples/io_expander/tpic6b595/demo.js +75 -0
  88. package/examples/io_expander/tpic6b595/minimal.js +26 -0
  89. package/examples/led/apa102/complete.js +72 -0
  90. package/examples/led/apa102/demo.js +92 -0
  91. package/examples/led/apa102/minimal.js +21 -0
  92. package/examples/led/sk6812rgbw/complete.js +42 -40
  93. package/examples/led/sk6812rgbw/demo.js +16 -16
  94. package/examples/led/sk6812rgbw/minimal.js +15 -17
  95. package/examples/led/ws2812b/complete.js +40 -38
  96. package/examples/led/ws2812b/demo.js +16 -16
  97. package/examples/led/ws2812b/minimal.js +13 -14
  98. package/examples/led/ws2814/complete.js +71 -0
  99. package/examples/led/ws2814/demo.js +82 -0
  100. package/examples/led/ws2814/minimal.js +23 -0
  101. package/examples/light/apds-9930/complete.js +46 -0
  102. package/examples/light/apds-9930/demo.js +31 -0
  103. package/examples/light/apds-9930/minimal.js +20 -0
  104. package/examples/light/apds9960/complete.js +64 -0
  105. package/examples/light/apds9960/demo.js +34 -0
  106. package/examples/light/apds9960/minimal.js +12 -0
  107. package/examples/magnetometer/as5600/complete.js +31 -29
  108. package/examples/magnetometer/as5600/demo.js +35 -33
  109. package/examples/magnetometer/as5600/minimal.js +7 -7
  110. package/examples/magnetometer/hmc5883l/complete.js +47 -0
  111. package/examples/magnetometer/hmc5883l/demo.js +50 -0
  112. package/examples/magnetometer/hmc5883l/minimal.js +15 -0
  113. package/examples/memory/24aa02uid/complete.js +42 -0
  114. package/examples/memory/24aa02uid/demo.js +42 -0
  115. package/examples/memory/24aa02uid/minimal.js +12 -0
  116. package/examples/other/mpr121/complete.js +50 -0
  117. package/examples/other/mpr121/demo.js +30 -0
  118. package/examples/other/mpr121/minimal.js +17 -0
  119. package/examples/power/ade7953/complete.js +44 -0
  120. package/examples/power/ade7953/demo.js +28 -0
  121. package/examples/power/ade7953/minimal.js +15 -0
  122. package/examples/power/ina219/complete.js +20 -18
  123. package/examples/power/ina219/demo.js +15 -13
  124. package/examples/power/ina219/minimal.js +8 -8
  125. package/examples/power/ina226/complete.js +24 -22
  126. package/examples/power/ina226/demo.js +22 -20
  127. package/examples/power/ina226/minimal.js +5 -5
  128. package/examples/power/ina3221/complete.js +51 -49
  129. package/examples/power/ina3221/demo.js +13 -13
  130. package/examples/power/ina3221/minimal.js +8 -8
  131. package/examples/pressure/bmp085/complete.js +29 -0
  132. package/examples/pressure/bmp085/demo.js +34 -0
  133. package/examples/pressure/bmp085/minimal.js +19 -0
  134. package/examples/pressure/bmp180/complete.js +20 -17
  135. package/examples/pressure/bmp180/demo.js +24 -22
  136. package/examples/pressure/bmp180/minimal.js +13 -11
  137. package/examples/pressure/bmp280/complete.js +29 -22
  138. package/examples/pressure/bmp280/demo.js +42 -23
  139. package/examples/pressure/bmp280/minimal.js +13 -11
  140. package/examples/pressure/bmp384/complete.js +29 -0
  141. package/examples/pressure/bmp384/demo.js +40 -0
  142. package/examples/pressure/bmp384/minimal.js +18 -0
  143. package/examples/pressure/bmp581/complete.js +34 -0
  144. package/examples/pressure/bmp581/demo.js +56 -0
  145. package/examples/pressure/bmp581/minimal.js +19 -0
  146. package/examples/pressure/lps22df/complete.js +50 -0
  147. package/examples/pressure/lps22df/demo.js +41 -0
  148. package/examples/pressure/lps22df/minimal.js +19 -0
  149. package/examples/pressure/lps28dfw/complete.js +37 -0
  150. package/examples/pressure/lps28dfw/demo.js +33 -0
  151. package/examples/pressure/lps28dfw/minimal.js +19 -0
  152. package/examples/pressure/lps33hw/complete.js +43 -0
  153. package/examples/pressure/lps33hw/demo.js +43 -0
  154. package/examples/pressure/lps33hw/minimal.js +19 -0
  155. package/examples/rfid/mfrc522/complete.js +44 -0
  156. package/examples/rfid/mfrc522/demo.js +70 -0
  157. package/examples/rfid/mfrc522/minimal.js +17 -0
  158. package/package.json +5 -1
  159. package/src/chips/accelerometer/adxl345.js +482 -0
  160. package/src/chips/accelerometer/adxl362.js +594 -0
  161. package/src/chips/adc_dac/ad7705.js +345 -0
  162. package/src/chips/adc_dac/ad7706.js +331 -0
  163. package/src/chips/adc_dac/hx710a.js +184 -0
  164. package/src/chips/adc_dac/hx710b.js +186 -0
  165. package/src/chips/adc_dac/hx711.js +165 -0
  166. package/src/chips/adc_dac/mcp4725.js +48 -40
  167. package/src/chips/adc_dac/mcp4728.js +297 -0
  168. package/src/chips/adc_dac/pcf8591.js +196 -0
  169. package/src/chips/comms/mcp2515.js +659 -0
  170. package/src/chips/comms/rda5807m.js +393 -0
  171. package/src/chips/comms/rfm9x.js +590 -0
  172. package/src/chips/display/pcf8576.js +232 -0
  173. package/src/chips/environmental/aht21.js +185 -0
  174. package/src/chips/environmental/bme280.js +385 -0
  175. package/src/chips/environmental/bme680.js +512 -0
  176. package/src/chips/gas/ens160.js +298 -0
  177. package/src/chips/gnss/neo6.js +438 -0
  178. package/src/chips/gyroscope/l3g4200d.js +366 -0
  179. package/src/chips/gyroscope/l3gd20h.js +314 -0
  180. package/src/chips/humidity/dht11.js +135 -0
  181. package/src/chips/imu/mpu6050.js +289 -0
  182. package/src/chips/imu/mpu9250.js +394 -0
  183. package/src/chips/imu/mpu9255.js +447 -0
  184. package/src/chips/io_expander/mcp23017.js +240 -157
  185. package/src/chips/io_expander/pcf8574.js +149 -124
  186. package/src/chips/io_expander/pcf8575.js +164 -135
  187. package/src/chips/io_expander/tpic6b595.js +242 -0
  188. package/src/chips/led/_NeoPixelRGBBase.js +204 -0
  189. package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
  190. package/src/chips/led/apa102.js +236 -0
  191. package/src/chips/led/sk6812rgbw.js +15 -149
  192. package/src/chips/led/ws2812b.js +14 -139
  193. package/src/chips/led/ws2814.js +49 -0
  194. package/src/chips/light/apds-9930.js +342 -0
  195. package/src/chips/light/apds9960.js +499 -0
  196. package/src/chips/magnetometer/as5600.js +102 -82
  197. package/src/chips/magnetometer/hmc5883l.js +222 -0
  198. package/src/chips/memory/_24aa02uid.js +182 -0
  199. package/src/chips/other/mpr121.js +375 -0
  200. package/src/chips/power/ade7953.js +427 -0
  201. package/src/chips/power/ina219.js +45 -40
  202. package/src/chips/power/ina226.js +50 -45
  203. package/src/chips/power/ina3221.js +95 -76
  204. package/src/chips/pressure/bmp085.js +251 -0
  205. package/src/chips/pressure/bmp180.js +54 -37
  206. package/src/chips/pressure/bmp280.js +188 -206
  207. package/src/chips/pressure/bmp384.js +410 -0
  208. package/src/chips/pressure/bmp581.js +489 -0
  209. package/src/chips/pressure/lps22df.js +341 -0
  210. package/src/chips/pressure/lps28dfw.js +314 -0
  211. package/src/chips/pressure/lps33hw.js +255 -0
  212. package/src/chips/rfid/mfrc522.js +674 -0
  213. package/src/connection/connection.js +111 -0
  214. package/src/connection/dhtxx.js +138 -0
  215. package/src/connection/dhtxx_mock.js +39 -0
  216. package/src/connection/hx711.js +132 -0
  217. package/src/connection/hx711_mock.js +52 -0
  218. package/src/{transport → connection}/i2c.js +13 -9
  219. package/src/connection/i2c_mock.js +91 -0
  220. package/src/connection/input_pin.js +156 -0
  221. package/src/connection/neo6_mock.js +45 -0
  222. package/src/{transport → connection}/neopixel.js +23 -4
  223. package/src/connection/output_pin.js +41 -0
  224. package/src/connection/sipo.js +159 -0
  225. package/src/{transport → connection}/smbus.js +14 -10
  226. package/src/{transport → connection}/spi.js +12 -8
  227. package/src/connection/spi_mock.js +151 -0
  228. package/src/{transport → connection}/uart.js +33 -26
  229. package/examples/display/.gitkeep +0 -0
  230. package/examples/environmental/.gitkeep +0 -0
  231. package/examples/gnss/.gitkeep +0 -0
  232. package/examples/humidity/.gitkeep +0 -0
  233. package/examples/imu/.gitkeep +0 -0
  234. package/examples/light/.gitkeep +0 -0
  235. package/examples/pressure/.gitkeep +0 -0
  236. package/src/chips/display/.gitkeep +0 -0
  237. package/src/chips/environmental/.gitkeep +0 -0
  238. package/src/chips/gnss/.gitkeep +0 -0
  239. package/src/chips/imu/.gitkeep +0 -0
  240. package/src/chips/light/.gitkeep +0 -0
  241. package/src/chips/pressure/.gitkeep +0 -0
@@ -0,0 +1,82 @@
1
+ 'use strict';
2
+
3
+ const { NeoPixelConnection } = require('../../../src/connection/neopixel');
4
+ const { WS2814Full } = require('../../../src/chips/led/ws2814');
5
+ const { _hsvToRgb } = require('../../../src/chips/led/_color');
6
+
7
+ const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
8
+ const SPI_DEVICE = parseInt(process.env.SPI_DEVICE || '0', 10);
9
+ const N_PIXELS = parseInt(process.env.N_PIXELS || '30', 10);
10
+
11
+ const FRAME_MS = 33; // ~30 fps
12
+ const RAINBOW_MS = 5000;
13
+ const FLASH_MS = 2000;
14
+ const DIM_MS = 2000;
15
+
16
+ const connection = new NeoPixelConnection(SPI_BUS, SPI_DEVICE); // Create NeoPixel connection, (busNumber, deviceNumber)
17
+ const strip = new WS2814Full(connection, N_PIXELS); // Create WS2814 full driver, (connection, n=N_PIXELS pixels)
18
+
19
+ function sleep(ms) {
20
+ return new Promise(r => setTimeout(r, ms));
21
+ }
22
+
23
+ async function main() {
24
+ // --- Rainbow rotation using RGB channels (white=0). Each pixel is
25
+ // assigned a hue offset by its position; the offset advances each
26
+ // frame so the rainbow rotates continuously around the strip.
27
+ // Demonstrates that WS2814's RGBW wire order is identity (R, G, B, W
28
+ // with no reorder), unlike the SK6812RGBW's GRBW order. Runs at
29
+ // ~30 fps for 5 seconds. ---
30
+ let hueOffset = 0;
31
+ const rainbowStart = Date.now();
32
+ let lastPrint = rainbowStart;
33
+ while (Date.now() - rainbowStart < RAINBOW_MS) {
34
+ for (let i = 0; i < N_PIXELS; i++) {
35
+ const h = (hueOffset + i / N_PIXELS) % 1.0;
36
+ const [r, g, b] = _hsvToRgb(h, 1.0, 1.0);
37
+ strip.set_pixel(i, r, g, b, 0); // Set pixel i to rainbow hue (w=0), (index=0–n-1, r=0–255, g=0–255, b=0–255, w=0–255) → void
38
+ }
39
+ await strip.show(); // Transmit buffer to strip, () → void
40
+ hueOffset = (hueOffset + 1.0 / (N_PIXELS * 2)) % 1.0;
41
+ const now = Date.now();
42
+ if (now - lastPrint >= 1000) {
43
+ console.log(`mode=rainbow brightness=${strip.brightness}`);
44
+ lastPrint = now;
45
+ }
46
+ await sleep(FRAME_MS);
47
+ }
48
+
49
+ // --- Warm white at full brightness for 2 seconds. r=255, g=200, b=150,
50
+ // w=255 blends the dedicated white element with amber-tinted RGB,
51
+ // exercising the white channel and the 32-bit RGBW pixel word at
52
+ // full brightness. ---
53
+ await strip.fill(255, 200, 150, 255); // Fill all pixels warm white, (r=0–255, g=0–255, b=0–255, w=0–255) → void
54
+ const flashStart = Date.now();
55
+ while (Date.now() - flashStart < FLASH_MS) {
56
+ console.log(`mode=warm-white brightness=${strip.brightness}`);
57
+ await sleep(100);
58
+ }
59
+
60
+ // --- Dim warm white to 50% using the brightness property and hold for
61
+ // 2 seconds. Demonstrates that brightness scaling is non-destructive:
62
+ // the stored RGBW values are unchanged, only the scale factor applied
63
+ // at show() time changes. ---
64
+ strip.brightness = 128; // Set global brightness, (value=0–255)
65
+ await strip.show(); // Transmit buffer to strip, () → void
66
+ const dimStart = Date.now();
67
+ while (Date.now() - dimStart < DIM_MS) {
68
+ console.log(`mode=warm-white-dimmed brightness=${strip.brightness}`);
69
+ await sleep(100);
70
+ }
71
+
72
+ // --- Cycle to cool white at full brightness. r=200, g=210, b=255,
73
+ // w=255 shifts the blend toward blue, showcasing the dedicated
74
+ // white element paired with a cool-tinted RGB base. ---
75
+ strip.brightness = 255; // Set global brightness, (value=0–255)
76
+ await strip.fill(200, 210, 255, 255); // Fill all pixels cool white, (r=0–255, g=0–255, b=0–255, w=0–255) → void
77
+ console.log(`mode=cool-white brightness=${strip.brightness}`);
78
+
79
+ await connection.close();
80
+ }
81
+
82
+ main();
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ const { NeoPixelConnection } = require('../../../src/connection/neopixel');
4
+ const { WS2814Minimal } = require('../../../src/chips/led/ws2814');
5
+
6
+ const SPI_BUS = parseInt(process.env.SPI_BUS || '0', 10);
7
+ const SPI_DEVICE = parseInt(process.env.SPI_DEVICE || '0', 10);
8
+
9
+ const connection = new NeoPixelConnection(SPI_BUS, SPI_DEVICE); // Create NeoPixel connection, (busNumber, deviceNumber)
10
+ const strip = new WS2814Minimal(connection, 30); // Create WS2814 driver, (connection, n=30 pixels)
11
+
12
+ (async () => {
13
+ await strip.fill(255, 0, 0); // Fill all pixels red, (r=0–255, g=0–255, b=0–255, w=0–255) → void
14
+ await new Promise(r => setTimeout(r, 1000));
15
+ await strip.fill(0, 255, 0); // Fill all pixels green, (r=0–255, g=0–255, b=0–255, w=0–255) → void
16
+ await new Promise(r => setTimeout(r, 1000));
17
+ await strip.fill(0, 0, 255); // Fill all pixels blue, (r=0–255, g=0–255, b=0–255, w=0–255) → void
18
+ await new Promise(r => setTimeout(r, 1000));
19
+ await strip.fill(0, 0, 0, 255); // Fill all pixels white (W channel), (r=0–255, g=0–255, b=0–255, w=0–255) → void
20
+ await new Promise(r => setTimeout(r, 1000));
21
+ await strip.off(); // Turn off all pixels, () → void
22
+ await connection.close();
23
+ })();
@@ -0,0 +1,46 @@
1
+ // Complete example for the APDS-9930 — exercises every Full-class method.
2
+
3
+ const { APDS9930Full } = require('periph/src/chips/light/apds-9930');
4
+ const { I2CConnection } = require('periph/src/connection/i2c_auto');
5
+
6
+ async function main() {
7
+ const connection = new I2CConnection(0x39); // Create I2C connection, (addr=0x39, bus=undefined) → I2CConnection
8
+ const apds = new APDS9930Full(connection); // Construct APDS-9930 Full, (connection) → APDS9930Full
9
+ // exposes ALS and proximity configuration methods
10
+
11
+ await new Promise(r => setTimeout(r, 110));
12
+
13
+ await apds.configureAls(0xDB, 0, false); // Configure ALS, (atime=0xDB, again=0, agl=false) → Promise<void>
14
+ // sets ALS integration time to 101 ms with 1x gain
15
+ await apds.configureProximity(8, 0, 0, false, 0xFF); // Configure proximity, (ppulse=8, pgain=0, pdrive=0, pdl=false, ptime=0xFF) → Promise<void>
16
+ // 8 LED pulses at 100 mA, 1x gain, no reduced drive
17
+ await apds.disableWait(); // Disable wait timer, () → Promise<void>
18
+ // clears WEN in ENABLE
19
+ await apds.setAlsThresholds(100, 60000, 1); // Set ALS thresholds, (low=100, high=60000, persistence=1) → Promise<void>
20
+ // fires after 1 consecutive out-of-range Ch0 count
21
+ await apds.setProximityThresholds(10, 200, 1); // Set proximity thresholds, (low=10, high=200, persistence=1) → Promise<void>
22
+ // fires on a single proximity reading outside [10, 200]
23
+ await apds.setProximityOffset(0); // Set proximity offset, (offset=0) → Promise<void>
24
+ // clears any prior offset
25
+ await apds.sleepAfterInterrupt(false); // Configure SAI, (enable=false) → Promise<void>
26
+ // chip stays in normal operation after an interrupt
27
+
28
+ for (let i = 0; i < 10; i++) {
29
+ await new Promise(r => setTimeout(r, 110));
30
+ const lx = await apds.lux(); // Read ambient illuminance, () → Promise<number> lx
31
+ // combines Ch0 and Ch1 with IR-compensation coefficients
32
+ const p = await apds.proximity(); // Read proximity count, () → Promise<number> count
33
+ // 16-bit ADC value
34
+ const c0 = await apds.ch0(); // Read Ch0 raw, () → Promise<number> count
35
+ // 16-bit ADC value of visible + IR channel
36
+ const c1 = await apds.ch1(); // Read Ch1 raw, () → Promise<number> count
37
+ // 16-bit ADC value of IR-only channel
38
+ const st = await apds.status(); // Read STATUS decoded, () → Promise<object>
39
+ // {avalid, pvalid, psat, aint, pint} booleans
40
+ console.log(`lux=${lx.toFixed(1)} prox=${p} ch0=${c0} ch1=${c1} status=${JSON.stringify(st)}`);
41
+ }
42
+ await apds.clearInterrupt('both'); // Clear interrupts, (channel='both') → Promise<void>
43
+ // issues special-function command 0xE7 to clear both ALS and proximity INT flags
44
+ }
45
+
46
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,31 @@
1
+ // Demo for the APDS-9930 — adaptive backlight + screen-lock scenario.
2
+
3
+ const { APDS9930Full } = require('periph/src/chips/light/apds-9930');
4
+ const { I2CConnection } = require('periph/src/connection/i2c_auto');
5
+
6
+ const DIM_LUX_THRESHOLD = 10.0;
7
+ const PROX_SCREEN_OFF = 400;
8
+
9
+ async function main() {
10
+ // --- Configure for adaptive backlight + screen-lock monitoring ---
11
+ // Defaults (ATIME=0xDB, 1x AGAIN, 8-pulse proximity, 100 mA drive)
12
+ // already give stable readings; we just print lux and proximity.
13
+ const connection = new I2CConnection(0x39); // Create I2C connection, (addr=0x39, bus=undefined) → I2CConnection
14
+ const apds = new APDS9930Full(connection); // Construct APDS-9930 Full, (connection) → APDS9930Full
15
+ // default 101 ms ALS integration, 8-pulse proximity, 100 mA drive
16
+
17
+ await new Promise(r => setTimeout(r, 110));
18
+ // --- Sample lux and proximity once per second for 30 cycles ---
19
+ for (let i = 0; i < 30; i++) {
20
+ await new Promise(r => setTimeout(r, 1000));
21
+ const lx = await apds.lux(); // Read ambient illuminance, () → Promise<number> lx
22
+ // IR-compensated lux via Ch0/Ch1 difference
23
+ const p = await apds.proximity(); // Read proximity count, () → Promise<number> count
24
+ // 16-bit ADC value; higher = closer
25
+ console.log(`lux=${lx.toFixed(1)} lx proximity=${p}`);
26
+ if (lx < DIM_LUX_THRESHOLD) console.log(' -> dim backlight');
27
+ if (p > PROX_SCREEN_OFF) console.log(' -> disable screen');
28
+ }
29
+ }
30
+
31
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,20 @@
1
+ // Minimal example for the APDS-9930 ambient light + proximity sensor.
2
+
3
+ const { APDS9930Minimal } = require('periph/src/chips/light/apds-9930');
4
+ const { I2CConnection } = require('periph/src/connection/i2c_auto');
5
+
6
+ async function main() {
7
+ const connection = new I2CConnection(0x39); // Create I2C connection, (addr=0x39, bus=undefined) → I2CConnection
8
+ const apds = new APDS9930Minimal(connection); // Construct APDS-9930 Minimal, (connection) → APDS9930Minimal
9
+ // initialises with ATIME=0xDB, PTIME=0xFF, PPULSE=8, CONTROL=0x20
10
+ await new Promise(r => setTimeout(r, 110));
11
+ setInterval(async () => {
12
+ const lx = await apds.lux(); // Read ambient illuminance, () → Promise<number> lx
13
+ // IR-compensated lux via Ch0/Ch1 difference
14
+ const p = await apds.proximity(); // Read proximity count, () → Promise<number> count
15
+ // 16-bit ADC value; higher = closer object
16
+ console.log(`lux=${lx.toFixed(1)} proximity=${p}`);
17
+ }, 1000);
18
+ }
19
+
20
+ main().catch(err => { console.error(err); process.exit(1); });
@@ -0,0 +1,64 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { APDS9960Full } = require('../../../src/chips/light/apds9960');
5
+
6
+ const connection = new I2CConnection(1, 0x39);
7
+ const apds = new APDS9960Full(connection); // Create APDS9960 driver, (connection) → APDS9960Full
8
+
9
+ (async () => {
10
+ console.log('0x' + (await apds.chipId()).toString(16)); // Read device ID, () → number
11
+
12
+ const { clear, red, green, blue } = await apds.color(); // Read all RGBC channels, () → { clear, red, green, blue }
13
+ // burst read 0x94-0x9B latches all channels atomically
14
+ console.log(await apds.colorClear()); // Read clear channel, () → number 0-65535
15
+ console.log(await apds.colorRed()); // Read red channel, () → number 0-65535
16
+ console.log(await apds.colorGreen()); // Read green channel, () → number 0-65535
17
+ console.log(await apds.colorBlue()); // Read blue channel, () → number 0-65535
18
+
19
+ await apds.configureAls(0xB6, 1); // Configure ALS, (atime 0-255, again 0-3) → void
20
+ // sets integration time and gain for the ALS/color engine
21
+ await apds.configureWait(0xFF, false); // Configure wait, (wtime 0-255, wlong=false) → void
22
+ // sets idle period between measurement cycles
23
+ await apds.enableWait(true); // Enable wait engine, (enabled) → void
24
+
25
+ await apds.enableProximity(true); // Enable proximity engine, (enabled) → void
26
+ await apds.configureProximityLed(0, 0, 0, 1); // Configure proximity LED, (ldrive 0-3, pgain 0-3, ppulse 0-63, pplen 0-3) → void
27
+ // sets LED drive strength, gain, pulse count and length
28
+ await apds.setLedBoost(0); // Set LED boost, (boost 0-3) → void
29
+ // multiplies LED current: 0=100%, 1=150%, 2=200%, 3=300%
30
+ console.log(await apds.proximity()); // Read proximity count, () → number 0-255
31
+
32
+ await apds.alsThreshold(100, 60000); // Set ALS thresholds, (low 0-65535, high 0-65535) → void
33
+ await apds.proximityThreshold(10, 200); // Set proximity thresholds, (low 0-255, high 0-255) → void
34
+ await apds.setPersistence(0, 1); // Set persistence, (ppers 0-15, apers 0-15) → void
35
+
36
+ await apds.enableAlsInterrupt(true); // Enable ALS interrupt, (enabled) → void
37
+ await apds.enableProximityInterrupt(true); // Enable proximity interrupt, (enabled) → void
38
+ await apds.clearAlsInterrupt(); // Clear ALS interrupt, () → void
39
+ await apds.clearProximityInterrupt(); // Clear proximity interrupt, () → void
40
+ await apds.clearAllInterrupts(); // Clear all interrupts, () → void
41
+
42
+ await apds.setProximityOffset(10, -5); // Set proximity offset, (ur -127..127, dl -127..127) → void
43
+ // sign-magnitude encoding compensates for optical crosstalk
44
+ await apds.setProximityMask(false, false, false, false); // Set proximity mask, (u, d, l, r) → void
45
+
46
+ await apds.enableGesture(true); // Enable gesture engine, (enabled) → void
47
+ await apds.configureGesture(1, 0, 0, 1, 1, 50, 20); // Configure gesture, (ggain, gldrive, gpulse, gplen, gwtime, gpenth, gexth) → void
48
+ // sets gain, LED drive, pulse, wait time, entry/exit thresholds
49
+ console.log(await apds.gestureAvailable()); // Check gesture data, () → boolean
50
+ console.log(await apds.gestureFifoLevel()); // Read FIFO level, () → number
51
+ console.log(await apds.readGestureFifo()); // Read gesture FIFO, () → Array<{u,d,l,r}>
52
+ await apds.clearGestureFifo(); // Clear gesture FIFO, () → void
53
+ await apds.enableGestureInterrupt(false); // Enable gesture interrupt, (enabled) → void
54
+ await apds.enableGesture(false); // Disable gesture engine, (enabled) → void
55
+
56
+ console.log(await apds.status()); // Read STATUS register, () → number
57
+ console.log(await apds.isAlsValid()); // Check ALS data valid, () → boolean
58
+ console.log(await apds.isProximityValid()); // Check proximity valid, () → boolean
59
+ console.log(await apds.isAlsSaturated()); // Check ALS saturated, () → boolean
60
+ console.log(await apds.isProximitySaturated()); // Check proximity saturated, () → boolean
61
+
62
+ await apds.enableProximity(false);
63
+ await connection.close();
64
+ })();
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { APDS9960Full } = require('../../../src/chips/light/apds9960');
5
+
6
+ const connection = new I2CConnection(1, 0x39);
7
+ const apds = new APDS9960Full(connection); // Create APDS9960 driver, (connection) → APDS9960Full
8
+
9
+ // --- Monitor ambient light with adaptive integration time ---
10
+ // Start with the default 200 ms integration (ATIME=0xB6). When the clear
11
+ // channel approaches saturation (>90% of max count), halve the integration
12
+ // time by doubling ATIME to prevent overflow.
13
+ let atime = 0xB6;
14
+ (async () => {
15
+ await apds.configureAls(atime, 1); // Configure ALS, (atime 0-255, again 0-3) → void
16
+ })();
17
+
18
+ setInterval(async () => {
19
+ while (!(await apds.isAlsValid())) {} // Check ALS data valid, () → boolean
20
+
21
+ const { clear, red, green, blue } = await apds.color(); // Read all RGBC channels, () → { clear, red, green, blue }
22
+ const lux = -0.32466 * red + 1.57837 * green + -0.73191 * blue;
23
+ console.log(`C=${clear} R=${red} G=${green} B=${blue} lux~${lux.toFixed(0)}`);
24
+
25
+ // --- Adaptive integration: reduce time when saturated ---
26
+ // At saturation the sensor clips; shortening integration recovers
27
+ // headroom at the cost of reduced sensitivity in low light.
28
+ if ((await apds.isAlsSaturated()) && atime < 0xFE) { // Check ALS saturated, () → boolean
29
+ atime = atime + Math.floor((256 - atime) / 2);
30
+ if (atime > 0xFE) atime = 0xFE;
31
+ await apds.configureAls(atime, 1); // Configure ALS, (atime 0-255, again 0-3) → void
32
+ console.log(`[SATURATED — reducing integration time, ATIME=0x${atime.toString(16)}]`);
33
+ }
34
+ }, 1000);
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { APDS9960Minimal } = require('../../../src/chips/light/apds9960');
5
+
6
+ const connection = new I2CConnection(1, 0x39);
7
+ const apds = new APDS9960Minimal(connection); // Create APDS9960 driver, (connection) → APDS9960Minimal
8
+
9
+ setInterval(async () => {
10
+ const { clear, red, green, blue } = await apds.color(); // Read all RGBC channels, () → { clear, red, green, blue }
11
+ console.log(`C=${clear} R=${red} G=${green} B=${blue}`);
12
+ }, 1000);
@@ -1,43 +1,45 @@
1
1
  'use strict';
2
2
 
3
- const { I2CTransport } = require('../../src/transport/i2c');
4
- const { AS5600Full } = require('../../src/chips/magnetometer/as5600');
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { AS5600Full } = require('../../../src/chips/magnetometer/as5600');
5
5
 
6
6
  const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
7
  const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x36', 16);
8
8
 
9
- const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
10
- const as5600 = new AS5600Full(transport); // Create AS5600 driver, (transport) → AS5600Full
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const as5600 = new AS5600Full(connection); // Create AS5600 driver, (connection) → AS5600Full
11
11
 
12
- // --- Status and magnet checks ---
13
- console.log(as5600.isMagnetDetected()); // Check magnet present, () → bool
14
- console.log(as5600.isMagnetTooStrong()); // Check magnet too strong, () → bool
15
- console.log(as5600.isMagnetTooWeak()); // Check magnet too weak, () → bool
16
- console.log(as5600.statusByte().toString(16)); // Read raw status, () → int
12
+ (async () => {
13
+ // --- Status and magnet checks ---
14
+ console.log(await as5600.isMagnetDetected()); // Check magnet present, () → bool
15
+ console.log(await as5600.isMagnetTooStrong()); // Check magnet too strong, () → bool
16
+ console.log(await as5600.isMagnetTooWeak()); // Check magnet too weak, () → bool
17
+ console.log((await as5600.statusByte()).toString(16)); // Read raw status, () → int
17
18
 
18
- // --- Angle readings ---
19
- console.log(as5600.angle()); // Read absolute angle, () → float degrees
20
- console.log(as5600.angleRaw()); // Read scaled angle count, () → int 0-4095
21
- console.log(as5600.rawAngle()); // Read raw unscaled angle, () → int 0-4095
22
- console.log(as5600.rawAngleDegrees()); // Read raw angle in degrees, () → float degrees
19
+ // --- Angle readings ---
20
+ console.log(await as5600.angle()); // Read absolute angle, () → float degrees
21
+ console.log(await as5600.angleRaw()); // Read scaled angle count, () → int 0-4095
22
+ console.log(await as5600.rawAngle()); // Read raw unscaled angle, () → int 0-4095
23
+ console.log(await as5600.rawAngleDegrees()); // Read raw angle in degrees, () → float degrees
23
24
 
24
- // --- Diagnostics ---
25
- console.log(as5600.agc()); // Read AGC value, () → int
26
- console.log(as5600.magnitude()); // Read CORDIC magnitude, () → int
25
+ // --- Diagnostics ---
26
+ console.log(await as5600.agc()); // Read AGC value, () → int
27
+ console.log(await as5600.magnitude()); // Read CORDIC magnitude, () → int
27
28
 
28
- // --- Position configuration (volatile) ---
29
- console.log(as5600.zeroPosition()); // Read ZPOS, () → int 0-4095
30
- console.log(as5600.maxPosition()); // Read MPOS, () → int 0-4095
31
- console.log(as5600.maxAngle()); // Read MANG, () → int 0-4095
29
+ // --- Position configuration (volatile) ---
30
+ console.log(await as5600.zeroPosition()); // Read ZPOS, () → int 0-4095
31
+ console.log(await as5600.maxPosition()); // Read MPOS, () → int 0-4095
32
+ console.log(await as5600.maxAngle()); // Read MANG, () → int 0-4095
32
33
 
33
- as5600.setZeroPosition(0); // Set zero position, (pos 0-4095) → None
34
- as5600.setMaxPosition(4095); // Set max position, (pos 0-4095) → None
35
- as5600.setMaxAngle(2048); // Set max angle span, (span 0-4095) → None
34
+ await as5600.setZeroPosition(0); // Set zero position, (pos 0-4095) → None
35
+ await as5600.setMaxPosition(4095); // Set max position, (pos 0-4095) → None
36
+ await as5600.setMaxAngle(2048); // Set max angle span, (span 0-4095) → None
36
37
 
37
- // --- Configure power mode and output ---
38
- as5600.configure(0, 0, 0, 0, 0, 0, false); // Configure chip, (pm 0-3, hyst 0-3, outs 0-2, pwmf 0-3, sf 0-3, fth 0-7, wd bool) → None
38
+ // --- Configure power mode and output ---
39
+ await as5600.configure(0, 0, 0, 0, 0, 0, false); // Configure chip, (pm 0-3, hyst 0-3, outs 0-2, pwmf 0-3, sf 0-3, fth 0-7, wd bool) → None
39
40
 
40
- // --- Burn count ---
41
- console.log(as5600.burnCount()); // Read burn count, () → int 0-3
41
+ // --- Burn count ---
42
+ console.log(await as5600.burnCount()); // Read burn count, () → int 0-3
42
43
 
43
- transport.close();
44
+ await connection.close();
45
+ })();
@@ -1,49 +1,51 @@
1
1
  'use strict';
2
2
 
3
- const { I2CTransport } = require('../../src/transport/i2c');
4
- const { AS5600Full } = require('../../src/chips/magnetometer/as5600');
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { AS5600Full } = require('../../../src/chips/magnetometer/as5600');
5
5
 
6
6
  const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
7
  const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x36', 16);
8
8
 
9
- const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
10
- const as5600 = new AS5600Full(transport);
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const as5600 = new AS5600Full(connection);
11
11
 
12
12
  // --- Motor feedback monitor: read angle 10 times per second ---
13
13
  // AGC monitoring detects magnet distance drift; status changes alert to magnet removal.
14
14
  // In 5 V mode, target AGC ≈ 128; in 3.3 V mode, target AGC ≈ 64.
15
15
 
16
- let prevStatus = as5600.statusByte();
17
-
18
- for (let n = 0; n < 10; n++) {
19
- const a = as5600.angle(); // Read absolute angle, () → float degrees
20
- const r = as5600.rawAngle(); // Read raw unscaled angle, () → int 0-4095
21
- const g = as5600.agc(); // Read AGC value, () → int
22
-
23
- // --- Check for status changes (magnet inserted/removed) ---
24
- const status = as5600.statusByte();
25
- if (status !== prevStatus) {
26
- if (!as5600.isMagnetDetected()) {
27
- console.log('[MAGNET REMOVED] MD=0');
28
- } else {
29
- console.log('[MAGNET DETECTED] MD=1 MH=%d ML=%d',
30
- as5600.isMagnetTooStrong(), as5600.isMagnetTooWeak());
16
+ (async () => {
17
+ let prevStatus = await as5600.statusByte();
18
+
19
+ for (let n = 0; n < 10; n++) {
20
+ const a = await as5600.angle(); // Read absolute angle, () → float degrees
21
+ const r = await as5600.rawAngle(); // Read raw unscaled angle, () → int 0-4095
22
+ const g = await as5600.agc(); // Read AGC value, () → int
23
+
24
+ // --- Check for status changes (magnet inserted/removed) ---
25
+ const status = await as5600.statusByte();
26
+ if (status !== prevStatus) {
27
+ if (!(await as5600.isMagnetDetected())) {
28
+ console.log('[MAGNET REMOVED] MD=0');
29
+ } else {
30
+ console.log('[MAGNET DETECTED] MD=1 MH=%d ML=%d',
31
+ await as5600.isMagnetTooStrong(), await as5600.isMagnetTooWeak());
32
+ }
33
+ prevStatus = status;
31
34
  }
32
- prevStatus = status;
33
- }
34
35
 
35
- // --- AGC health check ---
36
- if (as5600.isMagnetDetected()) {
37
- let tag = '[OK]';
38
- if (g < 64 || g > 192) {
39
- tag = '[AGC low — magnet weak or too far]';
36
+ // --- AGC health check ---
37
+ if (await as5600.isMagnetDetected()) {
38
+ let tag = '[OK]';
39
+ if (g < 64 || g > 192) {
40
+ tag = '[AGC low — magnet weak or too far]';
41
+ }
42
+ console.log('angle=%.2f° raw=%d agc=%d %s', a, r, g, tag);
40
43
  }
41
- console.log('angle=%.2f° raw=%d agc=%d %s', a, r, g, tag);
42
- }
43
44
 
44
- // 100 ms synchronous delay
45
- const end = Date.now() + 100;
46
- while (Date.now() < end) {}
47
- }
45
+ // 100 ms synchronous delay
46
+ const end = Date.now() + 100;
47
+ while (Date.now() < end) {}
48
+ }
48
49
 
49
- transport.close();
50
+ await connection.close();
51
+ })();
@@ -1,16 +1,16 @@
1
1
  'use strict';
2
2
 
3
- const { I2CTransport } = require('../../src/transport/i2c');
4
- const { AS5600Minimal } = require('../../src/chips/magnetometer/as5600');
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { AS5600Minimal } = require('../../../src/chips/magnetometer/as5600');
5
5
 
6
6
  const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
7
  const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x36', 16);
8
8
 
9
- const transport = new I2CTransport(I2C_BUS, I2C_ADDR);
10
- const as5600 = new AS5600Minimal(transport); // Create AS5600 driver, (transport) → AS5600Minimal
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const as5600 = new AS5600Minimal(connection); // Create AS5600 driver, (connection) → AS5600Minimal
11
11
 
12
- setInterval(() => {
13
- const a = as5600.angle(); // Read absolute angle, () → float degrees
14
- const r = as5600.angleRaw(); // Read scaled angle count, () → int 0-4095
12
+ setInterval(async () => {
13
+ const a = await as5600.angle(); // Read absolute angle, () → float degrees
14
+ const r = await as5600.angleRaw(); // Read scaled angle count, () → int 0-4095
15
15
  console.log('angle=%.2f° raw=%d', a, r);
16
16
  }, 1000);
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HMC5883LFull } = require('../../../src/chips/magnetometer/hmc5883l');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x1E', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const hmc5883l = new HMC5883LFull(connection); // Create HMC5883L driver, (connection) → HMC5883LFull
11
+
12
+ async function main() {
13
+ // --- Identification ---
14
+ const [idA, idB, idC] = await hmc5883l.identify(); // Read ID registers, () → (int, int, int)
15
+ console.log('ID: 0x%s 0x%s 0x%s', idA.toString(16), idB.toString(16), idC.toString(16));
16
+
17
+ // --- Status ---
18
+ console.log('Status: 0x%s', (await hmc5883l.status()).toString(16)); // Read raw status, () → int
19
+ console.log('Data ready:', await hmc5883l.dataReady()); // Check data ready, () → bool
20
+
21
+ // --- Magnetic field readings ---
22
+ const { x, y, z } = await hmc5883l.magneticField(); // Read magnetic field, () → { x: float T, y: float T, z: float T }
23
+ console.log('X=%.6f T Y=%.6f T Z=%.6f T', x, y, z);
24
+
25
+ // --- Configuration ---
26
+ await hmc5883l.configure(15, 8, 1); // Configure chip, (odr 0.75-75 Hz, averaging 1/2/4/8, gain 0-7) → None
27
+ // writes Config A and B registers
28
+ await hmc5883l.setGain(2); // Set gain, (gain 0-7) → None
29
+ // updates GN bits in Config B
30
+ await hmc5883l.setMode('single'); // Set operating mode, ('continuous'|'single'|'idle') → None
31
+ // writes MD bits in Mode Register
32
+
33
+ // --- Single-shot measurement ---
34
+ await new Promise(r => setTimeout(r, 6));
35
+ const single = await hmc5883l.singleMeasurement(); // Single-shot measurement, () → { x: float T, y: float T, z: float T }
36
+ // writes single-measurement mode, waits 6 ms, reads all axes
37
+ console.log('Single: X=%.6f T Y=%.6f T Z=%.6f T', single.x, single.y, single.z);
38
+
39
+ await hmc5883l.setMode('continuous'); // Set operating mode, ('continuous'|'single'|'idle') → None
40
+
41
+ // --- Self-test ---
42
+ const selfTest = await hmc5883l.selfTest(true); // Self-test with positive bias, (positive=bool) → { x: float T, y: float T, z: float T }
43
+ // configures bias, takes measurement, restores normal mode
44
+ console.log('Self-test: X=%.6f T Y=%.6f T Z=%.6f T', selfTest.x, selfTest.y, selfTest.z);
45
+ }
46
+
47
+ main().catch(console.error);
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HMC5883LFull } = require('../../../src/chips/magnetometer/hmc5883l');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x1E', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const hmc5883l = new HMC5883LFull(connection);
11
+
12
+ async function main() {
13
+ // --- Configure for electronic compass ---
14
+ // 8-sample averaging at 15 Hz suppresses noise; ±1.3 Ga gain covers Earth's field (~0.5 Ga).
15
+ await hmc5883l.configure(15, 8, 1); // Configure chip, (odr 0.75-75 Hz, averaging 1/2/4/8, gain 0-7) → None
16
+
17
+ console.log('Electronic compass demo — hold sensor flat, rotate horizontally');
18
+ console.log('Vertical mount warning: |Z| > 30 uT indicates tilt compensation needed');
19
+ console.log('');
20
+
21
+ // --- Sample and compute heading ---
22
+ // User rotates the sensor horizontally; we compute heading from X/Y axes.
23
+ // At n=5, user is prompted to tilt vertically to demonstrate Z-axis detection.
24
+ for (let n = 0; n < 10; n++) {
25
+ while (!(await hmc5883l.dataReady())) { // Check data ready, () → bool
26
+ await new Promise(r => setTimeout(r, 1));
27
+ }
28
+ const { x, y, z } = await hmc5883l.magneticField(); // Read magnetic field, () → { x: float T, y: float T, z: float T }
29
+
30
+ // --- Compute heading from X and Y ---
31
+ if (x !== null && y !== null) {
32
+ const heading = Math.atan2(y, x) * 180 / Math.PI;
33
+ const h = heading < 0 ? heading + 360 : heading;
34
+ console.log('Heading: %.1f°', h);
35
+ }
36
+
37
+ // --- Vertical mount detection ---
38
+ if (z !== null && Math.abs(z) > 30e-6) {
39
+ console.log('[TILT WARNING] Z=%.1f uT — tilt compensation needed', z * 1e6);
40
+ }
41
+
42
+ if (n === 4) {
43
+ console.log('>>> Now tilt sensor vertically <<<');
44
+ }
45
+
46
+ await new Promise(r => setTimeout(r, 500));
47
+ }
48
+ }
49
+
50
+ main().catch(console.error);
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ const { I2CConnection } = require('../../../src/connection/i2c');
4
+ const { HMC5883LMinimal } = require('../../../src/chips/magnetometer/hmc5883l');
5
+
6
+ const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
7
+ const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x1E', 16);
8
+
9
+ const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
10
+ const hmc5883l = new HMC5883LMinimal(connection); // Create HMC5883L driver, (connection) → HMC5883LMinimal
11
+
12
+ setInterval(async () => {
13
+ const { x, y, z } = await hmc5883l.magneticField(); // Read magnetic field, () → { x: float T, y: float T, z: float T }
14
+ console.log('X=%.6f T Y=%.6f T Z=%.6f T', x, y, z);
15
+ }, 1000);