#include <Arduino.h>
#include <U8g2lib.h>
#include "bitmaps.h"
// initialization for the 128x64px OLED display
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
int8_t progress = 2;
// global variables to store parsed data
// int playerStatus = -1;
// String volume = "";
// String title = "";
// String artist = "";
// String position = "";
// String timeRemaining = "";
int playerStatus = 1;
String volume = "54%";
String title = "Open Hearts";
String artist = "The Weeknd";
String position = "0:48";
String timeRemaining = "-3:06";
void setup() {
u8g2.begin();
}
void loop() {
u8g2.clearBuffer();
u8g2.setFontMode(1);
u8g2.setBitmapMode(1);
// top section
spotify_logo();
// bottom buttons
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.drawStr(6, 63, "Menu");
u8g2.drawXBM(0, 54, 37, 10, image_btn_bits);
u8g2.drawStr(98, 63, "Data");
u8g2.drawXBM(91, 54, 37, 10, image_btn_bits);
u8g2.drawXBM(45, 54, 38, 10, image_media_btn_bits);
if (playerStatus) pause();
else play();
// audio percentage
u8g2.setFont(u8g2_font_4x6_tr);
u8g2.drawStr(110, 9, volume.c_str());
u8g2.drawLine(125, 10, 109, 10);
u8g2.drawLine(0, 13, 127, 13); // top line
// media section
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.drawStr(getCenteredXPosition(title.c_str()), 29, title.c_str()); // track title
u8g2.drawStr(getCenteredXPosition(artist.c_str()), 39, artist.c_str()); // track artist
u8g2.setFont(u8g2_font_4x6_tr);
u8g2.drawStr(3, 46, position.c_str()); // track time start
u8g2.drawStr(getRightAlignedXPosition(timeRemaining.c_str()), 46, timeRemaining.c_str()); // track time end
u8g2.drawRFrame(3, 47, 122, 4, 2); // progress bar frame
u8g2.drawBox(4, 48, progress, 2); // progress barr
if (progress < 121) progress++;
else progress = 2;
u8g2.sendBuffer();
}
void play() {
u8g2.setDrawColor(2);
u8g2.drawXBM(62, 56, 6, 7, image_play_bits);
}
void pause() {
u8g2.setDrawColor(2);
u8g2.drawFrame(61, 56, 2, 7);
u8g2.drawFrame(65, 56, 2, 7);
}
void spotify_logo() {
u8g2.drawXBM(1, 1, 11, 11, image_spotify_bits); // logo
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.drawStr(14, 10, "Spotify");
}
int getCenteredXPosition(const char* text) {
int textWidth = u8g2.getStrWidth(text); // Get text width
int screenWidth = u8g2.getDisplayWidth(); // Get screen width
// Calculate the X position to center the text
return (screenWidth - textWidth) / 2;
}
int getRightAlignedXPosition(const char* text) {
int textWidth = u8g2.getStrWidth(text);
return 110 - textWidth + 15;
}