Files
IoTManager/include/items/ButtonInClass.h

61 lines
1.6 KiB
C
Raw Normal View History

2020-09-02 22:34:49 +03:00
#pragma once
#include <Arduino.h>
2020-10-04 02:45:26 +03:00
2020-09-02 22:34:49 +03:00
#include "Class/LineParsing.h"
#include "Global.h"
2020-11-04 23:48:21 +03:00
2020-09-03 01:12:43 +03:00
class ButtonInClass : public LineParsing {
2020-09-02 22:34:49 +03:00
protected:
int numberEntering = 0;
int state = _state.toInt();
public:
2020-09-03 01:12:43 +03:00
ButtonInClass() : LineParsing(){};
2020-09-02 22:34:49 +03:00
void init() {
if (_pin != "") {
int number = numberEntering++;
buttons[number].attach(_pin.toInt());
buttons[number].interval(_db.toInt());
but[number] = true;
jsonWriteStr(configOptionJson, "switch_num_" + String(number), _key);
}
}
void loop() {
static uint8_t switch_number = 1;
if (but[switch_number]) {
buttons[switch_number].update();
if (buttons[switch_number].fell()) {
String key = jsonReadStr(configOptionJson, "switch_num_" + String(switch_number));
2020-10-05 00:03:53 +03:00
state = 1;
2020-09-02 22:34:49 +03:00
switchChangeVirtual(key, String(state));
}
2020-10-04 02:45:26 +03:00
if (buttons[switch_number].rose()) {
2020-10-04 23:54:41 +03:00
String key = jsonReadStr(configOptionJson, "switch_num_" + String(switch_number));
2020-10-05 00:03:53 +03:00
state = 0;
2020-10-04 23:54:41 +03:00
switchChangeVirtual(key, String(state));
2020-10-04 02:45:26 +03:00
}
2020-09-02 22:34:49 +03:00
}
switch_number++;
if (switch_number == NUM_BUTTONS) {
switch_number = 0;
}
}
2020-10-04 02:45:26 +03:00
2020-09-02 22:34:49 +03:00
void switchStateSetDefault() {
if (_state != "") {
switchChangeVirtual(_key, _state);
}
}
void switchChangeVirtual(String key, String state) {
eventGen(key, "");
jsonWriteInt(configLiveJson, key, state.toInt());
2020-10-04 02:45:26 +03:00
publishStatus(key, state);
2020-09-02 22:34:49 +03:00
}
};
2020-09-03 02:07:12 +03:00
extern ButtonInClass myButtonIn;