-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnilloLed.cpp
More file actions
167 lines (152 loc) · 4.37 KB
/
Copy pathAnilloLed.cpp
File metadata and controls
167 lines (152 loc) · 4.37 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "AnilloLed.h"
AnilloLed::AnilloLed(int brilloInicial)
{
brillo = brilloInicial;
FastLED.addLeds<WS2812B, DATA_PIN_LEDS>(leds, NUM_LEDS);
luzCentral = new LuzCentral(brilloInicial);
ledActual = 0;
millisInicioAnimacion = 0;
brilloAntesAnimacion = 0;
numeroFades = 0;
numeroTotalFades = 5; // Valor por defecto
numeroGiros = 0;
numeroTotalGiros = 1; // Valor por defecto
etapaAnimandoConFade = false;
estado = EstadoEjecucion::On;
CambiarBrillo(brillo);
}
bool AnilloLed::LanzarAnimacion(int numFades, int numGiros)
{
numeroTotalFades = numFades;
numeroTotalGiros = numGiros;
brilloAntesAnimacion = brillo; // Se almacena el brillo antes de modificarlo en la animación
numeroFades = 0;
numeroGiros = 0;
etapaAnimandoConFade = true;
fadeDescendente = true;
fadeInicialDescendente = true;
millisInicioAnimacion = millis();
ledActual = 0;
estado = EstadoEjecucion::Animando;
}
bool AnilloLed::Actualizar()
{
switch(estado)
{
case EstadoEjecucion::Animando:
// Etapa Fades
if (numeroFades < numeroTotalFades)
{
// Si quedan fades por hacer ...
if (fadeDescendente)
{
brillo = brillo - DELTA_FADE;
if (brillo <= MIN_BRILLO)
{
// Final del fade descendente
brillo = MIN_BRILLO;
fadeDescendente = false; // Ahora será ascendente
if (fadeInicialDescendente == false)
{
// Ciclo completo, se incrementa el número de fades
numeroFades = numeroFades + 1;
}
}
}
else
{
brillo = brillo + DELTA_FADE;
if (brillo >= MAX_BRILLO)
{
// Final del fade ascendente
brillo = MAX_BRILLO;
fadeDescendente = true; // Ahora será descendente
if (fadeInicialDescendente == true)
{
// Ciclo completo, se incrementa el número de fades
numeroFades = numeroFades + 1;
}
}
}
CambiarBrilloAnimacion(brillo); // Se cambia el brillo
}
else
{
// Etapa Giros
if (numeroGiros < numeroTotalGiros)
{
int millisActuales = millis();
if (millisActuales - millisInicioAnimacion > MILLIS_POR_LED)
{
// Paso al siguiente led
if (ledActual == NUM_LEDS - 1)
{
// Fin del giro
numeroGiros = numeroGiros + 1;
ledActual = 0; // Se reinicia
if(numeroGiros < numeroTotalGiros) // En el último caso no se apaga para luego encender; genera un parpadeo feo
luzCentral->Apagar();
}
else
{
ledActual = ledActual + 1;
if(NUM_LEDS / 2 == ledActual )
{
luzCentral->Encender();
}
}
EncenderSoloUnLed(ledActual);
}
}
else
{
// Fin de la animacion, se vuelve a encendido
//AnilloLed::CambiarBrilloAnimacion(brilloAntesAnimacion);
Encender();
}
}
break;
}
}
void AnilloLed::EncenderSoloUnLed(int numLed)
{
for (int cont=0;cont<NUM_LEDS;cont++)
{
if (cont == numLed)
leds[cont] = CRGB::Blue;
else
leds[cont] = CRGB::Black;
}
}
void AnilloLed::Apagar()
{
estado = EstadoEjecucion::Off;
// Leds en azul
for (int cont=0;cont<NUM_LEDS;cont++)
leds[cont] = CRGB::Black;
luzCentral->Apagar();
}
void AnilloLed::Encender()
{
estado = EstadoEjecucion::On;
// Leds en azul
for (int cont=0;cont<NUM_LEDS;cont++)
leds[cont] = CRGB::Blue;
luzCentral->Encender();
}
void AnilloLed::CambiarBrillo(int nuevoBrillo)
{
// Sólo si el estado es on u off
if (estado == EstadoEjecucion::On || estado == EstadoEjecucion::Off)
CambiarBrilloAnimacion(nuevoBrillo);
}
void AnilloLed::CambiarBrilloAnimacion(int nuevoBrillo)
{
//Serial.print("Cambiar brillo ");
//Serial.println(nuevoBrillo);
FastLED.setBrightness(nuevoBrillo);
}
EstadoEjecucion AnilloLed::ObtenerEstado()
{
return estado;
}