-
Notifications
You must be signed in to change notification settings - Fork 1
/
rgb.cpp
197 lines (167 loc) · 5.43 KB
/
rgb.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* Arduino Includes */
#if (PIXEL_TYPE == NEOPIXELS)
#include <Adafruit_NeoPixel.h>
#endif
/* ADL Includes */
#include "raat.h"
#include "binary-output.h"
#include "raat-debouncer.h"
#include "raat-oneshot-timer.h"
#include "raat-oneshot-task.h"
#include "raat-task.h"
#include "rgb-param.h"
/* Application Includes */
#include "application.h"
#include "rgb.h"
#include "nonlinear.h"
/* Defines, typedefs, constants */
static const uint32_t FADER_MAX = 250;
static const uint32_t FADER_MIN = 20;
/* Local Objects and Variables */
static PixelType * s_pFixed;
static PixelType * s_pVariable;
static uint8_t const * s_pVariableLevels;
static bool s_nonlinear = false;
static RGBParam ** s_pRGBFixed;
static RGBParam *s_pRGBFinish;
static uint32_t s_multiplier = 0;
static uint8_t s_nsteps = 0;
static uint16_t s_brightness_table[8] = {0};
static uint32_t s_fader = FADER_MIN;
static bool s_fade_up = true;
static void update_fader()
{
if (s_fade_up)
{
incrementwithlimit(s_fader, FADER_MAX);
incrementwithlimit(s_fader, FADER_MAX);
s_fade_up = (s_fader < FADER_MAX);
}
else
{
decrementwithlimit(s_fader, FADER_MIN);
decrementwithlimit(s_fader, FADER_MIN);
s_fade_up = (s_fader == FADER_MIN);
}
}
static void update_fixed(RGBParam * pRGBFixed[5], uint16_t * p_brightness_table)
{
for (uint8_t i=0; i<5; i++)
{
if (!app_get_rgb_matched(i))
{
s_pFixed->setPixelColor(
i,
p_brightness_table[pRGBFixed[i]->get(eR)] / PIXEL_DIVIDER,
p_brightness_table[pRGBFixed[i]->get(eG)] / PIXEL_DIVIDER,
p_brightness_table[pRGBFixed[i]->get(eB)] / PIXEL_DIVIDER
);
}
else
{
s_pFixed->setPixelColor(
i,
(s_pRGBFinish->get(eR) * s_fader) / (FADER_MAX * PIXEL_DIVIDER),
(s_pRGBFinish->get(eG) * s_fader) / (FADER_MAX * PIXEL_DIVIDER),
(s_pRGBFinish->get(eB) * s_fader) / (FADER_MAX * PIXEL_DIVIDER)
);
}
}
s_pFixed->show();
}
static void update_variable(uint8_t const * const pVariableLevels, uint16_t * p_brightness_table)
{
for (uint8_t i=0; i<5; i++)
{
if (!app_get_rgb_matched(i))
{
s_pVariable->setPixelColor(
i,
p_brightness_table[pVariableLevels[(i*3)+0]] / PIXEL_DIVIDER,
p_brightness_table[pVariableLevels[(i*3)+1]] / PIXEL_DIVIDER,
p_brightness_table[pVariableLevels[(i*3)+2]] / PIXEL_DIVIDER
);
}
else
{
s_pVariable->setPixelColor(
i,
(s_pRGBFinish->get(eR) * s_fader) / (FADER_MAX * PIXEL_DIVIDER),
(s_pRGBFinish->get(eG) * s_fader) / (FADER_MAX * PIXEL_DIVIDER),
(s_pRGBFinish->get(eB) * s_fader) / (FADER_MAX * PIXEL_DIVIDER)
);
}
}
s_pVariable->show();
}
static void debug_task_fn(RAATTask& pThisTask, void * pTaskData)
{
(void)pThisTask;
(void)pTaskData;
raat_logln(LOG_RGB, "Levels:");
for (uint8_t i=0; i<5; i++)
{
raat_logln(LOG_RGB, "F: %d,%d,%d, V: %d,%d,%d",
s_pRGBFixed[i]->get(eR), s_pRGBFixed[i]->get(eG), s_pRGBFixed[i]->get(eB),
s_pVariableLevels[(i*3)+0], s_pVariableLevels[(i*3)+1], s_pVariableLevels[(i*3)+2]
);
}
}
static RAATTask debug_task(2000, debug_task_fn, NULL);
static void update_task_fn(RAATTask& pThisTask, void * pTaskData)
{
(void)pThisTask;
(void)pTaskData;
update_fixed(s_pRGBFixed, s_brightness_table);
update_variable(s_pVariableLevels, s_brightness_table);
update_fader();
}
static RAATTask update_task(10, update_task_fn, NULL);
static void update_brightness_table(uint16_t * p_table, uint32_t multiplier, bool nonlinear_brightness, uint8_t nsteps)
{
if (nonlinear_brightness)
{
update_nonlinear_table(p_table, multiplier, nsteps);
}
else
{
for (uint8_t i = 0; i<nsteps; i++)
{
p_table[i] = multiplier*i;
}
}
}
static bool brightness_table_requires_update(uint32_t multiplier, bool nonlinear_brightness, uint8_t nsteps)
{
return (s_multiplier != multiplier) || (s_nonlinear != nonlinear_brightness) || (s_nsteps != nsteps);
}
/* Public Functions */
void rgb_setup(PixelType * pFixed, PixelType * pVariable, RGBParam * pRGBFinish, uint32_t multiplier, bool nonlinear_brightness, uint8_t nsteps)
{
s_pFixed = pFixed;
s_pVariable = pVariable;
s_pRGBFinish = pRGBFinish;
s_multiplier = multiplier;
s_nonlinear = nonlinear_brightness;
update_brightness_table(s_brightness_table, s_multiplier, nonlinear_brightness, nsteps);
}
void rgb_tick(RGBParam * pRGBFixed[5], uint8_t const * const pVariableLevels, uint32_t multiplier, bool nonlinear_brightness, uint8_t nsteps)
{
s_pRGBFixed = pRGBFixed;
s_pVariableLevels = pVariableLevels;
if (brightness_table_requires_update(multiplier, nonlinear_brightness, nsteps))
{
s_multiplier = multiplier;
s_nonlinear = nonlinear_brightness;
s_nsteps = nsteps;
update_brightness_table(s_brightness_table, s_multiplier, nonlinear_brightness, nsteps);
Serial.println("New table:");
for (uint8_t i = 0; i < nsteps; i++)
{
Serial.print(s_brightness_table[i]);
Serial.print(", ");
}
}
debug_task.run();
update_task.run();
}