-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoutineCyclePallete.cpp
95 lines (81 loc) · 2.5 KB
/
RoutineCyclePallete.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <algorithm>
#include "RoutineCyclePallete.h"
#include "PixelArray.h"
#include "FastLED.h"
#include "Arduino.h"
#include "Logging.h"
#include "Math.h"
#include "ColorPallete.h"
CMemoryPool<CRoutineCyclePallete, CRoutineCyclePallete::c_alloc_qty> CRoutineCyclePallete::s_pool;
CRoutineCyclePallete::CRoutineCyclePallete(CPixelArray* pixels,
bool forward,
size_t period_sec,
bool dimensional,
size_t num_colors) :
CRoutine(pixels),
m_base_color(rand() % ColorPallete::Qty),
m_forward(forward),
m_period_sec(period_sec),
m_dimensional(dimensional),
m_num_colors(num_colors)
{
m_last_run = millis();
if(m_num_colors > ColorPallete::Qty)
{
char logstr[256];
sprintf(logstr, "CRoutineCyclePallete::CRoutineCyclePallete: num_colors [%u] "
"is > max value of [%u]. Exiting...", m_num_colors, ColorPallete::Qty);
CLogging::log(logstr);
}
}
CRoutineCyclePallete::~CRoutineCyclePallete()
{
}
void CRoutineCyclePallete::Continue()
{
size_t now = millis();
float move_by = (float)(now - m_last_run) / (m_period_sec * 1000);
if(m_forward == true)
{
m_midpoint += move_by;
if(m_midpoint > 1.0)
{
m_midpoint -= 1.0;
}
}
else
{
m_midpoint -= move_by;
if(m_midpoint < 0.0)
{
m_midpoint += 1.0;
}
}
m_last_run = now;
for(size_t i=0;i<GetSize();i++)
{
float this_index;
if(m_dimensional)
{
this_index = (GetCoordinate(i).x + 1.00) / 2.00;
}
else
{
this_index = (float)i / GetSize();
}
CRGB color;
for(size_t color_index=0;color_index<ColorPallete::Qty;color_index++)
{
float color_loc = (float)color_index / ColorPallete::Qty + m_midpoint;
size_t raw_index = ((color_index % m_num_colors) + m_base_color) % ColorPallete::Qty;
CRGB blend = ColorPallete::s_colors[raw_index];
float distance = fabs(this_index - color_loc);
float weight = fabs(1.0 - distance);
weight = fabs(weight - 0.50) * 2;
weight *= weight;
weight *= weight;
color = CPixelArray::Blend(color, blend, weight);
}
SetPixel(i, color);
}
}