From 2f753f20d2bbfcbe48e9ed003d0280ea9e9f9281 Mon Sep 17 00:00:00 2001 From: nitko12 Date: Wed, 23 Sep 2020 15:11:48 +0200 Subject: [PATCH] Added comments. --- .../05-Spotify_api_example.ino | 33 +++++++++++++++++-- .../05-Spotify_api_example/generatedUI.h | 2 ++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/examples/3. Projects/05-Spotify_api_example/05-Spotify_api_example.ino b/examples/3. Projects/05-Spotify_api_example/05-Spotify_api_example.ino index 0dcdc02..c742c34 100644 --- a/examples/3. Projects/05-Spotify_api_example/05-Spotify_api_example.ino +++ b/examples/3. Projects/05-Spotify_api_example/05-Spotify_api_example.ino @@ -6,10 +6,14 @@ #include #include +// Include auto generated UI code #include "generatedUI.h" +// Declare our Inkplate object Inkplate display(INKPLATE_1BIT); +// Fill these with your credentials + char ssid[] = ""; char password[] = ""; @@ -20,7 +24,9 @@ char clientSecret[] = ""; #define SPOTIFY_MARKET "IE" +// -------------------------------- +// Variables for storing data to be rendered to screen char name[128]; char title[128]; char album[128]; @@ -32,23 +38,29 @@ int elapsedTime = 0; int totalTime = 0; float elapsed = 0; +// Define how often to do a full screen refresh #define FULLREFRESH 10 int cnt = 0; +// Adjust these for your timezone const char *ntpServer = "pool.ntp.org"; const long gmtOffset_sec = 3600; const int daylightOffset_sec = 3600; +// Initiate Spotify API wrapper object WiFiClientSecure client; ArduinoSpotify spotify(client, clientId, clientSecret, SPOTIFY_REFRESH_TOKEN); -unsigned long delayBetweenRequests = 100; // Time between requests (1 minute) +// Time between requests to Spotify API +unsigned long delayBetweenRequests = 100; void setup() { + // Initialise serial and display objects Serial.begin(115200); display.begin(); + // Connect to WiFi WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); @@ -76,6 +88,7 @@ void setup() Serial.print("IP address: "); Serial.println(WiFi.localIP()); + // Set Spotify certificate client.setCACert(spotify_server_cert); Serial.println("Refreshing Access Tokens"); @@ -84,13 +97,16 @@ void setup() Serial.println("Failed to get access tokens"); } + // Get time from internet configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); + // Display blank screen display.display(); } void loop() { + // If Inkplate disconnected form WiFi try reconnecting and if that fails reset if (!display.isConnected()) { Serial.println("Reconnecting..."); @@ -99,8 +115,10 @@ void loop() ESP.restart(); } + // Get currently playing track info CurrentlyPlaying currentlyPlaying = spotify.getCurrentlyPlaying(SPOTIFY_MARKET); + // If all went well display to screen if (!currentlyPlaying.error) { parsePlayingData(currentlyPlaying); @@ -112,21 +130,25 @@ void loop() void parsePlayingData(CurrentlyPlaying currentlyPlaying) { + // Copy data to global variables strcpy(name, currentlyPlaying.firstArtistName); strcpy(title, currentlyPlaying.trackName); strcpy(album, currentlyPlaying.albumName); playing = currentlyPlaying.isPlaying; + // Find image whose dimensions are closest to 300 by 300 int mn = 0; for (int i = 1; i < currentlyPlaying.numImages; i++) if (abs(currentlyPlaying.albumImages[i].width - 300) + abs(currentlyPlaying.albumImages[i].width - 300) < abs(currentlyPlaying.albumImages[mn].width - 300) + abs(currentlyPlaying.albumImages[mn].width - 300)) mn = i; + // Copy data to global variables imgW = currentlyPlaying.albumImages[mn].width; strcpy(url, currentlyPlaying.albumImages[mn].url); + // Copy data to global variables elapsed = (float)currentlyPlaying.progressMs / (float)currentlyPlaying.duraitonMs; elapsedTime = currentlyPlaying.progressMs / 1000; totalTime = currentlyPlaying.duraitonMs / 1000; @@ -134,19 +156,20 @@ void parsePlayingData(CurrentlyPlaying currentlyPlaying) void updateScreenContent() { + // Initally clear the INkplate buffer display.clearDisplay(); + // Display playing info line1_color = line2_color = playing; triangle2_color = !playing; + // Limit text to 35 chars and add ... if needed strncpy(text1_content, name, min((int)35, (int)strlen(name))); strncpy(text2_content, title, min((int)35, (int)strlen(title))); strncpy(text3_content, album, min((int)35, (int)strlen(album))); - text1_content[min((int)35, (int)strlen(name))] = 0; text2_content[min((int)35, (int)strlen(title))] = 0; text3_content[min((int)35, (int)strlen(album))] = 0; - if (strlen(name) >= 35) strcat(text1_content, "..."); if (strlen(title) >= 35) @@ -154,6 +177,7 @@ void updateScreenContent() if (strlen(album) >= 35) strcat(text3_content, "..."); + // Functions to center text and image int16_t x1 = 0, y1 = 0; uint16_t w = 0, h = 0; @@ -173,6 +197,7 @@ void updateScreenContent() circle2_center_x = elapsed * (730 - 70) + 70; + // Display time digital_clock2_h = elapsedTime / 60; digital_clock2_m = elapsedTime % 60; @@ -186,8 +211,10 @@ void updateScreenContent() digital_clock4_m = timeinfo.tm_min; } + // Draw all rest auto generated UI mainDraw(); + // Every FULLREFRESH do a full refresh if (cnt++ % FULLREFRESH == 0) display.display(); else diff --git a/examples/3. Projects/05-Spotify_api_example/generatedUI.h b/examples/3. Projects/05-Spotify_api_example/generatedUI.h index fcd8ad4..540e87d 100644 --- a/examples/3. Projects/05-Spotify_api_example/generatedUI.h +++ b/examples/3. Projects/05-Spotify_api_example/generatedUI.h @@ -3,6 +3,8 @@ #include "Fonts/Roboto_Condensed_32.h" #include "Inkplate.h" +// Not fully auto generated, tweaked a little for 1bit mode + extern Inkplate display; int line0_start_x = 70;