From f4a5bc0218336570d7c3938f54177be5f67dc129 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Fri, 31 Jul 2020 01:29:16 +0200 Subject: [PATCH] pwm add --- include/Class/Pwm.h | 33 +++++++++++++++++++++++++++++++++ include/Class/button.h | 1 - src/Class/Button.cpp | 1 - src/Class/Pwm.cpp | 2 ++ src/Cmd.cpp | 38 +++++++++++++++----------------------- 5 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 include/Class/Pwm.h create mode 100644 src/Class/Pwm.cpp diff --git a/include/Class/Pwm.h b/include/Class/Pwm.h new file mode 100644 index 00000000..15353b32 --- /dev/null +++ b/include/Class/Pwm.h @@ -0,0 +1,33 @@ +#pragma once + +#include + +#include "Class/LineParsing.h" +#include "Global.h" + +class Pwm : public LineParsing { + public: + Pwm() : LineParsing(){}; + + void pwmModeSet() { + if (_pin != "") { + pinMode(_pin.toInt(), INPUT); + } + } + + void pwmStateSetDefault() { + if (_state != "") { + pwmChange(_key, _pin, _state); + } + } + + void pwmChange(String key, String pin, String state) { + int pinInt = pin.toInt(); + analogWrite(pinInt, state.toInt()); + eventGen(key, ""); + jsonWriteInt(configLiveJson, key, state.toInt()); + MqttClient::publishStatus(key, state); + } +}; + +extern Pwm* myPwm; \ No newline at end of file diff --git a/include/Class/button.h b/include/Class/button.h index 8e4a55a4..87b4a805 100644 --- a/include/Class/button.h +++ b/include/Class/button.h @@ -10,7 +10,6 @@ class Button : public LineParsing { void pinModeSet() { if (_pin != "") { - Serial.println(_pin); pinMode(_pin.toInt(), OUTPUT); } } diff --git a/src/Class/Button.cpp b/src/Class/Button.cpp index b97e7b54..220de75d 100644 --- a/src/Class/Button.cpp +++ b/src/Class/Button.cpp @@ -1,3 +1,2 @@ #include "Class/Button.h" - Button* myButton; \ No newline at end of file diff --git a/src/Class/Pwm.cpp b/src/Class/Pwm.cpp new file mode 100644 index 00000000..5e2d6ce9 --- /dev/null +++ b/src/Class/Pwm.cpp @@ -0,0 +1,2 @@ +#include "Class/Pwm.h" +Pwm* myPwm; \ No newline at end of file diff --git a/src/Cmd.cpp b/src/Cmd.cpp index c24ab29f..1ab0e642 100644 --- a/src/Cmd.cpp +++ b/src/Cmd.cpp @@ -2,6 +2,7 @@ #include "Class/Button.h" #include "Class/LineParsing.h" +#include "Class/Pwm.h" #include "Global.h" #include "Module/Terminal.h" #include "Servo/Servos.h" @@ -99,15 +100,14 @@ void cmd_init() { sCmd.addCommand("firmwareUpdate", firmwareUpdate); sCmd.addCommand("firmwareVersion", firmwareVersion); - handle_time_init(); - - myButton = new Button(); + handle_time_init(); } //==========================================Модуль кнопок=================================================== //button out light toggle Кнопки Свет 1 pin[12] inv[1] st[1] //========================================================================================================== void button() { + myButton = new Button(); myButton->update(); String key = myButton->gkey(); String pin = myButton->gpin(); @@ -137,31 +137,23 @@ void buttonSet() { //pwm out volume range Кнопки Свет 1 pin[12] st[500] //================================================================================================================== void pwm() { - //line->update(); - //String key = line->gkey(); - //String pin = line->gpin(); - //String state = line->gstate(); - //line->clear(); - - //sCmd.addCommand(key.c_str(), pwmSet); - // - //if (pin != "") { - // jsonWriteInt(configOptionJson, key + "_pin", pin.toInt()); - // analogWrite(pin.toInt(), state.toInt()); - // jsonWriteInt(configLiveJson, key, state.toInt()); - // MqttClient::publishStatus(key, String(state)); - //} + myPwm = new Pwm(); + myPwm->update(); + String key = myPwm->gkey(); + String pin = myPwm->gpin(); + String inv = myPwm->ginv(); + sCmd.addCommand(key.c_str(), pwmSet); + jsonWriteStr(configOptionJson, key + "_pin", pin); + myPwm->pwmModeSet(); + myPwm->pwmStateSetDefault(); + myPwm->clear(); } void pwmSet() { String key = sCmd.order(); String state = sCmd.next(); - int pin = jsonReadInt(configOptionJson, key + "_pin"); - - analogWrite(pin, state.toInt()); - eventGen(key, ""); - jsonWriteStr(configLiveJson, key, state); - MqttClient::publishStatus(key, state); + String pin = jsonReadStr(configOptionJson, key + "_pin"); + myPwm->pwmChange(key, pin, state); } //==================================================================================================================