mirror of
https://github.com/arwidcool/Solder-Plate.git
synced 2024-11-24 06:50:14 +01:00
refactored buttons to be more robust.
This commit is contained in:
parent
a16db86095
commit
e0e6652978
129
src/Buttons.cpp
129
src/Buttons.cpp
@ -1,129 +0,0 @@
|
||||
#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;
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
#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
|
40
src/buttons/Button.cpp
Normal file
40
src/buttons/Button.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
#include "Button.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
|
||||
// Constructor
|
||||
Button::Button(ButtonKind kind, uint8_t pin) : kind(kind), pin(pin), state(ButtonState::IDLE) {
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
lastStateChangeTime = 0;
|
||||
}
|
||||
|
||||
ButtonKind Button::getKind() {
|
||||
return this->kind;
|
||||
}
|
||||
|
||||
uint8_t Button::getPin() {
|
||||
return this->pin;
|
||||
}
|
||||
|
||||
ButtonState Button::getState() {
|
||||
return this->state;
|
||||
}
|
||||
|
||||
ButtonState Button::setState(ButtonState state) {
|
||||
lastStateChangeTime = millis();
|
||||
this->state = state;
|
||||
return this->state;
|
||||
}
|
||||
|
||||
void Button::loop() {
|
||||
if (digitalRead(this->pin) == LOW) {
|
||||
if (this->state == ButtonState::IDLE && millis() - lastStateChangeTime > 50) {
|
||||
this->setState(ButtonState::PRESSED);
|
||||
}
|
||||
} else if (this->state == ButtonState::PRESSED) {
|
||||
this->setState(ButtonState::RELEASED);
|
||||
}
|
||||
if (this->state == ButtonState::RELEASED && millis() - lastStateChangeTime > 50) {
|
||||
this->setState(ButtonState::IDLE);
|
||||
}
|
||||
}
|
22
src/buttons/Button.h
Normal file
22
src/buttons/Button.h
Normal file
@ -0,0 +1,22 @@
|
||||
#include "__enums.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
class Button
|
||||
{
|
||||
public:
|
||||
// Constructor
|
||||
Button(ButtonKind kind, uint8_t pin);
|
||||
|
||||
ButtonKind getKind();
|
||||
uint8_t getPin();
|
||||
ButtonState getState();
|
||||
ButtonState setState(ButtonState state);
|
||||
void loop();
|
||||
|
||||
|
||||
private :
|
||||
unsigned long lastStateChangeTime;
|
||||
ButtonKind kind;
|
||||
uint8_t pin;
|
||||
ButtonState state;
|
||||
};
|
37
src/buttons/Buttons.cpp
Normal file
37
src/buttons/Buttons.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include "Buttons.h"
|
||||
|
||||
Button* __buttons[4] = { nullptr };
|
||||
|
||||
|
||||
ButtonKind Buttons::getPressedButton()
|
||||
{
|
||||
for (int i=0; i<4; i++) {
|
||||
if (__buttons[i]->getState() == ButtonState::PRESSED) {
|
||||
return __buttons[i]->getKind();
|
||||
}
|
||||
}
|
||||
|
||||
return NONE;
|
||||
}
|
||||
|
||||
Buttons::Buttons()
|
||||
{
|
||||
initializeButtons();
|
||||
}
|
||||
|
||||
void Buttons::initializeButtons()
|
||||
{
|
||||
__buttons[0] = new Button(ButtonKind::UP, upButton);
|
||||
__buttons[1] = new Button(ButtonKind::DOWN, downButton);
|
||||
__buttons[2] = new Button(ButtonKind::BACK, backButton);
|
||||
__buttons[3] = new Button(ButtonKind::SELECT, selectButton);
|
||||
}
|
||||
|
||||
ButtonKind Buttons::handleButtons()
|
||||
{
|
||||
|
||||
for (int i=0; i<4; i++) {
|
||||
__buttons[i]->loop();
|
||||
}
|
||||
return getPressedButton();
|
||||
}
|
27
src/buttons/Buttons.h
Normal file
27
src/buttons/Buttons.h
Normal file
@ -0,0 +1,27 @@
|
||||
#ifndef BUTTONS_H
|
||||
#define BUTTONS_H
|
||||
|
||||
// Include any necessary libraries here
|
||||
#include <Arduino.h>
|
||||
#include "Button.h"
|
||||
|
||||
|
||||
// Button pins
|
||||
#define upButton 21
|
||||
#define downButton 22
|
||||
#define backButton 23
|
||||
#define selectButton 24
|
||||
|
||||
class Buttons
|
||||
{
|
||||
public:
|
||||
// Constructor
|
||||
Buttons();
|
||||
|
||||
ButtonKind getPressedButton();
|
||||
ButtonKind handleButtons();
|
||||
void static initializeButtons();
|
||||
|
||||
};
|
||||
|
||||
#endif // BUTTONS_H
|
21
src/buttons/__enums.h
Normal file
21
src/buttons/__enums.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef __enums_h__
|
||||
#define __enums_h__
|
||||
|
||||
|
||||
enum ButtonState
|
||||
{
|
||||
IDLE,
|
||||
PRESSED,
|
||||
RELEASED
|
||||
};
|
||||
|
||||
enum ButtonKind
|
||||
{
|
||||
UP,
|
||||
DOWN,
|
||||
BACK,
|
||||
SELECT,
|
||||
NONE
|
||||
};
|
||||
|
||||
#endif
|
3
src/leds/leds.h
Normal file
3
src/leds/leds.h
Normal file
@ -0,0 +1,3 @@
|
||||
#define yellowLED 18
|
||||
#define greenLED 19
|
||||
#define redLED 20
|
34
src/main.cpp
34
src/main.cpp
@ -6,18 +6,14 @@
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <Wire.h>
|
||||
#include <Tools/Thermistor.h>
|
||||
#include "Buttons.h"
|
||||
#include "buttons/Buttons.h"
|
||||
#include "leds/leds.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
|
||||
|
||||
|
||||
// LCD display pins
|
||||
#define TFT_CS 7
|
||||
@ -38,8 +34,7 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||
// 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;
|
||||
|
||||
Buttons buttons = Buttons();
|
||||
ArduPID PID;
|
||||
|
||||
void i2cScanner();
|
||||
@ -94,8 +89,25 @@ void setup()
|
||||
void loop()
|
||||
{
|
||||
|
||||
buttons.handleButtons();
|
||||
digitalWrite(yellowLED, LOW);
|
||||
digitalWrite(greenLED, LOW);
|
||||
digitalWrite(redLED, LOW);
|
||||
|
||||
ButtonKind k = buttons.handleButtons();
|
||||
if (k == ButtonKind::UP) {
|
||||
Serial.println("UP");
|
||||
digitalWrite(yellowLED, HIGH);
|
||||
} else if (k == ButtonKind::DOWN) {
|
||||
Serial.println("DOWN");
|
||||
digitalWrite(yellowLED, HIGH);
|
||||
} else if (k == ButtonKind::BACK) {
|
||||
Serial.println("BACK");
|
||||
digitalWrite(redLED, HIGH);
|
||||
} else if (k == ButtonKind::SELECT) {
|
||||
Serial.println("SELECT");
|
||||
digitalWrite(greenLED, HIGH);
|
||||
}
|
||||
/*
|
||||
analogRef.calculate();
|
||||
|
||||
float sysVoltage = analogRef.sysVoltage;
|
||||
@ -137,7 +149,9 @@ void loop()
|
||||
|
||||
// Serial.print("Input voltage: ");
|
||||
// Serial.println(analogRef.calculateInputVoltage());
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
void i2cScanner()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user