From 203a18c1582ecae90468d6527c311c82d4e0c1c6 Mon Sep 17 00:00:00 2001 From: EnricoGuccii Date: Wed, 7 May 2025 22:37:36 +0200 Subject: better RGB --- .vscode/settings.json | 3 +++ lib/oled/oled.cpp | 10 +++---- lib/oled/oled.h | 16 +++++++---- lib/rgb/rgb.cpp | 75 ++++++++++++++++++++++++++++++++------------------- lib/rgb/rgb.h | 11 +++++--- src/main.cpp | 70 ++++++----------------------------------------- 6 files changed, 81 insertions(+), 104 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..70e34ec --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "C_Cpp.errorSquiggles": "disabled" +} \ No newline at end of file diff --git a/lib/oled/oled.cpp b/lib/oled/oled.cpp index 0c433f2..0634527 100644 --- a/lib/oled/oled.cpp +++ b/lib/oled/oled.cpp @@ -5,16 +5,16 @@ // #include "settings.h" // #include "oled.h" -// Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); -// void oled_init() { +// OLED::OLED() { +// Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // Wire.begin(SDA_PIN, SCL_PIN); // display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR); // display.clearDisplay(); // display.display(); // } -// void oled_print(const String& text, int x, int y) { +// void OLED::print(const String& text, int x, int y) { // display.setTextSize(1); // display.setTextColor(WHITE); // display.setCursor(x, y); @@ -22,12 +22,12 @@ // display.display(); // } -// void oled_clear() { +// void OLED::clear() { // display.clearDisplay(); // display.display(); // } -// void oled_drawBox() { +// void OLED::drawBox() { // display.drawRect(0, 0, 128, 64, WHITE); // display.display(); // } diff --git a/lib/oled/oled.h b/lib/oled/oled.h index e229dbe..426842a 100644 --- a/lib/oled/oled.h +++ b/lib/oled/oled.h @@ -2,8 +2,14 @@ // #include // #include "settings.h" - -// void oled_init(); -// void oled_clear(); -// void oled_print(const String& text, int x, int y); -// void oled_drawBox(); +// class OLED +// { +// public: +// OLED(); +// void clear(); +// void print(const String &text, int x, int y); +// void drawBox(); +// void setCursor(int x, int y); +// void setTextSize(int size); +// void showAnimation(); +// }; \ No newline at end of file diff --git a/lib/rgb/rgb.cpp b/lib/rgb/rgb.cpp index a748006..469cf3d 100644 --- a/lib/rgb/rgb.cpp +++ b/lib/rgb/rgb.cpp @@ -7,7 +7,6 @@ int RGB::scaleColor(uint8_t val) return map(val, 0, 255, 0, 1023); } - RGB::RGB() { pinMode(RED_PIN, OUTPUT); @@ -15,53 +14,72 @@ RGB::RGB() pinMode(BLUE_PIN, OUTPUT); } - -void RGB::setColorRGB(uint8_t red, uint8_t green, uint8_t blue) +void RGB::setColorRGB(uint8_t red, uint8_t green, uint8_t blue, bool saveColor = true) { analogWrite(RED_PIN, scaleColor(red)); analogWrite(GREEN_PIN, scaleColor(green)); analogWrite(BLUE_PIN, scaleColor(blue)); + if (saveColor) + { + currentR = red; + currentG = green; + currentB = blue; + } } - void RGB::fadeColor(uint8_t red, uint8_t green, uint8_t blue, int duration) { - for (int i = 0; i <= 255; i++) - { - setColorRGB(red * i / 255, green * i / 255, blue * i / 255); - delay(duration / 255); - } - for (int i = 255; i >= 0; i--) + int startR = currentR; + int startG = currentG; + int startB = currentB; + + for (int i = 0; i <= duration; i++) { - setColorRGB(red * i / 255, green * i / 255, blue * i / 255); - delay(duration / 255); - } -} + uint8_t newR = startR + ((red - startR) * i) / duration; + uint8_t newG = startG + ((green - startG) * i) / duration; + uint8_t newB = startB + ((blue - startB) * i) / duration; + setColorRGB(newR, newG, newB, false); + delay(1); + } -void RGB::blinkColor(uint8_t red, uint8_t green, uint8_t blue, int duration) -{ setColorRGB(red, green, blue); - delay(duration); - setColorRGB(0, 0, 0); - delay(duration); } - -void RGB::breatheColor(uint8_t red, uint8_t green, uint8_t blue, int duration) +void RGB::blink(int cycles, int interval) { - for (int i = 0; i <= 255; i++) + for (int i = 0; i < cycles; i++) { - setColorRGB(red * i / 255, green * i / 255, blue * i / 255); - delay(duration / 255); + setColorRGB(0, 0, 0, false); + delay(interval); + setColorRGB(currentR, currentG, currentB, false); + delay(interval); } - for (int i = 255; i >= 0; i--) +} + +void RGB::breathe(int cycles, int interval) +{ + uint8_t baseR = currentR; + uint8_t baseG = currentG; + uint8_t baseB = currentB; + + for (int c = 0; c < cycles; ++c) { - setColorRGB(red * i / 255, green * i / 255, blue * i / 255); - delay(duration / 255); + for (int i = 0; i <= 255; ++i) + { + setColorRGB(baseR * i / 255, baseG * i / 255, baseB * i / 255, false); + delay(interval / 510); + } + + for (int i = 255; i >= 0; --i) + { + setColorRGB(baseR * i / 255, baseG * i / 255, baseB * i / 255, false); + delay(interval / 510); + } } -} + setColorRGB(baseR, baseG, baseB); +} void RGB::rainbowCycle(int duration) { @@ -73,4 +91,5 @@ void RGB::rainbowCycle(int duration) setColorRGB(red, green, blue); delay(duration / 256); } + setColorRGB(currentR, currentG, currentB, false); } diff --git a/lib/rgb/rgb.h b/lib/rgb/rgb.h index 562e9dd..2f50088 100644 --- a/lib/rgb/rgb.h +++ b/lib/rgb/rgb.h @@ -6,14 +6,17 @@ class RGB { public: + uint8_t currentR; + uint8_t currentG; + uint8_t currentB; + RGB(); - void setColorRGB(uint8_t red, uint8_t green, uint8_t blue); + void setColorRGB(uint8_t red, uint8_t green, uint8_t blue, bool saveColor); void fadeColor(uint8_t red, uint8_t green, uint8_t blue, int duration); - void blinkColor(uint8_t red, uint8_t green, uint8_t blue, int duration); - void breatheColor(uint8_t red, uint8_t green, uint8_t blue, int duration); + void blink(int cycles, int interval); + void breathe(int duration, int interval); void rainbowCycle(int duration); private: int scaleColor(uint8_t val); }; - diff --git a/src/main.cpp b/src/main.cpp index 453eb0b..34d760c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,16 +1,17 @@ #include -#include "oled.h" +// #include "oled.h" #include "rgb.h" + +// OLED oled; +RGB rgb; + void setup() { + // oled_init(); //oled_print("siema", 0, 10); // oled_drawBox(); - - - // pinMode(RED_PIN, OUTPUT); - // pinMode(GREEN_PIN, OUTPUT); - // pinMode(BLUE_PIN, OUTPUT); + // pinMode(IN1, OUTPUT); // pinMode(IN2, OUTPUT); @@ -21,61 +22,6 @@ void setup() { void loop() { - // setColorRGB(255, 0, 0); // Red - // oled_print(" red", 0, 10); - // delay(2500); - // oled_clear(); - // setColorRGB(0, 255, 0); // Green - // oled_print(" green", 0, 10); - // delay(2500); - // oled_clear(); - // setColorRGB(0, 0, 255); // Blue - // oled_print(" blue", 0, 10); - // delay(2500); - // oled_clear(); - // setColorRGB(255, 255, 0); // Yellow - // oled_print(" yellow", 0, 10); - // delay(2500); - // oled_clear(); - // setColorRGB(0, 255, 255); // Cyan - // oled_print(" cyan", 0, 10); - // delay(2500); - // oled_clear(); - // setColorRGB(255, 0, 255); // Magenta - // oled_print(" magenta", 0, 10); - // delay(2500); - // oled_clear(); - // setColorRGB(255, 255, 255); // White - // oled_print(" white", 0, 10); - // delay(2500); - // oled_clear(); - // setColorRGB(0, 0, 0); // Off - // oled_print(" off", 0, 10); - // delay(2500); - // oled_clear(); - // digitalWrite(RED_PIN, HIGH); - // digitalWrite(GREEN_PIN, LOW); - // digitalWrite(BLUE_PIN, LOW); - // digitalWrite(IN1, HIGH); - // digitalWrite(IN2, LOW); - // digitalWrite(IN3, HIGH); - // digitalWrite(IN4, LOW); - // delay(2000); - // digitalWrite(RED_PIN, LOW); - // digitalWrite(GREEN_PIN, HIGH); - // digitalWrite(BLUE_PIN, LOW); - // digitalWrite(IN1, LOW); - // digitalWrite(IN2, HIGH); - // digitalWrite(IN3, LOW); - // digitalWrite(IN4, HIGH); - // delay(2000); - // digitalWrite(RED_PIN, LOW); - // digitalWrite(GREEN_PIN, LOW); - // digitalWrite(BLUE_PIN, HIGH); - // digitalWrite(IN1, LOW); - // digitalWrite(IN2, HIGH); - // digitalWrite(IN3, HIGH); - // digitalWrite(IN4, LOW); - // delay(2000); + } -- cgit v1.2.3