Now, controlled via MQTT a Playback of a melody can Start, Stop, Pause etc. Settings like speed, total duration, pauses etc can be set.
92 lines
2.2 KiB
C++
92 lines
2.2 KiB
C++
#line 1 "C:\\Users\\espi_\\Documents\\Arduino\\vesper\\functions.hpp"
|
|
#pragma once
|
|
|
|
extern std::vector<uint16_t> melody_steps;
|
|
|
|
void setMelodyAttributes(JsonDocument doc);
|
|
void loadMelodyInRAM(std::vector<uint16_t> &melody_steps);
|
|
|
|
|
|
void handleJSON(String * payload) {
|
|
|
|
JsonDocument doc;
|
|
DeserializationError error = deserializeJson(doc, payload);
|
|
|
|
if (error) {
|
|
Serial.print("deserializeJson() failed: ");
|
|
Serial.println(error.c_str());
|
|
return;
|
|
}
|
|
|
|
setMelodyAttributes(doc);
|
|
loadMelodyInRAM(melody_steps);
|
|
|
|
}
|
|
|
|
|
|
void setMelodyAttributes(JsonDocument doc){
|
|
|
|
|
|
melody.name = doc["name"].as<const char*>(); // Convert to std::string "name" : "eortastiko"
|
|
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<uint16_t>();
|
|
|
|
|
|
|
|
// Print Just for Debugging Purposes
|
|
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 loadMelodyInRAM(std::vector<uint16_t> &melody_steps) {
|
|
|
|
//if (!newMelody || !playing) return;
|
|
|
|
// read the file and save in RAM
|
|
|
|
std::string filePath = "/" + melody.name + ".bin";
|
|
Serial.println("New Melody Selected !!!");
|
|
Serial.println("Reading data from file...");
|
|
//Serial.println(filePath);
|
|
File bin_file = SPIFFS.open(filePath.c_str(), "r");
|
|
if (!bin_file) {
|
|
Serial.println("Failed to Open File");
|
|
return;
|
|
}
|
|
|
|
|
|
size_t fileSize = bin_file.size();
|
|
size_t steps = fileSize / 2;
|
|
melody_steps.resize(steps);
|
|
|
|
Serial.print("Opened File ! Size: ");
|
|
Serial.print(fileSize);
|
|
Serial.print(" Steps: ");
|
|
Serial.println(steps);
|
|
|
|
for (size_t i=0; i<steps; i++){
|
|
melody_steps[i] = bin_file.read() << 8 | bin_file.read();
|
|
}
|
|
|
|
for (size_t i=0; i<steps; i++){
|
|
Serial.print("Current Step: ");
|
|
Serial.printf("%03d // ", i);
|
|
Serial.print(" HEX Value: ");
|
|
Serial.printf("0x%04X\n", melody_steps[i]);
|
|
}
|
|
Serial.println("Closing File");
|
|
bin_file.close();
|
|
|
|
// closing the file
|
|
|
|
} |