Files
project-vesper/vesper.ino
bonamin a33f626dde 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.
2025-01-19 21:15:07 +02:00

137 lines
3.9 KiB
C++

#include <WiFi.h>
#include <AsyncMqttClient.h>
#include <ArduinoJson.h>
#include <FS.h>
#include <SPIFFS.h>
#include <string>
#include <Wire.h>
#include <Adafruit_PCF8574.h>
#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");
}
};
melody_attributes melody;
std::vector<uint16_t> melody_steps;
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 "MQTT_Message_Handling.hpp"
#include "MQTT_WiFi_Utilities.hpp"
#include "PlaybackControls.hpp"
#include "bellEngine.hpp"
TaskHandle_t bellEngineHandle = NULL;
void setup()
{
// 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");
return;
}
Serial.println("SPIFFS mounted successfully");
delay(50);
// Initialize WiFi and MQTT
WiFi.onEvent(WiFiEvent);
InitMqtt();
ConnectWiFi_STA();
delay(1000);
xTaskCreatePinnedToCore(
bellEngine, // Task function
"bellEngine", // Task name
8192, // Stack size
NULL, // Task input parameters
1, // Task priority, be carefull when changing this
&bellEngineHandle, // Task handle, add one if you want control over the task (resume or suspend the task)
1 // Core to run the task on
);
xTaskCreatePinnedToCore(
durationTimer, // Task function
"durationTimer", // Task name
8192, // Stack size
NULL, // Task input parameters
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
);
}
void loop()
{
}