This page provides a comprehensive overview of the Spotify MacroBoard source code, explaining the main components, program flow, and key functions.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/benz206/SpotifyMacroBoard/llms.txt
Use this file to discover all available pages before exploring further.
Code structure
The Spotify MacroBoard code is organized into several functional sections:- Setup and initialization: Hardware configuration and WiFi connection
- Display management: OLED screen updates and graphics
- LED control: RGB LED strip animations and color transitions
- Button handling: Input detection and action triggering
- API communication: HTTP requests to Spotify control server
- State management: Tracking playback state and metadata
Libraries and dependencies
The code uses several libraries for hardware control and networking:| Library | Purpose |
|---|---|
Adafruit_GFX | Graphics primitives for drawing |
Adafruit_SSD1306 | OLED display driver (128x64, I2C) |
FastLED | WS2812B LED strip control |
SMBCredentials.h | User credentials and pin definitions |
WiFi | ESP32 WiFi connectivity |
WiFiClientSecure | HTTPS/TLS client for secure API calls |
Wire | I2C communication protocol |
Global objects and variables
Hardware objects
wifiClient: Secure HTTPS client for API communicationI2C: I2C bus interface for OLED displaydisplay: SSD1306 OLED display object (128x64 pixels)LEDs: Array of RGB LED objects (20 LEDs)
Button state tracking
HIGH when not pressed and LOW when pressed.
Timing control
Playback state
Setup function
Thesetup() function runs once when the ESP32 boots and initializes all hardware components.
Configure GPIO pins
Initialize OLED display
Connect to WiFi
Indicate successful connection
Main loop
Theloop() function continuously monitors buttons, updates the display, and synchronizes playback state.
Loop execution flow
- Button detection: Scans all 7 buttons for state changes (edge detection)
- WiFi monitoring: Updates signal strength indicator every 10 seconds
- Track sync: Fetches current track metadata every 5 seconds
- Progress tracking: Updates progress bar every second (increments locally if playing)
- LED animation: Smoothly fades LEDs when track changes (4ms per step)
The loop uses non-blocking delays with
millis() timing. This allows multiple tasks to run concurrently without freezing the system.Key functions
Button handling
Each button triggers a specific function through a function pointer array:updateState() with an action character:
API communication
TheupdateState() function sends control commands to the server:
API endpoint format:
GET /api/manageState/{PASSWORD}/{ACTION}Actions: playPause, back, skip, vdec, vinc, loop, shuffleFetching current track
TheupdateCurrent() function retrieves track metadata:
Expected JSON response format
Expected JSON response format
JSON parsing
The code uses a simple string-based JSON parser:LED color transitions
When a new track starts, LEDs smoothly transition to the album’s dominant color:Display updates
TheupdateScreen() function draws the top status bar with WiFi signal and volume:
Progress bar rendering
TheupdateTime() function draws the playback progress bar:
Error indication
The code uses LED colors to indicate different error states:| LED Color | Meaning | Cause |
|---|---|---|
| Orange-red | Display initialization failed | OLED not detected at I2C address 0x3C |
| Red | Connection failed | Cannot connect to server |
| Yellow | Request timeout | Server not responding within 5 seconds |
| Orange | Invalid response | Malformed HTTP headers |
| Green | Normal operation | Successfully connected and syncing |
Memory usage
The compiled sketch has the following resource utilization on ESP32:Performance considerations
Non-blocking architecture
All timing usesmillis() instead of delay(), allowing:
- Responsive button input
- Smooth LED animations
- Concurrent display updates
- Uninterrupted network communication
Update frequencies
| Task | Frequency | Justification |
|---|---|---|
| Button scan | Every loop (~1000 Hz) | Ensures no button presses are missed |
| Track metadata | 5 seconds | Balances freshness with API load |
| Progress bar | 1 second | Sufficient for visual feedback |
| WiFi signal | 10 seconds | Rarely changes quickly |
| LED fade | 4 milliseconds | Smooth visual transition |
Connection management
HTTP connections useKeep-Alive to reduce overhead:
Customization opportunities
Adjust update intervals
Modify timing constants to change update frequencies:Change LED brightness
Adjust LED brightness inSMBCredentials.h:
Modify button actions
Reorder or replace button functions:Next steps
Now that you understand the code structure:Hardware assembly
Build the physical MacroBoard
Troubleshooting
Resolve common issues