Skip to content

Commit 2c32e4b

Browse files
committed
apply patch from github, thanks mark!
1 parent bc23645 commit 2c32e4b

File tree

2 files changed

+44
-55
lines changed

2 files changed

+44
-55
lines changed

src/displayapp/screens/Music.cpp

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,22 @@
2525

2626
using namespace Pinetime::Applications::Screens;
2727

28-
static void event_handler(lv_obj_t* obj, lv_event_t event) {
29-
Music* screen = static_cast<Music*>(obj->user_data);
30-
screen->OnObjectEvent(obj, event);
31-
}
28+
namespace {
29+
void EventHandler(lv_obj_t* obj, lv_event_t event) {
30+
auto* screen = static_cast<Music*>(obj->user_data);
31+
screen->OnObjectEvent(obj, event);
32+
}
3233

33-
/**
34-
* Set the pixel array to display by the image
35-
* This just calls lv_img_set_src but adds type safety
36-
*
37-
* @param img pointer to an image object
38-
* @param data the image array
39-
*/
40-
inline void lv_img_set_src_arr(lv_obj_t* img, const lv_img_dsc_t* src_img) {
41-
lv_img_set_src(img, src_img);
34+
/**
35+
* Set the pixel array to display by the image
36+
* This just calls lv_img_set_src but adds type safety
37+
*
38+
* @param img pointer to an image object
39+
* @param data the image array
40+
*/
41+
inline void lv_img_set_src_arr(lv_obj_t* img, const lv_img_dsc_t* src_img) {
42+
lv_img_set_src(img, src_img);
43+
}
4244
}
4345

4446
/**
@@ -55,7 +57,7 @@ Music::Music(Pinetime::Controllers::MusicService& music, const Controllers::Ble&
5557

5658
btnVolDown = lv_btn_create(lv_scr_act(), nullptr);
5759
btnVolDown->user_data = this;
58-
lv_obj_set_event_cb(btnVolDown, event_handler);
60+
lv_obj_set_event_cb(btnVolDown, EventHandler);
5961
lv_obj_set_size(btnVolDown, 117, 60);
6062
lv_obj_align(btnVolDown, nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0);
6163
lv_obj_add_style(btnVolDown, LV_STATE_DEFAULT, &btn_style);
@@ -64,7 +66,7 @@ Music::Music(Pinetime::Controllers::MusicService& music, const Controllers::Ble&
6466

6567
btnVolUp = lv_btn_create(lv_scr_act(), nullptr);
6668
btnVolUp->user_data = this;
67-
lv_obj_set_event_cb(btnVolUp, event_handler);
69+
lv_obj_set_event_cb(btnVolUp, EventHandler);
6870
lv_obj_set_size(btnVolUp, 117, 60);
6971
lv_obj_align(btnVolUp, nullptr, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
7072
lv_obj_add_style(btnVolUp, LV_STATE_DEFAULT, &btn_style);
@@ -73,7 +75,7 @@ Music::Music(Pinetime::Controllers::MusicService& music, const Controllers::Ble&
7375

7476
btnPrev = lv_btn_create(lv_scr_act(), nullptr);
7577
btnPrev->user_data = this;
76-
lv_obj_set_event_cb(btnPrev, event_handler);
78+
lv_obj_set_event_cb(btnPrev, EventHandler);
7779
lv_obj_set_size(btnPrev, 76, 76);
7880
lv_obj_align(btnPrev, nullptr, LV_ALIGN_IN_BOTTOM_LEFT, 3, 0);
7981
lv_obj_add_style(btnPrev, LV_STATE_DEFAULT, &btn_style);
@@ -82,7 +84,7 @@ Music::Music(Pinetime::Controllers::MusicService& music, const Controllers::Ble&
8284

8385
btnNext = lv_btn_create(lv_scr_act(), nullptr);
8486
btnNext->user_data = this;
85-
lv_obj_set_event_cb(btnNext, event_handler);
87+
lv_obj_set_event_cb(btnNext, EventHandler);
8688
lv_obj_set_size(btnNext, 76, 76);
8789
lv_obj_align(btnNext, nullptr, LV_ALIGN_IN_BOTTOM_RIGHT, -3, 0);
8890
lv_obj_add_style(btnNext, LV_STATE_DEFAULT, &btn_style);
@@ -91,25 +93,13 @@ Music::Music(Pinetime::Controllers::MusicService& music, const Controllers::Ble&
9193

9294
btnPlayPause = lv_btn_create(lv_scr_act(), nullptr);
9395
btnPlayPause->user_data = this;
94-
lv_obj_set_event_cb(btnPlayPause, event_handler);
96+
lv_obj_set_event_cb(btnPlayPause, EventHandler);
9597
lv_obj_set_size(btnPlayPause, 76, 76);
9698
lv_obj_align(btnPlayPause, nullptr, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
9799
lv_obj_add_style(btnPlayPause, LV_STATE_DEFAULT, &btn_style);
98100
txtPlayPause = lv_label_create(btnPlayPause, nullptr);
99101
lv_label_set_text_static(txtPlayPause, Symbols::play);
100102

101-
buttons[0] = btnPrev;
102-
buttons[1] = btnPlayPause;
103-
buttons[2] = btnNext;
104-
buttons[3] = btnVolDown;
105-
buttons[4] = btnVolUp;
106-
107-
controlLabels[0] = txtBtnPrev;
108-
controlLabels[1] = txtPlayPause;
109-
controlLabels[2] = txtBtnNext;
110-
controlLabels[3] = txtVolDown;
111-
controlLabels[4] = txtVolUp;
112-
113103
// I'm using the txtTrack label as the top anchor for the whole lot
114104
// of song, artist, progress bar and duration text (0:00 and -0:00) so
115105
// its much easier to move that around and mess with the buttons separately
@@ -174,12 +164,11 @@ void Music::Refresh() {
174164
if (!connected) {
175165
SetDisconnectedUI();
176166
return;
177-
} else {
178-
musicService.event(Controllers::MusicService::EVENT_MUSIC_OPEN);
179-
SetConnectedUI();
180-
RefreshTrackInfo();
181-
return;
182167
}
168+
musicService.event(Controllers::MusicService::EVENT_MUSIC_OPEN);
169+
SetConnectedUI();
170+
RefreshTrackInfo();
171+
return;
183172
}
184173

185174
if (bleState.Get()) {
@@ -191,11 +180,11 @@ void Music::SetDisconnectedUI() {
191180
lv_label_set_text_static(txtArtist, "Disconnected");
192181
lv_label_set_text_static(txtTrack, "");
193182

194-
for (auto btn : buttons) {
183+
for (auto* btn : GetButtons()) {
195184
lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, Colors::bgDark);
196185
}
197186

198-
for (auto lbl : controlLabels) {
187+
for (auto* lbl : GetButtonLabels()) {
199188
lv_obj_set_style_local_text_color(lbl, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::bgAlt);
200189
}
201190

@@ -209,11 +198,11 @@ void Music::SetDisconnectedUI() {
209198
}
210199

211200
void Music::SetConnectedUI() {
212-
for (auto btn : buttons) {
201+
for (auto* btn : GetButtons()) {
213202
lv_obj_set_style_local_bg_color(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, Colors::bgAlt);
214203
}
215204

216-
for (auto lbl : controlLabels) {
205+
for (auto* lbl : GetButtonLabels()) {
217206
lv_obj_set_style_local_text_color(lbl, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
218207
}
219208
}
@@ -246,17 +235,17 @@ void Music::RefreshTrackInfo() {
246235
}
247236

248237
void Music::UpdateLength() {
249-
int remaining = totalLength - currentPosition;
250-
if (remaining < 0) {
251-
remaining = 0;
252-
}
238+
int remaining = std::max(totalLength - currentPosition, 0);
253239

254240
if (totalLength > (99 * 60 * 60)) {
255241
lv_label_set_text_static(txtCurrentPosition, "Inf");
256242
lv_label_set_text_static(txtTrackDuration, "Inf");
257243
} else if (totalLength > (99 * 60)) {
258244
lv_label_set_text_fmt(txtCurrentPosition, "%d:%02d", (currentPosition / (60 * 60)) % 100, ((currentPosition % (60 * 60)) / 60) % 100);
259245
lv_label_set_text_fmt(txtTrackDuration, "-%d:%02d", (remaining / (60 * 60)) % 100, ((remaining % (60 * 60)) / 60) % 100);
246+
// These conversions are narrowing: lv_bar_set_range accepts int16_t args
247+
// resolve by normalising?
248+
// same for else branch below
260249
lv_bar_set_range(barTrackDuration, 0, totalLength > 0 ? totalLength : 1);
261250
lv_bar_set_value(barTrackDuration, currentPosition, LV_ANIM_OFF);
262251
} else {
@@ -276,18 +265,14 @@ void Music::OnObjectEvent(lv_obj_t* obj, lv_event_t event) {
276265
} else if (obj == btnPrev) {
277266
musicService.event(Controllers::MusicService::EVENT_MUSIC_PREV);
278267
} else if (obj == btnPlayPause) {
279-
if (playing == Pinetime::Controllers::MusicService::MusicStatus::Playing) {
268+
if (playing) {
280269
musicService.event(Controllers::MusicService::EVENT_MUSIC_PAUSE);
281-
282-
// Let's assume it stops playing instantly
283-
playing = Controllers::MusicService::NotPlaying;
284270
} else {
285271
musicService.event(Controllers::MusicService::EVENT_MUSIC_PLAY);
286-
287-
// Let's assume it starts playing instantly
288-
// TODO: In the future should check for BT connection for better UX
289-
playing = Controllers::MusicService::Playing;
290272
}
273+
// Let's assume it stops/starts playing instantly
274+
// TODO: In the future should check for BT connection for better UX
275+
playing = !playing;
291276
} else if (obj == btnNext) {
292277
musicService.event(Controllers::MusicService::EVENT_MUSIC_NEXT);
293278
}

src/displayapp/screens/Music.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
#include <FreeRTOS.h>
2121
#include <lvgl/src/lv_core/lv_obj.h>
22+
#include <array>
2223
#include <string>
2324
#include "displayapp/screens/Screen.h"
24-
#include "displayapp/widgets/PageIndicator.h"
2525
#include "displayapp/apps/Apps.h"
2626
#include "displayapp/Controllers.h"
2727
#include "Symbols.h"
@@ -62,15 +62,19 @@ namespace Pinetime {
6262
lv_obj_t* btnVolDown;
6363
lv_obj_t* btnVolUp;
6464

65-
lv_obj_t* buttons[5];
65+
std::array<lv_obj_t*, 5> GetButtons() {
66+
return {btnPrev, btnPlayPause, btnNext, btnVolDown, btnVolUp};
67+
}
6668

6769
lv_obj_t* txtBtnPrev;
6870
lv_obj_t* txtPlayPause;
6971
lv_obj_t* txtBtnNext;
7072
lv_obj_t* txtVolDown;
7173
lv_obj_t* txtVolUp;
7274

73-
lv_obj_t* controlLabels[5];
75+
std::array<lv_obj_t*, 5> GetButtonLabels() {
76+
return {txtBtnPrev, txtPlayPause, txtBtnNext, txtVolDown, txtVolUp};
77+
}
7478

7579
lv_obj_t* txtArtist;
7680
lv_obj_t* txtTrack;
@@ -96,7 +100,7 @@ namespace Pinetime {
96100

97101
lv_task_t* taskRefresh;
98102

99-
Utility::DirtyValue<bool> bleState {};
103+
Utility::DirtyValue<bool> bleState;
100104

101105
/** Watchapp */
102106
};

0 commit comments

Comments
 (0)