Skip to content

Commit

Permalink
Added the ability to write context URI, Doesn't always work though
Browse files Browse the repository at this point in the history
  • Loading branch information
witnessmenow committed Dec 2, 2023
1 parent 4fa66f7 commit dd7b059
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 31 deletions.
17 changes: 14 additions & 3 deletions SpotifyDiyThing/SpotifyDiyThing.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

#define NFC_ENABLED 1

// This causes issues in certain circumstances e.g. Play an album and let it auto play to related songs
bool writeContextToNfc = true;

// ----------------------------
// Library Defines - Need to be defined before library import
// ----------------------------
Expand Down Expand Up @@ -210,10 +213,18 @@ void loop()

spotifyDisplay->checkForInput();

#ifdef NFC_ENABLED
bool forceUpdate = nfcLoop(lastTrackUri);
#else
bool forceUpdate = false;

#ifdef NFC_ENABLED
if (writeContextToNfc)
{
forceUpdate = nfcLoop(lastTrackUri, lastTrackContextUri);
}
else
{
forceUpdate = nfcLoop(lastTrackUri);
}

#endif

updateCurrentlyPlaying(forceUpdate);
Expand Down
38 changes: 31 additions & 7 deletions SpotifyDiyThing/nfc.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ bool updateSpotify(char *tagContent)
return false;
}

bool handleTag(const char *trackUri)
bool handleTag(const char *trackUri, const char *contextUri)
{
NfcTag tag = nfc.read();

Expand Down Expand Up @@ -287,8 +287,6 @@ bool handleTag(const char *trackUri)
Serial.println(uid);
}

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

int trackLength = strlen(trackUri);
int contextLength = 0;

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

bool hasContext = false;

if (contextUri != NULL)
{
int contextLengthCheck = strlen(contextUri);

if (contextLengthCheck > 0)
{
hasContext = true;

// extra 1 for comma
contextLength = contextLengthCheck + 1;
}
}

// One for new line, one for the 0x00 needed at the start
int payloadLength = trackLength + contextLength + 2;

// One for new line, one for the 0x00 needed at the start
byte payloadBytes[trackLength + 2];
byte payloadBytes[payloadLength];
// Write to the new buffer offset by one
// currentTrackUri.getBytes(&payloadBytes[1], trackLength + 1);
memcpy(payloadBytes + sizeof(byte), trackUri, trackLength + 1);
payloadBytes[0] = 0;

r.setPayload(payloadBytes, trackLength + 1);
if (hasContext)
{
payloadBytes[trackLength + 1] = ',';
// extra 1 for comma
memcpy(payloadBytes + (sizeof(byte) * (trackLength + 2)), contextUri, contextLength);
}

r.setPayload(payloadBytes, payloadLength - 1); // not sure why this is -1

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

bool nfcLoop(const char *trackUri)
bool nfcLoop(const char *trackUri, const char *contextUri = NULL)
{
forceUpdate = false;
if (millis() > nfcDueTime)
{
if (nfc.tagPresent() && handleTag(trackUri))
if (nfc.tagPresent() && handleTag(trackUri, contextUri))
{
Serial.println("Successful Read - Back to loop:");
nfcDueTime = millis() + 5000; // 5 second cool down on NFC tag if succesful
Expand Down
60 changes: 39 additions & 21 deletions SpotifyDiyThing/spotifyLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ long songStartMillis;
long songDuration;

char lastTrackUri[200];
char lastTrackContextUri[200];

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

Expand All @@ -22,10 +23,14 @@ void spotifySetup(SpotifyDisplay *theDisplay, const char *clientId, const char *
sp_Display = theDisplay;
client.setCACert(spotify_server_cert);
spotify.lateInit(clientId, clientSecret);

lastTrackUri[0] = '\0';
lastTrackContextUri[0] = '\0';
}

bool isSameTrack(const char *trackUri)
{

return strcmp(lastTrackUri, trackUri) == 0;
}

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

void setTrackContextUri(const char *trackContext)
{
if (trackContext == NULL)
{
lastTrackContextUri[0] = '\0';
}
else
{
strcpy(lastTrackContextUri, trackContext);
}
}

void spotifyRefreshToken(const char *refreshToken)
{
spotify.setRefreshToken(refreshToken);
Expand All @@ -50,32 +67,33 @@ void spotifyRefreshToken(const char *refreshToken)

void handleCurrentlyPlaying(CurrentlyPlaying currentlyPlaying)
{

// printCurrentlyPlayingToSerial(currentlyPlaying);

if (!isSameTrack(currentlyPlaying.trackUri))
if (currentlyPlaying.trackUri != NULL)
{
setTrackUri(currentlyPlaying.trackUri);
if (!isSameTrack(currentlyPlaying.trackUri))
{
setTrackUri(currentlyPlaying.trackUri);
setTrackContextUri(currentlyPlaying.contextUri);

// We have a new Song, need to update the text
sp_Display->printCurrentlyPlayingToScreen(currentlyPlaying);
}
// We have a new Song, need to update the text
sp_Display->printCurrentlyPlayingToScreen(currentlyPlaying);
}

albumArtChanged = sp_Display->processImageInfo(currentlyPlaying);
albumArtChanged = sp_Display->processImageInfo(currentlyPlaying);

sp_Display->displayTrackProgress(currentlyPlaying.progressMs, currentlyPlaying.durationMs);
sp_Display->displayTrackProgress(currentlyPlaying.progressMs, currentlyPlaying.durationMs);

if (currentlyPlaying.isPlaying)
{
// If we know at what millis the song started at, we can make a good guess
// at updating the progress bar more often than checking the API
songStartMillis = millis() - currentlyPlaying.progressMs;
songDuration = currentlyPlaying.durationMs;
}
else
{
// Song doesn't seem to be playing, do not update the progress
songStartMillis = 0;
if (currentlyPlaying.isPlaying)
{
// If we know at what millis the song started at, we can make a good guess
// at updating the progress bar more often than checking the API
songStartMillis = millis() - currentlyPlaying.progressMs;
songDuration = currentlyPlaying.durationMs;
}
else
{
// Song doesn't seem to be playing, do not update the progress
songStartMillis = 0;
}
}
}

Expand Down

0 comments on commit dd7b059

Please sign in to comment.