Compare commits

3 Commits

7 changed files with 59 additions and 155 deletions

View File

@@ -1,31 +1,16 @@
#pragma once
/**
* @brief Function used to read state of a normally open(LOW) button
*
* @param buttonPin The pin of the button
* @return true
* @return false
*/
bool debounceReadNO(int buttonPin);
class Reader
{
private:
unsigned long lastDebounceTime = 0;
bool lastButtonStateNO = LOW;
bool buttonStateNO = HIGH;
bool lastButtonStateNC = HIGH;
bool buttonStateNC = LOW;
int lastPin;
/**
* @brief Function used to read state of a normally closed(HIGH) button
*
* @param buttonPin The pin of the button
* @return true
* @return false
*/
bool debounceReadNC(int buttonPin);
/**
* @brief Sets all the pins to their correct modes
*
*/
void setPinModes();
/**
* @brief Resets the potentiometer by setting the wiper all the way to the A pin
*
*/
void resetPotentiometer();
public:
bool debounceReadNO(int buttonPin);
bool debounceReadNC(int buttonPin);
};

View File

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

View File

@@ -1,34 +1,8 @@
/**
* @file commonFunctions.cpp
* @author Lyubomir Penev (me@lpenev.com, 571147@student.fontys.nl)
* @brief This file contains all the functions used to read buttons
* @version 0.1
* @date 2025-10-31
*
* @copyright Copyright (c) 2025
*
*/
#include <Arduino.h>
#include <commonFunctions.h>
#include <potControlls.h>
#include <constants.h>
unsigned long lastDebounceTime = 0;
bool lastButtonStateNO = LOW;
bool buttonStateNO = HIGH;
bool lastButtonStateNC = HIGH;
bool buttonStateNC = LOW;
int lastPin;
/**
* @brief Function used to read state of a normally open(LOW) button
*
* @param buttonPin The pin of the button
* @return true
* @return false
*/
bool debounceReadNO(int buttonPin)
bool Reader::debounceReadNO(int buttonPin)
{
bool reading = digitalRead(buttonPin);
@@ -61,14 +35,7 @@ bool debounceReadNO(int buttonPin)
return buttonPressed;
}
/**
* @brief Function used to read state of a normally closed(HIGH) button
*
* @param buttonPin The pin of the button
* @return true
* @return false
*/
bool debounceReadNC(int buttonPin)
bool Reader::debounceReadNC(int buttonPin)
{
bool reading = digitalRead(buttonPin);
@@ -99,43 +66,4 @@ bool debounceReadNC(int buttonPin)
lastButtonStateNC = reading;
}
return buttonPressed;
}
/**
* @brief Sets all the pins to their correct modes
*
*/
void setPinModes()
{
pinMode(LED_HIGH, OUTPUT); // Green
pinMode(LED_MED, OUTPUT); // Red LED (D4)
pinMode(LED_LOW, OUTPUT); // Green LED
pinMode(BTN_STAFF_UP, INPUT); // SW1
pinMode(BTN_STAFF_DWN, INPUT); // SW2
// RF Receiver
pinMode(REMOTE_A, INPUT); // Remote Button A
pinMode(REMOTE_B, INPUT); // Remote Button B
pinMode(REMOTE_C, INPUT); // Remote Button C
pinMode(REMOTE_D, INPUT); // Remote Button D
// Potentiometer
pinMode(POT_CS, OUTPUT); // D10 - CS
pinMode(POT_UD, OUTPUT); // D9 - U/D
digitalWrite(POT_CS, HIGH);
digitalWrite(POT_UD, LOW);
}
/**
* @brief Resets the potentiometer by setting the wiper all the way to the A pin
*
*/
void resetPotentiometer()
{
// Reset the potentiometer
for (int i = 0; i < 64; i++)
{
potIncrement();
}
}

View File

@@ -10,10 +10,34 @@ int maxVolume = 64;
int userVolume = 64;
int oldUserVolume = userVolume;
Reader reader;
void setup()
{
setPinModes();
resetPotentiometer();
pinMode(LED_HIGH, OUTPUT); // Green
pinMode(LED_MED, OUTPUT); // Red LED (D4)
pinMode(LED_LOW, OUTPUT); // Green LED
pinMode(BTN_STAFF_UP, INPUT); // SW1
pinMode(BTN_STAFF_DWN, INPUT); // SW2
// RF Receiver
pinMode(REMOTE_A, INPUT); // Remote Button A
pinMode(REMOTE_B, INPUT); // Remote Button B
pinMode(REMOTE_C, INPUT); // Remote Button C
pinMode(REMOTE_D, INPUT); // Remote Button D
// Potentiometer
pinMode(POT_CS, OUTPUT); // D10 - CS
pinMode(POT_UD, OUTPUT); // D9 - U/D
digitalWrite(POT_CS, HIGH);
digitalWrite(POT_UD, LOW);
// Reset the potentiometer
for (int i = 0; i < 64; i++)
{
potIncrement();
}
}
void loop()
@@ -21,39 +45,39 @@ void loop()
updateLeds(maxVolume);
// SW1 - Staff Volume Up
if (debounceReadNC(BTN_STAFF_UP))
if (reader.debounceReadNC(BTN_STAFF_UP))
{
staffVolumeUp(&maxVolume);
}
// SW2 - Staff Volume Down
if (debounceReadNC(BTN_STAFF_DWN))
if (reader.debounceReadNC(BTN_STAFF_DWN))
{
staffVolumeDown(&maxVolume, &userVolume, &oldUserVolume);
}
// Remote D - Mute
if (debounceReadNO(REMOTE_D))
if (reader.debounceReadNO(REMOTE_D))
{
mute(&userVolume, &oldUserVolume, maxVolume);
}
// Remote A - Significantly Lower Volume
if (debounceReadNO(REMOTE_A))
if (reader.debounceReadNO(REMOTE_A))
{
lowerVolume(&userVolume, &oldUserVolume, maxVolume);
}
// Remote B - Volume Up
// Make sure we are not is a state of mute or lowered
if (debounceReadNO(REMOTE_B) && !(oldUserVolume > userVolume))
if (reader.debounceReadNO(REMOTE_B) && !(oldUserVolume > userVolume))
{
volumeUp(&userVolume, &oldUserVolume, maxVolume);
}
// Remote C - Volume Down
// Make sure we are not is a state of mute or lowered
if (debounceReadNO(REMOTE_C) && !(oldUserVolume > userVolume))
if (reader.debounceReadNO(REMOTE_C) && !(oldUserVolume > userVolume))
{
volumeDown(&userVolume, &oldUserVolume);
}

View File

@@ -25,7 +25,6 @@ void mute(int *userVolume, int *oldUserVolume, int maxVolume)
if (*oldUserVolume > *userVolume)
{
// This is if the user is muted and is asking to be unmuted
// Check if max volume has been lowered since mute
if (*oldUserVolume > maxVolume)
{

11
test/README Normal file
View File

@@ -0,0 +1,11 @@
This directory is intended for PlatformIO Test Runner and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html

View File

@@ -1,43 +0,0 @@
#include <Arduino.h>
#include <unity.h>
void setUp(void)
{
// set stuff up here
}
void tearDown(void)
{
// clean stuff up here
}
void test_function_should_doBlahAndBlah(void)
{
TEST_ASSERT_TRUE(true);
}
void test_function_should_doAlsoDoBlah(void)
{
TEST_ASSERT_TRUE(true);
}
int runUnityTests(void)
{
UNITY_BEGIN();
RUN_TEST(test_function_should_doBlahAndBlah);
RUN_TEST(test_function_should_doAlsoDoBlah);
return UNITY_END();
}
/**
* For Arduino framework
*/
void setup()
{
// Wait ~2 seconds before the Unity test runner
// establishes connection with a board Serial interface
delay(2000);
runUnityTests();
}
void loop() {}