diff options
author | Fred Sundvik <fsundvik@gmail.com> | 2016-07-06 20:15:45 +0300 |
---|---|---|
committer | Fred Sundvik <fsundvik@gmail.com> | 2016-07-06 20:15:45 +0300 |
commit | 9f33a5593cc70dfb0885328061f1aa4b2c2fa386 (patch) | |
tree | c122041dcc3c4e2d01daf5995dad03939d16f55c /quantum/visualizer/lcd_backlight.c | |
parent | 19f480992c015aec0a15dca43e740bad8b7834e6 (diff) | |
parent | bde869aa7ec8601459bc63b9636081d21108d1be (diff) | |
download | qmk_firmware-9f33a5593cc70dfb0885328061f1aa4b2c2fa386.tar.gz qmk_firmware-9f33a5593cc70dfb0885328061f1aa4b2c2fa386.zip |
Add 'quantum/visualizer/' from commit 'bde869aa7ec8601459bc63b9636081d21108d1be'
git-subtree-dir: quantum/visualizer
git-subtree-mainline: 19f480992c015aec0a15dca43e740bad8b7834e6
git-subtree-split: bde869aa7ec8601459bc63b9636081d21108d1be
Diffstat (limited to 'quantum/visualizer/lcd_backlight.c')
-rw-r--r-- | quantum/visualizer/lcd_backlight.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/quantum/visualizer/lcd_backlight.c b/quantum/visualizer/lcd_backlight.c new file mode 100644 index 000000000..70187d1e0 --- /dev/null +++ b/quantum/visualizer/lcd_backlight.c | |||
@@ -0,0 +1,85 @@ | |||
1 | /* | ||
2 | The MIT License (MIT) | ||
3 | |||
4 | Copyright (c) 2016 Fred Sundvik | ||
5 | |||
6 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
7 | of this software and associated documentation files (the "Software"), to deal | ||
8 | in the Software without restriction, including without limitation the rights | ||
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
10 | copies of the Software, and to permit persons to whom the Software is | ||
11 | furnished to do so, subject to the following conditions: | ||
12 | |||
13 | The above copyright notice and this permission notice shall be included in all | ||
14 | copies or substantial portions of the Software. | ||
15 | |||
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
22 | SOFTWARE. | ||
23 | */ | ||
24 | |||
25 | #include "lcd_backlight.h" | ||
26 | #include <math.h> | ||
27 | |||
28 | static uint8_t current_hue = 0x00; | ||
29 | static uint8_t current_saturation = 0x00; | ||
30 | static uint8_t current_intensity = 0xFF; | ||
31 | static uint8_t current_brightness = 0x7F; | ||
32 | |||
33 | void lcd_backlight_init(void) { | ||
34 | lcd_backlight_hal_init(); | ||
35 | lcd_backlight_color(current_hue, current_saturation, current_intensity); | ||
36 | } | ||
37 | |||
38 | // This code is based on Brian Neltner's blogpost and example code | ||
39 | // "Why every LED light should be using HSI colorspace". | ||
40 | // http://blog.saikoled.com/post/43693602826/why-every-led-light-should-be-using-hsi | ||
41 | static void hsi_to_rgb(float h, float s, float i, uint16_t* r_out, uint16_t* g_out, uint16_t* b_out) { | ||
42 | unsigned int r, g, b; | ||
43 | h = fmodf(h, 360.0f); // cycle h around to 0-360 degrees | ||
44 | h = 3.14159f * h / 180.0f; // Convert to radians. | ||
45 | s = s > 0.0f ? (s < 1.0f ? s : 1.0f) : 0.0f; // clamp s and i to interval [0,1] | ||
46 | i = i > 0.0f ? (i < 1.0f ? i : 1.0f) : 0.0f; | ||
47 | |||
48 | // Math! Thanks in part to Kyle Miller. | ||
49 | if(h < 2.09439f) { | ||
50 | r = 65535.0f * i/3.0f *(1.0f + s * cos(h) / cosf(1.047196667f - h)); | ||
51 | g = 65535.0f * i/3.0f *(1.0f + s *(1.0f - cosf(h) / cos(1.047196667f - h))); | ||
52 | b = 65535.0f * i/3.0f *(1.0f - s); | ||
53 | } else if(h < 4.188787) { | ||
54 | h = h - 2.09439; | ||
55 | g = 65535.0f * i/3.0f *(1.0f + s * cosf(h) / cosf(1.047196667f - h)); | ||
56 | b = 65535.0f * i/3.0f *(1.0f + s * (1.0f - cosf(h) / cosf(1.047196667f - h))); | ||
57 | r = 65535.0f * i/3.0f *(1.0f - s); | ||
58 | } else { | ||
59 | h = h - 4.188787; | ||
60 | b = 65535.0f*i/3.0f * (1.0f + s * cosf(h) / cosf(1.047196667f - h)); | ||
61 | r = 65535.0f*i/3.0f * (1.0f + s * (1.0f - cosf(h) / cosf(1.047196667f - h))); | ||
62 | g = 65535.0f*i/3.0f * (1.0f - s); | ||
63 | } | ||
64 | *r_out = r > 65535 ? 65535 : r; | ||
65 | *g_out = g > 65535 ? 65535 : g; | ||
66 | *b_out = b > 65535 ? 65535 : b; | ||
67 | } | ||
68 | |||
69 | void lcd_backlight_color(uint8_t hue, uint8_t saturation, uint8_t intensity) { | ||
70 | uint16_t r, g, b; | ||
71 | float hue_f = 360.0f * (float)hue / 255.0f; | ||
72 | float saturation_f = (float)saturation / 255.0f; | ||
73 | float intensity_f = (float)intensity / 255.0f; | ||
74 | intensity_f *= (float)current_brightness / 255.0f; | ||
75 | hsi_to_rgb(hue_f, saturation_f, intensity_f, &r, &g, &b); | ||
76 | current_hue = hue; | ||
77 | current_saturation = saturation; | ||
78 | current_intensity = intensity; | ||
79 | lcd_backlight_hal_color(r, g, b); | ||
80 | } | ||
81 | |||
82 | void lcd_backlight_brightness(uint8_t b) { | ||
83 | current_brightness = b; | ||
84 | lcd_backlight_color(current_hue, current_saturation, current_intensity); | ||
85 | } | ||