-- Meta description of nodes. -- Copyright (C) 2014 Tristan Gingold -- -- This program 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 2 of the License, or -- (at your option) any later version. -- -- This program 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 this program. If not, see . with Types; use Types; with Vhdl.Nodes; use Vhdl.Nodes; with Vhdl.Tokens; use Vhdl.Tokens; with PSL.Types; use PSL.Types; package Vhdl.Nodes_Meta is -- The enumeration of all possible types in the nodes. type Types_Enum is ( -- TYPES ); -- The enumeration of all fields defined in iirs. type Fields_Enum is ( -- FIELDS ); pragma Discard_Names (Fields_Enum); -- Return the type of field F. function Get_Field_Type (F : Fields_Enum) return Types_Enum; -- Get the name of a field. function Get_Field_Image (F : Fields_Enum) return String; -- Get the name of a kind. function Get_Iir_Image (K : Iir_Kind) return String; -- Possible attributes of a field. type Field_Attribute is ( -- ATTRS ); -- Get the attribute of a field. function Get_Field_Attribute (F : Fields_Enum) return Field_Attribute; type Fields_Index_Extended is new Int32; subtype Fields_Index is Fields_Index_Extended range 0 .. Fields_Index_Extended'Last; type Fields_Array is array (Fields_Index range <>) of Fields_Enum; -- Return the list of fields for node K. The fields are sorted: first -- the non nodes/list of nodes, then the nodes/lists that aren't reference, -- and then the reference. function Get_Fields (K : Iir_Kind) return Fields_Array; -- Likewise, but without using arrays (for interfacing with C). function Get_Fields_First (K : Iir_Kind) return Fields_Index; function Get_Fields_Last (K : Iir_Kind) return Fields_Index; function Get_Field_By_Index (Idx : Fields_Index) return Fields_Enum; -- Get/Set a field. -- FUNCS end Vhdl.Nodes_Meta; value=''/>
path: root/quantum/visualizer/lcd_backlight.c
blob: 6cd996f75e5f076d108a7631b20e7c62017eeba8 (plain)
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
/*
The MIT License (MIT)

Copyright (c) 2016 Fred Sundvik

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "lcd_backlight.h"
#include <math.h>

static uint8_t current_hue = 0;
static uint8_t current_saturation = 0;
static uint8_t current_intensity = 0;
static uint8_t current_brightness = 0;

void lcd_backlight_init(void) {
    lcd_backlight_hal_init();
    lcd_backlight_color(current_hue, current_saturation, current_intensity);
}

// This code is based on Brian Neltner's blogpost and example code
// "Why every LED light should be using HSI colorspace".
// http://blog.saikoled.com/post/43693602826/why-every-led-light-should-be-using-hsi
static void hsi_to_rgb(float h, float s, float i, uint16_t* r_out, uint16_t* g_out, uint16_t* b_out) {
    unsigned int r, g, b;
    h = fmodf(h, 360.0f); // cycle h around to 0-360 degrees
    h = 3.14159f * h / 180.0f; // Convert to radians.
    s = s > 0.0f ? (s < 1.0f ? s : 1.0f) : 0.0f; // clamp s and i to interval [0,1]
    i = i > 0.0f ? (i < 1.0f ? i : 1.0f) : 0.0f;

    // Math! Thanks in part to Kyle Miller.
    if(h < 2.09439f) {
        r = 65535.0f * i/3.0f *(1.0f + s * cos(h) / cosf(1.047196667f - h));
        g = 65535.0f * i/3.0f *(1.0f + s *(1.0f - cosf(h) / cos(1.047196667f - h)));
        b = 65535.0f * i/3.0f *(1.0f - s);
    } else if(h < 4.188787) {
        h = h - 2.09439;
        g = 65535.0f * i/3.0f *(1.0f + s * cosf(h) / cosf(1.047196667f - h));
        b = 65535.0f * i/3.0f *(1.0f + s * (1.0f - cosf(h) / cosf(1.047196667f - h)));
        r = 65535.0f * i/3.0f *(1.0f - s);
    } else {
        h = h - 4.188787;
        b = 65535.0f*i/3.0f * (1.0f + s * cosf(h) / cosf(1.047196667f - h));
        r = 65535.0f*i/3.0f * (1.0f + s * (1.0f - cosf(h) / cosf(1.047196667f - h)));
        g = 65535.0f*i/3.0f * (1.0f - s);
    }
    *r_out = r > 65535 ? 65535 : r;
    *g_out = g > 65535 ? 65535 : g;
    *b_out = b > 65535 ? 65535 : b;
}

void lcd_backlight_color(uint8_t hue, uint8_t saturation, uint8_t intensity) {
    uint16_t r, g, b;
    float hue_f = 360.0f * (float)hue / 255.0f;
    float saturation_f = (float)saturation / 255.0f;
    float intensity_f = (float)intensity / 255.0f;
    intensity_f *= (float)current_brightness / 255.0f;
    hsi_to_rgb(hue_f, saturation_f, intensity_f, &r, &g, &b);
	current_hue = hue;
	current_saturation = saturation;
	current_intensity = intensity;
	lcd_backlight_hal_color(r, g, b);
}

void lcd_backlight_brightness(uint8_t b) {
    current_brightness = b;
    lcd_backlight_color(current_hue, current_saturation, current_intensity);
}

uint8_t lcd_get_backlight_brightness(void) {
	return current_brightness;
}