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,312 @@
1
+ 'use strict';
2
+
3
+ const CFG_RDY = 0x80;
4
+ const CFG_CONT = 0x10;
5
+ const VREF = 2.048;
6
+ const BITS_TO_S = { 12: 0, 14: 1, 16: 2, 18: 3 };
7
+ const GAIN_TO_G = { 1: 0, 2: 1, 4: 2, 8: 3 };
8
+ const CONV_S = { 12: 1 / 240, 14: 1 / 60, 16: 1 / 15, 18: 1 / 3.75 }; // nominal conversion times
9
+
10
+ const GC_RESET = Buffer.from([0x00, 0x06]);
11
+ const GC_CONVERT = Buffer.from([0x00, 0x08]);
12
+
13
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
14
+
15
+ /**
16
+ * MCP3422 2-channel 18-bit delta-sigma ADC with PGA (Microchip) — minimal
17
+ * interface.
18
+ *
19
+ * Two differential inputs, an on-board 2.048 V reference, a PGA (x1, x2, x4,
20
+ * x8) and an on-board oscillator. The resolution is selected together with the
21
+ * data rate: 12 bit / 240 SPS, 14 bit / 60 SPS, 16 bit / 15 SPS, 18 bit /
22
+ * 3.75 SPS. The full-scale differential input range is ±2.048 V / gain. The
23
+ * chip has no register map: a write is one configuration byte
24
+ * (`RDY C1 C0 O/C S1 S0 G1 G0`) and a read returns the 2 (12/14/16 bit) or 3
25
+ * (18 bit) result bytes followed by the configuration byte, whose RDY bit
26
+ * (0 = fresh result) says whether the conversion has finished. The driver
27
+ * therefore uses the plain Connection `write()` / `read()` calls.
28
+ *
29
+ * Defaults: one-shot mode (lowest power), 16 bit / 15 SPS (LSB 62.5 µV), gain
30
+ * x1. `init()` writes that configuration with RDY = 0, so nothing is started.
31
+ */
32
+ class MCP3422Minimal {
33
+ /**
34
+ * @param {import('../../connection/connection').Connection} connection - I²C connection (0x68–0x6F).
35
+ */
36
+ constructor(connection) {
37
+ this._conn = connection;
38
+ this._bits = 16;
39
+ this._gain = 1;
40
+ this._channel = 0;
41
+ this._continuous = false;
42
+ this._discard = false;
43
+ }
44
+
45
+ /** 7-bit I²C address of the standard part (order code A0). */
46
+ static get I2C_ADDRESS() { return 0x68; }
47
+
48
+ _config(rdy) {
49
+ let c = (this._channel << 5) | (BITS_TO_S[this._bits] << 2) | GAIN_TO_G[this._gain];
50
+ if (rdy) c |= CFG_RDY;
51
+ if (this._continuous) c |= CFG_CONT;
52
+ return c;
53
+ }
54
+
55
+ _nbytes() { return this._bits === 18 ? 3 : 2; }
56
+
57
+ _decode(data) {
58
+ let raw = 0;
59
+ for (let i = 0; i < this._nbytes(); i++) raw = (raw * 256) + data[i];
60
+ const span = 2 ** this._bits;
61
+ raw = raw % span;
62
+ if (raw >= span / 2) raw -= span;
63
+ return raw;
64
+ }
65
+
66
+ async _waitReady(convS) {
67
+ const polls = Math.floor((2 * convS + 0.010) / 0.005) + 1;
68
+ for (let i = 0; i < polls; i++) {
69
+ const data = await this._conn.read(this._nbytes() + 1);
70
+ if (!(data[this._nbytes()] & CFG_RDY)) return this._decode(data);
71
+ await sleep(5);
72
+ }
73
+ throw new Error('MCP3422 conversion did not finish');
74
+ }
75
+
76
+ async _convert(channel) {
77
+ this._channel = channel < 1 ? 0 : 1;
78
+ this._continuous = false;
79
+ this._discard = false;
80
+ const conv = CONV_S[this._bits];
81
+ await this._conn.write(Buffer.from([this._config(true)]));
82
+ await sleep(conv * 1100);
83
+ return this._waitReady(conv);
84
+ }
85
+
86
+ /**
87
+ * Write the default configuration (one-shot, 16 bit / 15 SPS, gain x1, CH1,
88
+ * RDY = 0); nothing is started and the chip goes to standby.
89
+ * @returns {Promise<void>}
90
+ */
91
+ async init() {
92
+ await this._conn.write(Buffer.from([this._config(false)]));
93
+ }
94
+
95
+ /**
96
+ * Run a one-shot conversion on a channel and return the signed code.
97
+ * @param {number} channel - 0 (CH1) or 1 (CH2); values outside 0–1 are clamped.
98
+ * @returns {Promise<number>} Signed code, −2^(N−1) … 2^(N−1) − 1 (N = resolution bits).
99
+ * @throws {Error} if the conversion does not finish in time.
100
+ */
101
+ async readRaw(channel) {
102
+ return this._convert(channel);
103
+ }
104
+
105
+ /**
106
+ * Run a one-shot conversion on a channel and return the differential voltage.
107
+ * @param {number} channel - 0 (CH1) or 1 (CH2); values outside 0–1 are clamped.
108
+ * @returns {Promise<number>} `CHn+ − CHn−` in volts, `raw * lsb / gain`.
109
+ */
110
+ async readVoltage(channel) {
111
+ return (await this._convert(channel)) * this.lsb() / this._gain;
112
+ }
113
+
114
+ /**
115
+ * Convert both channels, one conversion each.
116
+ * @returns {Promise<number[]>} `[CH1, CH2]` voltages in volts.
117
+ */
118
+ async readAll() {
119
+ return [await this.readVoltage(0), await this.readVoltage(1)];
120
+ }
121
+
122
+ /**
123
+ * Size of one code before the gain.
124
+ * @returns {number} `2 * 2.048 / 2^bits` in volts.
125
+ */
126
+ lsb() {
127
+ return 2 * VREF / (2 ** this._bits);
128
+ }
129
+ }
130
+
131
+ /**
132
+ * MCP3422 full interface — extends Minimal with resolution, gain and channel
133
+ * selection, continuous conversion, status reads, scans and general calls. The
134
+ * driver tracks resolution, gain, channel and mode.
135
+ */
136
+ class MCP3422Full extends MCP3422Minimal {
137
+ async _apply() {
138
+ if (this._continuous) {
139
+ await this._conn.write(Buffer.from([this._config(false)]));
140
+ this._discard = true;
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Select the resolution (and with it the data rate).
146
+ * @param {number} bits - 12 (240 SPS), 14 (60 SPS), 16 (15 SPS) or 18 (3.75 SPS).
147
+ * @returns {Promise<void>}
148
+ * @throws {RangeError} for any other value.
149
+ */
150
+ async setResolution(bits) {
151
+ if (!(bits in BITS_TO_S)) throw new RangeError('bits must be 12, 14, 16 or 18');
152
+ this._bits = bits;
153
+ await this._apply();
154
+ }
155
+
156
+ /**
157
+ * The selected resolution.
158
+ * @returns {number} 12, 14, 16 or 18.
159
+ */
160
+ getResolution() { return this._bits; }
161
+
162
+ /**
163
+ * Select the PGA gain.
164
+ * @param {number} gain - 1, 2, 4 or 8.
165
+ * @returns {Promise<void>}
166
+ * @throws {RangeError} for any other value.
167
+ */
168
+ async setGain(gain) {
169
+ if (!(gain in GAIN_TO_G)) throw new RangeError('gain must be 1, 2, 4 or 8');
170
+ this._gain = gain;
171
+ await this._apply();
172
+ }
173
+
174
+ /**
175
+ * The selected PGA gain.
176
+ * @returns {number} 1, 2, 4 or 8.
177
+ */
178
+ getGain() { return this._gain; }
179
+
180
+ /**
181
+ * Select the channel used by continuous conversion.
182
+ * @param {number} channel - 0 (CH1) or 1 (CH2).
183
+ * @returns {Promise<void>}
184
+ */
185
+ async setChannel(channel) {
186
+ this._channel = channel < 1 ? 0 : 1;
187
+ await this._apply();
188
+ }
189
+
190
+ /**
191
+ * Switch to continuous conversion at the current rate and gain.
192
+ * @param {number} [channel] - Optional channel 0 or 1 to select first.
193
+ * @returns {Promise<void>}
194
+ */
195
+ async startContinuous(channel) {
196
+ if (channel !== undefined && channel !== null) this._channel = channel < 1 ? 0 : 1;
197
+ this._continuous = true;
198
+ this._discard = true;
199
+ await this._conn.write(Buffer.from([this._config(false)]));
200
+ }
201
+
202
+ /**
203
+ * Return to one-shot mode / standby (writes O/C = 0, RDY = 0).
204
+ * @returns {Promise<void>}
205
+ */
206
+ async stopContinuous() {
207
+ this._continuous = false;
208
+ this._discard = false;
209
+ await this._conn.write(Buffer.from([this._config(false)]));
210
+ }
211
+
212
+ /**
213
+ * Report the conversion mode.
214
+ * @returns {boolean} true in continuous mode.
215
+ */
216
+ isContinuous() { return this._continuous; }
217
+
218
+ /**
219
+ * Wait for a fresh result in continuous mode and return the signed code. The
220
+ * first sample after a configuration change is discarded.
221
+ * @returns {Promise<number>}
222
+ * @throws {Error} if the driver is not in continuous mode or no result arrives in time.
223
+ */
224
+ async readContinuousRaw() {
225
+ if (!this._continuous) throw new Error('not in continuous mode; call startContinuous()');
226
+ const conv = CONV_S[this._bits];
227
+ if (this._discard) {
228
+ await this._waitReady(conv);
229
+ this._discard = false;
230
+ }
231
+ return this._waitReady(conv);
232
+ }
233
+
234
+ /**
235
+ * Voltage form of readContinuousRaw().
236
+ * @returns {Promise<number>} Differential voltage in volts.
237
+ */
238
+ async readContinuous() {
239
+ return (await this.readContinuousRaw()) * this.lsb() / this._gain;
240
+ }
241
+
242
+ /**
243
+ * Read the configuration byte (the last byte of a read).
244
+ * @returns {Promise<number>} Configuration byte; bit 7 is the live RDY flag.
245
+ */
246
+ async readConfig() {
247
+ const data = await this._conn.read(this._nbytes() + 1);
248
+ return data[this._nbytes()];
249
+ }
250
+
251
+ /**
252
+ * Report whether a fresh result is available (RDY = 0).
253
+ * @returns {Promise<boolean>}
254
+ */
255
+ async isReady() {
256
+ return !((await this.readConfig()) & CFG_RDY);
257
+ }
258
+
259
+ /**
260
+ * Run one one-shot conversion per entry, in order.
261
+ * @param {number[]} channels - Channels (0 or 1).
262
+ * @returns {Promise<number[]>} Voltages in volts, one per entry.
263
+ */
264
+ async readScan(channels) {
265
+ const out = [];
266
+ for (const ch of channels) out.push(await this.readVoltage(ch));
267
+ return out;
268
+ }
269
+
270
+ /**
271
+ * Send the general call reset (0x00 0x06): power-on defaults on the chip
272
+ * (continuous, 12 bit, gain x1, CH1); the driver state follows. Needs a
273
+ * Connection that transmits to the general-call address.
274
+ * @returns {Promise<void>}
275
+ */
276
+ async generalCallReset() {
277
+ await this._conn.write(GC_RESET);
278
+ this._bits = 12;
279
+ this._gain = 1;
280
+ this._channel = 0;
281
+ this._continuous = true;
282
+ this._discard = true;
283
+ }
284
+
285
+ /**
286
+ * Send the general call conversion (0x00 0x08): a one-shot conversion on every
287
+ * device on the bus; read the result with readPendingRaw().
288
+ * @returns {Promise<void>}
289
+ */
290
+ async generalCallConvert() {
291
+ await this._conn.write(GC_CONVERT);
292
+ this._continuous = false;
293
+ }
294
+
295
+ /**
296
+ * Wait for RDY = 0 and return the code of a conversion started elsewhere.
297
+ * @returns {Promise<number>}
298
+ */
299
+ async readPendingRaw() {
300
+ return this._waitReady(CONV_S[this._bits]);
301
+ }
302
+
303
+ /**
304
+ * Full-scale differential input range at the current gain.
305
+ * @returns {number} `2.048 / gain` in volts.
306
+ */
307
+ fullScale() {
308
+ return VREF / this._gain;
309
+ }
310
+ }
311
+
312
+ module.exports = { MCP3422Minimal, MCP3422Full };