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,452 @@
1
+ 'use strict';
2
+
3
+ const _ADDR_SA0_LOW = 0x3C;
4
+ const _ADDR_SA0_HIGH = 0x3D;
5
+
6
+ const _CMD_SET_MODE = 0x40;
7
+ const _CMD_DEVICE_SELECT = 0x60;
8
+ const _CMD_RAM_ACCESS = 0x70;
9
+ const _CMD_SET_START_BANK = 0x7C;
10
+ const _CONT = 0x80;
11
+
12
+ const STATUS_BLANK = 0;
13
+ const STATUS_NORMAL = 1;
14
+ const STATUS_ALL_ON = 2;
15
+ const STATUS_INVERSE = 3;
16
+
17
+ const ACCESS_CHARACTER = 0;
18
+ const ACCESS_HALF_GRAPHIC = 1;
19
+ const ACCESS_FULL_GRAPHIC = 2;
20
+
21
+ const RAM_COLUMNS = 40;
22
+ const RAM_BANKS = 4;
23
+
24
+ // multiplex rate (rows) -> M[1:0]
25
+ const _MUX_CODE = { 8: 1, 16: 2, 24: 3, 32: 0 };
26
+ const _PER_COL = [1, 2, 4];
27
+
28
+ /**
29
+ * PCF8578 row/column LCD dot-matrix driver — minimal interface.
30
+ *
31
+ * Drives a stand-alone dot-matrix LCD (mixed mode: the chip drives both rows and columns) out
32
+ * of the box. With `rows` rows the panel is `40 - rows` columns wide (8x32, 16x24, 24x16 or
33
+ * 32x8). The chip has no register map; this driver streams command bytes and display data with
34
+ * the plain connection `write` / `writeRead` calls. The I2C address is 0x3C (SA0 = VSS) or 0x3D
35
+ * (SA0 = VDD); the chip supports 100 kHz at most.
36
+ *
37
+ * The constructor starts the initialisation (waits for the internal reset, clears the whole
38
+ * display RAM, then switches the display on in normal video with start bank 0); every method
39
+ * waits for it and `ready()` resolves (or rejects) when it is done. A shadow copy of the 4 x 40
40
+ * byte RAM is kept so single-pixel updates need only one byte on the bus. One RAM byte is
41
+ * 8 vertical pixels of one column; bit 0 is the top row of the bank.
42
+ *
43
+ * @param {import('../../connection/connection').Connection} connection - Configured I2C
44
+ * connection pointing at the device (100 kHz maximum).
45
+ * @param {number} [rows=16] - Number of display rows: 8, 16, 24 or 32.
46
+ */
47
+ class PCF8578Minimal {
48
+ constructor(connection, rows = 16) {
49
+ if (!(rows in _MUX_CODE)) {
50
+ throw new RangeError('rows must be 8, 16, 24 or 32');
51
+ }
52
+ this._conn = connection;
53
+ this._rows = rows;
54
+ this._mixed = true;
55
+ this._status = STATUS_NORMAL;
56
+ this._startBank = 0;
57
+ this._shadow = [];
58
+ for (let b = 0; b < RAM_BANKS; b++) this._shadow.push(new Uint8Array(RAM_COLUMNS));
59
+ this._ready = this._init();
60
+ this._ready.catch(() => {}); // surfaced through ready() and the first awaited call
61
+ }
62
+
63
+ /**
64
+ * Resolves when the initialisation sequence is finished (rejects if it failed).
65
+ * @returns {Promise<void>}
66
+ */
67
+ ready() { return this._ready; }
68
+
69
+ _modeByte(status, cont) {
70
+ return (cont ? _CONT : 0) | _CMD_SET_MODE | ((this._mixed ? 1 : 0) << 4)
71
+ | (status << 2) | _MUX_CODE[this._rows];
72
+ }
73
+
74
+ _ramAccessByte(access, bank, cont = true) {
75
+ return (cont ? _CONT : 0) | _CMD_RAM_ACCESS | (access << 2) | bank;
76
+ }
77
+
78
+ _zeroShadow() {
79
+ for (const bank of this._shadow) bank.fill(0);
80
+ }
81
+
82
+ async _init() {
83
+ await new Promise((resolve) => setTimeout(resolve, 2)); // >= 1 ms internal power-on reset
84
+ this._mixed = true;
85
+ this._startBank = 0;
86
+ const out = [
87
+ _CONT | _CMD_DEVICE_SELECT, // device-select 0
88
+ this._modeByte(STATUS_BLANK, true), // mixed, blank
89
+ _CONT | _CMD_SET_START_BANK, // start bank 0
90
+ this._ramAccessByte(ACCESS_FULL_GRAPHIC, 0), // full-graphic, bank 0
91
+ 0x00, // load-X 0 (C = 0)
92
+ ...new Array(RAM_COLUMNS * RAM_BANKS).fill(0),
93
+ ];
94
+ await this._conn.write(Buffer.from(out));
95
+ this._zeroShadow();
96
+ this._status = STATUS_NORMAL;
97
+ await this._conn.write(Buffer.from([this._modeByte(STATUS_NORMAL, false)]));
98
+ }
99
+
100
+ _requireMixed() {
101
+ if (!this._mixed) {
102
+ throw new Error('display RAM is only accessible in mixed mode');
103
+ }
104
+ }
105
+
106
+ // Write data at RAM column x / bank and mirror it in the shadow.
107
+ async _ramWrite(x, bank, data, access = ACCESS_CHARACTER) {
108
+ this._requireMixed();
109
+ const out = [
110
+ _CONT | _CMD_DEVICE_SELECT,
111
+ this._ramAccessByte(access, bank),
112
+ x & 0x3F,
113
+ ...data,
114
+ ];
115
+ await this._conn.write(Buffer.from(out));
116
+ const perCol = _PER_COL[access];
117
+ for (let i = 0; i < data.length; i++) {
118
+ this._shadow[bank + (i % perCol)][x + Math.floor(i / perCol)] = data[i];
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Zero the whole display RAM (4 banks x 40 columns) and the shadow.
124
+ * @returns {Promise<void>}
125
+ */
126
+ async clear() {
127
+ await this._ready;
128
+ this._requireMixed();
129
+ const out = [
130
+ _CONT | _CMD_DEVICE_SELECT,
131
+ this._ramAccessByte(ACCESS_FULL_GRAPHIC, 0),
132
+ 0x00,
133
+ ...new Array(RAM_COLUMNS * RAM_BANKS).fill(0),
134
+ ];
135
+ await this._conn.write(Buffer.from(out));
136
+ this._zeroShadow();
137
+ }
138
+
139
+ /**
140
+ * Set or clear one pixel. Out-of-range coordinates are ignored. Writes exactly one RAM byte.
141
+ *
142
+ * @param {number} x - Display column, 0 .. (40 - rows - 1).
143
+ * @param {number} y - Display row, 0 .. rows - 1.
144
+ * @param {boolean} [on=true] - true lights the pixel, false clears it.
145
+ * @returns {Promise<void>}
146
+ */
147
+ async setPixel(x, y, on = true) {
148
+ await this._ready;
149
+ this._requireMixed();
150
+ if (x < 0 || x >= RAM_COLUMNS - this._rows || y < 0 || y >= this._rows) return;
151
+ const ramX = this._rows + x;
152
+ const bank = y >> 3;
153
+ const bit = 1 << (y & 7);
154
+ const value = on ? (this._shadow[bank][ramX] | bit) : (this._shadow[bank][ramX] & ~bit & 0xFF);
155
+ await this._ramWrite(ramX, bank, [value]);
156
+ }
157
+
158
+ /**
159
+ * Write consecutive column bytes in character access mode.
160
+ *
161
+ * @param {number} x - First display column, 0 .. (40 - rows - 1).
162
+ * @param {number} bank - Display bank (8-row band), 0 .. rows/8 - 1.
163
+ * @param {number[]|Buffer} data - Column bytes; bit 0 is the top row of the bank. The write
164
+ * is truncated at the last display column.
165
+ * @returns {Promise<void>}
166
+ */
167
+ async writeColumns(x, bank, data) {
168
+ await this._ready;
169
+ if (bank < 0 || bank >= (this._rows >> 3)) {
170
+ throw new RangeError('bank out of range for the configured rows');
171
+ }
172
+ const columns = RAM_COLUMNS - this._rows;
173
+ if (x < 0 || x >= columns) return;
174
+ const bytes = Array.from(data).slice(0, columns - x);
175
+ if (bytes.length === 0) return;
176
+ await this._ramWrite(this._rows + x, bank, bytes);
177
+ }
178
+ }
179
+
180
+ /**
181
+ * PCF8578 full interface — extends PCF8578Minimal.
182
+ *
183
+ * Adds pixel read-back from the shadow, fill, line/rectangle/bitmap drawing, display status
184
+ * (blank / normal / all-on / inverse), multiplex rate and row mode, hardware vertical scrolling
185
+ * via the start bank, raw RAM write/read in all three access modes, and the device-select
186
+ * command for PCF8579 cascades.
187
+ *
188
+ * @param {import('../../connection/connection').Connection} connection - Configured I2C
189
+ * connection pointing at the device (100 kHz maximum).
190
+ * @param {number} [rows=16] - Number of display rows: 8, 16, 24 or 32.
191
+ */
192
+ class PCF8578Full extends PCF8578Minimal {
193
+ _plot(x, y, on, dirty) {
194
+ if (x < 0 || x >= RAM_COLUMNS - this._rows || y < 0 || y >= this._rows) return;
195
+ const ramX = this._rows + x;
196
+ const bank = y >> 3;
197
+ const bit = 1 << (y & 7);
198
+ this._shadow[bank][ramX] = on ? (this._shadow[bank][ramX] | bit) : (this._shadow[bank][ramX] & ~bit & 0xFF);
199
+ dirty.add(bank * RAM_COLUMNS + ramX);
200
+ }
201
+
202
+ // One transaction per run of consecutive changed columns in a bank.
203
+ async _flush(dirty) {
204
+ const items = Array.from(dirty).sort((a, b) => a - b);
205
+ let i = 0;
206
+ while (i < items.length) {
207
+ const bank = Math.floor(items[i] / RAM_COLUMNS);
208
+ const start = items[i] % RAM_COLUMNS;
209
+ let end = start;
210
+ while (i + 1 < items.length && items[i + 1] === items[i] + 1
211
+ && Math.floor(items[i + 1] / RAM_COLUMNS) === bank) {
212
+ i++;
213
+ end++;
214
+ }
215
+ await this._ramWrite(start, bank, Array.from(this._shadow[bank].slice(start, end + 1)));
216
+ i++;
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Return the state of one pixel from the shadow (no bus traffic).
222
+ *
223
+ * @param {number} x - Display column.
224
+ * @param {number} y - Display row.
225
+ * @returns {Promise<boolean>} true if the pixel is set; false if clear or out of range.
226
+ */
227
+ async getPixel(x, y) {
228
+ await this._ready;
229
+ if (x < 0 || x >= RAM_COLUMNS - this._rows || y < 0 || y >= this._rows) return false;
230
+ return (this._shadow[y >> 3][this._rows + x] & (1 << (y & 7))) !== 0;
231
+ }
232
+
233
+ /**
234
+ * Set every displayed pixel on or off.
235
+ * @param {boolean} [on=true] - true lights all pixels, false clears them.
236
+ * @returns {Promise<void>}
237
+ */
238
+ async fill(on = true) {
239
+ await this._ready;
240
+ const columns = RAM_COLUMNS - this._rows;
241
+ for (let bank = 0; bank < (this._rows >> 3); bank++) {
242
+ await this._ramWrite(this._rows, bank, new Array(columns).fill(on ? 0xFF : 0x00));
243
+ }
244
+ }
245
+
246
+ /**
247
+ * Draw a Bresenham line, written as one batch of changed bytes.
248
+ *
249
+ * @param {number} x0 - Start column.
250
+ * @param {number} y0 - Start row.
251
+ * @param {number} x1 - End column.
252
+ * @param {number} y1 - End row.
253
+ * @param {boolean} [on=true] - true draws, false erases.
254
+ * @returns {Promise<void>}
255
+ */
256
+ async drawLine(x0, y0, x1, y1, on = true) {
257
+ await this._ready;
258
+ this._requireMixed();
259
+ const dirty = new Set();
260
+ const dx = Math.abs(x1 - x0);
261
+ const dy = -Math.abs(y1 - y0);
262
+ const sx = x0 < x1 ? 1 : -1;
263
+ const sy = y0 < y1 ? 1 : -1;
264
+ let err = dx + dy;
265
+ for (;;) {
266
+ this._plot(x0, y0, on, dirty);
267
+ if (x0 === x1 && y0 === y1) break;
268
+ const e2 = 2 * err;
269
+ if (e2 >= dy) { err += dy; x0 += sx; }
270
+ if (e2 <= dx) { err += dx; y0 += sy; }
271
+ }
272
+ await this._flush(dirty);
273
+ }
274
+
275
+ /**
276
+ * Draw a rectangle outline or a filled rectangle.
277
+ *
278
+ * @param {number} x - Left column.
279
+ * @param {number} y - Top row.
280
+ * @param {number} w - Width in pixels.
281
+ * @param {number} h - Height in pixels.
282
+ * @param {boolean} [on=true] - true draws, false erases.
283
+ * @param {boolean} [fill=false] - true fills the interior.
284
+ * @returns {Promise<void>}
285
+ */
286
+ async drawRect(x, y, w, h, on = true, fill = false) {
287
+ await this._ready;
288
+ this._requireMixed();
289
+ if (w <= 0 || h <= 0) return;
290
+ const dirty = new Set();
291
+ for (let yy = y; yy < y + h; yy++) {
292
+ for (let xx = x; xx < x + w; xx++) {
293
+ if (fill || yy === y || yy === y + h - 1 || xx === x || xx === x + w - 1) {
294
+ this._plot(xx, yy, on, dirty);
295
+ }
296
+ }
297
+ }
298
+ await this._flush(dirty);
299
+ }
300
+
301
+ /**
302
+ * Blit a column-major bitmap aligned to a bank boundary.
303
+ *
304
+ * @param {number} x - Left display column.
305
+ * @param {number} y - Top display row; must be a multiple of 8.
306
+ * @param {Array<number[]|Buffer>|Buffer} bands - One array per 8-pixel-high band from the
307
+ * top, each holding one byte per column (bit 0 = top row). A single Buffer is treated as one
308
+ * band. Content outside the display is clipped.
309
+ * @returns {Promise<void>}
310
+ */
311
+ async showBitmap(x, y, bands) {
312
+ if (y % 8 !== 0 || y < 0) {
313
+ throw new RangeError('y must be a non-negative multiple of 8');
314
+ }
315
+ const list = Buffer.isBuffer(bands) ? [bands] : bands;
316
+ for (let i = 0; i < list.length; i++) {
317
+ const bank = (y >> 3) + i;
318
+ if (bank >= (this._rows >> 3)) break;
319
+ await this.writeColumns(x, bank, list[i]);
320
+ }
321
+ }
322
+
323
+ /**
324
+ * Set the display status; RAM contents are preserved.
325
+ * @param {number} status - PCF8578Full.DISPLAY_BLANK / _NORMAL / _ALL_ON / _INVERSE.
326
+ * @returns {Promise<void>}
327
+ */
328
+ async setDisplay(status) {
329
+ await this._ready;
330
+ if (status < 0 || status > 3) throw new RangeError('status must be in 0..3');
331
+ this._status = status;
332
+ await this._conn.write(Buffer.from([this._modeByte(status, false)]));
333
+ }
334
+
335
+ /**
336
+ * Change the multiplex rate and the row/mixed mode. In row mode (`mixed = false`) the RAM
337
+ * cannot be accessed; pixel and RAM calls throw until mixed mode is restored.
338
+ *
339
+ * @param {number} rows - 8, 16, 24 or 32.
340
+ * @param {boolean} [mixed=true] - true for mixed mode, false for row mode.
341
+ * @returns {Promise<void>}
342
+ */
343
+ async setMode(rows, mixed = true) {
344
+ await this._ready;
345
+ if (!(rows in _MUX_CODE)) throw new RangeError('rows must be 8, 16, 24 or 32');
346
+ this._rows = rows;
347
+ this._mixed = !!mixed;
348
+ await this._conn.write(Buffer.from([this._modeByte(this._status, false)]));
349
+ }
350
+
351
+ /**
352
+ * Select the RAM bank shown at the top of the display (vertical scroll).
353
+ * @param {number} bank - 0 .. 3.
354
+ * @returns {Promise<void>}
355
+ */
356
+ async setStartBank(bank) {
357
+ await this._ready;
358
+ if (bank < 0 || bank > 3) throw new RangeError('bank must be in 0..3');
359
+ this._startBank = bank;
360
+ await this._conn.write(Buffer.from([_CMD_SET_START_BANK | bank]));
361
+ }
362
+
363
+ /**
364
+ * Add `banks` (may be negative) to the start bank, modulo the banks in use.
365
+ * @param {number} banks - Number of 8-row steps.
366
+ * @returns {Promise<void>}
367
+ */
368
+ async scroll(banks) {
369
+ await this._ready;
370
+ const n = this._rows >> 3;
371
+ await this.setStartBank((((this._startBank + banks) % n) + n) % n);
372
+ }
373
+
374
+ _checkRamArgs(x, bank, count, access) {
375
+ if (access < 0 || access > 2) throw new RangeError('access must be in 0..2');
376
+ if (x < 0 || x > 39 || bank < 0 || bank > 3) throw new RangeError('x must be in 0..39 and bank in 0..3');
377
+ if (access === 1 && bank % 2) throw new RangeError('half-graphic access must start at an even bank');
378
+ if (access === 2 && bank !== 0) throw new RangeError('full-graphic access must start at bank 0');
379
+ if (x + Math.ceil(count / _PER_COL[access]) > 40) throw new RangeError('write would pass column 39');
380
+ }
381
+
382
+ /**
383
+ * Raw RAM write in character, half-graphic or full-graphic order.
384
+ *
385
+ * @param {number} x - RAM column, 0 .. 39.
386
+ * @param {number} bank - RAM bank, 0 .. 3 (even for half-graphic, 0 for full-graphic).
387
+ * @param {number[]|Buffer} data - Bytes to write.
388
+ * @param {number} [mode=0] - ACCESS_CHARACTER, ACCESS_HALF_GRAPHIC or ACCESS_FULL_GRAPHIC.
389
+ * @returns {Promise<void>}
390
+ */
391
+ async writeRam(x, bank, data, mode = ACCESS_CHARACTER) {
392
+ await this._ready;
393
+ const bytes = Array.from(data);
394
+ this._checkRamArgs(x, bank, bytes.length, mode);
395
+ if (bytes.length === 0) return;
396
+ await this._ramWrite(x, bank, bytes, mode);
397
+ }
398
+
399
+ /**
400
+ * Raw RAM read: address-set commands, repeated start, then `n` bytes.
401
+ *
402
+ * @param {number} x - RAM column, 0 .. 39.
403
+ * @param {number} bank - RAM bank, 0 .. 3.
404
+ * @param {number} n - Number of bytes to read.
405
+ * @param {number} [mode=0] - Access mode as for writeRam.
406
+ * @returns {Promise<Buffer>} The bytes read.
407
+ */
408
+ async readRam(x, bank, n, mode = ACCESS_CHARACTER) {
409
+ await this._ready;
410
+ this._requireMixed();
411
+ this._checkRamArgs(x, bank, n, mode);
412
+ const cmds = Buffer.from([
413
+ _CONT | _CMD_DEVICE_SELECT,
414
+ this._ramAccessByte(mode, bank),
415
+ x & 0x3F,
416
+ ]);
417
+ return this._conn.writeRead(cmds, n);
418
+ }
419
+
420
+ /**
421
+ * Load the subaddress counter (for PCF8579 cascades). Subaddress 0 addresses the PCF8578
422
+ * itself; pixel and RAM calls restore subaddress 0 before their own accesses.
423
+ * @param {number} subaddress - 0 .. 15.
424
+ * @returns {Promise<void>}
425
+ */
426
+ async deviceSelect(subaddress) {
427
+ await this._ready;
428
+ if (subaddress < 0 || subaddress > 15) throw new RangeError('subaddress must be in 0..15');
429
+ await this._conn.write(Buffer.from([_CMD_DEVICE_SELECT | subaddress]));
430
+ }
431
+
432
+ /**
433
+ * Re-run initialisation at the current row count (clears RAM, mixed mode, normal video).
434
+ * @returns {Promise<void>}
435
+ */
436
+ async reset() {
437
+ await this._ready;
438
+ this._ready = this._init();
439
+ this._ready.catch(() => {});
440
+ await this._ready;
441
+ }
442
+ }
443
+
444
+ PCF8578Full.DISPLAY_BLANK = STATUS_BLANK;
445
+ PCF8578Full.DISPLAY_NORMAL = STATUS_NORMAL;
446
+ PCF8578Full.DISPLAY_ALL_ON = STATUS_ALL_ON;
447
+ PCF8578Full.DISPLAY_INVERSE = STATUS_INVERSE;
448
+ PCF8578Full.ACCESS_CHARACTER = ACCESS_CHARACTER;
449
+ PCF8578Full.ACCESS_HALF_GRAPHIC = ACCESS_HALF_GRAPHIC;
450
+ PCF8578Full.ACCESS_FULL_GRAPHIC = ACCESS_FULL_GRAPHIC;
451
+
452
+ module.exports = { PCF8578Minimal, PCF8578Full };