periph 1.3.2 → 1.4.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 (114) hide show
  1. package/README.md +27 -0
  2. package/examples/accelerometer/bma180/complete.js +83 -0
  3. package/examples/accelerometer/bma180/demo.js +47 -0
  4. package/examples/accelerometer/bma180/minimal.js +21 -0
  5. package/examples/accelerometer/fxos8700/complete.js +111 -0
  6. package/examples/accelerometer/fxos8700/demo.js +80 -0
  7. package/examples/accelerometer/fxos8700/minimal.js +20 -0
  8. package/examples/accelerometer/lis3dh/complete.js +123 -0
  9. package/examples/accelerometer/lis3dh/demo.js +99 -0
  10. package/examples/accelerometer/lis3dh/minimal.js +19 -0
  11. package/examples/accelerometer/lsm303/complete.js +82 -0
  12. package/examples/accelerometer/lsm303/demo.js +96 -0
  13. package/examples/accelerometer/lsm303/minimal.js +23 -0
  14. package/examples/accelerometer/mma8451q/complete.js +96 -0
  15. package/examples/accelerometer/mma8451q/demo.js +120 -0
  16. package/examples/accelerometer/mma8451q/minimal.js +20 -0
  17. package/examples/adc_dac/ad5243/complete.js +52 -0
  18. package/examples/adc_dac/ad5243/demo.js +50 -0
  19. package/examples/adc_dac/ad5243/minimal.js +23 -0
  20. package/examples/adc_dac/ad5248/complete.js +49 -0
  21. package/examples/adc_dac/ad5248/demo.js +42 -0
  22. package/examples/adc_dac/ad5248/minimal.js +21 -0
  23. package/examples/adc_dac/ads1013/complete.js +51 -0
  24. package/examples/adc_dac/ads1013/demo.js +51 -0
  25. package/examples/adc_dac/ads1013/minimal.js +21 -0
  26. package/examples/adc_dac/ads1014/complete.js +88 -0
  27. package/examples/adc_dac/ads1014/demo.js +52 -0
  28. package/examples/adc_dac/ads1014/minimal.js +21 -0
  29. package/examples/adc_dac/ads1113/complete.js +47 -0
  30. package/examples/adc_dac/ads1113/demo.js +39 -0
  31. package/examples/adc_dac/ads1113/minimal.js +20 -0
  32. package/examples/adc_dac/ads1114/complete.js +77 -0
  33. package/examples/adc_dac/ads1114/demo.js +47 -0
  34. package/examples/adc_dac/ads1114/minimal.js +20 -0
  35. package/examples/adc_dac/ads1115/complete.js +82 -0
  36. package/examples/adc_dac/ads1115/demo.js +55 -0
  37. package/examples/adc_dac/ads1115/minimal.js +20 -0
  38. package/examples/adc_dac/es8311/complete.js +80 -0
  39. package/examples/adc_dac/es8311/demo.js +59 -0
  40. package/examples/adc_dac/es8311/minimal.js +18 -0
  41. package/examples/adc_dac/mcp4xxx/complete.js +97 -0
  42. package/examples/adc_dac/mcp4xxx/demo.js +76 -0
  43. package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
  44. package/examples/display/pcf8578/complete.js +58 -0
  45. package/examples/display/pcf8578/demo.js +62 -0
  46. package/examples/display/pcf8578/minimal.js +19 -0
  47. package/examples/display/pcf8579/complete.js +58 -0
  48. package/examples/display/pcf8579/demo.js +61 -0
  49. package/examples/display/pcf8579/minimal.js +19 -0
  50. package/examples/imu/icm20948/complete.js +114 -0
  51. package/examples/imu/icm20948/demo.js +79 -0
  52. package/examples/imu/icm20948/minimal.js +22 -0
  53. package/examples/imu/qmi8658a/complete.js +134 -0
  54. package/examples/imu/qmi8658a/demo.js +81 -0
  55. package/examples/imu/qmi8658a/minimal.js +22 -0
  56. package/examples/io_expander/mcp23008/complete.js +70 -0
  57. package/examples/io_expander/mcp23008/demo.js +58 -0
  58. package/examples/io_expander/mcp23008/minimal.js +20 -0
  59. package/examples/memory/mb85rc/complete.js +40 -0
  60. package/examples/memory/mb85rc/demo.js +58 -0
  61. package/examples/memory/mb85rc/minimal.js +15 -0
  62. package/examples/motor/drv2605/complete.js +100 -0
  63. package/examples/motor/drv2605/demo.js +55 -0
  64. package/examples/motor/drv2605/minimal.js +24 -0
  65. package/examples/motor/tmc2130/complete.js +138 -0
  66. package/examples/motor/tmc2130/demo.js +96 -0
  67. package/examples/motor/tmc2130/minimal.js +30 -0
  68. package/examples/motor/tmc2208/complete.js +92 -0
  69. package/examples/motor/tmc2208/demo.js +85 -0
  70. package/examples/motor/tmc2208/minimal.js +25 -0
  71. package/examples/motor/tmc2209/complete.js +106 -0
  72. package/examples/motor/tmc2209/demo.js +97 -0
  73. package/examples/motor/tmc2209/minimal.js +25 -0
  74. package/examples/motor/tmc5160/complete.js +205 -0
  75. package/examples/motor/tmc5160/demo.js +104 -0
  76. package/examples/motor/tmc5160/minimal.js +35 -0
  77. package/examples/power/axp2101/complete.js +135 -0
  78. package/examples/power/axp2101/demo.js +68 -0
  79. package/examples/power/axp2101/minimal.js +24 -0
  80. package/examples/rtc/pcf85063a/complete.js +68 -0
  81. package/examples/rtc/pcf85063a/demo.js +76 -0
  82. package/examples/rtc/pcf85063a/minimal.js +20 -0
  83. package/package.json +1 -1
  84. package/src/chips/accelerometer/bma180.js +573 -0
  85. package/src/chips/accelerometer/fxos8700.js +1600 -0
  86. package/src/chips/accelerometer/lis3dh.js +1341 -0
  87. package/src/chips/accelerometer/lsm303.js +1471 -0
  88. package/src/chips/accelerometer/mma8451q.js +1406 -0
  89. package/src/chips/adc_dac/_ads111x_base.js +272 -0
  90. package/src/chips/adc_dac/ad5243.js +231 -0
  91. package/src/chips/adc_dac/ad5248.js +194 -0
  92. package/src/chips/adc_dac/ads1013.js +219 -0
  93. package/src/chips/adc_dac/ads1014.js +509 -0
  94. package/src/chips/adc_dac/ads1113.js +112 -0
  95. package/src/chips/adc_dac/ads1114.js +201 -0
  96. package/src/chips/adc_dac/ads1115.js +232 -0
  97. package/src/chips/adc_dac/es8311.js +621 -0
  98. package/src/chips/adc_dac/mcp4xxx.js +982 -0
  99. package/src/chips/display/pcf8578.js +452 -0
  100. package/src/chips/display/pcf8579.js +451 -0
  101. package/src/chips/imu/icm20948.js +849 -0
  102. package/src/chips/imu/qmi8658a.js +1011 -0
  103. package/src/chips/io_expander/mcp23008.js +558 -0
  104. package/src/chips/memory/mb85rc.js +166 -0
  105. package/src/chips/motor/drv2605.js +592 -0
  106. package/src/chips/motor/tmc2130.js +537 -0
  107. package/src/chips/motor/tmc2208.js +490 -0
  108. package/src/chips/motor/tmc2209.js +565 -0
  109. package/src/chips/motor/tmc5160.js +900 -0
  110. package/src/chips/power/axp2101.js +968 -0
  111. package/src/chips/rtc/pcf85063a.js +482 -0
  112. package/src/connection/uart.js +9 -0
  113. package/src/connection/uart_mock.js +65 -0
  114. package/src/discovery/registry.js +45 -19
@@ -0,0 +1,509 @@
1
+ 'use strict';
2
+
3
+ const { toSigned } = require('../../connection/register');
4
+
5
+ const _REG_CONVERSION = 0x00;
6
+ const _REG_CONFIG = 0x01;
7
+ const _REG_LO_THRESH = 0x02;
8
+ const _REG_HI_THRESH = 0x03;
9
+
10
+ const _CONFIG_RESET = 0x8583;
11
+ const _LO_RESET = 0x8000;
12
+ const _HI_RESET = 0x7FFF;
13
+
14
+ const _OS = 0x8000;
15
+ const _MODE_SINGLE = 0x0100;
16
+ const _QUEUE_DISABLE = 3;
17
+
18
+ const _FULL_SCALE = [6.144, 4.096, 2.048, 1.024, 0.512, 0.256];
19
+ const _SPS = [128, 250, 490, 920, 1600, 2400, 3300];
20
+
21
+ const SOURCE_CONVERSION_READY = 0x01;
22
+ const SOURCE_COMPARATOR = 0x02;
23
+
24
+ function _sleep(ms) {
25
+ return new Promise((resolve) => setTimeout(resolve, ms));
26
+ }
27
+
28
+ /**
29
+ * ADS1014 12-bit I²C ADC with PGA and comparator (Texas Instruments) —
30
+ * minimal interface.
31
+ *
32
+ * Single-input (AIN0 − AIN1) ΔΣ ADC with internal reference and oscillator.
33
+ * Registers are 16-bit, big-endian, addressed through a persistent Address
34
+ * Pointer. Four selectable addresses (0x48–0x4B) via the ADDR strap. The
35
+ * chip has no ID register, so construction performs no I²C traffic.
36
+ *
37
+ * Defaults: ±2.048 V range (1 mV/LSB), 1600 SPS, single-shot, comparator
38
+ * disabled. The whole Config word is written for every conversion.
39
+ */
40
+ class ADS1014Minimal {
41
+ /**
42
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x48–0x4B).
43
+ */
44
+ constructor(connection) {
45
+ this._conn = connection;
46
+ this._pga = 2;
47
+ this._dr = 4;
48
+ this._continuous = false;
49
+ this._compMode = 0;
50
+ this._compPol = 0;
51
+ this._compLat = false;
52
+ this._queue = 0;
53
+ this._source = 0;
54
+ }
55
+
56
+ async _readReg(reg) {
57
+ const buf = await this._conn.readReg(reg & 0xFF, 2);
58
+ return buf.readUInt16BE(0);
59
+ }
60
+
61
+ async _writeReg(reg, value) {
62
+ await this._conn.writeReg(reg & 0xFF, Buffer.from([(value >> 8) & 0xFF, value & 0xFF]));
63
+ }
64
+
65
+ _compQue() {
66
+ if (this._source === SOURCE_CONVERSION_READY) return 0;
67
+ if (this._source === SOURCE_COMPARATOR) return this._queue;
68
+ return _QUEUE_DISABLE;
69
+ }
70
+
71
+ _configWord(osBit, continuous) {
72
+ let word = osBit ? _OS : 0;
73
+ word |= this._pga << 9;
74
+ if (!continuous) word |= _MODE_SINGLE;
75
+ word |= this._dr << 5;
76
+ word |= this._compMode << 4;
77
+ word |= this._compPol << 3;
78
+ word |= (this._compLat ? 1 : 0) << 2;
79
+ word |= this._compQue();
80
+ return word;
81
+ }
82
+
83
+ async _readConversion() {
84
+ return toSigned(await this._readReg(_REG_CONVERSION), 16) >> 4;
85
+ }
86
+
87
+ /**
88
+ * Run one single-shot conversion of AIN0 − AIN1. Writes the whole Config
89
+ * word with OS = 1, polls OS until the chip is idle again, then reads
90
+ * CONVERSION.
91
+ * @returns {Promise<number>} 12-bit two's-complement code, −2048…2047.
92
+ */
93
+ async readRaw() {
94
+ await this._writeReg(_REG_CONFIG, this._configWord(true, false));
95
+ this._continuous = false;
96
+ for (let i = 0; i < 100; i++) {
97
+ if ((await this._readReg(_REG_CONFIG)) & _OS) break;
98
+ await _sleep(1);
99
+ }
100
+ return this._readConversion();
101
+ }
102
+
103
+ /**
104
+ * Run one single-shot conversion and convert it to volts.
105
+ * @returns {Promise<number>} Voltage AIN0 − AIN1 in volts (raw × full-scale / 2048).
106
+ */
107
+ async readVoltage() {
108
+ return (await this.readRaw()) * _FULL_SCALE[this._pga] / 2048;
109
+ }
110
+ }
111
+
112
+ /**
113
+ * ADS1014 full interface — extends Minimal with PGA gain, data rate,
114
+ * continuous conversion, the programmable comparator and the Level-2
115
+ * ALERT/RDY interrupt API.
116
+ */
117
+ class ADS1014Full extends ADS1014Minimal {
118
+ /**
119
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x48–0x4B).
120
+ */
121
+ constructor(connection) {
122
+ super(connection);
123
+ this._loRaw = -2048;
124
+ this._hiRaw = 2047;
125
+ this._callback = null;
126
+ this._edgeHandler = null;
127
+ this._pollTimer = null;
128
+ }
129
+
130
+ async _writeConfig() {
131
+ await this._writeReg(_REG_CONFIG, this._configWord(false, this._continuous));
132
+ }
133
+
134
+ /**
135
+ * Select the full-scale range used by all following reads. Thresholds
136
+ * must be rewritten after a gain change.
137
+ * @param {number} pga - PGA_6_144 (0) … PGA_0_256 (5).
138
+ * @returns {Promise<void>}
139
+ * @throws {RangeError} If pga is outside 0–5 (6 and 7 alias ±0.256 V).
140
+ */
141
+ async setGain(pga) {
142
+ if (!Number.isInteger(pga) || pga < 0 || pga > 5) throw new RangeError('pga must be 0-5');
143
+ this._pga = pga;
144
+ if (this._continuous) await this._writeConfig();
145
+ }
146
+
147
+ /**
148
+ * Read the configured PGA code.
149
+ * @returns {number} PGA code 0–5.
150
+ */
151
+ getGain() {
152
+ return this._pga;
153
+ }
154
+
155
+ /**
156
+ * Full-scale range of the current gain.
157
+ * @returns {number} ±FSR in volts (6.144, 4.096, 2.048, 1.024, 0.512 or 0.256).
158
+ */
159
+ fullScale() {
160
+ return _FULL_SCALE[this._pga];
161
+ }
162
+
163
+ /**
164
+ * Size of one conversion code.
165
+ * @returns {number} fullScale() / 2048 in volts.
166
+ */
167
+ lsb() {
168
+ return _FULL_SCALE[this._pga] / 2048;
169
+ }
170
+
171
+ /**
172
+ * Select the conversion data rate.
173
+ * @param {number} dr - DR_128 (0) … DR_3300 (6).
174
+ * @returns {Promise<void>}
175
+ * @throws {RangeError} If dr is outside 0–6 (7 aliases 3300 SPS).
176
+ */
177
+ async setDataRate(dr) {
178
+ if (!Number.isInteger(dr) || dr < 0 || dr > 6) throw new RangeError('dr must be 0-6');
179
+ this._dr = dr;
180
+ if (this._continuous) await this._writeConfig();
181
+ }
182
+
183
+ /**
184
+ * Read the configured data-rate code.
185
+ * @returns {number} DR code 0–6.
186
+ */
187
+ getDataRate() {
188
+ return this._dr;
189
+ }
190
+
191
+ /**
192
+ * Current data rate.
193
+ * @returns {number} 128, 250, 490, 920, 1600, 2400 or 3300 samples per second.
194
+ */
195
+ dataRateSps() {
196
+ return _SPS[this._dr];
197
+ }
198
+
199
+ /**
200
+ * Start continuous conversion (MODE = 0) at the current data rate.
201
+ * @returns {Promise<void>}
202
+ */
203
+ async startContinuous() {
204
+ this._continuous = true;
205
+ await this._writeConfig();
206
+ }
207
+
208
+ /**
209
+ * Read the most recent completed result without touching Config (valid
210
+ * at least 1/DR after startContinuous()).
211
+ * @returns {Promise<number>} 12-bit two's-complement code, −2048…2047.
212
+ */
213
+ async readContinuousRaw() {
214
+ return this._readConversion();
215
+ }
216
+
217
+ /**
218
+ * Read the most recent completed result in volts.
219
+ * @returns {Promise<number>} readContinuousRaw() × lsb().
220
+ */
221
+ async readContinuous() {
222
+ return (await this._readConversion()) * this.lsb();
223
+ }
224
+
225
+ /**
226
+ * Stop continuous conversion (MODE = 1, single-shot / power-down).
227
+ * @returns {Promise<void>}
228
+ */
229
+ async stopContinuous() {
230
+ this._continuous = false;
231
+ await this._writeConfig();
232
+ }
233
+
234
+ /**
235
+ * Report whether a single-shot conversion is running (OS = 0; only
236
+ * meaningful in single-shot mode).
237
+ * @returns {Promise<boolean>} True while converting.
238
+ */
239
+ async isConverting() {
240
+ return ((await this._readReg(_REG_CONFIG)) & _OS) === 0;
241
+ }
242
+
243
+ /**
244
+ * Read the raw CONFIG register.
245
+ * @returns {Promise<number>} 16-bit CONFIG value.
246
+ */
247
+ async readConfig() {
248
+ return this._readReg(_REG_CONFIG);
249
+ }
250
+
251
+ /**
252
+ * Write the reset values back (CONFIG 0x8583, LO_THRESH 0x8000, HI_THRESH
253
+ * 0x7FFF) and clear the cached settings. Not an I²C general-call reset.
254
+ * @returns {Promise<void>}
255
+ */
256
+ async reset() {
257
+ await this._writeReg(_REG_CONFIG, _CONFIG_RESET);
258
+ await this._writeReg(_REG_LO_THRESH, _LO_RESET);
259
+ await this._writeReg(_REG_HI_THRESH, _HI_RESET);
260
+ this._pga = 2;
261
+ this._dr = 4;
262
+ this._continuous = false;
263
+ this._compMode = 0;
264
+ this._compPol = 0;
265
+ this._compLat = false;
266
+ this._queue = 0;
267
+ this._source = 0;
268
+ this._loRaw = -2048;
269
+ this._hiRaw = 2047;
270
+ }
271
+
272
+ /**
273
+ * Configure the comparator and enable it as the ALERT/RDY source.
274
+ * @param {number} [mode=COMP_TRADITIONAL] - COMP_TRADITIONAL (0) or COMP_WINDOW (1).
275
+ * @param {number} [polarity=ACTIVE_LOW] - ACTIVE_LOW (0) or ACTIVE_HIGH (1).
276
+ * @param {boolean} [latch=false] - Latch the alert until CONVERSION is read.
277
+ * @param {number} [queue=QUEUE_1] - QUEUE_1 (0), QUEUE_2 (1) or QUEUE_4 (2) consecutive out-of-range conversions.
278
+ * @returns {Promise<void>}
279
+ * @throws {RangeError} If an argument is out of range.
280
+ */
281
+ async configureComparator(mode = 0, polarity = 0, latch = false, queue = 0) {
282
+ if (mode !== 0 && mode !== 1) throw new RangeError('mode must be COMP_TRADITIONAL or COMP_WINDOW');
283
+ if (polarity !== 0 && polarity !== 1) throw new RangeError('polarity must be ACTIVE_LOW or ACTIVE_HIGH');
284
+ if (!Number.isInteger(queue) || queue < 0 || queue > 2) throw new RangeError('queue must be QUEUE_1, QUEUE_2 or QUEUE_4');
285
+ this._compMode = mode;
286
+ this._compPol = polarity;
287
+ this._compLat = !!latch;
288
+ this._queue = queue;
289
+ this._source = SOURCE_COMPARATOR;
290
+ await this._writeConfig();
291
+ }
292
+
293
+ /**
294
+ * Disable the comparator (COMP_QUE = 11); ALERT/RDY goes high-impedance.
295
+ * @returns {Promise<void>}
296
+ */
297
+ async disableComparator() {
298
+ this._source = 0;
299
+ await this._writeConfig();
300
+ }
301
+
302
+ async _writeThreshold(reg, raw) {
303
+ if (!Number.isInteger(raw) || raw < -2048 || raw > 2047) throw new RangeError('raw must be -2048..2047');
304
+ await this._writeReg(reg, (raw & 0x0FFF) << 4);
305
+ }
306
+
307
+ /**
308
+ * Write the comparator low threshold (LO_THRESH = raw << 4).
309
+ * @param {number} raw - 12-bit two's-complement code, −2048…2047.
310
+ * @returns {Promise<void>}
311
+ * @throws {RangeError} If raw is out of range.
312
+ */
313
+ async setLowThresholdRaw(raw) {
314
+ await this._writeThreshold(_REG_LO_THRESH, raw);
315
+ this._loRaw = raw;
316
+ }
317
+
318
+ /**
319
+ * Write the comparator high threshold (HI_THRESH = raw << 4).
320
+ * @param {number} raw - 12-bit two's-complement code, −2048…2047.
321
+ * @returns {Promise<void>}
322
+ * @throws {RangeError} If raw is out of range.
323
+ */
324
+ async setHighThresholdRaw(raw) {
325
+ await this._writeThreshold(_REG_HI_THRESH, raw);
326
+ this._hiRaw = raw;
327
+ }
328
+
329
+ /**
330
+ * Write both comparator thresholds from volts (converted with lsb(),
331
+ * clamped to the code range).
332
+ * @param {number} low - Low threshold in volts.
333
+ * @param {number} high - High threshold in volts.
334
+ * @returns {Promise<void>}
335
+ * @throws {RangeError} If high is not greater than low.
336
+ */
337
+ async setThresholds(low, high) {
338
+ if (high <= low) throw new RangeError('high must be greater than low');
339
+ const step = this.lsb();
340
+ const toCode = (v) => Math.max(-2048, Math.min(2047, Math.round(v / step)));
341
+ await this.setLowThresholdRaw(toCode(low));
342
+ await this.setHighThresholdRaw(toCode(high));
343
+ }
344
+
345
+ /**
346
+ * Read both thresholds back.
347
+ * @returns {Promise<[number, number]>} [low, high] as sign-extended 12-bit codes.
348
+ */
349
+ async getThresholdsRaw() {
350
+ const lo = toSigned(await this._readReg(_REG_LO_THRESH), 16) >> 4;
351
+ const hi = toSigned(await this._readReg(_REG_HI_THRESH), 16) >> 4;
352
+ return [lo, hi];
353
+ }
354
+
355
+ /**
356
+ * Release a latched comparator output by reading CONVERSION.
357
+ * @returns {Promise<void>}
358
+ */
359
+ async clearAlert() {
360
+ await this._readReg(_REG_CONVERSION);
361
+ }
362
+
363
+ // -- Interrupt API (Level 2) --------------------------------------------
364
+
365
+ /**
366
+ * Route one source to ALERT/RDY; the other is disabled.
367
+ * SOURCE_CONVERSION_READY writes HI_THRESH = 0x8000 and LO_THRESH = 0x0000
368
+ * and sets COMP_QUE = 00. SOURCE_COMPARATOR restores the thresholds last
369
+ * set through the driver and the configured queue.
370
+ * @param {number} source - SOURCE_CONVERSION_READY or SOURCE_COMPARATOR.
371
+ * @returns {Promise<void>}
372
+ * @throws {RangeError} For an unknown source, or for SOURCE_COMPARATOR when the thresholds set through the driver have high ≤ low.
373
+ */
374
+ async enableInterrupt(source) {
375
+ if (source === SOURCE_CONVERSION_READY) {
376
+ await this._writeReg(_REG_HI_THRESH, 0x8000);
377
+ await this._writeReg(_REG_LO_THRESH, 0x0000);
378
+ } else if (source === SOURCE_COMPARATOR) {
379
+ if (this._hiRaw <= this._loRaw) {
380
+ throw new RangeError('set thresholds (high > low) before enabling the comparator');
381
+ }
382
+ await this._writeReg(_REG_LO_THRESH, (this._loRaw & 0x0FFF) << 4);
383
+ await this._writeReg(_REG_HI_THRESH, (this._hiRaw & 0x0FFF) << 4);
384
+ } else {
385
+ throw new RangeError('unknown interrupt source');
386
+ }
387
+ this._source = source;
388
+ await this._writeConfig();
389
+ }
390
+
391
+ /**
392
+ * Release ALERT/RDY (COMP_QUE = 11). The two sources share the pin, so it
393
+ * is released whichever source is given.
394
+ * @param {number} _source - SOURCE_CONVERSION_READY or SOURCE_COMPARATOR.
395
+ * @returns {Promise<void>}
396
+ */
397
+ async disableInterrupt(_source) {
398
+ this._source = 0;
399
+ await this._writeConfig();
400
+ }
401
+
402
+ /**
403
+ * Report whether the enabled source is asserted. The chip has no
404
+ * interrupt-status register: with an InputPin on the connection, the
405
+ * ALERT/RDY level is sampled against COMP_POL (and a latched comparator
406
+ * alert is cleared by reading CONVERSION). Without a pin,
407
+ * SOURCE_CONVERSION_READY falls back to CONFIG.OS (single-shot only) and
408
+ * SOURCE_COMPARATOR returns 0.
409
+ * @returns {Promise<number>} The enabled SOURCE_* constant if asserted, else 0.
410
+ */
411
+ async pollInterrupt() {
412
+ if (this._source === 0) return 0;
413
+ const pin = this._conn.intPin;
414
+ if (pin) {
415
+ const asserted = pin.isHigh() === (this._compPol === 1);
416
+ if (!asserted) return 0;
417
+ if (this._source === SOURCE_COMPARATOR && this._compLat) await this.clearAlert();
418
+ return this._source;
419
+ }
420
+ if (this._source === SOURCE_CONVERSION_READY && !this._continuous &&
421
+ ((await this._readReg(_REG_CONFIG)) & _OS)) {
422
+ return SOURCE_CONVERSION_READY;
423
+ }
424
+ return 0;
425
+ }
426
+
427
+ /**
428
+ * Subscribe to ALERT/RDY events. With `connection.intPin` the callback
429
+ * runs on every asserting edge (falling for ACTIVE_LOW, rising for
430
+ * ACTIVE_HIGH). Otherwise a 5 ms polling loop calls back whenever
431
+ * pollInterrupt() changes. Call enableInterrupt() first.
432
+ * @param {function(number): void} callback - Receives the pollInterrupt() value.
433
+ * @returns {Promise<void>}
434
+ */
435
+ async onInterrupt(callback) {
436
+ this._callback = callback;
437
+ if (this._conn.intPin) {
438
+ this._edgeHandler = async () => {
439
+ const status = await this.pollInterrupt();
440
+ if (status && this._callback) this._callback(status);
441
+ };
442
+ await this._conn.intPin.onEdge(this._edgeHandler, this._compPol === 1 ? 'rising' : 'falling');
443
+ } else {
444
+ let last = await this.pollInterrupt();
445
+ let busy = false;
446
+ this._pollTimer = setInterval(async () => {
447
+ if (busy) return;
448
+ busy = true;
449
+ try {
450
+ const status = await this.pollInterrupt();
451
+ if (status !== last) {
452
+ last = status;
453
+ if (this._callback) this._callback(status);
454
+ }
455
+ } finally {
456
+ busy = false;
457
+ }
458
+ }, 5);
459
+ }
460
+ }
461
+
462
+ /**
463
+ * Unsubscribe and stop delivery.
464
+ * @returns {Promise<void>}
465
+ */
466
+ async offInterrupt() {
467
+ if (this._conn.intPin && this._edgeHandler) {
468
+ await this._conn.intPin.offEdge(this._edgeHandler);
469
+ this._edgeHandler = null;
470
+ }
471
+ if (this._pollTimer) {
472
+ clearInterval(this._pollTimer);
473
+ this._pollTimer = null;
474
+ }
475
+ this._callback = null;
476
+ }
477
+ }
478
+
479
+ /** Default 7-bit I²C address (ADDR = GND). Valid range 0x48–0x4B. */
480
+ ADS1014Minimal.I2C_ADDRESS = 0x48;
481
+
482
+ /** PGA full-scale settings. */
483
+ ADS1014Full.PGA_6_144 = 0;
484
+ ADS1014Full.PGA_4_096 = 1;
485
+ ADS1014Full.PGA_2_048 = 2;
486
+ ADS1014Full.PGA_1_024 = 3;
487
+ ADS1014Full.PGA_0_512 = 4;
488
+ ADS1014Full.PGA_0_256 = 5;
489
+ /** Data-rate codes. */
490
+ ADS1014Full.DR_128 = 0;
491
+ ADS1014Full.DR_250 = 1;
492
+ ADS1014Full.DR_490 = 2;
493
+ ADS1014Full.DR_920 = 3;
494
+ ADS1014Full.DR_1600 = 4;
495
+ ADS1014Full.DR_2400 = 5;
496
+ ADS1014Full.DR_3300 = 6;
497
+ /** Comparator mode, polarity and queue settings. */
498
+ ADS1014Full.COMP_TRADITIONAL = 0;
499
+ ADS1014Full.COMP_WINDOW = 1;
500
+ ADS1014Full.ACTIVE_LOW = 0;
501
+ ADS1014Full.ACTIVE_HIGH = 1;
502
+ ADS1014Full.QUEUE_1 = 0;
503
+ ADS1014Full.QUEUE_2 = 1;
504
+ ADS1014Full.QUEUE_4 = 2;
505
+ /** Interrupt sources — only one can drive ALERT/RDY at a time. */
506
+ ADS1014Full.SOURCE_CONVERSION_READY = SOURCE_CONVERSION_READY;
507
+ ADS1014Full.SOURCE_COMPARATOR = SOURCE_COMPARATOR;
508
+
509
+ module.exports = { ADS1014Minimal, ADS1014Full };
@@ -0,0 +1,112 @@
1
+ 'use strict';
2
+
3
+ const { ADS111xBase, RATES, REG_CONFIG, SOURCE_CONVERSION_READY, SOURCE_COMPARATOR } = require('./_ads111x_base');
4
+
5
+ /**
6
+ * ADS1113 16-bit ΔΣ ADC (one differential input (AIN0 − AIN1), fixed ±2.048 V) — minimal interface.
7
+ *
8
+ * Texas Instruments I²C ADC, four addresses (0x48–0x4B) via the ADDR strap,
9
+ * no ID register. Construction performs no I²C traffic; every conversion
10
+ * writes the full Config word (single-shot, ±2.048 V, 128 SPS defaults) and
11
+ * leaves the chip in power-down. Registers are 16-bit big-endian behind a
12
+ * write-only Address Pointer; the implementation is shared with its siblings
13
+ * in `_ads111x_base`.
14
+ */
15
+ class ADS1113Minimal extends ADS111xBase {
16
+ /**
17
+ * Run a single-shot conversion of AIN0 − AIN1.
18
+ * @returns {Promise<number>} Signed 16-bit code (−32768…32767).
19
+ */
20
+ async readRaw() {
21
+ return this._convert(0);
22
+ }
23
+
24
+ /**
25
+ * Run a single-shot conversion and return volts (raw × 62.5 µV at the default range).
26
+ * @returns {Promise<number>} Voltage AIN0 − AIN1 in V.
27
+ */
28
+ async readVoltage() {
29
+ return (await this.readRaw()) * 0.0000625;
30
+ }
31
+ }
32
+
33
+ /**
34
+ * ADS1113 full interface — extends Minimal with data rate, continuous conversion, status and reset.
35
+ */
36
+ class ADS1113Full extends ADS1113Minimal {
37
+ /**
38
+ * Run a single-shot conversion and return volts at the fixed ±2.048 V range.
39
+ * @returns {Promise<number>} Voltage AIN0 − AIN1 in V.
40
+ */
41
+ async readVoltage() {
42
+ return (await this.readRaw()) * this._lsb();
43
+ }
44
+
45
+ /**
46
+ * Start continuous conversion at the current data rate (MODE ← 0).
47
+ * @returns {Promise<void>}
48
+ */
49
+ async startContinuous() {
50
+ await this._startContinuous(0);
51
+ }
52
+
53
+ /** @returns {number} ±FSR of the current gain in volts. */
54
+ fullScale() { return this._fullScale(); }
55
+
56
+ /** @returns {number} Size of one code in volts (fullScale() / 32768). */
57
+ lsb() { return this._lsb(); }
58
+
59
+ /**
60
+ * Set the data rate used by all following conversions.
61
+ * @param {number} dr - DR_8 … DR_860 (0–7).
62
+ * @returns {Promise<void>}
63
+ * @throws {RangeError} If dr is outside 0–7.
64
+ */
65
+ async setDataRate(dr) {
66
+ await this._setDataRate(dr);
67
+ }
68
+
69
+ /** @returns {number} The configured DR code (0–7). */
70
+ getDataRate() { return this._dr; }
71
+
72
+ /** @returns {number} The configured data rate in samples per second. */
73
+ dataRateSps() { return RATES[this._dr]; }
74
+
75
+ /**
76
+ * Read the most recent completed result without touching Config.
77
+ * @returns {Promise<number>} Signed 16-bit code.
78
+ */
79
+ async readContinuousRaw() { return this._readContinuousRaw(); }
80
+
81
+ /**
82
+ * Read the most recent continuous result in volts.
83
+ * @returns {Promise<number>} Voltage in V.
84
+ */
85
+ async readContinuous() { return (await this._readContinuousRaw()) * this._lsb(); }
86
+
87
+ /**
88
+ * Return to single-shot / power-down mode (MODE ← 1).
89
+ * @returns {Promise<void>}
90
+ */
91
+ async stopContinuous() { await this._stopContinuous(); }
92
+
93
+ /** @returns {Promise<boolean>} True while CONFIG.OS reads 0 (single-shot mode only). */
94
+ async isConverting() { return this._isConverting(); }
95
+
96
+ /** @returns {Promise<number>} The raw 16-bit CONFIG register. */
97
+ async readConfig() { return this._readReg(REG_CONFIG); }
98
+
99
+ /**
100
+ * Write the reset values back and clear cached settings. Not an I²C general-call reset.
101
+ * @returns {Promise<void>}
102
+ */
103
+ async reset() { await this._reset(false); }
104
+ }
105
+
106
+ /** Default 7-bit I²C address (ADDR = GND). Valid range 0x48–0x4B. */
107
+ ADS1113Minimal.I2C_ADDRESS = 0x48;
108
+
109
+ /** Data-rate codes (DR[2:0]). */
110
+ Object.assign(ADS1113Full, { DR_8: 0, DR_16: 1, DR_32: 2, DR_64: 3, DR_128: 4, DR_250: 5, DR_475: 6, DR_860: 7 });
111
+
112
+ module.exports = { ADS1113Minimal, ADS1113Full };