aboutsummaryrefslogtreecommitdiffstats
path: root/quantum/visualizer/lcd_backlight.c
blob: 70187d1e000fb91914a7ca618f71e8ef0094d123 (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
/*
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 = 0x00;
static uint8_t current_saturation = 0x00;
static uint8_t current_intensity = 0xFF;
static uint8_t current_brightness = 0x7F;

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);
}
ort 80 for HTTP connections from hosts */ uip_listen(HTONS(HTTP_SERVER_PORT)); /* Mount the Dataflash disk via FatFS */ f_mount(0, &DiskFATState); } /** uIP stack application callback for the simple HTTP webserver. This function must be called each time the * TCP/IP stack needs a TCP packet to be processed. */ void HTTPServerApp_Callback(void) { uip_tcp_appstate_t* const AppState = &uip_conn->appstate; if (uip_aborted() || uip_timedout() || uip_closed()) { /* Lock to the closed state so that no further processing will occur on the connection */ AppState->HTTPServer.CurrentState = WEBSERVER_STATE_Closing; AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing; } if (uip_connected()) { /* New connection - initialize connection state values */ AppState->HTTPServer.CurrentState = WEBSERVER_STATE_OpenRequestedFile; AppState->HTTPServer.NextState = WEBSERVER_STATE_OpenRequestedFile; AppState->HTTPServer.FileOpen = false; AppState->HTTPServer.ACKedFilePos = 0; AppState->HTTPServer.SentChunkSize = 0; } if (uip_acked()) { /* Add the amount of ACKed file data to the total sent file bytes counter */ AppState->HTTPServer.ACKedFilePos += AppState->HTTPServer.SentChunkSize; /* Progress to the next state once the current state's data has been ACKed */ AppState->HTTPServer.CurrentState = AppState->HTTPServer.NextState; } if (uip_rexmit()) { /* Return file pointer to the last ACKed position */ f_lseek(&AppState->HTTPServer.FileHandle, AppState->HTTPServer.ACKedFilePos); } if (uip_rexmit() || uip_acked() || uip_newdata() || uip_connected() || uip_poll()) { switch (AppState->HTTPServer.CurrentState) { case WEBSERVER_STATE_OpenRequestedFile: HTTPServerApp_OpenRequestedFile(); break; case WEBSERVER_STATE_SendResponseHeader: HTTPServerApp_SendResponseHeader(); break; case WEBSERVER_STATE_SendData: HTTPServerApp_SendData(); break; case WEBSERVER_STATE_Closing: /* Connection is being terminated for some reason - close file handle */ f_close(&AppState->HTTPServer.FileHandle); AppState->HTTPServer.FileOpen = false; /* If connection is not already closed, close it */ uip_close(); AppState->HTTPServer.CurrentState = WEBSERVER_STATE_Closed; AppState->HTTPServer.NextState = WEBSERVER_STATE_Closed; break; } } } /** HTTP Server State handler for the Request Process state. This state manages the processing of incoming HTTP * GET requests to the server from the receiving HTTP client. */ static void HTTPServerApp_OpenRequestedFile(void) { uip_tcp_appstate_t* const AppState = &uip_conn->appstate; char* const AppData = (char*)uip_appdata; /* No HTTP header received from the client, abort processing */ if (!(uip_newdata())) return; char* RequestToken = strtok(AppData, " "); char* RequestedFileName = strtok(NULL, " "); /* Must be a GET request, abort otherwise */ if (strcmp_P(RequestToken, PSTR("GET")) != 0) { uip_abort(); return; } /* Copy over the requested filename */ strlcpy(AppState->HTTPServer.FileName, &RequestedFileName[1], sizeof(AppState->HTTPServer.FileName)); /* Determine the length of the URI so that it can be checked to see if it is a directory */ uint8_t FileNameLen = strlen(AppState->HTTPServer.FileName); /* If the URI is a directory, append the default filename */ if ((AppState->HTTPServer.FileName[FileNameLen - 1] == '/') || !(FileNameLen)) { strlcpy_P(&AppState->HTTPServer.FileName[FileNameLen], DefaultDirFileName, (sizeof(AppState->HTTPServer.FileName) - FileNameLen)); } /* Try to open the file from the Dataflash disk */ AppState->HTTPServer.FileOpen = (f_open(&AppState->HTTPServer.FileHandle, AppState->HTTPServer.FileName, (FA_OPEN_EXISTING | FA_READ)) == FR_OK); /* Lock to the SendResponseHeader state until connection terminated */ AppState->HTTPServer.CurrentState = WEBSERVER_STATE_SendResponseHeader; AppState->HTTPServer.NextState = WEBSERVER_STATE_SendResponseHeader; } /** HTTP Server State handler for the HTTP Response Header Send state. This state manages the transmission of * the HTTP response header to the receiving HTTP client. */ static void HTTPServerApp_SendResponseHeader(void) { uip_tcp_appstate_t* const AppState = &uip_conn->appstate; char* const AppData = (char*)uip_appdata; char* Extension = strpbrk(AppState->HTTPServer.FileName, "."); bool FoundMIMEType = false; /* If the file isn't already open, it wasn't found - send back a 404 error response and abort */ if (!(AppState->HTTPServer.FileOpen)) { /* Copy over the HTTP 404 response header and send it to the receiving client */ strcpy_P(AppData, HTTP404Header); strcat(AppData, AppState->HTTPServer.FileName); uip_send(AppData, strlen(AppData)); AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing; return; } /* Copy over the HTTP 200 response header and send it to the receiving client */ strcpy_P(AppData, HTTP200Header); /* Check to see if a MIME type for the requested file's extension was found */ if (Extension != NULL) { /* Look through the MIME type list, copy over the required MIME type if found */ for (uint8_t i = 0; i < (sizeof(MIMETypes) / sizeof(MIMETypes[0])); i++) { if (strcmp(&Extension[1], MIMETypes[i].Extension) == 0) { strcat(AppData, MIMETypes[i].MIMEType); FoundMIMEType = true; break; } } } /* Check if a MIME type was found and copied to the output buffer */ if (!(FoundMIMEType)) { /* MIME type not found - copy over the default MIME type */ strcat_P(AppData, DefaultMIMEType); } /* Add the end-of-line terminator and end-of-headers terminator after the MIME type */ strcat_P(AppData, PSTR("\r\n\r\n")); /* Send the MIME header to the receiving client */ uip_send(AppData, strlen(AppData)); /* When the MIME header is ACKed, progress to the data send stage */ AppState->HTTPServer.NextState = WEBSERVER_STATE_SendData; } /** HTTP Server State handler for the Data Send state. This state manages the transmission of file chunks * to the receiving HTTP client. */ static void HTTPServerApp_SendData(void) { uip_tcp_appstate_t* const AppState = &uip_conn->appstate; char* const AppData = (char*)uip_appdata; /* Get the maximum segment size for the current packet */ uint16_t MaxChunkSize = uip_mss(); /* Read the next chunk of data from the open file */ f_read(&AppState->HTTPServer.FileHandle, AppData, MaxChunkSize, &AppState->HTTPServer.SentChunkSize); /* Send the next file chunk to the receiving client */ uip_send(AppData, AppState->HTTPServer.SentChunkSize); /* Check if we are at the last chunk of the file, if so next ACK should close the connection */ if (MaxChunkSize != AppState->HTTPServer.SentChunkSize) AppState->HTTPServer.NextState = WEBSERVER_STATE_Closing; }