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,539 @@
1
+ 'use strict';
2
+
3
+ const _REG_CONTROL_1 = 0x00;
4
+ const _REG_CONTROL_2 = 0x01;
5
+ const _REG_CONTROL_3 = 0x02;
6
+ const _REG_SECONDS = 0x03;
7
+ const _REG_MINUTE_ALARM = 0x0A;
8
+ const _REG_OFFSET = 0x0E;
9
+ const _REG_TMR_CLKOUT_CTRL = 0x0F;
10
+ const _REG_TMR_A_FREQ_CTRL = 0x10;
11
+ const _REG_TMR_A_REG = 0x11;
12
+ const _REG_TMR_B_FREQ_CTRL = 0x12;
13
+ const _REG_TMR_B_REG = 0x13;
14
+
15
+ // CONTROL_1 (0x00) bits.
16
+ const _C1_T = 0x40;
17
+ const _C1_STOP = 0x20;
18
+ const _C1_SR = 0x10;
19
+ const _C1_12_24 = 0x08;
20
+ const _C1_SIE = 0x04;
21
+ const _C1_AIE = 0x02;
22
+
23
+ // CONTROL_2 (0x01) bits.
24
+ const _C2_WTAF = 0x80;
25
+ const _C2_CTAF = 0x40;
26
+ const _C2_CTBF = 0x20;
27
+ const _C2_SF = 0x10;
28
+ const _C2_AF = 0x08;
29
+ const _C2_WTAIE = 0x04;
30
+ const _C2_CTAIE = 0x02;
31
+ const _C2_CTBIE = 0x01;
32
+ const _C2_CLEARABLE = 0x78; // CTAF|CTBF|SF|AF: write 0 clears, 1 keeps
33
+ const _C2_ENABLES = 0x07;
34
+
35
+ // CONTROL_3 (0x02) bits.
36
+ const _C3_PM_MASK = 0xE0;
37
+ const _C3_BSF = 0x08;
38
+ const _C3_BLF = 0x04;
39
+ const _C3_BSIE = 0x02;
40
+ const _C3_BLIE = 0x01;
41
+
42
+ const _SECONDS_OS = 0x80;
43
+
44
+ // TMR_CLKOUT_CTRL (0x0F) fields.
45
+ const _TMR_TAM = 0x80;
46
+ const _TMR_TBM = 0x40;
47
+ const _TMR_COF_MASK = 0x38;
48
+ const _TMR_TAC_MASK = 0x06;
49
+ const _TMR_TAC_COUNTDOWN = 0x02;
50
+ const _TMR_TAC_WATCHDOG = 0x04;
51
+ const _TMR_TBC = 0x01;
52
+
53
+ const _SOURCE_CLOCKS = { '4096hz': 0x00, '64hz': 0x01, '1hz': 0x02, '1_60hz': 0x03, '1_3600hz': 0x07 };
54
+
55
+ // TBW[2:0] -> low-pulse width in ms (datasheet Table 36; not uniformly spaced).
56
+ const _TBW_WIDTHS_MS = [46.875, 62.5, 78.125, 93.75, 125, 156.25, 187.5, 218.75];
57
+
58
+ const _CLKOUT_COF = { 32768: 0, 16384: 1, 8192: 2, 4096: 3, 1024: 4, 32: 5, 1: 6 };
59
+
60
+ const _PM_MODES = {
61
+ standard: [0x00, 0x04], // [low detection on, off]
62
+ direct: [0x01, 0x05],
63
+ disabled: [0x02, 0x07],
64
+ };
65
+
66
+ function _bcdToInt(b) {
67
+ return ((b >> 4) & 0x0F) * 10 + (b & 0x0F);
68
+ }
69
+
70
+ function _intToBcd(v) {
71
+ return (Math.floor(v / 10) << 4) | (v % 10);
72
+ }
73
+
74
+ /**
75
+ * PCF8523 low-power I²C real-time clock and calendar (NXP) — minimal
76
+ * interface.
77
+ *
78
+ * Reads/writes the battery-backed calendar clock with no configuration
79
+ * beyond the connection. `init()` enables battery switch-over in standard
80
+ * mode with battery-low detection (PM[2:0]=000) — a deliberate override of
81
+ * the chip's single-supply POR default. Fixed I²C address 0x68.
82
+ *
83
+ * This driver always operates the HOURS registers in 24-hour mode.
84
+ * `weekday` follows the datasheet's suggested assignment, 0=Sunday ..
85
+ * 6=Saturday — the WEEKDAYS register has no hardware-enforced meaning.
86
+ */
87
+ class PCF8523Minimal {
88
+ /**
89
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection at address 0x68.
90
+ */
91
+ constructor(connection) {
92
+ this._conn = connection;
93
+ }
94
+
95
+ async _writeReg(reg, value) {
96
+ await this._conn.write(Buffer.from([reg & 0xFF, value & 0xFF]));
97
+ }
98
+
99
+ async _readReg(reg) {
100
+ return (await this._conn.writeRead(Buffer.from([reg & 0xFF]), 1))[0];
101
+ }
102
+
103
+ async _readRegs(reg, n) {
104
+ return this._conn.writeRead(Buffer.from([reg & 0xFF]), n);
105
+ }
106
+
107
+ async _readControl1() {
108
+ // T must always be written 0 and SR always reads 0; mask both so a
109
+ // read-modify-write never triggers a reset.
110
+ return (await this._readReg(_REG_CONTROL_1)) & ~(_C1_T | _C1_SR) & 0xFF;
111
+ }
112
+
113
+ /**
114
+ * Confirm the device answers on the bus (plain CONTROL_1 read — the
115
+ * PCF8523 has no identity register) and enable battery switch-over
116
+ * standard mode with battery-low detection (PM[2:0]=000).
117
+ * @returns {Promise<void>}
118
+ */
119
+ async init() {
120
+ await this._readReg(_REG_CONTROL_1);
121
+ await this._writeReg(_REG_CONTROL_3, 0x00);
122
+ }
123
+
124
+ /**
125
+ * Read the calendar clock.
126
+ * @returns {Promise<{year: number, month: number, day: number, weekday: number, hour: number, minute: number, second: number}>}
127
+ * `year` is 2000-2099, `hour` is 0-23, `weekday` is 0=Sunday..6=Saturday.
128
+ */
129
+ async getDatetime() {
130
+ const raw = await this._readRegs(_REG_SECONDS, 7);
131
+ return {
132
+ year: 2000 + _bcdToInt(raw[6]),
133
+ month: _bcdToInt(raw[5] & 0x1F),
134
+ day: _bcdToInt(raw[3] & 0x3F),
135
+ weekday: raw[4] & 0x07,
136
+ hour: _bcdToInt(raw[2] & 0x3F),
137
+ minute: _bcdToInt(raw[1] & 0x7F),
138
+ second: _bcdToInt(raw[0] & 0x7F),
139
+ };
140
+ }
141
+
142
+ /**
143
+ * Set the calendar clock using the STOP-bit precision start: freeze the
144
+ * divider chain (STOP=1), write all seven time/date registers in one
145
+ * transaction, then release STOP. Forces 24-hour mode and clears the OS
146
+ * flag (the time is now known-good).
147
+ * @param {number} year - 2000-2099.
148
+ * @param {number} month - 1-12.
149
+ * @param {number} day - 1-31.
150
+ * @param {number} weekday - 0=Sunday..6=Saturday.
151
+ * @param {number} hour - 0-23.
152
+ * @param {number} minute - 0-59.
153
+ * @param {number} second - 0-59.
154
+ * @returns {Promise<void>}
155
+ */
156
+ async setDatetime(year, month, day, weekday, hour, minute, second) {
157
+ const ctrl1 = (await this._readControl1()) & ~_C1_12_24 & 0xFF;
158
+ await this._writeReg(_REG_CONTROL_1, ctrl1 | _C1_STOP);
159
+ await this._conn.write(Buffer.from([
160
+ _REG_SECONDS,
161
+ _intToBcd(second) & 0x7F, // OS = 0
162
+ _intToBcd(minute),
163
+ _intToBcd(hour) & 0x3F,
164
+ _intToBcd(day),
165
+ weekday & 0x07,
166
+ _intToBcd(month),
167
+ _intToBcd(year - 2000),
168
+ ]));
169
+ await this._writeReg(_REG_CONTROL_1, ctrl1 & ~_C1_STOP & 0xFF);
170
+ }
171
+ }
172
+
173
+ /**
174
+ * PCF8523 full interface — extends Minimal with the alarm, Timer A
175
+ * (countdown or watchdog), Timer B, programmable CLKOUT, offset
176
+ * calibration, battery backup control/status, oscillator-stop detection,
177
+ * software reset, and the Level-3 interrupt API.
178
+ *
179
+ * INT1 is shared with CLKOUT: interrupts only reach INT1 once CLKOUT is
180
+ * disabled (`disableClockOutput()`); the driver never does that
181
+ * implicitly. Timer B additionally drives the dedicated INT2 pin.
182
+ */
183
+ class PCF8523Full extends PCF8523Minimal {
184
+ constructor(connection) {
185
+ super(connection);
186
+ this._callback = null;
187
+ this._edgeHandler = null;
188
+ this._pollTimer = null;
189
+ }
190
+
191
+ async _updateTmrClkout(clearMask, setBits) {
192
+ const reg = await this._readReg(_REG_TMR_CLKOUT_CTRL);
193
+ await this._writeReg(_REG_TMR_CLKOUT_CTRL, (reg & ~clearMask) | setBits);
194
+ }
195
+
196
+ async _writeControl2(enables) {
197
+ // Re-supply the enable bits; write 1 to every clearable flag so none
198
+ // is cleared by accident (AND semantics). WTAF is read-only.
199
+ await this._writeReg(_REG_CONTROL_2, _C2_CLEARABLE | (enables & _C2_ENABLES));
200
+ }
201
+
202
+ async _writeControl3(value) {
203
+ // Write 1 to BSF so it is left unchanged; BLF is read-only.
204
+ await this._writeReg(_REG_CONTROL_3, (value & (_C3_PM_MASK | _C3_BSIE | _C3_BLIE)) | _C3_BSF);
205
+ }
206
+
207
+ // -- Alarm ------------------------------------------------------------
208
+
209
+ /**
210
+ * Decode the alarm registers (0x0A-0x0D).
211
+ * @returns {Promise<{minute: ?number, hour: ?number, day: ?number, weekday: ?number}>}
212
+ * Each field is `null` when disabled (AEN_x=1).
213
+ */
214
+ async getAlarm() {
215
+ const raw = await this._readRegs(_REG_MINUTE_ALARM, 4);
216
+ return {
217
+ minute: raw[0] & 0x80 ? null : _bcdToInt(raw[0] & 0x7F),
218
+ hour: raw[1] & 0x80 ? null : _bcdToInt(raw[1] & 0x3F),
219
+ day: raw[2] & 0x80 ? null : _bcdToInt(raw[2] & 0x3F),
220
+ weekday: raw[3] & 0x80 ? null : raw[3] & 0x07,
221
+ };
222
+ }
223
+
224
+ /**
225
+ * Write the alarm registers (0x0A-0x0D). A field left `null`/undefined
226
+ * is disabled; the alarm fires when every enabled field matches.
227
+ * @param {{minute?: ?number, hour?: ?number, day?: ?number, weekday?: ?number}} [alarm={}]
228
+ * `weekday` is 0=Sunday..6=Saturday.
229
+ * @returns {Promise<void>}
230
+ */
231
+ async setAlarm({ minute = null, hour = null, day = null, weekday = null } = {}) {
232
+ await this._conn.write(Buffer.from([
233
+ _REG_MINUTE_ALARM,
234
+ minute == null ? 0x80 : _intToBcd(minute) & 0x7F,
235
+ hour == null ? 0x80 : _intToBcd(hour) & 0x3F,
236
+ day == null ? 0x80 : _intToBcd(day) & 0x3F,
237
+ weekday == null ? 0x80 : weekday & 0x07,
238
+ ]));
239
+ }
240
+
241
+ // -- Timers -----------------------------------------------------------
242
+
243
+ /**
244
+ * Configure and start Timer A.
245
+ * @param {'countdown'|'watchdog'} mode
246
+ * @param {number} value - Countdown value, 0-255.
247
+ * @param {'4096hz'|'64hz'|'1hz'|'1_60hz'|'1_3600hz'} sourceClock
248
+ * @param {boolean} [pulsed=false] - Pulsed (true) or permanently-active interrupt.
249
+ * @returns {Promise<void>}
250
+ */
251
+ async configureTimerA(mode, value, sourceClock, pulsed = false) {
252
+ const tac = mode === 'watchdog' ? _TMR_TAC_WATCHDOG : _TMR_TAC_COUNTDOWN;
253
+ await this._writeReg(_REG_TMR_A_FREQ_CTRL, _SOURCE_CLOCKS[sourceClock]);
254
+ await this._writeReg(_REG_TMR_A_REG, value);
255
+ await this._updateTmrClkout(_TMR_TAM | _TMR_TAC_MASK, (pulsed ? _TMR_TAM : 0) | tac);
256
+ }
257
+
258
+ /**
259
+ * Stop Timer A (TAC=00).
260
+ * @returns {Promise<void>}
261
+ */
262
+ async disableTimerA() {
263
+ await this._updateTmrClkout(_TMR_TAC_MASK, 0);
264
+ }
265
+
266
+ /**
267
+ * @returns {Promise<number>} Timer A's live countdown value, 0-255 (not the loaded one).
268
+ */
269
+ async readTimerA() {
270
+ return this._readReg(_REG_TMR_A_REG);
271
+ }
272
+
273
+ /**
274
+ * Configure and start Timer B (also drives INT2).
275
+ * @param {number} value - Countdown value, 0-255.
276
+ * @param {'4096hz'|'64hz'|'1hz'|'1_60hz'|'1_3600hz'} sourceClock
277
+ * @param {number} [pulseWidthMs=46.875] - Pulsed-mode low-pulse width in ms; the
278
+ * nearest of the eight hardware widths (46.875-218.75 ms) is used.
279
+ * @param {boolean} [pulsed=false] - Pulsed (true) or permanently-active interrupt.
280
+ * @returns {Promise<void>}
281
+ */
282
+ async configureTimerB(value, sourceClock, pulseWidthMs = 46.875, pulsed = false) {
283
+ let tbw = 0;
284
+ _TBW_WIDTHS_MS.forEach((w, i) => {
285
+ if (Math.abs(w - pulseWidthMs) < Math.abs(_TBW_WIDTHS_MS[tbw] - pulseWidthMs)) tbw = i;
286
+ });
287
+ await this._writeReg(_REG_TMR_B_FREQ_CTRL, (tbw << 4) | _SOURCE_CLOCKS[sourceClock]);
288
+ await this._writeReg(_REG_TMR_B_REG, value);
289
+ await this._updateTmrClkout(_TMR_TBM | _TMR_TBC, (pulsed ? _TMR_TBM : 0) | _TMR_TBC);
290
+ }
291
+
292
+ /**
293
+ * Stop Timer B (TBC=0).
294
+ * @returns {Promise<void>}
295
+ */
296
+ async disableTimerB() {
297
+ await this._updateTmrClkout(_TMR_TBC, 0);
298
+ }
299
+
300
+ /**
301
+ * @returns {Promise<number>} Timer B's live countdown value, 0-255 (not the loaded one).
302
+ */
303
+ async readTimerB() {
304
+ return this._readReg(_REG_TMR_B_REG);
305
+ }
306
+
307
+ // -- CLKOUT -----------------------------------------------------------
308
+
309
+ /**
310
+ * Drive CLKOUT on the shared INT1/CLKOUT pin.
311
+ * @param {number} frequencyHz - One of 32768, 16384, 8192, 4096, 1024, 32, 1.
312
+ * @returns {Promise<void>}
313
+ */
314
+ async setClockOutput(frequencyHz) {
315
+ const cof = frequencyHz in _CLKOUT_COF ? _CLKOUT_COF[frequencyHz] : 7;
316
+ await this._updateTmrClkout(_TMR_COF_MASK, cof << 3);
317
+ }
318
+
319
+ /**
320
+ * Disable CLKOUT (COF=111), freeing INT1 for interrupts.
321
+ * @returns {Promise<void>}
322
+ */
323
+ async disableClockOutput() {
324
+ await this._updateTmrClkout(_TMR_COF_MASK, _TMR_COF_MASK);
325
+ }
326
+
327
+ // -- Offset -----------------------------------------------------------
328
+
329
+ /**
330
+ * Read the clock-offset calibration register.
331
+ * @returns {Promise<{offset: number, mode: 'every_two_hours'|'every_minute'}>}
332
+ * `offset` is -64..+63 LSB (4.34 ppm/LSB every two hours, 4.069 ppm/LSB every minute).
333
+ */
334
+ async getOffset() {
335
+ const raw = await this._readReg(_REG_OFFSET);
336
+ let offset = raw & 0x7F;
337
+ if (offset & 0x40) offset -= 128;
338
+ return { offset, mode: raw & 0x80 ? 'every_minute' : 'every_two_hours' };
339
+ }
340
+
341
+ /**
342
+ * Write the clock-offset calibration register.
343
+ * @param {number} offset - Two's-complement correction, -64 to 63 LSB.
344
+ * @param {'every_two_hours'|'every_minute'} [mode='every_two_hours']
345
+ * @returns {Promise<void>}
346
+ */
347
+ async setOffset(offset, mode = 'every_two_hours') {
348
+ await this._writeReg(_REG_OFFSET, (mode === 'every_minute' ? 0x80 : 0) | (offset & 0x7F));
349
+ }
350
+
351
+ // -- Battery backup ---------------------------------------------------
352
+
353
+ /**
354
+ * Select the battery switch-over mode (PM[2:0]).
355
+ * @param {'standard'|'direct'|'disabled'} mode - `disabled` means VDD only (tie VBAT to VDD).
356
+ * @param {boolean} [lowDetection=true] - Enable battery-low detection.
357
+ * @returns {Promise<void>}
358
+ */
359
+ async configureBatteryBackup(mode, lowDetection = true) {
360
+ const pm = _PM_MODES[mode][lowDetection ? 0 : 1];
361
+ const ctrl3 = await this._readReg(_REG_CONTROL_3);
362
+ await this._writeControl3((ctrl3 & ~_C3_PM_MASK) | (pm << 5));
363
+ }
364
+
365
+ /**
366
+ * @returns {Promise<boolean>} BSF — a switch-over to VBAT occurred since it was last cleared.
367
+ */
368
+ async isBatterySwitchedOver() {
369
+ return ((await this._readReg(_REG_CONTROL_3)) & _C3_BSF) !== 0;
370
+ }
371
+
372
+ /**
373
+ * Clear BSF only, leaving PM and the enable bits unchanged.
374
+ * @returns {Promise<void>}
375
+ */
376
+ async clearBatterySwitchover() {
377
+ const ctrl3 = await this._readReg(_REG_CONTROL_3);
378
+ await this._writeReg(_REG_CONTROL_3, ctrl3 & (_C3_PM_MASK | _C3_BSIE | _C3_BLIE));
379
+ }
380
+
381
+ /**
382
+ * @returns {Promise<boolean>} BLF (read-only) — VBAT is below the detection threshold.
383
+ */
384
+ async isBatteryLow() {
385
+ return ((await this._readReg(_REG_CONTROL_3)) & _C3_BLF) !== 0;
386
+ }
387
+
388
+ /**
389
+ * @returns {Promise<boolean>} OS flag (bit 7 of SECONDS) — the time may be
390
+ * invalid; cleared by `setDatetime()`.
391
+ */
392
+ async oscillatorStopped() {
393
+ return ((await this._readReg(_REG_SECONDS)) & _SECONDS_OS) !== 0;
394
+ }
395
+
396
+ /**
397
+ * Send the software-reset sequence (0x58 to CONTROL_1). Resets all
398
+ * control/configuration registers to POR defaults — including PM=111
399
+ * (battery backup disabled) — but keeps the time/date/alarm/timer values.
400
+ * @returns {Promise<void>}
401
+ */
402
+ async softwareReset() {
403
+ await this._writeReg(_REG_CONTROL_1, 0x58);
404
+ }
405
+
406
+ // -- Interrupt API (Level 3) ------------------------------------------
407
+
408
+ /**
409
+ * Read CONTROL_2/CONTROL_3, clear the set CTAF/CTBF/SF/AF/BSF flags
410
+ * (WTAF/BLF are read-only; enable bits untouched), and return the
411
+ * pre-clear status mask — test with the `PCF8523Full.SOURCE_*` constants.
412
+ * @returns {Promise<number>}
413
+ */
414
+ async pollInterrupt() {
415
+ const raw = await this._readRegs(_REG_CONTROL_2, 2);
416
+ const ctrl2 = raw[0];
417
+ const ctrl3 = raw[1];
418
+ let status = 0;
419
+ if (ctrl2 & _C2_SF) status |= PCF8523Full.SOURCE_SECOND;
420
+ if (ctrl2 & (_C2_CTAF | _C2_WTAF)) status |= PCF8523Full.SOURCE_TIMER_A;
421
+ if (ctrl2 & _C2_CTBF) status |= PCF8523Full.SOURCE_TIMER_B;
422
+ if (ctrl2 & _C2_AF) status |= PCF8523Full.SOURCE_ALARM;
423
+ if (ctrl3 & _C3_BSF) status |= PCF8523Full.SOURCE_BATTERY_SWITCH;
424
+ if (ctrl3 & _C3_BLF) status |= PCF8523Full.SOURCE_BATTERY_LOW;
425
+ // Write 0 only to the flags seen set, 1 to the rest, so a flag that
426
+ // sets between the read and this write is not lost.
427
+ if (ctrl2 & _C2_CLEARABLE) {
428
+ await this._writeReg(_REG_CONTROL_2, (_C2_CLEARABLE & ~ctrl2) | (ctrl2 & _C2_ENABLES));
429
+ }
430
+ if (ctrl3 & _C3_BSF) {
431
+ await this._writeReg(_REG_CONTROL_3, ctrl3 & (_C3_PM_MASK | _C3_BSIE | _C3_BLIE));
432
+ }
433
+ return status;
434
+ }
435
+
436
+ /**
437
+ * Enable one or more interrupt sources. `SOURCE_TIMER_A` sets WTAIE or
438
+ * CTAIE depending on Timer A's configured mode — call
439
+ * `configureTimerA()` first.
440
+ * @param {number} source - Bitwise OR of `PCF8523Full.SOURCE_*`.
441
+ * @returns {Promise<void>}
442
+ */
443
+ async enableInterrupt(source) {
444
+ await this._setInterruptEnables(source, true);
445
+ }
446
+
447
+ /**
448
+ * Disable one or more interrupt sources.
449
+ * @param {number} source - Bitwise OR of `PCF8523Full.SOURCE_*`.
450
+ * @returns {Promise<void>}
451
+ */
452
+ async disableInterrupt(source) {
453
+ await this._setInterruptEnables(source, false);
454
+ }
455
+
456
+ async _setInterruptEnables(source, enable) {
457
+ const S = PCF8523Full;
458
+ if (source & (S.SOURCE_SECOND | S.SOURCE_ALARM)) {
459
+ const bits = (source & S.SOURCE_SECOND ? _C1_SIE : 0) | (source & S.SOURCE_ALARM ? _C1_AIE : 0);
460
+ const ctrl1 = await this._readControl1();
461
+ await this._writeReg(_REG_CONTROL_1, enable ? ctrl1 | bits : ctrl1 & ~bits);
462
+ }
463
+ if (source & (S.SOURCE_TIMER_A | S.SOURCE_TIMER_B)) {
464
+ let bits = 0;
465
+ if (source & S.SOURCE_TIMER_A) {
466
+ if (enable) {
467
+ const tac = (await this._readReg(_REG_TMR_CLKOUT_CTRL)) & _TMR_TAC_MASK;
468
+ bits |= tac === _TMR_TAC_WATCHDOG ? _C2_WTAIE : _C2_CTAIE;
469
+ } else {
470
+ bits |= _C2_WTAIE | _C2_CTAIE;
471
+ }
472
+ }
473
+ if (source & S.SOURCE_TIMER_B) bits |= _C2_CTBIE;
474
+ const enables = (await this._readReg(_REG_CONTROL_2)) & _C2_ENABLES;
475
+ await this._writeControl2(enable ? enables | bits : enables & ~bits);
476
+ }
477
+ if (source & (S.SOURCE_BATTERY_SWITCH | S.SOURCE_BATTERY_LOW)) {
478
+ const bits = (source & S.SOURCE_BATTERY_SWITCH ? _C3_BSIE : 0) | (source & S.SOURCE_BATTERY_LOW ? _C3_BLIE : 0);
479
+ const ctrl3 = await this._readReg(_REG_CONTROL_3);
480
+ await this._writeControl3(enable ? ctrl3 | bits : ctrl3 & ~bits);
481
+ }
482
+ }
483
+
484
+ /**
485
+ * Subscribe to interrupts. Uses `connection.intPin.onEdge()` when an
486
+ * INT-line InputPin is wired; falls back to a 5 ms polling loop
487
+ * otherwise. Call `disableClockOutput()` first if INT1 still carries
488
+ * CLKOUT.
489
+ * @param {function(number): void} callback - Called with the status mask (test against SOURCE_*).
490
+ * @returns {Promise<void>}
491
+ */
492
+ async onInterrupt(callback) {
493
+ this._callback = callback;
494
+ const dispatch = async () => {
495
+ const status = await this.pollInterrupt();
496
+ if (status && this._callback) this._callback(status);
497
+ };
498
+ if (this._conn.intPin) {
499
+ this._edgeHandler = dispatch;
500
+ await this._conn.intPin.onEdge(this._edgeHandler, 'falling');
501
+ } else {
502
+ this._pollTimer = setInterval(dispatch, 5);
503
+ }
504
+ }
505
+
506
+ /**
507
+ * Unsubscribe from interrupts.
508
+ * @returns {Promise<void>}
509
+ */
510
+ async offInterrupt() {
511
+ if (this._conn.intPin && this._edgeHandler) {
512
+ await this._conn.intPin.offEdge(this._edgeHandler);
513
+ this._edgeHandler = null;
514
+ }
515
+ if (this._pollTimer) {
516
+ clearInterval(this._pollTimer);
517
+ this._pollTimer = null;
518
+ }
519
+ this._callback = null;
520
+ }
521
+ }
522
+
523
+ /** Second tick (SF). */
524
+ PCF8523Full.SOURCE_SECOND = 0x01;
525
+ /** Timer A timed out (CTAF or WTAF). */
526
+ PCF8523Full.SOURCE_TIMER_A = 0x02;
527
+ /** Timer B timed out (CTBF). */
528
+ PCF8523Full.SOURCE_TIMER_B = 0x04;
529
+ /** All enabled alarm fields matched (AF). */
530
+ PCF8523Full.SOURCE_ALARM = 0x08;
531
+ /** Battery switch-over occurred (BSF). */
532
+ PCF8523Full.SOURCE_BATTERY_SWITCH = 0x10;
533
+ /** Battery low (BLF). */
534
+ PCF8523Full.SOURCE_BATTERY_LOW = 0x20;
535
+
536
+ /** Fixed 7-bit I²C address. */
537
+ PCF8523Minimal.I2C_ADDRESS = 0x68;
538
+
539
+ module.exports = { PCF8523Minimal, PCF8523Full };