grblHAL core  20240318
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 
47 typedef void (*rgb_set_color_ptr)(uint16_t device, rgb_color_t color);
48 
53 typedef void (*rgb_set_color_masked_ptr)(uint16_t device, rgb_color_t color, rgb_color_mask_t mask);
54 
59 typedef uint8_t (*rgb_set_intensity_ptr)(uint8_t intensity);
60 
63 typedef void (*rgb_write_ptr)(void);
64 
65 typedef struct {
71  uint16_t num_devices;
72 } rgb_ptr_t;
73 
74 // helper structure and functions, not used by the core
75 
76 typedef struct {
77  uint16_t num_leds;
78  uint16_t num_bytes;
79  uint8_t *leds;
80  uint8_t intensity;
82 
83 static inline bool rgb_is_neopixels (rgb_ptr_t *device)
84 {
85  return device->out != NULL && device->cap.R > 126 && device->cap.G > 126 && device->cap.B > 126;
86 }
87 
88 static inline bool rgb_is_onoff (rgb_ptr_t *device)
89 {
90  return device->out != NULL && device->cap.R == 1 && device->cap.G == 1 && device->cap.B == 1;
91 }
92 
93 // Intensity conversions
94 
95 static inline rgb_color_t rgb_set_intensity (rgb_color_t color, uint8_t intensity)
96 {
97  color.R = (uint8_t)(((color.R + 1) * intensity) >> 8);
98  color.G = (uint8_t)(((color.G + 1) * intensity) >> 8);
99  color.B = (uint8_t)(((color.B + 1) * intensity) >> 8);
100 
101  return color;
102 }
103 
104 static inline rgb_color_t rgb_reset_intensity (rgb_color_t color, uint8_t intensity)
105 {
106  color.R = (uint8_t)((color.R << 8) / (intensity + 1));
107  color.G = (uint8_t)((color.G << 8) / (intensity + 1));
108  color.B = (uint8_t)((color.B << 8) / (intensity + 1));
109 
110  return color;
111 }
112 
113 // RGB to/from 3 bytes per pixel packed format
114 
115 static inline void rgb_3bpp_pack (uint8_t *led, rgb_color_t color, rgb_color_mask_t mask, uint8_t intensity)
116 {
117  uint32_t R = 0, G = 0, B = 0;
118  uint8_t bitmask = 0b10000000;
119 
120  color = rgb_set_intensity(color, intensity);
121 
122  do {
123  R <<= 3;
124  R |= color.R & bitmask ? 0b110 : 0b100;
125  G <<= 3;
126  G |= color.G & bitmask ? 0b110 : 0b100;
127  B <<= 3;
128  B |= color.B & bitmask ? 0b110 : 0b100;
129  } while(bitmask >>= 1);
130 
131  if(mask.G) {
132  *led++ = (uint8_t)(G >> 16);
133  *led++ = (uint8_t)(G >> 8);
134  *led++ = (uint8_t)G;
135  } else
136  led += 3;
137 
138  if(mask.R) {
139  *led++ = (uint8_t)(R >> 16);
140  *led++ = (uint8_t)(R >> 8);
141  *led++ = (uint8_t)R;
142  } else
143  led += 3;
144 
145  if(mask.B) {
146  *led++ = (uint8_t)(B >> 16);
147  *led++ = (uint8_t)(B >> 8);
148  *led = (uint8_t)B;
149  }
150 }
151 
152 static inline rgb_color_t rgb_3bpp_unpack (uint8_t *led, uint8_t intensity)
153 {
154  rgb_color_t color = {0};
155 
156  if(intensity) {
157 
158  uint32_t R = 0, G = 0, B = 0;
159  uint8_t bitmask = 0b00000001;
160 
161  G = *led++ << 16;
162  G |= *led++ << 8;
163  G |= *led++;
164  R = *led++ << 16;
165  R |= *led++ << 8;
166  R |= *led++;
167  B = *led++ << 16;
168  B |= *led++ << 8;
169  B |= *led;
170 
171  do {
172  if((R & 0b110) == 0b110)
173  color.R |= bitmask;
174  R >>= 3;
175  if((G & 0b110) == 0b110)
176  color.G |= bitmask;
177  G >>= 3;
178  if((B & 0b110) == 0b110)
179  color.B |= bitmask;
180  B >>= 3;
181  } while(bitmask <<= 1);
182 
183  color = rgb_reset_intensity(color, intensity);
184  }
185 
186  return color;
187 }
188 
189 // RGB to/from 1 byte per pixel packed format
190 
191 static inline void rgb_1bpp_assign (uint8_t *led, rgb_color_t color, rgb_color_mask_t mask)
192 {
193  if(mask.G)
194  *led++ = color.G;
195  else
196  led++;
197 
198  if(mask.R)
199  *led++ = color.R;
200  else
201  led++;
202 
203  if(mask.B)
204  *led = color.B;
205 }
206 
207 static inline void rgb_1bpp_pack (uint8_t *led, rgb_color_t color, rgb_color_mask_t mask, uint8_t intensity)
208 {
209  color = rgb_set_intensity(color, intensity);
210  rgb_1bpp_assign(led, color, mask);
211 }
212 
213 static inline rgb_color_t rgb_1bpp_unpack (uint8_t *led, uint8_t intensity)
214 {
215  rgb_color_t color = {0};
216 
217  if(intensity) {
218 
219  color.G = *led++;
220  color.R = *led++;
221  color.B = *led;
222 
223  color = rgb_reset_intensity(color, intensity);
224  }
225 
226  return color;
227 }
228 
229 //
uint8_t(* rgb_set_intensity_ptr)(uint8_t intensity)
Pointer to function for setting RGB (LED) intensity.
Definition: rgb.h:59
void(* rgb_write_ptr)(void)
Pointer to function for outputting RGB (LED) data to Neopixel strip.
Definition: rgb.h:63
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:53
void(* rgb_set_color_ptr)(uint16_t device, rgb_color_t color)
Pointer to function for setting RGB (LED) output.
Definition: rgb.h:47
Definition: rgb.h:76
uint8_t intensity
Definition: rgb.h:80
uint16_t num_bytes
Definition: rgb.h:78
uint8_t * leds
Definition: rgb.h:79
uint16_t num_leds
Definition: rgb.h:77
Definition: rgb.h:65
rgb_write_ptr write
Optional handler for outputting data to Neopixel strip.
Definition: rgb.h:68
rgb_set_intensity_ptr set_intensity
Optional handler for setting intensity, range 0 - 255.
Definition: rgb.h:69
rgb_set_color_ptr out
Optional handler for setting device (LED) color.
Definition: rgb.h:66
rgb_set_color_masked_ptr out_masked
Optional handler for setting device (LED) color, with mask for which LEDs to change.
Definition: rgb.h:67
rgb_color_t cap
Driver capability, color value: 0 - not available, 1 - on off, > 1 - intensity range 0 - n.
Definition: rgb.h:70
uint16_t num_devices
Number of devices (LEDs) available.
Definition: rgb.h:71
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