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.
- package/README.md +27 -0
- package/examples/adc_dac/ad5251/complete.js +66 -0
- package/examples/adc_dac/ad5251/demo.js +69 -0
- package/examples/adc_dac/ad5251/minimal.js +23 -0
- package/examples/adc_dac/ad5252/complete.js +66 -0
- package/examples/adc_dac/ad5252/demo.js +69 -0
- package/examples/adc_dac/ad5252/minimal.js +23 -0
- package/examples/adc_dac/ads7828/complete.js +46 -0
- package/examples/adc_dac/ads7828/demo.js +45 -0
- package/examples/adc_dac/ads7828/minimal.js +17 -0
- package/examples/adc_dac/cat5171/complete.js +52 -0
- package/examples/adc_dac/cat5171/demo.js +59 -0
- package/examples/adc_dac/cat5171/minimal.js +23 -0
- package/examples/adc_dac/cs43l22/complete.js +88 -0
- package/examples/adc_dac/cs43l22/demo.js +56 -0
- package/examples/adc_dac/cs43l22/minimal.js +20 -0
- package/examples/adc_dac/ds1881/complete.js +62 -0
- package/examples/adc_dac/ds1881/demo.js +61 -0
- package/examples/adc_dac/ds1881/minimal.js +25 -0
- package/examples/adc_dac/ds3502/complete.js +46 -0
- package/examples/adc_dac/ds3502/demo.js +54 -0
- package/examples/adc_dac/ds3502/minimal.js +28 -0
- package/examples/adc_dac/mcp3422/complete.js +48 -0
- package/examples/adc_dac/mcp3422/demo.js +49 -0
- package/examples/adc_dac/mcp3422/minimal.js +25 -0
- package/examples/display/sh1106/complete.js +101 -0
- package/examples/display/sh1106/demo.js +68 -0
- package/examples/display/sh1106/minimal.js +25 -0
- package/examples/display/ssd1305/complete.js +129 -0
- package/examples/display/ssd1305/demo.js +71 -0
- package/examples/display/ssd1305/minimal.js +25 -0
- package/examples/display/ssd1306/complete.js +115 -0
- package/examples/display/ssd1306/demo.js +70 -0
- package/examples/display/ssd1306/minimal.js +25 -0
- package/examples/environmental/am2315/complete.js +34 -0
- package/examples/environmental/am2315/demo.js +46 -0
- package/examples/environmental/am2315/minimal.js +21 -0
- package/examples/humidity/htu21df/complete.js +49 -0
- package/examples/humidity/htu21df/demo.js +50 -0
- package/examples/humidity/htu21df/minimal.js +22 -0
- package/examples/humidity/sht31/complete.js +53 -0
- package/examples/humidity/sht31/demo.js +53 -0
- package/examples/humidity/sht31/minimal.js +21 -0
- package/examples/humidity/si7021/complete.js +55 -0
- package/examples/humidity/si7021/demo.js +58 -0
- package/examples/humidity/si7021/minimal.js +21 -0
- package/examples/imu/bno055/complete.js +85 -0
- package/examples/imu/bno055/demo.js +70 -0
- package/examples/imu/bno055/minimal.js +20 -0
- package/examples/io_expander/pca6408a/complete.js +63 -0
- package/examples/io_expander/pca6408a/demo.js +58 -0
- package/examples/io_expander/pca6408a/minimal.js +20 -0
- package/examples/io_expander/pca6416a/complete.js +68 -0
- package/examples/io_expander/pca6416a/demo.js +60 -0
- package/examples/io_expander/pca6416a/minimal.js +20 -0
- package/examples/io_expander/pca9536/complete.js +63 -0
- package/examples/io_expander/pca9536/demo.js +57 -0
- package/examples/io_expander/pca9536/minimal.js +20 -0
- package/examples/io_expander/pca9539/complete.js +68 -0
- package/examples/io_expander/pca9539/demo.js +60 -0
- package/examples/io_expander/pca9539/minimal.js +20 -0
- package/examples/light/max44009/complete.js +56 -0
- package/examples/light/max44009/demo.js +49 -0
- package/examples/light/max44009/minimal.js +22 -0
- package/examples/memory/at24c02n/complete.js +39 -0
- package/examples/memory/at24c02n/demo.js +39 -0
- package/examples/memory/at24c02n/minimal.js +15 -0
- package/examples/memory/at24c64/complete.js +39 -0
- package/examples/memory/at24c64/demo.js +39 -0
- package/examples/memory/at24c64/minimal.js +15 -0
- package/examples/memory/cat24c512/complete.js +39 -0
- package/examples/memory/cat24c512/demo.js +39 -0
- package/examples/memory/cat24c512/minimal.js +15 -0
- package/examples/other/pca9541/complete.js +52 -0
- package/examples/other/pca9541/demo.js +61 -0
- package/examples/other/pca9541/minimal.js +23 -0
- package/examples/temperature/tmp102/complete.js +74 -0
- package/examples/temperature/tmp102/demo.js +71 -0
- package/examples/temperature/tmp102/minimal.js +20 -0
- package/examples/tof/vl6180x/complete.js +62 -0
- package/examples/tof/vl6180x/demo.js +77 -0
- package/examples/tof/vl6180x/minimal.js +23 -0
- package/package.json +1 -1
- package/src/chips/adc_dac/ad5251.js +345 -0
- package/src/chips/adc_dac/ad5252.js +345 -0
- package/src/chips/adc_dac/ads7828.js +225 -0
- package/src/chips/adc_dac/cat5171.js +213 -0
- package/src/chips/adc_dac/cs43l22.js +645 -0
- package/src/chips/adc_dac/ds1881.js +357 -0
- package/src/chips/adc_dac/ds3502.js +241 -0
- package/src/chips/adc_dac/mcp3422.js +312 -0
- package/src/chips/display/mono_oled.js +182 -0
- package/src/chips/display/sh1106.js +496 -0
- package/src/chips/display/ssd1305.js +655 -0
- package/src/chips/display/ssd1306.js +458 -0
- package/src/chips/environmental/am2315.js +237 -0
- package/src/chips/humidity/htu21df.js +222 -0
- package/src/chips/humidity/sht31.js +284 -0
- package/src/chips/humidity/si7021.js +282 -0
- package/src/chips/imu/bno055.js +496 -0
- package/src/chips/io_expander/pca6408a.js +407 -0
- package/src/chips/io_expander/pca6416a.js +455 -0
- package/src/chips/io_expander/pca9536.js +398 -0
- package/src/chips/io_expander/pca9539.js +457 -0
- package/src/chips/light/max44009.js +336 -0
- package/src/chips/memory/at24c02n.js +220 -0
- package/src/chips/memory/at24c64.js +220 -0
- package/src/chips/memory/cat24c512.js +221 -0
- package/src/chips/other/pca9541.js +315 -0
- package/src/chips/temperature/tmp102.js +361 -0
- package/src/chips/tof/vl6180x.js +580 -0
- package/src/discovery/registry.js +65 -13
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const CFG_RDY = 0x80;
|
|
4
|
+
const CFG_CONT = 0x10;
|
|
5
|
+
const VREF = 2.048;
|
|
6
|
+
const BITS_TO_S = { 12: 0, 14: 1, 16: 2, 18: 3 };
|
|
7
|
+
const GAIN_TO_G = { 1: 0, 2: 1, 4: 2, 8: 3 };
|
|
8
|
+
const CONV_S = { 12: 1 / 240, 14: 1 / 60, 16: 1 / 15, 18: 1 / 3.75 }; // nominal conversion times
|
|
9
|
+
|
|
10
|
+
const GC_RESET = Buffer.from([0x00, 0x06]);
|
|
11
|
+
const GC_CONVERT = Buffer.from([0x00, 0x08]);
|
|
12
|
+
|
|
13
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* MCP3422 2-channel 18-bit delta-sigma ADC with PGA (Microchip) — minimal
|
|
17
|
+
* interface.
|
|
18
|
+
*
|
|
19
|
+
* Two differential inputs, an on-board 2.048 V reference, a PGA (x1, x2, x4,
|
|
20
|
+
* x8) and an on-board oscillator. The resolution is selected together with the
|
|
21
|
+
* data rate: 12 bit / 240 SPS, 14 bit / 60 SPS, 16 bit / 15 SPS, 18 bit /
|
|
22
|
+
* 3.75 SPS. The full-scale differential input range is ±2.048 V / gain. The
|
|
23
|
+
* chip has no register map: a write is one configuration byte
|
|
24
|
+
* (`RDY C1 C0 O/C S1 S0 G1 G0`) and a read returns the 2 (12/14/16 bit) or 3
|
|
25
|
+
* (18 bit) result bytes followed by the configuration byte, whose RDY bit
|
|
26
|
+
* (0 = fresh result) says whether the conversion has finished. The driver
|
|
27
|
+
* therefore uses the plain Connection `write()` / `read()` calls.
|
|
28
|
+
*
|
|
29
|
+
* Defaults: one-shot mode (lowest power), 16 bit / 15 SPS (LSB 62.5 µV), gain
|
|
30
|
+
* x1. `init()` writes that configuration with RDY = 0, so nothing is started.
|
|
31
|
+
*/
|
|
32
|
+
class MCP3422Minimal {
|
|
33
|
+
/**
|
|
34
|
+
* @param {import('../../connection/connection').Connection} connection - I²C connection (0x68–0x6F).
|
|
35
|
+
*/
|
|
36
|
+
constructor(connection) {
|
|
37
|
+
this._conn = connection;
|
|
38
|
+
this._bits = 16;
|
|
39
|
+
this._gain = 1;
|
|
40
|
+
this._channel = 0;
|
|
41
|
+
this._continuous = false;
|
|
42
|
+
this._discard = false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 7-bit I²C address of the standard part (order code A0). */
|
|
46
|
+
static get I2C_ADDRESS() { return 0x68; }
|
|
47
|
+
|
|
48
|
+
_config(rdy) {
|
|
49
|
+
let c = (this._channel << 5) | (BITS_TO_S[this._bits] << 2) | GAIN_TO_G[this._gain];
|
|
50
|
+
if (rdy) c |= CFG_RDY;
|
|
51
|
+
if (this._continuous) c |= CFG_CONT;
|
|
52
|
+
return c;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
_nbytes() { return this._bits === 18 ? 3 : 2; }
|
|
56
|
+
|
|
57
|
+
_decode(data) {
|
|
58
|
+
let raw = 0;
|
|
59
|
+
for (let i = 0; i < this._nbytes(); i++) raw = (raw * 256) + data[i];
|
|
60
|
+
const span = 2 ** this._bits;
|
|
61
|
+
raw = raw % span;
|
|
62
|
+
if (raw >= span / 2) raw -= span;
|
|
63
|
+
return raw;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async _waitReady(convS) {
|
|
67
|
+
const polls = Math.floor((2 * convS + 0.010) / 0.005) + 1;
|
|
68
|
+
for (let i = 0; i < polls; i++) {
|
|
69
|
+
const data = await this._conn.read(this._nbytes() + 1);
|
|
70
|
+
if (!(data[this._nbytes()] & CFG_RDY)) return this._decode(data);
|
|
71
|
+
await sleep(5);
|
|
72
|
+
}
|
|
73
|
+
throw new Error('MCP3422 conversion did not finish');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async _convert(channel) {
|
|
77
|
+
this._channel = channel < 1 ? 0 : 1;
|
|
78
|
+
this._continuous = false;
|
|
79
|
+
this._discard = false;
|
|
80
|
+
const conv = CONV_S[this._bits];
|
|
81
|
+
await this._conn.write(Buffer.from([this._config(true)]));
|
|
82
|
+
await sleep(conv * 1100);
|
|
83
|
+
return this._waitReady(conv);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Write the default configuration (one-shot, 16 bit / 15 SPS, gain x1, CH1,
|
|
88
|
+
* RDY = 0); nothing is started and the chip goes to standby.
|
|
89
|
+
* @returns {Promise<void>}
|
|
90
|
+
*/
|
|
91
|
+
async init() {
|
|
92
|
+
await this._conn.write(Buffer.from([this._config(false)]));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Run a one-shot conversion on a channel and return the signed code.
|
|
97
|
+
* @param {number} channel - 0 (CH1) or 1 (CH2); values outside 0–1 are clamped.
|
|
98
|
+
* @returns {Promise<number>} Signed code, −2^(N−1) … 2^(N−1) − 1 (N = resolution bits).
|
|
99
|
+
* @throws {Error} if the conversion does not finish in time.
|
|
100
|
+
*/
|
|
101
|
+
async readRaw(channel) {
|
|
102
|
+
return this._convert(channel);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Run a one-shot conversion on a channel and return the differential voltage.
|
|
107
|
+
* @param {number} channel - 0 (CH1) or 1 (CH2); values outside 0–1 are clamped.
|
|
108
|
+
* @returns {Promise<number>} `CHn+ − CHn−` in volts, `raw * lsb / gain`.
|
|
109
|
+
*/
|
|
110
|
+
async readVoltage(channel) {
|
|
111
|
+
return (await this._convert(channel)) * this.lsb() / this._gain;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Convert both channels, one conversion each.
|
|
116
|
+
* @returns {Promise<number[]>} `[CH1, CH2]` voltages in volts.
|
|
117
|
+
*/
|
|
118
|
+
async readAll() {
|
|
119
|
+
return [await this.readVoltage(0), await this.readVoltage(1)];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Size of one code before the gain.
|
|
124
|
+
* @returns {number} `2 * 2.048 / 2^bits` in volts.
|
|
125
|
+
*/
|
|
126
|
+
lsb() {
|
|
127
|
+
return 2 * VREF / (2 ** this._bits);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* MCP3422 full interface — extends Minimal with resolution, gain and channel
|
|
133
|
+
* selection, continuous conversion, status reads, scans and general calls. The
|
|
134
|
+
* driver tracks resolution, gain, channel and mode.
|
|
135
|
+
*/
|
|
136
|
+
class MCP3422Full extends MCP3422Minimal {
|
|
137
|
+
async _apply() {
|
|
138
|
+
if (this._continuous) {
|
|
139
|
+
await this._conn.write(Buffer.from([this._config(false)]));
|
|
140
|
+
this._discard = true;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Select the resolution (and with it the data rate).
|
|
146
|
+
* @param {number} bits - 12 (240 SPS), 14 (60 SPS), 16 (15 SPS) or 18 (3.75 SPS).
|
|
147
|
+
* @returns {Promise<void>}
|
|
148
|
+
* @throws {RangeError} for any other value.
|
|
149
|
+
*/
|
|
150
|
+
async setResolution(bits) {
|
|
151
|
+
if (!(bits in BITS_TO_S)) throw new RangeError('bits must be 12, 14, 16 or 18');
|
|
152
|
+
this._bits = bits;
|
|
153
|
+
await this._apply();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The selected resolution.
|
|
158
|
+
* @returns {number} 12, 14, 16 or 18.
|
|
159
|
+
*/
|
|
160
|
+
getResolution() { return this._bits; }
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Select the PGA gain.
|
|
164
|
+
* @param {number} gain - 1, 2, 4 or 8.
|
|
165
|
+
* @returns {Promise<void>}
|
|
166
|
+
* @throws {RangeError} for any other value.
|
|
167
|
+
*/
|
|
168
|
+
async setGain(gain) {
|
|
169
|
+
if (!(gain in GAIN_TO_G)) throw new RangeError('gain must be 1, 2, 4 or 8');
|
|
170
|
+
this._gain = gain;
|
|
171
|
+
await this._apply();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The selected PGA gain.
|
|
176
|
+
* @returns {number} 1, 2, 4 or 8.
|
|
177
|
+
*/
|
|
178
|
+
getGain() { return this._gain; }
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Select the channel used by continuous conversion.
|
|
182
|
+
* @param {number} channel - 0 (CH1) or 1 (CH2).
|
|
183
|
+
* @returns {Promise<void>}
|
|
184
|
+
*/
|
|
185
|
+
async setChannel(channel) {
|
|
186
|
+
this._channel = channel < 1 ? 0 : 1;
|
|
187
|
+
await this._apply();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Switch to continuous conversion at the current rate and gain.
|
|
192
|
+
* @param {number} [channel] - Optional channel 0 or 1 to select first.
|
|
193
|
+
* @returns {Promise<void>}
|
|
194
|
+
*/
|
|
195
|
+
async startContinuous(channel) {
|
|
196
|
+
if (channel !== undefined && channel !== null) this._channel = channel < 1 ? 0 : 1;
|
|
197
|
+
this._continuous = true;
|
|
198
|
+
this._discard = true;
|
|
199
|
+
await this._conn.write(Buffer.from([this._config(false)]));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Return to one-shot mode / standby (writes O/C = 0, RDY = 0).
|
|
204
|
+
* @returns {Promise<void>}
|
|
205
|
+
*/
|
|
206
|
+
async stopContinuous() {
|
|
207
|
+
this._continuous = false;
|
|
208
|
+
this._discard = false;
|
|
209
|
+
await this._conn.write(Buffer.from([this._config(false)]));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Report the conversion mode.
|
|
214
|
+
* @returns {boolean} true in continuous mode.
|
|
215
|
+
*/
|
|
216
|
+
isContinuous() { return this._continuous; }
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Wait for a fresh result in continuous mode and return the signed code. The
|
|
220
|
+
* first sample after a configuration change is discarded.
|
|
221
|
+
* @returns {Promise<number>}
|
|
222
|
+
* @throws {Error} if the driver is not in continuous mode or no result arrives in time.
|
|
223
|
+
*/
|
|
224
|
+
async readContinuousRaw() {
|
|
225
|
+
if (!this._continuous) throw new Error('not in continuous mode; call startContinuous()');
|
|
226
|
+
const conv = CONV_S[this._bits];
|
|
227
|
+
if (this._discard) {
|
|
228
|
+
await this._waitReady(conv);
|
|
229
|
+
this._discard = false;
|
|
230
|
+
}
|
|
231
|
+
return this._waitReady(conv);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Voltage form of readContinuousRaw().
|
|
236
|
+
* @returns {Promise<number>} Differential voltage in volts.
|
|
237
|
+
*/
|
|
238
|
+
async readContinuous() {
|
|
239
|
+
return (await this.readContinuousRaw()) * this.lsb() / this._gain;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Read the configuration byte (the last byte of a read).
|
|
244
|
+
* @returns {Promise<number>} Configuration byte; bit 7 is the live RDY flag.
|
|
245
|
+
*/
|
|
246
|
+
async readConfig() {
|
|
247
|
+
const data = await this._conn.read(this._nbytes() + 1);
|
|
248
|
+
return data[this._nbytes()];
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Report whether a fresh result is available (RDY = 0).
|
|
253
|
+
* @returns {Promise<boolean>}
|
|
254
|
+
*/
|
|
255
|
+
async isReady() {
|
|
256
|
+
return !((await this.readConfig()) & CFG_RDY);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Run one one-shot conversion per entry, in order.
|
|
261
|
+
* @param {number[]} channels - Channels (0 or 1).
|
|
262
|
+
* @returns {Promise<number[]>} Voltages in volts, one per entry.
|
|
263
|
+
*/
|
|
264
|
+
async readScan(channels) {
|
|
265
|
+
const out = [];
|
|
266
|
+
for (const ch of channels) out.push(await this.readVoltage(ch));
|
|
267
|
+
return out;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Send the general call reset (0x00 0x06): power-on defaults on the chip
|
|
272
|
+
* (continuous, 12 bit, gain x1, CH1); the driver state follows. Needs a
|
|
273
|
+
* Connection that transmits to the general-call address.
|
|
274
|
+
* @returns {Promise<void>}
|
|
275
|
+
*/
|
|
276
|
+
async generalCallReset() {
|
|
277
|
+
await this._conn.write(GC_RESET);
|
|
278
|
+
this._bits = 12;
|
|
279
|
+
this._gain = 1;
|
|
280
|
+
this._channel = 0;
|
|
281
|
+
this._continuous = true;
|
|
282
|
+
this._discard = true;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Send the general call conversion (0x00 0x08): a one-shot conversion on every
|
|
287
|
+
* device on the bus; read the result with readPendingRaw().
|
|
288
|
+
* @returns {Promise<void>}
|
|
289
|
+
*/
|
|
290
|
+
async generalCallConvert() {
|
|
291
|
+
await this._conn.write(GC_CONVERT);
|
|
292
|
+
this._continuous = false;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Wait for RDY = 0 and return the code of a conversion started elsewhere.
|
|
297
|
+
* @returns {Promise<number>}
|
|
298
|
+
*/
|
|
299
|
+
async readPendingRaw() {
|
|
300
|
+
return this._waitReady(CONV_S[this._bits]);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Full-scale differential input range at the current gain.
|
|
305
|
+
* @returns {number} `2.048 / gain` in volts.
|
|
306
|
+
*/
|
|
307
|
+
fullScale() {
|
|
308
|
+
return VREF / this._gain;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
module.exports = { MCP3422Minimal, MCP3422Full };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Shared building blocks of the monochrome page-organised OLED controller drivers (SSD130x style):
|
|
5
|
+
* a page-major 1 bpp framebuffer with drawing primitives, and the two transports (control-byte
|
|
6
|
+
* framed I²C, D/C-pin framed 4-wire SPI).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const I2C_CHUNK = 32; // largest write in bytes including the control byte
|
|
10
|
+
const SPI_CHUNK = 128;
|
|
11
|
+
|
|
12
|
+
/** Page-major monochrome framebuffer (bit 0 of a byte = top pixel of its page) with drawing primitives. */
|
|
13
|
+
class MonoFramebuffer {
|
|
14
|
+
/**
|
|
15
|
+
* @param {number} width - Columns.
|
|
16
|
+
* @param {number} height - Rows, a multiple of 8.
|
|
17
|
+
*/
|
|
18
|
+
constructor(width, height) {
|
|
19
|
+
this.width = width;
|
|
20
|
+
this.height = height;
|
|
21
|
+
this.pages = height / 8;
|
|
22
|
+
this.data = Buffer.alloc(width * this.pages);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Zero the framebuffer. */
|
|
26
|
+
clear() { this.data.fill(0); }
|
|
27
|
+
|
|
28
|
+
/** Set (or clear) every pixel. @param {boolean} [on=true] */
|
|
29
|
+
fill(on = true) { this.data.fill(on ? 0xFF : 0); }
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Set or clear one pixel; out-of-range coordinates are ignored.
|
|
33
|
+
* @param {number} x - Column.
|
|
34
|
+
* @param {number} y - Row.
|
|
35
|
+
* @param {boolean} [on=true]
|
|
36
|
+
*/
|
|
37
|
+
setPixel(x, y, on = true) {
|
|
38
|
+
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return;
|
|
39
|
+
const i = (y >> 3) * this.width + x;
|
|
40
|
+
if (on) this.data[i] |= 1 << (y & 7);
|
|
41
|
+
else this.data[i] &= ~(1 << (y & 7)) & 0xFF;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Return a pixel; out-of-range coordinates read as false.
|
|
46
|
+
* @param {number} x
|
|
47
|
+
* @param {number} y
|
|
48
|
+
* @returns {boolean}
|
|
49
|
+
*/
|
|
50
|
+
getPixel(x, y) {
|
|
51
|
+
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return false;
|
|
52
|
+
return (this.data[(y >> 3) * this.width + x] & (1 << (y & 7))) !== 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Bresenham line. @param {number} x0 @param {number} y0 @param {number} x1 @param {number} y1 @param {boolean} [on=true] */
|
|
56
|
+
drawLine(x0, y0, x1, y1, on = true) {
|
|
57
|
+
const dx = Math.abs(x1 - x0);
|
|
58
|
+
const dy = -Math.abs(y1 - y0);
|
|
59
|
+
const sx = x0 < x1 ? 1 : -1;
|
|
60
|
+
const sy = y0 < y1 ? 1 : -1;
|
|
61
|
+
let err = dx + dy;
|
|
62
|
+
for (;;) {
|
|
63
|
+
this.setPixel(x0, y0, on);
|
|
64
|
+
if (x0 === x1 && y0 === y1) break;
|
|
65
|
+
const e2 = 2 * err;
|
|
66
|
+
if (e2 >= dy) { err += dy; x0 += sx; }
|
|
67
|
+
if (e2 <= dx) { err += dx; y0 += sy; }
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Rectangle outline or filled rectangle.
|
|
73
|
+
* @param {number} x
|
|
74
|
+
* @param {number} y
|
|
75
|
+
* @param {number} w
|
|
76
|
+
* @param {number} h
|
|
77
|
+
* @param {boolean} [on=true]
|
|
78
|
+
* @param {boolean} [fill=false]
|
|
79
|
+
*/
|
|
80
|
+
drawRect(x, y, w, h, on = true, fill = false) {
|
|
81
|
+
if (w <= 0 || h <= 0) return;
|
|
82
|
+
if (fill) {
|
|
83
|
+
for (let yy = y; yy < y + h; yy++) for (let xx = x; xx < x + w; xx++) this.setPixel(xx, yy, on);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
this.drawLine(x, y, x + w - 1, y, on);
|
|
87
|
+
this.drawLine(x, y + h - 1, x + w - 1, y + h - 1, on);
|
|
88
|
+
this.drawLine(x, y, x, y + h - 1, on);
|
|
89
|
+
this.drawLine(x + w - 1, y, x + w - 1, y + h - 1, on);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Copy a column-major bitmap (8 pixels per byte, bit 0 = top); clipped. `columns` holds `heightPages`
|
|
94
|
+
* groups of columns, page by page: byte p * n + c is column c of page row p.
|
|
95
|
+
* @param {number} x
|
|
96
|
+
* @param {number} y - Top row, a multiple of 8.
|
|
97
|
+
* @param {Buffer|number[]} columns
|
|
98
|
+
* @param {number} [heightPages=1]
|
|
99
|
+
* @throws {RangeError} If y is not a multiple of 8.
|
|
100
|
+
*/
|
|
101
|
+
blit(x, y, columns, heightPages = 1) {
|
|
102
|
+
if (y % 8 !== 0) throw new RangeError('y must be a multiple of 8');
|
|
103
|
+
const n = Math.floor(columns.length / heightPages);
|
|
104
|
+
for (let p = 0; p < heightPages; p++) {
|
|
105
|
+
const page = y / 8 + p;
|
|
106
|
+
if (page < 0 || page >= this.pages) continue;
|
|
107
|
+
for (let c = 0; c < n; c++) {
|
|
108
|
+
const xx = x + c;
|
|
109
|
+
if (xx >= 0 && xx < this.width) this.data[page * this.width + xx] = columns[p * n + c];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The bytes of one page. @param {number} page @returns {Buffer} */
|
|
115
|
+
page(page) {
|
|
116
|
+
const start = page * this.width;
|
|
117
|
+
return this.data.subarray(start, start + this.width);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The framebuffer column-major (for vertical addressing). @returns {Buffer} */
|
|
121
|
+
transposed() {
|
|
122
|
+
const out = Buffer.alloc(this.data.length);
|
|
123
|
+
let n = 0;
|
|
124
|
+
for (let x = 0; x < this.width; x++) {
|
|
125
|
+
for (let p = 0; p < this.pages; p++) out[n++] = this.data[p * this.width + x];
|
|
126
|
+
}
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** I²C transport: every transaction starts with a control byte (0x00 = commands, 0x40 = RAM data). */
|
|
132
|
+
class I2cOledBus {
|
|
133
|
+
/** @param {object} connection - I2CConnection at 0x3C or 0x3D. */
|
|
134
|
+
constructor(connection) {
|
|
135
|
+
this._conn = connection;
|
|
136
|
+
this.canRead = true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
writeCommands(bytes) { return this._conn.write(Buffer.concat([Buffer.from([0x00]), Buffer.from(bytes)])); }
|
|
140
|
+
|
|
141
|
+
async writeData(data) {
|
|
142
|
+
const step = I2C_CHUNK - 1;
|
|
143
|
+
for (let i = 0; i < data.length; i += step) {
|
|
144
|
+
await this._conn.write(Buffer.concat([Buffer.from([0x40]), data.subarray(i, i + step)]));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async readStatus() {
|
|
149
|
+
await this._conn.write(Buffer.from([0x00]));
|
|
150
|
+
return Buffer.from(await this._conn.read(1))[0];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async readRam(n) {
|
|
154
|
+
await this._conn.write(Buffer.from([0x40]));
|
|
155
|
+
return Buffer.from(await this._conn.read(n + 1)).subarray(1);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async hardwareReset() {}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** 4-wire SPI transport on a display connection: D/C low for every command byte, high for RAM data. */
|
|
162
|
+
class SpiOledBus {
|
|
163
|
+
/** @param {object} connection - DisplayConnection. */
|
|
164
|
+
constructor(connection) {
|
|
165
|
+
this._conn = connection;
|
|
166
|
+
this.canRead = false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async writeCommands(bytes) {
|
|
170
|
+
for (const b of bytes) await this._conn.sendCommand(b);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async writeData(data) {
|
|
174
|
+
for (let i = 0; i < data.length; i += SPI_CHUNK) {
|
|
175
|
+
await this._conn.sendData(data.subarray(i, i + SPI_CHUNK));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
hardwareReset() { return this._conn.reset(); }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
module.exports = { MonoFramebuffer, I2cOledBus, SpiOledBus, I2C_CHUNK, SPI_CHUNK };
|