From 470d7bfaccfc835f7dbc4a87c34cfa4e81a719b2 Mon Sep 17 00:00:00 2001 From: bonamin Date: Sat, 18 Oct 2025 18:43:51 +0300 Subject: [PATCH] Fixed Indexing on Bell Output Assignments --- vesper/src/BellEngine/BellEngine.cpp | 11 ++++-- vesper/src/ConfigManager/ConfigManager.cpp | 2 +- vesper/src/OutputManager/OutputManager.cpp | 41 ++++++++-------------- vesper/src/Player/Player.cpp | 4 +-- vesper/vesper.ino | 2 +- 5 files changed, 27 insertions(+), 33 deletions(-) diff --git a/vesper/src/BellEngine/BellEngine.cpp b/vesper/src/BellEngine/BellEngine.cpp index aeba09f..df2c10d 100644 --- a/vesper/src/BellEngine/BellEngine.cpp +++ b/vesper/src/BellEngine/BellEngine.cpp @@ -224,7 +224,14 @@ void BellEngine::playbackLoop() { // Activate note with MAXIMUM PRECISION activateNote(note); - // Precise timing delay + // Precise timing delay - validate speed to prevent division by zero + if (_player.speed == 0) { + LOG_ERROR("❌ Invalid speed=0 detected, stopping playback"); + _player.hardStop = true; + _engineRunning.store(false); + return; + } + uint32_t tempoMicros = _player.speed * 1000; // Convert ms to microseconds preciseDelay(tempoMicros); } @@ -252,7 +259,7 @@ void BellEngine::activateNote(uint16_t note) { if (bellConfig == 0) continue; // Convert 1-indexed config to 0-indexed bellIndex - uint8_t bellIndex = bellConfig - 1; + uint8_t bellIndex = bellConfig-1; // Additional safety check to prevent underflow crashes if (bellIndex >= 255) { diff --git a/vesper/src/ConfigManager/ConfigManager.cpp b/vesper/src/ConfigManager/ConfigManager.cpp index d4da3bb..11fac75 100644 --- a/vesper/src/ConfigManager/ConfigManager.cpp +++ b/vesper/src/ConfigManager/ConfigManager.cpp @@ -44,7 +44,7 @@ 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 + 1; // 1-indexed mapping by default + bellConfig.outputs[i] = i; // 0-indexed mapping } } diff --git a/vesper/src/OutputManager/OutputManager.cpp b/vesper/src/OutputManager/OutputManager.cpp index c7424f8..b25e0eb 100644 --- a/vesper/src/OutputManager/OutputManager.cpp +++ b/vesper/src/OutputManager/OutputManager.cpp @@ -75,26 +75,20 @@ uint8_t OutputManager::getPhysicalOutput(uint8_t virtualOutput) const { return virtualOutput; } - // Get 1-indexed bell output from config - uint16_t bellOutput1Indexed = _configManager->getBellOutput(virtualOutput); + // Get 0-indexed bell output from config + uint16_t bellOutput = _configManager->getBellOutput(virtualOutput); // Handle unconfigured bells (255 = disabled) - if (bellOutput1Indexed == 255) { + if (bellOutput == 255) { LOG_WARNING("⚠️ Bell %d not configured (255)", virtualOutput); return 255; // Return invalid to prevent firing } - // Handle invalid 0 configuration - if (bellOutput1Indexed == 0) { - LOG_ERROR("❌ Bell %d configured as 0 (invalid - should be 1-indexed)", virtualOutput); - return 255; - } + // Physical output is already 0-indexed from config + uint8_t physicalOutput = (uint8_t)bellOutput; - // Convert 1-indexed config to 0-indexed physical output - uint8_t physicalOutput = (uint8_t)(bellOutput1Indexed - 1); - - LOG_DEBUG("🔗 Bell %d → 1-indexed config %d → 0-indexed output %d", - virtualOutput, bellOutput1Indexed, physicalOutput); + LOG_DEBUG("🔗 Bell %d → 0-indexed output %d", + virtualOutput, physicalOutput); return physicalOutput; } @@ -140,25 +134,18 @@ void OutputManager::fireClockOutput(uint8_t virtualOutput, uint16_t durationMs) return; } - // Convert 1-indexed config value to 0-indexed physical output - if (physicalOutput == 0) { - LOG_ERROR("❌ Clock output configured as 0 (invalid - should be 1-indexed)"); - return; - } - - uint8_t zeroIndexedOutput = physicalOutput - 1; // Convert 1-indexed to 0-indexed - - if (!isValidPhysicalOutput(zeroIndexedOutput)) { - LOG_ERROR("❌ Invalid physical output for clock: %d (1-indexed config: %d, max outputs: %d)", - zeroIndexedOutput, physicalOutput, getMaxOutputs()); + // Physical output is already 0-indexed from config + if (!isValidPhysicalOutput(physicalOutput)) { + LOG_ERROR("❌ Invalid physical output for clock: %d (max outputs: %d)", + physicalOutput, getMaxOutputs()); return; } // Fire the physical output directly - fireOutputForDuration(zeroIndexedOutput, durationMs); + fireOutputForDuration(physicalOutput, durationMs); - LOG_DEBUG("🕐 FIRE Clock Virtual %d (C%d) → 1-indexed config %d → 0-indexed output %d for %dms", - virtualOutput, virtualOutput + 1, physicalOutput, zeroIndexedOutput, durationMs); + LOG_DEBUG("🕐 FIRE Clock Virtual %d (C%d) → 0-indexed output %d for %dms", + virtualOutput, virtualOutput + 1, physicalOutput, durationMs); } // ==================== PCF8574/PCF8575 MULTI-CHIP IMPLEMENTATION ==================== diff --git a/vesper/src/Player/Player.cpp b/vesper/src/Player/Player.cpp index 774d388..c8ee2d9 100644 --- a/vesper/src/Player/Player.cpp +++ b/vesper/src/Player/Player.cpp @@ -10,7 +10,7 @@ Player::Player(CommunicationRouter* comm, FileManager* fm) , name("melody1") , uid("x") , url("-") - , noteAssignments{1,2,3,4,5,6,0,0,0,0,0,0,0,0,0,0} + , noteAssignments{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16} , speed(500) , segment_duration(15000) , pause_duration(0) @@ -37,7 +37,7 @@ Player::Player() , name("melody1") , uid("x") , url("-") - , noteAssignments{1,2,3,4,5,6,0,0,0,0,0,0,0,0,0,0} + , noteAssignments{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16} , speed(500) , segment_duration(15000) , pause_duration(0) diff --git a/vesper/vesper.ino b/vesper/vesper.ino index 7be21dd..ebc9e46 100644 --- a/vesper/vesper.ino +++ b/vesper/vesper.ino @@ -62,7 +62,7 @@ * 👨‍💻 AUTHOR: BellSystems bonamin */ -#define FW_VERSION "0.1" +#define FW_VERSION "1.0" /*