-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
tft-espi.ts
73 lines (69 loc) · 3 KB
/
tft-espi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { getLayerProperties } from "../core/decorators/mapping";
import {AbstractImageLayer} from '../core/layers/abstract-image.layer';
import {AbstractLayer} from '../core/layers/abstract.layer';
import {fontTypes} from '../draw/fonts/fontTypes';
import {imgDataToXBMP, packedHexColor565, toCppVariableName} from '../utils';
import {Platform} from './platform';
import defaultTemplate from './templates/tft-espi/default.pug';
export class TFTeSPIPlatform extends Platform {
public static id = 'tft-espi';
protected name = 'TFT_eSPI';
protected description = 'TFT_eSPI';
protected fonts: TPlatformFont[] = [fontTypes['adafruit']];
constructor() {
super();
this.features.hasCustomFontSize = true;
this.features.hasRGBSupport = true;
this.features.defaultColor = '#FFFFFF';
}
protected templates = {
Default: {
template: defaultTemplate,
settings: {
wrap: false
}
}
};
generateSourceCode(layers: AbstractLayer[], ctx?: OffscreenCanvasRenderingContext2D): string {
const declarations: {type: string; data: any}[] = [];
const xbmps = [];
const xbmpsNames = [];
const layerData = layers
.sort((a: AbstractLayer, b: AbstractLayer) => a.index - b.index)
.map((layer) => {
const props = getLayerProperties(layer);
if (layer instanceof AbstractImageLayer) {
const XBMP = imgDataToXBMP(layer.data, 0, 0, layer.size.x, layer.size.y, true).join(',');
if (xbmps.includes(XBMP)) {
props.imageName = xbmpsNames[xbmps.indexOf(XBMP)];
} else {
const name = layer.imageName ? toCppVariableName(layer.imageName) : 'paint';
const nameRegexp = new RegExp(`${name}_?\d*`);
const countWithSameName = xbmpsNames.filter((n) => nameRegexp.test(n)).length;
const varName = `image_${name + (countWithSameName || name == 'paint' ? `_${countWithSameName}` : '')}_bits`;
declarations.push({
type: 'bitmap',
data: {
name: varName,
value: XBMP
}
});
xbmps.push(XBMP);
xbmpsNames.push(varName);
props.imageName = varName;
}
}
return props;
});
const source = this.templates[this.currentTemplate].template({
declarations,
layers: layerData,
settings: Object.assign({}, this.settings, this.templates[this.currentTemplate].settings),
packColor: (color) => this.packColor(color)
});
return source;
}
protected packColor(color: string): string {
return packedHexColor565(color);
}
}