This commit is contained in:
Mal 2024-10-14 12:04:46 +02:00
commit 460a396a21
2 changed files with 90 additions and 0 deletions

60
SimpleLed.cpp Normal file
View File

@ -0,0 +1,60 @@
#include "SimpleLed.h"
SimpleLed::SimpleLed(int pinRed, int pinGreen, int pinBlue) {
this->pin_red = pinRed;
this->pin_green = pinGreen;
this->pin_blue = pinBlue;
}
void SimpleLed::begin()
{
pinMode(this->pin_red, OUTPUT);
pinMode(this->pin_green, OUTPUT);
pinMode(this->pin_blue, OUTPUT);
this->setColor(color);
}
void SimpleLed::setColor(const Color &color)
{
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);
}
void SimpleLed::translateToColor(const Color &color, int duration_millis)
{
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);
}

30
SimpleLed.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef SimpleLed_h
#define SimpleLed_h
#include "Arduino.h"
typedef struct {
float red;
float green;
float blue;
} Color;
class SimpleLed
{
int pin_red = 0;
int pin_green = 0;
int pin_blue = 0;
Color color = {0.0, 0.0, 0.0};
public:
SimpleLed(int pinRed, int pinGreen, int pinBlue);
void begin();
void setColor(const Color &color);
void translateToColor(const Color &color, int duration_millis);
};
#endif