From 1dfea88db166bf920a9ee00e3d2a3d6f797aa6b1 Mon Sep 17 00:00:00 2001 From: -help Date: Fri, 26 Jan 2024 01:22:42 +0200 Subject: [PATCH] Refactor Buttons --- src/Buttons.cpp | 129 +++++++++++++++++++++++++++++++ src/Buttons.h | 77 ++++++++++++++++++ src/Tools/Thermistor.cpp | 10 ++- src/Tools/Thermistor.h | 40 ++++------ src/main.cpp | 163 +++------------------------------------ 5 files changed, 241 insertions(+), 178 deletions(-) create mode 100644 src/Buttons.cpp create mode 100644 src/Buttons.h diff --git a/src/Buttons.cpp b/src/Buttons.cpp new file mode 100644 index 0000000..5c1ba63 --- /dev/null +++ b/src/Buttons.cpp @@ -0,0 +1,129 @@ +#include "Buttons.h" + +volatile bool upButtonPressed = false; +volatile bool downButtonPressed = false; +volatile bool backButtonPressed = false; +volatile bool selectButtonPressed = false; + +ButtonState upButtonState = IDLE; +ButtonState downButtonState = IDLE; +ButtonState backButtonState = IDLE; +ButtonState selectButtonState = IDLE; + + +Button Buttons::getPressedButton() +{ + if (upButtonPressed) + { + upButtonPressed = false; + + return UP; + } + else if (downButtonPressed) + { + downButtonPressed = false; + + return DOWN; + } + else if (backButtonPressed) + { + backButtonPressed = false; + + return BACK; + } + else if (selectButtonPressed) + { + selectButtonPressed = false; + + return SELECT; + } + + return NONE; +} + +Buttons::Buttons() +{ + initializeButtons(); +} + +Buttons::~Buttons() +{ +} + +void Buttons::initializeButtons() +{ + + pinMode(upButton, INPUT_PULLUP); + pinMode(downButton, INPUT_PULLUP); + pinMode(backButton, INPUT_PULLUP); + pinMode(selectButton, INPUT_PULLUP); + + attachInterrupt(digitalPinToInterrupt(upButton), upButtonISR, FALLING); + attachInterrupt(digitalPinToInterrupt(downButton), downButtonISR, FALLING); + attachInterrupt(digitalPinToInterrupt(backButton), backButtonISR, FALLING); + attachInterrupt(digitalPinToInterrupt(selectButton), selectButtonISR, FALLING); +} + +Button Buttons::handleButtons() +{ + + Button pressedButton = getPressedButton(); + + if (pressedButton != NONE) + { + Serial.print("Button pressed: "); + switch (pressedButton) + { + case UP: + Serial.println("UP"); + digitalWrite(yellowLED, HIGH); + delay(100); + digitalWrite(yellowLED, LOW); + break; + case DOWN: + Serial.println("DOWN"); + digitalWrite(yellowLED, HIGH); + delay(100); + digitalWrite(yellowLED, LOW); + break; + case BACK: + Serial.println("BACK"); + digitalWrite(redLED, HIGH); + delay(100); + digitalWrite(redLED, LOW); + break; + case SELECT: + Serial.println("SELECT"); + analogWrite(greenLED, 20); + delay(100); + analogWrite(greenLED, 0); + + break; + default: + break; + } + } + + return pressedButton; +} + +void Buttons::upButtonISR() +{ + + upButtonPressed = true; +} + +void Buttons::downButtonISR() +{ + downButtonPressed = true; +} + +void Buttons::backButtonISR() +{ + backButtonPressed = true; +} + +void Buttons::selectButtonISR() +{ + selectButtonPressed = true; +} diff --git a/src/Buttons.h b/src/Buttons.h new file mode 100644 index 0000000..09a7d9d --- /dev/null +++ b/src/Buttons.h @@ -0,0 +1,77 @@ +#ifndef BUTTONS_H +#define BUTTONS_H + +// Include any necessary libraries here +#include + +enum ButtonState +{ + IDLE, + PRESSED, + RELEASED +}; + +enum Button +{ + UP, + DOWN, + BACK, + SELECT, + NONE +}; + + + +extern volatile bool upButtonPressed; +extern volatile bool downButtonPressed; +extern volatile bool backButtonPressed; +extern volatile bool selectButtonPressed; + +extern enum ButtonState upButtonState; +extern enum ButtonState downButtonState; +extern enum ButtonState backButtonState; +extern enum ButtonState selectButtonState; + + + +// LED pins +#define yellowLED 18 +#define greenLED 19 +#define redLED 20 + +// Button pins +#define upButton 21 +#define downButton 22 +#define backButton 23 +#define selectButton 24 +// Define any constants or macros here +#define BUTTON_PIN 2 + +// Declare any global variables here + + +// Declare any function prototypes here + +class Buttons +{ +public: + // Constructor + Buttons(); + ~Buttons(); + + Button getPressedButton(); + Button handleButtons(); + + void static upButtonISR(); + void static downButtonISR(); + void static backButtonISR(); + void static selectButtonISR(); + + void static initializeButtons(); + + // Public Variables + bool pressed = false; + bool released = false; +}; + +#endif // BUTTONS_H diff --git a/src/Tools/Thermistor.cpp b/src/Tools/Thermistor.cpp index 4b7552d..01e7fcf 100644 --- a/src/Tools/Thermistor.cpp +++ b/src/Tools/Thermistor.cpp @@ -13,6 +13,15 @@ Thermistor::Thermistor(uint8_t pin, float resistance, TempCalibration calibratio calculateCoefficents(resistance, calibration); } +Thermistor::Thermistor(uint8_t pin, float resistance) +{ + + thermistorPin = pin; + setRes = resistance; + + calculateCoefficents(resistance, calibration_100K_3950); +} + Thermistor::~Thermistor() { } @@ -32,7 +41,6 @@ int Thermistor::getTemperature() temp = temp / samples; - return temp; } diff --git a/src/Tools/Thermistor.h b/src/Tools/Thermistor.h index 8d69e32..b216c95 100644 --- a/src/Tools/Thermistor.h +++ b/src/Tools/Thermistor.h @@ -4,18 +4,6 @@ #include #include - -#define THERMISTOR1_PIN 39 -#define THERMISTOR2_PIN 38 -#define THERMISTOR3_PIN 37 -#define THERMISTOR4_PIN 36 -#define THERMISTOR5_PIN 33 -#define THERMISTOR6_PIN 32 - -extern AnalogRef analogRef; - -// Include any necessary libraries here - struct Coefficents { float a; @@ -33,6 +21,20 @@ struct TempCalibration float highR; }; + +extern TempCalibration calibration_100K_3950; + +#define THERMISTOR1_PIN 39 +#define THERMISTOR2_PIN 38 +#define THERMISTOR3_PIN 37 +#define THERMISTOR4_PIN 36 +#define THERMISTOR5_PIN 33 +#define THERMISTOR6_PIN 32 + +extern AnalogRef analogRef; + +// Include any necessary libraries here + class Thermistor { public: @@ -41,33 +43,25 @@ public: Thermistor(uint8_t pin, float resistance, TempCalibration calibration); + Thermistor(uint8_t pin, float resistance); + // Destructor ~Thermistor(); // Public Methods int getTemperature(); - float getResistance(); + float getResistance(); // Public Variables private: - const double K = 273.15; - float sensorResistance; - uint8_t thermistorPin; float setRes; - Coefficents coefficents; - float referenceResistance; - void calculateCoefficents(float resistance, TempCalibration calibration); - - - - }; #endif // THERMISTOR_H diff --git a/src/main.cpp b/src/main.cpp index 7be5cd9..29ab0c5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,50 +6,18 @@ #include #include #include - -enum Button -{ - UP, - DOWN, - BACK, - SELECT, - NONE -}; +#include "Buttons.h" AnalogRef analogRef(5.0); +// Calibration data for 100K thermistor +TempCalibration calibration_100K_3950 = {25, 100000, 86, 10000, 170, 1000}; + // LED pins #define yellowLED 18 #define greenLED 19 #define redLED 20 -// Button pins -#define upButton 21 -#define downButton 22 -#define backButton 23 -#define selectButton 24 - -volatile bool upButtonPressed = false; -volatile bool downButtonPressed = false; -volatile bool backButtonPressed = false; -volatile bool selectButtonPressed = false; - -volatile bool upButtonReleased = false; -volatile bool downButtonReleased = false; -volatile bool backButtonReleased = false; -volatile bool selectButtonReleased = false; - -enum ButtonState -{ - IDLE, - PRESSED, - RELEASED -}; - -ButtonState upButtonState = IDLE; -ButtonState downButtonState = IDLE; -ButtonState backButtonState = IDLE; -ButtonState selectButtonState = IDLE; // LCD display pins #define TFT_CS 7 @@ -64,25 +32,16 @@ Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST); // OLED display width and height, for a typical 128x64 display #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 - #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); -// Initalize a 3950 100K thermistor with 2.5k reference resistor -Thermistor thermistor1(THERMISTOR1_PIN, 2500, {25, 100000, 86, 10000, 170, 1000}); +// Initalize a 3950 100K thermistor with 2.5k reference resistor using the default calibration data for 100K thermistor +Thermistor thermistor1(THERMISTOR1_PIN, 2500); + +Buttons buttons; ArduPID PID; -Button getPressedButton(); -Button handleButtons(); - - - -void upButtonISR(); -void downButtonISR(); -void backButtonISR(); -void selectButtonISR(); - void i2cScanner(); void setup() @@ -97,22 +56,11 @@ void setup() analogWriteFrequency(64); - display.setRotation(3); pinMode(yellowLED, OUTPUT); pinMode(greenLED, OUTPUT); pinMode(redLED, OUTPUT); - pinMode(upButton, INPUT_PULLUP); - pinMode(downButton, INPUT_PULLUP); - pinMode(backButton, INPUT_PULLUP); - pinMode(selectButton, INPUT_PULLUP); - - attachInterrupt(digitalPinToInterrupt(upButton), upButtonISR, FALLING); - attachInterrupt(digitalPinToInterrupt(downButton), downButtonISR, FALLING); - attachInterrupt(digitalPinToInterrupt(backButton), backButtonISR, FALLING); - attachInterrupt(digitalPinToInterrupt(selectButton), selectButtonISR, FALLING); - Serial.begin(9600); Serial.println("Starting LCD"); @@ -146,7 +94,7 @@ void setup() void loop() { - handleButtons(); + buttons.handleButtons(); analogRef.calculate(); @@ -191,99 +139,6 @@ void loop() // Serial.println(analogRef.calculateInputVoltage()); } -Button getPressedButton() -{ - if (upButtonPressed) - { - upButtonPressed = false; - - return UP; - } - else if (downButtonPressed) - { - downButtonPressed = false; - - return DOWN; - } - else if (backButtonPressed) - { - backButtonPressed = false; - - return BACK; - } - else if (selectButtonPressed) - { - selectButtonPressed = false; - - return SELECT; - } - - return NONE; -} - -Button handleButtons() -{ - - Button pressedButton = getPressedButton(); - - if (pressedButton != NONE) - { - Serial.print("Button pressed: "); - switch (pressedButton) - { - case UP: - Serial.println("UP"); - digitalWrite(yellowLED, HIGH); - delay(100); - digitalWrite(yellowLED, LOW); - break; - case DOWN: - Serial.println("DOWN"); - digitalWrite(yellowLED, HIGH); - delay(100); - digitalWrite(yellowLED, LOW); - break; - case BACK: - Serial.println("BACK"); - digitalWrite(redLED, HIGH); - delay(100); - digitalWrite(redLED, LOW); - break; - case SELECT: - Serial.println("SELECT"); - analogWrite(greenLED, 20); - delay(100); - analogWrite(greenLED, 0); - - break; - default: - break; - } - } - - return pressedButton; -} - -void upButtonISR() -{ - upButtonPressed = true; -} - -void downButtonISR() -{ - downButtonPressed = true; -} - -void backButtonISR() -{ - backButtonPressed = true; -} - -void selectButtonISR() -{ - selectButtonPressed = true; -} - void i2cScanner() {