-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ino
More file actions
270 lines (214 loc) · 7.39 KB
/
Copy pathcode.ino
File metadata and controls
270 lines (214 loc) · 7.39 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
/*
* Spotify Display - Clean Version with Dynamic Lyrics Font
*/
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
// Colors
#define COLOR_BG 0x0000
#define COLOR_PRIMARY 0x1E93
#define COLOR_TEXT 0xFFFF
#define COLOR_TEXT_SEC 0xAD55
#define COLOR_TEXT_MUTED 0x630C
// Layout
#define ART_X 20
#define ART_Y 20
#define ART_SIZE 200
#define ART_DISP 200
#define INFO_X 240
#define INFO_Y 30
#define INFO_W 220
#define STATUS_X 240
#define STATUS_Y 140
#define STATUS_W 220
#define PROGRESS_X 20
#define PROGRESS_Y 310
#define PROGRESS_W 440
#define PROGRESS_H 4
#define LYRICS_X 20
#define LYRICS_Y 230
#define LYRICS_W 460
#define LYRICS_H 70
#define LYRIC1_Y 235
#define LYRIC2_Y 265
// Font sizes
#define FONT_TITLE 2
#define FONT_ARTIST 2
#define FONT_ALBUM 1
#define FONT_LYRIC_NEXT 1
#define FONT_STATUS 2
// Buffer
#define ROW_BYTES (ART_SIZE * 2)
uint8_t rowBuf[ROW_BYTES];
bool firstRun = true;
int lastProgress = -1;
String lastLyric1 = "";
String lastLyric2 = "";
void clearArea(int x, int y, int w, int h) {
tft.fillRect(x, y, w, h, COLOR_BG);
}
void drawText(int x, int y, const String& text, uint16_t color, uint8_t size) {
tft.setTextColor(color, COLOR_BG);
tft.setTextSize(size);
tft.setCursor(x, y);
tft.print(text);
}
// Shorten for title/artist/album only
String shortenText(const String& text, int maxLen) {
if (text.length() <= maxLen) return text;
return text.substring(0, maxLen - 2) + "..";
}
void drawSongInfo(const String& title, const String& artist, const String& album) {
clearArea(INFO_X, INFO_Y, INFO_W, 120);
String shortTitle = shortenText(title, 18);
drawText(INFO_X, INFO_Y, shortTitle, COLOR_TEXT, FONT_TITLE);
String shortArtist = shortenText(artist, 22);
drawText(INFO_X, INFO_Y + 35, shortArtist, COLOR_PRIMARY, FONT_ARTIST);
String shortAlbum = shortenText(album, 35);
drawText(INFO_X, INFO_Y + 65, shortAlbum, COLOR_TEXT_SEC, FONT_ALBUM);
}
void drawStatus(bool playing) {
clearArea(STATUS_X, STATUS_Y, STATUS_W, 25);
if (playing) {
drawText(STATUS_X, STATUS_Y, "PLAYING", COLOR_PRIMARY, FONT_STATUS);
} else {
drawText(STATUS_X, STATUS_Y, "PAUSED", COLOR_TEXT_MUTED, FONT_STATUS);
}
}
void drawProgress(int progress, bool playing) {
if (progress == lastProgress && !firstRun) return;
lastProgress = progress;
clearArea(PROGRESS_X - 5, PROGRESS_Y - 12, PROGRESS_W + 10, 25);
tft.fillRect(PROGRESS_X, PROGRESS_Y, PROGRESS_W, PROGRESS_H, COLOR_TEXT_MUTED);
int fill = (PROGRESS_W * progress) / 100;
if (fill > 0) {
tft.fillRect(PROGRESS_X, PROGRESS_Y, fill, PROGRESS_H, COLOR_PRIMARY);
}
int dotX = PROGRESS_X + fill;
tft.fillCircle(dotX, PROGRESS_Y + PROGRESS_H/2, 5, playing ? COLOR_PRIMARY : COLOR_TEXT_SEC);
tft.setTextSize(1);
tft.setTextColor(COLOR_TEXT_MUTED, COLOR_BG);
int currentSec = (progress * 180) / 100;
int totalSec = 180;
char timeStr[16];
sprintf(timeStr, "%d:%02d", currentSec / 60, currentSec % 60);
tft.setCursor(PROGRESS_X, PROGRESS_Y - 10);
tft.print(timeStr);
sprintf(timeStr, "%d:%02d", totalSec / 60, totalSec % 60);
tft.setCursor(PROGRESS_X + PROGRESS_W - 24, PROGRESS_Y - 10);
tft.print(timeStr);
}
void drawLyrics(const String& line1, const String& line2) {
clearArea(LYRICS_X, LYRICS_Y, LYRICS_W, LYRICS_H);
// Calculate font size based on line length (no clipping!)
if (line1.length() > 0) {
// Size 2 fits ~38 chars, Size 1 fits ~76 chars
uint8_t fontSize = 2;
if (line1.length() > 38) {
fontSize = 1;
}
drawText(LYRICS_X, LYRIC1_Y, line1, COLOR_TEXT, fontSize);
}
if (line2.length() > 0) {
drawText(LYRICS_X, LYRIC2_Y, line2, COLOR_TEXT_SEC, FONT_LYRIC_NEXT);
}
}
void receiveImage() {
Serial.write('R');
for (int row = 0; row < ART_SIZE; row++) {
int got = 0;
unsigned long start = millis();
while (got < ROW_BYTES) {
if (Serial.available()) {
rowBuf[got++] = Serial.read();
} else if (millis() - start > 5000) {
Serial.write('E');
return;
}
}
for (int px = 0; px < ART_SIZE; px++) {
uint16_t color = ((uint16_t)rowBuf[px*2] << 8) | rowBuf[px*2+1];
tft.drawPixel(ART_X + px, ART_Y + row, color);
}
Serial.write('K');
}
tft.drawRect(ART_X - 1, ART_Y - 1, ART_DISP + 2, ART_DISP + 2, COLOR_PRIMARY);
}
void drawIdle() {
tft.fillScreen(COLOR_BG);
drawText(180, 150, "Spotify", COLOR_PRIMARY, 3);
drawText(185, 180, "Ready", COLOR_TEXT_MUTED, 1);
}
void parseData(String header, String& title, String& artist, String& album,
int& progress, bool& hasImage, bool& lyricsChanged, bool& isPlaying,
String& lyric1, String& lyric2) {
int parts[9];
parts[0] = 4;
for (int i = 1; i < 9; i++) {
parts[i] = header.indexOf('|', parts[i-1] + 1);
if (parts[i] < 0) parts[i] = header.length();
}
title = header.substring(parts[0]+1, parts[1]);
artist = header.substring(parts[1]+1, parts[2]);
album = header.substring(parts[2]+1, parts[3]);
progress = header.substring(parts[3]+1, parts[4]).toInt();
hasImage = header.substring(parts[4]+1, parts[5]).toInt() == 1;
lyricsChanged = header.substring(parts[5]+1, parts[6]).toInt() == 1;
isPlaying = header.substring(parts[6]+1, parts[7]).toInt() == 1;
String lyricPayload = header.substring(parts[7]+1);
int sep = lyricPayload.indexOf("||");
if (sep >= 0) {
lyric1 = lyricPayload.substring(0, sep);
lyric2 = lyricPayload.substring(sep + 2);
} else {
lyric1 = lyricPayload;
lyric2 = "";
}
}
void setup() {
Serial.begin(115200);
uint16_t id = tft.readID();
if (id == 0xD3D3) id = 0x9486;
tft.begin(id);
tft.setRotation(1);
tft.fillScreen(COLOR_BG);
drawIdle();
}
void loop() {
if (!Serial.available()) return;
String header = Serial.readStringUntil('\n');
header.trim();
if (header == "IDLE") {
drawIdle();
firstRun = true;
lastProgress = -1;
lastLyric1 = "";
lastLyric2 = "";
return;
}
if (!header.startsWith("DATA|")) return;
String title, artist, album, lyric1, lyric2;
int progress;
bool hasImage, lyricsChanged, isPlaying;
parseData(header, title, artist, album, progress, hasImage, lyricsChanged, isPlaying, lyric1, lyric2);
if (hasImage || firstRun) {
tft.fillScreen(COLOR_BG);
receiveImage();
drawSongInfo(title, artist, album);
drawStatus(isPlaying);
drawProgress(progress, isPlaying);
drawLyrics(lyric1, lyric2);
firstRun = false;
lastLyric1 = lyric1;
lastLyric2 = lyric2;
}
else {
drawProgress(progress, isPlaying);
if (lyricsChanged && (lyric1 != lastLyric1 || lyric2 != lastLyric2)) {
drawLyrics(lyric1, lyric2);
lastLyric1 = lyric1;
lastLyric2 = lyric2;
}
}
delay(10);
}