Added Websocket Support Added Universal Message Handling for both MQTT and WS Added Timekeeper Class, that handles Physical Clock and Scheduling Added Bell Assignment Settings, Note to Bell mapping
90 lines
2.1 KiB
C++
90 lines
2.1 KiB
C++
#pragma once
|
|
|
|
extern melody_attributes melody;
|
|
|
|
bool timeToStop(unsigned long now);
|
|
bool timeToPause(unsigned long now);
|
|
bool timeToResume(unsigned long now);
|
|
void durationTimer(void *param);
|
|
|
|
// Timer TASK to control playback state
|
|
void durationTimer(void *param) {
|
|
|
|
// Task Setup
|
|
Serial.print("Main millis: ");
|
|
Serial.println(millis());
|
|
|
|
|
|
// Task Loop
|
|
while (true) {
|
|
|
|
unsigned long now = millis();
|
|
|
|
if (timeToStop(now)) {
|
|
melody.stop();
|
|
} else if (timeToPause(now)) {
|
|
melody.pause();
|
|
} else if (timeToResume(now)) {
|
|
melody.unpause();
|
|
}
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(100)); // Check every 100ms
|
|
}
|
|
}
|
|
|
|
// Check if it's time to stop playback
|
|
bool timeToStop(unsigned long now) {
|
|
if (melody.isPlaying) {
|
|
uint64_t stopTime = melody.startTime + melody.duration;
|
|
if (now >= stopTime) {
|
|
Serial.println("TIMER: Total Duration Reached");
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Check if it's time to pause playback
|
|
bool timeToPause(unsigned long now) {
|
|
if (melody.isPlaying && melody.loop_duration > 0) {
|
|
uint64_t pauseTimeLimit = melody.loopStartTime + melody.loop_duration;
|
|
Serial.print("PTL: ");
|
|
Serial.print(pauseTimeLimit);
|
|
Serial.print(" // NOW: ");
|
|
Serial.println(now);
|
|
if (now >= pauseTimeLimit && !melody.isPaused) {
|
|
Serial.println("TIMER: Segment Duration Reached");
|
|
melody.pauseTime = now;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Check if it's time to resume playback
|
|
bool timeToResume(unsigned long now) {
|
|
if (melody.isPaused) {
|
|
uint64_t resumeTime = melody.pauseTime + melody.interval;
|
|
if (now >= resumeTime) {
|
|
Serial.println("TIMER: Pause Duration Reached");
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Handles Incoming Commands to PLAY or STOP
|
|
void handlePlaybackCommands(char * command){
|
|
Serial.print("INCOMING COMMAND: ");
|
|
Serial.println(command);
|
|
if (command[0] == '1') {
|
|
melody.play();
|
|
PublishMqtt("OK - PLAY");
|
|
} else if (command[0] == '0') {
|
|
melody.forceStop();
|
|
PublishMqtt("OK - STOP");
|
|
}
|
|
|
|
|
|
}
|