Skip to content

Commit b9554a8

Browse files
committed
Simplify power negotiation logic.
1 parent 9d39167 commit b9554a8

File tree

3 files changed

+123
-51
lines changed

3 files changed

+123
-51
lines changed

.vscode/settings.json

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"files.associations": {
3+
"*.impl": "cpp",
4+
"*.cppx": "cpp",
5+
"*.tcc": "cpp",
6+
"bitset": "cpp",
7+
"list": "cpp",
8+
"unordered_map": "cpp",
9+
"fstream": "cpp",
10+
"istream": "cpp",
11+
"ostream": "cpp",
12+
"streambuf": "cpp",
13+
"functional": "cpp",
14+
"__node_handle": "cpp",
15+
"hash_map": "cpp",
16+
"map": "cpp",
17+
"string": "cpp",
18+
"vector": "cpp",
19+
"mutex": "cpp",
20+
"condition_variable": "cpp",
21+
"__mutex_base": "cpp",
22+
"memory": "cpp",
23+
"chrono": "cpp",
24+
"__hash_table": "cpp",
25+
"array": "cpp",
26+
"deque": "cpp",
27+
"string_view": "cpp",
28+
"initializer_list": "cpp",
29+
"__bit_reference": "cpp",
30+
"__split_buffer": "cpp",
31+
"__tree": "cpp",
32+
"filesystem": "cpp",
33+
"iterator": "cpp",
34+
"cctype": "cpp",
35+
"clocale": "cpp",
36+
"cmath": "cpp",
37+
"cstdarg": "cpp",
38+
"cstddef": "cpp",
39+
"cstdio": "cpp",
40+
"cstdlib": "cpp",
41+
"cstring": "cpp",
42+
"ctime": "cpp",
43+
"cwchar": "cpp",
44+
"cwctype": "cpp",
45+
"atomic": "cpp",
46+
"codecvt": "cpp",
47+
"complex": "cpp",
48+
"cstdint": "cpp",
49+
"exception": "cpp",
50+
"algorithm": "cpp",
51+
"memory_resource": "cpp",
52+
"numeric": "cpp",
53+
"optional": "cpp",
54+
"random": "cpp",
55+
"ratio": "cpp",
56+
"system_error": "cpp",
57+
"tuple": "cpp",
58+
"type_traits": "cpp",
59+
"utility": "cpp",
60+
"iomanip": "cpp",
61+
"iosfwd": "cpp",
62+
"limits": "cpp",
63+
"new": "cpp",
64+
"sstream": "cpp",
65+
"stdexcept": "cpp",
66+
"thread": "cpp",
67+
"cinttypes": "cpp",
68+
"typeinfo": "cpp",
69+
"bit": "cpp",
70+
"__config": "cpp",
71+
"__debug": "cpp",
72+
"__errc": "cpp",
73+
"__functional_base": "cpp",
74+
"__locale": "cpp",
75+
"__nullptr": "cpp",
76+
"__string": "cpp",
77+
"__threading_support": "cpp",
78+
"__tuple": "cpp",
79+
"ios": "cpp",
80+
"locale": "cpp",
81+
"queue": "cpp",
82+
"stack": "cpp",
83+
"*.cx": "cpp"
84+
}
85+
}

src/Braccio++.cpp

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ extern "C"
4242
{
4343
void braccio_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p);
4444
void braccio_read_keypad(lv_indev_drv_t * indev, lv_indev_data_t * data);
45-
void braccio_unlock_pd_semaphore_irq();
46-
void braccio_unlock_pd_semaphore();
45+
void braccio_onPowerIrqEvent();
46+
void braccio_onPowerTimerEvent();
4747
};
4848

4949
/**************************************************************************************
@@ -77,7 +77,6 @@ BraccioClass::BraccioClass()
7777
, _display_thd{}
7878
, _pd_events{}
7979
, _pd_timer{}
80-
, _start_pd_burst{0xFFFFFFFF}
8180
, _pd_thd{osPriorityHigh}
8281
{
8382

@@ -99,9 +98,9 @@ bool BraccioClass::begin(voidFuncPtr custom_menu)
9998
pinMode(RS485_RX_PIN, INPUT_PULLUP);
10099

101100
_PD_UFP.init_PPS(_i2c_mtx, PPS_V(7.2), PPS_A(2.0));
101+
attachInterrupt(PIN_FUSB302_INT, braccio_onPowerIrqEvent, FALLING);
102102
_pd_thd.start(mbed::callback(this, &BraccioClass::pd_thread_func));
103-
attachInterrupt(PIN_FUSB302_INT, braccio_unlock_pd_semaphore_irq, FALLING);
104-
_pd_timer.attach(braccio_unlock_pd_semaphore, 10ms);
103+
_pd_timer.attach(braccio_onPowerTimerEvent, 10ms);
105104

106105
button_init();
107106

@@ -112,26 +111,15 @@ bool BraccioClass::begin(voidFuncPtr custom_menu)
112111
lvgl_init();
113112
_display_thd.start(mbed::callback(this, &BraccioClass::display_thread_func));
114113

115-
116-
auto check_power_func = [this]()
117-
{
118-
if (!_PD_UFP.is_PPS_ready())
119-
{
120-
_PD_UFP.print_status(Serial);
121-
_PD_UFP.set_PPS(PPS_V(7.2), PPS_A(2.0));
122-
delay(10);
123-
}
124-
};
125-
126-
lvgl_splashScreen(2000, check_power_func);
114+
lvgl_splashScreen(2000);
127115
lv_obj_clean(lv_scr_act());
128116

129117
if (!_PD_UFP.is_PPS_ready())
130118
lvgl_pleaseConnectPower();
131119

132120
/* Loop forever, if no power is attached. */
133-
while(!_PD_UFP.is_PPS_ready())
134-
check_power_func();
121+
while(!_PD_UFP.is_PPS_ready()) { }
122+
/* check_power_func(); */
135123
lv_obj_clean(lv_scr_act());
136124

137125
if (custom_menu)
@@ -248,15 +236,14 @@ void BraccioClass::lvgl_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, l
248236
lv_disp_flush_ready(disp);
249237
}
250238

251-
void BraccioClass::unlock_pd_semaphore_irq()
239+
void BraccioClass::onPowerIrqEvent()
252240
{
253-
_start_pd_burst = millis();
254-
_pd_events.set(2);
241+
_pd_events.set(PD_IRQ_EVENT_FLAG);
255242
}
256243

257-
void BraccioClass::unlock_pd_semaphore()
244+
void BraccioClass::onPowerTimerEvent()
258245
{
259-
_pd_events.set(1);
246+
_pd_events.set(PD_TIMER_EVENT_FLAG);
260247
}
261248

262249
/**************************************************************************************
@@ -411,7 +398,7 @@ void BraccioClass::display_thread_func()
411398
}
412399
}
413400

414-
void BraccioClass::lvgl_splashScreen(unsigned long const duration_ms, std::function<void()> check_power_func)
401+
void BraccioClass::lvgl_splashScreen(unsigned long const duration_ms)
415402
{
416403
extern const lv_img_dsc_t img_bulb_gif;
417404

@@ -420,14 +407,8 @@ void BraccioClass::lvgl_splashScreen(unsigned long const duration_ms, std::funct
420407
lv_gif_set_src(img, &img_bulb_gif);
421408
lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
422409

423-
/* Wait until the splash screen duration is over.
424-
* Meanwhile use the wait time for checking the
425-
* power.
426-
*/
427-
for (unsigned long const start = millis(); millis() - start < duration_ms;)
428-
{
429-
check_power_func();
430-
}
410+
/* Wait until the splash screen duration is over. */
411+
for (unsigned long const start = millis(); millis() - start < duration_ms; delay(10)) { }
431412

432413
lv_obj_del(img);
433414
}
@@ -457,27 +438,32 @@ void BraccioClass::lvgl_defaultMenu()
457438

458439
void BraccioClass::pd_thread_func()
459440
{
460-
_start_pd_burst = millis();
461441
size_t last_time_ask_pps = 0;
462442

463443
for(;;)
464444
{
465-
auto ret = _pd_events.wait_any(0xFF);
466-
if ((ret & 1) && (millis() - _start_pd_burst > 1000)) {
445+
uint32_t const flags = _pd_events.wait_any(0xFF);
446+
447+
if (flags & PD_IRQ_EVENT_FLAG)
448+
{
467449
_pd_timer.detach();
468-
_pd_timer.attach(braccio_unlock_pd_semaphore, 5s);
450+
_pd_timer.attach(braccio_onPowerTimerEvent, 10ms);
469451
}
470-
if (ret & 2) {
452+
453+
if (flags & PD_TIMER_EVENT_FLAG)
454+
{
471455
_pd_timer.detach();
472-
_pd_timer.attach(braccio_unlock_pd_semaphore, 50ms);
456+
_pd_timer.attach(braccio_onPowerTimerEvent, 10ms);
473457
}
474-
if (millis() - last_time_ask_pps > 5000) {
458+
459+
_PD_UFP.run();
460+
_PD_UFP.print_status(Serial);
461+
462+
if ((millis() - last_time_ask_pps) > 5000)
463+
{
475464
_PD_UFP.set_PPS(PPS_V(7.2), PPS_A(2.0));
476465
last_time_ask_pps = millis();
477466
}
478-
_PD_UFP.run();
479-
if (_PD_UFP.is_power_ready() && _PD_UFP.is_PPS_ready()) {
480-
}
481467
}
482468
}
483469

@@ -538,12 +524,12 @@ extern "C" void braccio_read_keypad(lv_indev_drv_t * drv, lv_indev_data_t* data)
538524
data->key = last_key;
539525
}
540526

541-
void braccio_unlock_pd_semaphore_irq()
527+
void braccio_onPowerIrqEvent()
542528
{
543-
Braccio.unlock_pd_semaphore_irq();
529+
Braccio.onPowerIrqEvent();
544530
}
545531

546-
void braccio_unlock_pd_semaphore()
532+
void braccio_onPowerTimerEvent()
547533
{
548-
Braccio.unlock_pd_semaphore();
534+
Braccio.onPowerTimerEvent();
549535
}

src/Braccio++.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ class BraccioClass
103103

104104
/* Those functions MUST NOT be used by the user. */
105105
void lvgl_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p);
106-
void unlock_pd_semaphore_irq();
107-
void unlock_pd_semaphore();
106+
void onPowerIrqEvent();
107+
void onPowerTimerEvent();
108108

109109
protected:
110110

@@ -156,14 +156,15 @@ class BraccioClass
156156
void display_init();
157157
void lvgl_init();
158158
void display_thread_func();
159-
void lvgl_splashScreen(unsigned long const duration_ms, std::function<void()> check_power_func);
159+
void lvgl_splashScreen(unsigned long const duration_ms);
160160
void lvgl_pleaseConnectPower();
161161
void lvgl_defaultMenu();
162162

163163

164+
static uint32_t constexpr PD_IRQ_EVENT_FLAG = 1;
165+
static uint32_t constexpr PD_TIMER_EVENT_FLAG = 2;
164166
rtos::EventFlags _pd_events;
165167
mbed::Ticker _pd_timer;
166-
unsigned int _start_pd_burst;
167168
rtos::Thread _pd_thd;
168169
void pd_thread_func();
169170
};

0 commit comments

Comments
 (0)