grblHAL core  20241107
rgb.h
Go to the documentation of this file.
1 /*
2  rgb.h - typedefs, API structure and helper functions for RGB lights and LED strips
3 
4  Part of grblHAL
5 
6  Copyright (c) 2024 Terje Io
7 
8  grblHAL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  grblHAL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 typedef union {
23  uint8_t value;
24  uint8_t mask;
25  struct {
26  uint8_t B :1,
27  G :1,
28  R :1,
29  W :1,
30  unused :4;
31  };
33 
34 typedef union {
35  uint32_t value;
36  struct {
37  uint8_t B;
38  uint8_t G;
39  uint8_t R;
40  uint8_t W;
41  };
42 } rgb_color_t;
43 
44 typedef union {
45  uint8_t mask;
46  struct {
47  uint8_t is_blocking :1,
50  };
52 
56 typedef void (*rgb_set_color_ptr)(uint16_t device, rgb_color_t color);
57 
62 typedef void (*rgb_set_color_masked_ptr)(uint16_t device, rgb_color_t color, rgb_color_mask_t mask);
63 
68 typedef uint8_t (*rgb_set_intensity_ptr)(uint8_t intensity);
69 
72 typedef void (*rgb_write_ptr)(void);
73 
74 typedef struct {
81  uint16_t num_devices;
82 } rgb_ptr_t;
83 
84 // helper structure and functions, not used by the core
85 
86 typedef struct {
87  uint16_t num_leds;
88  uint16_t num_bytes;
89  uint8_t *leds;
90  uint8_t intensity;
92 
93 static inline bool rgb_is_neopixels (rgb_ptr_t *device)
94 {
95  return device->out != NULL && device->cap.R > 126 && device->cap.G > 126 && device->cap.B > 126;
96 }
97 
98 static inline bool rgb_is_onoff (rgb_ptr_t *device)
99 {
100  return device->out != NULL && device->cap.R == 1 && device->cap.G == 1 && device->cap.B == 1;
101 }
102 
103 // Intensity conversions
104 
105 static inline rgb_color_t rgb_set_intensity (rgb_color_t color, uint8_t intensity)
106 {
107  color.R = (uint8_t)(((color.R + 1) * intensity) >> 8);
108  color.G = (uint8_t)(((color.G + 1) * intensity) >> 8);
109  color.B = (uint8_t)(((color.B + 1) * intensity) >> 8);
110 
111  return color;
112 }
113 
114 static inline rgb_color_t rgb_reset_intensity (rgb_color_t color, uint8_t intensity)
115 {
116  color.R = (uint8_t)((color.R << 8) / (intensity + 1));
117  color.G = (uint8_t)((color.G << 8) / (intensity + 1));
118  color.B = (uint8_t)((color.B << 8) / (intensity + 1));
119 
120  return color;
121 }
122 
123 // RGB to/from 3 bytes per pixel packed format
124 
125 static inline void rgb_3bpp_pack (uint8_t *led, rgb_color_t color, rgb_color_mask_t mask, uint8_t intensity)
126 {
127  uint32_t R = 0, G = 0, B = 0;
128  uint8_t bitmask = 0b10000000;
129 
130  color = rgb_set_intensity(color, intensity);
131 
132  do {
133  R <<= 3;
134  R |= color.R & bitmask ? 0b011 : 0b010;
135  G <<= 3;
136  G |= color.G & bitmask ? 0b011 : 0b010;
137  B <<= 3;
138  B |= color.B & bitmask ? 0b011 : 0b010;
139  } while(bitmask >>= 1);
140 
141  if(mask.G) {
142  *led++ = (uint8_t)(G >> 16);
143  *led++ = (uint8_t)(G >> 8);
144  *led++ = (uint8_t)G;
145  } else
146  led += 3;
147 
148  if(mask.R) {
149  *led++ = (uint8_t)(R >> 16);
150  *led++ = (uint8_t)(R >> 8);
151  *led++ = (uint8_t)R;
152  } else
153  led += 3;
154 
155  if(mask.B) {
156  *led++ = (uint8_t)(B >> 16);
157  *led++ = (uint8_t)(B >> 8);
158  *led = (uint8_t)B;
159  }
160 }
161 
162 static inline rgb_color_t rgb_3bpp_unpack (uint8_t *led, uint8_t intensity)
163 {
164  rgb_color_t color = {0};
165 
166  if(intensity) {
167 
168  uint32_t R = 0, G = 0, B = 0;
169  uint8_t bitmask = 0b00000001;
170 
171  G = *led++ << 16;
172  G |= *led++ << 8;
173  G |= *led++;
174  R = *led++ << 16;
175  R |= *led++ << 8;
176  R |= *led++;
177  B = *led++ << 16;
178  B |= *led++ << 8;
179  B |= *led;
180 
181  do {
182  if((R & 0b011) == 0b011)
183  color.R |= bitmask;
184  R >>= 3;
185  if((G & 0b011) == 0b011)
186  color.G |= bitmask;
187  G >>= 3;
188  if((B & 0b011) == 0b011)
189  color.B |= bitmask;
190  B >>= 3;
191  } while(bitmask <<= 1);
192 
193  color = rgb_reset_intensity(color, intensity);
194  }
195 
196  return color;
197 }
198 
199 // RGB to/from 1 byte per pixel packed format
200 
201 static inline void rgb_1bpp_assign (uint8_t *led, rgb_color_t color, rgb_color_mask_t mask)
202 {
203  if(mask.G)
204  *led++ = color.G;
205  else
206  led++;
207 
208  if(mask.R)
209  *led++ = color.R;
210  else
211  led++;
212 
213  if(mask.B)
214  *led = color.B;
215 }
216 
217 static inline void rgb_1bpp_pack (uint8_t *led, rgb_color_t color, rgb_color_mask_t mask, uint8_t intensity)
218 {
219  color = rgb_set_intensity(color, intensity);
220  rgb_1bpp_assign(led, color, mask);
221 }
222 
223 static inline rgb_color_t rgb_1bpp_unpack (uint8_t *led, uint8_t intensity)
224 {
225  rgb_color_t color = {0};
226 
227  if(intensity) {
228 
229  color.G = *led++;
230  color.R = *led++;
231  color.B = *led;
232 
233  color = rgb_reset_intensity(color, intensity);
234  }
235 
236  return color;
237 }
238 
239 //
uint8_t(* rgb_set_intensity_ptr)(uint8_t intensity)
Pointer to function for setting RGB (LED) intensity.
Definition: rgb.h:68
void(* rgb_write_ptr)(void)
Pointer to function for outputting RGB (LED) data to Neopixel strip.
Definition: rgb.h:72
void(* rgb_set_color_masked_ptr)(uint16_t device, rgb_color_t color, rgb_color_mask_t mask)
Pointer to function for setting RGB (LED) output, with mask for which LEDs to change.
Definition: rgb.h:62
void(* rgb_set_color_ptr)(uint16_t device, rgb_color_t color)
Pointer to function for setting RGB (LED) output.
Definition: rgb.h:56
Definition: rgb.h:86
uint8_t intensity
Definition: rgb.h:90
uint16_t num_bytes
Definition: rgb.h:88
uint8_t * leds
Definition: rgb.h:89
uint16_t num_leds
Definition: rgb.h:87
Definition: rgb.h:74
rgb_write_ptr write
Optional handler for outputting data to Neopixel strip.
Definition: rgb.h:77
rgb_set_intensity_ptr set_intensity
Optional handler for setting intensity, range 0 - 255.
Definition: rgb.h:78
rgb_set_color_ptr out
Optional handler for setting device (LED) color.
Definition: rgb.h:75
rgb_set_color_masked_ptr out_masked
Optional handler for setting device (LED) color, with mask for which LEDs to change.
Definition: rgb.h:76
rgb_properties_t flags
Driver property flags.
Definition: rgb.h:80
rgb_color_t cap
Driver capability, color value: 0 - not available, 1 - on off, > 1 - intensity range 0 - n.
Definition: rgb.h:79
uint16_t num_devices
Number of devices (LEDs) available.
Definition: rgb.h:81
Definition: rgb.h:22
uint8_t W
Definition: rgb.h:29
uint8_t value
Definition: rgb.h:23
uint8_t B
Definition: rgb.h:26
uint8_t mask
Definition: rgb.h:24
uint8_t G
Definition: rgb.h:27
uint8_t R
Definition: rgb.h:28
uint8_t unused
Definition: rgb.h:30
Definition: rgb.h:34
uint8_t W
White.
Definition: rgb.h:40
uint8_t B
Blue.
Definition: rgb.h:37
uint8_t G
Green.
Definition: rgb.h:38
uint8_t R
Red.
Definition: rgb.h:39
uint32_t value
Definition: rgb.h:35
Definition: rgb.h:44
uint8_t is_strip
Definition: rgb.h:48
uint8_t unassigned
Definition: rgb.h:49
uint8_t mask
Definition: rgb.h:45
uint8_t is_blocking
Definition: rgb.h:47