MCPcopy Index your code
hub / github.com/cifertech/DisplayKit / generateTFTCode

Function generateTFTCode

app.js:5224–5590  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5222}
5223
5224function generateTFTCode() {
5225 let code = "";
5226 code += "#include <TFT_eSPI.h>\n";
5227 code += "#include <SPI.h>\n\n";
5228 code += "// --- Generated by DisplayKit ---\n";
5229 code += "// Notes:\n";
5230 code += "// - This is a COMPLETE sketch skeleton (setup/loop + screen functions).\n";
5231 code += "// - Touch/button interaction is not wired automatically; see `handleUiActions()`.\n";
5232 code += "// - Element rotation is a preview-only feature; TFT_eSPI primitives are not rotated.\n\n";
5233
5234 // Collect any GFX FreeFonts used by elements so we can emit the required includes.
5235 const usedFreeFonts = new Set();
5236 screens.forEach((scr) => {
5237 (scr.elements || []).forEach((el) => {
5238 if (!elementHasText(el.type)) return;
5239 const font = getElementFontForMode(el, "tft");
5240 if (!/^\d+$/.test(String(font))) usedFreeFonts.add(String(font));
5241 });
5242 });
5243 if (usedFreeFonts.size) {
5244 code += "// NOTE: To use FreeFonts, enable LOAD_GFXFF in TFT_eSPI/User_Setup.h\n";
5245 usedFreeFonts.forEach((name) => {
5246 code += `#include <Fonts/${name}.h>\n`;
5247 });
5248 code += "\n";
5249 }
5250
5251 code += "TFT_eSPI tft = TFT_eSPI();\n";
5252 if (useSprite) {
5253 code += "TFT_eSprite spr = TFT_eSprite(&tft);\n";
5254 }
5255 code += "\n";
5256
5257 const bg565 = hexToRgb565(bgColor);
5258 code += `static const uint16_t DK_BG = ${bg565};\n\n`;
5259
5260 // Screen IDs -> indices for basic navigation stubs.
5261 const screenIndex = new Map();
5262 screens.forEach((s, i) => screenIndex.set(s.id, i));
5263 const defaultScreen = screens.find((s) => s.id === activeScreenId) || screens[0];
5264 const defaultIndex = defaultScreen ? (screenIndex.get(defaultScreen.id) ?? 0) : 0;
5265 code += `enum DkScreenId { ${screens.map((s, i) => `DK_SCREEN_${i}`).join(", ")} };\n`;
5266 code += `static DkScreenId g_screen = DK_SCREEN_${defaultIndex};\n`;
5267 code += "static bool g_dirty = true;\n";
5268 code += "static uint32_t g_lastDrawMs = 0;\n\n";
5269
5270
5271 const imageElements = [];
5272 screens.forEach((scr) => {
5273 scr.elements.forEach((el) => {
5274 if (el.type !== "image" && el.type !== "icon") return;
5275 const rgb565 = getRgb565ForElement(el);
5276 const dims = getImageDimsForElement(el);
5277 if (rgb565 && dims && dims.w && dims.h) {
5278 imageElements.push(el);
5279 }
5280 });
5281 });

Callers 1

updateCodeFunction · 0.85

Calls 6

elementHasTextFunction · 0.85
getElementFontForModeFunction · 0.85
hexToRgb565Function · 0.85
getRgb565ForElementFunction · 0.85
getImageDimsForElementFunction · 0.85
getScreenFnNameFunction · 0.85

Tested by

no test coverage detected