potion_party_leds/main/server.h

115 lines
3.4 KiB
C
Raw Normal View History

2023-09-18 20:41:46 +00:00
#ifndef _potion_party_server_h
#define _potion_party_server_h
2023-09-15 06:21:00 +00:00
2023-09-18 20:41:46 +00:00
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "shared.h"
#include "leds.h"
#include "esp_http_server.h"
#include "esp_system.h"
#include "esp_netif.h"
static
httpd_handle_t g_http_server = NULL;
static
void parse_leds_query(char* query_string, size_t query_size) {
char query_value[16];
char query_key[3];
size_t gradient_point_count = 0;
if(httpd_query_key_value(query_string, "l", query_value, sizeof(query_value)) == ESP_OK) {
gradient_point_count = atoi(query_value);
} else {
return;
}
struct gradient_point_t* points = malloc(gradient_point_count * sizeof(union led_t));
for(int point = 0; point < gradient_point_count; ++point) {
sprintf(query_key, "r%d", point);
if(httpd_query_key_value(query_string, query_key, query_value, sizeof(query_size)) == ESP_OK) {
points[point].led.components.red = atoi(query_value);
}
sprintf(query_key, "g%d", point);
if(httpd_query_key_value(query_string, query_key, query_value, sizeof(query_size)) == ESP_OK) {
points[point].led.components.green = atoi(query_value);
}
sprintf(query_key, "b%d", point);
if(httpd_query_key_value(query_string, query_key, query_value, sizeof(query_value)) == ESP_OK) {
points[point].led.components.blue = atoi(query_value);
}
sprintf(query_key, "a%d", point);
if(httpd_query_key_value(query_string, query_key, query_value, sizeof(query_value)) == ESP_OK) {
points[point].led.components.global = atoi(query_value) | 0xE0;
}
sprintf(query_key, "t%d", point);
if(httpd_query_key_value(query_string, query_key, query_value, sizeof(query_value)) == ESP_OK) {
points[point].offset = atoi(query_value);
}
LOGLN("led %d:", point);
LOGLN(" r %d", points[point].led.components.red);
LOGLN(" g %d", points[point].led.components.green);
LOGLN(" b %d", points[point].led.components.blue);
LOGLN(" global %d", points[point].led.components.global);
LOGLN(" t %d", (int)points[point].offset);
}
leds_set_gradient(points, gradient_point_count);
}
static
esp_err_t on_http_post(httpd_req_t* request) {
LOGLN("POST received on '/'.");
char* buffer;
size_t buffer_len;
buffer_len = httpd_req_get_url_query_len(request) + 1;
if(buffer_len > 1) {
buffer = malloc(buffer_len * sizeof(char));
if(httpd_req_get_url_query_str(request, buffer, buffer_len) == ESP_OK) {
LOGLN("Received query.");
parse_leds_query(buffer, buffer_len);
}
}
const char* response = "OK!";
httpd_resp_send(request, response, strlen(response));
return ESP_OK;
}
httpd_uri_t post = {
.uri="/",
.method=HTTP_POST,
.handler=&on_http_post,
.user_ctx = NULL
};
static
httpd_handle_t start_webserver(void) {
httpd_handle_t server = NULL;
httpd_config_t server_config = HTTPD_DEFAULT_CONFIG();
LOGLN("Starting HTTPd server ':%d'.", server_config.server_port);
if(httpd_start(&server, &server_config) == ESP_OK) {
httpd_register_uri_handler(server, &post);
return server;
}
LOGLN("Failed to start HTTPd server.");
return NULL;
}
static
void server_init(void) {
g_http_server = start_webserver();
2023-09-15 06:21:00 +00:00
}
2023-09-18 20:41:46 +00:00
#endif // !_potion_party_server_h