development #4

Merged
elduko merged 6 commits from development into main 2025-10-31 15:22:25 +01:00
11 changed files with 157 additions and 46 deletions

View File

@@ -1,3 +1,16 @@
#pragma once #pragma once
bool debounceRead(int buttonPin, int *lastButtonState, unsigned long *lastDebounceTime, int *buttonState); class Reader
{
private:
unsigned long lastDebounceTime = 0;
bool lastButtonStateNO = LOW;
bool buttonStateNO = HIGH;
bool lastButtonStateNC = HIGH;
bool buttonStateNC = LOW;
int lastPin;
public:
bool debounceReadNO(int buttonPin);
bool debounceReadNC(int buttonPin);
};

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
const int DEBOUNCE_DELAY = 50; const int DEBOUNCE_DELAY = 200;
const int USER_STEPS = 2; const int USER_STEPS = 2;
const int STAFF_STEPS = 16; const int STAFF_STEPS = 16;

View File

@@ -1,4 +1,14 @@
#pragma once #pragma once
/**
* @brief This function sets all the LEDs to a default(LOW) state
*
*/
void updateLeds(int volume); void updateLeds(int volume);
/**
* @brief This function turns on the correct LED depending on the current max(staff) volume
*
* @param volume The current max(staff) volume
*/
void resetLeds(); void resetLeds();

View File

@@ -1,4 +1,13 @@
#pragma once #pragma once
/**
* @brief This function moves the potentiometer wiper one step to pin A
*
*/
void potIncrement(); void potIncrement();
/**
* @brief This function moves the potentiometer wiper one step to pin B
*
*/
void potDecrement(); void potDecrement();

View File

@@ -10,8 +10,8 @@ void staffVolumeUp(int *maxVolume);
/** /**
* @brief Used to decrease the maximum staff volume. If the max volume is set lower than what the current user volume is, then the user volume will also be lowered. * @brief Used to decrease the maximum staff volume. If the max volume is set lower than what the current user volume is, then the user volume will also be lowered.
* *
* @param maxVolume Variable used to keep track of the max volume * @param maxVolume Pointer to the variable used to keep track of the max volume
* @param userVolume Pointer to the variable used to keep track of the user volume * @param userVolume Pointer to the variable used to keep track of the user volume
* @param oldUserVolume Pointer to the variable used to keep track of the user volume when in mute or lowered * @param oldUserVolume Pointer to the variable used to keep track of the user volume when in mute or lowered
*/ */
void staffVolumeDown(int maxVolume, int *userVolume, int *oldUserVolume); void staffVolumeDown(int *maxVolume, int *userVolume, int *oldUserVolume);

View File

@@ -2,33 +2,68 @@
#include <commonFunctions.h> #include <commonFunctions.h>
#include <constants.h> #include <constants.h>
bool debounceRead(int buttonPin, int *lastButtonState, unsigned long *lastDebounceTime, int *buttonState) bool Reader::debounceReadNO(int buttonPin)
{ {
int reading = digitalRead(buttonPin); bool reading = digitalRead(buttonPin);
// If the switch changed, due to noise or pressing: if (reading)
if (reading != *lastButtonState)
{ {
// reset the debouncing timer lastPin = buttonPin;
*lastDebounceTime = millis();
} }
if ((millis() - *lastDebounceTime) > DEBOUNCE_DELAY) bool buttonPressed = false;
if (reading != lastButtonStateNO && lastPin == buttonPin)
{ {
// whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state: lastDebounceTime = millis(); // reset debounce timer
}
// if the button state has changed: if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY)
if (reading != *buttonState)
{ {
*buttonState = reading; // If button state is stable and just changed to pressed
if (reading == HIGH && buttonStateNO == LOW)
if (*buttonState == HIGH) {
return true; buttonPressed = true;
else
return false;
} }
buttonStateNO = reading;
} }
// save the reading. Next time through the loop, it'll be the lastButtonState: if (lastPin == buttonPin)
*lastButtonState = reading; {
lastButtonStateNO = reading;
}
return buttonPressed;
}
bool Reader::debounceReadNC(int buttonPin)
{
bool reading = digitalRead(buttonPin);
if (reading == LOW)
{
lastPin = buttonPin;
}
bool buttonPressed = false;
if (reading != lastButtonStateNC && lastPin == buttonPin)
{
lastDebounceTime = millis(); // reset debounce timer
}
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY)
{
// If button state is stable and just changed to pressed
if (reading == LOW && buttonStateNC == HIGH)
{
buttonPressed = true;
}
buttonStateNC = reading;
}
if (lastPin == buttonPin)
{
lastButtonStateNC = reading;
}
return buttonPressed;
} }

View File

@@ -1,7 +1,22 @@
/**
* @file ledControlls.cpp
* @author Lyubomir Penev (me@lpenev.com, 571147@student.fontys.nl)
* @brief This file contains all the functions which control the LEDs used to indicate the current max volume
* @version 0.1
* @date 2025-10-31
*
* @copyright Copyright (c) 2025
*
*/
#include <Arduino.h> #include <Arduino.h>
#include <constants.h> #include <constants.h>
#include <ledControlls.h> #include <ledControlls.h>
/**
* @brief This function sets all the LEDs to a default(LOW) state
*
*/
void resetLeds() void resetLeds()
{ {
digitalWrite(LED_HIGH, LOW); digitalWrite(LED_HIGH, LOW);
@@ -9,6 +24,11 @@ void resetLeds()
digitalWrite(LED_LOW, LOW); digitalWrite(LED_LOW, LOW);
} }
/**
* @brief This function turns on the correct LED depending on the current max(staff) volume
*
* @param volume The current max(staff) volume
*/
void updateLeds(int volume) void updateLeds(int volume)
{ {
switch (volume) switch (volume)

View File

@@ -10,9 +10,7 @@ int maxVolume = 64;
int userVolume = 64; int userVolume = 64;
int oldUserVolume = userVolume; int oldUserVolume = userVolume;
int buttonState; Reader reader;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
void setup() void setup()
{ {
@@ -29,11 +27,13 @@ void setup()
pinMode(REMOTE_C, INPUT); // Remote Button C pinMode(REMOTE_C, INPUT); // Remote Button C
pinMode(REMOTE_D, INPUT); // Remote Button D pinMode(REMOTE_D, INPUT); // Remote Button D
// Potentiometer
pinMode(POT_CS, OUTPUT); // D10 - CS pinMode(POT_CS, OUTPUT); // D10 - CS
pinMode(POT_UD, OUTPUT); // D9 - U/D pinMode(POT_UD, OUTPUT); // D9 - U/D
digitalWrite(POT_CS, HIGH); digitalWrite(POT_CS, HIGH);
digitalWrite(POT_UD, LOW); digitalWrite(POT_UD, LOW);
// Reset the potentiometer
for (int i = 0; i < 64; i++) for (int i = 0; i < 64; i++)
{ {
potIncrement(); potIncrement();
@@ -45,38 +45,39 @@ void loop()
updateLeds(maxVolume); updateLeds(maxVolume);
// SW1 - Staff Volume Up // SW1 - Staff Volume Up
if (!debounceRead(BTN_STAFF_UP, &lastButtonState, &lastDebounceTime, &buttonState)) if (reader.debounceReadNC(BTN_STAFF_UP))
{ {
staffVolumeUp(&maxVolume); staffVolumeUp(&maxVolume);
} }
// SW2 - Staff Volume Down // SW2 - Staff Volume Down
if (!debounceRead(BTN_STAFF_DWN, &lastButtonState, &lastDebounceTime, &buttonState)) if (reader.debounceReadNC(BTN_STAFF_DWN))
{ {
staffVolumeDown(maxVolume, &userVolume, &oldUserVolume); staffVolumeDown(&maxVolume, &userVolume, &oldUserVolume);
} }
// Remote D - Mute // Remote D - Mute
if (debounceRead(REMOTE_D, &lastButtonState, &lastDebounceTime, &buttonState)) if (reader.debounceReadNO(REMOTE_D))
{ {
mute(&userVolume, &oldUserVolume, maxVolume); mute(&userVolume, &oldUserVolume, maxVolume);
} }
// Remote A - Significantly Lower Volume // Remote A - Significantly Lower Volume
if (debounceRead(REMOTE_A, &lastButtonState, &lastDebounceTime, &buttonState)) if (reader.debounceReadNO(REMOTE_A))
{ {
lowerVolume(&userVolume, &oldUserVolume, maxVolume); lowerVolume(&userVolume, &oldUserVolume, maxVolume);
} }
// Remote B - Volume Up // Remote B - Volume Up
// Make sure we are not is a state of mute or lowered // Make sure we are not is a state of mute or lowered
if (debounceRead(REMOTE_B, &lastButtonState, &lastDebounceTime, &buttonState) && !(oldUserVolume > userVolume)) if (reader.debounceReadNO(REMOTE_B) && !(oldUserVolume > userVolume))
{ {
volumeUp(&userVolume, &oldUserVolume, maxVolume); volumeUp(&userVolume, &oldUserVolume, maxVolume);
} }
// Remote C - Volume Down // Remote C - Volume Down
if (debounceRead(REMOTE_C, &lastButtonState, &lastDebounceTime, &buttonState) && !(oldUserVolume > userVolume)) // Make sure we are not is a state of mute or lowered
if (reader.debounceReadNO(REMOTE_C) && !(oldUserVolume > userVolume))
{ {
volumeDown(&userVolume, &oldUserVolume); volumeDown(&userVolume, &oldUserVolume);
} }

View File

@@ -1,7 +1,26 @@
/**
* @file potControlls.cpp
* @author Lyubomir Penev (me@lpenev.com, 571147@student.fontys.nl)
* @brief Here are all the functions responsible for incrementing and decrementing the potentiometer
* @version 0.1
* @date 2025-10-31
*
* @copyright Copyright (c) 2025
*
* Here are all the functions responsible for incrementing and decrementing the MCP4011 digital potentiometer. For more information on the protocol used to control the potentiometer you can look on pages 29 and 31 of the datasheet, and for timing information you can checkout pages 6 and 7.
*
* Datasheet: https://ww1.microchip.com/downloads/en/devicedoc/21978a.pdf
*
*/
#include <Arduino.h> #include <Arduino.h>
#include <constants.h> #include <constants.h>
#include <potControlls.h> #include <potControlls.h>
/**
* @brief This function moves the potentiometer wiper one step to pin A
*
*/
void potIncrement() void potIncrement()
{ {
digitalWrite(POT_UD, HIGH); digitalWrite(POT_UD, HIGH);
@@ -18,6 +37,10 @@ void potIncrement()
digitalWrite(POT_CS, HIGH); digitalWrite(POT_CS, HIGH);
} }
/**
* @brief This function moves the potentiometer wiper one step to pin B
*
*/
void potDecrement() void potDecrement()
{ {
digitalWrite(POT_UD, LOW); digitalWrite(POT_UD, LOW);

View File

@@ -1,6 +1,6 @@
/** /**
* @file staffFunctions.cpp * @file staffFunctions.cpp
* @author Lyubomir Penev * @author Lyubomir Penev (me@lpenev.com, 571147@student.fontys.nl)
* @brief Here are all the implementations of the staff functions * @brief Here are all the implementations of the staff functions
* @version 1.0 * @version 1.0
* @date 2025-10-23 * @date 2025-10-23
@@ -29,30 +29,30 @@ void staffVolumeUp(int *maxVolume)
/** /**
* @brief Used to decrease the maximum staff volume. If the max volume is set lower than what the current user volume is, then the user volume will also be lowered. * @brief Used to decrease the maximum staff volume. If the max volume is set lower than what the current user volume is, then the user volume will also be lowered.
* *
* @param maxVolume Variable used to keep track of the max volume * @param maxVolume Pointer to the variable used to keep track of the max volume
* @param userVolume Pointer to the variable used to keep track of the user volume * @param userVolume Pointer to the variable used to keep track of the user volume
* @param oldUserVolume Pointer to the variable used to keep track of the user volume when in mute or lowered * @param oldUserVolume Pointer to the variable used to keep track of the user volume when in mute or lowered
*/ */
void staffVolumeDown(int maxVolume, int *userVolume, int *oldUserVolume) void staffVolumeDown(int *maxVolume, int *userVolume, int *oldUserVolume)
{ {
if (maxVolume > STAFF_STEPS) if (*maxVolume > STAFF_STEPS)
{ {
maxVolume -= STAFF_STEPS; *maxVolume -= STAFF_STEPS;
if (*userVolume > maxVolume) if (*userVolume > *maxVolume)
{ {
for (int i = 0; i < (*userVolume - maxVolume); i++) for (int i = 0; i < (*userVolume - *maxVolume); i++)
{ {
potDecrement(); potDecrement();
} }
*userVolume = maxVolume; *userVolume = *maxVolume;
*oldUserVolume = *userVolume; *oldUserVolume = *userVolume;
} }
if (*oldUserVolume > maxVolume) if (*oldUserVolume > *maxVolume)
{ {
*oldUserVolume = maxVolume; *oldUserVolume = *maxVolume;
} }
} }
} }

View File

@@ -1,6 +1,6 @@
/** /**
* @file userFunctions.cpp * @file userFunctions.cpp
* @author Lyubomir Penev * @author Lyubomir Penev (me@lpenev.com, 571147@student.fontys.nl)
* @brief Here are all the implementations for the user functions. * @brief Here are all the implementations for the user functions.
* @version 1.0 * @version 1.0
* @date 2025-10-23 * @date 2025-10-23