From 484f342965dd022591044bcef6e64abda7dc164d Mon Sep 17 00:00:00 2001 From: calania <34073343+calania@users.noreply.github.com> Date: Thu, 2 Dec 2021 10:06:07 +0100 Subject: [PATCH] Update RGBLed.cpp --- src/RGBLed.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/RGBLed.cpp b/src/RGBLed.cpp index 79268e0..8dbe618 100644 --- a/src/RGBLed.cpp +++ b/src/RGBLed.cpp @@ -16,9 +16,23 @@ bool RGBLed::COMMON_CATHODE = false; RGBLed::RGBLed(int red, int green, int blue, bool common): _red(red), _green(green), _blue(blue), _common(common) { - pinMode(_red, OUTPUT); + #if defined(ESP32) + ledcSetup(0, 5000, 8); + ledcSetup(1, 5000, 8); + ledcSetup(2, 5000, 8); + + + ledcAttachPin(_red, 0); + ledcAttachPin(_green, 1); + ledcAttachPin(_blue, 2); + + #else + pinMode(_red, OUTPUT); pinMode(_green, OUTPUT); pinMode(_blue, OUTPUT); + #endif + + } void RGBLed::off() { @@ -88,13 +102,25 @@ void RGBLed::color(int red, int green, int blue) { if (blue > 255) { blue = 255; } if (_common == COMMON_ANODE) { + #if defined(ESP32) + ledcWrite(0, 255 - red); + ledcWrite(1, 255 - green); + ledcWrite(2, 255 - blue); + #else analogWrite(_red, 255 - red); analogWrite(_green, 255 - green); analogWrite(_blue, 255 - blue); + #endif } else { + #if defined(ESP32) + ledcWrite(0, red); + ledcWrite(1, green); + ledcWrite(2, blue); + #else analogWrite(_red, red); analogWrite(_green, green); - analogWrite(_blue, blue); + analogWrite(_blue, blue); + #endif } }