#include <Arduino.h>
// Global variables to store parsed data
int playerStatus = -1;
String volume = "";
String title = "";
String artist = "";
String position = "";
String timeRemaining = "";
// Function to parse and save received data
void parsePlayerctlData(String data) {
// Reset variables
playerStatus = -1;
volume = "";
title = "";
artist = "";
position = "";
timeRemaining = "";
// Parse status
int firstSemicolon = data.indexOf(';');
if (firstSemicolon != -1) {
playerStatus = data.substring(0, firstSemicolon).toInt();
data = data.substring(firstSemicolon + 1);
}
// Parse volume
int secondSemicolon = data.indexOf(';');
if (secondSemicolon != -1) {
volume = data.substring(0, secondSemicolon);
data = data.substring(secondSemicolon + 1);
}
// Parse title
int thirdSemicolon = data.indexOf(';');
if (thirdSemicolon != -1) {
title = data.substring(0, thirdSemicolon);
data = data.substring(thirdSemicolon + 1);
}
// Parse artist
int fourthSemicolon = data.indexOf(';');
if (fourthSemicolon != -1) {
artist = data.substring(0, fourthSemicolon);
data = data.substring(fourthSemicolon + 1);
}
// Parse position
int fifthSemicolon = data.indexOf(';');
if (fifthSemicolon != -1) {
position = data.substring(0, fifthSemicolon);
data = data.substring(fifthSemicolon + 1);
}
// Parse time remaining
int sixthSemicolon = data.indexOf(';');
if (sixthSemicolon != -1) {
timeRemaining = data.substring(0, sixthSemicolon);
}
}
// Optional: Function to print parsed data (for debugging)
void printParsedData() {
Serial.println("Parsed Playerctl Data:");
Serial.print("Status: "); Serial.println(playerStatus);
Serial.print("Volume: "); Serial.println(volume);
Serial.print("Title: "); Serial.println(title);
Serial.print("Artist: "); Serial.println(artist);
Serial.print("Position: "); Serial.println(position);
Serial.print("Time Remaining: "); Serial.println(timeRemaining);
}
void setup() {
Serial.begin(9600);
while (!Serial) {
; // Wait for serial port to connect
}
Serial.println("Arduino ready to receive playerctl data");
}
void loop() {
// Check if data is available
if (Serial.available()) {
String receivedData = Serial.readStringUntil('\n');
// Parse the received data
parsePlayerctlData(receivedData);
// Optional: Print parsed data for debugging
printParsedData();
// Add your custom logic here based on the parsed data
// For example:
if (playerStatus == 1) {
// Music is playing
// Do something when playing
} else if (playerStatus == 0) {
// Music is paused
// Do something when paused
}
}
}