periph 1.2.1 → 1.3.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 (123) hide show
  1. package/README.md +11 -0
  2. package/examples/accelerometer/bma150/complete.js +62 -0
  3. package/examples/accelerometer/bma150/demo.js +65 -0
  4. package/examples/accelerometer/bma150/minimal.js +18 -0
  5. package/examples/adc_dac/hy3131/complete.js +66 -0
  6. package/examples/adc_dac/hy3131/demo.js +37 -0
  7. package/examples/adc_dac/hy3131/minimal.js +18 -0
  8. package/examples/discovery/complete.js +25 -0
  9. package/examples/discovery/demo.js +30 -0
  10. package/examples/discovery/minimal.js +12 -0
  11. package/examples/display/il0373/complete.js +71 -0
  12. package/examples/display/il0373/demo.js +49 -0
  13. package/examples/display/il0373/minimal.js +26 -0
  14. package/examples/display/ssd1331/complete.js +83 -0
  15. package/examples/display/ssd1331/demo.js +49 -0
  16. package/examples/display/ssd1331/minimal.js +27 -0
  17. package/examples/display/st7036/complete.js +71 -0
  18. package/examples/display/st7036/demo.js +56 -0
  19. package/examples/display/st7036/minimal.js +21 -0
  20. package/examples/display/uc1611s/complete.js +135 -0
  21. package/examples/display/uc1611s/complete_spi.js +128 -0
  22. package/examples/display/uc1611s/demo.js +90 -0
  23. package/examples/display/uc1611s/demo_spi.js +91 -0
  24. package/examples/display/uc1611s/minimal.js +28 -0
  25. package/examples/display/uc1611s/minimal_spi.js +30 -0
  26. package/examples/display/uc8159/complete.js +64 -0
  27. package/examples/display/uc8159/demo.js +41 -0
  28. package/examples/display/uc8159/minimal.js +28 -0
  29. package/examples/display/uc8276c/complete.js +73 -0
  30. package/examples/display/uc8276c/demo.js +49 -0
  31. package/examples/display/uc8276c/minimal.js +26 -0
  32. package/examples/environmental/bme280/complete.js +1 -1
  33. package/examples/environmental/bme280/demo.js +1 -1
  34. package/examples/environmental/bme280/minimal.js +1 -1
  35. package/examples/gyroscope/l3g4200d/complete.js +1 -1
  36. package/examples/gyroscope/l3g4200d/demo.js +1 -1
  37. package/examples/gyroscope/l3g4200d/minimal.js +1 -1
  38. package/examples/memory/23x1024/complete.js +54 -0
  39. package/examples/memory/23x1024/demo.js +51 -0
  40. package/examples/memory/23x1024/minimal.js +22 -0
  41. package/examples/memory/mx25l12845e/complete.js +66 -0
  42. package/examples/memory/mx25l12845e/demo.js +66 -0
  43. package/examples/memory/mx25l12845e/minimal.js +27 -0
  44. package/examples/memory/sd_spi/complete.js +55 -0
  45. package/examples/memory/sd_spi/demo.js +67 -0
  46. package/examples/memory/sd_spi/minimal.js +32 -0
  47. package/examples/pressure/bmp280/complete.js +1 -1
  48. package/examples/pressure/bmp280/demo.js +1 -1
  49. package/examples/pressure/bmp280/minimal.js +1 -1
  50. package/examples/pressure/bmp581/complete.js +1 -1
  51. package/examples/pressure/bmp581/demo.js +1 -1
  52. package/examples/pressure/bmp581/minimal.js +1 -1
  53. package/examples/pressure/lps22df/complete.js +1 -1
  54. package/examples/pressure/lps22df/demo.js +1 -1
  55. package/examples/pressure/lps22df/minimal.js +1 -1
  56. package/package.json +1 -1
  57. package/src/chips/accelerometer/adxl345.js +10 -31
  58. package/src/chips/accelerometer/adxl362.js +7 -7
  59. package/src/chips/accelerometer/bma150.js +419 -0
  60. package/src/chips/adc_dac/ad7705.js +36 -9
  61. package/src/chips/adc_dac/ad7706.js +39 -9
  62. package/src/chips/adc_dac/hy3131.js +311 -0
  63. package/src/chips/display/il0373.js +607 -0
  64. package/src/chips/display/ssd1331.js +412 -0
  65. package/src/chips/display/st7036.js +401 -0
  66. package/src/chips/display/uc1611s.js +648 -0
  67. package/src/chips/display/uc8159.js +394 -0
  68. package/src/chips/display/uc8276c.js +658 -0
  69. package/src/chips/environmental/bme280.js +31 -46
  70. package/src/chips/environmental/bme680.js +38 -43
  71. package/src/chips/gas/ens160.js +18 -26
  72. package/src/chips/gyroscope/l3g4200d.js +34 -50
  73. package/src/chips/gyroscope/l3gd20h.js +24 -40
  74. package/src/chips/imu/mpu6050.js +29 -41
  75. package/src/chips/imu/mpu9250.js +42 -66
  76. package/src/chips/imu/mpu9255.js +51 -75
  77. package/src/chips/io_expander/mcp23017.js +24 -33
  78. package/src/chips/light/apds-9930.js +52 -60
  79. package/src/chips/light/apds9960.js +69 -77
  80. package/src/chips/magnetometer/as5600.js +13 -27
  81. package/src/chips/magnetometer/hmc5883l.js +32 -29
  82. package/src/chips/memory/_23x1024.js +228 -0
  83. package/src/chips/memory/_24aa02uid.js +8 -11
  84. package/src/chips/memory/mx25l12845e.js +467 -0
  85. package/src/chips/memory/sd_spi.js +486 -0
  86. package/src/chips/motor/drv8830.js +4 -4
  87. package/src/chips/other/mpr121.js +7 -7
  88. package/src/chips/power/ina219.js +5 -8
  89. package/src/chips/power/ina226.js +5 -8
  90. package/src/chips/power/ina3221.js +5 -8
  91. package/src/chips/pressure/bmp085.js +17 -11
  92. package/src/chips/pressure/bmp180.js +7 -7
  93. package/src/chips/pressure/bmp280.js +21 -33
  94. package/src/chips/pressure/bmp384.js +28 -39
  95. package/src/chips/pressure/bmp581.js +59 -70
  96. package/src/chips/pressure/lps22df.js +50 -52
  97. package/src/chips/pressure/lps28dfw.js +15 -13
  98. package/src/chips/pressure/lps33hw.js +7 -5
  99. package/src/chips/rtc/ds3231.js +8 -8
  100. package/src/chips/rtc/pcf8523.js +8 -14
  101. package/src/chips/temperature/mcp9808.js +9 -7
  102. package/src/chips/temperature/tmp117.js +4 -4
  103. package/src/connection/connection.js +6 -4
  104. package/src/connection/dhtxx.js +5 -3
  105. package/src/connection/display.js +278 -0
  106. package/src/connection/display_mock.js +77 -0
  107. package/src/connection/hx711.js +5 -3
  108. package/src/connection/i2c.js +6 -4
  109. package/src/connection/i2c_mock.js +47 -0
  110. package/src/connection/input_pin.js +36 -5
  111. package/src/connection/register.js +14 -0
  112. package/src/connection/register_connection.js +65 -0
  113. package/src/connection/sipo.js +4 -2
  114. package/src/connection/sipo_mock.js +49 -0
  115. package/src/connection/smbus.js +6 -4
  116. package/src/connection/softspi.js +73 -0
  117. package/src/connection/spi.js +110 -23
  118. package/src/connection/spi_mock.js +50 -0
  119. package/src/connection/uart.js +2 -1
  120. package/src/discovery/discovery.js +218 -0
  121. package/src/discovery/registry.js +85 -0
  122. package/src/storage/block_device.js +95 -0
  123. package/src/storage/ram_block_device.js +72 -0
@@ -0,0 +1,658 @@
1
+ 'use strict';
2
+
3
+ const BLACK = 0;
4
+ const WHITE = 1;
5
+ const RED = 2;
6
+
7
+ const MODE_KWR = 0;
8
+ const MODE_KW = 1;
9
+
10
+ const _CMD_PSR = 0x00;
11
+ const _CMD_PWR = 0x01;
12
+ const _CMD_POF = 0x02;
13
+ const _CMD_PON = 0x04;
14
+ const _CMD_BTST = 0x06;
15
+ const _CMD_DSLP = 0x07;
16
+ const _CMD_DTM1 = 0x10;
17
+ const _CMD_DRF = 0x12;
18
+ const _CMD_DTM2 = 0x13;
19
+ const _CMD_AUTO = 0x17;
20
+ const _CMD_LUTC = 0x20;
21
+ const _CMD_LUTWW = 0x21;
22
+ const _CMD_LUTKW = 0x22;
23
+ const _CMD_LUTWK = 0x23;
24
+ const _CMD_LUTKK = 0x24;
25
+ const _CMD_PLL = 0x30;
26
+ const _CMD_TSC = 0x40;
27
+ const _CMD_TSE = 0x41;
28
+ const _CMD_CDI = 0x50;
29
+ const _CMD_LPD = 0x51;
30
+ const _CMD_TRES = 0x61;
31
+ const _CMD_REV = 0x70;
32
+ const _CMD_FLG = 0x71;
33
+ const _CMD_VV = 0x81;
34
+ const _CMD_VDCS = 0x82;
35
+ const _CMD_PTL = 0x90;
36
+ const _CMD_PTIN = 0x91;
37
+ const _CMD_PTOUT = 0x92;
38
+ const _CMD_ROTP = 0xA2;
39
+
40
+ const MAX_WIDTH = 400;
41
+ const MAX_HEIGHT = 300;
42
+
43
+ const _ZEROS = Buffer.alloc(512);
44
+
45
+ // Frame rate in Hz for PLL M (1..7) and N (1..7), panel datasheet p. 24.
46
+ const _PLL_HZ = [
47
+ [29, 14, 10, 7, 6, 5, 4],
48
+ [57, 29, 19, 14, 11, 10, 8],
49
+ [86, 43, 29, 21, 17, 14, 12],
50
+ [114, 57, 38, 29, 23, 19, 16],
51
+ [150, 72, 48, 36, 29, 24, 20],
52
+ [171, 86, 57, 43, 34, 29, 24],
53
+ [200, 100, 67, 50, 40, 33, 29],
54
+ ];
55
+
56
+ function _code(value, base, step, maxCode) {
57
+ const c = Math.floor((value - base) / step + 0.5);
58
+ return Math.max(0, Math.min(maxCode, c));
59
+ }
60
+
61
+ /**
62
+ * UC8276C black/white/red e-paper controller — minimal interface.
63
+ *
64
+ * Drives a 400 x 300 (up to 400 source x 300 gate) white/black/red
65
+ * electrophoretic panel such as the Waveshare 4.2inch e-Paper (B). The driver
66
+ * keeps the picture in framebuffers and sends it on show(). Every update is
67
+ * the datasheet sequence DTM1 + DTM2 -> PON -> DRF -> POF, waiting on BUSY_N
68
+ * after each power/refresh step; show() resolves only when the whole refresh
69
+ * is done (about 17 s on the reference panel).
70
+ *
71
+ * The connection is a DisplayConnection (see specs/transport_display.md) that
72
+ * owns CS#, D/C#, RES# and BUSY_N. BUSY_N is active low.
73
+ *
74
+ * The constructor starts the initialisation sequence; every method waits for
75
+ * it, and `ready()` resolves (or rejects, e.g. on a BUSY_N timeout) when it is
76
+ * done.
77
+ *
78
+ * Pixel polarity: the framebuffer mapping (B/W bit 1 = white, red bit 1 = red)
79
+ * holds for CDI.DDX = 01, which the driver writes explicitly at
80
+ * initialisation instead of relying on the OTP default.
81
+ *
82
+ * @param {object} connection - A DisplayConnection (SIMPLEX wiring is enough).
83
+ * @param {object} [options]
84
+ * @param {number} [options.width=400] - Pixels per row, a multiple of 8, 8..400.
85
+ * @param {number} [options.height=300] - Rows, 1..300.
86
+ * @param {number|null} [options.busyTimeoutMs=30000] - Limit for every BUSY_N wait in
87
+ * milliseconds, or null for no limit. The full refresh was measured at 16.8 s.
88
+ * @throws {RangeError} If width or height is out of range.
89
+ */
90
+ class UC8276CMinimal {
91
+ constructor(connection, { width = MAX_WIDTH, height = MAX_HEIGHT, busyTimeoutMs = 30000 } = {}) {
92
+ if (!Number.isInteger(width) || width < 8 || width > MAX_WIDTH || width % 8 !== 0) {
93
+ throw new RangeError(`width must be a multiple of 8 in 8..${MAX_WIDTH}`);
94
+ }
95
+ if (!Number.isInteger(height) || height < 1 || height > MAX_HEIGHT) {
96
+ throw new RangeError(`height must be in 1..${MAX_HEIGHT}`);
97
+ }
98
+ this._conn = connection;
99
+ this._width = width;
100
+ this._height = height;
101
+ this._timeout = busyTimeoutMs;
102
+ this._rowBytes = width / 8;
103
+ this._size = this._rowBytes * height;
104
+ this._bw = Buffer.alloc(this._size, 0xFF);
105
+ this._red = null;
106
+ this._psr = 0x0F;
107
+ this._asleep = false;
108
+ this._ready = this._initSequence();
109
+ this._ready.catch(() => {}); // surfaced through ready() and the first awaited call
110
+ }
111
+
112
+ /** @returns {number} Panel width in pixels. */
113
+ get width() { return this._width; }
114
+
115
+ /** @returns {number} Panel height in pixels. */
116
+ get height() { return this._height; }
117
+
118
+ /**
119
+ * Resolves when initialisation has finished; rejects if it failed.
120
+ * @returns {Promise<void>}
121
+ */
122
+ ready() { return this._ready; }
123
+
124
+ /**
125
+ * Fill the framebuffers with one colour. The panel is unchanged until show().
126
+ * @param {number} [color=WHITE] - WHITE, BLACK or RED.
127
+ */
128
+ clear(color = WHITE) {
129
+ this._bw.fill(color === WHITE ? 0xFF : 0x00);
130
+ if (color === RED) {
131
+ this._red = Buffer.alloc(this._size, 0xFF);
132
+ } else if (this._red !== null) {
133
+ this._red.fill(0);
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Set one pixel in the framebuffer. Out-of-range coordinates are ignored.
139
+ * The red plane is allocated on the first RED pixel.
140
+ * @param {number} x - Column, 0 <= x < width.
141
+ * @param {number} y - Row, 0 <= y < height.
142
+ * @param {number} [color=BLACK] - BLACK, WHITE or RED.
143
+ */
144
+ setPixel(x, y, color = BLACK) {
145
+ if (x < 0 || y < 0 || x >= this._width || y >= this._height) return;
146
+ const index = y * this._rowBytes + (x >> 3);
147
+ const mask = 0x80 >> (x & 7);
148
+ if (color === WHITE) this._bw[index] |= mask;
149
+ else this._bw[index] &= ~mask & 0xFF;
150
+ if (color === RED) {
151
+ if (this._red === null) this._red = Buffer.alloc(this._size);
152
+ this._red[index] |= mask;
153
+ } else if (this._red !== null) {
154
+ this._red[index] &= ~mask & 0xFF;
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Write both planes to the controller and run a full display update.
160
+ *
161
+ * DTM1, DTM2, PON, wait BUSY_N, DRF, wait BUSY_N, POF, wait BUSY_N. A
162
+ * sleeping controller is woken (hardware reset + initialisation) first.
163
+ * @returns {Promise<void>}
164
+ * @throws {Error} If a BUSY_N wait exceeds busyTimeoutMs.
165
+ */
166
+ async show() {
167
+ await this._ready;
168
+ if (this._asleep) await this._wake();
169
+ await this._writePlanes();
170
+ await this._powerCycleRefresh();
171
+ }
172
+
173
+ /**
174
+ * Put the controller into deep sleep (DSLP with check code 0xA5).
175
+ * Only a hardware reset restores communication; show() and Full.wake() do
176
+ * that. The panel keeps its image.
177
+ * @returns {Promise<void>}
178
+ */
179
+ async sleep() {
180
+ await this._ready;
181
+ await this._conn.sendCommand(_CMD_DSLP, [0xA5]);
182
+ this._asleep = true;
183
+ }
184
+
185
+ async _waitReady(timeoutMs = this._timeout) {
186
+ if (!(await this._conn.waitBusy(false, timeoutMs))) {
187
+ throw new Error(`UC8276C: BUSY_N did not go high within ${timeoutMs} ms`);
188
+ }
189
+ }
190
+
191
+ async _initSequence() {
192
+ if (typeof this._conn.ready === 'function') await this._conn.ready();
193
+ await this._conn.reset();
194
+ await this._waitReady();
195
+ await this._conn.sendCommand(_CMD_PSR, [this._psr]);
196
+ if (this._width !== MAX_WIDTH || this._height !== MAX_HEIGHT) {
197
+ await this._conn.sendCommand(_CMD_TRES, [
198
+ (this._width >> 8) & 1, this._width & 0xF8,
199
+ (this._height >> 8) & 1, this._height & 0xFF,
200
+ ]);
201
+ }
202
+ // VBD = 11, DDX = 01, CDI = 0111: the framebuffer polarity holds
203
+ await this._conn.sendCommand(_CMD_CDI, [0xD7]);
204
+ }
205
+
206
+ async _wake() {
207
+ this._asleep = false;
208
+ await this._initSequence();
209
+ }
210
+
211
+ async _sendZeros(total) {
212
+ while (total > 0) {
213
+ const n = Math.min(total, _ZEROS.length);
214
+ await this._conn.sendData(_ZEROS.subarray(0, n));
215
+ total -= n;
216
+ }
217
+ }
218
+
219
+ async _sendPlane(cmd, plane) {
220
+ await this._conn.sendCommand(cmd);
221
+ if (plane === null) await this._sendZeros(this._size);
222
+ else await this._conn.sendData(plane);
223
+ }
224
+
225
+ async _writePlanes() {
226
+ await this._sendPlane(_CMD_DTM1, this._bw);
227
+ await this._sendPlane(_CMD_DTM2, this._red);
228
+ }
229
+
230
+ async _powerCycleRefresh() {
231
+ await this._conn.sendCommand(_CMD_PON);
232
+ await this._waitReady();
233
+ await this._conn.sendCommand(_CMD_DRF);
234
+ await this._waitReady();
235
+ await this._conn.sendCommand(_CMD_POF);
236
+ await this._waitReady();
237
+ }
238
+ }
239
+
240
+ /**
241
+ * UC8276C black/white/red e-paper controller — full interface.
242
+ *
243
+ * Extends UC8276CMinimal with drawing helpers, partial-window updates, the
244
+ * auto sequence, pixel mode / scan direction, power and VCOM settings, frame
245
+ * rate, user LUTs and the read commands (status, temperature, revision, VCOM,
246
+ * low power, OTP).
247
+ *
248
+ * Register layouts follow the panel datasheet where it disagrees with the chip
249
+ * datasheet (PWR, PLL, VCOM_DC); see specs/display/uc8276c.md. The read*
250
+ * methods need a half-duplex wire mode (the controller drives the shared
251
+ * DIN/DOUT line); with a SIMPLEX connection they reject.
252
+ */
253
+ class UC8276CFull extends UC8276CMinimal {
254
+ // -- framebuffer ------------------------------------------------------
255
+
256
+ /**
257
+ * Colour of one framebuffer pixel.
258
+ * @param {number} x - Column.
259
+ * @param {number} y - Row.
260
+ * @returns {number} BLACK, WHITE or RED (BLACK when out of range).
261
+ */
262
+ getPixel(x, y) {
263
+ if (x < 0 || y < 0 || x >= this._width || y >= this._height) return BLACK;
264
+ const index = y * this._rowBytes + (x >> 3);
265
+ const mask = 0x80 >> (x & 7);
266
+ if (this._red !== null && (this._red[index] & mask)) return RED;
267
+ return (this._bw[index] & mask) ? WHITE : BLACK;
268
+ }
269
+
270
+ /**
271
+ * Bresenham line into the framebuffer.
272
+ * @param {number} x0 - Start column.
273
+ * @param {number} y0 - Start row.
274
+ * @param {number} x1 - End column.
275
+ * @param {number} y1 - End row.
276
+ * @param {number} [color=BLACK] - BLACK, WHITE or RED.
277
+ */
278
+ drawLine(x0, y0, x1, y1, color = BLACK) {
279
+ const dx = Math.abs(x1 - x0);
280
+ const dy = -Math.abs(y1 - y0);
281
+ const sx = x0 < x1 ? 1 : -1;
282
+ const sy = y0 < y1 ? 1 : -1;
283
+ let err = dx + dy;
284
+ for (;;) {
285
+ this.setPixel(x0, y0, color);
286
+ if (x0 === x1 && y0 === y1) break;
287
+ const e2 = 2 * err;
288
+ if (e2 >= dy) { err += dy; x0 += sx; }
289
+ if (e2 <= dx) { err += dx; y0 += sy; }
290
+ }
291
+ }
292
+
293
+ /**
294
+ * Rectangle outline or filled rectangle into the framebuffer.
295
+ * @param {number} x - Left column.
296
+ * @param {number} y - Top row.
297
+ * @param {number} w - Width in pixels.
298
+ * @param {number} h - Height in pixels.
299
+ * @param {number} [color=BLACK] - BLACK, WHITE or RED.
300
+ * @param {boolean} [fill=false] - true fills the rectangle.
301
+ */
302
+ drawRect(x, y, w, h, color = BLACK, fill = false) {
303
+ if (w <= 0 || h <= 0) return;
304
+ if (fill) {
305
+ for (let j = 0; j < h; j++) {
306
+ for (let i = 0; i < w; i++) this.setPixel(x + i, y + j, color);
307
+ }
308
+ return;
309
+ }
310
+ this.drawLine(x, y, x + w - 1, y, color);
311
+ this.drawLine(x, y + h - 1, x + w - 1, y + h - 1, color);
312
+ this.drawLine(x, y, x, y + h - 1, color);
313
+ this.drawLine(x + w - 1, y, x + w - 1, y + h - 1, color);
314
+ }
315
+
316
+ /**
317
+ * Copy a 1-bit-per-pixel bitmap into the framebuffer. Row-major, MSB-first,
318
+ * rows padded to whole bytes. Set bits are drawn in color, clear bits leave
319
+ * the framebuffer unchanged.
320
+ * @param {number} x - Left column.
321
+ * @param {number} y - Top row.
322
+ * @param {Buffer|Uint8Array|number[]} data - Bitmap bytes.
323
+ * @param {number} w - Bitmap width in pixels.
324
+ * @param {number} h - Bitmap height in pixels.
325
+ * @param {number} [color=BLACK] - BLACK, WHITE or RED.
326
+ */
327
+ blit(x, y, data, w, h, color = BLACK) {
328
+ if (w <= 0 || h <= 0) return;
329
+ const rowBytes = Math.ceil(w / 8);
330
+ for (let j = 0; j < h; j++) {
331
+ for (let i = 0; i < w; i++) {
332
+ if (data[j * rowBytes + (i >> 3)] & (0x80 >> (i & 7))) this.setPixel(x + i, y + j, color);
333
+ }
334
+ }
335
+ }
336
+
337
+ // -- updates ----------------------------------------------------------
338
+
339
+ /**
340
+ * Update only a window: write just its rows, then refresh.
341
+ *
342
+ * PTIN, PTL (window widened to whole 8-pixel columns), the window's bytes of
343
+ * both planes, PON, DRF, POF, PTOUT. Saves SPI time; the waveform is the OTP
344
+ * full waveform, so the panel still flashes.
345
+ * @param {number} x - Left column.
346
+ * @param {number} y - Top row.
347
+ * @param {number} w - Width in pixels.
348
+ * @param {number} h - Height in pixels.
349
+ * @returns {Promise<void>}
350
+ * @throws {RangeError} If the window is empty or outside the panel.
351
+ */
352
+ async showRegion(x, y, w, h) {
353
+ if (w <= 0 || h <= 0) throw new RangeError('empty window');
354
+ if (x < 0) { w += x; x = 0; }
355
+ if (y < 0) { h += y; y = 0; }
356
+ if (x >= this._width || y >= this._height || w <= 0 || h <= 0) {
357
+ throw new RangeError('window outside the panel');
358
+ }
359
+ const x0 = x & ~7;
360
+ const x1 = Math.min((x + w - 1) | 7, this._width - 1);
361
+ const y0 = y;
362
+ const y1 = Math.min(y + h - 1, this._height - 1);
363
+ await this._ready;
364
+ if (this._asleep) await this._wake();
365
+ await this._conn.sendCommand(_CMD_PTIN);
366
+ await this._conn.sendCommand(_CMD_PTL, [
367
+ (x0 >> 8) & 1, x0 & 0xF8,
368
+ (x1 >> 8) & 1, (x1 & 0xF8) | 0x07,
369
+ (y0 >> 8) & 1, y0 & 0xFF,
370
+ (y1 >> 8) & 1, y1 & 0xFF,
371
+ 0x00, // PT_SCAN = 0: scan only inside the window
372
+ ]);
373
+ const winBytes = (x1 - x0 + 1) / 8;
374
+ for (const [cmd, plane] of [[_CMD_DTM1, this._bw], [_CMD_DTM2, this._red]]) {
375
+ await this._conn.sendCommand(cmd);
376
+ for (let row = y0; row <= y1; row++) {
377
+ if (plane === null) {
378
+ await this._sendZeros(winBytes);
379
+ } else {
380
+ const start = row * this._rowBytes + (x0 >> 3);
381
+ await this._conn.sendData(plane.subarray(start, start + winBytes));
382
+ }
383
+ }
384
+ }
385
+ try {
386
+ await this._powerCycleRefresh();
387
+ } finally {
388
+ await this._conn.sendCommand(_CMD_PTOUT);
389
+ }
390
+ }
391
+
392
+ /**
393
+ * Full-frame update using the AUTO sequence instead of PON/DRF/POF.
394
+ *
395
+ * AUTO is listed only in the chip datasheet. On the reference panel
396
+ * (Waveshare 4.2inch e-Paper (B)) it starts no refresh: the call returns
397
+ * after the plane transfer and the panel keeps its picture. Use show()
398
+ * unless your panel is known to accept it.
399
+ * @param {boolean} [deepSleep=false] - true runs AUTO 0xA7 (also enters deep sleep afterwards).
400
+ * @returns {Promise<void>}
401
+ */
402
+ async refreshAuto(deepSleep = false) {
403
+ await this._ready;
404
+ if (this._asleep) await this._wake();
405
+ await this._writePlanes();
406
+ await this._conn.sendCommand(_CMD_AUTO, [deepSleep ? 0xA7 : 0xA5]);
407
+ await this._waitReady();
408
+ if (deepSleep) this._asleep = true;
409
+ }
410
+
411
+ /**
412
+ * Hardware reset and initialisation (exit from deep sleep). Framebuffers are kept.
413
+ * @returns {Promise<void>}
414
+ */
415
+ async wake() {
416
+ await this._ready;
417
+ await this._wake();
418
+ }
419
+
420
+ /**
421
+ * Wait for BUSY_N to go high.
422
+ * @param {number|null} [timeoutMs] - Limit in milliseconds, null for no limit;
423
+ * the constructor's busyTimeoutMs when omitted.
424
+ * @returns {Promise<boolean>} true when ready, false on timeout.
425
+ */
426
+ async waitBusy(timeoutMs = this._timeout) {
427
+ await this._ready;
428
+ return this._conn.waitBusy(false, timeoutMs);
429
+ }
430
+
431
+ // -- settings ---------------------------------------------------------
432
+
433
+ /**
434
+ * Select the pixel mode.
435
+ * @param {number} mode - MODE_KWR (black/white/red, default) or MODE_KW
436
+ * (black/white with NEW/OLD; needs KW LUTs in OTP).
437
+ * @returns {Promise<void>}
438
+ */
439
+ async setMode(mode) {
440
+ if (mode === MODE_KW) this._psr |= 0x10;
441
+ else this._psr &= ~0x10 & 0xFF;
442
+ await this._writePsr();
443
+ }
444
+
445
+ /**
446
+ * Mirror the picture: horizontal clears SHL, vertical clears UD. The
447
+ * framebuffer is unchanged.
448
+ * @param {boolean} horizontal - Mirror left/right.
449
+ * @param {boolean} vertical - Mirror top/bottom.
450
+ * @returns {Promise<void>}
451
+ */
452
+ async setFlip(horizontal, vertical) {
453
+ if (horizontal) this._psr &= ~0x04 & 0xFF;
454
+ else this._psr |= 0x04;
455
+ if (vertical) this._psr &= ~0x08 & 0xFF;
456
+ else this._psr |= 0x08;
457
+ await this._writePsr();
458
+ }
459
+
460
+ /**
461
+ * Source/gate power (PWR, panel-datasheet layout, internal DC-DC).
462
+ *
463
+ * Advanced: the values must match the panel vendor's; a wrong set can drive
464
+ * the panel outside its rating. The driver does not write PWR by default.
465
+ * @param {number} vdh - Positive source voltage, 2.4..11.0 V (0.2 V steps).
466
+ * @param {number} vdl - Negative source voltage magnitude, 2.4..11.0 V.
467
+ * @param {number} vdhr - Red source voltage, 2.4..11.0 V.
468
+ * @param {number} [vgh=16] - Gate voltage magnitude: 16, 15, 14 or 13 V (VGL = -VGH).
469
+ * @returns {Promise<void>}
470
+ */
471
+ async setPower(vdh, vdl, vdhr, vgh = 16.0) {
472
+ await this._ready;
473
+ const lv = Math.max(0, Math.min(3, Math.floor(16.0 - vgh + 0.5)));
474
+ await this._conn.sendCommand(_CMD_PWR, [
475
+ 0x03, // VDS_EN | VDG_EN
476
+ lv, // VCOM_HV = 0, VGHL_LV[1:0]
477
+ _code(vdh, 2.4, 0.2, 0x2B),
478
+ _code(Math.abs(vdl), 2.4, 0.2, 0x2B),
479
+ _code(vdhr, 2.4, 0.2, 0x2B),
480
+ ]);
481
+ }
482
+
483
+ /**
484
+ * VCOM_DC (VDCS): -0.10 .. -3.25 V in 0.05 V steps (panel layout).
485
+ * @param {number} volts - VCOM voltage, e.g. the value printed on the panel label.
486
+ * @returns {Promise<void>}
487
+ */
488
+ async setVcomDc(volts) {
489
+ await this._ready;
490
+ await this._conn.sendCommand(_CMD_VDCS, [_code(-0.10 - volts, 0.0, 0.05, 0x3F)]);
491
+ }
492
+
493
+ /**
494
+ * Frame rate (PLL, M/N layout): the nearest rate of the panel's table.
495
+ * @param {number} hz - Requested rate, 4..200 Hz.
496
+ * @returns {Promise<void>}
497
+ */
498
+ async setFrameRate(hz) {
499
+ await this._ready;
500
+ let best = 0x3C; // M = 7, N = 4 (50 Hz)
501
+ let bestDiff = 10000;
502
+ for (let m = 1; m <= 7; m++) {
503
+ for (let n = 1; n <= 7; n++) {
504
+ const diff = Math.abs(_PLL_HZ[m - 1][n - 1] - hz);
505
+ if (diff < bestDiff) { bestDiff = diff; best = (m << 3) | n; }
506
+ }
507
+ }
508
+ await this._conn.sendCommand(_CMD_PLL, [best]);
509
+ }
510
+
511
+ /**
512
+ * Booster soft start (BTST).
513
+ * @param {number} a - Phase A byte.
514
+ * @param {number} b - Phase B byte.
515
+ * @param {number} c - Phase C byte.
516
+ * @returns {Promise<void>}
517
+ */
518
+ async setSoftStart(a, b, c) {
519
+ await this._ready;
520
+ await this._conn.sendCommand(_CMD_BTST, [a, b, c]);
521
+ }
522
+
523
+ /**
524
+ * VCOM and data interval (CDI).
525
+ * @param {number} cdi - Interval, 0..15 (0b0111 = 10 hsync, the default).
526
+ * @param {number} [border=3] - Border data selection VBD, 0..3 (3 = floating in KWR).
527
+ * @param {number} [polarity=1] - Data polarity DDX, 0..3. The framebuffer mapping
528
+ * assumes 1; changing it is the caller's responsibility.
529
+ * @returns {Promise<void>}
530
+ */
531
+ async setInterval(cdi, border = 3, polarity = 1) {
532
+ await this._ready;
533
+ await this._conn.sendCommand(_CMD_CDI, [((border & 3) << 6) | ((polarity & 3) << 4) | (cdi & 0x0F)]);
534
+ }
535
+
536
+ /**
537
+ * Temperature sensor source (TSE): internal (default) or external LM75.
538
+ * @param {boolean} external - true selects the external sensor.
539
+ * @param {number} [offset=0] - Temperature offset, -8..+7 degrees Celsius.
540
+ * @returns {Promise<void>}
541
+ */
542
+ async setTemperatureSensor(external, offset = 0) {
543
+ await this._ready;
544
+ await this._conn.sendCommand(_CMD_TSE, [(external ? 0x80 : 0x00) | (offset & 0x0F)]);
545
+ }
546
+
547
+ /**
548
+ * Load user waveform tables and select them (PSR.REG = 1).
549
+ *
550
+ * The LUT commands exist only in the chip datasheet; verify the panel
551
+ * accepts them. Lengths: lutC 56, lutWW 42, lutKW 56, lutWK 56, lutKK 56
552
+ * bytes. Any table may be null to leave it alone.
553
+ * @param {Buffer|null} lutC - VCOM LUT.
554
+ * @param {Buffer|null} lutWW - White-to-white LUT.
555
+ * @param {Buffer|null} lutKW - Black-to-white (red in KWR) LUT.
556
+ * @param {Buffer|null} lutWK - White-to-black LUT.
557
+ * @param {Buffer|null} lutKK - Black-to-black LUT.
558
+ * @returns {Promise<void>}
559
+ */
560
+ async writeLut(lutC, lutWW, lutKW, lutWK, lutKK) {
561
+ await this._ready;
562
+ const tables = [[_CMD_LUTC, lutC], [_CMD_LUTWW, lutWW], [_CMD_LUTKW, lutKW],
563
+ [_CMD_LUTWK, lutWK], [_CMD_LUTKK, lutKK]];
564
+ for (const [cmd, table] of tables) {
565
+ if (table) await this._conn.sendCommand(cmd, table);
566
+ }
567
+ this._psr |= 0x20;
568
+ await this._writePsr();
569
+ }
570
+
571
+ /**
572
+ * Send a raw command and parameters (escape hatch).
573
+ * @param {number} command - Command byte.
574
+ * @param {Buffer|number[]} [data=[]] - Parameter bytes.
575
+ * @returns {Promise<void>}
576
+ */
577
+ async sendCommand(command, data = []) {
578
+ await this._ready;
579
+ await this._conn.sendCommand(command, data);
580
+ }
581
+
582
+ // -- reads (half-duplex wiring only) ----------------------------------
583
+
584
+ /**
585
+ * Read FLG: bit 6 PTL_FLAG, 5 I2C_ERR, 4 I2C_BUSYN, 3 data_flag, 2 PON, 1 POF, 0 BUSY_N.
586
+ * @returns {Promise<number>} The status byte.
587
+ */
588
+ async readStatus() {
589
+ return (await this._read(_CMD_FLG, 1))[0];
590
+ }
591
+
592
+ /**
593
+ * Read the internal temperature sensor (TSC).
594
+ * @returns {Promise<number>} Signed degrees Celsius.
595
+ */
596
+ async readTemperature() {
597
+ const v = (await this._read(_CMD_TSC, 2))[0];
598
+ return v > 127 ? v - 256 : v;
599
+ }
600
+
601
+ /**
602
+ * Read REV. The panel datasheet gives a 1-byte LUT revision, the chip
603
+ * datasheet 5 bytes (reserved, CHIP_REV, LUT_REV[7:0], [15:8], [23:16]).
604
+ * @param {number} [length=1] - 1 (panel form) or 5 (chip form).
605
+ * @returns {Promise<Array>} [chipRev, lutVersion], or [null, lutRev] for the 1-byte form.
606
+ */
607
+ async readRevision(length = 1) {
608
+ const raw = await this._read(_CMD_REV, length);
609
+ if (length === 1) return [null, raw[0]];
610
+ return [raw[1], raw[2] | (raw[3] << 8) | (raw[4] << 16)];
611
+ }
612
+
613
+ /**
614
+ * Read the measured VCOM (VV, panel layout).
615
+ * @returns {Promise<number>} Volts, -0.10 V - 0.05 V x VV[5:0].
616
+ */
617
+ async readVcom() {
618
+ return -0.10 - 0.05 * ((await this._read(_CMD_VV, 1))[0] & 0x3F);
619
+ }
620
+
621
+ /**
622
+ * Read low-power detection (LPD, with LPMD = 0).
623
+ * @returns {Promise<boolean>} true when the supply is low.
624
+ */
625
+ async readLowPower() {
626
+ return ((await this._read(_CMD_LPD, 1))[0] & 1) === 0;
627
+ }
628
+
629
+ /**
630
+ * Read OTP bytes from address 0 (ROTP; the dummy byte is dropped).
631
+ * @param {number} length - Number of bytes.
632
+ * @returns {Promise<Buffer>} The OTP contents.
633
+ */
634
+ async readOtp(length) {
635
+ await this._ready;
636
+ await this._conn.sendCommand(_CMD_ROTP);
637
+ await this._conn.readData(1);
638
+ return this._conn.readData(length);
639
+ }
640
+
641
+ // -- internals --------------------------------------------------------
642
+
643
+ async _writePsr() {
644
+ await this._ready;
645
+ await this._conn.sendCommand(_CMD_PSR, [this._psr]);
646
+ }
647
+
648
+ async _read(cmd, length) {
649
+ await this._ready;
650
+ await this._conn.sendCommand(cmd);
651
+ return this._conn.readData(length);
652
+ }
653
+ }
654
+
655
+ module.exports = {
656
+ UC8276CMinimal, UC8276CFull,
657
+ BLACK, WHITE, RED, MODE_KWR, MODE_KW, MAX_WIDTH, MAX_HEIGHT,
658
+ };