Skip to content

Commit dd7b059

Browse files
committed
Added the ability to write context URI, Doesn't always work though
1 parent 4fa66f7 commit dd7b059

File tree

3 files changed

+84
-31
lines changed

3 files changed

+84
-31
lines changed

SpotifyDiyThing/SpotifyDiyThing.ino

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828
#define NFC_ENABLED 1
2929

30+
// This causes issues in certain circumstances e.g. Play an album and let it auto play to related songs
31+
bool writeContextToNfc = true;
32+
3033
// ----------------------------
3134
// Library Defines - Need to be defined before library import
3235
// ----------------------------
@@ -210,10 +213,18 @@ void loop()
210213

211214
spotifyDisplay->checkForInput();
212215

213-
#ifdef NFC_ENABLED
214-
bool forceUpdate = nfcLoop(lastTrackUri);
215-
#else
216216
bool forceUpdate = false;
217+
218+
#ifdef NFC_ENABLED
219+
if (writeContextToNfc)
220+
{
221+
forceUpdate = nfcLoop(lastTrackUri, lastTrackContextUri);
222+
}
223+
else
224+
{
225+
forceUpdate = nfcLoop(lastTrackUri);
226+
}
227+
217228
#endif
218229

219230
updateCurrentlyPlaying(forceUpdate);

SpotifyDiyThing/nfc.h

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ bool updateSpotify(char *tagContent)
224224
return false;
225225
}
226226

227-
bool handleTag(const char *trackUri)
227+
bool handleTag(const char *trackUri, const char *contextUri)
228228
{
229229
NfcTag tag = nfc.read();
230230

@@ -287,8 +287,6 @@ bool handleTag(const char *trackUri)
287287
Serial.println(uid);
288288
}
289289

290-
// Force the data into a String (might work depending on the content)
291-
// Real code should use smarter processing
292290
char payloadAsString[payloadLength + 1];
293291
int numChars = 0;
294292
for (int c = 0; c < payloadLength; c++)
@@ -340,6 +338,7 @@ bool handleTag(const char *trackUri)
340338
Serial.println("Writing track");
341339

342340
int trackLength = strlen(trackUri);
341+
int contextLength = 0;
343342

344343
NdefMessage message = NdefMessage();
345344
// This seems to be a blank card, lets write to it
@@ -351,14 +350,39 @@ bool handleTag(const char *trackUri)
351350
mimeType.getBytes(type, sizeof(type));
352351
r.setType(type, mimeType.length());
353352

353+
bool hasContext = false;
354+
355+
if (contextUri != NULL)
356+
{
357+
int contextLengthCheck = strlen(contextUri);
358+
359+
if (contextLengthCheck > 0)
360+
{
361+
hasContext = true;
362+
363+
// extra 1 for comma
364+
contextLength = contextLengthCheck + 1;
365+
}
366+
}
367+
368+
// One for new line, one for the 0x00 needed at the start
369+
int payloadLength = trackLength + contextLength + 2;
370+
354371
// One for new line, one for the 0x00 needed at the start
355-
byte payloadBytes[trackLength + 2];
372+
byte payloadBytes[payloadLength];
356373
// Write to the new buffer offset by one
357374
// currentTrackUri.getBytes(&payloadBytes[1], trackLength + 1);
358375
memcpy(payloadBytes + sizeof(byte), trackUri, trackLength + 1);
359376
payloadBytes[0] = 0;
360377

361-
r.setPayload(payloadBytes, trackLength + 1);
378+
if (hasContext)
379+
{
380+
payloadBytes[trackLength + 1] = ',';
381+
// extra 1 for comma
382+
memcpy(payloadBytes + (sizeof(byte) * (trackLength + 2)), contextUri, contextLength);
383+
}
384+
385+
r.setPayload(payloadBytes, payloadLength - 1); // not sure why this is -1
362386

363387
message.addRecord(r);
364388
boolean success = nfc.write(message);
@@ -379,12 +403,12 @@ bool handleTag(const char *trackUri)
379403
return false;
380404
}
381405

382-
bool nfcLoop(const char *trackUri)
406+
bool nfcLoop(const char *trackUri, const char *contextUri = NULL)
383407
{
384408
forceUpdate = false;
385409
if (millis() > nfcDueTime)
386410
{
387-
if (nfc.tagPresent() && handleTag(trackUri))
411+
if (nfc.tagPresent() && handleTag(trackUri, contextUri))
388412
{
389413
Serial.println("Successful Read - Back to loop:");
390414
nfcDueTime = millis() + 5000; // 5 second cool down on NFC tag if succesful

SpotifyDiyThing/spotifyLogic.h

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ long songStartMillis;
88
long songDuration;
99

1010
char lastTrackUri[200];
11+
char lastTrackContextUri[200];
1112

1213
// You might want to make this much smaller, so it will update responsively
1314

@@ -22,10 +23,14 @@ void spotifySetup(SpotifyDisplay *theDisplay, const char *clientId, const char *
2223
sp_Display = theDisplay;
2324
client.setCACert(spotify_server_cert);
2425
spotify.lateInit(clientId, clientSecret);
26+
27+
lastTrackUri[0] = '\0';
28+
lastTrackContextUri[0] = '\0';
2529
}
2630

2731
bool isSameTrack(const char *trackUri)
2832
{
33+
2934
return strcmp(lastTrackUri, trackUri) == 0;
3035
}
3136

@@ -34,6 +39,18 @@ void setTrackUri(const char *trackUri)
3439
strcpy(lastTrackUri, trackUri);
3540
}
3641

42+
void setTrackContextUri(const char *trackContext)
43+
{
44+
if (trackContext == NULL)
45+
{
46+
lastTrackContextUri[0] = '\0';
47+
}
48+
else
49+
{
50+
strcpy(lastTrackContextUri, trackContext);
51+
}
52+
}
53+
3754
void spotifyRefreshToken(const char *refreshToken)
3855
{
3956
spotify.setRefreshToken(refreshToken);
@@ -50,32 +67,33 @@ void spotifyRefreshToken(const char *refreshToken)
5067

5168
void handleCurrentlyPlaying(CurrentlyPlaying currentlyPlaying)
5269
{
53-
54-
// printCurrentlyPlayingToSerial(currentlyPlaying);
55-
56-
if (!isSameTrack(currentlyPlaying.trackUri))
70+
if (currentlyPlaying.trackUri != NULL)
5771
{
58-
setTrackUri(currentlyPlaying.trackUri);
72+
if (!isSameTrack(currentlyPlaying.trackUri))
73+
{
74+
setTrackUri(currentlyPlaying.trackUri);
75+
setTrackContextUri(currentlyPlaying.contextUri);
5976

60-
// We have a new Song, need to update the text
61-
sp_Display->printCurrentlyPlayingToScreen(currentlyPlaying);
62-
}
77+
// We have a new Song, need to update the text
78+
sp_Display->printCurrentlyPlayingToScreen(currentlyPlaying);
79+
}
6380

64-
albumArtChanged = sp_Display->processImageInfo(currentlyPlaying);
81+
albumArtChanged = sp_Display->processImageInfo(currentlyPlaying);
6582

66-
sp_Display->displayTrackProgress(currentlyPlaying.progressMs, currentlyPlaying.durationMs);
83+
sp_Display->displayTrackProgress(currentlyPlaying.progressMs, currentlyPlaying.durationMs);
6784

68-
if (currentlyPlaying.isPlaying)
69-
{
70-
// If we know at what millis the song started at, we can make a good guess
71-
// at updating the progress bar more often than checking the API
72-
songStartMillis = millis() - currentlyPlaying.progressMs;
73-
songDuration = currentlyPlaying.durationMs;
74-
}
75-
else
76-
{
77-
// Song doesn't seem to be playing, do not update the progress
78-
songStartMillis = 0;
85+
if (currentlyPlaying.isPlaying)
86+
{
87+
// If we know at what millis the song started at, we can make a good guess
88+
// at updating the progress bar more often than checking the API
89+
songStartMillis = millis() - currentlyPlaying.progressMs;
90+
songDuration = currentlyPlaying.durationMs;
91+
}
92+
else
93+
{
94+
// Song doesn't seem to be playing, do not update the progress
95+
songStartMillis = 0;
96+
}
7997
}
8098
}
8199

0 commit comments

Comments
 (0)