Added Melody Struct & Ajusted MQTT Subs and Deserialization
A new Melody Struct was added containing the melody's main attributes. The MQTT Topic Subscriptions were updated to a more proper format. Also added DEV_ID that corresponds to the device's ID or Serial Number. Chnaged the Deserialization function to actually adjust the MelodyStruct's attributes. Added basic play/stop functionality.
This commit is contained in:
71
functions.hpp
Normal file
71
functions.hpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
extern volatile bool playing;
|
||||
|
||||
|
||||
void playback(JsonDocument doc){
|
||||
if (doc["playback"].as<bool>()){
|
||||
playing = true;
|
||||
Serial.println("START Playback!");
|
||||
}
|
||||
else if (doc["playback"].as<bool>()){
|
||||
playing = false;
|
||||
Serial.println("STOP Playback!");
|
||||
}
|
||||
}
|
||||
|
||||
void selectMelody(JsonDocument doc){
|
||||
|
||||
melody.name = doc["name"].as<const char*>(); // Convert to std::string
|
||||
melody.id = doc["id"].as<uint16_t>();
|
||||
melody.duration = doc["duration"].as<uint32_t>();
|
||||
melody.infinite_play = doc["infinite"].as<bool>();
|
||||
melody.interval_duration = doc["inter_dur"].as<uint16_t>();
|
||||
melody.speed = doc["speed"].as<uint8_t>();
|
||||
|
||||
Serial.printf("Name: %s, ID: %d, Duration: %lu, Inf: %s, Inter: %d, Speed: %d\n",
|
||||
melody.name.c_str(),
|
||||
melody.id,
|
||||
melody.duration,
|
||||
melody.infinite_play ? "true" : "false",
|
||||
melody.interval_duration,
|
||||
melody.speed);
|
||||
|
||||
}
|
||||
|
||||
void handleJSON(String payload) {
|
||||
|
||||
JsonDocument doc;
|
||||
DeserializationError error = deserializeJson(doc, payload);
|
||||
|
||||
if (error) {
|
||||
Serial.print("deserializeJson() failed: ");
|
||||
Serial.println(error.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
selectMelody(doc);
|
||||
playback(doc);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
possible topics:
|
||||
|
||||
vesper/client-id/control
|
||||
/play
|
||||
/stop
|
||||
|
||||
vesper/client-id/select_melody
|
||||
|
||||
vesper/client-id/update_melody
|
||||
|
||||
vesper/client-id/add_melody
|
||||
*/
|
||||
Reference in New Issue
Block a user