From 225cdc4526515e84c7ceb0e0b7a69e7ebe5ee455 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Wed, 12 Jun 2024 17:40:32 +0700 Subject: [PATCH] gym initial commit --- src/button.cpp | 78 +++++++++++++++++++++++++++++++++++++++ src/button.hpp | 25 +++++++++++++ src/config.hpp | 17 +++++---- src/main.cpp | 99 ++++++++++++++++++++++++++++++++++++++------------ src/main.hpp | 5 ++- 5 files changed, 190 insertions(+), 34 deletions(-) create mode 100644 src/button.cpp create mode 100644 src/button.hpp diff --git a/src/button.cpp b/src/button.cpp new file mode 100644 index 0000000..6f1374a --- /dev/null +++ b/src/button.cpp @@ -0,0 +1,78 @@ +#include "button.hpp" + +Button::Button(DigitalInputCard *inputCard, uint8_t inputPin) +{ + this->inputCard = inputCard; + this->inputPin = inputPin; + this->input_handler_id = -1; // initialize to MAX_UINT16 + this->shortPressTime = 100; // default short press time + this->longPressTime = 2000; // default long press time +} + +Button::~Button() +{ + if (this->input_handler_id != -1) + this->inputCard->unregisterCallback(this->input_handler_id); +} + +void Button::begin() +{ + auto bindedCallback = std::bind(&Button::buttonChangedCallback, this, std::placeholders::_1, std::placeholders::_2); + this->input_handler_id = this->inputCard->registerCallback(bindedCallback); +} + +void Button::setMinLongPressTime(uint32_t time) +{ + this->longPressTime = time; +} + +void Button::setMinShortPressTime(uint32_t time) +{ + this->shortPressTime = time; +} + +void Button::setLongPressCallback(std::function callback) +{ + this->longPressCallback = callback; +} + +void Button::setShortPressCallback(std::function callback) +{ + this->shortPressCallback = callback; +} + +void Button::buttonChangedCallback(uint8_t pin, uint8_t value) +{ + if (pin != this->inputPin) + return; + + if (value) { + // button pressed + this->buttonState = true; + this->pressedTime = millis(); + } else { + // button released + // If long press is not triggered, then it is a short press + if (this->longPressTriggered == false) { + // check if it is a short press + if (millis() - this->pressedTime > this->shortPressTime) { + if (this->shortPressCallback) + this->shortPressCallback(); + } + } + this->buttonState = false; + this->longPressTriggered = false; + } +} + +void Button::loop() +{ + if (this->buttonState) { + if (millis() - this->pressedTime > this->longPressTime) { + if (this->longPressCallback) { + this->longPressCallback(); + this->longPressTriggered = true; + } + } + } +} diff --git a/src/button.hpp b/src/button.hpp new file mode 100644 index 0000000..2799f90 --- /dev/null +++ b/src/button.hpp @@ -0,0 +1,25 @@ +#include + +class Button { + public: + Button(DigitalInputCard* inputCard, uint8_t inputPin); + ~Button(); + void setMinLongPressTime(uint32_t time); + void setMinShortPressTime(uint32_t time); + void setLongPressCallback(std::function callback); + void setShortPressCallback(std::function callback); + void loop(); + void begin(); + private: + DigitalInputCard* inputCard; + uint8_t inputPin; + uint16_t input_handler_id; + uint32_t shortPressTime; + uint32_t longPressTime; + std::function longPressCallback; + std::function shortPressCallback; + uint32_t pressedTime; + bool buttonState; + bool longPressTriggered; + void buttonChangedCallback(uint8_t pin, uint8_t value); +}; \ No newline at end of file diff --git a/src/config.hpp b/src/config.hpp index aa3dc10..30ab0de 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -1,16 +1,17 @@ #pragma once -/*********************************************** - * Variants * - ***********************************************/ -// @note You can use -D flag to define the variant and comment out the line below -// #define AC_TYPE AC_TYPE_CEILING - /*********************************************** * Pin Definitions * ***********************************************/ -#define TEMP_SENSOR_PIN 32 -#define TEMP_REPORT_INTERVAL_MS 5000 +#define RIGHT_ON_BUTTON_PIN 0 +#define RIGHT_OFF_BUTTON_PIN 1 +#define LEFT_ON_BUTTON_PIN 2 +#define LEFT_OFF_BUTTON_PIN 3 +#define BUTTON_SHORT_PRESS_TIME_MS 150 +#define BUTTON_LONG_PRESS_TIME_MS 2000 + +#define RIGHT_LIGHT_RELAY_PIN 0 +#define LEFT_LIGHT_RELAY_PIN 1 /*********************************************** * Display Configuration * diff --git a/src/main.cpp b/src/main.cpp index 92aed03..345c139 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,8 +4,10 @@ ************************************************/ ESPMegaPRO espmega = ESPMegaPRO(); ESPMegaDisplayOTA internalDisplayOTA = ESPMegaDisplayOTA(); - -DHTNEW dht(TEMP_SENSOR_PIN); +Button rightOnButton(&espmega.inputs, RIGHT_ON_BUTTON_PIN); +Button rightOffButton(&espmega.inputs, RIGHT_OFF_BUTTON_PIN); +Button leftOnButton(&espmega.inputs, LEFT_ON_BUTTON_PIN); +Button leftOffButton(&espmega.inputs, LEFT_OFF_BUTTON_PIN); /************************************************ * End of Global Variables * @@ -55,35 +57,84 @@ void setup() // Input callbacks // This pre-load the input buffers // We need to do this to prevent switches that are left on the "on" position from triggering the callback - ESP_LOGV("CUD IoT OS", "Pre-loading input buffers"); - espmega.inputs.loop(); espmega.inputs.registerCallback(handle_input_change); espmega.iot->registerMqttCallback(handle_mqtt_message); internalDisplayOTA.begin("/intdisp", espmega.display, espmega.webServer); - // Setup the temperature sensor + button_init(); ESP_LOGI("CUD IoT OS", "Initialization Complete"); } +void button_init() +{ + // Initialize buttons + + // Right On Button + rightOnButton.setMinShortPressTime(BUTTON_SHORT_PRESS_TIME_MS); + rightOnButton.setMinLongPressTime(BUTTON_LONG_PRESS_TIME_MS); + rightOnButton.setShortPressCallback([]() + { + // Turn on right light + espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, HIGH); + }); + rightOnButton.setLongPressCallback([]() + { + // Turn on both lights + espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, HIGH); + espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, HIGH); + }); + rightOnButton.begin(); + + // Right Off Button + rightOffButton.setMinShortPressTime(BUTTON_SHORT_PRESS_TIME_MS); + rightOffButton.setMinLongPressTime(BUTTON_LONG_PRESS_TIME_MS); + rightOffButton.setShortPressCallback([]() + { + // Turn off right light + espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, LOW); + }); + rightOffButton.setLongPressCallback([]() + { + // Turn off both lights + espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, LOW); + espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, LOW); + }); + rightOffButton.begin(); + + // Left On Button + leftOnButton.setMinShortPressTime(BUTTON_SHORT_PRESS_TIME_MS); + leftOnButton.setMinLongPressTime(BUTTON_LONG_PRESS_TIME_MS); + leftOnButton.setShortPressCallback([]() + { + // Turn on left light + espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, HIGH); + }); + leftOnButton.setLongPressCallback([]() + { + // Turn on both lights + espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, HIGH); + espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, HIGH); + }); + leftOnButton.begin(); + + // Left Off Button + leftOffButton.setMinShortPressTime(BUTTON_SHORT_PRESS_TIME_MS); + leftOffButton.setMinLongPressTime(BUTTON_LONG_PRESS_TIME_MS); + leftOffButton.setShortPressCallback([]() + { + // Turn off left light + espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, LOW); + }); + leftOffButton.setLongPressCallback([]() + { + // Turn off both lights + espmega.outputs.digitalWrite(RIGHT_LIGHT_RELAY_PIN, LOW); + espmega.outputs.digitalWrite(LEFT_LIGHT_RELAY_PIN, LOW); + }); + leftOffButton.begin(); +} + void loop() { - static uint32_t lastTempUpdate = 0; - if (millis() - lastTempUpdate > TEMP_REPORT_INTERVAL_MS) - { - if(dht.read() != DHTLIB_OK) - { - ESP_LOGE("CUD IoT OS", "Failed to read temperature and humidity"); - return; - } - - float temp = dht.getTemperature(); - float hum = dht.getHumidity(); - char buffer[50]; - snprintf(buffer, 50, "%.2f", temp); - espmega.iot->publishRelative("temperature", buffer); - snprintf(buffer, 50, "%.2f", hum); - espmega.iot->publishRelative("humidity", buffer); - lastTempUpdate = millis(); - } espmega.loop(); } @@ -101,5 +152,5 @@ void handle_input_change(uint8_t pin, bool state) void handle_mqtt_message(char *topic, char *payload) { - // Do nothing + // Do nothing } \ No newline at end of file diff --git a/src/main.hpp b/src/main.hpp index 717d229..1229bbc 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include "button.hpp" #include "config.hpp" /*********************************************** @@ -22,4 +22,5 @@ void setup(); void loop(); void send_stop_bytes(HardwareSerial &uart); void handle_input_change(uint8_t pin, bool state); -void handle_mqtt_message(char *topic, char *payload); \ No newline at end of file +void handle_mqtt_message(char *topic, char *payload); +void button_init(); \ No newline at end of file