periph 1.2.0 → 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 +18 -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/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -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/examples/rtc/ds3231/complete.js +77 -0
- package/examples/rtc/ds3231/demo.js +60 -0
- package/examples/rtc/ds3231/minimal.js +23 -0
- package/examples/rtc/pcf8523/complete.js +66 -0
- package/examples/rtc/pcf8523/demo.js +65 -0
- package/examples/rtc/pcf8523/minimal.js +20 -0
- package/examples/temperature/mcp9808/complete.js +89 -0
- package/examples/temperature/mcp9808/demo.js +62 -0
- package/examples/temperature/mcp9808/minimal.js +21 -0
- package/examples/temperature/tmp117/complete.js +90 -0
- package/examples/temperature/tmp117/demo.js +73 -0
- package/examples/temperature/tmp117/minimal.js +21 -0
- package/examples/tof/vl53l0x/complete.js +118 -0
- package/examples/tof/vl53l0x/demo.js +83 -0
- package/examples/tof/vl53l0x/minimal.js +25 -0
- package/examples/tof/vl53l1x/complete.js +151 -0
- package/examples/tof/vl53l1x/demo.js +100 -0
- package/examples/tof/vl53l1x/minimal.js +25 -0
- 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 +45 -69
- package/src/chips/imu/mpu9255.js +54 -78
- 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 +239 -0
- 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 +504 -0
- package/src/chips/rtc/pcf8523.js +533 -0
- package/src/chips/temperature/mcp9808.js +490 -0
- package/src/chips/temperature/tmp117.js +471 -0
- package/src/chips/tof/_vl53_base.js +160 -0
- package/src/chips/tof/vl53l0x.js +816 -0
- package/src/chips/tof/vl53l1x.js +816 -0
- 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
- package/examples/motor/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const WIDTH = 96;
|
|
4
|
+
const HEIGHT = 64;
|
|
5
|
+
const BUFFER_SIZE = WIDTH * HEIGHT * 2;
|
|
6
|
+
|
|
7
|
+
const BLACK = 0x0000;
|
|
8
|
+
const WHITE = 0xFFFF;
|
|
9
|
+
const RED = 0xF800;
|
|
10
|
+
const GREEN = 0x07E0;
|
|
11
|
+
const BLUE = 0x001F;
|
|
12
|
+
const YELLOW = 0xFFE0;
|
|
13
|
+
const CYAN = 0x07FF;
|
|
14
|
+
const MAGENTA = 0xF81F;
|
|
15
|
+
|
|
16
|
+
const MODE_NORMAL = 0xA4;
|
|
17
|
+
const MODE_ALL_ON = 0xA5;
|
|
18
|
+
const MODE_ALL_OFF = 0xA6;
|
|
19
|
+
const MODE_INVERSE = 0xA7;
|
|
20
|
+
|
|
21
|
+
const _CMD_COLUMN = 0x15;
|
|
22
|
+
const _CMD_ROW = 0x75;
|
|
23
|
+
const _CMD_CONTRAST_A = 0x81;
|
|
24
|
+
const _CMD_CONTRAST_B = 0x82;
|
|
25
|
+
const _CMD_CONTRAST_C = 0x83;
|
|
26
|
+
const _CMD_MASTER_CURRENT = 0x87;
|
|
27
|
+
const _CMD_PRECHARGE_A = 0x8A;
|
|
28
|
+
const _CMD_PRECHARGE_B = 0x8B;
|
|
29
|
+
const _CMD_PRECHARGE_C = 0x8C;
|
|
30
|
+
const _CMD_REMAP = 0xA0;
|
|
31
|
+
const _CMD_START_LINE = 0xA1;
|
|
32
|
+
const _CMD_OFFSET = 0xA2;
|
|
33
|
+
const _CMD_MODE_NORMAL = 0xA4;
|
|
34
|
+
const _CMD_MUX = 0xA8;
|
|
35
|
+
const _CMD_DIM_SETTING = 0xAB;
|
|
36
|
+
const _CMD_MASTER_CONFIG = 0xAD;
|
|
37
|
+
const _CMD_DISPLAY_DIM = 0xAC;
|
|
38
|
+
const _CMD_DISPLAY_OFF = 0xAE;
|
|
39
|
+
const _CMD_DISPLAY_ON = 0xAF;
|
|
40
|
+
const _CMD_POWER_SAVE = 0xB0;
|
|
41
|
+
const _CMD_PHASE = 0xB1;
|
|
42
|
+
const _CMD_CLOCK = 0xB3;
|
|
43
|
+
const _CMD_GRAY_LINEAR = 0xB9;
|
|
44
|
+
const _CMD_PRECHARGE_LEVEL = 0xBB;
|
|
45
|
+
const _CMD_VCOMH = 0xBE;
|
|
46
|
+
const _CMD_LINE = 0x21;
|
|
47
|
+
const _CMD_RECT = 0x22;
|
|
48
|
+
const _CMD_COPY = 0x23;
|
|
49
|
+
const _CMD_DIM = 0x24;
|
|
50
|
+
const _CMD_CLEAR = 0x25;
|
|
51
|
+
const _CMD_FILL = 0x26;
|
|
52
|
+
const _CMD_SCROLL_SETUP = 0x27;
|
|
53
|
+
const _CMD_SCROLL_OFF = 0x2E;
|
|
54
|
+
const _CMD_SCROLL_ON = 0x2F;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Pack 8-bit R, G, B into an RGB565 value.
|
|
58
|
+
* @param {number} r - Red 0..255.
|
|
59
|
+
* @param {number} g - Green 0..255.
|
|
60
|
+
* @param {number} b - Blue 0..255.
|
|
61
|
+
* @returns {number} RGB565.
|
|
62
|
+
*/
|
|
63
|
+
function rgb(r, g, b) {
|
|
64
|
+
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xFF) >> 3);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// RGB565 -> three 5/6/5-bit operands (C = red, B = green, A = blue).
|
|
68
|
+
function _color(c) {
|
|
69
|
+
return [((c >> 11) & 0x1F) << 1, (c >> 5) & 0x3F, (c & 0x1F) << 1];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* SSD1331 96 x 64 RGB OLED controller — minimal interface.
|
|
74
|
+
*
|
|
75
|
+
* Drives the Waveshare 0.95inch RGB OLED over 4-wire SPI. The picture is kept
|
|
76
|
+
* in a 96 x 64 RGB565 framebuffer (12 288 bytes, high byte first) and sent to
|
|
77
|
+
* the display RAM by show().
|
|
78
|
+
*
|
|
79
|
+
* The connection is a DisplayConnection (see specs/transport_display.md) with
|
|
80
|
+
* no BUSY pin. The SSD1331 takes command parameters with D/C# low, so the
|
|
81
|
+
* driver sends every byte of a command as its own sendCommand(byte); only
|
|
82
|
+
* pixel data uses sendData().
|
|
83
|
+
*
|
|
84
|
+
* The constructor starts the initialisation sequence; every method waits for
|
|
85
|
+
* it, and `ready()` resolves (or rejects) when it is done.
|
|
86
|
+
*
|
|
87
|
+
* @param {object} connection - A DisplayConnection (SIMPLEX wiring, no BUSY pin).
|
|
88
|
+
*/
|
|
89
|
+
class SSD1331Minimal {
|
|
90
|
+
constructor(connection) {
|
|
91
|
+
this._conn = connection;
|
|
92
|
+
this._fb = Buffer.alloc(BUFFER_SIZE);
|
|
93
|
+
this._remap = 0x72;
|
|
94
|
+
this._ready = this._initSequence();
|
|
95
|
+
this._ready.catch(() => {}); // surfaced through ready() and the first awaited call
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** @returns {number} Panel width in pixels (96). */
|
|
99
|
+
get width() { return WIDTH; }
|
|
100
|
+
|
|
101
|
+
/** @returns {number} Panel height in pixels (64). */
|
|
102
|
+
get height() { return HEIGHT; }
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Resolves when initialisation has finished; rejects if it failed.
|
|
106
|
+
* @returns {Promise<void>}
|
|
107
|
+
*/
|
|
108
|
+
ready() { return this._ready; }
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Fill the framebuffer with one colour. The display is unchanged until show().
|
|
112
|
+
* @param {number} [color=BLACK] - RGB565 colour.
|
|
113
|
+
*/
|
|
114
|
+
clear(color = BLACK) {
|
|
115
|
+
const hi = (color >> 8) & 0xFF;
|
|
116
|
+
const lo = color & 0xFF;
|
|
117
|
+
for (let i = 0; i < BUFFER_SIZE; i += 2) {
|
|
118
|
+
this._fb[i] = hi;
|
|
119
|
+
this._fb[i + 1] = lo;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Set one framebuffer pixel. Out-of-range coordinates are ignored.
|
|
125
|
+
* @param {number} x - Column 0..95.
|
|
126
|
+
* @param {number} y - Row 0..63.
|
|
127
|
+
* @param {number} [color=WHITE] - RGB565 colour.
|
|
128
|
+
*/
|
|
129
|
+
setPixel(x, y, color = WHITE) {
|
|
130
|
+
if (x < 0 || y < 0 || x >= WIDTH || y >= HEIGHT) return;
|
|
131
|
+
const i = (y * WIDTH + x) * 2;
|
|
132
|
+
this._fb[i] = (color >> 8) & 0xFF;
|
|
133
|
+
this._fb[i + 1] = color & 0xFF;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Send the whole framebuffer to the display RAM: window 0..95 x 0..63 and
|
|
138
|
+
* 12 288 bytes in 192-byte (one row) transfers.
|
|
139
|
+
* @returns {Promise<void>}
|
|
140
|
+
*/
|
|
141
|
+
async show() {
|
|
142
|
+
await this._ready;
|
|
143
|
+
await this._window(0, 0, WIDTH - 1, HEIGHT - 1);
|
|
144
|
+
for (let off = 0; off < BUFFER_SIZE; off += 2 * WIDTH) {
|
|
145
|
+
await this._conn.sendData(this._fb.subarray(off, off + 2 * WIDTH));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Switch the display on (0xAF); the RAM content is shown. @returns {Promise<void>} */
|
|
150
|
+
async displayOn() { await this._ready; await this._cmd(_CMD_DISPLAY_ON); }
|
|
151
|
+
|
|
152
|
+
/** Switch the display off (0xAE); the RAM content is kept. @returns {Promise<void>} */
|
|
153
|
+
async displayOff() { await this._ready; await this._cmd(_CMD_DISPLAY_OFF); }
|
|
154
|
+
|
|
155
|
+
// One command byte or parameter each, all with D/C# low.
|
|
156
|
+
async _cmd(...values) {
|
|
157
|
+
for (const v of values) await this._conn.sendCommand(v & 0xFF);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async _window(x0, y0, x1, y1) {
|
|
161
|
+
await this._cmd(_CMD_COLUMN, x0, x1, _CMD_ROW, y0, y1);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async _initSequence() {
|
|
165
|
+
if (typeof this._conn.ready === 'function') await this._conn.ready();
|
|
166
|
+
await this._conn.reset();
|
|
167
|
+
await this._conn.delayMs(100);
|
|
168
|
+
await this._cmd(_CMD_DISPLAY_OFF);
|
|
169
|
+
await this._cmd(_CMD_CONTRAST_A, 0xFF, _CMD_CONTRAST_B, 0xFF, _CMD_CONTRAST_C, 0xFF);
|
|
170
|
+
await this._cmd(_CMD_MASTER_CURRENT, 0x06);
|
|
171
|
+
await this._cmd(_CMD_PRECHARGE_A, 0x64, _CMD_PRECHARGE_B, 0x78, _CMD_PRECHARGE_C, 0x64);
|
|
172
|
+
await this._cmd(_CMD_REMAP, this._remap);
|
|
173
|
+
await this._cmd(_CMD_START_LINE, 0x00, _CMD_OFFSET, 0x00);
|
|
174
|
+
await this._cmd(_CMD_MODE_NORMAL);
|
|
175
|
+
await this._cmd(_CMD_MUX, 0x3F, _CMD_MASTER_CONFIG, 0x8E);
|
|
176
|
+
await this._cmd(_CMD_POWER_SAVE, 0x0B, _CMD_PHASE, 0x31, _CMD_CLOCK, 0xF0);
|
|
177
|
+
await this._cmd(_CMD_PRECHARGE_LEVEL, 0x3A, _CMD_VCOMH, 0x3E);
|
|
178
|
+
await this._cmd(_CMD_SCROLL_OFF);
|
|
179
|
+
await this._cmd(_CMD_CLEAR, 0x00, 0x00, 0x5F, 0x3F);
|
|
180
|
+
await this._conn.delayMs(100);
|
|
181
|
+
await this._cmd(_CMD_DISPLAY_ON);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* SSD1331 96 x 64 RGB OLED controller — full interface.
|
|
187
|
+
*
|
|
188
|
+
* Extends SSD1331Minimal with software drawing into the framebuffer, partial
|
|
189
|
+
* transfers, the controller's graphic acceleration commands (line, rectangle
|
|
190
|
+
* with fill, copy, dim and clear window, scrolling — these act on the display
|
|
191
|
+
* RAM only, not on the host framebuffer), and the electrical settings
|
|
192
|
+
* (contrast, master current, flip, display mode, power save, dim mode, gray
|
|
193
|
+
* table).
|
|
194
|
+
*/
|
|
195
|
+
class SSD1331Full extends SSD1331Minimal {
|
|
196
|
+
// -- framebuffer ------------------------------------------------------
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* RGB565 colour of one framebuffer pixel.
|
|
200
|
+
* @param {number} x - Column.
|
|
201
|
+
* @param {number} y - Row.
|
|
202
|
+
* @returns {number} RGB565 (0 when out of range).
|
|
203
|
+
*/
|
|
204
|
+
getPixel(x, y) {
|
|
205
|
+
if (x < 0 || y < 0 || x >= WIDTH || y >= HEIGHT) return 0;
|
|
206
|
+
const i = (y * WIDTH + x) * 2;
|
|
207
|
+
return (this._fb[i] << 8) | this._fb[i + 1];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Bresenham line into the framebuffer.
|
|
212
|
+
* @param {number} x0 - Start column.
|
|
213
|
+
* @param {number} y0 - Start row.
|
|
214
|
+
* @param {number} x1 - End column.
|
|
215
|
+
* @param {number} y1 - End row.
|
|
216
|
+
* @param {number} [color=WHITE] - RGB565 colour.
|
|
217
|
+
*/
|
|
218
|
+
drawLine(x0, y0, x1, y1, color = WHITE) {
|
|
219
|
+
const dx = Math.abs(x1 - x0);
|
|
220
|
+
const dy = -Math.abs(y1 - y0);
|
|
221
|
+
const sx = x0 < x1 ? 1 : -1;
|
|
222
|
+
const sy = y0 < y1 ? 1 : -1;
|
|
223
|
+
let err = dx + dy;
|
|
224
|
+
for (;;) {
|
|
225
|
+
this.setPixel(x0, y0, color);
|
|
226
|
+
if (x0 === x1 && y0 === y1) break;
|
|
227
|
+
const e2 = 2 * err;
|
|
228
|
+
if (e2 >= dy) { err += dy; x0 += sx; }
|
|
229
|
+
if (e2 <= dx) { err += dx; y0 += sy; }
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Rectangle outline or filled rectangle into the framebuffer.
|
|
235
|
+
* @param {number} x - Left column.
|
|
236
|
+
* @param {number} y - Top row.
|
|
237
|
+
* @param {number} w - Width in pixels.
|
|
238
|
+
* @param {number} h - Height in pixels.
|
|
239
|
+
* @param {number} [color=WHITE] - RGB565 colour.
|
|
240
|
+
* @param {boolean} [fill=false] - true fills the rectangle.
|
|
241
|
+
*/
|
|
242
|
+
drawRect(x, y, w, h, color = WHITE, fill = false) {
|
|
243
|
+
if (w <= 0 || h <= 0) return;
|
|
244
|
+
for (let j = 0; j < h; j++) {
|
|
245
|
+
for (let i = 0; i < w; i++) {
|
|
246
|
+
if (fill || i === 0 || j === 0 || i === w - 1 || j === h - 1) this.setPixel(x + i, y + j, color);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Copy an RGB565 bitmap into the framebuffer.
|
|
253
|
+
* @param {number} x - Left column.
|
|
254
|
+
* @param {number} y - Top row.
|
|
255
|
+
* @param {ArrayLike<number>} pixels - Row-major w * h RGB565 values.
|
|
256
|
+
* @param {number} w - Bitmap width in pixels.
|
|
257
|
+
* @param {number} h - Bitmap height in pixels.
|
|
258
|
+
*/
|
|
259
|
+
blit(x, y, pixels, w, h) {
|
|
260
|
+
for (let j = 0; j < h; j++) {
|
|
261
|
+
for (let i = 0; i < w; i++) this.setPixel(x + i, y + j, pixels[j * w + i]);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Send only a rectangle of the framebuffer (one window, row by row).
|
|
267
|
+
* @param {number} x - Left column.
|
|
268
|
+
* @param {number} y - Top row.
|
|
269
|
+
* @param {number} w - Width in pixels.
|
|
270
|
+
* @param {number} h - Height in pixels. The region is clipped to the panel.
|
|
271
|
+
* @returns {Promise<void>}
|
|
272
|
+
*/
|
|
273
|
+
async showRegion(x, y, w, h) {
|
|
274
|
+
await this._ready;
|
|
275
|
+
if (x < 0) { w += x; x = 0; }
|
|
276
|
+
if (y < 0) { h += y; y = 0; }
|
|
277
|
+
w = Math.min(w, WIDTH - x);
|
|
278
|
+
h = Math.min(h, HEIGHT - y);
|
|
279
|
+
if (w <= 0 || h <= 0) return;
|
|
280
|
+
await this._window(x, y, x + w - 1, y + h - 1);
|
|
281
|
+
for (let j = 0; j < h; j++) {
|
|
282
|
+
const start = ((y + j) * WIDTH + x) * 2;
|
|
283
|
+
await this._conn.sendData(this._fb.subarray(start, start + w * 2));
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// -- graphic acceleration (display RAM only) ---------------------------
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Hardware line (0x21) on the display RAM.
|
|
291
|
+
* @param {number} x0 - Start column 0..95.
|
|
292
|
+
* @param {number} y0 - Start row 0..63.
|
|
293
|
+
* @param {number} x1 - End column.
|
|
294
|
+
* @param {number} y1 - End row.
|
|
295
|
+
* @param {number} color - RGB565 colour.
|
|
296
|
+
* @returns {Promise<void>}
|
|
297
|
+
*/
|
|
298
|
+
async hwLine(x0, y0, x1, y1, color) {
|
|
299
|
+
await this._ready;
|
|
300
|
+
await this._cmd(_CMD_LINE, x0, y0, x1, y1, ..._color(color));
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Hardware rectangle (0x26 fill enable + 0x22) on the display RAM.
|
|
305
|
+
* @param {number} x - Left column.
|
|
306
|
+
* @param {number} y - Top row.
|
|
307
|
+
* @param {number} w - Width (>= 1).
|
|
308
|
+
* @param {number} h - Height (>= 1).
|
|
309
|
+
* @param {number} lineColor - Outline RGB565.
|
|
310
|
+
* @param {number} [fillColor=0] - Fill RGB565 (used when fill is true).
|
|
311
|
+
* @param {boolean} [fill=false] - Fill the interior.
|
|
312
|
+
* @returns {Promise<void>}
|
|
313
|
+
*/
|
|
314
|
+
async hwRect(x, y, w, h, lineColor, fillColor = 0, fill = false) {
|
|
315
|
+
await this._ready;
|
|
316
|
+
if (w <= 0 || h <= 0) return;
|
|
317
|
+
await this._cmd(_CMD_FILL, fill ? 1 : 0);
|
|
318
|
+
await this._cmd(_CMD_RECT, x, y, x + w - 1, y + h - 1, ..._color(lineColor), ..._color(fillColor));
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** Clear a window of the display RAM (0x25). @returns {Promise<void>} */
|
|
322
|
+
async hwClear(x0, y0, x1, y1) { await this._ready; await this._cmd(_CMD_CLEAR, x0, y0, x1, y1); }
|
|
323
|
+
|
|
324
|
+
/** Dim a window of the display RAM (0x24). @returns {Promise<void>} */
|
|
325
|
+
async hwDim(x0, y0, x1, y1) { await this._ready; await this._cmd(_CMD_DIM, x0, y0, x1, y1); }
|
|
326
|
+
|
|
327
|
+
/** Copy a window of the display RAM to (x, y) (0x23). @returns {Promise<void>} */
|
|
328
|
+
async hwCopy(x0, y0, x1, y1, x, y) { await this._ready; await this._cmd(_CMD_COPY, x0, y0, x1, y1, x, y); }
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Configure and start continuous scrolling (0x27, 0x2F).
|
|
332
|
+
* @param {number} horizontal - Columns per step 0..95 (0 = none).
|
|
333
|
+
* @param {number} startRow - First scrolled row.
|
|
334
|
+
* @param {number} rows - Number of scrolled rows.
|
|
335
|
+
* @param {number} vertical - Rows per step 0..63 (0 = none).
|
|
336
|
+
* @param {number} [interval=0] - 0 = 6, 1 = 10, 2 = 100, 3 = 200 frames per step.
|
|
337
|
+
* @returns {Promise<void>}
|
|
338
|
+
*/
|
|
339
|
+
async scroll(horizontal, startRow, rows, vertical, interval = 0) {
|
|
340
|
+
await this._ready;
|
|
341
|
+
await this._cmd(_CMD_SCROLL_SETUP, horizontal, startRow, rows, vertical, interval & 3, _CMD_SCROLL_ON);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** Stop scrolling (0x2E). The display RAM must be rewritten afterwards. @returns {Promise<void>} */
|
|
345
|
+
async scrollStop() { await this._ready; await this._cmd(_CMD_SCROLL_OFF); }
|
|
346
|
+
|
|
347
|
+
// -- settings ----------------------------------------------------------
|
|
348
|
+
|
|
349
|
+
/** Contrast of colour channels A, B, C (0x81..0x83), 0..255. @returns {Promise<void>} */
|
|
350
|
+
async setContrast(a, b, c) {
|
|
351
|
+
await this._ready;
|
|
352
|
+
await this._cmd(_CMD_CONTRAST_A, a, _CMD_CONTRAST_B, b, _CMD_CONTRAST_C, c);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** Master current (0x87), n/16 for n = 1..16 (clamped). @returns {Promise<void>} */
|
|
356
|
+
async setMasterCurrent(n) {
|
|
357
|
+
await this._ready;
|
|
358
|
+
await this._cmd(_CMD_MASTER_CURRENT, Math.min(16, Math.max(1, n)) - 1);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Display mode.
|
|
363
|
+
* @param {number} mode - MODE_NORMAL, MODE_ALL_ON, MODE_ALL_OFF or MODE_INVERSE.
|
|
364
|
+
* @returns {Promise<void>}
|
|
365
|
+
*/
|
|
366
|
+
async setDisplayMode(mode) { await this._ready; await this._cmd(mode); }
|
|
367
|
+
|
|
368
|
+
/** Raw remap and colour depth byte (0xA0). @returns {Promise<void>} */
|
|
369
|
+
async setRemap(flags) {
|
|
370
|
+
await this._ready;
|
|
371
|
+
this._remap = flags & 0xFF;
|
|
372
|
+
await this._cmd(_CMD_REMAP, this._remap);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Mirror horizontally (column remap, bit 1) and/or vertically (COM scan, bit 4).
|
|
377
|
+
* @returns {Promise<void>}
|
|
378
|
+
*/
|
|
379
|
+
async setFlip(horizontal, vertical) {
|
|
380
|
+
let r = this._remap & ~0x12;
|
|
381
|
+
if (horizontal) r |= 0x02;
|
|
382
|
+
if (vertical) r |= 0x10;
|
|
383
|
+
await this.setRemap(r);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** Power save mode (0xB0): true = 0x1A enable, false = 0x0B disable. @returns {Promise<void>} */
|
|
387
|
+
async setPowerSave(on) { await this._ready; await this._cmd(_CMD_POWER_SAVE, on ? 0x1A : 0x0B); }
|
|
388
|
+
|
|
389
|
+
/** Dim mode (0xAB + 0xAC): contrasts A/B/C and pre-charge voltage 0..31. @returns {Promise<void>} */
|
|
390
|
+
async dimMode(a, b, c, precharge) {
|
|
391
|
+
await this._ready;
|
|
392
|
+
await this._cmd(_CMD_DIM_SETTING, 0x00, a, b, c, precharge & 0x1F, _CMD_DISPLAY_DIM);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** Restore the linear gray scale table (0xB9). @returns {Promise<void>} */
|
|
396
|
+
async grayLinear() { await this._ready; await this._cmd(_CMD_GRAY_LINEAR); }
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Send any command with parameters (all with D/C# low).
|
|
400
|
+
* @param {number} command - Command byte.
|
|
401
|
+
* @param {number[]} [params=[]] - Parameter bytes.
|
|
402
|
+
* @returns {Promise<void>}
|
|
403
|
+
*/
|
|
404
|
+
async sendCommand(command, params = []) { await this._ready; await this._cmd(command, ...params); }
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
module.exports = {
|
|
408
|
+
SSD1331Minimal, SSD1331Full, rgb,
|
|
409
|
+
WIDTH, HEIGHT, BUFFER_SIZE,
|
|
410
|
+
BLACK, WHITE, RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA,
|
|
411
|
+
MODE_NORMAL, MODE_ALL_ON, MODE_ALL_OFF, MODE_INVERSE,
|
|
412
|
+
};
|