-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoutineExpand.cpp
129 lines (108 loc) · 2.94 KB
/
RoutineExpand.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
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
#ifdef GEOM_DOME
#include <algorithm>
#include <cmath>
#include "RoutineExpand.h"
#include "Dome.h"
#include "DomeMappings.h"
#include "ColorPallete.h"
CMemoryPool<CRoutineExpand, CRoutineExpand::c_alloc_qty> CRoutineExpand::s_pool;
CRoutineExpand::CRoutineExpand(CPixelArray* pixels) :
CRoutine(pixels)
{
m_color_index = rand() % ColorPallete::Qty;
}
CRoutineExpand::~CRoutineExpand()
{
}
CDome* CRoutineExpand::GetDome()
{
return reinterpret_cast<CDome*>(GetPixels());
}
void CRoutineExpand::Advance()
{
size_t now = millis();
if(m_shape_group == All && now - m_last_run >= c_delay_ms)
{
m_shape_group = InnerHex;
m_last_run = now;
}
else if(m_shape_group != All && now - m_last_run >= c_group_ms)
{
m_shape_group = static_cast<ShapeGroup>(static_cast<int>(m_shape_group) + 1);
m_last_run = now;
if(m_shape_group == All)
{
m_color_index++;
m_color_index %= ColorPallete::Qty;
}
}
}
float CRoutineExpand::CalculateBrightness()
{
size_t now = millis();
float brightness = 1.0;
float time_ms = now - m_last_run;
if(m_shape_group == All)
{
if(time_ms < c_group_ms / 2)
{
brightness = powf((float)(now - m_last_run) / (c_group_ms / 2), c_q);
brightness = std::min<float>(brightness, 1.0);
}
}
else
{
if(time_ms < c_group_ms / 2)
{
brightness = powf((float)(now - m_last_run) / (c_group_ms / 2), c_q);
brightness = std::min<float>(brightness, 1.0);
}
else if(time_ms < c_group_ms)
{
brightness = 1.0 - powf((float)(now - m_last_run - c_group_ms / 2) / (c_group_ms / 2), c_q);
brightness = std::max<float>(brightness, 0.0);
}
}
}
bool CRoutineExpand::isLit(size_t shape_index)
{
switch(m_shape_group)
{
case InnerHex:
{
bool inner = false;
return DomeMappings::ShapeIsHex(shape_index, inner) && inner;
}
case OuterHex:
{
bool inner = false;
return DomeMappings::ShapeIsHex(shape_index, inner) && !inner;
}
case Other:
{
bool inner = false;
return DomeMappings::ShapeIsHex(shape_index, inner) == false;
}
default:
return true;
}
}
void CRoutineExpand::Continue()
{
SetAllPixels(CRGB::Black);
Advance();
CHSV color = rgb2hsv_approximate(ColorPallete::s_colors[m_color_index]);
color.v *= CalculateBrightness();
for(size_t i=0;i<DomeMappings::c_num_shapes;i++)
{
CPixelArrayLegs* shape = GetDome()->GetShape(i);
if(isLit(i) == true)
{
for(size_t index=0;index<shape->GetSize();index++)
{
SetPixel(index + shape->GetOffset(), color);
}
}
}
}
#endif // GEOM_DOME