Skip to content

Fix lvgl crash on startup with ArduinoCore-mbed >= 2.7.2 #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/Braccio_Basic/Braccio_Basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ bool move_joint = false;

static void event_handler(lv_event_t * e)
{
Braccio.lvgl_lock();
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if (code == LV_EVENT_CLICKED)
Expand All @@ -46,15 +47,19 @@ static void event_handler(lv_event_t * e)
if (strcmp(txt, "Move") == 0)
move_joint = !move_joint;
}
Braccio.lvgl_unlock();
}

void customMenu()
{
Braccio.lvgl_lock();
lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act());
lv_btnmatrix_set_map(btnm1, btnm_map);
lv_btnmatrix_set_btn_ctrl(btnm1, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
lv_obj_align(btnm1, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_event_cb(btnm1, event_handler, LV_EVENT_ALL, NULL);
Braccio.lvgl_unlock();

Braccio.connectJoystickTo(btnm1);
}

Expand Down
5 changes: 4 additions & 1 deletion examples/LCD_Custom_Menu/LCD_Custom_Menu.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ static const char * btnm_map[] = {"Option 1", "\n",
* FUNCTIONS
**************************************************************************************/

void customMenu(){
void customMenu()
{
Braccio.lvgl_lock();
lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act());
lv_btnmatrix_set_map(btnm1, btnm_map);
lv_obj_align(btnm1, LV_ALIGN_CENTER, MARGIN_LEFT, MARGIN_TOP);
Braccio.lvgl_unlock();
}

/**************************************************************************************
Expand Down
11 changes: 9 additions & 2 deletions examples/LCD_Menu_Joystick/LCD_Menu_Joystick.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ static const char * btnm_map[] = {"Option 1", "Option 2", "\n",
* FUNCTIONS
**************************************************************************************/

static void event_handler(lv_event_t * e){
static void event_handler(lv_event_t * e)
{
Braccio.lvgl_lock();
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if (code == LV_EVENT_CLICKED) {
Expand All @@ -40,9 +42,12 @@ static void event_handler(lv_event_t * e){
LV_LOG_USER("%s was pressed\n", txt);
Serial.println(String(txt) + " was pressed.");
}
Braccio.lvgl_unlock();
}

void customMenu(){
void customMenu()
{
Braccio.lvgl_lock();
lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act());
lv_btnmatrix_set_map(btnm1, btnm_map);
lv_btnmatrix_set_btn_ctrl(btnm1, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
Expand All @@ -53,6 +58,8 @@ void customMenu(){
lv_btnmatrix_set_btn_ctrl(btnm1, 5, LV_BTNMATRIX_CTRL_CHECKABLE);
lv_obj_align(btnm1, LV_ALIGN_CENTER, MARGIN_LEFT, MARGIN_TOP);
lv_obj_add_event_cb(btnm1, event_handler, LV_EVENT_ALL, NULL);
Braccio.lvgl_unlock();

Braccio.connectJoystickTo(btnm1);
}

Expand Down
8 changes: 7 additions & 1 deletion examples/LCD_Motors/LCD_Motors.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static const char * btnm_map[] = {"Motor 1", "Motor 2", "\n",

static void event_handler(lv_event_t * e)
{
Braccio.lvgl_lock();
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if (code == LV_EVENT_CLICKED) {
Expand Down Expand Up @@ -63,9 +64,12 @@ static void event_handler(lv_event_t * e)
selected_motor = 0;
}
}
Braccio.lvgl_unlock();
}

void customMenu() {
void customMenu()
{
Braccio.lvgl_lock();
lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act());
lv_btnmatrix_set_map(btnm1, btnm_map);
lv_btnmatrix_set_btn_ctrl(btnm1, 0, LV_BTNMATRIX_CTRL_CHECKABLE);
Expand All @@ -76,6 +80,8 @@ void customMenu() {
lv_btnmatrix_set_btn_ctrl(btnm1, 5, LV_BTNMATRIX_CTRL_CHECKABLE);
lv_obj_align(btnm1, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_event_cb(btnm1, event_handler, LV_EVENT_ALL, NULL);
Braccio.lvgl_unlock();

Braccio.connectJoystickTo(btnm1);
}

Expand Down
28 changes: 20 additions & 8 deletions src/Braccio++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ BraccioClass::BraccioClass()
, _lvgl_draw_buf{}
, _lvgl_p_obj_group{nullptr}
, _lvgl_kb_indev{nullptr}
, _display_mtx{}
, _display_thd{}
, _pd_events{}
, _pd_timer{}
Expand Down Expand Up @@ -238,6 +239,7 @@ int BraccioClass::getKey() {

void BraccioClass::connectJoystickTo(lv_obj_t* obj)
{
mbed::ScopedLock<rtos::Mutex> lock(_display_mtx);
lv_group_add_obj(_lvgl_p_obj_group, obj);
lv_indev_set_group(_lvgl_kb_indev, _lvgl_p_obj_group);
}
Expand Down Expand Up @@ -409,31 +411,40 @@ void BraccioClass::lvgl_init()

void BraccioClass::display_thread_func()
{
for(;;)
for(;; delay(LV_DISP_DEF_REFR_PERIOD))
{
mbed::ScopedLock<rtos::Mutex> lock(_display_mtx);
lv_task_handler();
lv_tick_inc(LV_DISP_DEF_REFR_PERIOD);
delay(LV_DISP_DEF_REFR_PERIOD);
}
}

void BraccioClass::lvgl_splashScreen(unsigned long const duration_ms)
{
extern const lv_img_dsc_t img_bulb_gif;
lv_obj_t * img = nullptr;

LV_IMG_DECLARE(img_bulb_gif);
lv_obj_t* img = lv_gif_create(lv_scr_act());
lv_gif_set_src(img, &img_bulb_gif);
lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
{
mbed::ScopedLock<rtos::Mutex> lock(_display_mtx);
LV_IMG_DECLARE(img_bulb_gif);
img = lv_gif_create(lv_scr_act());
lv_gif_set_src(img, &img_bulb_gif);
lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
}

/* Wait until the splash screen duration is over. */
for (unsigned long const start = millis(); millis() - start < duration_ms; delay(10)) { }

lv_obj_del(img);
{
mbed::ScopedLock<rtos::Mutex> lock(_display_mtx);
lv_obj_del(img);
}
}

void BraccioClass::lvgl_pleaseConnectPower()
{
mbed::ScopedLock<rtos::Mutex> lock(_display_mtx);

lv_style_set_text_font(&_lv_style, &lv_font_montserrat_32);
lv_obj_t * label1 = lv_label_create(lv_scr_act());
lv_obj_add_style(label1, &_lv_style, 0);
Expand All @@ -445,7 +456,8 @@ void BraccioClass::lvgl_pleaseConnectPower()

void BraccioClass::lvgl_defaultMenu()
{
// TODO: create a meaningful default menu
mbed::ScopedLock<rtos::Mutex> lock(_display_mtx);

lv_style_set_text_font(&_lv_style, &lv_font_montserrat_32);
lv_obj_t * label1 = lv_label_create(lv_scr_act());
lv_obj_add_style(label1, &_lv_style, 0);
Expand Down
4 changes: 4 additions & 0 deletions src/Braccio++.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class BraccioClass
inline bool isJoystickPressed_DOWN() { return (digitalRead(BTN_DOWN) == LOW); }
inline bool isButtonPressed_ENTER() { return (digitalRead(BTN_ENTER) == LOW); }

inline void lvgl_lock () { _display_mtx.lock(); }
inline void lvgl_unlock() { _display_mtx.unlock(); }

static BraccioClass& get_default_instance() {
static BraccioClass dev;
return dev;
Expand Down Expand Up @@ -151,6 +154,7 @@ class BraccioClass
lv_group_t * _lvgl_p_obj_group;
lv_indev_t * _lvgl_kb_indev;
lv_style_t _lv_style;
rtos::Mutex _display_mtx;
rtos::Thread _display_thd;
bool backlight_init();
void display_init();
Expand Down