Skip to content

Don't require explicit lvgl_lock() in most cases #78

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#include <Braccio++.h>

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -27,7 +26,6 @@ void customMenu() {
lv_obj_center(label1);

lv_obj_add_style(btn1, &style, 0);
Braccio.lvgl_unlock();
}

void setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}

/**************************************************************************************
Expand Down
34 changes: 24 additions & 10 deletions src/Braccio++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(void)> custom_menu, bool const wait_for_all_motor_connected)
{
static int constexpr RS485_RX_PIN = 1;

Expand Down Expand Up @@ -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<void(void)>(this, &BraccioClass::lvgl_defaultMenu);
}
drawMenu();

_servos.begin();
_servos.setTime(SLOW);
Expand Down Expand Up @@ -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<rtos::Mutex> 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<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);
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)
Expand Down Expand Up @@ -424,8 +434,9 @@ void BraccioClass::display_thread_func()
{
for(;; delay(LV_DISP_DEF_REFR_PERIOD))
{
mbed::ScopedLock<rtos::Mutex> lock(_display_mtx);
_display_mtx.lock();
lv_task_handler();
_display_mtx.unlock();
lv_tick_inc(LV_DISP_DEF_REFR_PERIOD);
}
}
Expand Down Expand Up @@ -465,10 +476,13 @@ void BraccioClass::lvgl_pleaseConnectPower()
lv_obj_set_pos(label1, 0, 0);
}

void BraccioClass::drawMenu() {
mbed::ScopedLock<rtos::Mutex> lock(_display_mtx);
_draw_menu();
}

void BraccioClass::lvgl_defaultMenu()
{
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
11 changes: 7 additions & 4 deletions src/Braccio++.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(void)> custom_menu) { return begin(custom_menu, true); }
bool begin(mbed::Callback<void(void)> custom_menu, bool const wait_for_all_motor_connected);

void pingOn();
void pingOff();
Expand All @@ -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); }
Expand Down Expand Up @@ -134,6 +134,8 @@ class BraccioClass
void setMotorConnectionStatus(int const id, bool const is_connected);
void motorConnectedThreadFunc();

mbed::Callback<void(void)> _draw_menu;
lv_event_cb_t event_cb;

static int constexpr BTN_LEFT = 3;
static int constexpr BTN_RIGHT = 4;
Expand Down Expand Up @@ -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;
Expand Down