forked from grblHAL/core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rgb.h
229 lines (183 loc) · 6.18 KB
/
rgb.h
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
rgb.h - typedefs, API structure and helper functions for RGB lights and LED strips
Part of grblHAL
Copyright (c) 2024 Terje Io
grblHAL 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 3 of the License, or
(at your option) any later version.
grblHAL 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 grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
typedef union {
uint8_t value;
uint8_t mask;
struct {
uint8_t B :1,
G :1,
R :1,
W :1,
unused :4;
};
} rgb_color_mask_t;
typedef union {
uint32_t value;
struct {
uint8_t B; //!< Blue
uint8_t G; //!< Green
uint8_t R; //!< Red
uint8_t W; //!< White
};
} rgb_color_t;
/*! \brief Pointer to function for setting RGB (LED) output.
\param color a \a rgb_color_t union.
*/
typedef void (*rgb_set_color_ptr)(uint16_t device, rgb_color_t color);
/*! \brief Pointer to function for setting RGB (LED) output, with mask for which LEDs to change.
\param color a \a rgb_color_t union.
\param mask a \a rgb_color_mask_t union.
*/
typedef void (*rgb_set_color_masked_ptr)(uint16_t device, rgb_color_t color, rgb_color_mask_t mask);
/*! \brief Pointer to function for setting RGB (LED) intensity.
\param intensity in the range 0 - 255.
\returns previuous intensity.
*/
typedef uint8_t (*rgb_set_intensity_ptr)(uint8_t intensity);
/*! \brief Pointer to function for outputting RGB (LED) data to Neopixel strip.
*/
typedef void (*rgb_write_ptr)(void);
typedef struct {
rgb_set_color_ptr out; //!< Optional handler for setting device (LED) color.
rgb_set_color_masked_ptr out_masked; //!< Optional handler for setting device (LED) color, with mask for which LEDs to change.
rgb_write_ptr write; //!< Optional handler for outputting data to Neopixel strip.
rgb_set_intensity_ptr set_intensity; //!< Optional handler for setting intensity, range 0 - 255.
rgb_color_t cap; //!< Driver capability, color value: 0 - not available, 1 - on off, > 1 - intensity range 0 - n.
uint16_t num_devices; //!< Number of devices (LEDs) available.
} rgb_ptr_t;
// helper structure and functions, not used by the core
typedef struct {
uint16_t num_leds;
uint16_t num_bytes;
uint8_t *leds;
uint8_t intensity;
} neopixel_cfg_t;
static inline bool rgb_is_neopixels (rgb_ptr_t *device)
{
return device->out != NULL && device->cap.R > 126 && device->cap.G > 126 && device->cap.B > 126;
}
static inline bool rgb_is_onoff (rgb_ptr_t *device)
{
return device->out != NULL && device->cap.R == 1 && device->cap.G == 1 && device->cap.B == 1;
}
// Intensity conversions
static inline rgb_color_t rgb_set_intensity (rgb_color_t color, uint8_t intensity)
{
color.R = (uint8_t)(((color.R + 1) * intensity) >> 8);
color.G = (uint8_t)(((color.G + 1) * intensity) >> 8);
color.B = (uint8_t)(((color.B + 1) * intensity) >> 8);
return color;
}
static inline rgb_color_t rgb_reset_intensity (rgb_color_t color, uint8_t intensity)
{
color.R = (uint8_t)((color.R << 8) / (intensity + 1));
color.G = (uint8_t)((color.G << 8) / (intensity + 1));
color.B = (uint8_t)((color.B << 8) / (intensity + 1));
return color;
}
// RGB to/from 3 bytes per pixel packed format
static inline void rgb_3bpp_pack (uint8_t *led, rgb_color_t color, rgb_color_mask_t mask, uint8_t intensity)
{
uint32_t R = 0, G = 0, B = 0;
uint8_t bitmask = 0b10000000;
color = rgb_set_intensity(color, intensity);
do {
R <<= 3;
R |= color.R & bitmask ? 0b110 : 0b100;
G <<= 3;
G |= color.G & bitmask ? 0b110 : 0b100;
B <<= 3;
B |= color.B & bitmask ? 0b110 : 0b100;
} while(bitmask >>= 1);
if(mask.G) {
*led++ = (uint8_t)(G >> 16);
*led++ = (uint8_t)(G >> 8);
*led++ = (uint8_t)G;
} else
led += 3;
if(mask.R) {
*led++ = (uint8_t)(R >> 16);
*led++ = (uint8_t)(R >> 8);
*led++ = (uint8_t)R;
} else
led += 3;
if(mask.B) {
*led++ = (uint8_t)(B >> 16);
*led++ = (uint8_t)(B >> 8);
*led = (uint8_t)B;
}
}
static inline rgb_color_t rgb_3bpp_unpack (uint8_t *led, uint8_t intensity)
{
rgb_color_t color = {0};
if(intensity) {
uint32_t R = 0, G = 0, B = 0;
uint8_t bitmask = 0b00000001;
G = *led++ << 16;
G |= *led++ << 8;
G |= *led++;
R = *led++ << 16;
R |= *led++ << 8;
R |= *led++;
B = *led++ << 16;
B |= *led++ << 8;
B |= *led;
do {
if((R & 0b110) == 0b110)
color.R |= bitmask;
R >>= 3;
if((G & 0b110) == 0b110)
color.G |= bitmask;
G >>= 3;
if((B & 0b110) == 0b110)
color.B |= bitmask;
B >>= 3;
} while(bitmask <<= 1);
color = rgb_reset_intensity(color, intensity);
}
return color;
}
// RGB to/from 1 byte per pixel packed format
static inline void rgb_1bpp_assign (uint8_t *led, rgb_color_t color, rgb_color_mask_t mask)
{
if(mask.G)
*led++ = color.G;
else
led++;
if(mask.R)
*led++ = color.R;
else
led++;
if(mask.B)
*led = color.B;
}
static inline void rgb_1bpp_pack (uint8_t *led, rgb_color_t color, rgb_color_mask_t mask, uint8_t intensity)
{
color = rgb_set_intensity(color, intensity);
rgb_1bpp_assign(led, color, mask);
}
static inline rgb_color_t rgb_1bpp_unpack (uint8_t *led, uint8_t intensity)
{
rgb_color_t color = {0};
if(intensity) {
color.G = *led++;
color.R = *led++;
color.B = *led;
color = rgb_reset_intensity(color, intensity);
}
return color;
}
//