button logic finished

Now on par with Python implementation
This commit is contained in:
Martin Brodbeck 2022-06-01 14:56:02 +02:00
parent 278de69a16
commit bde19fd2b7

View file

@ -23,21 +23,29 @@ constexpr uint BUTTON_3_PIN = 15;
constexpr char CUSTOM_CHAR_DEG = 0xDF; constexpr char CUSTOM_CHAR_DEG = 0xDF;
constexpr char CUSTOM_CHAR_AE = 0xE1; constexpr char CUSTOM_CHAR_AE = 0xE1;
// Global variables which have to be accessed by callback function
bool isSystemOn = false;
float temp_tgt{28.0};
absolute_time_t lastPressed = get_absolute_time();
using std::string; using std::string;
void buttonPressedCallback(uint gpio, uint32_t events) { void buttonPressedCallback(uint gpio, uint32_t events) {
static absolute_time_t lastPressed = get_absolute_time(); if (absolute_time_diff_us(lastPressed, get_absolute_time()) < 750000) {
return;
} else {
lastPressed = get_absolute_time();
}
switch (gpio) { switch (gpio) {
case BUTTON_1_PIN: case BUTTON_1_PIN:
temp_tgt -= 0.5;
break; break;
case BUTTON_2_PIN: case BUTTON_2_PIN:
temp_tgt += 0.5;
break; break;
case BUTTON_3_PIN: case BUTTON_3_PIN:
if (absolute_time_diff_us(lastPressed, get_absolute_time()) > 750000) { isSystemOn = !isSystemOn;
std::cout << "Button 3 pressed!" << std::endl;
lastPressed = get_absolute_time();
}
break; break;
} }
} }
@ -62,17 +70,17 @@ int main() {
// Initialize the Buttons // Initialize the Buttons
gpio_set_irq_enabled_with_callback(BUTTON_1_PIN, GPIO_IRQ_EDGE_FALL, true, gpio_set_irq_enabled_with_callback(BUTTON_1_PIN, GPIO_IRQ_EDGE_FALL, true,
&buttonPressedCallback); &buttonPressedCallback);
gpio_set_irq_enabled(BUTTON_2_PIN, GPIO_IRQ_EDGE_FALL, true); gpio_set_irq_enabled_with_callback(BUTTON_2_PIN, GPIO_IRQ_EDGE_FALL, true,
gpio_set_irq_enabled(BUTTON_3_PIN, GPIO_IRQ_EDGE_FALL, true); &buttonPressedCallback);
gpio_set_irq_enabled_with_callback(BUTTON_3_PIN, GPIO_IRQ_EDGE_FALL, true,
&buttonPressedCallback);
float temp_act{0}; float temp_act{0};
float temp_tgt{28.0};
float temp_diff{0.5}; float temp_diff{0.5};
std::stringstream lcdText{}; std::stringstream lcdText{};
bool isHeating = false; bool isHeating = false;
bool isSystemOn = false; string heatInfo{""};
string heatInfo{" "}; string systemInfo{""};
string systemInfo{"OFF"};
lcdText << " G" << CUSTOM_CHAR_AE << "rbox Manager\n (Ver. " lcdText << " G" << CUSTOM_CHAR_AE << "rbox Manager\n (Ver. "
<< PROJECT_VERSION << ")"; << PROJECT_VERSION << ")";
@ -92,6 +100,7 @@ int main() {
isHeating = false; isHeating = false;
} }
isHeating ? heatInfo = ">H<" : heatInfo = " "; isHeating ? heatInfo = ">H<" : heatInfo = " ";
isSystemOn ? systemInfo = "ON " : systemInfo = "OFF";
relais.activate(isHeating); relais.activate(isHeating);