periph 1.3.3 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/README.md +36 -0
  2. package/examples/adc_dac/ad5251/complete.js +66 -0
  3. package/examples/adc_dac/ad5251/demo.js +69 -0
  4. package/examples/adc_dac/ad5251/minimal.js +23 -0
  5. package/examples/adc_dac/ad5252/complete.js +66 -0
  6. package/examples/adc_dac/ad5252/demo.js +69 -0
  7. package/examples/adc_dac/ad5252/minimal.js +23 -0
  8. package/examples/adc_dac/ads7828/complete.js +46 -0
  9. package/examples/adc_dac/ads7828/demo.js +45 -0
  10. package/examples/adc_dac/ads7828/minimal.js +17 -0
  11. package/examples/adc_dac/cat5171/complete.js +52 -0
  12. package/examples/adc_dac/cat5171/demo.js +59 -0
  13. package/examples/adc_dac/cat5171/minimal.js +23 -0
  14. package/examples/adc_dac/cs43l22/complete.js +88 -0
  15. package/examples/adc_dac/cs43l22/demo.js +56 -0
  16. package/examples/adc_dac/cs43l22/minimal.js +20 -0
  17. package/examples/adc_dac/ds1881/complete.js +62 -0
  18. package/examples/adc_dac/ds1881/demo.js +61 -0
  19. package/examples/adc_dac/ds1881/minimal.js +25 -0
  20. package/examples/adc_dac/ds3502/complete.js +46 -0
  21. package/examples/adc_dac/ds3502/demo.js +54 -0
  22. package/examples/adc_dac/ds3502/minimal.js +28 -0
  23. package/examples/adc_dac/es8311/complete.js +80 -0
  24. package/examples/adc_dac/es8311/demo.js +59 -0
  25. package/examples/adc_dac/es8311/minimal.js +18 -0
  26. package/examples/adc_dac/mcp3422/complete.js +48 -0
  27. package/examples/adc_dac/mcp3422/demo.js +49 -0
  28. package/examples/adc_dac/mcp3422/minimal.js +25 -0
  29. package/examples/adc_dac/mcp4xxx/complete.js +97 -0
  30. package/examples/adc_dac/mcp4xxx/demo.js +76 -0
  31. package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
  32. package/examples/display/sh1106/complete.js +101 -0
  33. package/examples/display/sh1106/demo.js +68 -0
  34. package/examples/display/sh1106/minimal.js +25 -0
  35. package/examples/display/ssd1305/complete.js +129 -0
  36. package/examples/display/ssd1305/demo.js +71 -0
  37. package/examples/display/ssd1305/minimal.js +25 -0
  38. package/examples/display/ssd1306/complete.js +115 -0
  39. package/examples/display/ssd1306/demo.js +70 -0
  40. package/examples/display/ssd1306/minimal.js +25 -0
  41. package/examples/environmental/am2315/complete.js +34 -0
  42. package/examples/environmental/am2315/demo.js +46 -0
  43. package/examples/environmental/am2315/minimal.js +21 -0
  44. package/examples/humidity/htu21df/complete.js +49 -0
  45. package/examples/humidity/htu21df/demo.js +50 -0
  46. package/examples/humidity/htu21df/minimal.js +22 -0
  47. package/examples/humidity/sht31/complete.js +53 -0
  48. package/examples/humidity/sht31/demo.js +53 -0
  49. package/examples/humidity/sht31/minimal.js +21 -0
  50. package/examples/humidity/si7021/complete.js +55 -0
  51. package/examples/humidity/si7021/demo.js +58 -0
  52. package/examples/humidity/si7021/minimal.js +21 -0
  53. package/examples/imu/bno055/complete.js +85 -0
  54. package/examples/imu/bno055/demo.js +70 -0
  55. package/examples/imu/bno055/minimal.js +20 -0
  56. package/examples/imu/qmi8658a/complete.js +134 -0
  57. package/examples/imu/qmi8658a/demo.js +81 -0
  58. package/examples/imu/qmi8658a/minimal.js +22 -0
  59. package/examples/io_expander/pca6408a/complete.js +63 -0
  60. package/examples/io_expander/pca6408a/demo.js +58 -0
  61. package/examples/io_expander/pca6408a/minimal.js +20 -0
  62. package/examples/io_expander/pca6416a/complete.js +68 -0
  63. package/examples/io_expander/pca6416a/demo.js +60 -0
  64. package/examples/io_expander/pca6416a/minimal.js +20 -0
  65. package/examples/io_expander/pca9536/complete.js +63 -0
  66. package/examples/io_expander/pca9536/demo.js +57 -0
  67. package/examples/io_expander/pca9536/minimal.js +20 -0
  68. package/examples/io_expander/pca9539/complete.js +68 -0
  69. package/examples/io_expander/pca9539/demo.js +60 -0
  70. package/examples/io_expander/pca9539/minimal.js +20 -0
  71. package/examples/light/max44009/complete.js +56 -0
  72. package/examples/light/max44009/demo.js +49 -0
  73. package/examples/light/max44009/minimal.js +22 -0
  74. package/examples/memory/at24c02n/complete.js +39 -0
  75. package/examples/memory/at24c02n/demo.js +39 -0
  76. package/examples/memory/at24c02n/minimal.js +15 -0
  77. package/examples/memory/at24c64/complete.js +39 -0
  78. package/examples/memory/at24c64/demo.js +39 -0
  79. package/examples/memory/at24c64/minimal.js +15 -0
  80. package/examples/memory/cat24c512/complete.js +39 -0
  81. package/examples/memory/cat24c512/demo.js +39 -0
  82. package/examples/memory/cat24c512/minimal.js +15 -0
  83. package/examples/motor/tmc2130/complete.js +138 -0
  84. package/examples/motor/tmc2130/demo.js +96 -0
  85. package/examples/motor/tmc2130/minimal.js +30 -0
  86. package/examples/motor/tmc2208/complete.js +92 -0
  87. package/examples/motor/tmc2208/demo.js +85 -0
  88. package/examples/motor/tmc2208/minimal.js +25 -0
  89. package/examples/motor/tmc2209/complete.js +106 -0
  90. package/examples/motor/tmc2209/demo.js +97 -0
  91. package/examples/motor/tmc2209/minimal.js +25 -0
  92. package/examples/motor/tmc5160/complete.js +205 -0
  93. package/examples/motor/tmc5160/demo.js +104 -0
  94. package/examples/motor/tmc5160/minimal.js +35 -0
  95. package/examples/other/pca9541/complete.js +52 -0
  96. package/examples/other/pca9541/demo.js +61 -0
  97. package/examples/other/pca9541/minimal.js +23 -0
  98. package/examples/power/axp2101/complete.js +135 -0
  99. package/examples/power/axp2101/demo.js +68 -0
  100. package/examples/power/axp2101/minimal.js +24 -0
  101. package/examples/rtc/pcf85063a/complete.js +68 -0
  102. package/examples/rtc/pcf85063a/demo.js +76 -0
  103. package/examples/rtc/pcf85063a/minimal.js +20 -0
  104. package/examples/temperature/tmp102/complete.js +74 -0
  105. package/examples/temperature/tmp102/demo.js +71 -0
  106. package/examples/temperature/tmp102/minimal.js +20 -0
  107. package/examples/tof/vl6180x/complete.js +62 -0
  108. package/examples/tof/vl6180x/demo.js +77 -0
  109. package/examples/tof/vl6180x/minimal.js +23 -0
  110. package/package.json +1 -1
  111. package/src/chips/adc_dac/ad5251.js +345 -0
  112. package/src/chips/adc_dac/ad5252.js +345 -0
  113. package/src/chips/adc_dac/ads7828.js +225 -0
  114. package/src/chips/adc_dac/cat5171.js +213 -0
  115. package/src/chips/adc_dac/cs43l22.js +645 -0
  116. package/src/chips/adc_dac/ds1881.js +357 -0
  117. package/src/chips/adc_dac/ds3502.js +241 -0
  118. package/src/chips/adc_dac/es8311.js +621 -0
  119. package/src/chips/adc_dac/mcp3422.js +312 -0
  120. package/src/chips/adc_dac/mcp4xxx.js +982 -0
  121. package/src/chips/display/mono_oled.js +182 -0
  122. package/src/chips/display/sh1106.js +496 -0
  123. package/src/chips/display/ssd1305.js +655 -0
  124. package/src/chips/display/ssd1306.js +458 -0
  125. package/src/chips/environmental/am2315.js +237 -0
  126. package/src/chips/humidity/htu21df.js +222 -0
  127. package/src/chips/humidity/sht31.js +284 -0
  128. package/src/chips/humidity/si7021.js +282 -0
  129. package/src/chips/imu/bno055.js +496 -0
  130. package/src/chips/imu/qmi8658a.js +1011 -0
  131. package/src/chips/io_expander/pca6408a.js +407 -0
  132. package/src/chips/io_expander/pca6416a.js +455 -0
  133. package/src/chips/io_expander/pca9536.js +398 -0
  134. package/src/chips/io_expander/pca9539.js +457 -0
  135. package/src/chips/light/max44009.js +336 -0
  136. package/src/chips/memory/at24c02n.js +220 -0
  137. package/src/chips/memory/at24c64.js +220 -0
  138. package/src/chips/memory/cat24c512.js +221 -0
  139. package/src/chips/motor/tmc2130.js +537 -0
  140. package/src/chips/motor/tmc2208.js +490 -0
  141. package/src/chips/motor/tmc2209.js +565 -0
  142. package/src/chips/motor/tmc5160.js +900 -0
  143. package/src/chips/other/pca9541.js +315 -0
  144. package/src/chips/power/axp2101.js +968 -0
  145. package/src/chips/rtc/pcf85063a.js +482 -0
  146. package/src/chips/temperature/tmp102.js +361 -0
  147. package/src/chips/tof/vl6180x.js +580 -0
  148. package/src/connection/uart.js +9 -0
  149. package/src/connection/uart_mock.js +65 -0
  150. package/src/discovery/registry.js +85 -8
@@ -0,0 +1,482 @@
1
+ 'use strict';
2
+
3
+ const _REG_CONTROL_1 = 0x00;
4
+ const _REG_CONTROL_2 = 0x01;
5
+ const _REG_OFFSET = 0x02;
6
+ const _REG_RAM = 0x03;
7
+ const _REG_SECONDS = 0x04;
8
+ const _REG_SECOND_ALARM = 0x0B;
9
+ const _REG_TIMER_VALUE = 0x10;
10
+ const _REG_TIMER_MODE = 0x11;
11
+
12
+ // Control_1 (0x00) bits.
13
+ const _C1_STOP = 0x20;
14
+ const _C1_SR = 0x10;
15
+ const _C1_CIE = 0x04;
16
+ const _C1_12_24 = 0x02;
17
+ const _C1_CAP_SEL = 0x01;
18
+
19
+ // Control_2 (0x01) bits.
20
+ const _C2_AIE = 0x80;
21
+ const _C2_AF = 0x40;
22
+ const _C2_MI = 0x20;
23
+ const _C2_HMI = 0x10;
24
+ const _C2_TF = 0x08;
25
+ const _C2_COF = 0x07;
26
+ const _C2_FLAGS = 0x48; // AF|TF: write 0 clears, 1 keeps
27
+
28
+ // Timer_mode (0x11) bits.
29
+ const _TM_TCF_MASK = 0x18;
30
+ const _TM_TE = 0x04;
31
+ const _TM_TIE = 0x02;
32
+ const _TM_TI_TP = 0x01;
33
+
34
+ const _SECONDS_OS = 0x80;
35
+
36
+ const _SOURCE_CLOCKS = { '4096hz': 0x00, '64hz': 0x01, '1hz': 0x02, '1_60hz': 0x03 };
37
+
38
+ const _CLKOUT_COF = { 32768: 0, 16384: 1, 8192: 2, 4096: 3, 2048: 4, 1024: 5, 1: 6 };
39
+
40
+ function _bcdToInt(b) {
41
+ return ((b >> 4) & 0x0F) * 10 + (b & 0x0F);
42
+ }
43
+
44
+ function _intToBcd(v) {
45
+ return (Math.floor(v / 10) << 4) | (v % 10);
46
+ }
47
+
48
+ /**
49
+ * PCF85063A low-power I²C real-time clock and calendar (NXP) — minimal
50
+ * interface.
51
+ *
52
+ * Reads/writes the calendar clock with no configuration beyond the
53
+ * connection. `init()` forces 24-hour mode with the clock running (STOP=0);
54
+ * it writes no time register and does not reset the chip. Fixed I²C address
55
+ * 0x51. There is no battery switch-over — the chip is fed from a single VDD
56
+ * supply.
57
+ *
58
+ * `weekday` follows the datasheet's suggested assignment, 0=Sunday ..
59
+ * 6=Saturday — the Weekdays register has no hardware-enforced meaning.
60
+ */
61
+ class PCF85063AMinimal {
62
+ /**
63
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection at address 0x51.
64
+ */
65
+ constructor(connection) {
66
+ this._conn = connection;
67
+ }
68
+
69
+ async _writeReg(reg, value) {
70
+ await this._conn.writeReg(reg & 0xFF, value & 0xFF);
71
+ }
72
+
73
+ async _readReg(reg) {
74
+ return (await this._conn.readReg(reg & 0xFF, 1))[0];
75
+ }
76
+
77
+ async _readRegs(reg, n) {
78
+ return this._conn.readReg(reg & 0xFF, n);
79
+ }
80
+
81
+ async _readControl1() {
82
+ // SR always reads 0; mask it so a read-modify-write never resets.
83
+ return (await this._readReg(_REG_CONTROL_1)) & ~_C1_SR & 0xFF;
84
+ }
85
+
86
+ /**
87
+ * Confirm the device answers on the bus (plain Control_1 read — the
88
+ * PCF85063A has no identity register) and force 24-hour mode with
89
+ * STOP=0.
90
+ * @returns {Promise<void>}
91
+ */
92
+ async init() {
93
+ await this._readReg(_REG_CONTROL_1);
94
+ const ctrl1 = await this._readControl1();
95
+ await this._writeReg(_REG_CONTROL_1, ctrl1 & ~(_C1_STOP | _C1_12_24) & 0xFF);
96
+ }
97
+
98
+ /**
99
+ * Read the calendar clock (one 7-byte burst).
100
+ * @returns {Promise<{year: number, month: number, day: number, weekday: number, hour: number, minute: number, second: number}>}
101
+ * `year` is 2000-2099, `hour` is 0-23, `weekday` is 0=Sunday..6=Saturday.
102
+ */
103
+ async getDatetime() {
104
+ const raw = await this._readRegs(_REG_SECONDS, 7);
105
+ return {
106
+ year: 2000 + _bcdToInt(raw[6]),
107
+ month: _bcdToInt(raw[5] & 0x1F),
108
+ day: _bcdToInt(raw[3] & 0x3F),
109
+ weekday: raw[4] & 0x07,
110
+ hour: _bcdToInt(raw[2] & 0x3F),
111
+ minute: _bcdToInt(raw[1] & 0x7F),
112
+ second: _bcdToInt(raw[0] & 0x7F),
113
+ };
114
+ }
115
+
116
+ /**
117
+ * Set the calendar clock using the STOP-bit precision start: freeze the
118
+ * divider chain (STOP=1), write all seven time/date registers in one
119
+ * transaction, then release STOP. Forces 24-hour mode and clears the OS
120
+ * flag (the time is now known-good).
121
+ * @param {number} year - 2000-2099 (a RangeError is thrown otherwise).
122
+ * @param {number} month - 1-12.
123
+ * @param {number} day - 1-31.
124
+ * @param {number} weekday - 0=Sunday..6=Saturday.
125
+ * @param {number} hour - 0-23.
126
+ * @param {number} minute - 0-59.
127
+ * @param {number} second - 0-59.
128
+ * @returns {Promise<void>}
129
+ */
130
+ async setDatetime(year, month, day, weekday, hour, minute, second) {
131
+ if (year < 2000 || year > 2099) throw new RangeError('year must be 2000-2099');
132
+ const ctrl1 = (await this._readControl1()) & ~_C1_12_24 & 0xFF;
133
+ await this._writeReg(_REG_CONTROL_1, ctrl1 | _C1_STOP);
134
+ await this._conn.writeReg(_REG_SECONDS, Buffer.from([_intToBcd(second) & 0x7F, // OS = 0
135
+ _intToBcd(minute),
136
+ _intToBcd(hour) & 0x3F,
137
+ _intToBcd(day),
138
+ weekday & 0x07,
139
+ _intToBcd(month),
140
+ _intToBcd(year - 2000),]));
141
+ await this._writeReg(_REG_CONTROL_1, ctrl1 & ~_C1_STOP & 0xFF);
142
+ }
143
+ }
144
+
145
+ /**
146
+ * PCF85063A full interface — extends Minimal with the five-field alarm, the
147
+ * countdown timer, minute / half-minute interrupts, programmable CLKOUT,
148
+ * offset calibration, oscillator-load selection, oscillator-stop detection,
149
+ * clock stop/start, the free RAM byte, software reset and the interrupt API.
150
+ *
151
+ * The INT pin is open-drain and active-low; CLKOUT is a separate pin.
152
+ */
153
+ class PCF85063AFull extends PCF85063AMinimal {
154
+ constructor(connection) {
155
+ super(connection);
156
+ this._callback = null;
157
+ this._edgeHandler = null;
158
+ this._pollTimer = null;
159
+ }
160
+
161
+ async _updateControl1(clearMask, setBits) {
162
+ const ctrl1 = await this._readControl1();
163
+ await this._writeReg(_REG_CONTROL_1, (ctrl1 & ~clearMask) | setBits);
164
+ }
165
+
166
+ async _updateControl2(clearMask, setBits) {
167
+ // AF and TF are written 1 (unchanged) unless explicitly cleared
168
+ // through clearMask (AND semantics: write 0 clears, 1 keeps).
169
+ const reg = await this._readReg(_REG_CONTROL_2);
170
+ await this._writeReg(_REG_CONTROL_2, ((reg & ~clearMask) | setBits | (_C2_FLAGS & ~clearMask)) & 0xFF);
171
+ }
172
+
173
+ async _updateTimerMode(clearMask, setBits) {
174
+ const reg = await this._readReg(_REG_TIMER_MODE);
175
+ await this._writeReg(_REG_TIMER_MODE, ((reg & ~clearMask) | setBits) & 0x1F);
176
+ }
177
+
178
+ // -- Oscillator / clock control ---------------------------------------
179
+
180
+ /**
181
+ * @returns {Promise<boolean>} OS flag (bit 7 of Seconds) — the time may be
182
+ * invalid; cleared by `setDatetime()` or `clearOscillatorStop()`.
183
+ */
184
+ async oscillatorStopped() {
185
+ return ((await this._readReg(_REG_SECONDS)) & _SECONDS_OS) !== 0;
186
+ }
187
+
188
+ /**
189
+ * Clear OS, keeping the current seconds value. If `oscillatorStopped()`
190
+ * is still true afterwards the oscillator is not running.
191
+ * @returns {Promise<void>}
192
+ */
193
+ async clearOscillatorStop() {
194
+ const sec = await this._readReg(_REG_SECONDS);
195
+ await this._writeReg(_REG_SECONDS, sec & 0x7F);
196
+ }
197
+
198
+ /**
199
+ * Freeze the clock (STOP=1); the divider chain is held in reset.
200
+ * @returns {Promise<void>}
201
+ */
202
+ async stopClock() {
203
+ await this._updateControl1(0, _C1_STOP);
204
+ }
205
+
206
+ /**
207
+ * Release the clock (STOP=0).
208
+ * @returns {Promise<void>}
209
+ */
210
+ async startClock() {
211
+ await this._updateControl1(_C1_STOP, 0);
212
+ }
213
+
214
+ // -- Alarm ------------------------------------------------------------
215
+
216
+ /**
217
+ * Decode the alarm registers (0x0B-0x0F).
218
+ * @returns {Promise<{second: ?number, minute: ?number, hour: ?number, day: ?number, weekday: ?number}>}
219
+ * Each field is `null` when disabled (AEN_x=1).
220
+ */
221
+ async getAlarm() {
222
+ const raw = await this._readRegs(_REG_SECOND_ALARM, 5);
223
+ return {
224
+ second: raw[0] & 0x80 ? null : _bcdToInt(raw[0] & 0x7F),
225
+ minute: raw[1] & 0x80 ? null : _bcdToInt(raw[1] & 0x7F),
226
+ hour: raw[2] & 0x80 ? null : _bcdToInt(raw[2] & 0x3F),
227
+ day: raw[3] & 0x80 ? null : _bcdToInt(raw[3] & 0x3F),
228
+ weekday: raw[4] & 0x80 ? null : raw[4] & 0x07,
229
+ };
230
+ }
231
+
232
+ /**
233
+ * Write the alarm registers (0x0B-0x0F) and clear AF. A field left
234
+ * `null`/undefined is disabled; the alarm fires when every enabled field
235
+ * matches.
236
+ * @param {{second?: ?number, minute?: ?number, hour?: ?number, day?: ?number, weekday?: ?number}} [alarm={}]
237
+ * `weekday` is 0=Sunday..6=Saturday.
238
+ * @returns {Promise<void>}
239
+ */
240
+ async setAlarm({ second = null, minute = null, hour = null, day = null, weekday = null } = {}) {
241
+ await this._conn.writeReg(_REG_SECOND_ALARM, Buffer.from([second == null ? 0x80 : _intToBcd(second) & 0x7F,
242
+ minute == null ? 0x80 : _intToBcd(minute) & 0x7F,
243
+ hour == null ? 0x80 : _intToBcd(hour) & 0x3F,
244
+ day == null ? 0x80 : _intToBcd(day) & 0x3F,
245
+ weekday == null ? 0x80 : weekday & 0x07,]));
246
+ await this._updateControl2(_C2_AF, 0);
247
+ }
248
+
249
+ // -- Countdown timer --------------------------------------------------
250
+
251
+ /**
252
+ * Configure and start the countdown timer. Disables the timer first so
253
+ * `Timer_value` can be changed safely. Does not set TIE — call
254
+ * `enableInterrupt(SOURCE_TIMER)` to route it to INT.
255
+ * @param {number} value - Countdown value, 1-255.
256
+ * @param {'4096hz'|'64hz'|'1hz'|'1_60hz'} sourceClock
257
+ * @param {boolean} [pulsed=false] - Pulsed (true) or level (false, INT follows TF) interrupt.
258
+ * @returns {Promise<void>}
259
+ */
260
+ async configureTimer(value, sourceClock, pulsed = false) {
261
+ await this._updateTimerMode(_TM_TE, 0);
262
+ await this._writeReg(_REG_TIMER_VALUE, value);
263
+ await this._updateTimerMode(_TM_TCF_MASK | _TM_TI_TP,
264
+ (_SOURCE_CLOCKS[sourceClock] << 3) | (pulsed ? _TM_TI_TP : 0) | _TM_TE);
265
+ }
266
+
267
+ /**
268
+ * Stop the timer (TE=0, TIE=0) and select the 1/60 Hz clock for power saving.
269
+ * @returns {Promise<void>}
270
+ */
271
+ async disableTimer() {
272
+ await this._updateTimerMode(_TM_TE | _TM_TIE | _TM_TCF_MASK, _TM_TCF_MASK);
273
+ }
274
+
275
+ /**
276
+ * Read the live countdown value; reads until two consecutive reads agree.
277
+ * @returns {Promise<number>} Current counter value, 0-255 (not the loaded one).
278
+ */
279
+ async readTimer() {
280
+ let prev = await this._readReg(_REG_TIMER_VALUE);
281
+ for (;;) {
282
+ const cur = await this._readReg(_REG_TIMER_VALUE);
283
+ if (cur === prev) return cur;
284
+ prev = cur;
285
+ }
286
+ }
287
+
288
+ /**
289
+ * Select the minute / half-minute interrupt (requires offset mode `normal`).
290
+ * @param {'off'|'minute'|'half_minute'} mode
291
+ * @returns {Promise<void>}
292
+ */
293
+ async setMinuteInterrupt(mode) {
294
+ const bits = mode === 'minute' ? _C2_MI : mode === 'half_minute' ? _C2_HMI : 0;
295
+ await this._updateControl2(_C2_MI | _C2_HMI, bits);
296
+ }
297
+
298
+ // -- CLKOUT -----------------------------------------------------------
299
+
300
+ /**
301
+ * Drive CLKOUT.
302
+ * @param {number} frequencyHz - One of 32768, 16384, 8192, 4096, 2048, 1024, 1.
303
+ * @returns {Promise<void>}
304
+ */
305
+ async setClockOutput(frequencyHz) {
306
+ const cof = frequencyHz in _CLKOUT_COF ? _CLKOUT_COF[frequencyHz] : 7;
307
+ await this._updateControl2(_C2_COF, cof);
308
+ }
309
+
310
+ /**
311
+ * Disable CLKOUT (COF=111, pin low) to save power.
312
+ * @returns {Promise<void>}
313
+ */
314
+ async disableClockOutput() {
315
+ await this._updateControl2(_C2_COF, _C2_COF);
316
+ }
317
+
318
+ // -- Offset -----------------------------------------------------------
319
+
320
+ /**
321
+ * Read the clock-offset calibration register.
322
+ * @returns {Promise<{offset: number, mode: 'normal'|'course'}>}
323
+ * `offset` is -64..+63 LSB (4.34 ppm/LSB normal, 4.069 ppm/LSB course).
324
+ */
325
+ async getOffset() {
326
+ const raw = await this._readReg(_REG_OFFSET);
327
+ let offset = raw & 0x7F;
328
+ if (offset & 0x40) offset -= 128;
329
+ return { offset, mode: raw & 0x80 ? 'course' : 'normal' };
330
+ }
331
+
332
+ /**
333
+ * Write the clock-offset calibration register.
334
+ * @param {number} offset - Two's-complement correction, -64 to 63 LSB.
335
+ * @param {'normal'|'course'} [mode='normal'] - Correction every two hours / every four minutes.
336
+ * @returns {Promise<void>}
337
+ */
338
+ async setOffset(offset, mode = 'normal') {
339
+ await this._writeReg(_REG_OFFSET, (mode === 'course' ? 0x80 : 0) | (offset & 0x7F));
340
+ }
341
+
342
+ /**
343
+ * Select the oscillator load capacitance (CAP_SEL).
344
+ * @param {number} picofarads - 7 or 12.5; match the crystal's specified load.
345
+ * @returns {Promise<void>}
346
+ */
347
+ async setOscillatorLoad(picofarads) {
348
+ if (picofarads !== 7 && picofarads !== 12.5) throw new RangeError('picofarads must be 7 or 12.5');
349
+ await this._updateControl1(_C1_CAP_SEL, picofarads === 12.5 ? _C1_CAP_SEL : 0);
350
+ }
351
+
352
+ // -- RAM / reset ------------------------------------------------------
353
+
354
+ /**
355
+ * @returns {Promise<number>} The free general-purpose RAM byte, 0-255.
356
+ */
357
+ async readRam() {
358
+ return this._readReg(_REG_RAM);
359
+ }
360
+
361
+ /**
362
+ * Write the free general-purpose RAM byte.
363
+ * @param {number} value - 0-255.
364
+ * @returns {Promise<void>}
365
+ */
366
+ async writeRam(value) {
367
+ await this._writeReg(_REG_RAM, value);
368
+ }
369
+
370
+ /**
371
+ * Send the software-reset sequence (0x58 to Control_1). Resets ALL
372
+ * registers including the time (2000-01-01 00:00:00, OS=1) — run it
373
+ * before `setDatetime()`, never as a routine.
374
+ * @returns {Promise<void>}
375
+ */
376
+ async softwareReset() {
377
+ await this._writeReg(_REG_CONTROL_1, 0x58);
378
+ }
379
+
380
+ // -- Interrupt API ----------------------------------------------------
381
+
382
+ /**
383
+ * Read Control_2, clear the set AF/TF flags (enable bits and COF
384
+ * preserved), and return the pre-clear status mask — `SOURCE_ALARM`
385
+ * and/or `SOURCE_TIMER` (timer, minute and half-minute all report as
386
+ * `SOURCE_TIMER`).
387
+ * @returns {Promise<number>}
388
+ */
389
+ async pollInterrupt() {
390
+ const ctrl2 = await this._readReg(_REG_CONTROL_2);
391
+ let status = 0;
392
+ if (ctrl2 & _C2_AF) status |= PCF85063AFull.SOURCE_ALARM;
393
+ if (ctrl2 & _C2_TF) status |= PCF85063AFull.SOURCE_TIMER;
394
+ if (status) {
395
+ // Write 0 only to the flags seen set, 1 to the others, so a flag
396
+ // that sets between the read and this write is not lost.
397
+ await this._writeReg(_REG_CONTROL_2, (ctrl2 & ~_C2_FLAGS) | (_C2_FLAGS & ~ctrl2));
398
+ }
399
+ return status;
400
+ }
401
+
402
+ /**
403
+ * Enable one or more interrupt sources (AIE, TIE, MI, HMI, CIE).
404
+ * @param {number} source - Bitwise OR of `PCF85063AFull.SOURCE_*`.
405
+ * @returns {Promise<void>}
406
+ */
407
+ async enableInterrupt(source) {
408
+ await this._setInterruptEnables(source, true);
409
+ }
410
+
411
+ /**
412
+ * Disable one or more interrupt sources.
413
+ * @param {number} source - Bitwise OR of `PCF85063AFull.SOURCE_*`.
414
+ * @returns {Promise<void>}
415
+ */
416
+ async disableInterrupt(source) {
417
+ await this._setInterruptEnables(source, false);
418
+ }
419
+
420
+ async _setInterruptEnables(source, enable) {
421
+ const S = PCF85063AFull;
422
+ const bits2 = (source & S.SOURCE_ALARM ? _C2_AIE : 0)
423
+ | (source & S.SOURCE_MINUTE ? _C2_MI : 0)
424
+ | (source & S.SOURCE_HALF_MINUTE ? _C2_HMI : 0);
425
+ if (bits2) await this._updateControl2(enable ? 0 : bits2, enable ? bits2 : 0);
426
+ if (source & S.SOURCE_TIMER) await this._updateTimerMode(enable ? 0 : _TM_TIE, enable ? _TM_TIE : 0);
427
+ if (source & S.SOURCE_CORRECTION) await this._updateControl1(enable ? 0 : _C1_CIE, enable ? _C1_CIE : 0);
428
+ }
429
+
430
+ /**
431
+ * Subscribe to interrupts. Uses `connection.intPin.onEdge()` when an
432
+ * INT-line InputPin is wired; falls back to a 5 ms polling loop
433
+ * otherwise.
434
+ * @param {function(number): void} callback - Called with the status mask (SOURCE_ALARM / SOURCE_TIMER).
435
+ * @returns {Promise<void>}
436
+ */
437
+ async onInterrupt(callback) {
438
+ this._callback = callback;
439
+ const dispatch = async () => {
440
+ const status = await this.pollInterrupt();
441
+ if (status && this._callback) this._callback(status);
442
+ };
443
+ if (this._conn.intPin) {
444
+ this._edgeHandler = dispatch;
445
+ await this._conn.intPin.onEdge(this._edgeHandler, 'falling');
446
+ } else {
447
+ this._pollTimer = setInterval(dispatch, 5);
448
+ }
449
+ }
450
+
451
+ /**
452
+ * Unsubscribe from interrupts.
453
+ * @returns {Promise<void>}
454
+ */
455
+ async offInterrupt() {
456
+ if (this._conn.intPin && this._edgeHandler) {
457
+ await this._conn.intPin.offEdge(this._edgeHandler);
458
+ this._edgeHandler = null;
459
+ }
460
+ if (this._pollTimer) {
461
+ clearInterval(this._pollTimer);
462
+ this._pollTimer = null;
463
+ }
464
+ this._callback = null;
465
+ }
466
+ }
467
+
468
+ /** Alarm matched (AF). */
469
+ PCF85063AFull.SOURCE_ALARM = 0x01;
470
+ /** Countdown timer expired (TF). */
471
+ PCF85063AFull.SOURCE_TIMER = 0x02;
472
+ /** Minute interrupt (enable only; reported as SOURCE_TIMER). */
473
+ PCF85063AFull.SOURCE_MINUTE = 0x04;
474
+ /** Half-minute interrupt (enable only; reported as SOURCE_TIMER). */
475
+ PCF85063AFull.SOURCE_HALF_MINUTE = 0x08;
476
+ /** Offset-correction pulse (enable only, no flag). */
477
+ PCF85063AFull.SOURCE_CORRECTION = 0x10;
478
+
479
+ /** Fixed 7-bit I²C address. */
480
+ PCF85063AMinimal.I2C_ADDRESS = 0x51;
481
+
482
+ module.exports = { PCF85063AMinimal, PCF85063AFull };