Files
IoTManager/src/items/vPwmOut.cpp

67 lines
1.6 KiB
C++
Raw Normal View History

2021-10-05 19:21:52 +08:00
#include "Consts.h"
#ifdef EnablePwmOut
#include "items/vPwmOut.h"
#include <Arduino.h>
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "Consts.h"
#include "Global.h"
//this class save data to flash
PwmOut::PwmOut(unsigned int pin, String key) {
_pin = pin;
_key = key;
pinMode(_pin, OUTPUT);
int state = jsonReadInt(configStoreJson, key);
this->execute(String(state));
}
PwmOut::~PwmOut() {}
void PwmOut::execute(String state) {
analogWrite(_pin, state.toInt());
eventGen2(_key, state);
jsonWriteInt(configStoreJson, _key, state.toInt());
saveStore();
publishStatus(_key, state);
2021-12-13 16:26:43 +03:00
String path = mqttRootDevice + "/" + _key + "/status";
String json = "{}";
jsonWriteStr(json, "status", state);
String MyJson = json;
jsonWriteStr(MyJson, "topic", path);
ws.textAll(MyJson);
2021-10-05 19:21:52 +08:00
}
MyPwmOutVector* myPwmOut = nullptr;
void pwmOut() {
myLineParsing.update();
String key = myLineParsing.gkey();
String pin = myLineParsing.gpin();
myLineParsing.clear();
pwmOut_EnterCounter++;
addKey(key, pwmOut_KeyList, pwmOut_EnterCounter);
static bool firstTime = true;
if (firstTime) myPwmOut = new MyPwmOutVector();
firstTime = false;
myPwmOut->push_back(PwmOut(pin.toInt(), key));
sCmd.addCommand(key.c_str(), pwmOutExecute);
}
void pwmOutExecute() {
String key = sCmd.order();
String state = sCmd.next();
int number = getKeyNum(key, pwmOut_KeyList);
if (myPwmOut != nullptr) {
if (number != -1) {
myPwmOut->at(number).execute(state);
}
}
}
#endif