Skip to content

Instantly share code, notes, and snippets.

@julr

julr/main.c Secret

Created October 9, 2019 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julr/0767ce8c0c1067e696da751aa7afa58b to your computer and use it in GitHub Desktop.
Save julr/0767ce8c0c1067e696da751aa7afa58b to your computer and use it in GitHub Desktop.
LittleGL tabview navigation problem
/**
* @file main
*
*/
/*********************
* INCLUDES
*********************/
#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
#include <SDL.h>
#include "lvgl/lvgl.h"
#include "lv_drivers/display/monitor.h"
#include "lv_drivers/indev/mouse.h"
#include "lv_drivers/indev/mousewheel.h"
#include "lv_drivers/indev/keyboard.h"
#include "lv_examples/lv_apps/demo/demo.h"
#include "lv_examples/lv_apps/benchmark/benchmark.h"
#include "lv_examples/lv_tests/lv_test_theme/lv_test_theme_1.h"
#include "lv_examples/lv_tutorial/10_keyboard/lv_tutorial_keyboard.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void hal_init(void);
static int tick_thread(void *data);
static void event_handler(lv_obj_t* obj, lv_event_t event);
/**********************
* STATIC VARIABLES
**********************/
static lv_indev_t * kb_indev;
static lv_indev_t* mw_indev;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
int main(int argc, char** argv)
{
/*Initialize LittlevGL*/
lv_init();
/*Initialize the HAL for LittlevGL*/
hal_init();
/*
* Demo, benchmark, tests and tutorial.
*
* Uncomment any one (and only one) of the functions below to run that
* particular demo, test or tutorial.
*/
//demo_create();
//benchmark_create();
//lv_test_theme_1(lv_theme_night_init(210, NULL));
//lv_test_theme_1(lv_theme_night_init(100, NULL));
//lv_test_theme_1(lv_theme_material_init(210, NULL));
//lv_test_theme_1(lv_theme_alien_init(210, NULL));
//lv_test_theme_1(lv_theme_zen_init(210, NULL));
//lv_test_theme_1(lv_theme_nemo_init(210, NULL));
//lv_test_theme_1(lv_theme_mono_init(210, NULL));
//lv_test_theme_1(lv_theme_default_init(210, NULL));
//lv_tutorial_keyboard(kb_indev);
lv_obj_t* tabview;
tabview = lv_tabview_create(lv_scr_act(), NULL);
/*Add 3 tabs (the tabs are page (lv_page) and can be scrolled*/
lv_obj_t* tab1 = lv_tabview_add_tab(tabview, "Tab 1");
lv_obj_t* tab2 = lv_tabview_add_tab(tabview, "Tab 2");
lv_obj_t* tab3 = lv_tabview_add_tab(tabview, "Tab 3");
/*Add content to the tabs*/
lv_obj_t* btn1 = lv_btn_create(tab1, NULL);
lv_obj_set_event_cb(btn1, event_handler);
lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, 0);
lv_obj_t* label = lv_label_create(btn1, NULL);
lv_label_set_text(label, "Button");
label = lv_label_create(tab2, NULL);
lv_label_set_text(label, "Second tab");
label = lv_label_create(tab3, NULL);
lv_label_set_text(label, "Third tab");
/*Apply a group*/
lv_group_t* g = lv_group_create();
lv_group_add_obj(g, tabview);
lv_group_add_obj(g, btn1);
lv_indev_set_group(mw_indev, g);
while (1) {
/* Periodically call the lv_task handler.
* It could be done in a timer interrupt or an OS task too.*/
lv_task_handler();
Sleep(10); /*Just to let the system breathe */
}
return 0;
}
/**********************
* STATIC FUNCTIONS
**********************/
static void event_handler(lv_obj_t* obj, lv_event_t event)
{
if (event == LV_EVENT_CLICKED) {
printf("Clicked\n");
}
else if (event == LV_EVENT_VALUE_CHANGED) {
printf("Toggled\n");
}
}
/**
* Initialize the Hardware Abstraction Layer (HAL) for the Littlev graphics library
*/
static void hal_init(void)
{
/* Add a display
* Use the 'monitor' driver which creates window on PC's monitor to simulate a display*/
monitor_init();
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv); /*Basic initialization*/
static lv_disp_buf_t disp_buf1;
static lv_color_t buf1_1[LV_HOR_RES_MAX*LV_VER_RES_MAX];
lv_disp_buf_init(&disp_buf1, buf1_1, NULL, LV_HOR_RES_MAX*LV_VER_RES_MAX);
disp_drv.buffer = &disp_buf1;
disp_drv.flush_cb = monitor_flush;
lv_disp_drv_register(&disp_drv);
/* Add the mouse (or touchpad) as input device
* Use the 'mouse' driver which reads the PC's mouse*/
//#ifdef USE_MOUSE
// mouse_init();
// lv_indev_drv_t indev_drv;
// lv_indev_drv_init(&indev_drv); /*Basic initialization*/
// indev_drv.type = LV_INDEV_TYPE_POINTER;
// indev_drv.read_cb = mouse_read; /*This function will be called periodically (by the library) to get the mouse position and state*/
// lv_indev_drv_register(&indev_drv);
//#endif
#ifdef USE_MOUSEWHEEL
lv_indev_drv_t mw_drv;
lv_indev_drv_init(&mw_drv);
mw_drv.type = LV_INDEV_TYPE_ENCODER;
mw_drv.read_cb = mousewheel_read;
mw_indev = lv_indev_drv_register(&mw_drv);
#endif
/* If the PC keyboard driver is enabled in`lv_drv_conf.h`
* add this as an input device. It might be used in some examples. */
//#if USE_KEYBOARD
// lv_indev_drv_t kb_drv;
// lv_indev_drv_init(&kb_drv);
// kb_drv.type = LV_INDEV_TYPE_KEYPAD;
// kb_drv.read_cb = keyboard_read;
// kb_indev = lv_indev_drv_register(&kb_drv);
//#endif
/* Tick init.
* You have to call 'lv_tick_inc()' in every milliseconds
* Create an SDL thread to do this*/
SDL_CreateThread(tick_thread, "tick", NULL);
}
/**
* A task to measure the elapsed time for LittlevGL
* @param data unused
* @return never return
*/
static int tick_thread(void *data)
{
while (1) {
lv_tick_inc(5);
SDL_Delay(5); /*Sleep for 1 millisecond*/
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment