summaryrefslogtreecommitdiff
path: root/firmware/lib/rgb/rgb.cpp
blob: 6dc3834dbd7f3d2394a3ad704f7e6dcc5f0293f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <Arduino.h>
#include "settings.h"
#include "rgb.h"
#include <math.h>

#define RED_CHANNEL 0
#define GREEN_CHANNEL 1
#define BLUE_CHANNEL 2

#define PWM_FREQ 5000
#define PWM_RES 10

int RGB::scaleColor(uint8_t val)
{
  return map(val, 0, 255, 0, (1 << PWM_RES) - 1);
}
RGB::RGB()
    : currentR(0), currentG(0), currentB(0)
{
}

void RGB::init()
{
  ledcSetup(RED_CHANNEL, PWM_FREQ, PWM_RES);
  ledcAttachPin(RED_PIN, RED_CHANNEL);

  ledcSetup(GREEN_CHANNEL, PWM_FREQ, PWM_RES);
  ledcAttachPin(GREEN_PIN, GREEN_CHANNEL);

  ledcSetup(BLUE_CHANNEL, PWM_FREQ, PWM_RES);
  ledcAttachPin(BLUE_PIN, BLUE_CHANNEL);
}

void RGB::setColorRGB(uint8_t red, uint8_t green, uint8_t blue, bool saveColor = true)
{
  ledcWrite(RED_CHANNEL, scaleColor(red));
  ledcWrite(GREEN_CHANNEL, scaleColor(green));
  ledcWrite(BLUE_CHANNEL, scaleColor(blue));

  if (saveColor)
  {
    currentR = red;
    currentG = green;
    currentB = blue;
  }
}

void RGB::fadeColor(uint8_t red, uint8_t green, uint8_t blue, int duration)
{
  int startR = currentR;
  int startG = currentG;
  int startB = currentB;

  for (int i = 0; i <= duration; i++)
  {
    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);
  }

  setColorRGB(red, green, blue);
}

void RGB::blink(int cycles, int interval)
{
  for (int i = 0; i < cycles; i++)
  {
    setColorRGB(0, 0, 0, false);
    delay(interval);
    setColorRGB(currentR, currentG, currentB, false);
    delay(interval);
  }
}

void RGB::breathe(int cycles, int period)
{
  const int steps = 128;
  int stepDelay = period / (steps * 2);

  uint8_t baseR = currentR;
  uint8_t baseG = currentG;
  uint8_t baseB = currentB;

  for (int c = 0; c < cycles; ++c)
  {
    for (int i = 0; i <= steps; ++i)
    {
      float scale = (float)i / steps;
      setColorRGB(baseR * scale, baseG * scale, baseB * scale, false);
      delay(stepDelay);
    }

    for (int i = steps; i >= 0; --i)
    {
      float scale = (float)i / steps;
      setColorRGB(baseR * scale, baseG * scale, baseB * scale, false);
      delay(stepDelay);
    }
  }

  setColorRGB(baseR, baseG, baseB);
}

void RGB::rainbowCycle(int duration, float speed)
{
  // speed (0.1 – slow, 1.0 – normal, 2.0 – fast)
  const int steps = 256;

  for (int i = 0; i < steps; i++)
  {
    float angle = i * speed * 0.024;
    int red = sin(angle) * 127 + 128;
    int green = sin(angle + 2) * 127 + 128;
    int blue = sin(angle + 4) * 127 + 128;

    setColorRGB(red, green, blue, false);
    delay(duration / steps);
  }

  setColorRGB(currentR, currentG, currentB, false);
}