From f77318fcdbaf1edd7f7ad8518b515be197dd1c60 Mon Sep 17 00:00:00 2001 From: Lyubomir Penev Date: Wed, 22 Oct 2025 14:49:29 +0200 Subject: [PATCH] Extracted lowerVolume to seperate file --- include/userFunctions.h | 1 + src/main.cpp | 40 +-------------------------------------- src/userFunctions.cpp | 42 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/include/userFunctions.h b/include/userFunctions.h index 40e6cb5..30b4526 100644 --- a/include/userFunctions.h +++ b/include/userFunctions.h @@ -1,4 +1,5 @@ #pragma once +void lowerVolume(int *userVolume, int *oldUserVolume, int maxVolume); void volumeUp(int *userVolume, int *oldUserVolume, int maxVolume); void volumeDown(int *userVolume, int *oldUserVolume); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7ac911c..90dc927 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -125,45 +125,7 @@ void loop() // Remote A - Significantly Lower Volume if (debounceRead(REMOTE_A)) { - - // Check if we volume is already lowered - if (oldUserVolume > userVolume) - { - - // If it is lowered - restore it - // Check if max volume has changed since lowering - if (oldUserVolume > maxVolume) - { - // If the max volume has been lowered since lowering, then set the user volume to the new max volume - userVolume = maxVolume; - - for (int i = 0; i < maxVolume; i++) - { - potIncrement(); - } - } - else - { - // If the max volume hasn't changed then, or the old user volume is lower than the new max - restore it to where it was - userVolume = oldUserVolume; - - for (int i = 0; i < oldUserVolume; i++) - { - potIncrement(); - } - } - } - else - { - // This is if the user is not lowered and is aksing to be lowered - for (int i = 0; i < userVolume - 10; i++) - { - potDecrement(); - } - - oldUserVolume = userVolume; - userVolume -= 10; - } + lowerVolume(&userVolume, &oldUserVolume, maxVolume); } // Remote B - Volume Up diff --git a/src/userFunctions.cpp b/src/userFunctions.cpp index 1229b1a..a14f4ab 100644 --- a/src/userFunctions.cpp +++ b/src/userFunctions.cpp @@ -2,6 +2,48 @@ #include #include +void lowerVolume(int *userVolume, int *oldUserVolume, int maxVolume) +{ + // Check if we volume is already lowered + if (*oldUserVolume > *userVolume) + { + + // If it is lowered - restore it + // Check if max volume has changed since lowering + if (*oldUserVolume > maxVolume) + { + // If the max volume has been lowered since lowering, then set the user volume to the new max volume + *userVolume = maxVolume; + + for (int i = 0; i < maxVolume; i++) + { + potIncrement(); + } + } + else + { + // If the max volume hasn't changed then, or the old user volume is lower than the new max - restore it to where it was + *userVolume = *oldUserVolume; + + for (int i = 0; i < *oldUserVolume; i++) + { + potIncrement(); + } + } + } + else + { + // This is if the user is not lowered and is aksing to be lowered + for (int i = 0; i < *userVolume - 10; i++) + { + potDecrement(); + } + + *oldUserVolume = *userVolume; + *userVolume -= 10; + } +} + void volumeUp(int *userVolume, int *oldUserVolume, int maxVolume) { if ((*userVolume + USER_STEPS) > maxVolume)