periph 1.4.0 → 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 (112) hide show
  1. package/README.md +27 -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/mcp3422/complete.js +48 -0
  24. package/examples/adc_dac/mcp3422/demo.js +49 -0
  25. package/examples/adc_dac/mcp3422/minimal.js +25 -0
  26. package/examples/display/sh1106/complete.js +101 -0
  27. package/examples/display/sh1106/demo.js +68 -0
  28. package/examples/display/sh1106/minimal.js +25 -0
  29. package/examples/display/ssd1305/complete.js +129 -0
  30. package/examples/display/ssd1305/demo.js +71 -0
  31. package/examples/display/ssd1305/minimal.js +25 -0
  32. package/examples/display/ssd1306/complete.js +115 -0
  33. package/examples/display/ssd1306/demo.js +70 -0
  34. package/examples/display/ssd1306/minimal.js +25 -0
  35. package/examples/environmental/am2315/complete.js +34 -0
  36. package/examples/environmental/am2315/demo.js +46 -0
  37. package/examples/environmental/am2315/minimal.js +21 -0
  38. package/examples/humidity/htu21df/complete.js +49 -0
  39. package/examples/humidity/htu21df/demo.js +50 -0
  40. package/examples/humidity/htu21df/minimal.js +22 -0
  41. package/examples/humidity/sht31/complete.js +53 -0
  42. package/examples/humidity/sht31/demo.js +53 -0
  43. package/examples/humidity/sht31/minimal.js +21 -0
  44. package/examples/humidity/si7021/complete.js +55 -0
  45. package/examples/humidity/si7021/demo.js +58 -0
  46. package/examples/humidity/si7021/minimal.js +21 -0
  47. package/examples/imu/bno055/complete.js +85 -0
  48. package/examples/imu/bno055/demo.js +70 -0
  49. package/examples/imu/bno055/minimal.js +20 -0
  50. package/examples/io_expander/pca6408a/complete.js +63 -0
  51. package/examples/io_expander/pca6408a/demo.js +58 -0
  52. package/examples/io_expander/pca6408a/minimal.js +20 -0
  53. package/examples/io_expander/pca6416a/complete.js +68 -0
  54. package/examples/io_expander/pca6416a/demo.js +60 -0
  55. package/examples/io_expander/pca6416a/minimal.js +20 -0
  56. package/examples/io_expander/pca9536/complete.js +63 -0
  57. package/examples/io_expander/pca9536/demo.js +57 -0
  58. package/examples/io_expander/pca9536/minimal.js +20 -0
  59. package/examples/io_expander/pca9539/complete.js +68 -0
  60. package/examples/io_expander/pca9539/demo.js +60 -0
  61. package/examples/io_expander/pca9539/minimal.js +20 -0
  62. package/examples/light/max44009/complete.js +56 -0
  63. package/examples/light/max44009/demo.js +49 -0
  64. package/examples/light/max44009/minimal.js +22 -0
  65. package/examples/memory/at24c02n/complete.js +39 -0
  66. package/examples/memory/at24c02n/demo.js +39 -0
  67. package/examples/memory/at24c02n/minimal.js +15 -0
  68. package/examples/memory/at24c64/complete.js +39 -0
  69. package/examples/memory/at24c64/demo.js +39 -0
  70. package/examples/memory/at24c64/minimal.js +15 -0
  71. package/examples/memory/cat24c512/complete.js +39 -0
  72. package/examples/memory/cat24c512/demo.js +39 -0
  73. package/examples/memory/cat24c512/minimal.js +15 -0
  74. package/examples/other/pca9541/complete.js +52 -0
  75. package/examples/other/pca9541/demo.js +61 -0
  76. package/examples/other/pca9541/minimal.js +23 -0
  77. package/examples/temperature/tmp102/complete.js +74 -0
  78. package/examples/temperature/tmp102/demo.js +71 -0
  79. package/examples/temperature/tmp102/minimal.js +20 -0
  80. package/examples/tof/vl6180x/complete.js +62 -0
  81. package/examples/tof/vl6180x/demo.js +77 -0
  82. package/examples/tof/vl6180x/minimal.js +23 -0
  83. package/package.json +1 -1
  84. package/src/chips/adc_dac/ad5251.js +345 -0
  85. package/src/chips/adc_dac/ad5252.js +345 -0
  86. package/src/chips/adc_dac/ads7828.js +225 -0
  87. package/src/chips/adc_dac/cat5171.js +213 -0
  88. package/src/chips/adc_dac/cs43l22.js +645 -0
  89. package/src/chips/adc_dac/ds1881.js +357 -0
  90. package/src/chips/adc_dac/ds3502.js +241 -0
  91. package/src/chips/adc_dac/mcp3422.js +312 -0
  92. package/src/chips/display/mono_oled.js +182 -0
  93. package/src/chips/display/sh1106.js +496 -0
  94. package/src/chips/display/ssd1305.js +655 -0
  95. package/src/chips/display/ssd1306.js +458 -0
  96. package/src/chips/environmental/am2315.js +237 -0
  97. package/src/chips/humidity/htu21df.js +222 -0
  98. package/src/chips/humidity/sht31.js +284 -0
  99. package/src/chips/humidity/si7021.js +282 -0
  100. package/src/chips/imu/bno055.js +496 -0
  101. package/src/chips/io_expander/pca6408a.js +407 -0
  102. package/src/chips/io_expander/pca6416a.js +455 -0
  103. package/src/chips/io_expander/pca9536.js +398 -0
  104. package/src/chips/io_expander/pca9539.js +457 -0
  105. package/src/chips/light/max44009.js +336 -0
  106. package/src/chips/memory/at24c02n.js +220 -0
  107. package/src/chips/memory/at24c64.js +220 -0
  108. package/src/chips/memory/cat24c512.js +221 -0
  109. package/src/chips/other/pca9541.js +315 -0
  110. package/src/chips/temperature/tmp102.js +361 -0
  111. package/src/chips/tof/vl6180x.js +580 -0
  112. package/src/discovery/registry.js +65 -13
@@ -0,0 +1,655 @@
1
+ 'use strict';
2
+
3
+ const WIDTH = 132;
4
+ const HEIGHT = 64;
5
+ const I2C_CHUNK = 32; // largest write in bytes including the control byte
6
+ const SPI_CHUNK = 132;
7
+
8
+ const _CMD_COL_LO = 0x00; // | A[3:0]
9
+ const _CMD_COL_HI = 0x10; // | A[7:4]
10
+ const _CMD_ADDR_MODE = 0x20;
11
+ const _CMD_COL_RANGE = 0x21;
12
+ const _CMD_PAGE_RANGE = 0x22;
13
+ const _CMD_START_LINE = 0x40; // | A[5:0]
14
+ const _CMD_CONTRAST = 0x81;
15
+ const _CMD_BRIGHTNESS = 0x82;
16
+ const _CMD_LUT = 0x91;
17
+ const _CMD_BANK_1_16 = 0x92;
18
+ const _CMD_BANK_17_32 = 0x93;
19
+ const _CMD_SEG_REMAP = 0xA0; // | ADC
20
+ const _CMD_SCROLL_AREA = 0xA3;
21
+ const _CMD_ALL_ON = 0xA4; // | 1
22
+ const _CMD_INVERT = 0xA6; // | 1
23
+ const _CMD_MULTIPLEX = 0xA8;
24
+ const _CMD_DIM_MODE = 0xAB;
25
+ const _CMD_DIM_ON = 0xAC;
26
+ const _CMD_MASTER = 0xAD;
27
+ const _CMD_DISPLAY = 0xAE; // | 1
28
+ const _CMD_PAGE = 0xB0; // | A[2:0]
29
+ const _CMD_SCAN = 0xC0; // | (scan << 3)
30
+ const _CMD_OFFSET = 0xD3;
31
+ const _CMD_CLOCK = 0xD5;
32
+ const _CMD_AREA_COLOR = 0xD8;
33
+ const _CMD_PRECHARGE = 0xD9;
34
+ const _CMD_COM = 0xDA;
35
+ const _CMD_VCOMH = 0xDB;
36
+ const _INTERVALS = [0, 1, 2, 3, 4, 5, 6]; // table index -> 6, 32, 64, 128, 3, 4, 2 frames
37
+
38
+ const sleepMs = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
39
+
40
+ /** I²C transport: every transaction starts with a control byte (0x00 = commands, 0x40 = RAM data). */
41
+ class I2CBus {
42
+ constructor(connection) {
43
+ this._conn = connection;
44
+ this.canRead = true;
45
+ }
46
+
47
+ writeCommands(bytes) { return this._conn.write(Buffer.concat([Buffer.from([0x00]), Buffer.from(bytes)])); }
48
+
49
+ async writeData(data) {
50
+ const step = I2C_CHUNK - 1;
51
+ for (let i = 0; i < data.length; i += step) {
52
+ await this._conn.write(Buffer.concat([Buffer.from([0x40]), data.subarray(i, i + step)]));
53
+ }
54
+ }
55
+
56
+ async readStatus() {
57
+ await this._conn.write(Buffer.from([0x00]));
58
+ return Buffer.from(await this._conn.read(1))[0];
59
+ }
60
+
61
+ async readRam(n) {
62
+ await this._conn.write(Buffer.from([0x40]));
63
+ return Buffer.from(await this._conn.read(n + 1)).subarray(1);
64
+ }
65
+
66
+ async hardwareReset() {}
67
+ }
68
+
69
+ /** 4-wire SPI transport on a display connection: D/C low for every command byte, high for RAM data. */
70
+ class SpiBus {
71
+ constructor(connection) {
72
+ this._conn = connection;
73
+ this.canRead = false;
74
+ }
75
+
76
+ async writeCommands(bytes) {
77
+ for (const b of bytes) await this._conn.sendCommand(b);
78
+ }
79
+
80
+ async writeData(data) {
81
+ for (let i = 0; i < data.length; i += SPI_CHUNK) {
82
+ await this._conn.sendData(data.subarray(i, i + SPI_CHUNK));
83
+ }
84
+ }
85
+
86
+ hardwareReset() { return this._conn.reset(); }
87
+ }
88
+
89
+ /**
90
+ * SSD1305 132 x 64 OLED controller-driver — minimal interface (I²C).
91
+ *
92
+ * Drives a monochrome SSD1305 OLED panel (2.23" 128 x 32 or 1.3" 128 x 64 modules) over I²C at
93
+ * 0x3C (SA0 = low) or 0x3D (SA0 = high). The chip has 132 RAM columns; 128-pixel-wide modules
94
+ * usually show RAM columns 4..131, so `columnOffset` defaults to 4.
95
+ *
96
+ * All drawing goes into a framebuffer of width * height / 8 bytes (page-major, bit 0 = top pixel of
97
+ * the page); `show()` sends it page by page (page addressing, the reset default). The chip has no
98
+ * charge pump: VCC (7..15 V) must be supplied by the board. The initialization follows the
99
+ * datasheet power-on flow: setup stream with the display off, RAM cleared, display on + 100 ms.
100
+ * The constructor starts the initialisation;
101
+ * every method that talks to the chip waits for it, and `ready()` resolves (or rejects) when done.
102
+ *
103
+ * @param {object} connection - I2CConnection at 0x3C or 0x3D.
104
+ * @param {number} [width=128] - Visible columns, 1..132.
105
+ * @param {number} [height=64] - Rows, a multiple of 8, 16..64 (up to 32 uses sequential COM wiring).
106
+ * @param {number} [columnOffset=4] - First RAM column shown (0 for a true 132-column panel).
107
+ * @param {object} [bus] - Internal: transport override used by the SPI classes.
108
+ */
109
+ class SSD1305Minimal {
110
+ constructor(connection, width = 128, height = 64, columnOffset = 4, bus = null) {
111
+ if (!(width >= 1 && width <= WIDTH)) throw new RangeError('width must be 1..132');
112
+ if (!(height >= 16 && height <= HEIGHT && height % 8 === 0)) throw new RangeError('height must be a multiple of 8, 16..64');
113
+ if (!(columnOffset >= 0 && columnOffset + width <= WIDTH)) throw new RangeError('columnOffset + width must be at most 132');
114
+ this._bus = bus || new I2CBus(connection);
115
+ this.width = width;
116
+ this.height = height;
117
+ this._offset = columnOffset;
118
+ this._mode = 'page';
119
+ this._pages = height / 8;
120
+ this._fb = Buffer.alloc(width * this._pages);
121
+ this._adc = 0;
122
+ this._scan = 0;
123
+ this._ready = this._initSequence();
124
+ this._ready.catch(() => {}); // surfaced through ready() and the first awaited call
125
+ }
126
+
127
+ /**
128
+ * Resolves when initialisation has finished; rejects if it failed.
129
+ * @returns {Promise<void>}
130
+ */
131
+ ready() { return this._ready; }
132
+
133
+ // ---- internals -------------------------------------------------------------------------
134
+
135
+ _command(...values) {
136
+ return this._bus.writeCommands(Buffer.from(values));
137
+ }
138
+
139
+ _addressBytes(page, column) {
140
+ return Buffer.from([_CMD_PAGE | (page & 7), _CMD_COL_LO | (column & 0x0F), _CMD_COL_HI | ((column >> 4) & 0x0F)]);
141
+ }
142
+
143
+ async _writePage(page, data) {
144
+ await this._bus.writeCommands(this._addressBytes(page, this._offset));
145
+ await this._bus.writeData(data);
146
+ }
147
+
148
+ /** Datasheet power-on flow: reset, setup stream, RAM clear, display on. */
149
+ async _initSequence() {
150
+ await this._bus.hardwareReset();
151
+ await sleepMs(1);
152
+ await this._command(_CMD_DISPLAY,
153
+ _CMD_CLOCK, 0x70,
154
+ _CMD_MULTIPLEX, this.height - 1,
155
+ _CMD_OFFSET, 0x00,
156
+ _CMD_START_LINE,
157
+ _CMD_SEG_REMAP | this._adc,
158
+ _CMD_SCAN | (this._scan << 3),
159
+ _CMD_COM, this.height > 32 ? 0x12 : 0x02,
160
+ _CMD_CONTRAST, 0x80,
161
+ _CMD_PRECHARGE, 0x22,
162
+ _CMD_VCOMH, 0x34,
163
+ _CMD_MASTER, 0x8E,
164
+ _CMD_ALL_ON,
165
+ _CMD_INVERT,
166
+ _CMD_ADDR_MODE, 0x02);
167
+ const blank = Buffer.alloc(WIDTH);
168
+ for (let page = 0; page < 8; page++) {
169
+ await this._bus.writeCommands(this._addressBytes(page, 0));
170
+ await this._bus.writeData(blank);
171
+ }
172
+ await this._command(_CMD_DISPLAY | 1);
173
+ await sleepMs(100);
174
+ }
175
+
176
+ _showPage(page) {
177
+ const start = page * this.width;
178
+ return this._writePage(page, this._fb.subarray(start, start + this.width));
179
+ }
180
+
181
+ async _showAll() {
182
+ if (this._mode === 'page') {
183
+ for (let page = 0; page < this._pages; page++) await this._showPage(page);
184
+ return;
185
+ }
186
+ await this._command(_CMD_COL_RANGE, this._offset, this._offset + this.width - 1,
187
+ _CMD_PAGE_RANGE, 0, this._pages - 1);
188
+ if (this._mode === 'horizontal') {
189
+ await this._bus.writeData(this._fb);
190
+ return;
191
+ }
192
+ const out = Buffer.alloc(this._fb.length);
193
+ let n = 0;
194
+ for (let x = 0; x < this.width; x++) {
195
+ for (let p = 0; p < this._pages; p++) out[n++] = this._fb[p * this.width + x];
196
+ }
197
+ await this._bus.writeData(out);
198
+ }
199
+
200
+ // ---- public API ------------------------------------------------------------------------
201
+
202
+ /** Zero the framebuffer. Call `show()` to update the panel. */
203
+ clear() {
204
+ this._fb.fill(0);
205
+ }
206
+
207
+ /**
208
+ * Set or clear one pixel in the framebuffer. Out-of-range coordinates are ignored.
209
+ * @param {number} x - Column, 0 .. width - 1.
210
+ * @param {number} y - Row, 0 .. height - 1.
211
+ * @param {boolean} [on=true] - True to set the pixel, false to clear it.
212
+ */
213
+ setPixel(x, y, on = true) {
214
+ if (x < 0 || y < 0 || x >= this.width || y >= this.height) return;
215
+ const i = (y >> 3) * this.width + x;
216
+ if (on) this._fb[i] |= 1 << (y & 7);
217
+ else this._fb[i] &= ~(1 << (y & 7)) & 0xFF;
218
+ }
219
+
220
+ /**
221
+ * Write the whole framebuffer to the display RAM. Page addressing (default): per page a page +
222
+ * column command stream, then width data bytes. Horizontal / vertical addressing: column and
223
+ * page range commands, then one data stream.
224
+ * @returns {Promise<void>}
225
+ */
226
+ async show() {
227
+ await this._ready;
228
+ await this._showAll();
229
+ }
230
+ }
231
+
232
+ /**
233
+ * SSD1305 OLED controller-driver — full interface (I²C).
234
+ *
235
+ * Extends SSD1305Minimal with drawing primitives, contrast, brightness, dim mode, sleep, invert,
236
+ * all-on test, flip, start line, display offset, multiplex ratio, clock / oscillator, pre-charge,
237
+ * COM configuration, VCOMH level, addressing mode, hardware scrolling, area-colour mode with
238
+ * LUT / bank colours, status and RAM reads (I²C only) and raw commands.
239
+ *
240
+ * @param {object} connection - I2CConnection at 0x3C or 0x3D.
241
+ * @param {number} [width=128] - Visible columns, 1..132.
242
+ * @param {number} [height=64] - Rows, a multiple of 8, 16..64.
243
+ * @param {number} [columnOffset=4] - First RAM column shown.
244
+ * @param {object} [bus] - Internal: transport override used by the SPI classes.
245
+ */
246
+ class SSD1305Full extends SSD1305Minimal {
247
+ // ---- framebuffer -----------------------------------------------------------------------
248
+
249
+ /**
250
+ * Return a framebuffer pixel; out-of-range coordinates read as false.
251
+ * @param {number} x - Column.
252
+ * @param {number} y - Row.
253
+ * @returns {boolean} True if the pixel is set.
254
+ */
255
+ getPixel(x, y) {
256
+ if (x < 0 || y < 0 || x >= this.width || y >= this.height) return false;
257
+ return (this._fb[(y >> 3) * this.width + x] & (1 << (y & 7))) !== 0;
258
+ }
259
+
260
+ /** Set (or clear) the whole framebuffer. @param {boolean} [on=true] */
261
+ fill(on = true) {
262
+ this._fb.fill(on ? 0xFF : 0);
263
+ }
264
+
265
+ /**
266
+ * Draw a Bresenham line into the framebuffer.
267
+ * @param {number} x0 - Start column.
268
+ * @param {number} y0 - Start row.
269
+ * @param {number} x1 - End column.
270
+ * @param {number} y1 - End row.
271
+ * @param {boolean} [on=true] - True to set pixels, false to clear them.
272
+ */
273
+ drawLine(x0, y0, x1, y1, on = true) {
274
+ const dx = Math.abs(x1 - x0);
275
+ const dy = -Math.abs(y1 - y0);
276
+ const sx = x0 < x1 ? 1 : -1;
277
+ const sy = y0 < y1 ? 1 : -1;
278
+ let err = dx + dy;
279
+ for (;;) {
280
+ this.setPixel(x0, y0, on);
281
+ if (x0 === x1 && y0 === y1) break;
282
+ const e2 = 2 * err;
283
+ if (e2 >= dy) { err += dy; x0 += sx; }
284
+ if (e2 <= dx) { err += dx; y0 += sy; }
285
+ }
286
+ }
287
+
288
+ /**
289
+ * Draw a rectangle outline or a filled rectangle into the framebuffer.
290
+ * @param {number} x - Left column.
291
+ * @param {number} y - Top row.
292
+ * @param {number} w - Width in pixels.
293
+ * @param {number} h - Height in pixels.
294
+ * @param {boolean} [on=true] - True to set pixels, false to clear them.
295
+ * @param {boolean} [fill=false] - True for a filled rectangle.
296
+ */
297
+ drawRect(x, y, w, h, on = true, fill = false) {
298
+ if (w <= 0 || h <= 0) return;
299
+ if (fill) {
300
+ for (let yy = y; yy < y + h; yy++) for (let xx = x; xx < x + w; xx++) this.setPixel(xx, yy, on);
301
+ return;
302
+ }
303
+ this.drawLine(x, y, x + w - 1, y, on);
304
+ this.drawLine(x, y + h - 1, x + w - 1, y + h - 1, on);
305
+ this.drawLine(x, y, x, y + h - 1, on);
306
+ this.drawLine(x + w - 1, y, x + w - 1, y + h - 1, on);
307
+ }
308
+
309
+ /**
310
+ * Copy a column-major bitmap (8 pixels per byte, bit 0 = top) into the framebuffer. `columns`
311
+ * holds `heightPages` groups of columns, page by page: byte p * n + c is column c of page row p
312
+ * (n = length / heightPages). The bitmap is clipped to the framebuffer; y must be a multiple of 8.
313
+ * @param {number} x - Left column of the bitmap.
314
+ * @param {number} y - Top row, a multiple of 8.
315
+ * @param {Buffer|number[]} columns - Bitmap bytes.
316
+ * @param {number} [heightPages=1] - Number of 8-pixel-high rows of the bitmap.
317
+ */
318
+ blit(x, y, columns, heightPages = 1) {
319
+ if (y % 8 !== 0) throw new RangeError('y must be a multiple of 8');
320
+ const n = Math.floor(columns.length / heightPages);
321
+ for (let p = 0; p < heightPages; p++) {
322
+ const page = y / 8 + p;
323
+ if (page < 0 || page >= this._pages) continue;
324
+ for (let c = 0; c < n; c++) {
325
+ const xx = x + c;
326
+ if (xx >= 0 && xx < this.width) this._fb[page * this.width + xx] = columns[p * n + c];
327
+ }
328
+ }
329
+ }
330
+
331
+ /**
332
+ * Write one framebuffer page to the display RAM (cheaper partial update).
333
+ * @param {number} page - Page 0 .. height / 8 - 1.
334
+ * @returns {Promise<void>}
335
+ */
336
+ async showPage(page) {
337
+ await this._ready;
338
+ if (!(page >= 0 && page < this._pages)) return;
339
+ if (this._mode === 'page') {
340
+ await this._showPage(page);
341
+ return;
342
+ }
343
+ const start = page * this.width;
344
+ await this._command(_CMD_COL_RANGE, this._offset, this._offset + this.width - 1, _CMD_PAGE_RANGE, page, page);
345
+ await this._bus.writeData(this._fb.subarray(start, start + this.width));
346
+ }
347
+
348
+ // ---- display control -------------------------------------------------------------------
349
+
350
+ /** Set the segment output current (contrast). @param {number} value - 0..255 (reset 0x80). @returns {Promise<void>} */
351
+ async setContrast(value) {
352
+ await this._ready;
353
+ await this._command(_CMD_CONTRAST, value & 0xFF);
354
+ }
355
+
356
+ /** Switch the display on or off (RAM and settings are kept). @param {boolean} on @returns {Promise<void>} */
357
+ async setDisplay(on) {
358
+ await this._ready;
359
+ await this._command(_CMD_DISPLAY | (on ? 1 : 0));
360
+ }
361
+
362
+ /** Display off (0xAE); RAM and settings are kept and stay writable. Switch VCC off afterwards. @returns {Promise<void>} */
363
+ sleep() { return this.setDisplay(false); }
364
+
365
+ /** Display on, then wait 100 ms (tAF) for SEG / COM to turn on. @returns {Promise<void>} */
366
+ async wake() {
367
+ await this.setDisplay(true);
368
+ await sleepMs(100);
369
+ }
370
+
371
+ /**
372
+ * Switch between dim mode (0xAC) and normal display on (0xAF); configure the levels with `setDimMode` first.
373
+ * @param {boolean} on
374
+ * @returns {Promise<void>}
375
+ */
376
+ async setDim(on) {
377
+ await this._ready;
378
+ await this._command(on ? _CMD_DIM_ON : _CMD_DISPLAY | 1);
379
+ }
380
+
381
+ /**
382
+ * Configure the BANK0 contrast and colour-bank brightness used in dim mode.
383
+ * @param {number} contrast - 0..255.
384
+ * @param {number} brightness - 0..255.
385
+ * @returns {Promise<void>}
386
+ */
387
+ async setDimMode(contrast, brightness) {
388
+ await this._ready;
389
+ await this._command(_CMD_DIM_MODE, 0x00, contrast & 0xFF, brightness & 0xFF);
390
+ }
391
+
392
+ /** Invert the displayed image without changing RAM. @param {boolean} on @returns {Promise<void>} */
393
+ async setInvert(on) {
394
+ await this._ready;
395
+ await this._command(_CMD_INVERT | (on ? 1 : 0));
396
+ }
397
+
398
+ /** Force all pixels on (test mode, RAM unchanged). @param {boolean} on @returns {Promise<void>} */
399
+ async setAllOn(on) {
400
+ await this._ready;
401
+ await this._command(_CMD_ALL_ON | (on ? 1 : 0));
402
+ }
403
+
404
+ /**
405
+ * Mirror columns (segment re-map) and/or rows (COM scan direction); the framebuffer is unchanged.
406
+ * @param {boolean} horizontal - True to reverse the segment mapping (ADC = 1).
407
+ * @param {boolean} vertical - True to reverse the COM scan direction (0xC8).
408
+ * @returns {Promise<void>}
409
+ */
410
+ async setFlip(horizontal, vertical) {
411
+ await this._ready;
412
+ this._adc = horizontal ? 1 : 0;
413
+ this._scan = vertical ? 1 : 0;
414
+ await this._command(_CMD_SEG_REMAP | this._adc, _CMD_SCAN | (this._scan << 3));
415
+ }
416
+
417
+ /** Set the RAM line shown at COM0 (hardware vertical scroll). @param {number} line - 0..63. @returns {Promise<void>} */
418
+ async setStartLine(line) {
419
+ await this._ready;
420
+ await this._command(_CMD_START_LINE | (line & 0x3F));
421
+ }
422
+
423
+ /** Shift the COM mapping. @param {number} offset - 0..63. @returns {Promise<void>} */
424
+ async setDisplayOffset(offset) {
425
+ await this._ready;
426
+ await this._command(_CMD_OFFSET, offset & 0x3F);
427
+ }
428
+
429
+ /** Set the multiplex ratio. @param {number} ratio - 16..64. @returns {Promise<void>} */
430
+ async setMultiplex(ratio) {
431
+ await this._ready;
432
+ await this._command(_CMD_MULTIPLEX, (ratio - 1) & 0x3F);
433
+ }
434
+
435
+ /**
436
+ * Set the clock divide ratio and oscillator frequency.
437
+ * @param {number} divide - Divide ratio, 1..16.
438
+ * @param {number} osc - Oscillator setting, 0..15 (7 = reset value).
439
+ * @returns {Promise<void>}
440
+ */
441
+ async setClock(divide, osc) {
442
+ await this._ready;
443
+ await this._command(_CMD_CLOCK, ((osc & 0x0F) << 4) | ((divide - 1) & 0x0F));
444
+ }
445
+
446
+ /**
447
+ * Set the pre-charge phase 1 and phase 2 periods in display clocks.
448
+ * @param {number} phase1 - 1..15.
449
+ * @param {number} phase2 - 1..15.
450
+ * @returns {Promise<void>}
451
+ */
452
+ async setPrecharge(phase1, phase2) {
453
+ await this._ready;
454
+ await this._command(_CMD_PRECHARGE, ((phase2 & 0x0F) << 4) | (phase1 & 0x0F));
455
+ }
456
+
457
+ /**
458
+ * Select the COM pin configuration.
459
+ * @param {boolean} alternative - True alternative (reset), false sequential.
460
+ * @param {boolean} [remap=false] - True to enable the COM left / right remap.
461
+ * @returns {Promise<void>}
462
+ */
463
+ async setComConfig(alternative, remap = false) {
464
+ await this._ready;
465
+ await this._command(_CMD_COM, 0x02 | (alternative ? 0x10 : 0) | (remap ? 0x20 : 0));
466
+ }
467
+
468
+ /** Set the VCOMH deselect level (only bits 5:2 used). @param {number} value - 0x00..0x3C (reset 0x34). @returns {Promise<void>} */
469
+ async setVcomh(value) {
470
+ await this._ready;
471
+ await this._command(_CMD_VCOMH, value & 0x3C);
472
+ }
473
+
474
+ /**
475
+ * Select how `show()` writes the display RAM.
476
+ * @param {'page'|'horizontal'|'vertical'} mode
477
+ * @returns {Promise<void>}
478
+ */
479
+ async setAddressingMode(mode) {
480
+ const codes = { horizontal: 0, vertical: 1, page: 2 };
481
+ if (!(mode in codes)) throw new RangeError('mode must be page, horizontal or vertical');
482
+ await this._ready;
483
+ this._mode = mode;
484
+ await this._command(_CMD_ADDR_MODE, codes[mode]);
485
+ }
486
+
487
+ // ---- hardware scrolling ------------------------------------------------------------------
488
+
489
+ /**
490
+ * Set up a continuous horizontal scroll (start it with `scrollStart`). Do not call while scrolling.
491
+ * @param {boolean} right - True to scroll right.
492
+ * @param {number} startPage - 0..7.
493
+ * @param {number} endPage - startPage..7.
494
+ * @param {number} interval - Table index 0..6 = 6, 32, 64, 128, 3, 4, 2 frames per step.
495
+ * @param {number} [columns=1] - Columns per step, 1..4.
496
+ * @returns {Promise<void>}
497
+ */
498
+ async setScrollHorizontal(right, startPage, endPage, interval, columns = 1) {
499
+ await this._ready;
500
+ await this._command(right ? 0x26 : 0x27, columns & 7, startPage & 7, _INTERVALS[interval], endPage & 7);
501
+ }
502
+
503
+ /**
504
+ * Set up a continuous vertical + horizontal scroll (start it with `scrollStart`).
505
+ * @param {boolean} right - True to scroll right.
506
+ * @param {number} startPage - 0..7.
507
+ * @param {number} endPage - startPage..7.
508
+ * @param {number} interval - Table index 0..6.
509
+ * @param {number} columns - Horizontal columns per step, 0..4.
510
+ * @param {number} rows - Vertical rows per step, 1..63.
511
+ * @returns {Promise<void>}
512
+ */
513
+ async setScrollVertical(right, startPage, endPage, interval, columns, rows) {
514
+ await this._ready;
515
+ await this._command(right ? 0x29 : 0x2A, columns & 7, startPage & 7, _INTERVALS[interval], endPage & 7, rows & 0x3F);
516
+ }
517
+
518
+ /**
519
+ * Set the vertical scroll area.
520
+ * @param {number} topFixed - Rows in the fixed top area, 0..63.
521
+ * @param {number} scrollRows - Rows in the scrolling area, 1..64.
522
+ * @returns {Promise<void>}
523
+ */
524
+ async setScrollArea(topFixed, scrollRows) {
525
+ await this._ready;
526
+ await this._command(_CMD_SCROLL_AREA, topFixed & 0x3F, scrollRows & 0x7F);
527
+ }
528
+
529
+ /** Activate the configured scroll (0x2F). @returns {Promise<void>} */
530
+ async scrollStart() {
531
+ await this._ready;
532
+ await this._command(0x2F);
533
+ }
534
+
535
+ /** Deactivate scrolling (0x2E) and rewrite the RAM from the framebuffer, as the datasheet requires. @returns {Promise<void>} */
536
+ async scrollStop() {
537
+ await this._ready;
538
+ await this._command(0x2E);
539
+ await this._showAll();
540
+ }
541
+
542
+ // ---- area colour -------------------------------------------------------------------------
543
+
544
+ /**
545
+ * Enable the area-colour mode and/or the low-power display mode.
546
+ * @param {boolean} enable - True to enable area colour.
547
+ * @param {boolean} [lowPower=false] - True for low-power display mode.
548
+ * @returns {Promise<void>}
549
+ */
550
+ async setAreaColor(enable, lowPower = false) {
551
+ await this._ready;
552
+ await this._command(_CMD_AREA_COLOR, (enable ? 0x30 : 0) | (lowPower ? 0x05 : 0));
553
+ }
554
+
555
+ /** Set the colour-bank brightness. @param {number} value - 0..255 (reset 0x80). @returns {Promise<void>} */
556
+ async setBrightness(value) {
557
+ await this._ready;
558
+ await this._command(_CMD_BRIGHTNESS, value & 0xFF);
559
+ }
560
+
561
+ /**
562
+ * Set the drive pulse width codes (31..63 = 32..64 clocks) of BANK0 and colours A, B, C.
563
+ * @param {number} bank0
564
+ * @param {number} a
565
+ * @param {number} b
566
+ * @param {number} c
567
+ * @returns {Promise<void>}
568
+ */
569
+ async setLut(bank0, a, b, c) {
570
+ await this._ready;
571
+ await this._command(_CMD_LUT, bank0 & 0x3F, a & 0x3F, b & 0x3F, c & 0x3F);
572
+ }
573
+
574
+ /**
575
+ * Assign colours A..D (2 bits per bank) to the area-colour banks.
576
+ * @param {Buffer|number[]} banks1to16 - 4 bytes for banks 1..16.
577
+ * @param {Buffer|number[]|null} [banks17to32=null] - Optional 4 bytes for banks 17..32.
578
+ * @returns {Promise<void>}
579
+ */
580
+ async setBankColors(banks1to16, banks17to32 = null) {
581
+ if (banks1to16.length !== 4 || (banks17to32 !== null && banks17to32.length !== 4)) {
582
+ throw new RangeError('bank colour groups need 4 bytes');
583
+ }
584
+ await this._ready;
585
+ await this._command(_CMD_BANK_1_16, ...banks1to16);
586
+ if (banks17to32 !== null) await this._command(_CMD_BANK_17_32, ...banks17to32);
587
+ }
588
+
589
+ // ---- reads and raw access --------------------------------------------------------------
590
+
591
+ /**
592
+ * Read the status byte (I²C only).
593
+ * @returns {Promise<number>} Bit 6 = 1 when the display is off; the other bits are reserved.
594
+ */
595
+ async readStatus() {
596
+ await this._ready;
597
+ if (!this._bus.canRead) throw new Error('the SPI interface is write-only');
598
+ return this._bus.readStatus();
599
+ }
600
+
601
+ /**
602
+ * Read `n` bytes of display RAM (I²C only); the dummy byte after an address change is discarded.
603
+ * @param {number} page - RAM page 0..7.
604
+ * @param {number} column - Start RAM column.
605
+ * @param {number} n - Number of bytes.
606
+ * @returns {Promise<Buffer>} The RAM bytes.
607
+ */
608
+ async readPage(page, column, n) {
609
+ await this._ready;
610
+ if (!this._bus.canRead) throw new Error('the SPI interface is write-only');
611
+ await this._bus.writeCommands(this._addressBytes(page, column));
612
+ return this._bus.readRam(n);
613
+ }
614
+
615
+ /** Send raw bytes as one command stream. @param {...number} bytes @returns {Promise<void>} */
616
+ async sendCommand(...bytes) {
617
+ await this._ready;
618
+ await this._command(...bytes);
619
+ }
620
+ }
621
+
622
+ /**
623
+ * SSD1305 over 4-wire SPI — minimal interface.
624
+ *
625
+ * Same framebuffer, drawing and initialization as SSD1305Minimal, driven through a display connection
626
+ * (CS, D/C, RES optional, no BUSY). The chip must be strapped for 4-wire SPI. SPI mode 0, up to
627
+ * 4 MHz (2 MHz recommended). Every command byte is sent with D/C low, RAM data with D/C high. SPI cannot read:
628
+ * `readStatus()` / `readPage()` of the full class reject.
629
+ *
630
+ * @param {object} connection - DisplayConnection.
631
+ * @param {number} [width=128] - Visible columns, 1..132.
632
+ * @param {number} [height=64] - Rows, a multiple of 8, 16..64.
633
+ * @param {number} [columnOffset=4] - First RAM column shown.
634
+ */
635
+ class SSD1305SpiMinimal extends SSD1305Minimal {
636
+ constructor(connection, width = 128, height = 64, columnOffset = 4) {
637
+ super(null, width, height, columnOffset, new SpiBus(connection));
638
+ }
639
+ }
640
+
641
+ /**
642
+ * SSD1305 over 4-wire SPI — full interface (see SSD1305Full; `readStatus()` and `readPage()` reject).
643
+ *
644
+ * @param {object} connection - DisplayConnection.
645
+ * @param {number} [width=128] - Visible columns, 1..132.
646
+ * @param {number} [height=64] - Rows, a multiple of 8, 16..64.
647
+ * @param {number} [columnOffset=4] - First RAM column shown.
648
+ */
649
+ class SSD1305SpiFull extends SSD1305Full {
650
+ constructor(connection, width = 128, height = 64, columnOffset = 4) {
651
+ super(null, width, height, columnOffset, new SpiBus(connection));
652
+ }
653
+ }
654
+
655
+ module.exports = { SSD1305Minimal, SSD1305Full, SSD1305SpiMinimal, SSD1305SpiFull, WIDTH, HEIGHT, I2C_CHUNK, SPI_CHUNK };