Creation
This commit is contained in:
		
							parent
							
								
									0ec9b2e83a
								
							
						
					
					
						commit
						c029117fec
					
				
							
								
								
									
										87
									
								
								src/rgb_led.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								src/rgb_led.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,87 @@ | ||||
| // Basic Config
 | ||||
| #include "main.h" | ||||
| #include "globals.h" | ||||
| 
 | ||||
| #ifdef HAS_RGB_LED | ||||
| 
 | ||||
| // RGB Led instance
 | ||||
| SmartLed rgb_led(LED_WS2812, 1, HAS_RGB_LED); | ||||
| 
 | ||||
| // Luminosity from 0 to 100%
 | ||||
| uint8_t rgb_luminosity = 50 ; | ||||
| 
 | ||||
| float rgb_CalcColor(float p, float q, float t) | ||||
| { | ||||
|     if (t < 0.0f) | ||||
|         t += 1.0f; | ||||
|     if (t > 1.0f) | ||||
|         t -= 1.0f; | ||||
| 
 | ||||
|     if (t < 1.0f / 6.0f) | ||||
|         return p + (q - p) * 6.0f * t; | ||||
| 
 | ||||
|     if (t < 0.5f) | ||||
|         return q; | ||||
| 
 | ||||
|     if (t < 2.0f / 3.0f) | ||||
|         return p + ((q - p) * (2.0f / 3.0f - t) * 6.0f); | ||||
| 
 | ||||
|     return p; | ||||
| } | ||||
| 
 | ||||
| // ------------------------------------------------------------------------
 | ||||
| // Hue, Saturation, Lightness color members
 | ||||
| // HslColor using H, S, L values (0.0 - 1.0)
 | ||||
| // L should be limited to between (0.0 - 0.5)
 | ||||
| // ------------------------------------------------------------------------
 | ||||
| RGBColor rgb_hsl2rgb(float h, float s, float l) | ||||
| { | ||||
|     RGBColor RGB_color; | ||||
|     float r; | ||||
|     float g; | ||||
|     float b; | ||||
| 
 | ||||
|     if (s == 0.0f || l == 0.0f) | ||||
|     { | ||||
|         r = g = b = l; // achromatic or black
 | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         float q = l < 0.5f ? l * (1.0f + s) : l + s - (l * s); | ||||
|         float p = 2.0f * l - q; | ||||
|         r = rgb_CalcColor(p, q, h + 1.0f / 3.0f); | ||||
|         g = rgb_CalcColor(p, q, h); | ||||
|         b = rgb_CalcColor(p, q, h - 1.0f / 3.0f); | ||||
|     } | ||||
| 
 | ||||
|     RGB_color.R = (uint8_t)(r * 255.0f); | ||||
|     RGB_color.G = (uint8_t)(g * 255.0f); | ||||
|     RGB_color.B = (uint8_t)(b * 255.0f); | ||||
| 
 | ||||
|     return RGB_color; | ||||
| } | ||||
| 
 | ||||
| void rgb_set_color(uint16_t hue) { | ||||
|   if (hue == COLOR_NONE) { | ||||
|     // Off
 | ||||
|     rgb_led[0] = Rgb(0,0,0); | ||||
|   } else { | ||||
|     // see http://www.workwithcolor.com/blue-color-hue-range-01.htm
 | ||||
|     // H (is color from 0..360) should be between 0.0 and 1.0
 | ||||
|     // S is saturation keep it to 1
 | ||||
|     // L is brightness should be between 0.0 and 0.5
 | ||||
|     // rgb_luminosity is between 0 and 100 (percent)
 | ||||
|     RGBColor target = rgb_hsl2rgb( hue / 360.0f, 1.0f, 0.005f * cfg.rgblum); | ||||
|     //uint32_t color = target.R<<16 | target.G<<8 | target.B;
 | ||||
|     rgb_led[0] = Rgb(target.R, target.G, target.B); | ||||
|   } | ||||
|   // Show
 | ||||
|   rgb_led.show(); | ||||
| } | ||||
| 
 | ||||
| #else | ||||
| 
 | ||||
| // No RGB LED empty functions
 | ||||
| void rgb_set_color(uint16_t hue) {} | ||||
| 
 | ||||
| #endif | ||||
							
								
								
									
										30
									
								
								src/rgb_led.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/rgb_led.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,30 @@ | ||||
| 
 | ||||
| #pragma once | ||||
| 
 | ||||
| // value for HSL color
 | ||||
| // see http://www.workwithcolor.com/blue-color-hue-range-01.htm
 | ||||
| #define COLOR_RED              0 | ||||
| #define COLOR_ORANGE          30 | ||||
| #define COLOR_ORANGE_YELLOW   45 | ||||
| #define COLOR_YELLOW          60 | ||||
| #define COLOR_YELLOW_GREEN    90 | ||||
| #define COLOR_GREEN          120 | ||||
| #define COLOR_GREEN_CYAN     165 | ||||
| #define COLOR_CYAN           180 | ||||
| #define COLOR_CYAN_BLUE      210 | ||||
| #define COLOR_BLUE           240 | ||||
| #define COLOR_BLUE_MAGENTA   275 | ||||
| #define COLOR_MAGENTA        300 | ||||
| #define COLOR_PINK           350 | ||||
| #define COLOR_WHITE          360 | ||||
| #define COLOR_NONE           999 | ||||
| 
 | ||||
| struct RGBColor | ||||
| { | ||||
|     uint8_t R; | ||||
|     uint8_t G; | ||||
|     uint8_t B; | ||||
| }; | ||||
| 
 | ||||
| // Exported Functions
 | ||||
| void rgb_set_color(uint16_t hue); | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user