periph 1.6.0 → 1.6.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 (47) hide show
  1. package/README.md +11 -0
  2. package/examples/gas/rrh62000/complete.js +74 -0
  3. package/examples/gas/rrh62000/demo.js +87 -0
  4. package/examples/gas/rrh62000/minimal.js +22 -0
  5. package/examples/gas/scd30/complete.js +73 -0
  6. package/examples/gas/scd30/demo.js +102 -0
  7. package/examples/gas/scd30/minimal.js +20 -0
  8. package/examples/gas/scd40/complete.js +76 -0
  9. package/examples/gas/scd40/demo.js +109 -0
  10. package/examples/gas/scd40/minimal.js +20 -0
  11. package/examples/light/tsl2561/complete.js +74 -0
  12. package/examples/light/tsl2561/demo.js +46 -0
  13. package/examples/light/tsl2561/minimal.js +22 -0
  14. package/examples/light/tsl2591/complete.js +90 -0
  15. package/examples/light/tsl2591/demo.js +119 -0
  16. package/examples/light/tsl2591/minimal.js +22 -0
  17. package/examples/light/vcnl4040/complete.js +102 -0
  18. package/examples/light/vcnl4040/demo.js +125 -0
  19. package/examples/light/vcnl4040/minimal.js +25 -0
  20. package/examples/light/vcnl4200/complete.js +101 -0
  21. package/examples/light/vcnl4200/demo.js +126 -0
  22. package/examples/light/vcnl4200/minimal.js +25 -0
  23. package/examples/rfid/st25dv/complete.js +94 -0
  24. package/examples/rfid/st25dv/demo.js +121 -0
  25. package/examples/rfid/st25dv/minimal.js +21 -0
  26. package/examples/rtc/bq32000/complete.js +53 -0
  27. package/examples/rtc/bq32000/demo.js +77 -0
  28. package/examples/rtc/bq32000/minimal.js +20 -0
  29. package/examples/rtc/ds1307/complete.js +54 -0
  30. package/examples/rtc/ds1307/demo.js +96 -0
  31. package/examples/rtc/ds1307/minimal.js +20 -0
  32. package/examples/rtc/ds1371/complete.js +53 -0
  33. package/examples/rtc/ds1371/demo.js +85 -0
  34. package/examples/rtc/ds1371/minimal.js +25 -0
  35. package/package.json +1 -1
  36. package/src/chips/gas/rrh62000.js +418 -0
  37. package/src/chips/gas/scd30.js +368 -0
  38. package/src/chips/gas/scd40.js +451 -0
  39. package/src/chips/light/tsl2561.js +445 -0
  40. package/src/chips/light/tsl2591.js +561 -0
  41. package/src/chips/light/vcnl4040.js +576 -0
  42. package/src/chips/light/vcnl4200.js +592 -0
  43. package/src/chips/rfid/st25dv.js +628 -0
  44. package/src/chips/rtc/bq32000.js +334 -0
  45. package/src/chips/rtc/ds1307.js +327 -0
  46. package/src/chips/rtc/ds1371.js +453 -0
  47. package/src/discovery/registry.js +11 -11
@@ -0,0 +1,334 @@
1
+ 'use strict';
2
+
3
+ const _REG_SECONDS = 0x00;
4
+ const _REG_MINUTES = 0x01;
5
+ const _REG_CENT_HOURS = 0x02;
6
+ const _REG_CAL_CFG1 = 0x07;
7
+ const _REG_TCH2 = 0x08;
8
+ const _REG_CFG2 = 0x09;
9
+ const _REG_SF_KEY1 = 0x20;
10
+ const _REG_SF_KEY2 = 0x21;
11
+ const _REG_SFR = 0x22;
12
+
13
+ const _SF_KEY1 = 0x5E;
14
+ const _SF_KEY2 = 0xC7;
15
+
16
+ const _SECONDS_STOP = 0x80;
17
+ const _MINUTES_OF = 0x80;
18
+ const _HOURS_CENT_EN = 0x80;
19
+ const _HOURS_CENT = 0x40;
20
+
21
+ const _CFG1_OUT = 0x80;
22
+ const _CFG1_FT = 0x40;
23
+ const _CFG1_S = 0x20;
24
+ const _CFG1_CAL = 0x1F;
25
+
26
+ const _TCH2_SWITCH = 0x20;
27
+ const _CFG2_TCFE = 0x40;
28
+ const _CFG2_MASK = 0x4F; // TCFE | TCHE[3:0]
29
+ const _TCHE_ACTIVE = 0x05;
30
+ const _TCHE_INACTIVE = 0x0A;
31
+
32
+ const _SFR_FTF = 0x01;
33
+
34
+ // ppm per CAL step: S = 0 slows the RTC (+), S = 1 speeds it up (-).
35
+ const _PPM_PER_STEP_SLOW = 1e6 / 491520;
36
+ const _PPM_PER_STEP_FAST = 1e6 / 245760;
37
+
38
+ /** Lowest accepted setCalibration() value in ppm (reachable -126.1 plus rounding margin). */
39
+ const CALIBRATION_MIN_PPM = -126.5;
40
+ /** Highest accepted setCalibration() value in ppm (reachable +63.1 plus rounding margin). */
41
+ const CALIBRATION_MAX_PPM = 63.5;
42
+
43
+ /** Fixed 7-bit I²C address. */
44
+ const I2C_ADDRESS = 0x68;
45
+
46
+ function _bcdToInt(b) {
47
+ return ((b >> 4) & 0x0F) * 10 + (b & 0x0F);
48
+ }
49
+
50
+ function _intToBcd(v) {
51
+ return (Math.floor(v / 10) << 4) | (v % 10);
52
+ }
53
+
54
+ /**
55
+ * Convert a CAL_CFG1 register value into the calibration adjustment.
56
+ * @param {number} calCfg1 - CAL_CFG1 byte (only S and CAL are used).
57
+ * @returns {number} Adjustment in ppm (positive slows the RTC, negative speeds it up).
58
+ */
59
+ function calibrationToPpm(calCfg1) {
60
+ const cal = calCfg1 & _CFG1_CAL;
61
+ return (calCfg1 & _CFG1_S) ? -cal * _PPM_PER_STEP_FAST : cal * _PPM_PER_STEP_SLOW;
62
+ }
63
+
64
+ /**
65
+ * Pick the S bit and CAL step for the nearest achievable adjustment.
66
+ * @param {number} ppm - Requested adjustment, -126.5..+63.5 (positive slows the RTC).
67
+ * @returns {number} S and CAL bits (`S << 5 | CAL`) for CAL_CFG1.
68
+ * @throws {RangeError} If ppm is outside the accepted range.
69
+ */
70
+ function ppmToCalibration(ppm) {
71
+ if (!(ppm >= CALIBRATION_MIN_PPM && ppm <= CALIBRATION_MAX_PPM)) {
72
+ throw new RangeError('ppm must be -126.5..+63.5');
73
+ }
74
+ if (ppm >= 0) return Math.min(Math.floor(ppm / _PPM_PER_STEP_SLOW + 0.5), 31);
75
+ const cal = Math.min(Math.floor(-ppm / _PPM_PER_STEP_FAST + 0.5), 31);
76
+ return cal ? (_CFG1_S | cal) : 0;
77
+ }
78
+
79
+ /**
80
+ * BQ32000 real-time clock with trickle charger and backup switchover (Texas Instruments) —
81
+ * minimal interface.
82
+ *
83
+ * Calendar clock (seconds..year, BCD, 24-hour only) for a 32.768 kHz crystal. `init()` reads the
84
+ * seven clock registers once to confirm the chip answers and writes nothing. Fixed I²C address
85
+ * 0x68 (shared with the DS3231, PCF8523 and others — there is no identity register).
86
+ *
87
+ * `weekday` is 0=Sunday..6=Saturday (the chip stores 1..7). Registers 0x00-0x09 cannot be
88
+ * accessed while the chip runs from VBACK; reads then reject with the bus error.
89
+ */
90
+ class BQ32000Minimal {
91
+ /**
92
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection at address 0x68.
93
+ */
94
+ constructor(connection) {
95
+ this._conn = connection;
96
+ }
97
+
98
+ async _writeReg(reg, value) {
99
+ await this._conn.writeReg(reg & 0xFF, value & 0xFF);
100
+ }
101
+
102
+ async _readReg(reg) {
103
+ return (await this._conn.readReg(reg & 0xFF, 1))[0];
104
+ }
105
+
106
+ async _update(reg, clearMask, setBits) {
107
+ const value = await this._readReg(reg);
108
+ await this._writeReg(reg, (value & ~clearMask & 0xFF) | setBits);
109
+ }
110
+
111
+ /**
112
+ * Confirm the device answers by reading the clock registers (no writes — the BQ32000 has
113
+ * no identity register).
114
+ * @returns {Promise<void>}
115
+ */
116
+ async init() {
117
+ await this._conn.readReg(_REG_SECONDS, 7);
118
+ }
119
+
120
+ /**
121
+ * Read the calendar clock (one 7-byte burst from 0x00). The registers are returned as read
122
+ * (century bit ignored); check `oscillatorFailed()` (Full) to know whether the time is valid.
123
+ * @returns {Promise<{year: number, month: number, day: number, weekday: number, hour: number, minute: number, second: number}>}
124
+ * `year` is 2000-2099, `hour` is 0-23, `weekday` is 0=Sunday..6=Saturday.
125
+ */
126
+ async getDatetime() {
127
+ const raw = await this._conn.readReg(_REG_SECONDS, 7);
128
+ return {
129
+ year: 2000 + _bcdToInt(raw[6]),
130
+ month: _bcdToInt(raw[5] & 0x1F),
131
+ day: _bcdToInt(raw[4] & 0x3F),
132
+ weekday: ((raw[3] & 0x07) + 6) % 7,
133
+ hour: _bcdToInt(raw[2] & 0x3F),
134
+ minute: _bcdToInt(raw[1] & 0x7F),
135
+ second: _bcdToInt(raw[0] & 0x7F),
136
+ };
137
+ }
138
+
139
+ /**
140
+ * Write the calendar clock in one 7-byte transaction starting at 0x00, with STOP = 0,
141
+ * OF = 0 (clears the oscillator-fail flag) and CENT_EN = CENT = 0 (century tracking off).
142
+ * @param {number} year - 2000-2099.
143
+ * @param {number} month - 1-12.
144
+ * @param {number} day - 1-31.
145
+ * @param {number} weekday - 0=Sunday..6=Saturday (stored as 1-7).
146
+ * @param {number} hour - 0-23.
147
+ * @param {number} minute - 0-59.
148
+ * @param {number} second - 0-59.
149
+ * @returns {Promise<void>}
150
+ * @throws {RangeError} If year is outside 2000-2099 or weekday outside 0-6.
151
+ */
152
+ async setDatetime(year, month, day, weekday, hour, minute, second) {
153
+ if (year < 2000 || year > 2099) throw new RangeError('year must be 2000-2099');
154
+ if (weekday < 0 || weekday > 6) throw new RangeError('weekday must be 0-6');
155
+ await this._conn.writeReg(_REG_SECONDS, Buffer.from([
156
+ _intToBcd(second) & 0x7F,
157
+ _intToBcd(minute) & 0x7F,
158
+ _intToBcd(hour) & 0x3F,
159
+ weekday + 1,
160
+ _intToBcd(day) & 0x3F,
161
+ _intToBcd(month) & 0x1F,
162
+ _intToBcd(year - 2000),
163
+ ]));
164
+ }
165
+ }
166
+
167
+ /**
168
+ * BQ32000 full interface — extends Minimal with the oscillator-fail flag, oscillator
169
+ * stop/start, the century bits, ppm calibration, the IRQ level and calibration test signal,
170
+ * the trickle charger and raw register access.
171
+ *
172
+ * IRQ is an open-drain output, not an interrupt: the chip has no alarms or timers. Its pull-up
173
+ * must go to VCC.
174
+ */
175
+ class BQ32000Full extends BQ32000Minimal {
176
+ /**
177
+ * Read the oscillator-fail flag (MINUTES.OF).
178
+ * @returns {Promise<boolean>} True after the first power-up or an oscillator interruption —
179
+ * the time is invalid until `setDatetime()` or `clearOscillatorFail()`.
180
+ */
181
+ async oscillatorFailed() {
182
+ return ((await this._readReg(_REG_MINUTES)) & _MINUTES_OF) !== 0;
183
+ }
184
+
185
+ /** Clear OF (read-modify-write of MINUTES, keeping the minutes value). @returns {Promise<void>} */
186
+ async clearOscillatorFail() {
187
+ await this._update(_REG_MINUTES, _MINUTES_OF, 0);
188
+ }
189
+
190
+ /** Stop the oscillator (SECONDS.STOP = 1); the clock no longer advances. @returns {Promise<void>} */
191
+ async stopOscillator() {
192
+ await this._update(_REG_SECONDS, 0, _SECONDS_STOP);
193
+ }
194
+
195
+ /**
196
+ * Start the oscillator (SECONDS.STOP = 0). After the very first power application call
197
+ * `stopOscillator()` then `startOscillator()` once to make sure the oscillator runs.
198
+ * @returns {Promise<void>}
199
+ */
200
+ async startOscillator() {
201
+ await this._update(_REG_SECONDS, _SECONDS_STOP, 0);
202
+ }
203
+
204
+ /**
205
+ * Write the century bits of CENT_HOURS, keeping the hours.
206
+ * @param {boolean} enable - True enables century tracking (CENT toggles on 99 -> 00).
207
+ * @param {number} [century=0] - Century bit 0 or 1 (meaning is user-defined).
208
+ * @returns {Promise<void>}
209
+ */
210
+ async setCentury(enable, century = 0) {
211
+ const bits = (enable ? _HOURS_CENT_EN : 0) | (century ? _HOURS_CENT : 0);
212
+ await this._update(_REG_CENT_HOURS, _HOURS_CENT_EN | _HOURS_CENT, bits);
213
+ }
214
+
215
+ /**
216
+ * Read the century bits.
217
+ * @returns {Promise<{enabled: boolean, century: number}>}
218
+ */
219
+ async getCentury() {
220
+ const reg = await this._readReg(_REG_CENT_HOURS);
221
+ return { enabled: (reg & _HOURS_CENT_EN) !== 0, century: (reg & _HOURS_CENT) ? 1 : 0 };
222
+ }
223
+
224
+ /**
225
+ * Set the calibration to the nearest achievable adjustment; OUT and FT are kept. Positive
226
+ * values slow the RTC (S = 0, ~2.03 ppm per step, up to +63.1), negative values speed it up
227
+ * (S = 1, ~4.07 ppm per step, down to -126.1).
228
+ * @param {number} ppm - Adjustment in ppm, -126.5..+63.5.
229
+ * @returns {Promise<void>}
230
+ * @throws {RangeError} If ppm is outside the accepted range.
231
+ */
232
+ async setCalibration(ppm) {
233
+ const bits = ppmToCalibration(ppm);
234
+ await this._update(_REG_CAL_CFG1, _CFG1_S | _CFG1_CAL, bits);
235
+ }
236
+
237
+ /**
238
+ * Read the current calibration adjustment.
239
+ * @returns {Promise<number>} Adjustment in ppm (positive slows the RTC).
240
+ */
241
+ async getCalibration() {
242
+ return calibrationToPpm(await this._readReg(_REG_CAL_CFG1));
243
+ }
244
+
245
+ /**
246
+ * Drive IRQ as a static level (FT = 0, OUT = level).
247
+ * @param {boolean} level - True releases the open-drain output (high via the pull-up),
248
+ * false pulls it low.
249
+ * @returns {Promise<void>}
250
+ */
251
+ async setIrq(level) {
252
+ await this._update(_REG_CAL_CFG1, _CFG1_FT | _CFG1_OUT, level ? _CFG1_OUT : 0);
253
+ }
254
+
255
+ /**
256
+ * Output the calibration test signal on IRQ, or turn it off. 512 Hz excludes the ppm
257
+ * adjustment; 1 Hz includes it. SFR.FTF is written through the special-function key
258
+ * sequence (0x5E to 0x20, 0xC7 to 0x21).
259
+ * @param {number|null} hz - 512, 1, or null to return IRQ to the OUT level.
260
+ * @returns {Promise<void>}
261
+ * @throws {RangeError} For other values.
262
+ */
263
+ async setCalibrationOutput(hz) {
264
+ if (hz === null || hz === undefined) {
265
+ await this._update(_REG_CAL_CFG1, _CFG1_FT, 0);
266
+ return;
267
+ }
268
+ if (hz !== 512 && hz !== 1) throw new RangeError('hz must be 512, 1 or null');
269
+ await this._writeSfr(hz === 1 ? _SFR_FTF : 0);
270
+ await this._update(_REG_CAL_CFG1, 0, _CFG1_FT);
271
+ }
272
+
273
+ async _writeSfr(value) {
274
+ await this._writeReg(_REG_SF_KEY1, _SF_KEY1);
275
+ await this._writeReg(_REG_SF_KEY2, _SF_KEY2);
276
+ await this._writeReg(_REG_SFR, value);
277
+ }
278
+
279
+ /**
280
+ * Enable the trickle charger (TCHE = 0x5, TCH2 = 1, TCFE = fetBypass). Only for a supercap or
281
+ * a suitable rechargeable cell — never a primary coin cell. Reserved bits of TCH2 and CFG2
282
+ * are preserved.
283
+ * @param {boolean} [fetBypass=false] - True also closes the FET bypass switch (TCFE).
284
+ * @returns {Promise<void>}
285
+ */
286
+ async enableTrickleCharge(fetBypass = false) {
287
+ await this._update(_REG_CFG2, _CFG2_MASK, _TCHE_ACTIVE | (fetBypass ? _CFG2_TCFE : 0));
288
+ await this._update(_REG_TCH2, 0, _TCH2_SWITCH);
289
+ }
290
+
291
+ /** Disable the trickle charger (TCHE = 0xA, TCH2 = 0, TCFE = 0). @returns {Promise<void>} */
292
+ async disableTrickleCharge() {
293
+ await this._update(_REG_CFG2, _CFG2_MASK, _TCHE_INACTIVE);
294
+ await this._update(_REG_TCH2, _TCH2_SWITCH, 0);
295
+ }
296
+
297
+ /**
298
+ * Read one register.
299
+ * @param {number} reg - Register address 0x00-0x09.
300
+ * @returns {Promise<number>} Register value 0-255.
301
+ * @throws {RangeError} If reg is outside 0x00-0x09.
302
+ */
303
+ async readRegister(reg) {
304
+ BQ32000Full._checkReg(reg);
305
+ return this._readReg(reg);
306
+ }
307
+
308
+ /**
309
+ * Write one register. For TCH2 (0x08) only bit 5 and for CFG2 (0x09) only bits 6 and 3:0
310
+ * are taken from value; the reserved bits keep their read values.
311
+ * @param {number} reg - Register address 0x00-0x09.
312
+ * @param {number} value - Register value 0-255.
313
+ * @returns {Promise<void>}
314
+ * @throws {RangeError} If reg is outside 0x00-0x09.
315
+ */
316
+ async writeRegister(reg, value) {
317
+ BQ32000Full._checkReg(reg);
318
+ if (reg === _REG_TCH2) await this._update(reg, _TCH2_SWITCH, value & _TCH2_SWITCH);
319
+ else if (reg === _REG_CFG2) await this._update(reg, _CFG2_MASK, value & _CFG2_MASK);
320
+ else await this._writeReg(reg, value);
321
+ }
322
+
323
+ static _checkReg(reg) {
324
+ if (!(reg >= 0x00 && reg <= _REG_CFG2)) throw new RangeError('reg must be 0x00-0x09');
325
+ }
326
+ }
327
+
328
+ /** Fixed 7-bit I²C address. */
329
+ BQ32000Minimal.I2C_ADDRESS = I2C_ADDRESS;
330
+
331
+ module.exports = {
332
+ BQ32000Minimal, BQ32000Full, I2C_ADDRESS,
333
+ CALIBRATION_MIN_PPM, CALIBRATION_MAX_PPM, calibrationToPpm, ppmToCalibration,
334
+ };
@@ -0,0 +1,327 @@
1
+ 'use strict';
2
+
3
+ const _REG_SECONDS = 0x00;
4
+ const _REG_HOURS = 0x02;
5
+ const _REG_DAY = 0x03;
6
+ const _REG_CONTROL = 0x07;
7
+ const _REG_RAM = 0x08;
8
+
9
+ const _SECONDS_CH = 0x80;
10
+ const _HOURS_12 = 0x40;
11
+ const _HOURS_PM = 0x20;
12
+
13
+ const _CONTROL_OUT = 0x80;
14
+ const _CONTROL_SQWE = 0x10;
15
+ const _CONTROL_RS = 0x03;
16
+
17
+ const _SQW_RATES = { 1: 0x00, 4096: 0x01, 8192: 0x02, 32768: 0x03 };
18
+
19
+ /** Fixed 7-bit I²C address. */
20
+ const I2C_ADDRESS = 0x68;
21
+
22
+ /** Size of the battery-backed SRAM in bytes (registers 0x08-0x3F). */
23
+ const RAM_SIZE = 56;
24
+
25
+ function _bcdToInt(b) {
26
+ return ((b >> 4) & 0x0F) * 10 + (b & 0x0F);
27
+ }
28
+
29
+ function _intToBcd(v) {
30
+ return (Math.floor(v / 10) << 4) | (v % 10);
31
+ }
32
+
33
+ function _decodeHour(raw) {
34
+ if (raw & _HOURS_12) return (_bcdToInt(raw & 0x1F) % 12) + ((raw & _HOURS_PM) ? 12 : 0);
35
+ return _bcdToInt(raw & 0x3F);
36
+ }
37
+
38
+ function _encodeHour(hour, hour12) {
39
+ if (!hour12) return _intToBcd(hour);
40
+ return _HOURS_12 | (hour >= 12 ? _HOURS_PM : 0) | _intToBcd(hour % 12 || 12);
41
+ }
42
+
43
+ function _check(name, value, low, high) {
44
+ if (!Number.isInteger(value) || value < low || value > high) {
45
+ throw new RangeError(`${name} must be ${low}-${high}`);
46
+ }
47
+ }
48
+
49
+ function _checkTime(hour, minute, second) {
50
+ _check('hour', hour, 0, 23);
51
+ _check('minute', minute, 0, 59);
52
+ _check('second', second, 0, 59);
53
+ }
54
+
55
+ function _checkDate(year, month, day, weekday) {
56
+ _check('year', year, 2000, 2099);
57
+ _check('month', month, 1, 12);
58
+ _check('day', day, 1, 31);
59
+ _check('weekday', weekday, 1, 7);
60
+ }
61
+
62
+ function _dateBytes(year, month, day, weekday) {
63
+ return [weekday, _intToBcd(day), _intToBcd(month), _intToBcd(year - 2000)];
64
+ }
65
+
66
+ function _decodeTime(raw) {
67
+ return {
68
+ hour: _decodeHour(raw[2]),
69
+ minute: _bcdToInt(raw[1] & 0x7F),
70
+ second: _bcdToInt(raw[0] & 0x7F),
71
+ };
72
+ }
73
+
74
+ function _decodeDate(raw) {
75
+ return {
76
+ year: 2000 + _bcdToInt(raw[3]),
77
+ month: _bcdToInt(raw[2] & 0x1F),
78
+ day: _bcdToInt(raw[1] & 0x3F),
79
+ weekday: raw[0] & 0x07,
80
+ };
81
+ }
82
+
83
+ /**
84
+ * DS1307 64 x 8 serial I²C real-time clock (Maxim Integrated) — minimal interface.
85
+ *
86
+ * BCD calendar clock (seconds..year, 12- or 24-hour mode, leap years up to 2100) for a
87
+ * 32.768 kHz crystal, with 56 bytes of battery-backed SRAM and an open-drain SQW/OUT pin. No
88
+ * alarms, no interrupts, no calibration. Fixed I²C address 0x68 (shared with the DS3231, PCF8523
89
+ * and others — there is no identity register), standard mode (100 kHz) only. `init()` reads
90
+ * SECONDS once and writes nothing.
91
+ *
92
+ * `weekday` is ISO 8601, 1=Monday..7=Sunday (stored as is in DAY).
93
+ */
94
+ class DS1307Minimal {
95
+ /**
96
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection at address 0x68.
97
+ */
98
+ constructor(connection) {
99
+ this._conn = connection;
100
+ }
101
+
102
+ /**
103
+ * Confirm the device answers by reading SECONDS (no writes — the DS1307 has no identity
104
+ * register).
105
+ * @returns {Promise<void>}
106
+ */
107
+ async init() {
108
+ await this._conn.readReg(_REG_SECONDS, 1);
109
+ }
110
+
111
+ /**
112
+ * Read the calendar clock (one 7-byte burst from 0x00). 12-hour data is converted to 0-23;
113
+ * check `valid()` (Full) to know whether the clock runs.
114
+ * @returns {Promise<{year: number, month: number, day: number, weekday: number, hour: number, minute: number, second: number}>}
115
+ * `year` is 2000-2099, `hour` is 0-23, `weekday` is 1=Monday..7=Sunday.
116
+ */
117
+ async getDatetime() {
118
+ const raw = await this._conn.readReg(_REG_SECONDS, 7);
119
+ return { ..._decodeDate(raw.subarray(3, 7)), ..._decodeTime(raw.subarray(0, 3)) };
120
+ }
121
+
122
+ /**
123
+ * Write the calendar clock in one 7-byte transaction starting at 0x00, with CH = 0 (starts
124
+ * the oscillator) and 24-hour mode.
125
+ * @param {number} year - 2000-2099.
126
+ * @param {number} month - 1-12.
127
+ * @param {number} day - 1-31.
128
+ * @param {number} weekday - 1=Monday..7=Sunday.
129
+ * @param {number} hour - 0-23.
130
+ * @param {number} minute - 0-59.
131
+ * @param {number} second - 0-59.
132
+ * @returns {Promise<void>}
133
+ * @throws {RangeError} If any field is out of range (nothing is written).
134
+ */
135
+ async setDatetime(year, month, day, weekday, hour, minute, second) {
136
+ _checkDate(year, month, day, weekday);
137
+ _checkTime(hour, minute, second);
138
+ await this._conn.writeReg(_REG_SECONDS, Buffer.from([
139
+ _intToBcd(second), _intToBcd(minute), _intToBcd(hour),
140
+ ..._dateBytes(year, month, day, weekday),
141
+ ]));
142
+ }
143
+ }
144
+
145
+ /**
146
+ * DS1307 full interface — extends Minimal with clock halt/start, partial time and date access,
147
+ * 12/24-hour mode, the square-wave / static output on SQW/OUT and the 56-byte battery-backed
148
+ * RAM.
149
+ */
150
+ class DS1307Full extends DS1307Minimal {
151
+ async _readReg(reg) {
152
+ return (await this._conn.readReg(reg, 1))[0];
153
+ }
154
+
155
+ async _writeReg(reg, value) {
156
+ await this._conn.writeReg(reg, Buffer.from([value & 0xFF]));
157
+ }
158
+
159
+ /**
160
+ * Report whether the oscillator runs.
161
+ * @returns {Promise<boolean>} true when SECONDS.CH = 0.
162
+ */
163
+ async isRunning() {
164
+ return ((await this._readReg(_REG_SECONDS)) & _SECONDS_CH) === 0;
165
+ }
166
+
167
+ /**
168
+ * Stop the oscillator (CH = 1, keeping the seconds value); minimises the backup current.
169
+ * @returns {Promise<void>}
170
+ */
171
+ async halt() {
172
+ await this._writeReg(_REG_SECONDS, (await this._readReg(_REG_SECONDS)) | _SECONDS_CH);
173
+ }
174
+
175
+ /**
176
+ * Start the oscillator (CH = 0, keeping the seconds value).
177
+ * @returns {Promise<void>}
178
+ */
179
+ async start() {
180
+ await this._writeReg(_REG_SECONDS, (await this._readReg(_REG_SECONDS)) & ~_SECONDS_CH);
181
+ }
182
+
183
+ /**
184
+ * Report whether the clock holds a valid time. CH = 1 after the first power-up or a lost
185
+ * backup supply; the chip gives no other sign of an earlier power loss.
186
+ * @returns {Promise<boolean>} true when CH = 0.
187
+ */
188
+ async valid() {
189
+ return this.isRunning();
190
+ }
191
+
192
+ /**
193
+ * Read the time registers (0x00-0x02).
194
+ * @returns {Promise<{hour: number, minute: number, second: number}>} `hour` is 0-23.
195
+ */
196
+ async getTime() {
197
+ return _decodeTime(await this._conn.readReg(_REG_SECONDS, 3));
198
+ }
199
+
200
+ /**
201
+ * Read the date registers (0x03-0x06).
202
+ * @returns {Promise<{year: number, month: number, day: number, weekday: number}>}
203
+ * `weekday` is 1=Monday..7=Sunday.
204
+ */
205
+ async getDate() {
206
+ return _decodeDate(await this._conn.readReg(_REG_DAY, 4));
207
+ }
208
+
209
+ /**
210
+ * Write SECONDS, MINUTES and HOURS in one transaction, keeping CH (a halted clock stays
211
+ * halted) and the current 12/24-hour mode.
212
+ * @param {number} hour - 0-23.
213
+ * @param {number} minute - 0-59.
214
+ * @param {number} second - 0-59.
215
+ * @returns {Promise<void>}
216
+ * @throws {RangeError} If any field is out of range (nothing is written).
217
+ */
218
+ async setTime(hour, minute, second) {
219
+ _checkTime(hour, minute, second);
220
+ const cur = await this._conn.readReg(_REG_SECONDS, 3);
221
+ await this._conn.writeReg(_REG_SECONDS, Buffer.from([
222
+ (cur[0] & _SECONDS_CH) | _intToBcd(second),
223
+ _intToBcd(minute),
224
+ _encodeHour(hour, (cur[2] & _HOURS_12) !== 0),
225
+ ]));
226
+ }
227
+
228
+ /**
229
+ * Write DAY, DATE, MONTH and YEAR in one transaction.
230
+ * @param {number} year - 2000-2099.
231
+ * @param {number} month - 1-12.
232
+ * @param {number} day - 1-31.
233
+ * @param {number} weekday - 1=Monday..7=Sunday.
234
+ * @returns {Promise<void>}
235
+ * @throws {RangeError} If any field is out of range (nothing is written).
236
+ */
237
+ async setDate(year, month, day, weekday) {
238
+ _checkDate(year, month, day, weekday);
239
+ await this._conn.writeReg(_REG_DAY, Buffer.from(_dateBytes(year, month, day, weekday)));
240
+ }
241
+
242
+ /**
243
+ * Switch between 12- and 24-hour mode, re-entering the current hour. `getDatetime()` and
244
+ * `getTime()` return 0-23 either way.
245
+ * @param {boolean} hour12 - true selects 12-hour (AM/PM) mode, false 24-hour mode.
246
+ * @returns {Promise<void>}
247
+ */
248
+ async setHourMode(hour12) {
249
+ const hour = _decodeHour(await this._readReg(_REG_HOURS));
250
+ await this._writeReg(_REG_HOURS, _encodeHour(hour, Boolean(hour12)));
251
+ }
252
+
253
+ /**
254
+ * Enable the square wave on SQW/OUT, or turn it off.
255
+ * @param {number|null} frequencyHz - 1, 4096, 8192 or 32768; null clears SQWE (the pin then
256
+ * follows OUT).
257
+ * @returns {Promise<void>}
258
+ * @throws {RangeError} If frequencyHz is not a supported rate or null.
259
+ */
260
+ async setSquareWave(frequencyHz) {
261
+ if (frequencyHz !== null && frequencyHz !== undefined && !(frequencyHz in _SQW_RATES)) {
262
+ throw new RangeError('frequencyHz must be 1, 4096, 8192, 32768 or null');
263
+ }
264
+ const control = await this._readReg(_REG_CONTROL);
265
+ if (frequencyHz === null || frequencyHz === undefined) {
266
+ await this._writeReg(_REG_CONTROL, control & ~_CONTROL_SQWE);
267
+ } else {
268
+ await this._writeReg(_REG_CONTROL,
269
+ (control & ~_CONTROL_RS) | _CONTROL_SQWE | _SQW_RATES[frequencyHz]);
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Drive SQW/OUT as a static level (SQWE = 0, OUT = level).
275
+ * @param {boolean} level - true releases the open-drain pin (high via the pull-up), false
276
+ * pulls it low.
277
+ * @returns {Promise<void>}
278
+ */
279
+ async setOutput(level) {
280
+ const control = (await this._readReg(_REG_CONTROL)) & ~(_CONTROL_SQWE | _CONTROL_OUT);
281
+ await this._writeReg(_REG_CONTROL, control | (level ? _CONTROL_OUT : 0));
282
+ }
283
+
284
+ /**
285
+ * Read the CONTROL register.
286
+ * @returns {Promise<number>} Raw CONTROL byte (OUT bit 7, SQWE bit 4, RS bits 1:0).
287
+ */
288
+ async getControl() {
289
+ return this._readReg(_REG_CONTROL);
290
+ }
291
+
292
+ /**
293
+ * Read battery-backed SRAM (register 0x08 + address).
294
+ * @param {number} address - RAM offset 0-55.
295
+ * @param {number} length - 1..56-address bytes (no wrap past offset 55).
296
+ * @returns {Promise<Buffer>} The RAM contents.
297
+ * @throws {RangeError} If the range is outside the 56-byte RAM.
298
+ */
299
+ async readRam(address, length) {
300
+ DS1307Full._checkRam(address, length);
301
+ return Buffer.from(await this._conn.readReg(_REG_RAM + address, length));
302
+ }
303
+
304
+ /**
305
+ * Write battery-backed SRAM in one transaction (register 0x08 + address).
306
+ * @param {number} address - RAM offset 0-55.
307
+ * @param {Buffer|Uint8Array|number[]} data - 1..56-address bytes (no wrap past offset 55).
308
+ * @returns {Promise<void>}
309
+ * @throws {RangeError} If the range is outside the 56-byte RAM.
310
+ */
311
+ async writeRam(address, data) {
312
+ DS1307Full._checkRam(address, data.length);
313
+ await this._conn.writeReg(_REG_RAM + address, Buffer.from(data));
314
+ }
315
+
316
+ static _checkRam(address, length) {
317
+ _check('address', address, 0, RAM_SIZE - 1);
318
+ if (!Number.isInteger(length) || length < 1 || length > RAM_SIZE - address) {
319
+ throw new RangeError('length must be 1..56-address');
320
+ }
321
+ }
322
+ }
323
+
324
+ /** Fixed 7-bit I²C address. */
325
+ DS1307Minimal.I2C_ADDRESS = I2C_ADDRESS;
326
+
327
+ module.exports = { DS1307Minimal, DS1307Full, I2C_ADDRESS, RAM_SIZE };