lvgl示例程序自动提取及播放

lvgl内置了大量示例程序,但是模拟器每次只能演示一个示例。

通过脚本提取示例入口函数表。

在main.c中增加一个按钮,每点击一次,找回一个示例程序运行。

提取脚本


import os

def findAllFile(base):
    for root, ds, fs in os.walk(base):
        for f in fs:
            if f.endswith('.c')  and f.startswith("lv_example_"):
                fullname = os.path.join(root, f)
                # yield fullname
                func = f[:-2]
                func = '{' + func + ', "' + f[:-2] + '"}' +','
                yield func

def main():
    base = '.'
    print('#include "lv_examples.h"')
    print('')
    print('typedef struct funcname')
    print('{')
    print('    ex_func func;')
    print('    const char *name;')
    print('}func_info;')
    print('static func_info funcs[] = {')

    for i in findAllFile(base):
        print(i)

    print('};')
    print('static int _index = 0;')
    print('#define max_index sizeof(funcs) / sizeof(ex_func)')
    print('ex_func lv_get_next_ex(const char** pname)')
    print('{')
    print('    if (_index >= max_index)')
    print('        _index = 0;')
    print('    *pname = funcs[_index].name;')
    print('    return funcs[_index++].func;')
    print('}')
    print('')


if __name__ == '__main__':
    main()

main.c

static void lv_next_fun(lv_event_t * e)
{
  (void)e;
  lv_obj_t *obj;
  int child_sum;
  ex_func example;
  char *funcname = "";

  obj = lv_scr_act();
  lv_obj_clean(obj);
  lv_obj_remove_style_all(obj);

  example = lv_get_next_ex(&funcname);
  example();

  func_name_label = lv_label_create(lv_scr_act());
  lv_obj_set_style_bg_opa(func_name_label, LV_OPA_50, 0);
  lv_obj_set_style_bg_color(func_name_label, lv_color_black(), 0);
  lv_obj_set_style_text_color(func_name_label, lv_color_white(), 0);
  lv_obj_set_style_pad_top(func_name_label, 3, 0);
  lv_obj_set_style_pad_bottom(func_name_label, 3, 0);
  lv_obj_set_style_pad_left(func_name_label, 3, 0);
  lv_obj_set_style_pad_right(func_name_label, 3, 0);
  lv_obj_set_style_text_align(func_name_label, LV_TEXT_ALIGN_RIGHT, 0);
  lv_label_set_text(func_name_label, "?");
  lv_obj_align(func_name_label, LV_ALIGN_BOTTOM_MID, 0, 0);
  lv_obj_add_flag(func_name_label, LV_OBJ_FLAG_CLICKABLE);

  lv_obj_add_event_cb(func_name_label, lv_next_fun, LV_EVENT_CLICKED, NULL);
  lv_label_set_text_fmt(func_name_label, "ex: %s", funcname);

  printf("play ex: %s\n", funcname);
}

你可能感兴趣的:(lvgl)