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

Function generateU8g2Code

app.js:5592–5941  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5590}
5591
5592function generateU8g2Code() {
5593 let code = "";
5594 code += "#include <U8g2lib.h>\n";
5595 code += "#include <Wire.h>\n\n";
5596 code += "// --- Generated by DisplayKit ---\n";
5597 code += "// Notes:\n";
5598 code += "// - This is a COMPLETE sketch skeleton (setup/loop + screen functions).\n";
5599 code += "// - Button/touch interaction is not wired automatically; see `handleUiActions()`.\n";
5600 code += "// - Element rotation is a preview-only feature; U8g2 primitives are not rotated.\n\n";
5601
5602 const preset = U8G2_PRESETS.find(p => p.id === u8g2PresetId) || U8G2_PRESETS[0];
5603 if (preset.id === "custom") {
5604 code += preset.ctor + "\n\n";
5605 } else {
5606 code += preset.ctor + "\n\n";
5607 }
5608
5609 // Screen IDs -> indices for basic navigation stubs.
5610 const screenIndex = new Map();
5611 screens.forEach((s, i) => screenIndex.set(s.id, i));
5612 const defaultScreen = screens.find((s) => s.id === activeScreenId) || screens[0];
5613 const defaultIndex = defaultScreen ? (screenIndex.get(defaultScreen.id) ?? 0) : 0;
5614 code += `enum DkScreenId { ${screens.map((s, i) => `DK_SCREEN_${i}`).join(", ")} };\n`;
5615 code += `static DkScreenId g_screen = DK_SCREEN_${defaultIndex};\n`;
5616 code += "static bool g_dirty = true;\n";
5617 code += "static uint32_t g_lastDrawMs = 0;\n\n";
5618
5619 // Monochrome bitmaps for icon elements (XBMP)
5620 const monoIcons = [];
5621 screens.forEach((scr) => {
5622 scr.elements.forEach((el) => {
5623 if (el.type !== "icon") return;
5624 const bytes = getMonoBitmapForElement(el);
5625 const dims = getImageDimsForElement(el);
5626 if (bytes && bytes.length && dims && dims.w && dims.h) monoIcons.push(el);
5627 });
5628 });
5629
5630 monoIcons.forEach((el) => {
5631 const name = (el.imageName || "icon_" + el.id.replace(/[^a-zA-Z0-9_]/g, "_")) + "_xbm";
5632 const bytes = getMonoBitmapForElement(el) || [];
5633 code += `const unsigned char ${name}[${bytes.length}] PROGMEM = {\n`;
5634 for (let i = 0; i < bytes.length; i++) {
5635 const v = bytes[i] & 0xff;
5636 const hx = "0x" + v.toString(16).padStart(2, "0").toUpperCase();
5637 const isLast = i === bytes.length - 1;
5638 if (i % 16 === 0) code += " ";
5639 code += hx;
5640 code += isLast ? "" : ", ";
5641 if (i % 16 === 15 || isLast) code += "\n";
5642 }
5643 code += "};\n\n";
5644 });
5645
5646 screens.forEach((scr) => {
5647 const fnName = getScreenFnName(scr);
5648 code += `void ${fnName}() {\n`;
5649 code += " u8g2.clearBuffer();\n";

Callers 1

updateCodeFunction · 0.85

Calls 3

getMonoBitmapForElementFunction · 0.85
getImageDimsForElementFunction · 0.85
getScreenFnNameFunction · 0.85

Tested by

no test coverage detected