diff --git a/src/modules/exec/Enconder/Enconder.cpp b/src/modules/exec/Enconder/Enconder.cpp new file mode 100644 index 00000000..2b5d916c --- /dev/null +++ b/src/modules/exec/Enconder/Enconder.cpp @@ -0,0 +1,63 @@ +#include "Global.h" +#include "classes/IoTItem.h" + +#define EB_FAST 30 // таймаут быстрого поворота энкодера, мс +#define EB_DEB 50 // дебаунс кнопки, мс +#define EB_CLICK 400 // таймаут накликивания кнопки, мс +#include "EncButton2.h" + + +class Encoder : public IoTItem { + private: + float step = 1; + float stepOnPress = 5; + EncButton2 enc1; + + public: + Encoder(String parameters) : IoTItem(parameters) { + String pins; + jsonRead(parameters, "pins", pins); + int CLK = selectFromMarkerToMarker(pins, ",", 0).toInt(); + int DT = selectFromMarkerToMarker(pins, ",", 1).toInt(); + int SW = selectFromMarkerToMarker(pins, ",", 2).toInt(); + + jsonRead(parameters, "step", step); + jsonRead(parameters, "stepOnPress", stepOnPress); + + enc1.setPins(INPUT, CLK, DT, SW); + enc1.setEncReverse(false); + } + + void loop() { + if (enc1.tick() != 0) { + if (enc1.left()) { + value.valD = value.valD - step; + regEvent(value.valD, "Encoder_left"); + } else if (enc1.right()) { + value.valD = value.valD + step; + regEvent(value.valD, "Encoder_right"); + } else if (stepOnPress) { + if (enc1.leftH()) { + value.valD = value.valD - stepOnPress; + regEvent(value.valD, "Encoder_leftH"); + } else if (enc1.rightH()) { + value.valD = value.valD + stepOnPress; + regEvent(value.valD, "Encoder_rightH"); + } + } + + // в конце лучше вызвать resetState(), чтобы сбросить необработанные флаги! + enc1.resetState(); + } + } + + ~Encoder() {}; +}; + +void* getAPI_Encoder(String subtype, String param) { + if (subtype == F("Encoder")) { + return new Encoder(param); + } else { + return nullptr; + } +} \ No newline at end of file diff --git a/src/modules/exec/Enconder/modinfo.json b/src/modules/exec/Enconder/modinfo.json new file mode 100644 index 00000000..ce92fb01 --- /dev/null +++ b/src/modules/exec/Enconder/modinfo.json @@ -0,0 +1,66 @@ +{ + "menuSection": "Исполнительные устройства", + "configItem": [ + { + "global": 0, + "name": "Энкодер", + "type": "Writing", + "subtype": "Encoder", + "id": "enc", + "widget": "inputDgt", + "page": "Энкодер", + "descr": "Громкость", + "needSave": 0, + "val": "0", + "round" : 0, + + "step": 1, + "stepOnPress": 5, + "pins": "4,5,2" + } + ], + "about": { + "authorName": "Sergei Yakovlev", + "authorContact": "", + "authorGit": "", + "specialThanks": "", + "moduleName": "Encoder", + "moduleVersion": "1.5", + "usedRam": { + "esp32_4mb": 15, + "esp8266_4mb": 15 + }, + "subTypes": [ + "Encoder" + ], + "title": "Энкодер", + "moduleDesc": "модуль для работы с Энкодером. Кнопочный вариант совместим с модулями Multitouch и ButtonIn", + "retInfo": "Значение счетчика", + "propInfo": { + "step" : "Размер шага Энкодера, может принимать значение 0.0001 или 1000", + "stepOnPress": "Размер шага Энкодера при нажатой кнопке, 0 - отключает учет", + "pins": "Подключеные пины (CLK, DT, SW)" + } + }, + "defActive": true, + "usedLibs": { + "esp32_4mb": [ + "gyverlibs/EncButton @ ^2.0" + ], + "esp8266_4mb": [ + "gyverlibs/EncButton @ ^2.0" + ], + "esp8266_1mb": [ + "gyverlibs/EncButton @ ^2.0" + ], + "esp8266_1mb_ota": [ + "gyverlibs/EncButton @ ^2.0" + ], + "esp8285_1mb": [ + "gyverlibs/EncButton @ ^2.0" + ], + "esp8285_1mb_ota": [ + "gyverlibs/EncButton @ ^2.0" + ] + } +} \ No newline at end of file