From cc249e59c9edac93ad53cfb22688ff2e248d63ea Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 9 May 2022 10:15:10 +0200 Subject: [PATCH 1/2] Initial: automatically encapsulate display changes in other callbacks --- src/Braccio++.cpp | 34 ++++++++++++++++++++++++---------- src/Braccio++.h | 11 +++++++---- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Braccio++.cpp b/src/Braccio++.cpp index a0ddbfc..2fe5d61 100644 --- a/src/Braccio++.cpp +++ b/src/Braccio++.cpp @@ -94,7 +94,7 @@ BraccioClass::BraccioClass() * PUBLIC MEMBER FUNCTIONS **************************************************************************************/ -bool BraccioClass::begin(voidFuncPtr custom_menu, bool const wait_for_all_motor_connected) +bool BraccioClass::begin(mbed::Callback custom_menu, bool const wait_for_all_motor_connected) { static int constexpr RS485_RX_PIN = 1; @@ -130,10 +130,12 @@ bool BraccioClass::begin(voidFuncPtr custom_menu, bool const wait_for_all_motor_ while(!_PD_UFP.is_PPS_ready()) { delay(10); } lv_obj_clean(lv_scr_act()); - if (custom_menu) - custom_menu(); - else - lvgl_defaultMenu(); + if (custom_menu) { + _draw_menu = custom_menu; + } else { + _draw_menu = mbed::Callback(this, &BraccioClass::lvgl_defaultMenu); + } + drawMenu(); _servos.begin(); _servos.setTime(SLOW); @@ -237,11 +239,19 @@ int BraccioClass::getKey() { return 0; } -void BraccioClass::connectJoystickTo(lv_obj_t* obj) +static BraccioClass* _braccio_static_instance = nullptr; +void BraccioClass::onJoystickEvent(lv_event_t * e) { + mbed::ScopedLock lock(_braccio_static_instance->_display_mtx); + _braccio_static_instance->event_cb(e); +} + +void BraccioClass::connectJoystickTo(lv_obj_t* obj, lv_event_cb_t _event_cb) { - mbed::ScopedLock lock(_display_mtx); lv_group_add_obj(_lvgl_p_obj_group, obj); lv_indev_set_group(_lvgl_kb_indev, _lvgl_p_obj_group); + event_cb = _event_cb; + _braccio_static_instance = this; + lv_obj_add_event_cb(obj, &(BraccioClass::onJoystickEvent), LV_EVENT_ALL, NULL); } void BraccioClass::lvgl_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) @@ -424,8 +434,9 @@ void BraccioClass::display_thread_func() { for(;; delay(LV_DISP_DEF_REFR_PERIOD)) { - mbed::ScopedLock lock(_display_mtx); + _display_mtx.lock(); lv_task_handler(); + _display_mtx.unlock(); lv_tick_inc(LV_DISP_DEF_REFR_PERIOD); } } @@ -465,10 +476,13 @@ void BraccioClass::lvgl_pleaseConnectPower() lv_obj_set_pos(label1, 0, 0); } +void BraccioClass::drawMenu() { + mbed::ScopedLock lock(_display_mtx); + _draw_menu(); +} + void BraccioClass::lvgl_defaultMenu() { - mbed::ScopedLock 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); diff --git a/src/Braccio++.h b/src/Braccio++.h index 5ba6db9..261de08 100644 --- a/src/Braccio++.h +++ b/src/Braccio++.h @@ -65,8 +65,8 @@ class BraccioClass BraccioClass(); inline bool begin() { return begin(nullptr); } - inline bool begin(voidFuncPtr custom_menu) { return begin(custom_menu, true); } - bool begin(voidFuncPtr custom_menu, bool const wait_for_all_motor_connected); + inline bool begin(mbed::Callback custom_menu) { return begin(custom_menu, true); } + bool begin(mbed::Callback custom_menu, bool const wait_for_all_motor_connected); void pingOn(); void pingOff(); @@ -87,7 +87,7 @@ class BraccioClass inline void engage (int const id = SmartServoClass::BROADCAST) { _servos.engage(id); } int getKey(); - void connectJoystickTo(lv_obj_t* obj); + void connectJoystickTo(lv_obj_t* obj, lv_event_cb_t _event_cb = nullptr); inline bool isJoystickPressed_LEFT() { return (digitalRead(BTN_LEFT) == LOW); } inline bool isJoystickPressed_RIGHT() { return (digitalRead(BTN_RIGHT) == LOW); } @@ -134,6 +134,8 @@ class BraccioClass void setMotorConnectionStatus(int const id, bool const is_connected); void motorConnectedThreadFunc(); + mbed::Callback _draw_menu; + lv_event_cb_t event_cb; static int constexpr BTN_LEFT = 3; static int constexpr BTN_RIGHT = 4; @@ -163,7 +165,8 @@ class BraccioClass void lvgl_splashScreen(unsigned long const duration_ms); void lvgl_pleaseConnectPower(); void lvgl_defaultMenu(); - + void drawMenu(); + static void onJoystickEvent(lv_event_t * e); static uint32_t constexpr PD_IRQ_EVENT_FLAG = 1; static uint32_t constexpr PD_TIMER_EVENT_FLAG = 2; From 141224fb97a5ea2c74f4009118ccfb43038b8d22 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 9 May 2022 10:21:42 +0200 Subject: [PATCH 2/2] Fix examples accordingly --- .../01_creating_a_button/01_creating_a_button.ino | 2 -- .../02_designing_the_button/02_designing_the_button.ino | 2 -- .../03_creating_a_menu/03_creating_a_menu.ino | 2 -- .../04_testing_it_out/04_testing_it_out.ino | 2 -- .../05_display_challenge/05_display_challenge.ino | 2 -- .../02_handling_events_in_the_menu.ino | 8 +------- .../03_navigate_challenge_I/03_navigate_challenge_I.ino | 8 +------- .../04_navigate_challenge_II/04_navigate_challenge_II.ino | 8 +------- .../01_Braccio_Record_and_Replay/AppState.cpp | 6 +----- 9 files changed, 4 insertions(+), 36 deletions(-) diff --git a/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/01_creating_a_button/01_creating_a_button.ino b/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/01_creating_a_button/01_creating_a_button.ino index 2ee6ed1..a3bef7a 100644 --- a/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/01_creating_a_button/01_creating_a_button.ino +++ b/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/01_creating_a_button/01_creating_a_button.ino @@ -1,14 +1,12 @@ #include void customMenu() { - Braccio.lvgl_lock(); lv_obj_t * btn1 = lv_btn_create(lv_scr_act()); lv_obj_set_size(btn1, 120, 75); lv_obj_t * label1 = lv_label_create(btn1); lv_label_set_text(label1, "BTN 1"); lv_obj_align(btn1, LV_ALIGN_CENTER, 0, 0); lv_obj_center(label1); - Braccio.lvgl_unlock(); } void setup() { diff --git a/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/02_designing_the_button.ino b/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/02_designing_the_button.ino index 295e3eb..c261fd3 100644 --- a/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/02_designing_the_button.ino +++ b/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/02_designing_the_button/02_designing_the_button.ino @@ -9,7 +9,6 @@ void customMenu() { - Braccio.lvgl_lock(); static lv_style_t style; lv_style_init(&style); lv_style_set_bg_color(&style, lv_color_hex(COLOR_WHITE)); @@ -27,7 +26,6 @@ void customMenu() { lv_obj_center(label1); lv_obj_add_style(btn1, &style, 0); - Braccio.lvgl_unlock(); } void setup() { diff --git a/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/03_creating_a_menu/03_creating_a_menu.ino b/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/03_creating_a_menu/03_creating_a_menu.ino index e5ab044..4647025 100644 --- a/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/03_creating_a_menu/03_creating_a_menu.ino +++ b/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/03_creating_a_menu/03_creating_a_menu.ino @@ -15,7 +15,6 @@ static const char * btnm_map[] = {"OPTION 1", "OPTION 2", "\n", }; void customMenu() { - Braccio.lvgl_lock(); static lv_style_t style_bg; lv_style_init(&style_bg); lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_LIGHT_TEAL)); @@ -34,7 +33,6 @@ void customMenu() { lv_obj_add_style(btnm1, &style_bg, 0); lv_obj_add_style(btnm1, &style_btn, LV_PART_ITEMS); - Braccio.lvgl_unlock(); } void setup() { diff --git a/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/04_testing_it_out/04_testing_it_out.ino b/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/04_testing_it_out/04_testing_it_out.ino index 12c0601..1307669 100644 --- a/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/04_testing_it_out/04_testing_it_out.ino +++ b/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/04_testing_it_out/04_testing_it_out.ino @@ -14,7 +14,6 @@ static const char * btnm_map[] = {"OPTION 1", "OPTION 2", "OPTION 3", "\n", }; void customMenu() { - Braccio.lvgl_lock(); static lv_style_t style_bg; lv_style_init(&style_bg); lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_LIGHT_TEAL)); @@ -34,7 +33,6 @@ void customMenu() { lv_obj_add_style(btnm1, &style_bg, 0); lv_obj_add_style(btnm1, &style_btn, LV_PART_ITEMS); - Braccio.lvgl_unlock(); } void setup() { diff --git a/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/05_display_challenge/05_display_challenge.ino b/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/05_display_challenge/05_display_challenge.ino index 9209311..d4fcba7 100644 --- a/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/05_display_challenge/05_display_challenge.ino +++ b/examples/Platform_Tutorials/lessons/01-programming-the-braccio-display/05_display_challenge/05_display_challenge.ino @@ -14,7 +14,6 @@ static const char * btnm_map[] = {"BTN 1", "\n", }; void customMenu() { - Braccio.lvgl_lock(); static lv_style_t style_bg; lv_style_init(&style_bg); lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_BG)); @@ -34,7 +33,6 @@ void customMenu() { lv_obj_add_style(btnm1, &style_bg, 0); lv_obj_add_style(btnm1, &style_btn, LV_PART_ITEMS); - Braccio.lvgl_unlock(); } void setup() { diff --git a/examples/Platform_Tutorials/lessons/02-navigatting-the-display-menu/02_handling_events_in_the_menu/02_handling_events_in_the_menu.ino b/examples/Platform_Tutorials/lessons/02-navigatting-the-display-menu/02_handling_events_in_the_menu/02_handling_events_in_the_menu.ino index 45c392b..7b49ceb 100644 --- a/examples/Platform_Tutorials/lessons/02-navigatting-the-display-menu/02_handling_events_in_the_menu/02_handling_events_in_the_menu.ino +++ b/examples/Platform_Tutorials/lessons/02-navigatting-the-display-menu/02_handling_events_in_the_menu/02_handling_events_in_the_menu.ino @@ -16,7 +16,6 @@ static const char * btnm_map[] = {"Option 1", "Option 2", "\n", }; static void eventHandler(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_PRESSED) { @@ -26,11 +25,9 @@ static void eventHandler(lv_event_t * e) { LV_LOG_USER("%s was selected\n", txt); Serial.println(String(txt) + " was selected."); } - Braccio.lvgl_unlock(); } void customMenu() { - Braccio.lvgl_lock(); static lv_style_t style_bg; lv_style_init(&style_bg); lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_WHITE)); @@ -59,10 +56,7 @@ void customMenu() { lv_btnmatrix_set_one_checked(btnm1, true); - lv_obj_add_event_cb(btnm1, eventHandler, LV_EVENT_ALL, NULL); - Braccio.lvgl_unlock(); - - Braccio.connectJoystickTo(btnm1); + Braccio.connectJoystickTo(btnm1, eventHandler); } void setup() { diff --git a/examples/Platform_Tutorials/lessons/02-navigatting-the-display-menu/03_navigate_challenge_I/03_navigate_challenge_I.ino b/examples/Platform_Tutorials/lessons/02-navigatting-the-display-menu/03_navigate_challenge_I/03_navigate_challenge_I.ino index a531642..ede16e5 100644 --- a/examples/Platform_Tutorials/lessons/02-navigatting-the-display-menu/03_navigate_challenge_I/03_navigate_challenge_I.ino +++ b/examples/Platform_Tutorials/lessons/02-navigatting-the-display-menu/03_navigate_challenge_I/03_navigate_challenge_I.ino @@ -16,7 +16,6 @@ static const char * btnm_map[] = {"Option 1", "Option 2", "\n", }; static void eventHandler(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_PRESSING) { @@ -26,11 +25,9 @@ static void eventHandler(lv_event_t * e) { LV_LOG_USER("%s is pressed\n", txt); Serial.println(String(txt) + " is pressed."); } - Braccio.lvgl_unlock(); } void customMenu() { - Braccio.lvgl_lock(); static lv_style_t style_bg; lv_style_init(&style_bg); lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_LIGHT_TEAL)); @@ -59,10 +56,7 @@ void customMenu() { lv_btnmatrix_set_one_checked(btnm1, true); - lv_obj_add_event_cb(btnm1, eventHandler, LV_EVENT_ALL, NULL); - Braccio.lvgl_unlock(); - - Braccio.connectJoystickTo(btnm1); + Braccio.connectJoystickTo(btnm1, eventHandler); } void setup() { diff --git a/examples/Platform_Tutorials/lessons/02-navigatting-the-display-menu/04_navigate_challenge_II/04_navigate_challenge_II.ino b/examples/Platform_Tutorials/lessons/02-navigatting-the-display-menu/04_navigate_challenge_II/04_navigate_challenge_II.ino index 63358b2..e6b26f7 100644 --- a/examples/Platform_Tutorials/lessons/02-navigatting-the-display-menu/04_navigate_challenge_II/04_navigate_challenge_II.ino +++ b/examples/Platform_Tutorials/lessons/02-navigatting-the-display-menu/04_navigate_challenge_II/04_navigate_challenge_II.ino @@ -16,7 +16,6 @@ static const char * btnm_map[] = {"Option 1", "Option 2", "\n", }; static void eventHandler(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_RELEASED) { @@ -26,11 +25,9 @@ static void eventHandler(lv_event_t * e) { LV_LOG_USER("%s was released\n", txt); Serial.println(String(txt) + " was released."); } - Braccio.lvgl_unlock(); } void customMenu() { - Braccio.lvgl_lock(); static lv_style_t style_bg; lv_style_init(&style_bg); lv_style_set_bg_color(&style_bg, lv_color_hex(COLOR_LIGHT_TEAL)); @@ -59,10 +56,7 @@ void customMenu() { lv_btnmatrix_set_one_checked(btnm1, true); - lv_obj_add_event_cb(btnm1, eventHandler, LV_EVENT_ALL, NULL); - Braccio.lvgl_unlock(); - - Braccio.connectJoystickTo(btnm1); + Braccio.connectJoystickTo(btnm1, eventHandler); } void setup() { diff --git a/examples/Platform_Tutorials/projects/p03-learning-mode/01_Braccio_Record_and_Replay/AppState.cpp b/examples/Platform_Tutorials/projects/p03-learning-mode/01_Braccio_Record_and_Replay/AppState.cpp index d8317d2..2e00b95 100644 --- a/examples/Platform_Tutorials/projects/p03-learning-mode/01_Braccio_Record_and_Replay/AppState.cpp +++ b/examples/Platform_Tutorials/projects/p03-learning-mode/01_Braccio_Record_and_Replay/AppState.cpp @@ -58,7 +58,6 @@ static void event_handler_menu(lv_event_t * e) void custom_main_menu() { - Braccio.lvgl_lock(); static lv_style_t style_focus; lv_style_init(&style_focus); lv_style_set_outline_color(&style_focus, lv_color_hex(COLOR_ORANGE)); @@ -89,10 +88,7 @@ void custom_main_menu() lv_label_set_text_fmt(counter, "Counter: %d" , 0); lv_obj_align(counter, LV_ALIGN_CENTER, 0, 80); - lv_obj_add_event_cb(btnm, event_handler_menu, LV_EVENT_ALL, NULL); - Braccio.lvgl_unlock(); - - Braccio.connectJoystickTo(btnm); + Braccio.connectJoystickTo(btnm, event_handler_menu); } /**************************************************************************************