Files
IoTManager/include/Class/Switch.h

56 lines
1.5 KiB
C
Raw Normal View History

2020-07-31 01:52:57 +02:00
#pragma once
#include <Arduino.h>
2020-08-02 01:54:26 +02:00
2020-07-31 01:52:57 +02:00
#include "Class/LineParsing.h"
#include "Global.h"
class Switch : public LineParsing {
2020-08-02 01:54:26 +02:00
protected:
int numberEntering = 0;
int state = _state.toInt();
2020-07-31 01:52:57 +02:00
public:
Switch() : LineParsing(){};
2020-08-02 01:54:26 +02:00
void init() {
2020-07-31 01:52:57 +02:00
if (_pin != "") {
2020-08-02 01:54:26 +02:00
int number = numberEntering++;
buttons[number].attach(_pin.toInt());
buttons[number].interval(_db.toInt());
but[number] = true;
jsonWriteStr(configOptionJson, "switch_num_" + String(number), _key);
2020-07-31 01:52:57 +02:00
}
}
2020-08-02 01:54:26 +02:00
void loop() {
static uint8_t switch_number = 1;
if (but[switch_number]) {
buttons[switch_number].update();
if (buttons[switch_number].fell()) {
}
if (buttons[switch_number].rose()) {
String key = jsonReadStr(configOptionJson, "switch_num_" + String(switch_number));
state = !state;
switchChangeVirtual(key, String(state));
}
}
switch_number++;
if (switch_number == NUM_BUTTONS) {
switch_number = 0;
2020-07-31 01:52:57 +02:00
}
}
2020-08-02 01:54:26 +02:00
void switchStateSetDefault() {
if (_state != "") {
switchChangeVirtual(_key, _state);
2020-07-31 01:52:57 +02:00
}
}
2020-08-02 01:54:26 +02:00
void switchChangeVirtual(String key, String state) {
2020-07-31 01:52:57 +02:00
eventGen(key, "");
jsonWriteInt(configLiveJson, key, state.toInt());
MqttClient::publishStatus(key, state);
}
};
extern Switch* mySwitch;