-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.cpp
More file actions
925 lines (781 loc) · 29.2 KB
/
ui.cpp
File metadata and controls
925 lines (781 loc) · 29.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
//================================================================================
// MP3 Music Player for CYD - User Interface
// Auther: embedded-kiddie (https://github.com/embedded-kiddie)
// Released under the GPLv3 (https://www.gnu.org/licenses/gpl-3.0.html)
// LVGL version: 9.2.2 and up
//================================================================================
#include "ui.h"
#include "json.hpp"
#include <string.h> // for strncpy(), strrchr()
////////////////////// GLOBAL VARIABLES /////////////////////
UI_State_t ui_state;
UI_Control_t ui_control;
UI_Setting_t ui_setting = {
.selectBacklight = 1, // 30 sec
};
////////////////////// LOCAL VARIABLES //////////////////////
#include "MP3Player.h"
static MP3Player player;
static MP3Tags_t id3tags;
static UI_State_t nextState;
static UI_Screen_t screenFrom;
//--------------------------------------------------------------------------------
// Auto saving flag
//--------------------------------------------------------------------------------
#define SAVE_SETTING 1
#define SAVE_DURATION 2
#define SAVE_FAVORITE 4
static uint8_t autoSaving = 0;
/////////////////////////// IMAGES //////////////////////////
#include "src/photos.h"
////////////////////////// UI LOOP //////////////////////////
#define ADDITIONAL_TASK_PERIOD 1000 // [msec]
#define DO_EVERY(period, prev) \
static uint32_t prev = 0; for (uint32_t now = millis(); now - prev >= period; prev = now)
////////////////////// LOCAL FUNCTIONS //////////////////////
//--------------------------------------------------------------------------------
// Display a covoer photo on SD or flash
//--------------------------------------------------------------------------------
static void display_photo(uint32_t playNo) {
static constexpr lv_style_const_prop_t style_prop_photo[] = {
LV_STYLE_CONST_SHADOW_WIDTH(10),
LV_STYLE_CONST_SHADOW_OFFSET_Y(5),
LV_STYLE_CONST_SHADOW_OPA(LV_OPA_40),
LV_STYLE_CONST_PROPS_END
};
static LV_STYLE_CONST_INIT(style_photo, (void*)style_prop_photo);
// Display an image file on SD card
char buf[BUF_SIZE], *ptr;
buf[0] = MY_FS_ARDUINO_SD_LETTER;
buf[1] = ':';
// Skip drive letter "S:"
std::string path = player.GetFilePath(playNo);
strncpy(&buf[2], path.c_str(), sizeof(buf) - 2);
buf[sizeof(buf) - 1] = '\0';
// @photo.jpg
if (ptr = strrchr(buf, '/')) {
strcpy(ptr + 1, ALBUM_PHOTO_FILE ALBUM_PHOTO_EXT);
if (SD.exists(buf + 2)) {
lv_image_set_src(ui_AlbumImage, buf);
lv_obj_add_style(ui_AlbumImage, &style_photo, 0);
return;
}
}
// title.jpg
strncpy(&buf[2], path.c_str(), sizeof(buf) - 2);
if (ptr = strrchr(buf, '.')) {
strncpy(ptr, ALBUM_PHOTO_EXT, sizeof(ALBUM_PHOTO_EXT));
if (SD.exists(buf + 2)) {
lv_image_set_src(ui_AlbumImage, buf);
lv_obj_add_style(ui_AlbumImage, &style_photo, 0);
return;
}
}
#ifdef _PHOTOS_H_
#if true
// Display an image file on flash ROM at random
int pictNo = millis() % (N_PHOTOS - 1) + 1;
lv_image_set_src(ui_AlbumImage, photos[pictNo]);
lv_obj_add_style(ui_AlbumImage, &style_photo, 0);
#else
// Display an image specified by the number in @photo.txt
int pictNo = player.GetPhotoNo(playNo);
if (0 < pictNo && pictNo < N_PHOTOS) {
lv_image_set_src(ui_AlbumImage, photos[pictNo]);
lv_obj_add_style(ui_AlbumImage, &style_photo, 0);
} else {
lv_image_set_src (ui_AlbumImage, &img_album);
lv_obj_remove_style (ui_AlbumImage, &style_photo, 0);
}
#endif
#endif
}
//--------------------------------------------------------------------------------
// Control player: auto, next/previous or stop/continuous
//--------------------------------------------------------------------------------
static bool play_auto(void) {
return player.AutoPlay();
}
static bool play_next(bool next) {
bool ret = true;
if (ui_ScreenPlayList) {
ui_list_update_cell(ui_control.focusNo, false);
ui_list_update_icon(ui_control.playNo, false);
}
// When an audio file that is not favorite is selected in the playlist
if (bitRead(ui_setting.favorite, 7)) {
bitClear(ui_setting.favorite, 7);
ret = play_auto();
}
else {
if (ui_setting.favorite) {
ret = player.NextSelected(next, (ui_setting.repeat ? true : false));
} else if (next) {
player.PlayNext();
} else {
player.PlayPrev();
}
bitClear(ui_setting.repeat, 7); // Clear bit set by ui_event_Button{Next|Prev}()
ui_set_playNo(player.GetPlayNo()); // Clear ID3 tag, set ui_control and display photo
}
if (ui_ScreenPlayList) {
ui_list_update_play(ui_control.playNo, true);
}
return ret;
}
static void play_stop(void) {
lv_obj_set_state(ui_ButtonPlay, LV_STATE_CHECKED, false);
player.StopPlay();
}
//--------------------------------------------------------------------------------
// Update bar and label according to elapsed time
//--------------------------------------------------------------------------------
static void update_elapsed_time(void) {
uint32_t duration = audioGetDuration();
uint32_t elapsed = audioGetElapsedTime();
if (duration < elapsed) {
duration = elapsed;
}
if (duration) {
id3tags.meta.duration = duration;
}
lv_slider_set_range(ui_ElapsedBar, 0, duration);
lv_slider_set_value(ui_ElapsedBar, elapsed, LV_ANIM_OFF);
lv_label_set_text_fmt(ui_ElapsedStart, "%" LV_PRIu32 ":%02" LV_PRIu32, elapsed / 60, elapsed % 60);
lv_label_set_text_fmt(ui_ElapsedEnd, "%" LV_PRIu32 ":%02" LV_PRIu32, duration / 60, duration % 60);
// In case ID3 tags are empty
if (id3tags.title.empty() || id3tags.artist.empty() || id3tags.album.empty()) {
player.GetID3Tags(player.GetPlayNo(), id3tags);
ui_state = UI_STATE_ID3;
}
}
//--------------------------------------------------------------------------------
// Save / Load ui_setting to / from SD card
//--------------------------------------------------------------------------------
static bool save_setting(void) {
// Save partition
File fd = SD.open(MP3_ROOT_PATH MP3_SETTING_FILE, FILE_WRITE);
if (fd) {
fd.write((uint8_t *)&ui_setting, sizeof(ui_setting));
fd.close();
return true;
} else {
player.SetError("can't save " MP3_ROOT_PATH MP3_SETTING_FILE);
return false;
}
}
static bool load_setting(void) {
bool save = false;
if (SD.exists(MP3_ROOT_PATH MP3_SETTING_FILE)) {
File fd = SD.open(MP3_ROOT_PATH MP3_SETTING_FILE, FILE_READ);
if (!fd) {
player.SetError("can't load " MP3_ROOT_PATH MP3_SETTING_FILE);
return false;
}
// Save the sleep timer setting
uint8_t sleepTimer = ui_setting.selectSleepTimer;
fd.read((uint8_t *)&ui_setting, sizeof(ui_setting));
fd.close();
// Restore the setting
ui_setting.selectSleepTimer = sleepTimer;
bitClear(ui_setting.repeat, 7);
bitClear(ui_setting.favorite, 7);
}
// For the first time
else {
save = true;
}
// Check if the partition exists
char buf[BUF_SIZE];
uint8_t partition_max = 0;
for (int i = 1; i <= PARTITION_MAX; i++) {
snprintf(buf, sizeof(buf), MP3_ROOT_PATH PARTITION_PATH, i);
buf[sizeof(buf) - 1] = '\0';
if (SD.exists(buf)) {
partition_max = i;
} else {
break;
}
}
if (ui_setting.partition_max != partition_max) {
ui_setting.partition_max = partition_max;
save = true;
}
// Just in case
if (ui_setting.partition_id > ui_setting.partition_max) {
ui_setting.partition_id = 0;
save = true;
}
if (save && save_setting() == false) {
return false;
}
// Change the root folder with the current partition
if (ui_setting.partition_max && ui_setting.partition_id) {
snprintf(buf, sizeof(buf), PARTITION_PATH, ui_setting.partition_id);
buf[sizeof(buf) - 1] = '\0';
player.SetSubDir(buf);
} else {
player.SetSubDir("");
}
// Update UI
lv_obj_set_state(ui_Repeat, LV_STATE_CHECKED, ui_setting.repeat );
lv_obj_set_state(ui_Shuffle, LV_STATE_CHECKED, ui_setting.shuffle );
lv_obj_set_state(ui_Favorite, LV_STATE_CHECKED, ui_setting.favorite);
// Rewind
ui_control.playNo = ui_control.focusNo = 0;
return true;
}
//--------------------------------------------------------------------------------
// Update data to be automatically saved when playback reaches the end of file
//--------------------------------------------------------------------------------
static bool auto_saving(void) {
if (autoSaving) {
bool pause = false;
if (player.IsPlaying()) {
pause = true;
player.PauseResume();
}
// 1. Update all favorites that have been modified during playback
if (autoSaving & SAVE_FAVORITE) {
// Prevent assert() from being fired by node reordering
lv_obj_t *screen = lv_screen_active();
if (screen == ui_ScreenAlbumList) {
player.m_tree->traverse_node();
}
if (player.UpdateMetaData()) {
autoSaving ^= SAVE_FAVORITE;
}
if (screen == ui_ScreenAlbumList) {
player.m_tree->traverse_preorder();
}
}
// 2. Update the playback duration at the end of file
if (autoSaving & SAVE_DURATION) {
uint32_t playNo = player.GetPlayNo();
ui_list_update_duration(playNo, id3tags.meta.duration);
MP3Meta_t meta;
player.GetMetaData(playNo, &meta);
id3tags.meta.selected = meta.selected;
if (player.PutMetaData(playNo, &id3tags.meta)) {
autoSaving ^= SAVE_DURATION;
}
}
// 3. Update favorite and repeat when they are changed during playback
if (autoSaving & SAVE_SETTING) {
if (save_setting()) {
autoSaving ^= SAVE_SETTING;
}
}
if (pause) {
player.PauseResume();
}
if (autoSaving) {
autoSaving = 0; // Avoid infinite loop
return false;
}
}
return true;
}
//--------------------------------------------------------------------------------
// Scan SD card for audio files and create a playlist / album list
//--------------------------------------------------------------------------------
static bool create_playlist(void) {
// Setup UI setting
if (load_setting() == false) {
return false;
}
// Scan SD card for album folders
if (player.ScanAlbumDirs()) {
// Load album list
ui_album_load((void*)player.m_tree);
// Scan audio files base on album list
if (player.ScanAudioFiles(ui_setting.shuffle)) {
ui_set_playNo(ui_control.playNo);
return true;
}
}
return false;
}
static bool reset_playlist(void) {
// Stop playback before saving ui_setting to avoid conflict with SD access
play_stop();
player.DeleteNodeTree(); // delete m_tree
player.ClearAudioFiles(); // m_list.clear()
return save_setting(); // save ui_setting
}
static void reset_album(void) {
reset_playlist();
load_setting();
player.ScanAlbumDirs();
ui_album_create((void*)player.m_tree, true); // Load json data from SD card
}
//--------------------------------------------------------------------------------
// Check if the currently playing audio file is favorite
//--------------------------------------------------------------------------------
static bool check_favorite(void) {
return !ui_setting.favorite || player.IsPlaying() || player.IsSelected();
}
//--------------------------------------------------------------------------------
// Asynchronous function to reduce delays during screen transitions
//--------------------------------------------------------------------------------
static void stop_async(void *user_data) {
if (ui_state != UI_STATE_IDLE && ui_state != UI_STATE_ALBUM) {
ui_state = UI_STATE_STOP;
}
}
//--------------------------------------------------------------------------------
// Helper function for screen transition
//--------------------------------------------------------------------------------
static void change_screen(UI_Screen_t from, lv_obj_t ** screen, lv_screen_load_anim_t fademode, void (*screen_init)(void)) {
screenFrom = from;
if(*screen == NULL) {
screen_init();
}
lv_screen_load_anim(*screen, fademode, 500, 0, false);
}
////////////////////// GLOBAL FUNCTIONS /////////////////////
//********************************************************************************
// SCREEN: ui_ScreenMain event handlers
//********************************************************************************
void ui_event_ScreenMain(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_GESTURE);
lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_active());
if (dir == LV_DIR_RIGHT) {
change_screen(UI_SCREEN_MAIN, &ui_ScreenAlbumList, LV_SCR_LOAD_ANIM_MOVE_RIGHT, &ui_ScreenAlbumList_screen_init);
}
else if (dir == LV_DIR_LEFT) {
change_screen(UI_SCREEN_MAIN, &ui_ScreenPlayList, LV_SCR_LOAD_ANIM_MOVE_LEFT, &ui_ScreenPlayList_screen_init);
ui_list_focus_playing(player.GetPlayNo());
}
else if (dir == LV_DIR_TOP || dir == LV_DIR_BOTTOM) {
lv_screen_load_anim_t anim = (dir == LV_DIR_TOP ? LV_SCR_LOAD_ANIM_MOVE_TOP : LV_SCR_LOAD_ANIM_MOVE_BOTTOM);
change_screen(UI_SCREEN_MAIN, &ui_ScreenSetting, anim, &ui_ScreenSetting_screen_init);
}
}
void ui_event_GoToAlbumList(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
change_screen(UI_SCREEN_MAIN, &ui_ScreenAlbumList, LV_SCR_LOAD_ANIM_MOVE_RIGHT, &ui_ScreenAlbumList_screen_init);
}
void ui_event_GoToPlayList(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
change_screen(UI_SCREEN_MAIN, &ui_ScreenPlayList, LV_SCR_LOAD_ANIM_MOVE_LEFT, &ui_ScreenPlayList_screen_init);
}
void ui_event_GoToSetting(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
change_screen(UI_SCREEN_MAIN, &ui_ScreenSetting, LV_SCR_LOAD_ANIM_MOVE_BOTTOM, &ui_ScreenSetting_screen_init);
}
void ui_event_Favorite(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
lv_obj_t *obj = lv_event_get_target_obj(e);
ui_setting.favorite = (lv_obj_get_state(obj) & LV_STATE_CHECKED ? true : false);
// If unable to save, save at idle state
if (player.IsPlaying()) {
autoSaving |= SAVE_SETTING;
} else if (save_setting() == false) {
ui_state = UI_STATE_ERROR;
}
}
void ui_event_Repeat(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
lv_obj_t *obj = lv_event_get_target_obj(e);
ui_setting.repeat = (uint8_t)(lv_obj_get_state(obj) & LV_STATE_CHECKED ? true : false);
// If unable to save, save at idle state
if (player.IsPlaying()) {
autoSaving |= SAVE_SETTING;
} else if (save_setting() == false) {
ui_state = UI_STATE_ERROR;
}
}
void ui_event_Shuffle(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
lv_obj_t *obj = lv_event_get_target_obj(e);
ui_setting.shuffle = (lv_obj_get_state(obj) & LV_STATE_CHECKED ? true : false);
player.StopPlay();
player.ClearAudioFiles();
if (save_setting()) {
ui_state = UI_STATE_START;
} else {
ui_state = UI_STATE_ERROR;
}
}
void ui_event_ButtonPlay(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
lv_state_t state = lv_obj_get_state(ui_ButtonPlay);
ui_state = (state & LV_STATE_CHECKED ? UI_STATE_RESUME : UI_STATE_PAUSE);
}
void ui_event_ButtonNext(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
ui_state = UI_STATE_NEXT;
bitSet(ui_setting.repeat, 7); // Force to set the bit temporarily
}
void ui_event_ButtonPrev(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
ui_state = UI_STATE_PREV;
bitSet(ui_setting.repeat, 7); // Force to set the bit temporarily
}
void ui_event_VolumeMax(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
int vol = lv_slider_get_value(ui_Volume) + 1;
vol = constrain(vol, MP3_VOLUME_MIN, MP3_VOLUME_MAX);
lv_slider_set_value(ui_Volume, vol, LV_ANIM_OFF);
player.SetVolume(vol);
}
void ui_event_VolumeMin(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
int vol = lv_slider_get_value(ui_Volume) - 1;
vol = constrain(vol, MP3_VOLUME_MIN, MP3_VOLUME_MAX);
lv_slider_set_value(ui_Volume, vol, LV_ANIM_OFF);
player.SetVolume(vol);
}
void ui_event_Volume(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_VALUE_CHANGED);
int vol = lv_slider_get_value(ui_Volume);
player.SetVolume(vol);
}
void ui_event_ElapsedBar(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_VALUE_CHANGED);
if (player.IsPlaying()) {
audioSetElapsedTime(lv_slider_get_value(ui_ElapsedBar));
}
}
//********************************************************************************
// SCREEN: ui_ScreenPlayList event handlers
//********************************************************************************
void ui_event_ScreenPlayList(lv_event_t *e) {
lv_event_code_t event_code = lv_event_get_code(e);
DBG_ASSERT(
event_code == LV_EVENT_GESTURE ||
event_code == LV_EVENT_SCREEN_UNLOADED
);
if (event_code == LV_EVENT_GESTURE) {
lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_active());
lv_screen_load_anim_t anim = dir == LV_DIR_RIGHT ? LV_SCR_LOAD_ANIM_MOVE_RIGHT : LV_SCR_LOAD_ANIM_MOVE_LEFT;
change_screen(UI_SCREEN_PLAY_LIST, &ui_ScreenMain, anim, &ui_ScreenMain_screen_init);
}
else if (event_code == LV_EVENT_SCREEN_UNLOADED) {
ui_ScreenPlayList_screen_deinit();
}
}
void ui_event_PlayList_Play(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
ui_state = (ui_state == UI_STATE_PLAY ? UI_STATE_PAUSE : UI_STATE_RESUME);
lv_obj_set_state(ui_ButtonPlay, LV_STATE_CHECKED, ui_state == UI_STATE_PAUSE ? false : true);
}
void ui_event_PlayList_Heart(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_CLICKED);
MP3Meta_t meta;
uint32_t track_id = (uint32_t)lv_event_get_user_data(e);
player.GetMetaData(track_id, &meta);
lv_obj_t *obj = (lv_obj_t*)lv_event_get_current_target(e);
meta.selected = ui_list_get_heart_state(track_id);
// If unable to save, save at idle state
if (!player.PutMetaData(track_id, &meta)) {
autoSaving |= SAVE_FAVORITE;
}
}
//********************************************************************************
// SCREEN: ui_ScreenAlbumList event handlers
//********************************************************************************
void ui_event_ScreenAlbumList(lv_event_t *e) {
lv_event_code_t event_code = lv_event_get_code(e);
DBG_ASSERT(
event_code == LV_EVENT_CLICKED ||
event_code == LV_EVENT_GESTURE ||
event_code == LV_EVENT_SCREEN_LOADED ||
event_code == LV_EVENT_SCREEN_UNLOADED
);
if (event_code == LV_EVENT_CLICKED) {
if (lv_event_get_user_data(e)) {
change_screen(UI_SCREEN_ALBUM_LIST, &ui_ScreenSetting, LV_SCR_LOAD_ANIM_MOVE_BOTTOM, &ui_ScreenSetting_screen_init);
} else {
change_screen(UI_SCREEN_ALBUM_LIST, &ui_ScreenMain, LV_SCR_LOAD_ANIM_MOVE_LEFT, &ui_ScreenMain_screen_init);
}
}
else if (event_code == LV_EVENT_GESTURE) {
lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_active());
if (dir == LV_DIR_RIGHT || dir == LV_DIR_LEFT) {
lv_screen_load_anim_t anim = (dir == LV_DIR_RIGHT ? LV_SCR_LOAD_ANIM_MOVE_RIGHT : LV_SCR_LOAD_ANIM_MOVE_LEFT);
change_screen(UI_SCREEN_ALBUM_LIST, &ui_ScreenMain, anim, &ui_ScreenMain_screen_init);
}
else if (dir == LV_DIR_TOP || dir == LV_DIR_BOTTOM) {
lv_screen_load_anim_t anim = (dir == LV_DIR_TOP ? LV_SCR_LOAD_ANIM_MOVE_TOP : LV_SCR_LOAD_ANIM_MOVE_BOTTOM);
change_screen(UI_SCREEN_ALBUM_LIST, &ui_ScreenSetting, anim, &ui_ScreenSetting_screen_init);
}
}
else if (event_code == LV_EVENT_SCREEN_LOADED) {
// Increase free memory
// lv_fs_clear_cache(); // sdfs.{h|cpp}
// Create the album list (No need to access SD card)
ui_album_create((void*)player.m_tree, false);
// Stop playback to avoid conflict with SD access (async required)
lv_async_call(stop_async, NULL);
}
else if (event_code == LV_EVENT_SCREEN_UNLOADED) {
ui_ScreenAlbumList_screen_deinit();
ui_state = UI_STATE_CLEAR;
}
}
//********************************************************************************
// SCREEN: ui_ScreenSetting event handlers
//********************************************************************************
void ui_event_ScreenSetting(lv_event_t *e) {
lv_event_code_t event_code = lv_event_get_code(e);
DBG_ASSERT(
event_code == LV_EVENT_CLICKED ||
event_code == LV_EVENT_GESTURE ||
event_code == LV_EVENT_SCREEN_LOADED ||
event_code == LV_EVENT_SCREEN_UNLOADED
);
if (event_code == LV_EVENT_CLICKED) {
if (screenFrom == UI_SCREEN_MAIN) {
change_screen(UI_SCREEN_SETTING, &ui_ScreenMain, LV_SCR_LOAD_ANIM_MOVE_TOP, &ui_ScreenMain_screen_init);
} else if (screenFrom == UI_SCREEN_ALBUM_LIST) {
change_screen(UI_SCREEN_SETTING, &ui_ScreenAlbumList, LV_SCR_LOAD_ANIM_MOVE_TOP, &ui_ScreenAlbumList_screen_init);
}
}
else if (event_code == LV_EVENT_GESTURE) {
lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_active());
lv_screen_load_anim_t anim = (dir == LV_DIR_TOP ? LV_SCR_LOAD_ANIM_MOVE_TOP : LV_SCR_LOAD_ANIM_MOVE_BOTTOM);
if (screenFrom == UI_SCREEN_MAIN) {
change_screen(UI_SCREEN_SETTING, &ui_ScreenMain, anim, &ui_ScreenMain_screen_init);
} else if (screenFrom == UI_SCREEN_ALBUM_LIST) {
change_screen(UI_SCREEN_SETTING, &ui_ScreenAlbumList, anim, &ui_ScreenAlbumList_screen_init);
}
}
else if (event_code == LV_EVENT_SCREEN_LOADED) {
if (screenFrom == UI_SCREEN_ALBUM_LIST) {
lv_async_call(stop_async, NULL);
}
}
else if (event_code == LV_EVENT_SCREEN_UNLOADED) {
ui_ScreenSetting_screen_deinit();
if (ui_setting.partition_max) {
// Update the partition ID if a different ID is selected
uint8_t partition_id = *(uint8_t*)lv_event_get_user_data(e);
if (ui_setting.partition_id != partition_id) {
ui_setting.partition_id = partition_id;
// Change the procedure depending on the screen
lv_obj_t *screen = lv_screen_active();
if (screen == ui_ScreenMain) {
ui_state = UI_STATE_RESET; // Reset and create a new playlist
} else if (screen == ui_ScreenAlbumList) {
ui_state = UI_STATE_ALBUM; // Reset and create a new album list
}
}
}
}
}
void ui_event_Setting_Backlight(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_VALUE_CHANGED);
lv_obj_t * obj = lv_event_get_target_obj(e);
ui_setting.selectBacklight = lv_roller_get_selected(obj);
ui_setting_set_backlight();
// If unable to save, save at idle state
if (player.IsPlaying()) {
autoSaving |= SAVE_SETTING;
} else if (save_setting() == false) {
ui_state = UI_STATE_ERROR;
}
}
void ui_event_Setting_SleepTimer(lv_event_t *e) {
DBG_ASSERT(lv_event_get_code(e) == LV_EVENT_VALUE_CHANGED);
lv_obj_t * obj = lv_event_get_target_obj(e);
ui_setting.selectSleepTimer = lv_roller_get_selected(obj);
ui_setting_set_sleeptime();
ui_control.sleepStart = millis();
}
//--------------------------------------------------------------------------------
// Redraw the display panel when waking up from sleep
//--------------------------------------------------------------------------------
void ui_redisplay(void) {
lv_display_trigger_activity(NULL);
lv_screen_load(lv_screen_active());
}
//--------------------------------------------------------------------------------
// Start to play with the specified track
//--------------------------------------------------------------------------------
void ui_set_playNo(uint32_t track_id, bool event_in_playlist) {
// Clear ID3 tag
id3tags.title.clear();
id3tags.album.clear();
id3tags.artist.clear();
// When an audio file that is not favorite is selected in the playlist
if (event_in_playlist && ui_setting.favorite) {
MP3Tags_t tags;
player.GetID3Tags(track_id, tags);
if (!tags.meta.selected) {
bitSet(ui_setting.favorite, 7);
}
}
// Start the specified track to play
player.SetPlayNo(track_id);
// Update ui_control
ui_control.playNo = ui_control.focusNo = track_id;
display_photo(track_id);
// Update the look of the play button
if (ui_state != UI_STATE_PLAY) {
lv_obj_set_state(ui_ButtonPlay, LV_STATE_CHECKED, true);
ui_state = UI_STATE_PLAY;
}
}
//--------------------------------------------------------------------------------
// Get the latest information on MP3Player
//--------------------------------------------------------------------------------
const uint32_t ui_get_playNo(void) {
return player.GetPlayNo();
}
const uint32_t ui_get_counts(void) {
return player.GetCounts();
}
//--------------------------------------------------------------------------------
// Get ID3 tags (title, album, artist) from the file specified by id
//--------------------------------------------------------------------------------
void ui_get_id3tags(uint32_t track_id, MP3Tags_t &tags) {
player.GetID3Tags(track_id, tags);
}
//--------------------------------------------------------------------------------
// Optional functions for audio-I2S (defined in CYD_Audio.h as a weak function)
// Note: This function is called for each tag in the context of CORE 1.
//--------------------------------------------------------------------------------
void audio_id3data(const char *info) {
// Avoid a race condition with ui_state set by audio_eof_mp3()
if (ui_state == UI_STATE_PLAY) {
char *p;
if (p = strstr(info, "Title: ")) {
id3tags.title = p + 7;
} else
if (p = strstr(info, "Artist: ")) {
id3tags.artist = p + 8;
} else
if (p = strstr(info, "Album: ")) {
id3tags.album = p + 7;
ui_state = UI_STATE_ID3;
}
}
}
void audio_eof_mp3(const char *info) {
// Note: When the elapse bar is operated by hand, the duration will be shifted.
MP3Meta_t meta;
player.GetMetaData(player.GetPlayNo(), &meta);
if (meta.duration == 0 || abs(meta.duration - id3tags.meta.duration) >= 5 /* [sec] */) {
autoSaving |= SAVE_DURATION;
}
// Transition to UI_STATE_EOF once
ui_state = UI_STATE_EOF;
// Determine the next state
if (!player.IsLastSong(ui_setting.favorite) || ui_setting.repeat) {
nextState = UI_STATE_NEXT;
} else {
nextState = UI_STATE_STOP;
}
}
//////////////////// UI STATE CONTROLLER ////////////////////
void ui_init(void) {
ui_setting_set_backlight();
ui_setting_set_sleeptime();
ui_ScreenMain_screen_init();
lv_screen_load(ui_ScreenMain);
audioInit();
ui_state = UI_STATE_INIT;
}
//--------------------------------------------------------------------------------
// A finite state machine that controls the overall operation
// The steady state can be either "UI_STATE_PLAY" or "UI_STATE_IDLE",
// anything else is just a transient state that works as a command.
//--------------------------------------------------------------------------------
UI_State_t ui_loop(void) {
switch (ui_state) {
case UI_STATE_INIT:
if (player.begin(MP3_ROOT_PATH, MP3_VOLUME_INI)) {
lv_slider_set_value(ui_Volume, MP3_VOLUME_INI, LV_ANIM_OFF);
ui_state = UI_STATE_START;
} else {
ui_state = UI_STATE_ERROR;
}
break;
case UI_STATE_START:
if (create_playlist()) {
ui_state = UI_STATE_PLAY;
} else {
ui_state = UI_STATE_ERROR;
}
break;
case UI_STATE_PLAY:
if (!check_favorite()) {
ui_state = UI_STATE_NEXT;
} else if (!play_auto()) {
log_e(player.GetError());
ui_state = UI_STATE_NEXT;
}
break;
case UI_STATE_STOP:
play_stop();
ui_state = UI_STATE_IDLE;
break;
case UI_STATE_PAUSE:
player.PauseResume();
ui_state = UI_STATE_IDLE;
break;
case UI_STATE_RESUME:
player.PauseResume();
ui_state = UI_STATE_PLAY;
break;
case UI_STATE_NEXT:
ui_state = play_next(true) ? UI_STATE_PLAY : UI_STATE_STOP;
break;
case UI_STATE_PREV:
ui_state = play_next(false) ? UI_STATE_PLAY : UI_STATE_STOP;
break;
case UI_STATE_ID3:
lv_label_set_text_fmt(ui_MusicTitle, "%s %s / %s / %s",
LV_SYMBOL_AUDIO, id3tags.title.c_str(), id3tags.artist.c_str(), id3tags.album.c_str()
);
ui_state = UI_STATE_PLAY;
break;
case UI_STATE_EOF:
ui_state = auto_saving() ? nextState : UI_STATE_ERROR;
break;
case UI_STATE_CLEAR:
play_stop();
player.ClearAudioFiles();
ui_state = UI_STATE_START;
break;
case UI_STATE_RESET:
ui_state = reset_playlist() ? UI_STATE_START : UI_STATE_ERROR;
break;
case UI_STATE_ALBUM:
reset_album();
ui_state = UI_STATE_IDLE;
break;
case UI_STATE_ERROR:
lv_label_set_text_fmt(ui_MusicTitle, "%s %s", LV_SYMBOL_WARNING, player.GetError());
ui_state = UI_STATE_IDLE;
case UI_STATE_IDLE:
default:
if (!auto_saving()) {
ui_state = UI_STATE_ERROR;
}
break;
}
UI_State_t ret = UI_STATE_AWAKE;
// Additional periodic task
DO_EVERY(ADDITIONAL_TASK_PERIOD, task1Time) {
// update elapsed time
if (player.IsPlaying()) {
update_elapsed_time();
}
// deep sleep
if (ui_setting.selectSleepTimer) {
if (millis() - ui_control.sleepStart > ui_control.sleepTimer) {
ret = ui_state = UI_STATE_SLEEP; // enter deep sleep
}
}
// Backlight control according to the duration of non-operation
if (ui_setting.selectBacklight && ret == UI_STATE_AWAKE) {
if (lv_disp_get_inactive_time(NULL) > ui_control.backlightTimer) {
ret = UI_STATE_BLOFF; // turn backlight off
}
}
}
return ret;
}