Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
2887 lines (2612 loc) · 139 KB
/
Copy pathmain.cpp
File metadata and controls
2887 lines (2612 loc) · 139 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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////
/// Made by: Sanchit Minda() | Github/sanchitminda
/// Fully Functional OOP & State Machine Architecture
//////////////////////////////////////////////
#include <Arduino.h>
#include <vector>
#include <M5Unified.h>
#include <M5Cardputer.h>
#include <SPI.h>
#include <Preferences.h>
#include <nvs_flash.h>
#include <WiFi.h>
#include <WebServer.h>
#include "USB.h"
#include "USBMSC.h"
// Audio Libraries
#include <AudioOutput.h>
#include <AudioFileSourceSD.h>
#include <AudioFileSourceID3.h>
#include <AudioGeneratorMP3.h>
#include <AudioGeneratorFLAC.h>
#include <AudioGeneratorAAC.h>
#include <AudioGeneratorWAV.h>
#include <AudioFileSourceBuffer.h>
#include "equalizer.h"
// ==========================================
// CONSTANTS & CONFIG
// ==========================================
#define SD_SPI_SCK_PIN 40
#define SD_SPI_MISO_PIN 39
#define SD_SPI_MOSI_PIN 14
#define SD_SPI_CS_PIN 12
#define PLAYLIST_FILE "/playlist.txt" // "All Music" flat cache
#define CONFIG_FILE "/config.txt"
#define PLAYLIST_WIDTH 120
#define ROW_HEIGHT 15
#define HEADER_HEIGHT 20
#define BOTTOM_BAR_HEIGHT 18
#define MAX_VISIBLE_ROWS 6
// --- DYNAMIC COLORS & THEMES ---
uint16_t C_BG_DARK, C_BG_LIGHT, C_HEADER, C_ACCENT, C_PLAYING, C_HIGHLIGHT, C_TEXT_MAIN, C_TEXT_DIM;
const int NUM_THEMES = 4;
const char* themeLabels[] = { "Gunmetal Blue", "Cyberpunk", "Retro Amber", "Hacker Green" };
const int NUM_VIS_MODES = 4;
const char* visModeLabels[] = { "Classic Bars", "Waveform Line", "Circular Spikes", "OFF" };
void applyTheme(int index) {
switch(index) {
case 0: // Gunmetal Blue (Original)
C_BG_DARK = 0x1002; C_BG_LIGHT = 0x2124; C_HEADER = 0x18E3;
C_ACCENT = 0x05BF; C_PLAYING = 0x07E0; C_HIGHLIGHT = 0xF81F;
C_TEXT_MAIN = 0xFFFF; C_TEXT_DIM = 0x9492;
break;
case 1: // Cyberpunk
C_BG_DARK = 0x0803; C_BG_LIGHT = 0x1866; C_HEADER = 0xA013;
C_ACCENT = 0x07FF; C_PLAYING = 0xFFE0; C_HIGHLIGHT = 0xF800;
C_TEXT_MAIN = 0xFFFF; C_TEXT_DIM = 0x7BEF;
break;
case 2: // Retro Amber
C_BG_DARK = TFT_BLACK; C_BG_LIGHT = 0x2104; C_HEADER = 0x6A00;
C_ACCENT = 0xFDA0; C_PLAYING = TFT_ORANGE; C_HIGHLIGHT = TFT_RED;
C_TEXT_MAIN = 0xFEA0; C_TEXT_DIM = 0xA340;
break;
case 3: // Hacker Green
C_BG_DARK = 0x0000; C_BG_LIGHT = 0x0180; C_HEADER = 0x0320;
C_ACCENT = 0x07E0; C_PLAYING = 0x07FF; C_HIGHLIGHT = TFT_WHITE;
C_TEXT_MAIN = 0x07E0; C_TEXT_DIM = 0x03E0;
break;
}
}
enum LoopState { NO_LOOP, LOOP_ALL, LOOP_ONE };
enum UIState { UI_PLAYER, UI_SETTINGS, UI_HELP, UI_WIFI_SCAN, UI_TEXT_INPUT, UI_FOLDER_SELECT, UI_SEARCH, UI_EQ };
const uint32_t sampleRateValues[] = { 44100, 48000, 88200, 96000, 128000 };
const char* sampleRateLabels[] = { "44.1k", "48k", "88.2k", "96k", "128k" };
const long timeoutValues[] = { 0, 30000, 60000, 120000, 300000 };
const char* timeoutLabels[] = { "Always On", "30 Sec", "1 Min", "2 Min", "5 Min" };
const char* powerModeLabels[] = { "OFF", "BASIC (160)", "ULTRA (80)" };
const char* helpLines[] = {
"--- MUSIC PLAYER ---",
"Enter: Play / Pause",
"; / . : Scroll / Navigate",
"[ / ] : Volume - / +",
"N / B : Next / Prev Song",
"< / ? : Prev / Next Folder",
"/ / , : Seek +Xs / -Xs",
"S: Search F: Shuffle",
"Esc / ` : Settings",
"V: Visualizer",
"E: Equalizer (EQ)",
"I:print Close Help",
"--- SMART FEATURES ---",
"Web UI: Enable Wi-Fi in",
"settings to access.",
"Power Saver: Underclock",
"CPU to save battery life.",
"",
"--- POCKET MODE ---",
"Btn A (1 Click): Play/Pause",
"Btn A (2 Clicks): Next",
"Btn A (3 Clicks): Prev",
"Press any key to wake screen",
"--- EQUALIZER ---",
"; / . : Select Band",
", / / : -1dB / +1dB",
"R: Reset T: Toggle",
"`: Save & Exit EQ",
"--- ABOUT ---",
"Made with <3 by SaM",
"Sit back, relax, and",
"enjoy the music!",
"GH: github.com/sanchitminda",
"--- EQ FORK ---",
"7-band EQ by silasgubi",
"github.com/silasgubi/",
"MP3PlayerForM5CardputerEQ",
"Press E on player screen!"
};
const int numHelpLines = 40;
const int numSettings = 19; // +1 for Playlist Mode +1 for Fullscreen OFF Vis + Language + Font Size
// ==========================================
// GLOBALS
// ==========================================
struct Settings {
int brightness = 100;
int themeIndex = 0;
int visMode = 0;
int timeoutIndex = 0;
bool resumePlay = true;
int spkRateIndex = 4;
int lastIndex = 0;
uint32_t lastPos = 0;
bool wifiEnabled = false;
String wifiSSID = "";
String wifiPass = "";
bool isAPMode = false;
String apSSID = "Cardputer";
String apPass = "12345678";
int powerSaverMode = 0;
int seek = 0;
bool offVisFullscreen = false;
String currentFolder = ""; // "" = All Music; "/FolderName" = specific folder
String languageCode = "en"; // i18n language code, e.g. "en", "ja"
int uiFontSizeIndex = 0; // 0=Small(12), 1=Medium(16)
int8_t eqGain[EQ_BANDS] = {0};
bool eqEnabled = true;
};
Settings userSettings;
Preferences preferences;
WebServer server(80);
LGFX_Sprite visSprite(&M5Cardputer.Display);
LGFX_Sprite offVisSprite(&M5Cardputer.Display);
UIState currentState = UI_PLAYER;
unsigned long lastInputTime = 0;
bool isScreenOff = false;
// Text Input Globals
int textInputTarget = 0; // 0=STA Pass, 1=AP SSID, 2=AP Pass
String enteredText = "";
// Active playlist path (changes when user picks a folder)
String g_activePlaylist = PLAYLIST_FILE;
// Search globals
String g_searchQuery = "";
std::vector<int> g_searchResults; // indices into audioApp.songOffsets
int g_searchCursor = 0;
int g_searchScrollOffset = 0;
int g_offVisAnim = 0;
// ==========================================
// HARDWARE CLASSES (FFT & SPEAKER)
// ==========================================
#define FFT_SIZE 256
class fft_t {
float _wr[FFT_SIZE + 1], _wi[FFT_SIZE + 1], _fr[FFT_SIZE + 1], _fi[FFT_SIZE + 1];
uint16_t _br[FFT_SIZE + 1]; size_t _ie;
public:
fft_t(void) {
#ifndef M_PI
#define M_PI 3.141592653
#endif
_ie = logf( (float)FFT_SIZE ) / log(2.0) + 0.5;
static constexpr float omega = 2.0f * M_PI / FFT_SIZE;
static constexpr int s4 = FFT_SIZE / 4; static constexpr int s2 = FFT_SIZE / 2;
for ( int i = 1 ; i < s4 ; ++i) { float f = cosf(omega * i); _wi[s4 + i] = f; _wi[s4 - i] = f; _wr[i] = f; _wr[s2 - i] = -f; }
_wi[s4] = _wr[0] = 1; size_t je = 1; _br[0] = 0; _br[1] = FFT_SIZE / 2;
for ( size_t i = 0 ; i < _ie - 1 ; ++i ) { _br[ je << 1 ] = _br[ je ] >> 1; je = je << 1; for ( size_t j = 1 ; j < je ; ++j ) _br[je + j] = _br[je] + _br[j]; }
}
void exec(const int16_t* in) {
memset(_fi, 0, sizeof(_fi));
for ( size_t j = 0 ; j < FFT_SIZE / 2 ; ++j ) {
float basej = 0.25 * (1.0-_wr[j]); size_t r = FFT_SIZE - j - 1;
_fr[_br[j]] = basej * (in[j * 2] + in[j * 2 + 1]); _fr[_br[r]] = basej * (in[r * 2] + in[r * 2 + 1]);
}
size_t s = 1; size_t i = 0;
do { size_t ke = s; s <<= 1; size_t je = FFT_SIZE / s; size_t j = 0;
do { size_t k = 0;
do { size_t l = s * j + k; size_t m = ke * (2 * j + 1) + k; size_t p = je * k;
float Wxmr = _fr[m] * _wr[p] + _fi[m] * _wi[p], Wxmi = _fi[m] * _wr[p] - _fr[m] * _wi[p];
_fr[m] = _fr[l] - Wxmr; _fi[m] = _fi[l] - Wxmi; _fr[l] += Wxmr; _fi[l] += Wxmi;
} while ( ++k < ke);
} while ( ++j < je );
} while ( ++i < _ie );
}
uint32_t get(size_t index) { return (index < FFT_SIZE / 2) ? (uint32_t)sqrtf(_fr[ index ] * _fr[ index ] + _fi[ index ] * _fi[ index ]) : 0u; }
};
class AudioOutputM5Speaker : public AudioOutput {
public:
AudioOutputM5Speaker(m5::Speaker_Class* m5sound, uint8_t virtual_sound_channel = 0) { _m5sound = m5sound; _virtual_ch = virtual_sound_channel; }
virtual ~AudioOutputM5Speaker(void) {};
virtual bool begin(void) override { return true; }
virtual bool SetRate(int hz) override { hertz = hz; globalEQ.update((float)hz); return true; }
virtual bool ConsumeSample(int16_t sample[2]) override {
globalEQ.process(sample);
if (_tri_buffer_index < tri_buf_size) {
_tri_buffer[_tri_index][_tri_buffer_index] = sample[0]; _tri_buffer[_tri_index][_tri_buffer_index+1] = sample[1]; _tri_buffer_index += 2; return true;
}
flush(); return false;
}
virtual void flush(void) override {
if (_tri_buffer_index) { _m5sound->playRaw(_tri_buffer[_tri_index], _tri_buffer_index, hertz, true, 1, _virtual_ch); _tri_index = _tri_index < 2 ? _tri_index + 1 : 0; _tri_buffer_index = 0; }
}
virtual bool stop(void) override { flush(); _m5sound->stop(_virtual_ch); return true; }
const int16_t* getBuffer(void) const { return _tri_buffer[(_tri_index + 2) % 3]; }
protected:
m5::Speaker_Class* _m5sound; uint8_t _virtual_ch; static constexpr size_t tri_buf_size = 2048;
int16_t _tri_buffer[3][tri_buf_size]; size_t _tri_buffer_index = 0; size_t _tri_index = 0;
};
AudioOutputM5Speaker *out = nullptr;
static constexpr size_t WAVE_SIZE = 320;
static fft_t fft;
static int16_t raw_data[WAVE_SIZE * 2];
// ==========================================
// CONFIG MANAGER
// ==========================================
class ConfigManager {
public:
static void load() {
preferences.begin("sam_music", true);
userSettings.brightness = preferences.getInt("brightness", 100);
userSettings.timeoutIndex = preferences.getInt("timeoutIndex", 0);
userSettings.resumePlay = preferences.getBool("resumePlay", true);
userSettings.spkRateIndex = preferences.getInt("spkRate", 4);
userSettings.lastIndex = preferences.getInt("lastIndex", 0);
userSettings.lastPos = preferences.getUInt("lastPos", 0);
userSettings.wifiEnabled = preferences.getBool("wifiEnabled", false);
userSettings.wifiSSID = preferences.getString("wifiSSID", "");
userSettings.wifiPass = preferences.getString("wifiPass", "");
userSettings.isAPMode = preferences.getBool("isAPMode", false);
userSettings.apSSID = preferences.getString("apSSID", "Cardputer");
userSettings.apPass = preferences.getString("apPass", "12345678");
userSettings.powerSaverMode = preferences.getInt("powerMode", 0);
userSettings.themeIndex = preferences.getInt("themeIndex", 0);
userSettings.visMode = preferences.getInt("visMode", 0);
userSettings.seek = preferences.getInt("seek", 5);
userSettings.offVisFullscreen = preferences.getBool("offVisFull", false);
userSettings.currentFolder = preferences.getString("curFolder", "");
userSettings.languageCode = preferences.getString("langCode", "en");
userSettings.uiFontSizeIndex = preferences.getInt("uiFont", 0);
userSettings.eqEnabled = preferences.getBool("eqEnabled", true);
for (int i = 0; i < EQ_BANDS; i++) {
String key = "eq" + String(i);
userSettings.eqGain[i] = (int8_t)preferences.getInt(key.c_str(), 0);
}
preferences.end();
if(userSettings.apSSID.length() == 0) userSettings.apSSID = "Cardputer";
if(userSettings.apPass.length() < 8) userSettings.apPass = "12345678";
}
static void save(uint32_t currentPos = 0, int currentIndex = 0) {
if(currentPos > 0) { userSettings.lastPos = currentPos; userSettings.lastIndex = currentIndex; }
preferences.begin("sam_music", false);
preferences.putInt("brightness", userSettings.brightness);
preferences.putInt("timeoutIndex", userSettings.timeoutIndex);
preferences.putBool("resumePlay", userSettings.resumePlay);
preferences.putInt("spkRate", userSettings.spkRateIndex);
preferences.putInt("lastIndex", userSettings.lastIndex);
preferences.putUInt("lastPos", userSettings.lastPos);
preferences.putBool("wifiEnabled", userSettings.wifiEnabled);
preferences.putString("wifiSSID", userSettings.wifiSSID);
preferences.putString("wifiPass", userSettings.wifiPass);
preferences.putBool("isAPMode", userSettings.isAPMode);
preferences.putString("apSSID", userSettings.apSSID);
preferences.putString("apPass", userSettings.apPass);
preferences.putInt("powerMode", userSettings.powerSaverMode);
preferences.putInt("themeIndex", userSettings.themeIndex);
preferences.putInt("visMode", userSettings.visMode);
preferences.putInt("seek", userSettings.seek);
preferences.putBool("offVisFull", userSettings.offVisFullscreen);
preferences.putString("curFolder", userSettings.currentFolder);
preferences.putString("langCode", userSettings.languageCode);
preferences.putInt("uiFont", userSettings.uiFontSizeIndex);
preferences.putBool("eqEnabled", userSettings.eqEnabled);
for (int i = 0; i < EQ_BANDS; i++) {
String key = "eq" + String(i);
preferences.putInt(key.c_str(), (int)userSettings.eqGain[i]);
}
preferences.end();
}
static void exportToSD() {
if (SD.exists(CONFIG_FILE)) SD.remove(CONFIG_FILE);
File file = SD.open(CONFIG_FILE, FILE_WRITE);
if (file) {
file.println(userSettings.brightness); file.println(userSettings.timeoutIndex);
file.println(userSettings.resumePlay ? 1 : 0); file.println(userSettings.spkRateIndex);
file.println(userSettings.lastIndex); file.println(userSettings.lastPos);
file.println(userSettings.wifiEnabled ? 1 : 0); file.println(userSettings.wifiSSID);
file.println(userSettings.wifiPass); file.println(userSettings.isAPMode ? 1 : 0);
file.println(userSettings.apSSID); file.println(userSettings.apPass);
file.println(userSettings.powerSaverMode); file.println(userSettings.seek);
file.println(userSettings.currentFolder); file.close();
M5Cardputer.Display.fillScreen(C_BG_DARK); M5Cardputer.Display.setCursor(10, 40);
M5Cardputer.Display.setTextColor(C_PLAYING); M5Cardputer.Display.print("Exported to SD!"); delay(1000);
}
}
static void importFromSD() {
if (!SD.exists(CONFIG_FILE)) {
M5Cardputer.Display.fillScreen(C_BG_DARK); M5Cardputer.Display.setCursor(10, 40);
M5Cardputer.Display.setTextColor(TFT_RED); M5Cardputer.Display.print("No config.txt found!"); delay(1000); return;
}
File file = SD.open(CONFIG_FILE);
if (file) {
if(file.available()) userSettings.brightness = file.readStringUntil('\n').toInt();
if(file.available()) userSettings.timeoutIndex = file.readStringUntil('\n').toInt();
if(file.available()) userSettings.resumePlay = (file.readStringUntil('\n').toInt() == 1);
if(file.available()) userSettings.spkRateIndex = file.readStringUntil('\n').toInt();
if(file.available()) userSettings.lastIndex = file.readStringUntil('\n').toInt();
if(file.available()) userSettings.lastPos = file.readStringUntil('\n').toInt();
if(file.available()) userSettings.wifiEnabled = (file.readStringUntil('\n').toInt() == 1);
if(file.available()) { userSettings.wifiSSID = file.readStringUntil('\n'); userSettings.wifiSSID.trim(); }
if(file.available()) { userSettings.wifiPass = file.readStringUntil('\n'); userSettings.wifiPass.trim(); }
if(file.available()) userSettings.isAPMode = (file.readStringUntil('\n').toInt() == 1);
if(file.available()) { userSettings.apSSID = file.readStringUntil('\n'); userSettings.apSSID.trim(); }
if(file.available()) { userSettings.apPass = file.readStringUntil('\n'); userSettings.apPass.trim(); }
if(file.available()) userSettings.powerSaverMode = file.readStringUntil('\n').toInt();
if(file.available()) userSettings.seek = file.readStringUntil('\n').toInt();
if(file.available()) { userSettings.currentFolder = file.readStringUntil('\n'); userSettings.currentFolder.trim(); }
file.close();
save(); M5Cardputer.Display.setBrightness(userSettings.brightness);
M5Cardputer.Display.fillScreen(C_BG_DARK); M5Cardputer.Display.setCursor(10, 40);
M5Cardputer.Display.setTextColor(C_PLAYING); M5Cardputer.Display.print("Imported from SD!"); delay(1000);
}
}
};
void applyCpuFrequency() {
if (userSettings.wifiEnabled || userSettings.powerSaverMode == 0) setCpuFrequencyMhz(240);
else if (userSettings.powerSaverMode == 1) setCpuFrequencyMhz(160);
else setCpuFrequencyMhz(80);
}
// ==========================================
// PLAYLIST HELPERS
// ==========================================
// Returns the cache file path for a given folder.
// "" or "/" => the flat "All Music" cache at PLAYLIST_FILE
// "/Rock" => "/pl_Rock.txt"
// "/A/B" => "/pl_A_B.txt"
String getPlaylistPath(const String& folder) {
if (folder == "" || folder == "/") return String(PLAYLIST_FILE);
String safe = folder;
if (safe.startsWith("/")) safe = safe.substring(1);
safe.replace("/", "_");
return "/pl_" + safe + ".txt";
}
/* ==========================================
I18N (external language file) + Font helpers
- Language files live on SD at: /lang/<code>.txt (key=value)
- We currently read help.<index> lines only, with fallback to built-in English
- Font helper selects a Japanese-capable font and size
========================================== */
class I18N {
public:
static std::vector<String> help;
// Normalize potentially unsupported UTF-8 punctuation to safe ASCII
// and strip BOM/non-breaking spaces that can break parsing/rendering.
static void normalizeUTF8(String &s) {
// Strip UTF-8 BOM if present
s.replace("\xEF\xBB\xBF", "");
// Replace non-breaking space with normal space
s.replace("\xC2\xA0", " ");
// Dashes and minus variants -> ASCII hyphen-minus
s.replace("\xE2\x80\x90", "-"); // hyphen
s.replace("\xE2\x80\x91", "-"); // non-breaking hyphen
s.replace("\xE2\x80\x92", "-"); // figure dash
s.replace("\xE2\x80\x93", "-"); // en dash
s.replace("\xE2\x80\x94", "-"); // em dash
s.replace("\xE2\x88\x92", "-"); // minus sign
// Fancy quotes -> ASCII
s.replace("\xE2\x80\x98", "'"); // left single quote
s.replace("\xE2\x80\x99", "'"); // right single quote
s.replace("\xE2\x80\x9C", "\""); // left quote
s.replace("\xE2\x80\x9D", "\""); // right double quote
}
static void loadHelp(const String &code) {
help.clear();
// Try selected language, then fallback to
String path = "/lang/" + code + ".txt";
if (!SD.exists(path)) path = "/lang/en.txt";
if (SD.exists(path)) {
File f = SD.open(path);
if (f) {
while (f.available()) {
String line = f.readStringUntil('\n');
normalizeUTF8(line);
line.trim();
if (line.length() == 0 || line.startsWith("#")) continue;
int eq = line.indexOf('=');
if (eq > 0) {
String key = line.substring(0, eq); key.trim();
String val = line.substring(eq + 1);
normalizeUTF8(val);
val.trim();
if (key.startsWith("help.")) {
help.push_back(val);
}
}
}
f.close();
}
}
// Fallback to compiled English if nothing loaded
if (help.empty()) {
for (int i = 0; i < numHelpLines; ++i) help.push_back(String(helpLines[i]));
}
}
};
std::vector<String> I18N::help;
class UIFonts {
public:
static const lgfx::IFont* getUIFont() {
switch (userSettings.uiFontSizeIndex) {
case 1: return &fonts::lgfxJapanGothic_16;
default: return &fonts::lgfxJapanGothic_12;
}
}
};
/* ========================================== */
// ==========================================
// AUDIO ENGINE
// ==========================================
class AudioEngine {
public:
AudioFileSourceSD *file = nullptr;
AudioFileSourceID3 *id3 = nullptr;
AudioFileSourceBuffer *buff = nullptr;
AudioGenerator *decoder = nullptr;
std::vector<uint32_t> songOffsets;
int currentIndex = 0;
int browserIndex = 0;
bool isPaused = false;
bool isShuffle = false;
LoopState loopMode = NO_LOOP;
uint32_t paused_at = 0;
String currentTitle = "";
String currentArtist = "";
String currentAlbum = "";
void listDir(fs::FS &fs, const char *dirname, uint8_t levels, File &playlistFile) {
File root = fs.open(dirname); if (!root || !root.isDirectory()) return;
File f = root.openNextFile();
while (f) {
String fname = f.name();
if (fname.startsWith(".")){
f = root.openNextFile();
continue;
}
if (f.isDirectory()) { if (levels) listDir(fs, f.path(), levels - 1, playlistFile); }
else {
String filename = f.name(); String filepath = f.path();
String filenameLower = filename; filenameLower.toLowerCase();
if (filenameLower.endsWith(".mp3") || filenameLower.endsWith(".flac") || filenameLower.endsWith(".m4a") || filenameLower.endsWith(".aac") || filenameLower.endsWith(".wav")) {
playlistFile.println(filepath);
M5Cardputer.Display.fillScreen(C_BG_DARK); M5Cardputer.Display.setCursor(10, 40);
M5Cardputer.Display.println("Scanning..."); M5Cardputer.Display.setTextColor(C_ACCENT);
M5Cardputer.Display.println(filename); M5Cardputer.Display.setTextColor(C_TEXT_MAIN);
}
}
f = root.openNextFile();
}
}
// Full scan of the entire SD card → saves to PLAYLIST_FILE ("All Music")
void performFullScan() {
stop(); songOffsets.clear();
M5Cardputer.Display.fillScreen(C_BG_DARK); M5Cardputer.Display.setCursor(10, 40); M5Cardputer.Display.println("Scanning SD Card...");
if (SD.exists(PLAYLIST_FILE)) SD.remove(PLAYLIST_FILE);
File playlistFile = SD.open(PLAYLIST_FILE, FILE_WRITE);
if (playlistFile) { listDir(SD, "/", 3, playlistFile); playlistFile.close(); }
g_activePlaylist = PLAYLIST_FILE;
userSettings.currentFolder = "";
loadPlaylist();
}
// Scan a specific folder and cache it, then load.
// folder == "" means "All Music" → delegates to performFullScan.
void performFolderScan(const String& folder) {
if (folder == "" || folder == "/") { performFullScan(); return; }
stop(); songOffsets.clear();
M5Cardputer.Display.fillScreen(C_BG_DARK); M5Cardputer.Display.setCursor(10, 40);
M5Cardputer.Display.setTextColor(C_TEXT_MAIN); M5Cardputer.Display.println("Scanning folder...");
M5Cardputer.Display.setTextColor(C_ACCENT); M5Cardputer.Display.println(folder);
M5Cardputer.Display.setTextColor(C_TEXT_MAIN);
String cachePath = getPlaylistPath(folder);
if (SD.exists(cachePath)) SD.remove(cachePath);
File playlistFile = SD.open(cachePath, FILE_WRITE);
if (playlistFile) {
listDir(SD, folder.c_str(), 3, playlistFile);
playlistFile.close();
}
g_activePlaylist = cachePath;
userSettings.currentFolder = folder;
loadPlaylist();
}
// Load (or auto-scan) the playlist for a given folder.
// Returns false if no songs found.
bool loadPlaylistForFolder(const String& folder) {
String cachePath = getPlaylistPath(folder);
g_activePlaylist = cachePath;
userSettings.currentFolder = (folder == "/" ? "" : folder);
if (!SD.exists(cachePath)) {
// Cache missing → build it now
performFolderScan(folder == "" ? "/" : folder);
return (songOffsets.size() > 0);
}
return loadPlaylist();
}
bool loadPlaylist() {
songOffsets.clear();
if (!SD.exists(g_activePlaylist)) return false;
File f = SD.open(g_activePlaylist);
if (!f) return false;
while (f.available()) {
uint32_t pos = f.position();
String line = f.readStringUntil('\n'); line.trim(); line.toLowerCase();
if (line.endsWith(".mp3") || line.endsWith(".flac") || line.endsWith(".m4a") || line.endsWith(".aac") || line.endsWith(".wav")) { songOffsets.push_back(pos); }
}
f.close(); return (songOffsets.size() > 0);
}
String getSongPath(int index) {
if (index < 0 || (size_t)index >= songOffsets.size()) return "";
File f = SD.open(g_activePlaylist); f.seek(songOffsets[index]); String path = f.readStringUntil('\n'); f.close();
path.trim(); if (path.length() > 0 && !path.startsWith("/")) path = "/" + path;
return path;
}
void stop() {
if (decoder) { decoder->stop(); delete decoder; decoder = nullptr; }
if (id3) { id3->close(); delete id3; id3 = nullptr; }
if (buff) { buff->close(); delete buff; buff = nullptr; }
if (file) { file->close(); delete file; file = nullptr; }
}
static void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string);
bool play(int index, uint32_t startPos = 0) {
stop(); if (songOffsets.empty()) return false;
currentIndex = index; browserIndex = index; currentTitle = ""; currentArtist = ""; currentAlbum = "";
String fname = getSongPath(currentIndex);
file = new AudioFileSourceSD(fname.c_str());
buff = new AudioFileSourceBuffer(file, 16384);
id3 = new AudioFileSourceID3(buff);
id3->RegisterMetadataCB(MDCallback, (void*)"ID3TAG");
if (startPos > 0) id3->seek(startPos, 1);
String fnameLower = fname; fnameLower.toLowerCase();
if (fnameLower.endsWith(".flac")) decoder = new AudioGeneratorFLAC();
else if (fnameLower.endsWith(".m4a") || fnameLower.endsWith(".aac")) decoder = new AudioGeneratorAAC();
else if (fnameLower.endsWith(".wav")) decoder = new AudioGeneratorWAV();
else decoder = new AudioGeneratorMP3();
isPaused = false; return decoder->begin(id3, out);
}
void togglePause() {
if (!decoder) return;
if (decoder->isRunning()) { paused_at = id3->getPos(); decoder->stop(); isPaused = true; }
else if (isPaused) { play(currentIndex, paused_at); }
}
void seek(int seconds) {
if (!decoder || !decoder->isRunning() || !id3) return;
int32_t size = id3->getSize();
if (size <= 0) return;
int32_t newPos = id3->getPos() + (seconds * 16000);
if (newPos < 0) newPos = 0;
int32_t endGuard = std::max<int32_t>(0, size - 2000);
if (newPos > endGuard) newPos = endGuard;
play(currentIndex, newPos);
}
void next(bool autoPlay = false) {
if (songOffsets.empty()) return;
if (autoPlay && loopMode == LOOP_ONE) { play(currentIndex); return; }
if (isShuffle) currentIndex = random(0, songOffsets.size());
else {
currentIndex++;
if ((size_t)currentIndex >= songOffsets.size()) { if (loopMode == LOOP_ALL) currentIndex = 0; else { stop(); currentIndex = 0; return; } }
}
play(currentIndex);
}
void prev() {
if (songOffsets.empty()) return;
if (isShuffle) currentIndex = random(0, songOffsets.size());
else { currentIndex--; if (currentIndex < 0) currentIndex = songOffsets.size() - 1; }
play(currentIndex);
}
void loopTasks() {
if (decoder && decoder->isRunning()) {
if (!decoder->loop()) { decoder->stop(); next(true); if(userSettings.resumePlay) ConfigManager::save(id3 ? id3->getPos() : 0, currentIndex); }
}
}
// Scan root for top-level directories. Returns list including "" (All Music).
std::vector<String> listRootFolders() {
std::vector<String> folders;
folders.push_back(""); // index 0 = "All Music"
File root = SD.open("/");
if (!root || !root.isDirectory()) return folders;
File entry = root.openNextFile();
while (entry) {
if (entry.isDirectory()) {
String dname = entry.name();
if (!dname.startsWith(".")) {
String path = entry.path();
folders.push_back(path);
}
}
entry = root.openNextFile();
}
root.close();
return folders;
}
bool jumpFolder(int dir) {
auto folders = listRootFolders();
if (folders.empty()) return false;
// Find current folder index ("" means All Music)
int cur = 0;
for (int i = 0; i < (int)folders.size(); ++i) {
if (folders[i] == userSettings.currentFolder) { cur = i; break; }
}
// Try moving to next/prev non-empty, wrapping around
int tries = folders.size();
while (tries--) {
cur = (cur + (dir >= 0 ? 1 : -1) + (int)folders.size()) % (int)folders.size();
if (loadPlaylistForFolder(folders[cur]) && songOffsets.size() > 0) {
play(0);
ConfigManager::save();
return true;
}
}
return false;
}
bool nextFolder() { return jumpFolder(+1); }
bool prevFolder() { return jumpFolder(-1); }
};
AudioEngine audioApp;
// ==========================================
// UI MANAGER
// ==========================================
class UIManager {
public:
static int settingsCursor;
static int menuScrollOffset;
static bool showVisualizer;
static bool offVisActive;
static int wifiCursor;
static int wifiScrollOffset;
static int wifiNetworkCount;
static int helpScrollOffset;
// Folder browser state
static std::vector<String> folderList;
static int folderCursor;
static int folderScrollOffset;
// EQ state
static int eqCursor;
// -----------------------------------------------
// SEARCH UI
// -----------------------------------------------
static void rebuildSearchResults() {
g_searchResults.clear();
g_searchCursor = 0;
g_searchScrollOffset = 0;
if (g_searchQuery.length() == 0) return;
String queryLower = g_searchQuery; queryLower.toLowerCase();
File f = SD.open(g_activePlaylist);
if (!f) return;
int idx = 0;
while (f.available()) {
String line = f.readStringUntil('\n'); line.trim();
String lineLower = line; lineLower.toLowerCase();
// Extract filename portion for matching
int slash = lineLower.lastIndexOf('/');
String fname = (slash >= 0) ? lineLower.substring(slash + 1) : lineLower;
if (fname.indexOf(queryLower) >= 0) {
g_searchResults.push_back(idx);
}
idx++;
}
f.close();
}
static void drawSearch() {
M5Cardputer.Display.fillScreen(C_BG_DARK);
// Header
M5Cardputer.Display.fillRect(0, 0, M5Cardputer.Display.width(), HEADER_HEIGHT, C_HEADER);
M5Cardputer.Display.setFont(&fonts::Font0);
M5Cardputer.Display.setTextColor(C_TEXT_MAIN, C_HEADER);
M5Cardputer.Display.setCursor(5, 5);
M5Cardputer.Display.print("Search Songs");
// Search input box
int boxY = HEADER_HEIGHT + 4;
M5Cardputer.Display.fillRect(2, boxY, M5Cardputer.Display.width() - 4, 16, C_BG_LIGHT);
M5Cardputer.Display.drawRect(2, boxY, M5Cardputer.Display.width() - 4, 16, C_ACCENT);
M5Cardputer.Display.setTextColor(C_TEXT_MAIN);
M5Cardputer.Display.setCursor(6, boxY + 4);
String displayQuery = g_searchQuery.length() > 0 ? g_searchQuery : "";
M5Cardputer.Display.print(displayQuery + "_");
// Results
int yPos = boxY + 20;
int totalResults = g_searchResults.size();
if (g_searchQuery.length() == 0) {
M5Cardputer.Display.setTextColor(C_TEXT_DIM);
M5Cardputer.Display.setCursor(6, yPos);
M5Cardputer.Display.print("Type to search...");
} else if (totalResults == 0) {
M5Cardputer.Display.setTextColor(TFT_RED);
M5Cardputer.Display.setCursor(6, yPos);
M5Cardputer.Display.print("No results.");
} else {
for (int i = 0; i < MAX_VISIBLE_ROWS; i++) {
int ri = g_searchScrollOffset + i;
if (ri >= totalResults) break;
int songIdx = g_searchResults[ri];
bool isSelected = (ri == g_searchCursor);
bool isPlaying = (songIdx == audioApp.currentIndex);
if (isSelected) {
M5Cardputer.Display.fillRect(2, yPos - 1, M5Cardputer.Display.width() - 4, ROW_HEIGHT, C_ACCENT);
M5Cardputer.Display.setTextColor(C_BG_DARK);
} else if (isPlaying) {
M5Cardputer.Display.setTextColor(C_PLAYING);
} else {
M5Cardputer.Display.setTextColor(C_TEXT_MAIN);
}
// Get filename from playlist
String songPath = audioApp.getSongPath(songIdx);
int slash = songPath.lastIndexOf('/');
String fname = (slash >= 0) ? songPath.substring(slash + 1) : songPath;
if (fname.length() > 28) fname = fname.substring(0, 27) + "~";
M5Cardputer.Display.setCursor(6, yPos + 2);
if (isPlaying && !isSelected) M5Cardputer.Display.print("> ");
M5Cardputer.Display.print(fname);
yPos += ROW_HEIGHT;
}
// Scroll arrows
M5Cardputer.Display.setTextColor(C_ACCENT);
if (g_searchScrollOffset > 0)
M5Cardputer.Display.drawString("^", M5Cardputer.Display.width() - 10, boxY + 22);
if (g_searchScrollOffset + MAX_VISIBLE_ROWS < totalResults)
M5Cardputer.Display.drawString("v", M5Cardputer.Display.width() - 10, boxY + 20 + (MAX_VISIBLE_ROWS * ROW_HEIGHT) - 6);
}
// Footer
int footerY = M5Cardputer.Display.height() - BOTTOM_BAR_HEIGHT;
M5Cardputer.Display.fillRect(0, footerY, M5Cardputer.Display.width(), BOTTOM_BAR_HEIGHT, C_HEADER);
M5Cardputer.Display.setTextColor(C_TEXT_DIM, C_HEADER);
M5Cardputer.Display.setCursor(5, footerY + 4);
M5Cardputer.Display.print("Enter:Play ;/.:Scroll `:Close");
}
static void drawHelp() {
drawPopup("CONTROLS & HELP", "Press 'I' to Exit");
int px = 15, py = 15;
int contentY = py + 25;
int lineHeight = 12;
int visibleLines = 7;
M5Cardputer.Display.setFont(UIFonts::getUIFont());
M5Cardputer.Display.setTextColor(C_TEXT_MAIN);
if (I18N::help.empty()) { I18N::loadHelp(userSettings.languageCode); }
for (int i = 0; i < visibleLines; i++) {
int idx = helpScrollOffset + i;
if (idx >= (int)I18N::help.size()) break;
M5Cardputer.Display.setCursor(px + 10, contentY + (i * lineHeight));
M5Cardputer.Display.print(I18N::help[idx]);
}
M5Cardputer.Display.setTextColor(C_ACCENT);
if (helpScrollOffset > 0) M5Cardputer.Display.drawString("^", px + 190, contentY);
if (helpScrollOffset < (int)I18N::help.size() - visibleLines) M5Cardputer.Display.drawString("v", px + 190, contentY + (visibleLines * lineHeight) - 10);
}
static void drawPopup(const char* title, const char* footer) {
int px = 15, py = 15, pw = 210, ph = 120;
M5Cardputer.Display.fillRoundRect(px, py, pw, ph, 4, C_BG_LIGHT); M5Cardputer.Display.drawRoundRect(px, py, pw, ph, 4, C_ACCENT);
M5Cardputer.Display.fillRoundRect(px+2, py+2, pw-4, 18, 2, C_HEADER); M5Cardputer.Display.setFont(&fonts::Font0);
M5Cardputer.Display.setTextColor(C_TEXT_MAIN); M5Cardputer.Display.setCursor(px + 8, py + 5); M5Cardputer.Display.print(title);
if (footer) { M5Cardputer.Display.setCursor(px + 10, py + ph - 15); M5Cardputer.Display.setTextColor(C_TEXT_DIM); M5Cardputer.Display.print(footer); }
}
static void drawHeader() {
M5Cardputer.Display.fillRect(0, 0, M5Cardputer.Display.width(), HEADER_HEIGHT, C_HEADER);
M5Cardputer.Display.setFont(UIFonts::getUIFont());
String prefix = "Music Player";
if (userSettings.wifiEnabled) {
if (userSettings.isAPMode) { prefix = WiFi.softAPIP().toString(); M5Cardputer.Display.setTextColor(TFT_ORANGE, C_HEADER); }
else if (WiFi.status() == WL_CONNECTED) { prefix = WiFi.localIP().toString(); M5Cardputer.Display.setTextColor(C_ACCENT, C_HEADER); }
else M5Cardputer.Display.setTextColor(C_TEXT_MAIN, C_HEADER);
} else M5Cardputer.Display.setTextColor(C_TEXT_MAIN, C_HEADER);
String headerText = prefix + " [" + String(audioApp.currentIndex + 1) + "/" + String(audioApp.songOffsets.size()) + "]";
M5Cardputer.Display.drawString(headerText.c_str(), 5, 5);
drawBattery();
}
static void drawBattery() {
int batLevel = M5Cardputer.Power.getBatteryLevel();
int w = 24, h = 10, x = M5Cardputer.Display.width() - w - 5, y = 5;
M5Cardputer.Display.fillRect(x - 30, 0, w + 35, HEADER_HEIGHT, C_HEADER);
M5Cardputer.Display.drawRect(x, y, w, h, C_TEXT_MAIN);
M5Cardputer.Display.fillRect(x + w, y + 2, 2, 6, C_TEXT_MAIN);
uint16_t color = batLevel < 20 ? TFT_RED : (batLevel < 50 ? TFT_YELLOW : C_PLAYING);
int fillW = max(0, (int)map(batLevel, 0, 100, 0, w - 2));
M5Cardputer.Display.fillRect(x + 1, y + 1, fillW, h - 2, color);
M5Cardputer.Display.setFont(&fonts::Font0);
M5Cardputer.Display.setTextColor(C_TEXT_MAIN, C_HEADER);
M5Cardputer.Display.setCursor(batLevel == 100 ? x - 29 : x - 25, y + 1);
M5Cardputer.Display.print(batLevel);
M5Cardputer.Display.print("%");
}
static void drawBottomBar() {
int yPos = M5Cardputer.Display.height() - BOTTOM_BAR_HEIGHT;
M5Cardputer.Display.fillRect(0, yPos, M5Cardputer.Display.width(), BOTTOM_BAR_HEIGHT, C_HEADER);
M5Cardputer.Display.setTextColor(C_TEXT_MAIN); M5Cardputer.Display.setFont(&fonts::Font0);
M5Cardputer.Display.setCursor(5, yPos + 4); M5Cardputer.Display.print("Enter:Play ;/.:Scroll I:info");
if (!audioApp.songOffsets.empty()) {
String fname = audioApp.getSongPath(audioApp.currentIndex); fname.toLowerCase();
String codecText = "MP3"; uint16_t codecColor = C_ACCENT;
if (fname.endsWith(".flac")) { codecText = "FLAC"; codecColor = C_PLAYING; }
else if (fname.endsWith(".m4a") || fname.endsWith(".aac")) { codecText = "AAC"; codecColor = C_HIGHLIGHT; }
else if (fname.endsWith(".wav")) { codecText = "WAV"; codecColor = TFT_ORANGE; }
int boxW = 36, boxH = 14, boxX = M5Cardputer.Display.width() - boxW - 2, boxY = yPos + 2;
M5Cardputer.Display.fillRoundRect(boxX, boxY, boxW, boxH, 3, codecColor); M5Cardputer.Display.setTextColor(C_BG_DARK);
M5Cardputer.Display.setCursor(boxX + ((boxW - M5Cardputer.Display.textWidth(codecText.c_str())) / 2), boxY + 3);
M5Cardputer.Display.print(codecText);
}
}
static void drawPlaylist() {
M5Cardputer.Display.setFont(UIFonts::getUIFont());
int totalSongs = audioApp.songOffsets.size();
// Dynamic line height and rows based on selected UI font
int lineHeight = M5Cardputer.Display.fontHeight() + 3;
int listHeight = M5Cardputer.Display.height() - HEADER_HEIGHT - BOTTOM_BAR_HEIGHT;
int maxRows = max(1, listHeight / lineHeight);
int startIdx = max(0, min(audioApp.browserIndex - (maxRows / 2), totalSongs - maxRows));
int yPos = HEADER_HEIGHT + 2, xPos = 0;
// Background for playlist pane
M5Cardputer.Display.fillRect(0, HEADER_HEIGHT, PLAYLIST_WIDTH, listHeight, C_BG_LIGHT);
M5Cardputer.Display.drawFastVLine(PLAYLIST_WIDTH, HEADER_HEIGHT, listHeight, C_BG_DARK);
String dispName;
File f = SD.open(g_activePlaylist);
if (f && totalSongs > 0) f.seek(audioApp.songOffsets[startIdx]);
for (int i = 0; i < maxRows; i++) {
int actualIdx = startIdx + i;
if (actualIdx >= totalSongs) break;
// Row coloring and selection
if (actualIdx == audioApp.browserIndex) {
M5Cardputer.Display.fillRect(xPos + 2, yPos, PLAYLIST_WIDTH - 6, lineHeight - 1, C_ACCENT);
M5Cardputer.Display.setTextColor(C_BG_DARK);
} else if (actualIdx == audioApp.currentIndex) {
M5Cardputer.Display.setTextColor(C_PLAYING);
} else {
M5Cardputer.Display.setTextColor(C_TEXT_DIM);
}
// File name
dispName = f ? f.readStringUntil('\n') : "";
dispName.trim();
int slashIdx = dispName.lastIndexOf('/');
if (slashIdx >= 0) dispName = dispName.substring(slashIdx + 1);
// Compute available width, trim with "~" if needed
M5Cardputer.Display.setCursor(xPos + 5, yPos + 2);
String prefix = (actualIdx == audioApp.currentIndex) ? "> " : "";
if (prefix.length()) M5Cardputer.Display.print(prefix);
int availW = PLAYLIST_WIDTH - 10 - M5Cardputer.Display.textWidth(prefix.c_str());
String name = dispName;
int ellW = M5Cardputer.Display.textWidth("~");
while (name.length() > 0 && M5Cardputer.Display.textWidth(name.c_str()) > (availW - ellW)) {
name.remove(name.length() - 1);
}
if (name != dispName) name += "~";
M5Cardputer.Display.print(name);
yPos += lineHeight;
}
if (f) f.close();
}
static void drawNowPlaying() {
int xStart = PLAYLIST_WIDTH + 5, yStart = HEADER_HEIGHT + 5;
M5Cardputer.Display.fillRect(xStart, yStart, M5Cardputer.Display.width() - xStart, 50, C_BG_DARK);
M5Cardputer.Display.setFont(UIFonts::getUIFont()); M5Cardputer.Display.setTextColor(audioApp.isPaused ? TFT_ORANGE : C_PLAYING);
M5Cardputer.Display.setCursor(xStart + 5, yStart); M5Cardputer.Display.print(audioApp.isPaused ? "[PAUSED]" : "PLAYING >");
M5Cardputer.Display.setCursor(M5Cardputer.Display.width() - 55, yStart);
if (audioApp.isShuffle) { M5Cardputer.Display.setTextColor(C_HIGHLIGHT); M5Cardputer.Display.print("SHF "); } else { M5Cardputer.Display.setTextColor(C_BG_LIGHT); M5Cardputer.Display.print("___ "); }
switch(audioApp.loopMode) {
case NO_LOOP: M5Cardputer.Display.setTextColor(C_BG_LIGHT); M5Cardputer.Display.print("1X"); break;
case LOOP_ALL: M5Cardputer.Display.setTextColor(C_ACCENT); M5Cardputer.Display.print("ALL"); break;
case LOOP_ONE: M5Cardputer.Display.setTextColor(C_HIGHLIGHT); M5Cardputer.Display.print("ONE"); break;
}
M5Cardputer.Display.setTextColor(C_TEXT_MAIN); M5Cardputer.Display.setCursor(xStart + 5, yStart + 16);
if (audioApp.currentTitle.length() > 0) M5Cardputer.Display.print(audioApp.currentTitle.substring(0, 15));
if (audioApp.id3 && audioApp.file) {
int maxW = M5Cardputer.Display.width() - xStart - 10;
int curW = (int)((float)audioApp.id3->getPos() / (float)audioApp.id3->getSize() * maxW);
M5Cardputer.Display.fillRect(xStart-3, yStart+30-3, maxW+6, 9, C_BG_DARK); M5Cardputer.Display.fillRect(xStart, yStart+30, maxW, 3, C_BG_LIGHT);
M5Cardputer.Display.fillRect(xStart, yStart+30, min(curW, maxW), 3, C_HIGHLIGHT); M5Cardputer.Display.fillCircle(xStart + min(curW, maxW), yStart+30+1, 3, C_TEXT_MAIN);
}
int volY = yStart + 42; M5Cardputer.Display.setCursor(xStart + 5, volY); M5Cardputer.Display.setFont(&fonts::Font0);
M5Cardputer.Display.setTextColor(C_ACCENT); M5Cardputer.Display.print("VOL "); M5Cardputer.Display.drawRect(xStart + 30, volY, 60, 6, C_BG_LIGHT);
M5Cardputer.Display.fillRect(xStart + 31, volY + 1, (M5Cardputer.Speaker.getVolume() * 58) / 255, 4, C_ACCENT);
}
static void drawVisualizer() {
static bool offFullActive = false;
if (userSettings.visMode == 3 && userSettings.offVisFullscreen) {
UIManager::offVisActive = true;
UIManager::drawOffVisualizerFull();
offFullActive = true;
return;
} else if (offFullActive) {
UIManager::offVisActive = false;
offFullActive = false;
UIManager::drawBaseUI();
}
if (!audioApp.decoder || !audioApp.decoder->isRunning() || audioApp.isPaused || !showVisualizer) return;