LVGL JSON Parser Demo

JSON to C Conversion for LVGL

Original JSON

LVGL UI Simulation

JSON data will appear here after parsing

Python JSON to C Converter

import json
import re

def json_to_c(json_str, var_name="json_data"):
    # Parse JSON string
    data = json.loads(json_str)

    # Generate C code
    c_code = "/* Auto-generated from JSON */\n"
    c_code += "#include <stdint.h>\n"
    c_code += "#include <lvgl.h>\n\n"
    c_code += generate_c_struct(data, var_name, 0)
    return c_code

def generate_c_struct(data, name, indent):
    indent_str = ' ' * indent
    next_indent = indent + 1
    c_code = ""

    if isinstance(data, dict):
        # Handle dictionary
        c_code += indent_str + "typedef struct {\n"
        for key, value in data.items():
            c_code += generate_c_member(key, value, next_indent)
        c_code += indent_str + "}} " + name + "_t;\n\n"
        c_code += indent_str + name + "_t " + name + " = {\n"
        for key, value in data.items():
            c_code += generate_c_value(key, value, next_indent)
        c_code += indent_str + "};\n\n"
    elif isinstance(data, list):
        # Handle array
        c_code += indent_str + "typedef struct {\n"
        for i, item in enumerate(data):
            c_code += generate_c_member("item_" + str(i), item, next_indent)
        c_code += indent_str + "}} " + name + "_t;\n\n"
        c_code += indent_str + name + "_t " + name + "[] = {\n"
        for item in data:
            c_code += generate_c_value("", item, next_indent)
        c_code += indent_str + "};\n\n"
    return c_code

# Additional helper functions would be here...

This Python script converts JSON to C structures that can be used in LVGL. It handles nested objects and arrays, generating appropriate C structs and initialization code.

Generated C Code

/* Auto-generated from JSON */
#include <stdint.h>
#include <lvgl.h>

/* Device structure */
typedef struct {
    char name[16];
    char id[8];
    char firmware[8];
    struct {
        char type[12];
        float value;
        char unit[4];
        struct {
            int min;
            int max;
        } alerts;
    } sensors[2];
    struct {
        char mode[8];
        float target_temp;
        struct {
            char time[6];
            int temp;
        } schedule[3];
    } controls;
} device_t;

/* Initialized device data */
device_t device = {
    .name = "Smart Thermostat",
    .id = "TH-001",
    .firmware = "1.2.3",
    .sensors = {
        {
            .type = "temperature",
            .value = 22.5,
            .unit = "°C",
            .alerts = {15, 30}
        },
        {
            .type = "humidity",
            .value = 45,
            .unit = "%",
            .alerts = {30, 70}
        }
    },
    .controls = {
        .mode = "auto",
        .target_temp = 21.5,
        .schedule = {
            {"08:00", 21},
            {"18:00", 22},
            {"22:00", 19}
        }
    }
};

This C code is generated from the JSON input. It defines a structure that matches the JSON hierarchy and initializes it with the JSON values.

LVGL UI Code

/* Create device info label */
lv_obj_t *device_label = lv_label_create(lv_scr_act());
lv_label_set_text_fmt(device_label, "%s (ID: %s)\nFirmware: %s",
    device.name, device.id, device.firmware);
lv_obj_align(device_label, LV_ALIGN_TOP_LEFT, 10, 10);

/* Create sensor readings */
lv_obj_t *sensor_container = lv_obj_create(lv_scr_act());
lv_obj_set_size(sensor_container, 150, 100);
lv_obj_align(sensor_container, LV_ALIGN_TOP_RIGHT, -10, 10);
lv_obj_set_flex_flow(sensor_container, LV_FLEX_FLOW_COLUMN);

for(int i = 0; i < 2; i++) {
    lv_obj_t *sensor_label = lv_label_create(sensor_container);
    lv_label_set_text_fmt(sensor_label, "%s: %.1f%s",
        device.sensors[i].type, device.sensors[i].value, device.sensors[i].unit);
}

/* Create controls panel */
lv_obj_t *controls_panel = lv_obj_create(lv_scr_act());
lv_obj_set_size(controls_panel, 300, 150);
lv_obj_align(controls_panel, LV_ALIGN_BOTTOM_LEFT, 10, -10);
lv_obj_set_flex_flow(controls_panel, LV_FLEX_FLOW_COLUMN);

/* Add mode and target temp */
lv_obj_t *mode_label = lv_label_create(controls_panel);
lv_label_set_text_fmt(mode_label, "Mode: %s", device.controls.mode);

lv_obj_t *temp_label = lv_label_create(controls_panel);
lv_label_set_text_fmt(temp_label, "Target: %.1f°C", device.controls.target_temp);

/* Add schedule */
lv_obj_t *schedule_label = lv_label_create(controls_panel);
lv_label_set_text(schedule_label, "Schedule:");

lv_obj_t *schedule_list = lv_list_create(controls_panel);
lv_obj_set_size(schedule_list, 280, 80);

for(int i = 0; i < 3; i++) {
    lv_obj_t *item = lv_list_add_btn(schedule_list, NULL, "%s: %d°C",
        device.controls.schedule[i].time, device.controls.schedule[i].temp);
    lv_obj_add_flag(item, LV_OBJ_FLAG_EVENT_BUBBLE);
}

This LVGL code creates a UI that displays the device information, sensor readings, and control settings from the converted C structure.

Made with DeepSite LogoDeepSite - 🧬 Remix