From 0ac63e82edaf3f71a455206cccb4158f7e558447 Mon Sep 17 00:00:00 2001 From: bonamin Date: Sun, 12 Jul 2026 18:44:52 +0300 Subject: [PATCH] feat(F-040,F-041,F-042): relay.set_config, output numbering fix, clock batch commands Enforce 1-based output numbering throughout the entire stack. Stored values, MQTT wire format, and console UI are all 1-based; the single 0-based conversion happens only in OutputManager::getPhysicalOutput(). Fixed createDefaultBellConfig (was setting identity 0-based map instead of 0=disabled), updateBellDurations and updateBellOutputs (support both v2 0/1 and v1 b1/b2 keys), all call sites that bypassed getPhysicalOutput, and fireClockOutput conversion. New commands: - relay.set_config: combined durations + outputs in one round trip (F-040) - clock.set_config: enabled/c1/c2/pulse_duration/pause_duration in one call (F-042) - clock.set_alerts_config: alert type, bells, and silence windows in one call (F-042) Feature catalog updated with F-040, F-041, F-042 detail sections. Co-Authored-By: Claude Sonnet 4.6 --- docs/features/feature-catalog.md | 40 ++++++++ .../Handlers/ClockHandler/ClockHandler.cpp | 97 ++++++++++++++++++- .../Handlers/ClockHandler/ClockHandler.hpp | 2 + .../Handlers/RelayHandler/RelayHandler.cpp | 44 ++++++++- .../Handlers/RelayHandler/RelayHandler.hpp | 1 + src/ConfigManager/ConfigManager.cpp | 26 ++--- src/OutputManager/OutputManager.cpp | 93 +++++++++--------- src/TimeKeeper/TimeKeeper.cpp | 6 +- 8 files changed, 241 insertions(+), 68 deletions(-) diff --git a/docs/features/feature-catalog.md b/docs/features/feature-catalog.md index a0a0448..3045924 100644 --- a/docs/features/feature-catalog.md +++ b/docs/features/feature-catalog.md @@ -49,6 +49,9 @@ Every feature — user-facing or significant internal mechanism — is listed he | F-037 | Network status extended (SSID + MAC in `network.status`) | v2.1.0 | Stable | | F-038 | Bell test fire commands (`relay.test_bell` / `relay.test_batch`) | v2.1.0 | Stable | | F-039 | Clock output test fire commands (`clock.test_c1` / `clock.test_c2`) | v2.1.0 | Stable | +| F-040 | Combined relay config command (`relay.set_config`) | v2.1.1 | Stable | +| F-041 | Output numbering convention enforced — 1-based everywhere except raw chip pin | v2.1.1 | Stable | +| F-042 | Combined clock config commands (`clock.set_config` / `clock.set_alerts_config`) | v2.1.1 | Stable | --- @@ -526,3 +529,40 @@ Both use `OutputManager::fireClockOutput()` which resolves the physical pin from `ClockHandler` constructor now accepts an optional `OutputManager*` (defaults to `nullptr`); the router passes `_outputManager` so the test commands are live when the output manager is registered. Subsystem: `src/Communication/Handlers/ClockHandler/ClockHandler.cpp` + +--- + +### F-040 — Combined relay config command (`relay.set_config`) + +Single command that sets bell durations and/or output assignments in one round trip, replacing the need to send `relay.set_durations` + `relay.set_outputs` separately. Both `durations` and `outputs` fields are optional — at least one must be present. Partial updates accepted (only the fields provided are changed). + +Subsystem: `src/Communication/Handlers/RelayHandler/RelayHandler.cpp` + +--- + +### F-041 — Output numbering convention enforced — 1-based everywhere except raw chip pin + +Bell outputs are stored and communicated as **1-based numbers** throughout the entire stack (config, MQTT protocol, console UI). The single conversion to 0-based chip pin index happens only in `OutputManager::getPhysicalOutput()`, which subtracts 1 immediately before calling the hardware driver. + +`0` = disabled. `255` = unconfigured sentinel (internal only, never sent over wire). + +All call sites (BellEngine, RelayHandler test commands, TimeKeeper alerts) now go through `getPhysicalOutput()` rather than calling `fireOutputForDuration()` with a raw stored value. `relay.test_output` is the sole exception — it is a raw hardware diagnostic that intentionally takes a 0-based chip pin directly, and is documented as such. + +`createDefaultBellConfig()` initialised outputs to `i` (0-based identity map) — corrected to `0` (all disabled). + +Subsystems: `src/OutputManager/OutputManager.cpp`, `src/ConfigManager/ConfigManager.cpp`, `src/Communication/Handlers/RelayHandler/RelayHandler.cpp`, `src/TimeKeeper/TimeKeeper.cpp` + +--- + +### F-042 — Combined clock config commands (`clock.set_config` / `clock.set_alerts_config`) + +Two batch commands that each collapse multiple individual clock commands into one round trip: + +| Command | Replaces | +|---|---| +| `clock.set_config` | `clock.enable/disable` + `clock.set_outputs` + `clock.set_timings` | +| `clock.set_alerts_config` | `clock.set_alerts` + `clock.set_silence` | + +All fields optional — at least one required. Single `saveClockConfig()` call per command regardless of how many fields are set. + +Subsystem: `src/Communication/Handlers/ClockHandler/ClockHandler.cpp` diff --git a/src/Communication/Handlers/ClockHandler/ClockHandler.cpp b/src/Communication/Handlers/ClockHandler/ClockHandler.cpp index 96c71d7..02a8a00 100644 --- a/src/Communication/Handlers/ClockHandler/ClockHandler.cpp +++ b/src/Communication/Handlers/ClockHandler/ClockHandler.cpp @@ -14,6 +14,7 @@ std::vector ClockHandler::commands() const { return { "clock.set_outputs", "clock.set_timings", "clock.set_alerts", "clock.set_backlight", "clock.set_silence", + "clock.set_config", "clock.set_alerts_config", "clock.set_time", "clock.set_face", "clock.set_timezone", "clock.get_timezone", "clock.sync_ntp", "clock.enable", "clock.disable", @@ -24,9 +25,11 @@ std::vector ClockHandler::commands() const { } CommandResult ClockHandler::execute(const CommandEnvelope& envelope) { - if (envelope.command == "clock.set_outputs") return handleSetOutputs(envelope); - if (envelope.command == "clock.set_timings") return handleSetTimings(envelope); - if (envelope.command == "clock.set_alerts") return handleSetAlerts(envelope); + if (envelope.command == "clock.set_outputs") return handleSetOutputs(envelope); + if (envelope.command == "clock.set_timings") return handleSetTimings(envelope); + if (envelope.command == "clock.set_alerts") return handleSetAlerts(envelope); + if (envelope.command == "clock.set_config") return handleSetConfig(envelope); + if (envelope.command == "clock.set_alerts_config") return handleSetAlertsConfig(envelope); if (envelope.command == "clock.set_backlight") return handleSetBacklight(envelope); if (envelope.command == "clock.set_silence") return handleSetSilence(envelope); if (envelope.command == "clock.set_time") return handleSetTime(envelope); @@ -277,4 +280,92 @@ CommandResult ClockHandler::handleTestC2(const CommandEnvelope& e) { return CommandResult::ok("clock.test_c2", doc); } +CommandResult ClockHandler::handleSetConfig(const CommandEnvelope& e) { + JsonVariantConst c = e.contents.as(); + + bool hasEnabled = c["enabled"].is(); + bool hasC1 = c["c1"].is(); + bool hasC2 = c["c2"].is(); + bool hasPulseDuration = c["pulse_duration"].is(); + bool hasPauseDuration = c["pause_duration"].is(); + + if (!hasEnabled && !hasC1 && !hasC2 && !hasPulseDuration && !hasPauseDuration) { + return CommandResult::err("clock.set_config", + "Provide at least one field: enabled, c1, c2, pulse_duration, pause_duration"); + } + + if (hasEnabled) _configManager.setClockEnabled(c["enabled"].as()); + if (hasC1 || hasC2) { + JsonDocument tmp; + if (hasC1) tmp["c1output"] = c["c1"].as(); + if (hasC2) tmp["c2output"] = c["c2"].as(); + _configManager.updateClockOutputs(tmp.as()); + } + if (hasPulseDuration) _configManager.setClockPulseDuration(c["pulse_duration"].as()); + if (hasPauseDuration) _configManager.setClockPauseDuration(c["pause_duration"].as()); + + if (!_configManager.saveClockConfig()) { + return CommandResult::err("clock.set_config", "Settings applied but failed to save to SD"); + } + LOG_INFO(TAG, "clock.set_config applied"); + return CommandResult::ok("clock.set_config", "Clock configuration updated and saved"); +} + +CommandResult ClockHandler::handleSetAlertsConfig(const CommandEnvelope& e) { + JsonVariantConst c = e.contents.as(); + + bool hasAlerts = c["alert_type"].is() || + c["alert_interval"].is() || + c["hour_bell"].is() || + c["half_bell"].is() || + c["quarter_bell"].is(); + bool hasSilence = c["daytime_silence"].is() || + c["daytime_on"].is() || + c["daytime_off"].is() || + c["night_silence"].is() || + c["night_on"].is() || + c["night_off"].is(); + + if (!hasAlerts && !hasSilence) { + return CommandResult::err("clock.set_alerts_config", + "Provide at least one of: alert_type, alert_interval, hour_bell, half_bell, quarter_bell, " + "daytime_silence, daytime_on, daytime_off, night_silence, night_on, night_off"); + } + + if (hasAlerts) { + // Map wire-format snake_case keys to what updateClockAlerts expects (camelCase) + JsonDocument alertsDoc; + if (c["alert_type"].is()) alertsDoc["alertType"] = c["alert_type"]; + if (c["alert_interval"].is()) alertsDoc["alertRingInterval"] = c["alert_interval"]; + if (c["hour_bell"].is()) alertsDoc["hourBell"] = c["hour_bell"]; + if (c["half_bell"].is()) alertsDoc["halfBell"] = c["half_bell"]; + if (c["quarter_bell"].is()) alertsDoc["quarterBell"] = c["quarter_bell"]; + _configManager.updateClockAlerts(alertsDoc.as()); + } + + if (hasSilence) { + // Map wire-format flat keys to nested structure that updateClockSilence expects + JsonDocument silenceDoc; + if (c["daytime_silence"].is() || c["daytime_on"].is() || c["daytime_off"].is()) { + JsonObject dt = silenceDoc["daytime"].to(); + if (c["daytime_silence"].is()) dt["enabled"] = c["daytime_silence"]; + if (c["daytime_on"].is()) dt["onTime"] = c["daytime_on"]; + if (c["daytime_off"].is()) dt["offTime"] = c["daytime_off"]; + } + if (c["night_silence"].is() || c["night_on"].is() || c["night_off"].is()) { + JsonObject nt = silenceDoc["nighttime"].to(); + if (c["night_silence"].is()) nt["enabled"] = c["night_silence"]; + if (c["night_on"].is()) nt["onTime"] = c["night_on"]; + if (c["night_off"].is()) nt["offTime"] = c["night_off"]; + } + _configManager.updateClockSilence(silenceDoc.as()); + } + + if (!_configManager.saveClockConfig()) { + return CommandResult::err("clock.set_alerts_config", "Settings applied but failed to save to SD"); + } + LOG_INFO(TAG, "clock.set_alerts_config applied"); + return CommandResult::ok("clock.set_alerts_config", "Clock alerts and silence configuration updated and saved"); +} + #endif // HAS_RTC diff --git a/src/Communication/Handlers/ClockHandler/ClockHandler.hpp b/src/Communication/Handlers/ClockHandler/ClockHandler.hpp index 7b4e4ef..6fcf124 100644 --- a/src/Communication/Handlers/ClockHandler/ClockHandler.hpp +++ b/src/Communication/Handlers/ClockHandler/ClockHandler.hpp @@ -39,6 +39,8 @@ private: CommandResult handleGetConfig(const CommandEnvelope& e); CommandResult handleTestC1(const CommandEnvelope& e); CommandResult handleTestC2(const CommandEnvelope& e); + CommandResult handleSetConfig(const CommandEnvelope& e); + CommandResult handleSetAlertsConfig(const CommandEnvelope& e); }; #endif // HAS_RTC diff --git a/src/Communication/Handlers/RelayHandler/RelayHandler.cpp b/src/Communication/Handlers/RelayHandler/RelayHandler.cpp index f867f2e..9383a57 100644 --- a/src/Communication/Handlers/RelayHandler/RelayHandler.cpp +++ b/src/Communication/Handlers/RelayHandler/RelayHandler.cpp @@ -11,7 +11,7 @@ RelayHandler::RelayHandler(ConfigManager& configManager, OutputManager& outputMa std::vector RelayHandler::commands() const { return { - "relay.set_durations", "relay.set_outputs", "relay.get_config", + "relay.set_durations", "relay.set_outputs", "relay.set_config", "relay.get_config", "relay.test_output", "relay.test_bell", "relay.test_batch" }; } @@ -19,6 +19,7 @@ std::vector RelayHandler::commands() const { CommandResult RelayHandler::execute(const CommandEnvelope& envelope) { if (envelope.command == "relay.set_durations") return handleSetDurations(envelope); if (envelope.command == "relay.set_outputs") return handleSetOutputs(envelope); + if (envelope.command == "relay.set_config") return handleSetConfig(envelope); if (envelope.command == "relay.get_config") return handleGetConfig(envelope); if (envelope.command == "relay.test_output") return handleTestOutput(envelope); if (envelope.command == "relay.test_bell") return handleTestBell(envelope); @@ -56,6 +57,34 @@ CommandResult RelayHandler::handleSetOutputs(const CommandEnvelope& e) { return CommandResult::ok("relay.set_outputs", "Bell outputs updated and saved"); } +CommandResult RelayHandler::handleSetConfig(const CommandEnvelope& e) { + JsonVariant contents = const_cast(e.contents).as(); + + bool hasDurations = contents["durations"].is(); + bool hasOutputs = contents["outputs"].is(); + + if (!hasDurations && !hasOutputs) { + return CommandResult::err("relay.set_config", "Provide 'durations' and/or 'outputs' objects"); + } + + if (hasDurations) { + _configManager.updateBellDurations(contents["durations"].as()); + if (!_configManager.saveBellDurations()) { + return CommandResult::err("relay.set_config", "Durations applied but failed to save to SD"); + } + } + + if (hasOutputs) { + _configManager.updateBellOutputs(contents["outputs"].as()); + if (!_configManager.saveBellOutputs()) { + return CommandResult::err("relay.set_config", "Outputs applied but failed to save to SD"); + } + } + + LOG_INFO(TAG, "relay.config applied (durations=%d outputs=%d)", hasDurations, hasOutputs); + return CommandResult::ok("relay.set_config", "Bell configuration updated and saved"); +} + CommandResult RelayHandler::handleGetConfig(const CommandEnvelope& e) { JsonDocument doc; JsonObject durations = doc["durations"].to(); @@ -104,15 +133,19 @@ CommandResult RelayHandler::handleTestBell(const CommandEnvelope& e) { return CommandResult::err("relay.test_bell", "bell must be 0-15"); } - uint8_t output = (uint8_t)_configManager.getBellOutput(bell); + uint8_t output = _outputManager.getPhysicalOutput((uint8_t)bell); uint16_t durationMs = _configManager.getBellDuration(bell); + if (output == 255) { + return CommandResult::err("relay.test_bell", "Bell " + String(bell) + " is disabled or unconfigured"); + } + _outputManager.fireOutputForDuration(output, durationMs); - LOG_INFO(TAG, "Test bell %d → output %d for %d ms", bell, output, durationMs); + LOG_INFO(TAG, "Test bell %d → chip pin %d for %d ms", bell, output, durationMs); JsonDocument doc; doc["bell"] = bell; - doc["output"] = output; + doc["output"] = output + 1; // 1-based for human readability doc["duration_ms"] = durationMs; return CommandResult::ok("relay.test_bell", doc); } @@ -134,8 +167,9 @@ CommandResult RelayHandler::handleTestBatch(const CommandEnvelope& e) { if (!v.is()) continue; int bell = v.as(); if (bell < 0 || bell > 15) continue; - uint8_t output = (uint8_t)_configManager.getBellOutput((uint8_t)bell); + uint8_t output = _outputManager.getPhysicalOutput((uint8_t)bell); uint16_t durationMs = _configManager.getBellDuration((uint8_t)bell); + if (output == 255) continue; // disabled/unconfigured — skip silently _outputManager.fireOutputForDuration(output, durationMs); fired++; } diff --git a/src/Communication/Handlers/RelayHandler/RelayHandler.hpp b/src/Communication/Handlers/RelayHandler/RelayHandler.hpp index 66d7ed9..3814d44 100644 --- a/src/Communication/Handlers/RelayHandler/RelayHandler.hpp +++ b/src/Communication/Handlers/RelayHandler/RelayHandler.hpp @@ -21,6 +21,7 @@ private: CommandResult handleSetDurations(const CommandEnvelope& e); CommandResult handleSetOutputs(const CommandEnvelope& e); + CommandResult handleSetConfig(const CommandEnvelope& e); CommandResult handleGetConfig(const CommandEnvelope& e); CommandResult handleTestOutput(const CommandEnvelope& e); CommandResult handleTestBell(const CommandEnvelope& e); diff --git a/src/ConfigManager/ConfigManager.cpp b/src/ConfigManager/ConfigManager.cpp index 118c638..33e6995 100644 --- a/src/ConfigManager/ConfigManager.cpp +++ b/src/ConfigManager/ConfigManager.cpp @@ -54,10 +54,9 @@ void ConfigManager::generateNetworkIdentifiers() { } void ConfigManager::createDefaultBellConfig() { - // Initialize default durations (90ms for all bells) for (uint8_t i = 0; i < 16; i++) { bellConfig.durations[i] = 90; - bellConfig.outputs[i] = i; // 0-indexed mapping + bellConfig.outputs[i] = 0; // 0 = disabled (1-based scheme: 1=first relay, 0=none) } } @@ -433,7 +432,7 @@ bool ConfigManager::loadBellOutputs() { for (uint8_t i = 0; i < 16; i++) { String key = String("b") + (i + 1); if (doc.containsKey(key)) { - bellConfig.outputs[i] = doc[key].as(); // Already 0-indexed in file + bellConfig.outputs[i] = doc[key].as(); // 1-based: 1=first relay, 0=disabled } } @@ -450,7 +449,7 @@ bool ConfigManager::saveBellOutputs() { StaticJsonDocument<512> doc; for (uint8_t i = 0; i < 16; i++) { String key = String("b") + (i + 1); - doc[key] = bellConfig.outputs[i]; // Save 0-indexed outputs + doc[key] = bellConfig.outputs[i]; // 1-based: 1=first relay, 0=disabled } char buffer[512]; @@ -510,9 +509,12 @@ bool ConfigManager::saveBellConfigToNVS() { void ConfigManager::updateBellDurations(JsonVariant doc) { for (uint8_t i = 0; i < 16; i++) { - String key = String("b") + (i + 1); - if (doc.containsKey(key)) { - bellConfig.durations[i] = doc[key].as(); + String v2key = String(i); // v2: "0", "1", ... + String v1key = String("b") + (i + 1); // v1: "b1", "b2", ... + if (doc.containsKey(v2key)) { + bellConfig.durations[i] = doc[v2key].as(); + } else if (doc.containsKey(v1key)) { + bellConfig.durations[i] = doc[v1key].as(); } } LOG_DEBUG(TAG, "ConfigManager - Updated bell durations"); @@ -520,10 +522,12 @@ void ConfigManager::updateBellDurations(JsonVariant doc) { void ConfigManager::updateBellOutputs(JsonVariant doc) { for (uint8_t i = 0; i < 16; i++) { - String key = String("b") + (i + 1); - if (doc.containsKey(key)) { - bellConfig.outputs[i] = doc[key].as() - 1; - LOG_DEBUG(TAG, "ConfigManager - Bell %d output set to %d", i + 1, bellConfig.outputs[i]); + String v2key = String(i); // v2: "0", "1", ... + String v1key = String("b") + (i + 1); // v1: "b1", "b2", ... + if (doc.containsKey(v2key)) { + bellConfig.outputs[i] = doc[v2key].as(); + } else if (doc.containsKey(v1key)) { + bellConfig.outputs[i] = doc[v1key].as(); } } LOG_DEBUG(TAG, "ConfigManager - Updated bell outputs"); diff --git a/src/OutputManager/OutputManager.cpp b/src/OutputManager/OutputManager.cpp index d7712ea..bc8e566 100644 --- a/src/OutputManager/OutputManager.cpp +++ b/src/OutputManager/OutputManager.cpp @@ -77,21 +77,20 @@ uint8_t OutputManager::getPhysicalOutput(uint8_t virtualOutput) const { return virtualOutput; } - // Get 0-indexed bell output from config + // Config stores 1-based output numbers (1=first relay, 0=disabled, 255=unconfigured). + // Convert to 0-based chip index here — this is the single conversion point. uint16_t bellOutput = _configManager->getBellOutput(virtualOutput); - - // Handle unconfigured bells (255 = disabled) - if (bellOutput == 255) { - LOG_WARNING(TAG, "⚠️ Bell %d not configured (255)", virtualOutput); - return 255; // Return invalid to prevent firing + + if (bellOutput == 0 || bellOutput == 255) { + LOG_WARNING(TAG, "⚠️ Bell %d is disabled or unconfigured (stored=%d)", virtualOutput, bellOutput); + return 255; } - - // Physical output is already 0-indexed from config - uint8_t physicalOutput = (uint8_t)bellOutput; - - LOG_DEBUG(TAG, "🔗 Bell %d → 0-indexed output %d", - virtualOutput, physicalOutput); - + + uint8_t physicalOutput = (uint8_t)(bellOutput - 1); + + LOG_DEBUG(TAG, "🔗 Bell %d → Output %d → chip pin %d", + virtualOutput + 1, bellOutput, physicalOutput); + return physicalOutput; } @@ -115,28 +114,25 @@ void OutputManager::fireClockOutput(uint8_t virtualOutput, uint16_t durationMs) return; } - // Map virtual clock output to physical output using clock configuration - uint8_t physicalOutput; + // Map virtual clock output to physical output using clock configuration. + // Config stores 1-based output numbers — convert to 0-based chip index here. + uint8_t storedOutput; if (virtualOutput == 0) { - // Virtual clock output 0 = C1 - physicalOutput = _configManager->getClockOutput1(); - if (physicalOutput == 255) { - LOG_WARNING(TAG, "⚠️ Clock C1 not configured (255)"); - return; - } + storedOutput = _configManager->getClockOutput1(); } else if (virtualOutput == 1) { - // Virtual clock output 1 = C2 - physicalOutput = _configManager->getClockOutput2(); - if (physicalOutput == 255) { - LOG_WARNING(TAG, "⚠️ Clock C2 not configured (255)"); - return; - } + storedOutput = _configManager->getClockOutput2(); } else { LOG_ERROR(TAG, "❌ Invalid virtual clock output: %d (only 0=C1, 1=C2 supported)", virtualOutput); return; } - - // Physical output is already 0-indexed from config + + if (storedOutput == 0 || storedOutput == 255) { + LOG_WARNING(TAG, "⚠️ Clock C%d is disabled or unconfigured (stored=%d)", virtualOutput + 1, storedOutput); + return; + } + + uint8_t physicalOutput = storedOutput - 1; + if (!isValidPhysicalOutput(physicalOutput)) { LOG_ERROR(TAG, "❌ Invalid physical output for clock: %d (max outputs: %d)", physicalOutput, getMaxOutputs()); @@ -146,8 +142,8 @@ void OutputManager::fireClockOutput(uint8_t virtualOutput, uint16_t durationMs) // Fire the physical output directly fireOutputForDuration(physicalOutput, durationMs); - LOG_DEBUG(TAG, "🕐 FIRE Clock Virtual %d (C%d) → 0-indexed output %d for %dms", - virtualOutput, virtualOutput + 1, physicalOutput, durationMs); + LOG_DEBUG(TAG, "🕐 FIRE Clock C%d → Output %d → chip pin %d for %dms", + virtualOutput + 1, storedOutput, physicalOutput, durationMs); } // ==================== PCF8574/PCF8575 MULTI-CHIP IMPLEMENTATION ==================== @@ -203,32 +199,35 @@ bool PCF8574OutputManager::initialize() { if (_configManager) { LOG_INFO(TAG, "📋 Virtual Output Configuration Mappings:"); - for (uint8_t i = 0; i < min(16, (int)_totalOutputs); i++) { // Check virtual outputs - uint16_t configOutput = _configManager->getBellOutput(i); - if (configOutput < _totalOutputs) { - VirtualOutputInfo info = getVirtualOutputInfo(configOutput); - LOG_DEBUG(TAG, " Bell %d → Virtual Output %d → %s[%d] Pin %d", i, configOutput, info.chipType, info.chipIndex, info.localPin); - } else if (configOutput == 255) { - LOG_DEBUG(TAG, " Bell %d → Not configured (255)", i); + for (uint8_t i = 0; i < min(16, (int)_totalOutputs); i++) { + uint16_t configOutput = _configManager->getBellOutput(i); // 1-based + if (configOutput == 0 || configOutput == 255) { + LOG_DEBUG(TAG, " Bell %d → Disabled/unconfigured", i + 1); } else { - LOG_WARNING(TAG, "⚠️ Bell %d mapped to invalid virtual output %d (max: %d)", i, configOutput, _totalOutputs - 1); + uint8_t chipPin = (uint8_t)(configOutput - 1); // convert to 0-based + if (chipPin < _totalOutputs) { + VirtualOutputInfo info = getVirtualOutputInfo(chipPin); + LOG_DEBUG(TAG, " Bell %d → Output %d → %s[%d] Pin %d", i + 1, configOutput, info.chipType, info.chipIndex, info.localPin); + } else { + LOG_WARNING(TAG, "⚠️ Bell %d mapped to invalid output %d (max: %d)", i + 1, configOutput, _totalOutputs); + } } } - uint8_t c1 = _configManager->getClockOutput1(); - uint8_t c2 = _configManager->getClockOutput2(); + uint8_t c1 = _configManager->getClockOutput1(); // 1-based + uint8_t c2 = _configManager->getClockOutput2(); // 1-based LOG_INFO(TAG, "🕐 Clock Virtual Output Mappings:"); - if (c1 != 255 && c1 < _totalOutputs) { - VirtualOutputInfo info = getVirtualOutputInfo(c1); - LOG_DEBUG(TAG, " Clock C1 → Virtual Output %d → %s[%d] Pin %d", c1, info.chipType, info.chipIndex, info.localPin); + if (c1 > 0 && c1 != 255 && (uint8_t)(c1 - 1) < _totalOutputs) { + VirtualOutputInfo info = getVirtualOutputInfo(c1 - 1); + LOG_DEBUG(TAG, " Clock C1 → Output %d → %s[%d] Pin %d", c1, info.chipType, info.chipIndex, info.localPin); } else { LOG_DEBUG(TAG, " Clock C1 → Not configured"); } - if (c2 != 255 && c2 < _totalOutputs) { - VirtualOutputInfo info = getVirtualOutputInfo(c2); - LOG_DEBUG(TAG, " Clock C2 → Virtual Output %d → %s[%d] Pin %d", c2, info.chipType, info.chipIndex, info.localPin); + if (c2 > 0 && c2 != 255 && (uint8_t)(c2 - 1) < _totalOutputs) { + VirtualOutputInfo info = getVirtualOutputInfo(c2 - 1); + LOG_DEBUG(TAG, " Clock C2 → Output %d → %s[%d] Pin %d", c2, info.chipType, info.chipIndex, info.localPin); } else { LOG_DEBUG(TAG, " Clock C2 → Not configured"); } diff --git a/src/TimeKeeper/TimeKeeper.cpp b/src/TimeKeeper/TimeKeeper.cpp index 9ca3282..eea89a9 100644 --- a/src/TimeKeeper/TimeKeeper.cpp +++ b/src/TimeKeeper/TimeKeeper.cpp @@ -693,8 +693,10 @@ void Timekeeper::fireAlertBell(uint8_t bellNumber, int count) { LOG_DEBUG(TAG, "🔔 Alert bell #%d ring %d/%d (duration: %dms)", bellNumber + 1, i + 1, count, bellDuration); - // Fire the bell using OutputManager - _outputManager->fireOutputForDuration(bellNumber, bellDuration); + // bellNumber is a 0-based bell index — resolve to chip pin through the single conversion point + uint8_t chipPin = _outputManager->getPhysicalOutput(bellNumber); + if (chipPin == 255) return; // bell disabled or unconfigured + _outputManager->fireOutputForDuration(chipPin, bellDuration); // Wait between rings (only if there's more than one ring) if (i < count - 1) {