Button added and input

This commit is contained in:
Dmitry Borisenko
2020-08-02 01:54:26 +02:00
parent b2496bbc9f
commit 8df3ab8946
9 changed files with 133 additions and 119 deletions

View File

@@ -1,39 +1,52 @@
#pragma once
#include <Arduino.h>
#include "Class/LineParsing.h"
#include "Global.h"
class Switch : public LineParsing {
protected:
int numberEntering = 0;
int state = _state.toInt();
public:
Switch() : LineParsing(){};
void switchModeSet() {
void init() {
if (_pin != "") {
pinMode(_pin.toInt(), INPUT);
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()) {
}
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;
}
}
void switchStateSetDefault() {
if (_inv == "" && _state != "") {
switchChange(_key, _pin, _state, true);
if (_state != "") {
switchChangeVirtual(_key, _state);
}
}
void switchStateSetInvDefault() {
if (_inv != "" && _state != "") {
switchChange(_key, _pin, _state, false);
}
}
void switchChange(String key, String pin, String state, bool rev) {
int pinInt = pin.toInt();
int stateInt;
if (rev) {
digitalWrite(pinInt, state.toInt());
} else {
digitalWrite(pinInt, !state.toInt());
}
void switchChangeVirtual(String key, String state) {
eventGen(key, "");
jsonWriteInt(configLiveJson, key, state.toInt());
MqttClient::publishStatus(key, state);