-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLedManager.c
176 lines (140 loc) · 4.97 KB
/
LedManager.c
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
/*
* LedManager.c
*
* Created on: 28.09.2011
* Project: Lightpack
*
* Copyright (c) 2011 Mike Shatohin, mikeshatohin [at] gmail.com
*
* Lightpack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Lightpack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "Lightpack.h"
#include "LedDriver.h"
void LedManager_FillImages(const uint8_t red, const uint8_t green, const uint8_t blue)
{
for (uint8_t i = 0; i < LEDS_COUNT; i++)
{
g_Images.start[i].r = red;
g_Images.start[i].g = green;
g_Images.start[i].b = blue;
g_Images.current[i].r = red;
g_Images.current[i].g = green;
g_Images.current[i].b = blue;
g_Images.end[i].r = red;
g_Images.end[i].g = green;
g_Images.end[i].b = blue;
}
}
#if (LIGHTPACK_HW >= 6)
void EvalCurrentImage_SmoothlyAlg(void)
{
for (uint8_t i = 0; i < LEDS_COUNT; i++)
{
if (g_Images.smoothIndex[i] >= g_Settings.smoothSlowdown)
{
// Smooth change colors complete, rewrite start image
g_Images.current[i].r = g_Images.start[i].r = g_Images.end[i].r;
g_Images.current[i].g = g_Images.start[i].g = g_Images.end[i].g;
g_Images.current[i].b = g_Images.start[i].b = g_Images.end[i].b;
} else {
uint32_t coefEnd = ((uint32_t)g_Images.smoothIndex[i] << 16) / g_Settings.smoothSlowdown;
uint32_t coefStart = (1UL << 16) - coefEnd;
g_Images.current[i].r = (
coefStart * g_Images.start[i].r +
coefEnd * g_Images.end [i].r) >> 16;
g_Images.current[i].g = (
coefStart * g_Images.start[i].g +
coefEnd * g_Images.end [i].g) >> 16;
g_Images.current[i].b = (
coefStart * g_Images.start[i].b +
coefEnd * g_Images.end [i].b) >> 16;
g_Images.smoothIndex[i]++;
}
}
}
void LedManager_UpdateColors(void)
{
EvalCurrentImage_SmoothlyAlg();
if (g_Settings.isSmoothEnabled)
LedDriver_Update(g_Images.current);
else
LedDriver_Update(g_Images.end);
}
#elif (LIGHTPACK_HW == 5 || LIGHTPACK_HW == 4)
static inline void _StartConstantTime(void)
{
TCNT1 = 0;
}
static inline void _EndConstantTime(const uint8_t time)
{
while(TCNT1 < time * 256UL) { }
}
void EvalCurrentImage_SmoothlyAlg(void)
{
for (uint8_t i = 0; i < LEDS_COUNT; i++)
{
if (g_Images.smoothIndex[i] >= g_Settings.smoothSlowdown)
{
// Smooth change colors complete, rewrite start image
g_Images.current[i].r = g_Images.start[i].r = g_Images.end[i].r;
g_Images.current[i].g = g_Images.start[i].g = g_Images.end[i].g;
g_Images.current[i].b = g_Images.start[i].b = g_Images.end[i].b;
} else {
uint16_t coefEnd = ((uint16_t)g_Images.smoothIndex[i] << 8) / g_Settings.smoothSlowdown;
uint16_t coefStart = (1UL << 8) - coefEnd;
g_Images.current[i].r = (
coefStart * g_Images.start[i].r +
coefEnd * g_Images.end [i].r) >> 8;
g_Images.current[i].g = (
coefStart * g_Images.start[i].g +
coefEnd * g_Images.end [i].g) >> 8;
g_Images.current[i].b = (
coefStart * g_Images.start[i].b +
coefEnd * g_Images.end [i].b) >> 8;
g_Images.smoothIndex[i]++;
}
}
}
static inline void _PulseWidthModulation(void)
{
static uint8_t s_pwmIndex = 0; // index of current PWM level
if (s_pwmIndex == g_Settings.maxPwmValue)
{
s_pwmIndex = 0;
_StartConstantTime();
// Switch OFF LEDs on time sets in g_Settings.brightness
LedDriver_OffLeds();
// Also eval current image
if (g_Settings.isSmoothEnabled)
{
EvalCurrentImage_SmoothlyAlg();
}
_EndConstantTime(g_Settings.brightness);
}
if (g_Settings.isSmoothEnabled)
{
LedDriver_UpdatePWM(g_Images.current, s_pwmIndex);
} else {
LedDriver_UpdatePWM(g_Images.end, s_pwmIndex);
}
s_pwmIndex++;
// Clear timer counter
TCNT1 = 0x0000;
}
void LedManager_UpdateColors(void)
{
_PulseWidthModulation();
}
#endif