Add evaluatedSwitch component, Add buttonJoystick
Copy gpio component from gate project Add instance of evaluatedswitch for button next to joystick to config Add code for testing the button to main.cpp
This commit is contained in:
4
components/gpio/CMakeLists.txt
Normal file
4
components/gpio/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
idf_component_register(
|
||||
SRCS "gpio_evaluateSwitch.cpp"
|
||||
INCLUDE_DIRS "."
|
||||
)
|
||||
171
components/gpio/gpio_evaluateSwitch.cpp
Normal file
171
components/gpio/gpio_evaluateSwitch.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
#include "gpio_evaluateSwitch.hpp"
|
||||
|
||||
static const char *TAG = "evaluateSwitch";
|
||||
|
||||
|
||||
gpio_evaluatedSwitch::gpio_evaluatedSwitch( //minimal (use default values)
|
||||
gpio_num_t gpio_num_declare
|
||||
){
|
||||
gpio_num = gpio_num_declare;
|
||||
pullup = true;
|
||||
inverted = false;
|
||||
|
||||
init();
|
||||
};
|
||||
|
||||
|
||||
|
||||
gpio_evaluatedSwitch::gpio_evaluatedSwitch( //optional parameters given
|
||||
gpio_num_t gpio_num_declare,
|
||||
bool pullup_declare,
|
||||
bool inverted_declare
|
||||
){
|
||||
gpio_num = gpio_num_declare;
|
||||
pullup = pullup_declare;
|
||||
inverted = inverted_declare;
|
||||
|
||||
init();
|
||||
};
|
||||
|
||||
|
||||
|
||||
void gpio_evaluatedSwitch::init(){
|
||||
ESP_LOGI(TAG, "initializing gpio pin %d", (int)gpio_num);
|
||||
|
||||
//define gpio pin as input
|
||||
gpio_pad_select_gpio(gpio_num);
|
||||
gpio_set_direction(gpio_num, GPIO_MODE_INPUT);
|
||||
|
||||
if (pullup == true){ //enable pullup if desired (default)
|
||||
gpio_pad_select_gpio(gpio_num);
|
||||
gpio_set_pull_mode(gpio_num, GPIO_PULLUP_ONLY);
|
||||
}else{
|
||||
gpio_set_pull_mode(gpio_num, GPIO_FLOATING);
|
||||
gpio_pad_select_gpio(gpio_num);
|
||||
}
|
||||
//TODO add pulldown option
|
||||
//gpio_set_pull_mode(gpio_num, GPIO_PULLDOWN_ONLY);
|
||||
};
|
||||
|
||||
|
||||
void gpio_evaluatedSwitch::handle(){ //Statemachine for debouncing and edge detection
|
||||
if (inverted == true){
|
||||
//=========================================================
|
||||
//=========== Statemachine for inverted switch ============
|
||||
//=================== (switch to VCC) =====================
|
||||
//=========================================================
|
||||
switch (p_state){
|
||||
default:
|
||||
p_state = switchState::FALSE;
|
||||
break;
|
||||
|
||||
case switchState::FALSE: //input confirmed high (released)
|
||||
fallingEdge = false; //reset edge event
|
||||
if (gpio_get_level(gpio_num) == 1){ //pin high (on)
|
||||
p_state = switchState::HIGH;
|
||||
timestampHigh = esp_log_timestamp(); //save timestamp switched from low to high
|
||||
} else {
|
||||
msReleased = esp_log_timestamp() - timestampLow; //update duration released
|
||||
}
|
||||
break;
|
||||
|
||||
case switchState::HIGH: //input recently switched to high (pressed)
|
||||
if (gpio_get_level(gpio_num) == 1){ //pin still high (on)
|
||||
if (esp_log_timestamp() - timestampHigh > minOnMs){ //pin in same state long enough
|
||||
p_state = switchState::TRUE;
|
||||
state = true;
|
||||
risingEdge = true;
|
||||
msReleased = timestampHigh - timestampLow; //calculate duration the button was released
|
||||
}
|
||||
}else{
|
||||
p_state = switchState::FALSE;
|
||||
}
|
||||
break;
|
||||
|
||||
case switchState::TRUE: //input confirmed high (pressed)
|
||||
risingEdge = false; //reset edge event
|
||||
if (gpio_get_level(gpio_num) == 0){ //pin low (off)
|
||||
timestampLow = esp_log_timestamp();
|
||||
p_state = switchState::LOW;
|
||||
} else {
|
||||
msPressed = esp_log_timestamp() - timestampHigh; //update duration pressed
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case switchState::LOW: //input recently switched to low (released)
|
||||
if (gpio_get_level(gpio_num) == 0){ //pin still low (off)
|
||||
if (esp_log_timestamp() - timestampLow > minOffMs){ //pin in same state long enough
|
||||
p_state = switchState::FALSE;
|
||||
msPressed = timestampLow - timestampHigh; //calculate duration the button was pressed
|
||||
state=false;
|
||||
fallingEdge=true;
|
||||
}
|
||||
}else{
|
||||
p_state = switchState::TRUE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}else{
|
||||
//=========================================================
|
||||
//========= Statemachine for not inverted switch ==========
|
||||
//=================== (switch to GND) =====================
|
||||
//=========================================================
|
||||
switch (p_state){
|
||||
default:
|
||||
p_state = switchState::FALSE;
|
||||
break;
|
||||
|
||||
case switchState::FALSE: //input confirmed high (released)
|
||||
fallingEdge = false; //reset edge event
|
||||
if (gpio_get_level(gpio_num) == 0){ //pin low (on)
|
||||
p_state = switchState::LOW;
|
||||
timestampLow = esp_log_timestamp(); //save timestamp switched from high to low
|
||||
} else {
|
||||
msReleased = esp_log_timestamp() - timestampHigh; //update duration released
|
||||
}
|
||||
break;
|
||||
|
||||
case switchState::LOW: //input recently switched to low (pressed)
|
||||
if (gpio_get_level(gpio_num) == 0){ //pin still low (on)
|
||||
if (esp_log_timestamp() - timestampLow > minOnMs){ //pin in same state long enough
|
||||
p_state = switchState::TRUE;
|
||||
state = true;
|
||||
risingEdge = true;
|
||||
msReleased = timestampLow - timestampHigh; //calculate duration the button was released
|
||||
}
|
||||
}else{
|
||||
p_state = switchState::FALSE;
|
||||
}
|
||||
break;
|
||||
|
||||
case switchState::TRUE: //input confirmed low (pressed)
|
||||
risingEdge = false; //reset edge event
|
||||
if (gpio_get_level(gpio_num) == 1){ //pin high (off)
|
||||
timestampHigh = esp_log_timestamp();
|
||||
p_state = switchState::HIGH;
|
||||
} else {
|
||||
msPressed = esp_log_timestamp() - timestampLow; //update duration pressed
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case switchState::HIGH: //input recently switched to high (released)
|
||||
if (gpio_get_level(gpio_num) == 1){ //pin still high (off)
|
||||
if (esp_log_timestamp() - timestampHigh > minOffMs){ //pin in same state long enough
|
||||
p_state = switchState::FALSE;
|
||||
msPressed = timestampHigh - timestampLow; //calculate duration the button was pressed
|
||||
state=false;
|
||||
fallingEdge=true;
|
||||
}
|
||||
}else{
|
||||
p_state = switchState::TRUE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
59
components/gpio/gpio_evaluateSwitch.hpp
Normal file
59
components/gpio/gpio_evaluateSwitch.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h"
|
||||
}
|
||||
|
||||
//constructor examples:
|
||||
//switch to gnd and us internal pullup:
|
||||
//gpio_evaluatedSwitch s3(GPIO_NUM_14);
|
||||
//switch to gnd dont use internal pullup:
|
||||
//gpio_evaluatedSwitch s3(GPIO_NUM_14 false);
|
||||
//switch to VCC (inverted) and dont use internal pullup:
|
||||
//gpio_evaluatedSwitch s3(GPIO_NUM_14 false, true);
|
||||
|
||||
|
||||
class gpio_evaluatedSwitch {
|
||||
public:
|
||||
//--- input ---
|
||||
uint32_t minOnMs = 50;
|
||||
uint32_t minOffMs = 50;
|
||||
gpio_evaluatedSwitch( //constructor minimal (default parameters pullup=true, inverted=false)
|
||||
gpio_num_t gpio_num_declare
|
||||
);
|
||||
gpio_evaluatedSwitch( //constructor with optional parameters
|
||||
gpio_num_t gpio_num_declare,
|
||||
bool pullup_declare,
|
||||
bool inverted_declare=false
|
||||
);
|
||||
|
||||
//--- output --- TODO make readonly? (e.g. public section: const int& x = m_x;)
|
||||
bool state = false;
|
||||
bool risingEdge = false;
|
||||
bool fallingEdge = false;
|
||||
uint32_t msPressed = 0;
|
||||
uint32_t msReleased = 0;
|
||||
|
||||
//--- functions ---
|
||||
void handle(); //Statemachine for debouncing and edge detection
|
||||
|
||||
private:
|
||||
gpio_num_t gpio_num;
|
||||
bool pullup;
|
||||
bool inverted;
|
||||
|
||||
enum class switchState {TRUE, FALSE, LOW, HIGH};
|
||||
switchState p_state = switchState::FALSE;
|
||||
uint32_t timestampLow = 0;
|
||||
uint32_t timestampHigh = 0;
|
||||
void init();
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user