Added initial Melody Playback Ability

Now, controlled via MQTT a Playback of a melody can Start, Stop, Pause 
etc. 

Settings like speed, total duration, pauses etc can be set.
This commit is contained in:
2025-01-19 21:15:07 +02:00
parent 2cbbc8d591
commit a33f626dde
36 changed files with 4263 additions and 236 deletions

View File

@@ -4,40 +4,96 @@
#include <FS.h>
#include <SPIFFS.h>
#include <string>
#include <Wire.h>
#include <Adafruit_PCF8574.h>
#define DEV_ID "id-96638646"
#define PCF8574_ADDR 0x24
Adafruit_PCF8574 relays;
AsyncMqttClient mqttClient;
class melody_attributes {
public:
uint16_t id; // The (internal) ID of the selected melody. Not specificly used anywhere atm. Might be used later.
std::string name; // Name of the Melody saved. Will be used to read the file: /name.bin
uint16_t speed; // Time to wait per beat. (In Miliseconds)
uint32_t duration = 15000; // Total Duration that program will run (In Miliseconds)
uint32_t loop_duration = 0; // Duration of the playback per segment
uint32_t interval = 0; // Indicates the Duration of the Interval between finished segments, IF "inf" is true
bool infinite_play = false; // Infinite Loop Indicator (If True the melody will loop forever or until stoped, with pauses of "interval" in between loops)
bool isPlaying = false;; // Indicates if the Melody is actually Playing right now.
bool isPaused = false; // If playing, indicates if the Melody is Paused
uint64_t startTime = 0; // The time-point the Melody started Playing
uint64_t loopStartTime = 0; // The time-point the current segment started Playing
bool hardStop = false; // Flags a hardstop, immediately.
uint64_t pauseTime = 0; // The time-point the melody paused
void play() {
isPlaying = true;
hardStop = false;
startTime = loopStartTime = millis();
Serial.println("Plbck: PLAY");
}
void forceStop() {
hardStop = true;
isPlaying = false;
Serial.println("Plbck: FORCE STOP");
}
void stop() {
hardStop = false;
isPlaying = false;
Serial.println("Plbck: STOP");
}
void pause() {
isPaused = true;
Serial.println("Plbck: PAUSE");
}
void unpause() {
isPaused = false;
loopStartTime = millis();
Serial.println("Plbck: RESUME");
}
struct melody_attributes {
std::string name; // Contains the name of each Melody saved
uint16_t id; // The (internal) ID of the selected melody
uint32_t duration; // Indicates the total Duration in Minutes
bool infinite_play; // Infinite Loop Indicator (If True the melody will loop forever or until stoped, with pauses of "interval duration in between loops")
uint16_t interval_duration; // Indicates the Duration of the Interval between finished loops, IF "inf" is true
uint8_t speed; // Indicates the Speed in 9 Steps. 1-9 (Steps can be adjusted in the bellEngine function)
};
melody_attributes melody;
std::vector<uint16_t> melody_steps;
volatile bool playing = false;
uint16_t relayMask = 0; // Bitmask indicating which relays to activate
uint8_t relayDurations[16] = {5,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100}; // Deactivation timers for each relay in milliseconds
#include "config.h"
#include "functions.hpp"
#include "config.h" // Sustituir con datos de vuestra red
#include "MQTT.hpp"
#include "ESP32_Utils.hpp"
#include "ESP32_Utils_MQTT_Async.hpp"
#include "melody_handling.hpp"
#include "MQTT_Message_Handling.hpp"
#include "MQTT_WiFi_Utilities.hpp"
#include "PlaybackControls.hpp"
#include "bellEngine.hpp"
TaskHandle_t myTaskHandle = NULL;
TaskHandle_t bellEngineHandle = NULL;
void setup()
{
// Initialize Serial Communication (for debuggin)
// Initialize Serial Communications & I2C Bus (for debugging)
Serial.begin(115200);
delay(50);
// Initialize PCF8574
Wire.begin(4,15);
relays.begin(PCF8574_ADDR, &Wire);
// Initialize Relays
for (uint8_t p=0; p<6; p++){
relays.pinMode(p, OUTPUT);
relays.digitalWrite(p, HIGH);
}
// Initialize SPIFFS
if (!SPIFFS.begin(true)) { // 'true' means format SPIFFS if initialization fails
Serial.println("Failed to mount SPIFFS");
@@ -45,7 +101,8 @@ void setup()
}
Serial.println("SPIFFS mounted successfully");
delay(50);
// Initialize WiFi and MQTT
WiFi.onEvent(WiFiEvent);
InitMqtt();
@@ -58,18 +115,16 @@ void setup()
8192, // Stack size
NULL, // Task input parameters
1, // Task priority, be carefull when changing this
&myTaskHandle, // Task handle, add one if you want control over the task (resume or suspend the task)
&bellEngineHandle, // Task handle, add one if you want control over the task (resume or suspend the task)
1 // Core to run the task on
);
vTaskSuspend(myTaskHandle);
xTaskCreatePinnedToCore(
tempEngine, // Task function
"tempEngine", // Task name
xTaskCreatePinnedToCore(
durationTimer, // Task function
"durationTimer", // Task name
8192, // Stack size
NULL, // Task input parameters
1, // Task priority, be carefull when changing this
2, // Task priority, be carefull when changing this
NULL, // Task handle, add one if you want control over the task (resume or suspend the task)
1 // Core to run the task on
);
@@ -78,4 +133,4 @@ void setup()
void loop()
{
}
}