mirror of
https://github.com/arwidcool/Solder-Plate.git
synced 2024-11-24 15:00:15 +01:00
Refactor Buttons
This commit is contained in:
parent
e8228fbbb3
commit
1dfea88db1
129
src/Buttons.cpp
Normal file
129
src/Buttons.cpp
Normal file
@ -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;
|
||||
}
|
77
src/Buttons.h
Normal file
77
src/Buttons.h
Normal file
@ -0,0 +1,77 @@
|
||||
#ifndef BUTTONS_H
|
||||
#define BUTTONS_H
|
||||
|
||||
// Include any necessary libraries here
|
||||
#include <Arduino.h>
|
||||
|
||||
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
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -4,18 +4,6 @@
|
||||
#include <Arduino.h>
|
||||
#include <Tools/AnalogRef.h>
|
||||
|
||||
|
||||
#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
|
||||
|
163
src/main.cpp
163
src/main.cpp
@ -6,50 +6,18 @@
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <Wire.h>
|
||||
#include <Tools/Thermistor.h>
|
||||
|
||||
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()
|
||||
{
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user