gym initial commit
This commit is contained in:
parent
fa53ddbc0f
commit
225cdc4526
|
@ -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<void()> callback)
|
||||
{
|
||||
this->longPressCallback = callback;
|
||||
}
|
||||
|
||||
void Button::setShortPressCallback(std::function<void()> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
#include <DigitalInputCard.hpp>
|
||||
|
||||
class Button {
|
||||
public:
|
||||
Button(DigitalInputCard* inputCard, uint8_t inputPin);
|
||||
~Button();
|
||||
void setMinLongPressTime(uint32_t time);
|
||||
void setMinShortPressTime(uint32_t time);
|
||||
void setLongPressCallback(std::function<void()> callback);
|
||||
void setShortPressCallback(std::function<void()> callback);
|
||||
void loop();
|
||||
void begin();
|
||||
private:
|
||||
DigitalInputCard* inputCard;
|
||||
uint8_t inputPin;
|
||||
uint16_t input_handler_id;
|
||||
uint32_t shortPressTime;
|
||||
uint32_t longPressTime;
|
||||
std::function<void()> longPressCallback;
|
||||
std::function<void()> shortPressCallback;
|
||||
uint32_t pressedTime;
|
||||
bool buttonState;
|
||||
bool longPressTriggered;
|
||||
void buttonChangedCallback(uint8_t pin, uint8_t value);
|
||||
};
|
|
@ -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 *
|
||||
|
|
99
src/main.cpp
99
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
|
||||
}
|
|
@ -12,7 +12,7 @@
|
|||
#include <ESPMegaProOS.hpp>
|
||||
#include <ETH.h>
|
||||
#include <ESPMegaDisplayOTA.hpp>
|
||||
#include <dhtnew.h>
|
||||
#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);
|
||||
void handle_mqtt_message(char *topic, char *payload);
|
||||
void button_init();
|
Loading…
Reference in New Issue