periph 1.1.0 → 1.2.1

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 (266) hide show
  1. package/README.md +61 -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 +7 -7
  21. package/examples/adc_dac/hx711/demo.js +6 -6
  22. package/examples/adc_dac/hx711/minimal.js +7 -7
  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 +24 -22
  27. package/examples/adc_dac/mcp4728/demo.js +22 -22
  28. package/examples/adc_dac/mcp4728/minimal.js +8 -6
  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 +18 -15
  42. package/examples/display/pcf8576/demo.js +6 -6
  43. package/examples/display/pcf8576/minimal.js +12 -10
  44. package/examples/environmental/aht21/complete.js +15 -13
  45. package/examples/environmental/aht21/demo.js +8 -6
  46. package/examples/environmental/aht21/minimal.js +5 -5
  47. package/examples/environmental/bme280/complete.js +23 -20
  48. package/examples/environmental/bme280/demo.js +14 -14
  49. package/examples/environmental/bme280/minimal.js +13 -11
  50. package/examples/environmental/bme680/complete.js +25 -22
  51. package/examples/environmental/bme680/demo.js +38 -36
  52. package/examples/environmental/bme680/minimal.js +14 -12
  53. package/examples/gas/ens160/complete.js +29 -27
  54. package/examples/gas/ens160/demo.js +39 -37
  55. package/examples/gas/ens160/minimal.js +17 -15
  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 +27 -25
  69. package/examples/imu/mpu6050/demo.js +29 -26
  70. package/examples/imu/mpu6050/minimal.js +6 -6
  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 +45 -43
  105. package/examples/light/apds9960/demo.js +11 -9
  106. package/examples/light/apds9960/minimal.js +5 -5
  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 +23 -21
  114. package/examples/memory/24aa02uid/demo.js +32 -30
  115. package/examples/memory/24aa02uid/minimal.js +5 -5
  116. package/examples/motor/drv8830/complete.js +50 -0
  117. package/examples/motor/drv8830/demo.js +57 -0
  118. package/examples/motor/drv8830/minimal.js +23 -0
  119. package/examples/other/mpr121/complete.js +50 -0
  120. package/examples/other/mpr121/demo.js +30 -0
  121. package/examples/other/mpr121/minimal.js +17 -0
  122. package/examples/power/ade7953/complete.js +44 -0
  123. package/examples/power/ade7953/demo.js +28 -0
  124. package/examples/power/ade7953/minimal.js +15 -0
  125. package/examples/power/ina219/complete.js +20 -18
  126. package/examples/power/ina219/demo.js +15 -13
  127. package/examples/power/ina219/minimal.js +8 -8
  128. package/examples/power/ina226/complete.js +24 -22
  129. package/examples/power/ina226/demo.js +22 -20
  130. package/examples/power/ina226/minimal.js +5 -5
  131. package/examples/power/ina3221/complete.js +51 -49
  132. package/examples/power/ina3221/demo.js +13 -13
  133. package/examples/power/ina3221/minimal.js +8 -8
  134. package/examples/pressure/bmp085/complete.js +29 -0
  135. package/examples/pressure/bmp085/demo.js +34 -0
  136. package/examples/pressure/bmp085/minimal.js +19 -0
  137. package/examples/pressure/bmp180/complete.js +20 -17
  138. package/examples/pressure/bmp180/demo.js +24 -22
  139. package/examples/pressure/bmp180/minimal.js +13 -11
  140. package/examples/pressure/bmp280/complete.js +23 -20
  141. package/examples/pressure/bmp280/demo.js +44 -41
  142. package/examples/pressure/bmp280/minimal.js +13 -11
  143. package/examples/pressure/bmp384/complete.js +29 -0
  144. package/examples/pressure/bmp384/demo.js +40 -0
  145. package/examples/pressure/bmp384/minimal.js +18 -0
  146. package/examples/pressure/bmp581/complete.js +34 -0
  147. package/examples/pressure/bmp581/demo.js +56 -0
  148. package/examples/pressure/bmp581/minimal.js +19 -0
  149. package/examples/pressure/lps22df/complete.js +50 -0
  150. package/examples/pressure/lps22df/demo.js +41 -0
  151. package/examples/pressure/lps22df/minimal.js +19 -0
  152. package/examples/pressure/lps28dfw/complete.js +37 -0
  153. package/examples/pressure/lps28dfw/demo.js +33 -0
  154. package/examples/pressure/lps28dfw/minimal.js +19 -0
  155. package/examples/pressure/lps33hw/complete.js +43 -0
  156. package/examples/pressure/lps33hw/demo.js +43 -0
  157. package/examples/pressure/lps33hw/minimal.js +19 -0
  158. package/examples/rfid/mfrc522/complete.js +44 -0
  159. package/examples/rfid/mfrc522/demo.js +70 -0
  160. package/examples/rfid/mfrc522/minimal.js +17 -0
  161. package/examples/rtc/ds3231/complete.js +77 -0
  162. package/examples/rtc/ds3231/demo.js +60 -0
  163. package/examples/rtc/ds3231/minimal.js +23 -0
  164. package/examples/rtc/pcf8523/complete.js +66 -0
  165. package/examples/rtc/pcf8523/demo.js +65 -0
  166. package/examples/rtc/pcf8523/minimal.js +20 -0
  167. package/examples/temperature/mcp9808/complete.js +89 -0
  168. package/examples/temperature/mcp9808/demo.js +62 -0
  169. package/examples/temperature/mcp9808/minimal.js +21 -0
  170. package/examples/temperature/tmp117/complete.js +90 -0
  171. package/examples/temperature/tmp117/demo.js +73 -0
  172. package/examples/temperature/tmp117/minimal.js +21 -0
  173. package/examples/tof/vl53l0x/complete.js +118 -0
  174. package/examples/tof/vl53l0x/demo.js +83 -0
  175. package/examples/tof/vl53l0x/minimal.js +25 -0
  176. package/examples/tof/vl53l1x/complete.js +151 -0
  177. package/examples/tof/vl53l1x/demo.js +100 -0
  178. package/examples/tof/vl53l1x/minimal.js +25 -0
  179. package/package.json +5 -1
  180. package/src/chips/accelerometer/adxl345.js +482 -0
  181. package/src/chips/accelerometer/adxl362.js +594 -0
  182. package/src/chips/adc_dac/ad7705.js +345 -0
  183. package/src/chips/adc_dac/ad7706.js +331 -0
  184. package/src/chips/adc_dac/hx710a.js +184 -0
  185. package/src/chips/adc_dac/hx710b.js +186 -0
  186. package/src/chips/adc_dac/hx711.js +15 -15
  187. package/src/chips/adc_dac/mcp4725.js +48 -40
  188. package/src/chips/adc_dac/mcp4728.js +57 -44
  189. package/src/chips/adc_dac/pcf8591.js +196 -0
  190. package/src/chips/comms/mcp2515.js +659 -0
  191. package/src/chips/comms/rda5807m.js +393 -0
  192. package/src/chips/comms/rfm9x.js +590 -0
  193. package/src/chips/display/pcf8576.js +42 -33
  194. package/src/chips/environmental/aht21.js +52 -37
  195. package/src/chips/environmental/bme280.js +85 -66
  196. package/src/chips/environmental/bme680.js +110 -87
  197. package/src/chips/gas/ens160.js +82 -64
  198. package/src/chips/gnss/neo6.js +438 -0
  199. package/src/chips/gyroscope/l3g4200d.js +366 -0
  200. package/src/chips/gyroscope/l3gd20h.js +314 -0
  201. package/src/chips/humidity/dht11.js +135 -0
  202. package/src/chips/imu/mpu6050.js +89 -66
  203. package/src/chips/imu/mpu9250.js +394 -0
  204. package/src/chips/imu/mpu9255.js +447 -0
  205. package/src/chips/io_expander/mcp23017.js +240 -157
  206. package/src/chips/io_expander/pcf8574.js +149 -124
  207. package/src/chips/io_expander/pcf8575.js +164 -135
  208. package/src/chips/io_expander/tpic6b595.js +242 -0
  209. package/src/chips/led/_NeoPixelRGBBase.js +204 -0
  210. package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
  211. package/src/chips/led/apa102.js +236 -0
  212. package/src/chips/led/sk6812rgbw.js +15 -149
  213. package/src/chips/led/ws2812b.js +14 -139
  214. package/src/chips/led/ws2814.js +49 -0
  215. package/src/chips/light/apds-9930.js +342 -0
  216. package/src/chips/light/apds9960.js +158 -123
  217. package/src/chips/magnetometer/as5600.js +102 -82
  218. package/src/chips/magnetometer/hmc5883l.js +222 -0
  219. package/src/chips/memory/_24aa02uid.js +32 -29
  220. package/src/chips/motor/drv8830.js +239 -0
  221. package/src/chips/other/mpr121.js +375 -0
  222. package/src/chips/power/ade7953.js +427 -0
  223. package/src/chips/power/ina219.js +45 -40
  224. package/src/chips/power/ina226.js +50 -45
  225. package/src/chips/power/ina3221.js +95 -76
  226. package/src/chips/pressure/bmp085.js +251 -0
  227. package/src/chips/pressure/bmp180.js +54 -37
  228. package/src/chips/pressure/bmp280.js +72 -53
  229. package/src/chips/pressure/bmp384.js +410 -0
  230. package/src/chips/pressure/bmp581.js +489 -0
  231. package/src/chips/pressure/lps22df.js +341 -0
  232. package/src/chips/pressure/lps28dfw.js +314 -0
  233. package/src/chips/pressure/lps33hw.js +255 -0
  234. package/src/chips/rfid/mfrc522.js +674 -0
  235. package/src/chips/rtc/ds3231.js +504 -0
  236. package/src/chips/rtc/pcf8523.js +539 -0
  237. package/src/chips/temperature/mcp9808.js +488 -0
  238. package/src/chips/temperature/tmp117.js +471 -0
  239. package/src/chips/tof/_vl53_base.js +160 -0
  240. package/src/chips/tof/vl53l0x.js +816 -0
  241. package/src/chips/tof/vl53l1x.js +816 -0
  242. package/src/connection/connection.js +111 -0
  243. package/src/connection/dhtxx.js +138 -0
  244. package/src/connection/dhtxx_mock.js +39 -0
  245. package/src/{transport → connection}/hx711.js +49 -22
  246. package/src/connection/hx711_mock.js +52 -0
  247. package/src/{transport → connection}/i2c.js +13 -9
  248. package/src/connection/i2c_mock.js +91 -0
  249. package/src/connection/input_pin.js +156 -0
  250. package/src/connection/neo6_mock.js +45 -0
  251. package/src/{transport → connection}/neopixel.js +23 -4
  252. package/src/connection/output_pin.js +41 -0
  253. package/src/connection/sipo.js +159 -0
  254. package/src/{transport → connection}/smbus.js +14 -10
  255. package/src/{transport → connection}/spi.js +12 -8
  256. package/src/connection/spi_mock.js +151 -0
  257. package/src/{transport → connection}/uart.js +33 -26
  258. package/examples/gnss/.gitkeep +0 -0
  259. package/examples/humidity/.gitkeep +0 -0
  260. package/examples/motor/.gitkeep +0 -0
  261. package/examples/temperature/.gitkeep +0 -0
  262. package/examples/tof/.gitkeep +0 -0
  263. package/src/chips/gnss/.gitkeep +0 -0
  264. package/src/chips/motor/.gitkeep +0 -0
  265. package/src/chips/temperature/.gitkeep +0 -0
  266. package/src/chips/tof/.gitkeep +0 -0
@@ -0,0 +1,590 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * RFM9x (RFM95/96/97/98W) LoRa transceiver driver.
5
+ *
6
+ * All four modules share identical pins, register maps, SPI protocol, and
7
+ * LoRa modem logic. They differ only in supported frequency bands and the
8
+ * maximum spreading factor for RFM97W.
9
+ *
10
+ * The driver is built around an internal `_RFM9xBase` that owns all
11
+ * register-level logic. Four thin variant subclasses — `RFM95Minimal`,
12
+ * `RFM96Minimal`, `RFM97Minimal`, `RFM98Minimal` — supply the
13
+ * variant-specific frequency limits, maximum SF, and band flag via static
14
+ * class fields.
15
+ *
16
+ * `_RFM9xBase` exposes only the Minimal-stage public API (`init`, `send`,
17
+ * `receive`) plus underscore-prefixed private helpers. Full-stage
18
+ * functionality (reset, configure, setFrequency, setTxPower, interrupt
19
+ * receive, continuous receive, rssi/snr readouts, sleep/standby, version)
20
+ * is implemented ONLY in `_RFM9xFullMixin`, a mixin function applied to a
21
+ * `*Minimal` subclass to produce the corresponding `*Full` class:
22
+ *
23
+ * class RFM95Full extends _RFM9xFullMixin(RFM95Minimal) {}
24
+ *
25
+ * This is a structural (not just naming-convention) separation: `_RFM9xBase`
26
+ * literally does not define the Full-only methods, so a `*Minimal` instance
27
+ * cannot reach them, matching the reference Python driver
28
+ * (python/periph/chips/comms/rfm9x.py).
29
+ */
30
+
31
+ // Register addresses (module-scoped constants, not class members — a class
32
+ // field accessed via `this.X` only resolves for *instance* fields; a
33
+ // `static X` field is only reachable via `ClassName.X` or
34
+ // `this.constructor.X`, never bare `this.X`).
35
+ const _REG_FIFO = 0x00;
36
+ const _REG_OP_MODE = 0x01;
37
+ const _REG_FRF_MSB = 0x06;
38
+ const _REG_FRF_MID = 0x07;
39
+ const _REG_FRF_LSB = 0x08;
40
+ const _REG_PA_CONFIG = 0x09;
41
+ const _REG_OCP = 0x0B;
42
+ const _REG_LNA = 0x0C;
43
+ const _REG_FIFO_ADDR_PTR = 0x0D;
44
+ const _REG_FIFO_TX_BASE = 0x0E;
45
+ const _REG_FIFO_RX_BASE = 0x0F;
46
+ const _REG_FIFO_RX_CURRENT = 0x10;
47
+ const _REG_IRQ_FLAGS = 0x12;
48
+ const _REG_RX_NB_BYTES = 0x13;
49
+ const _REG_PKT_SNR = 0x19;
50
+ const _REG_PKT_RSSI = 0x1A;
51
+ const _REG_RSSI = 0x1B;
52
+ const _REG_MODEM_CONFIG_1 = 0x1D;
53
+ const _REG_MODEM_CONFIG_2 = 0x1E;
54
+ const _REG_PREAMBLE_LSB = 0x21;
55
+ const _REG_PAYLOAD_LENGTH = 0x22;
56
+ const _REG_MODEM_CONFIG_3 = 0x26;
57
+ const _REG_DETECTION_OPT = 0x31;
58
+ const _REG_DETECTION_THR = 0x37;
59
+ const _REG_DIO_MAPPING_1 = 0x40;
60
+ const _REG_VERSION = 0x42;
61
+ const _REG_PA_DAC = 0x4D;
62
+
63
+ const _MODE_LONG_RANGE = 0x80;
64
+ const _MODE_SLEEP = 0x00;
65
+ const _MODE_STANDBY = 0x01;
66
+ const _MODE_TX = 0x03;
67
+ const _MODE_RX_CONT = 0x05;
68
+ const _MODE_RX_SINGLE = 0x06;
69
+
70
+ const _IRQ_TX_DONE = 0x08;
71
+ const _IRQ_RX_DONE = 0x40;
72
+ const _IRQ_RX_TIMEOUT = 0x80;
73
+
74
+ const _PA_BOOST = 0x80;
75
+ const _PA_DAC_HIGH_POWER = 0x87;
76
+ const _PA_DAC_DEFAULT = 0x84;
77
+ const _OCP_240MA = 0x3B;
78
+ const _OCP_DEFAULT = 0x2B;
79
+
80
+ const _DIO0_RX_DONE = 0x00;
81
+ const _DIO0_TX_DONE = 0x40;
82
+
83
+ const _FXOSC = 32000000;
84
+ const _EXPECTED_VERSION = 0x12;
85
+
86
+ /**
87
+ * Base class for RFM95/96/97/98W LoRa transceivers (LoRa mode only).
88
+ *
89
+ * Owns all register-level logic. Only the Minimal-stage public API
90
+ * (`init`, `send`, `receive`) is exposed here; Full-stage functionality is
91
+ * implemented as underscore-prefixed private helpers and re-exposed
92
+ * publicly by `_RFM9xFullMixin`, so `*Minimal` instances never see it.
93
+ *
94
+ * Not exported — construct one of the `*Minimal` / `*Full` variant classes.
95
+ */
96
+ class _RFM9xBase {
97
+ /**
98
+ * @param {object} connection - SPI connection (write, read, writeRead).
99
+ * @param {number} frequencyHz - Carrier frequency in Hz; must lie in the variant's range.
100
+ * @param {import('../../connection/output_pin').OutputPin|null} [resetPin=null] -
101
+ * Optional NRESET OutputPin. When omitted, `init()` waits 10 ms for POR instead.
102
+ * @param {import('../../connection/input_pin').InputPin|null} [dio0Pin=null] -
103
+ * Optional DIO0 InputPin, used only by Full's `receive(timeoutMs, useInterrupt=true)`;
104
+ * Minimal ignores it.
105
+ */
106
+ constructor(connection, frequencyHz, resetPin = null, dio0Pin = null) {
107
+ this._connection = connection;
108
+ this._resetPin = resetPin;
109
+ this._dio0Pin = dio0Pin;
110
+ this.freqMinHz = this.constructor.FREQ_MIN_HZ;
111
+ this.freqMaxHz = this.constructor.FREQ_MAX_HZ;
112
+ this.maxSF = this.constructor.MAX_SF;
113
+ this._lfBand = this.constructor.LF_BAND;
114
+ if (frequencyHz < this.freqMinHz || frequencyHz > this.freqMaxHz) {
115
+ throw new RangeError(`frequencyHz ${frequencyHz} out of range [${this.freqMinHz}, ${this.freqMaxHz}]`);
116
+ }
117
+ this._frequencyHz = frequencyHz;
118
+ }
119
+
120
+ /**
121
+ * Initialise the chip: hardware reset (if a `resetPin` was passed to the
122
+ * constructor) or a 10 ms POR wait, then the full register init sequence
123
+ * (FSK SLEEP → LoRa SLEEP → STDBY; FIFO bases; frequency; modem
124
+ * defaults SF7/125 kHz/4-5; +17 dBm PA_BOOST).
125
+ * @returns {Promise<void>}
126
+ */
127
+ async init() {
128
+ if (this._resetPin) {
129
+ await this._resetViaPin(this._resetPin);
130
+ } else {
131
+ await this._sleep(10);
132
+ }
133
+ await this._initRegisters();
134
+ }
135
+
136
+ /** Run the register-level init sequence. Also used by Full's `reset()`. */
137
+ async _initRegisters() {
138
+ const version = await this._readReg(_REG_VERSION);
139
+ if (version !== _EXPECTED_VERSION) {
140
+ throw new Error(`RFM9x version mismatch: expected 0x${_EXPECTED_VERSION.toString(16).padStart(2, '0')}, got 0x${version.toString(16).padStart(2, '0')}`);
141
+ }
142
+
143
+ await this._writeReg(_REG_OP_MODE, 0x00);
144
+ await this._sleep(1);
145
+ await this._writeReg(_REG_OP_MODE, _MODE_LONG_RANGE | (this._lfBand ? 0x08 : 0x00) | _MODE_SLEEP);
146
+ await this._sleep(1);
147
+
148
+ if (this._lfBand) {
149
+ const lna = await this._readReg(_REG_LNA);
150
+ await this._writeReg(_REG_LNA, lna & 0x3F);
151
+ } else {
152
+ await this._writeReg(_REG_LNA, 0x23);
153
+ }
154
+ await this._writeReg(_REG_MODEM_CONFIG_3, (await this._readReg(_REG_MODEM_CONFIG_3)) | 0x04);
155
+
156
+ await this._writeReg(_REG_FIFO_TX_BASE, 0x80);
157
+ await this._writeReg(_REG_FIFO_RX_BASE, 0x00);
158
+
159
+ await this._setFrequency(this._frequencyHz);
160
+
161
+ await this._writeReg(_REG_MODEM_CONFIG_1, (0x07 << 4) | (0x01 << 1) | 0x00);
162
+ await this._writeReg(_REG_MODEM_CONFIG_2, (0x07 << 4) | (0x01 << 2) | 0x03);
163
+ await this._writeReg(_REG_PREAMBLE_LSB, 0x08);
164
+
165
+ await this._setTxPower(17, true);
166
+ await this._standby();
167
+ }
168
+
169
+ /** Pulse the NRESET pin low then high, per datasheet timing. */
170
+ async _resetViaPin(pin) {
171
+ await pin.set(false);
172
+ await this._sleep(1);
173
+ await pin.set(true);
174
+ await this._sleep(5);
175
+ }
176
+
177
+ _sleep(ms) {
178
+ return new Promise(r => setTimeout(r, ms));
179
+ }
180
+
181
+ async _writeReg(reg, value) {
182
+ await this._connection.write(Buffer.from([reg | 0x80, value & 0xFF]));
183
+ }
184
+
185
+ async _readReg(reg) {
186
+ return (await this._connection.writeRead(Buffer.from([reg & 0x7F]), 1))[0];
187
+ }
188
+
189
+ async _burstWrite(reg, data) {
190
+ const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
191
+ await this._connection.write(Buffer.concat([Buffer.from([reg | 0x80]), buf]));
192
+ }
193
+
194
+ async _burstRead(reg, len) {
195
+ return this._connection.writeRead(Buffer.from([reg & 0x7F]), len);
196
+ }
197
+
198
+ async _setFrequency(frequencyHz) {
199
+ if (frequencyHz < this.freqMinHz || frequencyHz > this.freqMaxHz) {
200
+ throw new RangeError(`frequencyHz ${frequencyHz} out of range [${this.freqMinHz}, ${this.freqMaxHz}]`);
201
+ }
202
+ const frf = Math.floor((frequencyHz * (1 << 19)) / _FXOSC);
203
+ await this._writeReg(_REG_FRF_MSB, (frf >> 16) & 0xFF);
204
+ await this._writeReg(_REG_FRF_MID, (frf >> 8) & 0xFF);
205
+ await this._writeReg(_REG_FRF_LSB, frf & 0xFF);
206
+ this._frequencyHz = frequencyHz;
207
+ }
208
+
209
+ async _setTxPower(powerDbm, usePaBoost = true) {
210
+ if (usePaBoost) {
211
+ if (powerDbm > 17) {
212
+ if (powerDbm > 20) powerDbm = 20;
213
+ await this._writeReg(_REG_PA_DAC, _PA_DAC_HIGH_POWER);
214
+ await this._writeReg(_REG_OCP, _OCP_240MA);
215
+ await this._writeReg(_REG_PA_CONFIG, _PA_BOOST | 0x0F);
216
+ } else {
217
+ if (powerDbm < 2) powerDbm = 2;
218
+ await this._writeReg(_REG_PA_DAC, _PA_DAC_DEFAULT);
219
+ await this._writeReg(_REG_OCP, _OCP_DEFAULT);
220
+ await this._writeReg(_REG_PA_CONFIG, _PA_BOOST | (powerDbm - 2));
221
+ }
222
+ } else {
223
+ await this._writeReg(_REG_PA_DAC, _PA_DAC_DEFAULT);
224
+ await this._writeReg(_REG_OCP, _OCP_DEFAULT);
225
+ const maxPower = 7;
226
+ const pmax = 10.8 + 0.6 * maxPower;
227
+ let op = Math.floor(powerDbm - pmax + 15);
228
+ if (op < 0) op = 0;
229
+ if (op > 15) op = 15;
230
+ await this._writeReg(_REG_PA_CONFIG, (maxPower << 4) | op);
231
+ }
232
+ }
233
+
234
+ async _configure(sf, bandwidthKhz, codingRate, crc = true) {
235
+ const bwTable = [7.8, 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125.0, 250.0, 500.0];
236
+ let bwCode = 0x07;
237
+ for (let i = 0; i < 10; i++) {
238
+ if (bandwidthKhz === bwTable[i]) { bwCode = i; break; }
239
+ }
240
+ if (sf < 6 || sf > this.maxSF) sf = Math.min(Math.max(sf, 6), this.maxSF);
241
+
242
+ if (sf === 6) {
243
+ await this._writeReg(_REG_DETECTION_OPT, 0x05);
244
+ await this._writeReg(_REG_DETECTION_THR, 0x0C);
245
+ } else {
246
+ await this._writeReg(_REG_DETECTION_OPT, 0x03);
247
+ await this._writeReg(_REG_DETECTION_THR, 0x0A);
248
+ }
249
+
250
+ const implicitHeader = (sf === 6);
251
+ const cr = (codingRate >= 5 && codingRate <= 8) ? (codingRate - 4) : 0x01;
252
+ await this._writeReg(_REG_MODEM_CONFIG_1, (bwCode << 4) | (cr << 1) | (implicitHeader ? 1 : 0));
253
+ await this._writeReg(_REG_MODEM_CONFIG_2, (sf << 4) | ((crc ? 1 : 0) << 2) | 0x03);
254
+ }
255
+
256
+ /**
257
+ * Send a packet.
258
+ * @param {Buffer|Uint8Array|string} data - Payload; max 255 bytes.
259
+ * @returns {Promise<void>}
260
+ */
261
+ async send(data) {
262
+ const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
263
+ if (buf.length > 255) throw new RangeError(`payload length ${buf.length} exceeds 255`);
264
+ await this._standby();
265
+ await this._writeReg(_REG_FIFO_ADDR_PTR, 0x80);
266
+ await this._burstWrite(_REG_FIFO, buf);
267
+ await this._writeReg(_REG_PAYLOAD_LENGTH, buf.length);
268
+ await this._writeReg(_REG_DIO_MAPPING_1, _DIO0_TX_DONE);
269
+ await this._writeReg(_REG_OP_MODE, _MODE_LONG_RANGE | (this._lfBand ? 0x08 : 0x00) | _MODE_TX);
270
+
271
+ while (true) {
272
+ const irq = await this._readReg(_REG_IRQ_FLAGS);
273
+ if (irq & _IRQ_TX_DONE) break;
274
+ await this._sleep(2);
275
+ }
276
+ await this._writeReg(_REG_IRQ_FLAGS, _IRQ_TX_DONE);
277
+ await this._standby();
278
+ }
279
+
280
+ /**
281
+ * Receive a single packet (polling).
282
+ * @param {number} timeoutMs - Receive timeout in milliseconds (default 2000).
283
+ * @returns {Promise<Buffer | null>} Received payload bytes, or null on timeout.
284
+ */
285
+ async receive(timeoutMs = 2000) {
286
+ return this._receivePolling(timeoutMs);
287
+ }
288
+
289
+ async _receivePolling(timeoutMs) {
290
+ await this._standby();
291
+ await this._writeReg(_REG_DIO_MAPPING_1, _DIO0_RX_DONE);
292
+ await this._writeReg(_REG_OP_MODE, _MODE_LONG_RANGE | (this._lfBand ? 0x08 : 0x00) | _MODE_RX_SINGLE);
293
+
294
+ const start = Date.now();
295
+ while (Date.now() - start < timeoutMs) {
296
+ const irq = await this._readReg(_REG_IRQ_FLAGS);
297
+ if (irq & _IRQ_RX_DONE) {
298
+ await this._writeReg(_REG_IRQ_FLAGS, _IRQ_RX_DONE);
299
+ return await this._readPayload();
300
+ }
301
+ if (irq & _IRQ_RX_TIMEOUT) {
302
+ await this._writeReg(_REG_IRQ_FLAGS, _IRQ_RX_TIMEOUT);
303
+ return null;
304
+ }
305
+ await this._sleep(5);
306
+ }
307
+ await this._writeReg(_REG_OP_MODE, _MODE_LONG_RANGE | (this._lfBand ? 0x08 : 0x00) | _MODE_STANDBY);
308
+ return null;
309
+ }
310
+
311
+ async _readPayload() {
312
+ const current = await this._readReg(_REG_FIFO_RX_CURRENT);
313
+ await this._writeReg(_REG_FIFO_ADDR_PTR, current);
314
+ const n = await this._readReg(_REG_RX_NB_BYTES);
315
+ return await this._burstRead(_REG_FIFO, n);
316
+ }
317
+
318
+ async _standby() {
319
+ await this._writeReg(_REG_OP_MODE, _MODE_LONG_RANGE | (this._lfBand ? 0x08 : 0x00) | _MODE_STANDBY);
320
+ }
321
+
322
+ async _sleepMode() {
323
+ await this._writeReg(_REG_OP_MODE, _MODE_LONG_RANGE | (this._lfBand ? 0x08 : 0x00) | _MODE_SLEEP);
324
+ }
325
+ }
326
+
327
+ /**
328
+ * Mixin that adds Full-stage functionality on top of a `*Minimal` subclass.
329
+ *
330
+ * `_RFM9xBase` structurally lacks these methods (they are not defined
331
+ * anywhere on it), so applying this mixin is the only way to obtain them —
332
+ * a `*Minimal` instance cannot reach Full behaviour even by accident.
333
+ *
334
+ * Usage: `class RFM95Full extends _RFM9xFullMixin(RFM95Minimal) {}`.
335
+ *
336
+ * @param {typeof _RFM9xBase} Base - A `*Minimal` class (itself extending `_RFM9xBase`).
337
+ * @returns {typeof _RFM9xBase} A subclass of `Base` with the Full API added.
338
+ */
339
+ const _RFM9xFullMixin = (Base) => class extends Base {
340
+ /**
341
+ * Hardware reset via NRESET pin; re-runs the init sequence.
342
+ * @returns {Promise<void>}
343
+ * @throws {Error} If no `resetPin` was passed to the constructor.
344
+ */
345
+ async reset() {
346
+ if (!this._resetPin) throw new Error('reset() requires a resetPin');
347
+ await this._resetViaPin(this._resetPin);
348
+ await this._initRegisters();
349
+ }
350
+
351
+ /**
352
+ * Configure LoRa modulation parameters.
353
+ * @param {number} sf - Spreading factor 6-12 (variant-capped; RFM97W max 9).
354
+ * @param {number} bandwidthKhz - Signal bandwidth in kHz.
355
+ * @param {number} codingRate - Coding rate denominator 5-8.
356
+ * @param {boolean} [crc=true] - True to enable CRC on RX payloads (default).
357
+ * @returns {Promise<void>}
358
+ */
359
+ async configure(sf, bandwidthKhz, codingRate, crc = true) {
360
+ await this._configure(sf, bandwidthKhz, codingRate, crc);
361
+ }
362
+
363
+ /**
364
+ * Set the carrier frequency.
365
+ * @param {number} frequencyHz - Carrier frequency in Hz; must lie in the variant's range.
366
+ * @returns {Promise<void>}
367
+ */
368
+ async setFrequency(frequencyHz) {
369
+ await this._setFrequency(frequencyHz);
370
+ }
371
+
372
+ /**
373
+ * Set TX output power.
374
+ * @param {number} powerDbm - Output power in dBm. -1 to +14 (RFO) or +2 to +20 (PA_BOOST).
375
+ * @param {boolean} [usePaBoost=true] - True to use PA_BOOST pin (default), false for RFO.
376
+ * @returns {Promise<void>}
377
+ */
378
+ async setTxPower(powerDbm, usePaBoost = true) {
379
+ await this._setTxPower(powerDbm, usePaBoost);
380
+ }
381
+
382
+ /**
383
+ * Receive a single packet.
384
+ * @param {number} [timeoutMs=2000] - Receive timeout in milliseconds.
385
+ * @param {boolean} [useInterrupt=false] - True to wait on the DIO0 edge instead
386
+ * of polling the IRQ status register (requires `dio0Pin` passed to the constructor).
387
+ * @returns {Promise<Buffer | null>} Received payload bytes, or null on timeout.
388
+ * @throws {Error} If `useInterrupt` is true but no `dio0Pin` was configured.
389
+ */
390
+ async receive(timeoutMs = 2000, useInterrupt = false) {
391
+ if (useInterrupt) {
392
+ if (!this._dio0Pin) throw new Error('receive(useInterrupt=true) requires dio0Pin');
393
+ return this._receiveInterrupt(timeoutMs);
394
+ }
395
+ return this._receivePolling(timeoutMs);
396
+ }
397
+
398
+ /** DIO0-interrupt-driven single receive, used by receive(useInterrupt=true). */
399
+ async _receiveInterrupt(timeoutMs) {
400
+ await this._standby();
401
+ await this._writeReg(_REG_DIO_MAPPING_1, _DIO0_RX_DONE);
402
+ await this._writeReg(_REG_OP_MODE, _MODE_LONG_RANGE | (this._lfBand ? 0x08 : 0x00) | _MODE_RX_SINGLE);
403
+
404
+ return new Promise((resolve, reject) => {
405
+ let settled = false;
406
+ let timer = null;
407
+
408
+ const onEdge = async () => {
409
+ if (settled) return;
410
+ settled = true;
411
+ clearTimeout(timer);
412
+ try {
413
+ await this._dio0Pin.offEdge(onEdge);
414
+ const irq = await this._readReg(_REG_IRQ_FLAGS);
415
+ await this._writeReg(_REG_IRQ_FLAGS, irq);
416
+ resolve((irq & _IRQ_RX_DONE) ? await this._readPayload() : null);
417
+ } catch (err) {
418
+ reject(err);
419
+ }
420
+ };
421
+
422
+ timer = setTimeout(async () => {
423
+ if (settled) return;
424
+ settled = true;
425
+ try {
426
+ await this._dio0Pin.offEdge(onEdge);
427
+ await this._writeReg(_REG_OP_MODE, _MODE_LONG_RANGE | (this._lfBand ? 0x08 : 0x00) | _MODE_STANDBY);
428
+ resolve(null);
429
+ } catch (err) {
430
+ reject(err);
431
+ }
432
+ }, timeoutMs);
433
+
434
+ this._dio0Pin.onEdge(onEdge, 'rising').catch(reject);
435
+ });
436
+ }
437
+
438
+ /**
439
+ * Enter continuous receive mode; subsequent `readPacket()` calls drain the FIFO.
440
+ * @returns {Promise<void>}
441
+ */
442
+ async receiveContinuous() {
443
+ await this._standby();
444
+ await this._writeReg(_REG_DIO_MAPPING_1, _DIO0_RX_DONE);
445
+ await this._writeReg(_REG_OP_MODE, _MODE_LONG_RANGE | (this._lfBand ? 0x08 : 0x00) | _MODE_RX_CONT);
446
+ }
447
+
448
+ /**
449
+ * Read one packet from the FIFO in continuous receive mode.
450
+ * @returns {Promise<Buffer | null>} Payload bytes, or null if no packet is waiting.
451
+ */
452
+ async readPacket() {
453
+ const irq = await this._readReg(_REG_IRQ_FLAGS);
454
+ if (!(irq & _IRQ_RX_DONE)) return null;
455
+ await this._writeReg(_REG_IRQ_FLAGS, _IRQ_RX_DONE);
456
+ return await this._readPayload();
457
+ }
458
+
459
+ /**
460
+ * Return to STDBY from continuous receive mode.
461
+ * @returns {Promise<void>}
462
+ */
463
+ async stopReceive() {
464
+ await this._standby();
465
+ }
466
+
467
+ /**
468
+ * Enter STDBY mode (crystal on, RF/PLL off, FIFO accessible).
469
+ * @returns {Promise<void>}
470
+ */
471
+ async standby() {
472
+ await this._standby();
473
+ }
474
+
475
+ /**
476
+ * Enter SLEEP mode (lowest power; FIFO inaccessible).
477
+ * @returns {Promise<void>}
478
+ */
479
+ async sleep() {
480
+ await this._sleepMode();
481
+ }
482
+
483
+ /**
484
+ * Read RegVersion. Expect 0x12 (SX1276).
485
+ * @returns {Promise<number>}
486
+ */
487
+ async version() {
488
+ return await this._readReg(_REG_VERSION);
489
+ }
490
+
491
+ /**
492
+ * Current channel RSSI in dBm (readable in continuous RX mode).
493
+ * @returns {Promise<number>}
494
+ */
495
+ async rssi() {
496
+ return -137 + await this._readReg(_REG_RSSI);
497
+ }
498
+
499
+ /**
500
+ * RSSI of last received packet in dBm.
501
+ * @returns {Promise<number>}
502
+ */
503
+ async lastPacketRssi() {
504
+ return -137 + await this._readReg(_REG_PKT_RSSI);
505
+ }
506
+
507
+ /**
508
+ * SNR of last received packet in dB. The raw register holds a signed
509
+ * 8-bit value with 0.25 dB resolution; values below zero mean the
510
+ * signal is below the noise floor.
511
+ * @returns {Promise<number>}
512
+ */
513
+ async lastPacketSnr() {
514
+ let raw = await this._readReg(_REG_PKT_SNR);
515
+ if (raw & 0x80) raw = raw - 0x100;
516
+ return raw / 4.0;
517
+ }
518
+ };
519
+
520
+ /**
521
+ * RFM95W minimal driver — 868/915 MHz HF band, max SF=12.
522
+ *
523
+ * Constructor: see `_RFM9xBase` — `(connection, frequencyHz, resetPin=null, dio0Pin=null)`.
524
+ * `frequencyHz` must lie in 862-1020 MHz.
525
+ */
526
+ class RFM95Minimal extends _RFM9xBase {
527
+ static FREQ_MIN_HZ = 862_000_000;
528
+ static FREQ_MAX_HZ = 1_020_000_000;
529
+ static MAX_SF = 12;
530
+ static LF_BAND = false;
531
+ }
532
+
533
+ /**
534
+ * RFM96W minimal driver — 433/470 MHz LF band, max SF=12.
535
+ *
536
+ * Constructor: see `_RFM9xBase`. `frequencyHz` must lie in 410-525 MHz.
537
+ */
538
+ class RFM96Minimal extends _RFM9xBase {
539
+ static FREQ_MIN_HZ = 410_000_000;
540
+ static FREQ_MAX_HZ = 525_000_000;
541
+ static MAX_SF = 12;
542
+ static LF_BAND = true;
543
+ }
544
+
545
+ /**
546
+ * RFM97W minimal driver — 868/915 MHz HF band, max SF=9.
547
+ *
548
+ * Constructor: see `_RFM9xBase`. `frequencyHz` must lie in 862-1020 MHz.
549
+ */
550
+ class RFM97Minimal extends _RFM9xBase {
551
+ static FREQ_MIN_HZ = 862_000_000;
552
+ static FREQ_MAX_HZ = 1_020_000_000;
553
+ static MAX_SF = 9;
554
+ static LF_BAND = false;
555
+ }
556
+
557
+ /**
558
+ * RFM98W minimal driver — 433/470 MHz LF band, max SF=12.
559
+ *
560
+ * Constructor: see `_RFM9xBase`. `frequencyHz` must lie in 410-525 MHz.
561
+ */
562
+ class RFM98Minimal extends _RFM9xBase {
563
+ static FREQ_MIN_HZ = 410_000_000;
564
+ static FREQ_MAX_HZ = 525_000_000;
565
+ static MAX_SF = 12;
566
+ static LF_BAND = true;
567
+ }
568
+
569
+ /**
570
+ * RFM95W full driver — adds hardware reset and DIO0 interrupt support.
571
+ */
572
+ class RFM95Full extends _RFM9xFullMixin(RFM95Minimal) {}
573
+
574
+ /**
575
+ * RFM96W full driver — adds hardware reset and DIO0 interrupt support.
576
+ */
577
+ class RFM96Full extends _RFM9xFullMixin(RFM96Minimal) {}
578
+
579
+ /**
580
+ * RFM97W full driver — adds hardware reset and DIO0 interrupt support.
581
+ */
582
+ class RFM97Full extends _RFM9xFullMixin(RFM97Minimal) {}
583
+
584
+ /**
585
+ * RFM98W full driver — adds hardware reset and DIO0 interrupt support.
586
+ */
587
+ class RFM98Full extends _RFM9xFullMixin(RFM98Minimal) {}
588
+
589
+ module.exports = { RFM95Minimal, RFM96Minimal, RFM97Minimal, RFM98Minimal,
590
+ RFM95Full, RFM96Full, RFM97Full, RFM98Full };