Spaces:
Running
Running
Download index.html from toolmanduck/json-lvgl: direct link, hf CLI and curl.
- Browser
- Download file 17.6 kB
-
https://huggingface.co/spaces/toolmanduck/json-lvgl/resolve/main/index.html
- Command line
-
hf download hf://spaces/toolmanduck/json-lvgl/index.html
-
curl -L -o index.html https://huggingface.co/spaces/toolmanduck/json-lvgl/resolve/main/index.html
17.6 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>JSON to LVGL Converter</title> | |
| <script src="https://cdn.tailwindcss.com"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js"></script> | |
| <style> | |
| #jsonEditor, #lvglCode { | |
| height: 300px; | |
| width: 100%; | |
| } | |
| .lvgl-preview { | |
| background-color: #f0f0f0; | |
| border-radius: 8px; | |
| padding: 20px; | |
| min-height: 300px; | |
| position: relative; | |
| } | |
| .lvgl-object { | |
| border: 1px dashed #ccc; | |
| margin: 5px; | |
| padding: 10px; | |
| position: relative; | |
| background-color: white; | |
| } | |
| .lvgl-label { | |
| color: #333; | |
| font-family: Arial, sans-serif; | |
| font-size: 14px; | |
| } | |
| .lvgl-btn { | |
| background-color: #4CAF50; | |
| color: white; | |
| border: none; | |
| padding: 8px 16px; | |
| text-align: center; | |
| text-decoration: none; | |
| display: inline-block; | |
| font-size: 14px; | |
| margin: 4px 2px; | |
| cursor: pointer; | |
| border-radius: 4px; | |
| } | |
| .lvgl-slider { | |
| width: 100%; | |
| height: 10px; | |
| background-color: #ddd; | |
| margin: 15px 0; | |
| position: relative; | |
| } | |
| .lvgl-slider-thumb { | |
| width: 20px; | |
| height: 20px; | |
| background-color: #4CAF50; | |
| border-radius: 50%; | |
| position: absolute; | |
| top: -5px; | |
| left: 50%; | |
| } | |
| .lvgl-switch { | |
| width: 50px; | |
| height: 24px; | |
| background-color: #ccc; | |
| border-radius: 12px; | |
| position: relative; | |
| display: inline-block; | |
| } | |
| .lvgl-switch-knob { | |
| width: 20px; | |
| height: 20px; | |
| background-color: white; | |
| border-radius: 50%; | |
| position: absolute; | |
| top: 2px; | |
| left: 2px; | |
| transition: 0.3s; | |
| } | |
| .lvgl-switch.on { | |
| background-color: #4CAF50; | |
| } | |
| .lvgl-switch.on .lvgl-switch-knob { | |
| left: 28px; | |
| } | |
| .lvgl-dropdown { | |
| border: 1px solid #ccc; | |
| padding: 8px; | |
| width: 150px; | |
| position: relative; | |
| background-color: white; | |
| } | |
| .lvgl-dropdown:after { | |
| content: "▼"; | |
| position: absolute; | |
| right: 10px; | |
| top: 8px; | |
| font-size: 10px; | |
| } | |
| </style> | |
| </head> | |
| <body class="bg-gray-100"> | |
| <div class="container mx-auto px-4 py-8"> | |
| <h1 class="text-3xl font-bold text-center mb-8 text-blue-700">JSON to LVGL Converter</h1> | |
| <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6"> | |
| <div class="bg-white rounded-lg shadow-md p-4"> | |
| <h2 class="text-xl font-semibold mb-4 text-gray-800">JSON Input</h2> | |
| <div id="jsonEditor">{ | |
| "screen": { | |
| "width": 320, | |
| "height": 240, | |
| "bg_color": "#F0F0F0", | |
| "objects": [ | |
| { | |
| "type": "label", | |
| "x": 20, | |
| "y": 20, | |
| "text": "Welcome to LVGL", | |
| "color": "#000000", | |
| "font_size": 16 | |
| }, | |
| { | |
| "type": "button", | |
| "x": 20, | |
| "y": 60, | |
| "width": 100, | |
| "height": 40, | |
| "text": "Click Me", | |
| "bg_color": "#4CAF50", | |
| "text_color": "#FFFFFF" | |
| }, | |
| { | |
| "type": "slider", | |
| "x": 20, | |
| "y": 120, | |
| "width": 200, | |
| "min": 0, | |
| "max": 100, | |
| "value": 50 | |
| }, | |
| { | |
| "type": "switch", | |
| "x": 20, | |
| "y": 160, | |
| "state": false | |
| }, | |
| { | |
| "type": "dropdown", | |
| "x": 150, | |
| "y": 60, | |
| "width": 120, | |
| "options": ["Option 1", "Option 2", "Option 3"], | |
| "selected": 0 | |
| } | |
| ] | |
| } | |
| }</div> | |
| <button id="convertBtn" class="mt-4 bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"> | |
| Convert to LVGL | |
| </button> | |
| </div> | |
| <div class="bg-white rounded-lg shadow-md p-4"> | |
| <h2 class="text-xl font-semibold mb-4 text-gray-800">LVGL Code Output</h2> | |
| <div id="lvglCode"></div> | |
| </div> | |
| </div> | |
| <div class="bg-white rounded-lg shadow-md p-4"> | |
| <h2 class="text-xl font-semibold mb-4 text-gray-800">LVGL UI Preview</h2> | |
| <div id="lvglPreview" class="lvgl-preview"> | |
| <p class="text-gray-500">Preview will appear here after conversion</p> | |
| </div> | |
| </div> | |
| </div> | |
| <script> | |
| // Initialize ACE editors | |
| const jsonEditor = ace.edit("jsonEditor"); | |
| jsonEditor.setTheme("ace/theme/chrome"); | |
| jsonEditor.session.setMode("ace/mode/json"); | |
| jsonEditor.setOptions({ | |
| fontSize: "14px", | |
| showLineNumbers: false, | |
| showGutter: false, | |
| highlightActiveLine: false | |
| }); | |
| const lvglCodeEditor = ace.edit("lvglCode"); | |
| lvglCodeEditor.setTheme("ace/theme/chrome"); | |
| lvglCodeEditor.session.setMode("ace/mode/c_cpp"); | |
| lvglCodeEditor.setOptions({ | |
| fontSize: "14px", | |
| readOnly: true, | |
| highlightActiveLine: false | |
| }); | |
| lvglCodeEditor.renderer.setShowGutter(false); | |
| // Conversion function | |
| document.getElementById('convertBtn').addEventListener('click', function() { | |
| try { | |
| const jsonInput = jsonEditor.getValue(); | |
| const jsonData = JSON.parse(jsonInput); | |
| // Generate LVGL code | |
| const lvglCode = generateLvglCode(jsonData); | |
| lvglCodeEditor.setValue(lvglCode); | |
| // Generate preview | |
| generatePreview(jsonData); | |
| } catch (error) { | |
| alert('Error parsing JSON: ' + error.message); | |
| console.error(error); | |
| } | |
| }); | |
| function generateLvglCode(jsonData) { | |
| let code = `#include "lvgl.h"\n\n`; | |
| if (jsonData.screen) { | |
| const screen = jsonData.screen; | |
| code += `void create_ui() {\n`; | |
| code += ` lv_obj_t *screen = lv_scr_act();\n`; | |
| code += ` lv_obj_set_size(screen, ${screen.width || 320}, ${screen.height || 240});\n`; | |
| if (screen.bg_color) { | |
| code += ` lv_obj_set_style_bg_color(screen, lv_color_hex(${hexToLvglColor(screen.bg_color)}), LV_PART_MAIN);\n`; | |
| } | |
| code += `\n`; | |
| if (screen.objects && Array.isArray(screen.objects)) { | |
| screen.objects.forEach((obj, index) => { | |
| code += generateObjectCode(obj, index); | |
| }); | |
| } | |
| code += `}\n`; | |
| } | |
| return code; | |
| } | |
| function generateObjectCode(obj, index) { | |
| let code = ''; | |
| const varName = `${obj.type}_${index}`; | |
| switch (obj.type) { | |
| case 'label': | |
| code += ` lv_obj_t *${varName} = lv_label_create(screen);\n`; | |
| code += ` lv_label_set_text(${varName}, "${obj.text || ''}");\n`; | |
| code += ` lv_obj_set_pos(${varName}, ${obj.x || 0}, ${obj.y || 0});\n`; | |
| if (obj.color) { | |
| code += ` lv_obj_set_style_text_color(${varName}, lv_color_hex(${hexToLvglColor(obj.color)}), LV_PART_MAIN);\n`; | |
| } | |
| if (obj.font_size) { | |
| code += ` lv_obj_set_style_text_font(${varName}, &lv_font_montserrat_${obj.font_size}, LV_PART_MAIN);\n`; | |
| } | |
| break; | |
| case 'button': | |
| code += ` lv_obj_t *${varName} = lv_btn_create(screen);\n`; | |
| code += ` lv_obj_set_size(${varName}, ${obj.width || 100}, ${obj.height || 40});\n`; | |
| code += ` lv_obj_set_pos(${varName}, ${obj.x || 0}, ${obj.y || 0});\n`; | |
| if (obj.bg_color) { | |
| code += ` lv_obj_set_style_bg_color(${varName}, lv_color_hex(${hexToLvglColor(obj.bg_color)}), LV_PART_MAIN);\n`; | |
| } | |
| code += ` lv_obj_t *${varName}_label = lv_label_create(${varName});\n`; | |
| code += ` lv_label_set_text(${varName}_label, "${obj.text || 'Button'}");\n`; | |
| code += ` lv_obj_center(${varName}_label);\n`; | |
| if (obj.text_color) { | |
| code += ` lv_obj_set_style_text_color(${varName}_label, lv_color_hex(${hexToLvglColor(obj.text_color)}), LV_PART_MAIN);\n`; | |
| } | |
| break; | |
| case 'slider': | |
| code += ` lv_obj_t *${varName} = lv_slider_create(screen);\n`; | |
| code += ` lv_obj_set_size(${varName}, ${obj.width || 200}, 20);\n`; | |
| code += ` lv_obj_set_pos(${varName}, ${obj.x || 0}, ${obj.y || 0});\n`; | |
| code += ` lv_slider_set_range(${varName}, ${obj.min || 0}, ${obj.max || 100});\n`; | |
| code += ` lv_slider_set_value(${varName}, ${obj.value || 50}, LV_ANIM_OFF);\n`; | |
| break; | |
| case 'switch': | |
| code += ` lv_obj_t *${varName} = lv_switch_create(screen);\n`; | |
| code += ` lv_obj_set_pos(${varName}, ${obj.x || 0}, ${obj.y || 0});\n`; | |
| if (obj.state) { | |
| code += ` lv_obj_add_state(${varName}, LV_STATE_CHECKED);\n`; | |
| } | |
| break; | |
| case 'dropdown': | |
| code += ` lv_obj_t *${varName} = lv_dropdown_create(screen);\n`; | |
| code += ` lv_obj_set_size(${varName}, ${obj.width || 120}, 30);\n`; | |
| code += ` lv_obj_set_pos(${varName}, ${obj.x || 0}, ${obj.y || 0});\n`; | |
| if (obj.options && Array.isArray(obj.options)) { | |
| const optionsStr = obj.options.map(opt => opt.replace(/"/g, '\\"')).join('\\n'); | |
| code += ` lv_dropdown_set_options(${varName}, "${optionsStr}");\n`; | |
| } | |
| if (obj.selected !== undefined) { | |
| code += ` lv_dropdown_set_selected(${varName}, ${obj.selected});\n`; | |
| } | |
| break; | |
| default: | |
| code += ` // Unsupported object type: ${obj.type}\n`; | |
| } | |
| code += `\n`; | |
| return code; | |
| } | |
| function hexToLvglColor(hex) { | |
| // Remove # if present | |
| hex = hex.replace('#', ''); | |
| // Convert 3-digit hex to 6-digit | |
| if (hex.length === 3) { | |
| hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]; | |
| } | |
| // Convert to 0xRRGGBB format | |
| return `0x${hex}`; | |
| } | |
| function generatePreview(jsonData) { | |
| const previewDiv = document.getElementById('lvglPreview'); | |
| previewDiv.innerHTML = ''; | |
| if (jsonData.screen) { | |
| const screen = jsonData.screen; | |
| previewDiv.style.width = `${screen.width || 320}px`; | |
| previewDiv.style.height = `${screen.height || 240}px`; | |
| previewDiv.style.backgroundColor = screen.bg_color || '#F0F0F0'; | |
| if (screen.objects && Array.isArray(screen.objects)) { | |
| screen.objects.forEach((obj, index) => { | |
| const element = createPreviewElement(obj); | |
| if (element) { | |
| previewDiv.appendChild(element); | |
| } | |
| }); | |
| } | |
| } | |
| } | |
| function createPreviewElement(obj) { | |
| const element = document.createElement('div'); | |
| element.className = 'lvgl-object'; | |
| element.style.position = 'absolute'; | |
| element.style.left = `${obj.x || 0}px`; | |
| element.style.top = `${obj.y || 0}px`; | |
| switch (obj.type) { | |
| case 'label': | |
| element.className += ' lvgl-label'; | |
| element.style.fontSize = `${obj.font_size || 14}px`; | |
| element.style.color = obj.color || '#000000'; | |
| element.textContent = obj.text || 'Label'; | |
| break; | |
| case 'button': | |
| element.className += ' lvgl-btn'; | |
| element.style.width = `${obj.width || 100}px`; | |
| element.style.height = `${obj.height || 40}px`; | |
| element.style.backgroundColor = obj.bg_color || '#4CAF50'; | |
| element.style.color = obj.text_color || '#FFFFFF'; | |
| element.style.display = 'flex'; | |
| element.style.justifyContent = 'center'; | |
| element.style.alignItems = 'center'; | |
| element.textContent = obj.text || 'Button'; | |
| break; | |
| case 'slider': | |
| element.className += ' lvgl-slider-container'; | |
| element.style.width = `${obj.width || 200}px`; | |
| const sliderTrack = document.createElement('div'); | |
| sliderTrack.className = 'lvgl-slider'; | |
| const sliderThumb = document.createElement('div'); | |
| sliderThumb.className = 'lvgl-slider-thumb'; | |
| const value = obj.value || 50; | |
| const min = obj.min || 0; | |
| const max = obj.max || 100; | |
| const percentage = ((value - min) / (max - min)) * 100; | |
| sliderThumb.style.left = `calc(${percentage}% - 10px)`; | |
| sliderTrack.appendChild(sliderThumb); | |
| element.appendChild(sliderTrack); | |
| break; | |
| case 'switch': | |
| element.className += ' lvgl-switch-container'; | |
| const switchElement = document.createElement('div'); | |
| switchElement.className = 'lvgl-switch'; | |
| if (obj.state) { | |
| switchElement.classList.add('on'); | |
| } | |
| const switchKnob = document.createElement('div'); | |
| switchKnob.className = 'lvgl-switch-knob'; | |
| switchElement.appendChild(switchKnob); | |
| element.appendChild(switchElement); | |
| // Add click handler to toggle | |
| switchElement.addEventListener('click', function() { | |
| this.classList.toggle('on'); | |
| }); | |
| break; | |
| case 'dropdown': | |
| element.className += ' lvgl-dropdown-container'; | |
| element.style.width = `${obj.width || 120}px`; | |
| const dropdown = document.createElement('div'); | |
| dropdown.className = 'lvgl-dropdown'; | |
| const selectedIndex = obj.selected || 0; | |
| const options = obj.options || ['Option 1', 'Option 2', 'Option 3']; | |
| dropdown.textContent = options[selectedIndex]; | |
| element.appendChild(dropdown); | |
| break; | |
| default: | |
| return null; | |
| } | |
| return element; | |
| } | |
| </script> | |
| <p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=toolmanduck/json-lvgl" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body> | |
| </html> |