Skip to content

Commit cc249e5

Browse files
committed
Initial: automatically encapsulate display changes in other callbacks
1 parent 88bdca0 commit cc249e5

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/Braccio++.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ BraccioClass::BraccioClass()
9494
* PUBLIC MEMBER FUNCTIONS
9595
**************************************************************************************/
9696

97-
bool BraccioClass::begin(voidFuncPtr custom_menu, bool const wait_for_all_motor_connected)
97+
bool BraccioClass::begin(mbed::Callback<void(void)> custom_menu, bool const wait_for_all_motor_connected)
9898
{
9999
static int constexpr RS485_RX_PIN = 1;
100100

@@ -130,10 +130,12 @@ bool BraccioClass::begin(voidFuncPtr custom_menu, bool const wait_for_all_motor_
130130
while(!_PD_UFP.is_PPS_ready()) { delay(10); }
131131
lv_obj_clean(lv_scr_act());
132132

133-
if (custom_menu)
134-
custom_menu();
135-
else
136-
lvgl_defaultMenu();
133+
if (custom_menu) {
134+
_draw_menu = custom_menu;
135+
} else {
136+
_draw_menu = mbed::Callback<void(void)>(this, &BraccioClass::lvgl_defaultMenu);
137+
}
138+
drawMenu();
137139

138140
_servos.begin();
139141
_servos.setTime(SLOW);
@@ -237,11 +239,19 @@ int BraccioClass::getKey() {
237239
return 0;
238240
}
239241

240-
void BraccioClass::connectJoystickTo(lv_obj_t* obj)
242+
static BraccioClass* _braccio_static_instance = nullptr;
243+
void BraccioClass::onJoystickEvent(lv_event_t * e) {
244+
mbed::ScopedLock<rtos::Mutex> lock(_braccio_static_instance->_display_mtx);
245+
_braccio_static_instance->event_cb(e);
246+
}
247+
248+
void BraccioClass::connectJoystickTo(lv_obj_t* obj, lv_event_cb_t _event_cb)
241249
{
242-
mbed::ScopedLock<rtos::Mutex> lock(_display_mtx);
243250
lv_group_add_obj(_lvgl_p_obj_group, obj);
244251
lv_indev_set_group(_lvgl_kb_indev, _lvgl_p_obj_group);
252+
event_cb = _event_cb;
253+
_braccio_static_instance = this;
254+
lv_obj_add_event_cb(obj, &(BraccioClass::onJoystickEvent), LV_EVENT_ALL, NULL);
245255
}
246256

247257
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()
424434
{
425435
for(;; delay(LV_DISP_DEF_REFR_PERIOD))
426436
{
427-
mbed::ScopedLock<rtos::Mutex> lock(_display_mtx);
437+
_display_mtx.lock();
428438
lv_task_handler();
439+
_display_mtx.unlock();
429440
lv_tick_inc(LV_DISP_DEF_REFR_PERIOD);
430441
}
431442
}
@@ -465,10 +476,13 @@ void BraccioClass::lvgl_pleaseConnectPower()
465476
lv_obj_set_pos(label1, 0, 0);
466477
}
467478

479+
void BraccioClass::drawMenu() {
480+
mbed::ScopedLock<rtos::Mutex> lock(_display_mtx);
481+
_draw_menu();
482+
}
483+
468484
void BraccioClass::lvgl_defaultMenu()
469485
{
470-
mbed::ScopedLock<rtos::Mutex> lock(_display_mtx);
471-
472486
lv_style_set_text_font(&_lv_style, &lv_font_montserrat_32);
473487
lv_obj_t * label1 = lv_label_create(lv_scr_act());
474488
lv_obj_add_style(label1, &_lv_style, 0);

src/Braccio++.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ class BraccioClass
6565
BraccioClass();
6666

6767
inline bool begin() { return begin(nullptr); }
68-
inline bool begin(voidFuncPtr custom_menu) { return begin(custom_menu, true); }
69-
bool begin(voidFuncPtr custom_menu, bool const wait_for_all_motor_connected);
68+
inline bool begin(mbed::Callback<void(void)> custom_menu) { return begin(custom_menu, true); }
69+
bool begin(mbed::Callback<void(void)> custom_menu, bool const wait_for_all_motor_connected);
7070

7171
void pingOn();
7272
void pingOff();
@@ -87,7 +87,7 @@ class BraccioClass
8787
inline void engage (int const id = SmartServoClass::BROADCAST) { _servos.engage(id); }
8888

8989
int getKey();
90-
void connectJoystickTo(lv_obj_t* obj);
90+
void connectJoystickTo(lv_obj_t* obj, lv_event_cb_t _event_cb = nullptr);
9191

9292
inline bool isJoystickPressed_LEFT() { return (digitalRead(BTN_LEFT) == LOW); }
9393
inline bool isJoystickPressed_RIGHT() { return (digitalRead(BTN_RIGHT) == LOW); }
@@ -134,6 +134,8 @@ class BraccioClass
134134
void setMotorConnectionStatus(int const id, bool const is_connected);
135135
void motorConnectedThreadFunc();
136136

137+
mbed::Callback<void(void)> _draw_menu;
138+
lv_event_cb_t event_cb;
137139

138140
static int constexpr BTN_LEFT = 3;
139141
static int constexpr BTN_RIGHT = 4;
@@ -163,7 +165,8 @@ class BraccioClass
163165
void lvgl_splashScreen(unsigned long const duration_ms);
164166
void lvgl_pleaseConnectPower();
165167
void lvgl_defaultMenu();
166-
168+
void drawMenu();
169+
static void onJoystickEvent(lv_event_t * e);
167170

168171
static uint32_t constexpr PD_IRQ_EVENT_FLAG = 1;
169172
static uint32_t constexpr PD_TIMER_EVENT_FLAG = 2;

0 commit comments

Comments
 (0)