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