Skip to content

Commit 90d6a15

Browse files
committed
Pack code for checking power into lambda in order to avoid code duplication.
1 parent 1daccfd commit 90d6a15

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/Braccio++.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class BraccioClass
160160
void digitalWrite(int pin, uint8_t value);
161161

162162
// default display APIs
163-
void lvgl_splashScreen(unsigned long const duration_ms);
163+
void lvgl_splashScreen(unsigned long const duration_ms, std::function<void()> check_power_func);
164164
void lvgl_pleaseConnectPower();
165165
void defaultMenu();
166166

src/Braccio.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,27 @@ bool BraccioClass::begin(voidFuncPtr customMenu) {
125125

126126
_display_thread.start(mbed::callback(this, &BraccioClass::display_thread));
127127

128-
lvgl_splashScreen(2000);
128+
auto check_power_func = [this]()
129+
{
130+
if (!PD_UFP.is_PPS_ready())
131+
{
132+
i2c_mutex.lock();
133+
PD_UFP.print_status(Serial);
134+
PD_UFP.set_PPS(PPS_V(7.2), PPS_A(2.0));
135+
delay(10);
136+
i2c_mutex.unlock();
137+
}
138+
};
139+
140+
lvgl_splashScreen(2000, check_power_func);
129141
lv_obj_clean(lv_scr_act());
130142

131143
if (!PD_UFP.is_PPS_ready())
132144
lvgl_pleaseConnectPower();
133145

134146
/* Loop forever, if no power is attached. */
135147
while(!PD_UFP.is_PPS_ready())
136-
{
137-
i2c_mutex.lock();
138-
PD_UFP.print_status(Serial);
139-
PD_UFP.set_PPS(PPS_V(7.2), PPS_A(2.0));
140-
delay(10);
141-
i2c_mutex.unlock();
142-
Serial.println(millis());
143-
}
148+
check_power_func();
144149
lv_obj_clean(lv_scr_act());
145150

146151
if (customMenu) {
@@ -203,7 +208,7 @@ void BraccioClass::display_thread()
203208

204209
#include <extra/libs/gif/lv_gif.h>
205210

206-
void BraccioClass::lvgl_splashScreen(unsigned long const duration_ms)
211+
void BraccioClass::lvgl_splashScreen(unsigned long const duration_ms, std::function<void()> check_power_func)
207212
{
208213
LV_IMG_DECLARE(img_bulb_gif);
209214
lv_obj_t* img = lv_gif_create(lv_scr_act());
@@ -216,14 +221,7 @@ void BraccioClass::lvgl_splashScreen(unsigned long const duration_ms)
216221
*/
217222
for (unsigned long const start = millis(); millis() - start < duration_ms;)
218223
{
219-
if (!PD_UFP.is_PPS_ready())
220-
{
221-
i2c_mutex.lock();
222-
PD_UFP.print_status(Serial);
223-
PD_UFP.set_PPS(PPS_V(7.2), PPS_A(2.0));
224-
delay(10);
225-
i2c_mutex.unlock();
226-
}
224+
check_power_func();
227225
}
228226

229227
lv_obj_del(img);

0 commit comments

Comments
 (0)