2024-10-14 22:09:44 +02:00
|
|
|
#include "SimpLedRGB.h"
|
2024-10-14 12:04:46 +02:00
|
|
|
|
2024-10-22 14:56:27 +02:00
|
|
|
SimpLedRGB::SimpLedRGB(int pinRed, int pinGreen, int pinBlue)
|
|
|
|
{
|
2024-10-14 12:04:46 +02:00
|
|
|
this->pin_red = pinRed;
|
|
|
|
this->pin_green = pinGreen;
|
|
|
|
this->pin_blue = pinBlue;
|
|
|
|
}
|
|
|
|
|
2024-10-14 22:09:44 +02:00
|
|
|
void SimpLedRGB::begin()
|
2024-10-14 12:04:46 +02:00
|
|
|
{
|
|
|
|
pinMode(this->pin_red, OUTPUT);
|
|
|
|
pinMode(this->pin_green, OUTPUT);
|
|
|
|
pinMode(this->pin_blue, OUTPUT);
|
|
|
|
|
|
|
|
this->setColor(color);
|
|
|
|
}
|
|
|
|
|
2024-10-14 22:09:44 +02:00
|
|
|
void SimpLedRGB::setColor(const Color &color)
|
2024-10-14 12:04:46 +02:00
|
|
|
{
|
|
|
|
this->color.red = color.red;
|
|
|
|
this->color.green = color.green;
|
|
|
|
this->color.blue = color.blue;
|
|
|
|
|
|
|
|
analogWrite(this->pin_red, 255 * color.red);
|
|
|
|
analogWrite(this->pin_green, 255 * color.green);
|
|
|
|
analogWrite(this->pin_blue, 255 * color.blue);
|
|
|
|
}
|
|
|
|
|
2024-10-14 22:09:44 +02:00
|
|
|
void SimpLedRGB::translateToColor(const Color &color, int duration_millis)
|
2024-10-14 12:04:46 +02:00
|
|
|
{
|
|
|
|
const Color colorStart = {
|
|
|
|
this->color.red,
|
|
|
|
this->color.green,
|
|
|
|
this->color.blue
|
|
|
|
};
|
|
|
|
|
|
|
|
const Color colorDelta = {
|
|
|
|
color.red - this->color.red,
|
|
|
|
color.green - this->color.green,
|
|
|
|
color.blue - this->color.blue
|
|
|
|
};
|
|
|
|
|
|
|
|
long t_start = millis();
|
|
|
|
long t_end = t_start + duration_millis;
|
|
|
|
|
|
|
|
while (millis() < t_end) {
|
|
|
|
float progress = (float)(millis() - t_start) / (float)duration_millis;
|
|
|
|
|
|
|
|
const Color colorCurrent = {
|
|
|
|
colorStart.red + colorDelta.red * progress,
|
|
|
|
colorStart.green + colorDelta.green * progress,
|
|
|
|
colorStart.blue + colorDelta.blue * progress
|
|
|
|
};
|
|
|
|
|
|
|
|
setColor(colorCurrent);
|
|
|
|
}
|
|
|
|
|
|
|
|
setColor(color);
|
|
|
|
}
|
|
|
|
|
2024-10-19 00:24:35 +02:00
|
|
|
void SimpLedRGB::rainbowCycle(int cycleDurationMillis)
|
|
|
|
{
|
|
|
|
setColor(COLOR_RED);
|
|
|
|
delay(cycleDurationMillis);
|
|
|
|
translateToColor(COLOR_YELLOW, cycleDurationMillis);
|
|
|
|
delay(cycleDurationMillis);
|
|
|
|
translateToColor(COLOR_GREEN, cycleDurationMillis);
|
|
|
|
delay(cycleDurationMillis);
|
|
|
|
translateToColor(COLOR_CYAN, cycleDurationMillis);
|
|
|
|
delay(cycleDurationMillis);
|
|
|
|
translateToColor(COLOR_BLUE, cycleDurationMillis);
|
|
|
|
delay(cycleDurationMillis);
|
|
|
|
translateToColor(COLOR_VIOLET, cycleDurationMillis);
|
|
|
|
delay(cycleDurationMillis);
|
|
|
|
translateToColor(COLOR_RED, cycleDurationMillis);
|
|
|
|
}
|
|
|
|
|
2024-11-20 23:50:15 +01:00
|
|
|
void SimpLedRGB::candle(const Color &color, float wind)
|
2024-10-19 00:24:35 +02:00
|
|
|
{
|
2024-10-22 14:56:27 +02:00
|
|
|
int duration = random(250, 6000 - 5000 * wind);
|
|
|
|
float intensity = (float)random(100 + 400 * wind, 300 + 600 * wind) / 1000.0;
|
|
|
|
|
|
|
|
unsigned long start = millis();
|
|
|
|
unsigned long finish = start + duration;
|
|
|
|
|
|
|
|
float progress = 0.0;
|
|
|
|
|
|
|
|
while (progress < 1.0) {
|
|
|
|
progress = (float)(millis() - start) / (float)duration;
|
|
|
|
|
|
|
|
float brightness = (1.0 - intensity) + (1.0 + cos(PI * 2 * progress)) * 0.5 * intensity;
|
|
|
|
|
|
|
|
setColor(
|
|
|
|
{
|
|
|
|
color.red * brightness,
|
|
|
|
color.green * brightness,
|
|
|
|
color.blue * brightness,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2024-10-19 00:24:35 +02:00
|
|
|
}
|
|
|
|
|
2024-11-20 23:50:15 +01:00
|
|
|
void SimpLedRGB::pulse(const Color &color, float timesPerSecond, float amplitudeMin, float amplitudeMax)
|
2024-10-19 00:24:35 +02:00
|
|
|
{
|
|
|
|
float amplitude = (1.0 + sin(PULSE_PER_SECOND * millis() * timesPerSecond)) / 2.0;
|
|
|
|
float brightness = amplitudeMin + (amplitudeMax - amplitudeMin) * amplitude;
|
|
|
|
|
|
|
|
setColor({
|
|
|
|
color.red * brightness,
|
|
|
|
color.green * brightness,
|
|
|
|
color.blue * brightness
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|