From 5e66391520378892dbd17e6ef4fd3b3ff8fc5c75 Mon Sep 17 00:00:00 2001 From: biver Date: Thu, 11 Aug 2022 09:01:13 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20Pwm32?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_svelte/items.json | 8 ++-- platformio.ini | 3 +- src/modules/API.cpp | 4 +- src/modules/exec/Pwm32/Pwm32.cpp | 74 +++++++++++++++++++++++++++++ src/modules/exec/Pwm32/modinfo.json | 44 +++++++++++++++++ 5 files changed, 127 insertions(+), 6 deletions(-) create mode 100644 src/modules/exec/Pwm32/Pwm32.cpp create mode 100644 src/modules/exec/Pwm32/modinfo.json diff --git a/data_svelte/items.json b/data_svelte/items.json index ebc91b24..f4028041 100644 --- a/data_svelte/items.json +++ b/data_svelte/items.json @@ -96,16 +96,18 @@ "num": 6 }, { - "name": "7. PWM ESP8266", + "name": "7. PWM ESP32", "type": "Writing", - "subtype": "Pwm8266", + "subtype": "Pwm32", "id": "pwm", "widget": "range", "page": "Кнопки", "descr": "PWM", "int": 0, - "pin": 15, + "pin": 2, "freq": 5000, + "ledChannel": 2, + "PWM_resolution": 12, "val": 0, "apin": -1, "num": 7 diff --git a/platformio.ini b/platformio.ini index e61f0d7f..9e0deda1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -39,7 +39,7 @@ build_src_filter = ${env:esp32_4mb_fromitems.build_src_filter} [platformio] -default_envs = esp8266_4mb +default_envs = esp32_4mb data_dir = data_svelte [common_env_data] @@ -115,6 +115,7 @@ build_src_filter = + + + + + + + + diff --git a/src/modules/API.cpp b/src/modules/API.cpp index e32991d7..23b69a3f 100644 --- a/src/modules/API.cpp +++ b/src/modules/API.cpp @@ -5,7 +5,7 @@ void* getAPI_ButtonIn(String subtype, String params); void* getAPI_ButtonOut(String subtype, String params); void* getAPI_IoTServo(String subtype, String params); void* getAPI_Mp3(String subtype, String params); -void* getAPI_Pwm8266(String subtype, String params); +void* getAPI_Pwm32(String subtype, String params); void* getAPI_Timer(String subtype, String params); void* getAPI_Aht20(String subtype, String params); void* getAPI_AnalogAdc(String subtype, String params); @@ -32,7 +32,7 @@ if ((tmpAPI = getAPI_ButtonIn(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_ButtonOut(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_IoTServo(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Mp3(subtype, params)) != nullptr) return tmpAPI; -if ((tmpAPI = getAPI_Pwm8266(subtype, params)) != nullptr) return tmpAPI; +if ((tmpAPI = getAPI_Pwm32(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Timer(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Aht20(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_AnalogAdc(subtype, params)) != nullptr) return tmpAPI; diff --git a/src/modules/exec/Pwm32/Pwm32.cpp b/src/modules/exec/Pwm32/Pwm32.cpp new file mode 100644 index 00000000..6798da3f --- /dev/null +++ b/src/modules/exec/Pwm32/Pwm32.cpp @@ -0,0 +1,74 @@ +#include "Global.h" +#include "classes/IoTItem.h" +#include "Arduino.h" + +#include "esp32-hal.h" +#include "esp32-hal-ledc.h" + +extern IoTGpio IoTgpio; + +class Pwm32 : public IoTItem { + private: + int _pin; + int _freq; + int _apin, _oldValue; + bool _freezVal = true; + int _ledChannel; + int _resolution; + + public: + Pwm32(String parameters): IoTItem(parameters) { + _interval = _interval / 1000; // корректируем величину интервала int, теперь он в миллисекундах + + jsonRead(parameters, "pin", _pin); + jsonRead(parameters, "freq", _freq); + jsonRead(parameters, "ledChannel", _ledChannel); + jsonRead(parameters, "PWM_resolution", _resolution); + + pinMode(_pin, OUTPUT); + ledcSetup(_ledChannel, _freq, _resolution); + ledcAttachPin(_pin, _ledChannel); + ledcWrite(_ledChannel, value.valD); + + _resolution = pow(2, _resolution); // переводим биты в значение + + jsonRead(parameters, "apin", _apin); + if (_apin >= 0) IoTgpio.pinMode(_apin, INPUT); + } + + void doByInterval() { + if (_apin >= 0) { + value.valD = map(IoTgpio.analogRead(_apin), 0, 4095, 0, _resolution); + if (value.valD > _resolution - 6) value.valD = _resolution; // нивелируем погрешность установки мин и макс + else if (value.valD < 9) value.valD = 0; // из-за смягчения значений + if (abs(_oldValue - value.valD) > 20) { + _oldValue = value.valD; + ledcWrite(_ledChannel, value.valD); + _freezVal = false; + } else { + if (!_freezVal) { // отправляем событие только раз после серии изменений, чтоб не спамить события + regEvent(value.valD, "Pwm32"); + _freezVal = true; + } + } + } + } + + void setValue(IoTValue Value) { + value = Value; + ledcWrite(_ledChannel, value.valD); + regEvent(value.valD, "Pwm32"); + } + //======================================================================================================= + + ~Pwm32() {}; +}; + + +void* getAPI_Pwm32(String subtype, String param) { + if (subtype == F("Pwm32")) { + return new Pwm32(param); + } else { + return nullptr; + } +} diff --git a/src/modules/exec/Pwm32/modinfo.json b/src/modules/exec/Pwm32/modinfo.json new file mode 100644 index 00000000..4c2e5bc0 --- /dev/null +++ b/src/modules/exec/Pwm32/modinfo.json @@ -0,0 +1,44 @@ +{ + "menuSection": "Модули управления", + + "configItem": [{ + "name": "PWM ESP32", + "type": "Writing", + "subtype": "Pwm32", + "id": "pwm", + "widget": "range", + "page": "Кнопки", + "descr": "PWM", + "int": 0, + "pin": 2, + "freq": 5000, + "ledChannel": 2, + "PWM_resolution": 10, + "val": 0, + "apin": -1 + }], + + "about": { + "authorName": "Avaks", + "authorContact": "https://t.me/Avaks", + "authorGit": "https://github.com/avaksru", + "specialThanks": "Serghei Crasnicov @Serghei63", + "moduleName": "Pwm32", + "moduleVersion": "1.0", + "moduleDesc": "Позволяет управлять Широтно-Импульсной Модуляцией на конкретном пине платы.", + "propInfo": { + "int": "Количество миллисекунд между опросами аналога. 0 - выключено.", + "pin": "Управляемый пин", + "apin": "Номер GPIO аналогового пина. Если -1, то функция отключена.", + "ledChannel": "Канал ШИМ", + "PWM_resolution": "Разрешение, определяет диапазон значений от 1 до 16 => от 1 до 65536", + "freq": "Частота" + } + }, + + "defActive": true, + + "devices": { + "esp32_4mb": [] + } +} \ No newline at end of file