Added comments.

This commit is contained in:
nitko12 2020-09-23 15:11:48 +02:00
parent de0e62c932
commit 2f753f20d2
2 changed files with 32 additions and 3 deletions

View File

@ -6,10 +6,14 @@
#include <WiFi.h> #include <WiFi.h>
#include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
// Include auto generated UI code
#include "generatedUI.h" #include "generatedUI.h"
// Declare our Inkplate object
Inkplate display(INKPLATE_1BIT); Inkplate display(INKPLATE_1BIT);
// Fill these with your credentials
char ssid[] = ""; char ssid[] = "";
char password[] = ""; char password[] = "";
@ -20,7 +24,9 @@ char clientSecret[] = "";
#define SPOTIFY_MARKET "IE" #define SPOTIFY_MARKET "IE"
// --------------------------------
// Variables for storing data to be rendered to screen
char name[128]; char name[128];
char title[128]; char title[128];
char album[128]; char album[128];
@ -32,23 +38,29 @@ int elapsedTime = 0;
int totalTime = 0; int totalTime = 0;
float elapsed = 0; float elapsed = 0;
// Define how often to do a full screen refresh
#define FULLREFRESH 10 #define FULLREFRESH 10
int cnt = 0; int cnt = 0;
// Adjust these for your timezone
const char *ntpServer = "pool.ntp.org"; const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600; const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600; const int daylightOffset_sec = 3600;
// Initiate Spotify API wrapper object
WiFiClientSecure client; WiFiClientSecure client;
ArduinoSpotify spotify(client, clientId, clientSecret, SPOTIFY_REFRESH_TOKEN); 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() void setup()
{ {
// Initialise serial and display objects
Serial.begin(115200); Serial.begin(115200);
display.begin(); display.begin();
// Connect to WiFi
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
@ -76,6 +88,7 @@ void setup()
Serial.print("IP address: "); Serial.print("IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
// Set Spotify certificate
client.setCACert(spotify_server_cert); client.setCACert(spotify_server_cert);
Serial.println("Refreshing Access Tokens"); Serial.println("Refreshing Access Tokens");
@ -84,13 +97,16 @@ void setup()
Serial.println("Failed to get access tokens"); Serial.println("Failed to get access tokens");
} }
// Get time from internet
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
// Display blank screen
display.display(); display.display();
} }
void loop() void loop()
{ {
// If Inkplate disconnected form WiFi try reconnecting and if that fails reset
if (!display.isConnected()) if (!display.isConnected())
{ {
Serial.println("Reconnecting..."); Serial.println("Reconnecting...");
@ -99,8 +115,10 @@ void loop()
ESP.restart(); ESP.restart();
} }
// Get currently playing track info
CurrentlyPlaying currentlyPlaying = spotify.getCurrentlyPlaying(SPOTIFY_MARKET); CurrentlyPlaying currentlyPlaying = spotify.getCurrentlyPlaying(SPOTIFY_MARKET);
// If all went well display to screen
if (!currentlyPlaying.error) if (!currentlyPlaying.error)
{ {
parsePlayingData(currentlyPlaying); parsePlayingData(currentlyPlaying);
@ -112,21 +130,25 @@ void loop()
void parsePlayingData(CurrentlyPlaying currentlyPlaying) void parsePlayingData(CurrentlyPlaying currentlyPlaying)
{ {
// Copy data to global variables
strcpy(name, currentlyPlaying.firstArtistName); strcpy(name, currentlyPlaying.firstArtistName);
strcpy(title, currentlyPlaying.trackName); strcpy(title, currentlyPlaying.trackName);
strcpy(album, currentlyPlaying.albumName); strcpy(album, currentlyPlaying.albumName);
playing = currentlyPlaying.isPlaying; playing = currentlyPlaying.isPlaying;
// Find image whose dimensions are closest to 300 by 300
int mn = 0; int mn = 0;
for (int i = 1; i < currentlyPlaying.numImages; i++) for (int i = 1; i < currentlyPlaying.numImages; i++)
if (abs(currentlyPlaying.albumImages[i].width - 300) + abs(currentlyPlaying.albumImages[i].width - 300) < 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)) abs(currentlyPlaying.albumImages[mn].width - 300) + abs(currentlyPlaying.albumImages[mn].width - 300))
mn = i; mn = i;
// Copy data to global variables
imgW = currentlyPlaying.albumImages[mn].width; imgW = currentlyPlaying.albumImages[mn].width;
strcpy(url, currentlyPlaying.albumImages[mn].url); strcpy(url, currentlyPlaying.albumImages[mn].url);
// Copy data to global variables
elapsed = (float)currentlyPlaying.progressMs / (float)currentlyPlaying.duraitonMs; elapsed = (float)currentlyPlaying.progressMs / (float)currentlyPlaying.duraitonMs;
elapsedTime = currentlyPlaying.progressMs / 1000; elapsedTime = currentlyPlaying.progressMs / 1000;
totalTime = currentlyPlaying.duraitonMs / 1000; totalTime = currentlyPlaying.duraitonMs / 1000;
@ -134,19 +156,20 @@ void parsePlayingData(CurrentlyPlaying currentlyPlaying)
void updateScreenContent() void updateScreenContent()
{ {
// Initally clear the INkplate buffer
display.clearDisplay(); display.clearDisplay();
// Display playing info
line1_color = line2_color = playing; line1_color = line2_color = playing;
triangle2_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(text1_content, name, min((int)35, (int)strlen(name)));
strncpy(text2_content, title, min((int)35, (int)strlen(title))); strncpy(text2_content, title, min((int)35, (int)strlen(title)));
strncpy(text3_content, album, min((int)35, (int)strlen(album))); strncpy(text3_content, album, min((int)35, (int)strlen(album)));
text1_content[min((int)35, (int)strlen(name))] = 0; text1_content[min((int)35, (int)strlen(name))] = 0;
text2_content[min((int)35, (int)strlen(title))] = 0; text2_content[min((int)35, (int)strlen(title))] = 0;
text3_content[min((int)35, (int)strlen(album))] = 0; text3_content[min((int)35, (int)strlen(album))] = 0;
if (strlen(name) >= 35) if (strlen(name) >= 35)
strcat(text1_content, "..."); strcat(text1_content, "...");
if (strlen(title) >= 35) if (strlen(title) >= 35)
@ -154,6 +177,7 @@ void updateScreenContent()
if (strlen(album) >= 35) if (strlen(album) >= 35)
strcat(text3_content, "..."); strcat(text3_content, "...");
// Functions to center text and image
int16_t x1 = 0, y1 = 0; int16_t x1 = 0, y1 = 0;
uint16_t w = 0, h = 0; uint16_t w = 0, h = 0;
@ -173,6 +197,7 @@ void updateScreenContent()
circle2_center_x = elapsed * (730 - 70) + 70; circle2_center_x = elapsed * (730 - 70) + 70;
// Display time
digital_clock2_h = elapsedTime / 60; digital_clock2_h = elapsedTime / 60;
digital_clock2_m = elapsedTime % 60; digital_clock2_m = elapsedTime % 60;
@ -186,8 +211,10 @@ void updateScreenContent()
digital_clock4_m = timeinfo.tm_min; digital_clock4_m = timeinfo.tm_min;
} }
// Draw all rest auto generated UI
mainDraw(); mainDraw();
// Every FULLREFRESH do a full refresh
if (cnt++ % FULLREFRESH == 0) if (cnt++ % FULLREFRESH == 0)
display.display(); display.display();
else else

View File

@ -3,6 +3,8 @@
#include "Fonts/Roboto_Condensed_32.h" #include "Fonts/Roboto_Condensed_32.h"
#include "Inkplate.h" #include "Inkplate.h"
// Not fully auto generated, tweaked a little for 1bit mode
extern Inkplate display; extern Inkplate display;
int line0_start_x = 70; int line0_start_x = 70;