-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGBLed.cpp
75 lines (65 loc) · 1.29 KB
/
RGBLed.cpp
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
#include "RGBLed.h"
static RGBLed::tColor kColors[RGBLed::kLastColor] =
{
{0xFF, 0x00, 0x00},
{0x00, 0xFF, 0x00},
{0x00, 0x00, 0xFF},
{0xFF, 0xFF, 0x00},
{0xFF, 0x7F, 0x00},
};
RGBLed::RGBLed(int _red_pin, int _green_pin, int _blue_pin):
red_pin(_red_pin), green_pin(_green_pin), blue_pin(_blue_pin),
mode(OFF), power(0.0f)
{
pinMode(red_pin, OUTPUT);
pinMode(green_pin, OUTPUT);
pinMode(blue_pin, OUTPUT);
}
RGBLed::~RGBLed()
{}
void RGBLed::setColor(eColor c, eMode m)
{
color = kColors[c];
mode = m;
update();
}
void RGBLed::update()
{
bool need_change = false;
if (mode == ON && power != 1.0)
{
power = 1.0;
need_change = true;
}
if (mode == OFF && power != 0.0)
{
power = 0.0;
need_change = true;
}
analogWrite(red_pin, color.red * power);
analogWrite(green_pin, color.green * power);
analogWrite(blue_pin, color.blue * power);
}
void RGBLed::testLed()
{
for(size_t i = 0; i < kLastColor; ++i)
{
setColor((eColor)i, ON);
delay(500);
}
}
RGBLed::state::state(RGBLed* _led, RGBLed::eColor _color, RGBLed::eMode _mode)
{
led = _led;
last_color = led->color;
last_mode = led->mode;
last_power = led->power;
led->setColor(_color, _mode);
}
RGBLed::state::~state()
{
led->color = last_color;
led->mode = last_mode;
led->power = last_power;
led->update();
}