Added Separate Timings for each Relay

The setting is saved on a file in:
/settings/relayTimings.json

Then during bootup, it's read and restored. 
Settings can be set via MQTT command on Topic: 
vesoer/*dev-id*/control/settings/relayTimers
using a json format. 
{ "b1":50, b2...}
where b1 is Bell 1, b2 Bell 2, etc..
This commit is contained in:
2025-01-21 11:42:23 +02:00
parent a33f626dde
commit 84534025f4
5 changed files with 150 additions and 65 deletions

View File

@@ -2,11 +2,25 @@
#include <vector>
extern melody_attributes melody;
extern uint16_t relayMask;
// Define a structure to track active solenoids
struct ActiveRelay {
uint8_t relayIndex; // Index of the relay
uint64_t activationTime; // Activation start time
uint16_t duration; // Duration for which it should remain active
};
// Array of durations for each relay (configure remotely)
uint16_t relayDurations[16] = {90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90};
// Vector to track active solenoids
std::vector<ActiveRelay> activeRelays;
void loop_playback(std::vector<uint16_t> &melody_steps);
void relayControlTask(void *param);
void itsHammerTime(uint16_t note);
void turnOffRelays(uint64_t now);
void bellEngine(void *parameter) {
@@ -26,6 +40,25 @@ void bellEngine(void *parameter) {
}
// Task to deactivate relays dynamically
void relayControlTask(void *param) {
while (true) {
uint64_t now = millis();
// Iterate through active relays and deactivate those whose duration has elapsed
for (auto it = activeRelays.begin(); it != activeRelays.end();) {
if (now - it->activationTime >= it->duration) {
relays.digitalWrite(it->relayIndex, HIGH); // Deactivate the relay
it = activeRelays.erase(it); // Remove from the active list
} else {
++it; // Move to the next relay
}
}
vTaskDelay(pdMS_TO_TICKS(10)); // Check every 10ms
}
}
void loop_playback(std::vector<uint16_t> &melody_steps) {
@@ -41,28 +74,20 @@ void loop_playback(std::vector<uint16_t> &melody_steps) {
Serial.println("SINGLE LOOP OVER.");
//if (!melody.isPlaying) break; // Stop playback only after completing the loop
}
}
void itsHammerTime(uint16_t note){
// THIS NEEDS REWORK, TO WAIT, DYNAMICLY PER RELAY
// MUST BE CONFIGURABLE REMOTELY
for (uint8_t i=0; i<16; i++) {
if (note & (1 << i)) {
relays.digitalWrite(i, LOW);
}
}
vTaskDelay(pdMS_TO_TICKS(relayDurations[0]));
for (uint8_t i=0; i<16; i++) {
relays.digitalWrite(i, HIGH);
}
// Function to activate relays for a specific note
void itsHammerTime(uint16_t note) {
uint64_t now = millis();
for (uint8_t i = 0; i < 16; i++) {
if (note & (1 << i)) { // Check if this relay needs to activate
relays.digitalWrite(i, LOW); // Activate the relay
// Add to the activeRelays list
activeRelays.push_back({i, now, relayDurations[i]});
}
}
}