optimise simulation handling
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -12,7 +12,8 @@ typedef struct
|
||||
__BEGIN_DECLS
|
||||
char *get_time(void);
|
||||
|
||||
esp_err_t add_light_item(const char time[5], uint8_t red, uint8_t green, uint8_t blue);
|
||||
esp_err_t add_light_item(const char time[5], uint8_t red, uint8_t green, uint8_t blue, uint8_t white,
|
||||
uint8_t brightness, uint8_t saturation);
|
||||
void cleanup_light_items(void);
|
||||
void start_simulate_day(void);
|
||||
void start_simulate_night(void);
|
||||
|
@@ -7,6 +7,7 @@
|
||||
#include <esp_log.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -37,12 +38,36 @@ typedef struct light_item_node_t
|
||||
static light_item_node_t *head = NULL;
|
||||
static light_item_node_t *tail = NULL;
|
||||
|
||||
// Interpolation mode selection
|
||||
typedef enum
|
||||
{
|
||||
INTERPOLATION_RGB,
|
||||
INTERPOLATION_HSV
|
||||
} interpolation_mode_t;
|
||||
|
||||
// You can change this to test different interpolation methods
|
||||
static const interpolation_mode_t interpolation_mode = INTERPOLATION_RGB;
|
||||
|
||||
char *get_time(void)
|
||||
{
|
||||
return time;
|
||||
}
|
||||
|
||||
esp_err_t add_light_item(const char time[5], uint8_t red, uint8_t green, uint8_t blue)
|
||||
// Main interpolation function that selects the appropriate method
|
||||
static rgb_t interpolate_color(rgb_t start, rgb_t end, float factor)
|
||||
{
|
||||
switch (interpolation_mode)
|
||||
{
|
||||
case INTERPOLATION_RGB:
|
||||
return interpolate_color_rgb(start, end, factor);
|
||||
case INTERPOLATION_HSV:
|
||||
default:
|
||||
return interpolate_color_hsv(start, end, factor);
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t add_light_item(const char time[5], uint8_t red, uint8_t green, uint8_t blue, uint8_t white,
|
||||
uint8_t brightness, uint8_t saturation)
|
||||
{
|
||||
// Allocate memory for a new node in PSRAM.
|
||||
light_item_node_t *new_node = (light_item_node_t *)heap_caps_malloc(sizeof(light_item_node_t), MALLOC_CAP_SPIRAM);
|
||||
@@ -52,11 +77,17 @@ esp_err_t add_light_item(const char time[5], uint8_t red, uint8_t green, uint8_t
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
rgb_t color = {.red = red, .green = green, .blue = blue};
|
||||
hsv_t hsv = rgb_to_hsv(color);
|
||||
hsv.v = brightness;
|
||||
hsv.s = saturation;
|
||||
rgb_t adjusted_color = hsv_to_rgb(hsv);
|
||||
|
||||
// Initialize the data of the new node.
|
||||
memcpy(new_node->time, time, sizeof(new_node->time));
|
||||
new_node->red = red;
|
||||
new_node->green = green;
|
||||
new_node->blue = blue;
|
||||
new_node->red = adjusted_color.red;
|
||||
new_node->green = adjusted_color.green;
|
||||
new_node->blue = adjusted_color.blue;
|
||||
new_node->next = NULL;
|
||||
|
||||
// Append the new node to the end of the list.
|
||||
@@ -131,16 +162,62 @@ static light_item_node_t *find_best_light_item_for_time(int hhmm)
|
||||
|
||||
if (best_item == NULL)
|
||||
{
|
||||
ESP_LOGW(TAG, "No suitable light item found for time up to %04d", hhmm);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGD(TAG, "Best light item for time %04d is %s", hhmm, best_item->time);
|
||||
// If no item is found for the given time (e.g., before the first item of the day),
|
||||
// find the last item of the previous day.
|
||||
best_time = -1;
|
||||
current = head;
|
||||
while (current != NULL)
|
||||
{
|
||||
int current_time = atoi(current->time);
|
||||
if (current_time > best_time)
|
||||
{
|
||||
best_time = current_time;
|
||||
best_item = current;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
return best_item;
|
||||
}
|
||||
|
||||
static light_item_node_t *find_next_light_item_for_time(int hhmm)
|
||||
{
|
||||
light_item_node_t *current = head;
|
||||
light_item_node_t *next_item = NULL;
|
||||
int next_time = 9999; // Initialize with a value larger than any possible time
|
||||
|
||||
// First pass: find the soonest time after hhmm
|
||||
while (current != NULL)
|
||||
{
|
||||
int current_time = atoi(current->time);
|
||||
if (current_time > hhmm && current_time < next_time)
|
||||
{
|
||||
next_time = current_time;
|
||||
next_item = current;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
// If no item is found for the rest of the day, wrap around to the beginning of the next day
|
||||
if (next_item == NULL)
|
||||
{
|
||||
current = head;
|
||||
next_time = 9999;
|
||||
while (current != NULL)
|
||||
{
|
||||
int current_time = atoi(current->time);
|
||||
if (current_time < next_time)
|
||||
{
|
||||
next_time = current_time;
|
||||
next_item = current;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
return next_item;
|
||||
}
|
||||
|
||||
void start_simulate_day(void)
|
||||
{
|
||||
initialize_light_items();
|
||||
@@ -186,7 +263,6 @@ void simulate_cycle(void *args)
|
||||
cycle_duration_minutes, delay_ms);
|
||||
|
||||
int current_minute_of_day = 0;
|
||||
light_item_node_t *last_item = NULL;
|
||||
|
||||
while (1)
|
||||
{
|
||||
@@ -196,15 +272,47 @@ void simulate_cycle(void *args)
|
||||
time = time_to_string(hhmm);
|
||||
|
||||
light_item_node_t *current_item = find_best_light_item_for_time(hhmm);
|
||||
light_item_node_t *next_item = find_next_light_item_for_time(hhmm);
|
||||
|
||||
if (current_item != NULL && current_item != last_item)
|
||||
if (current_item != NULL && next_item != NULL)
|
||||
{
|
||||
ESP_LOGI(TAG, "Simulating time: %02d:%02d -> Closest schedule is %s. R:%d, G:%d, B:%d", hours, minutes,
|
||||
current_item->time, current_item->red, current_item->green, current_item->blue);
|
||||
int current_item_time_min = (atoi(current_item->time) / 100) * 60 + (atoi(current_item->time) % 100);
|
||||
int next_item_time_min = (atoi(next_item->time) / 100) * 60 + (atoi(next_item->time) % 100);
|
||||
|
||||
if (next_item_time_min < current_item_time_min)
|
||||
{
|
||||
next_item_time_min += total_minutes_in_day;
|
||||
}
|
||||
|
||||
int minutes_since_current_item_start = current_minute_of_day - current_item_time_min;
|
||||
if (minutes_since_current_item_start < 0)
|
||||
{
|
||||
minutes_since_current_item_start += total_minutes_in_day;
|
||||
}
|
||||
|
||||
int interval_duration = next_item_time_min - current_item_time_min;
|
||||
if (interval_duration == 0)
|
||||
{
|
||||
interval_duration = 1;
|
||||
}
|
||||
|
||||
float interpolation_factor = (float)minutes_since_current_item_start / (float)interval_duration;
|
||||
|
||||
// Prepare colors for interpolation
|
||||
rgb_t start_rgb = {.red = current_item->red, .green = current_item->green, .blue = current_item->blue};
|
||||
rgb_t end_rgb = {.red = next_item->red, .green = next_item->green, .blue = next_item->blue};
|
||||
|
||||
// Use the interpolation function
|
||||
rgb_t final_rgb = interpolate_color(start_rgb, end_rgb, interpolation_factor);
|
||||
|
||||
led_strip_update(LED_STATE_SIMULATION, final_rgb);
|
||||
}
|
||||
else if (current_item != NULL)
|
||||
{
|
||||
// No next item, just use current
|
||||
led_strip_update(
|
||||
LED_STATE_SIMULATION,
|
||||
(rgb_t){.red = current_item->red, .green = current_item->green, .blue = current_item->blue});
|
||||
last_item = current_item;
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(delay_ms));
|
||||
|
@@ -11,10 +11,10 @@ static const char *TAG = "storage";
|
||||
void initialize_storage()
|
||||
{
|
||||
esp_vfs_spiffs_conf_t conf = {
|
||||
.base_path = "/spiffs", // Der Basispfad, unter dem das Dateisystem gemountet wird
|
||||
.partition_label = NULL, // NULL, um die erste gefundene SPIFFS-Partition zu verwenden
|
||||
.max_files = 5, // Maximale Anzahl gleichzeitig geöffneter Dateien
|
||||
.format_if_mount_failed = false // Partition formatieren, wenn das Mounten fehlschlägt
|
||||
.base_path = "/spiffs",
|
||||
.partition_label = NULL,
|
||||
.max_files = 5,
|
||||
.format_if_mount_failed = false,
|
||||
};
|
||||
|
||||
esp_err_t ret = esp_vfs_spiffs_register(&conf);
|
||||
@@ -33,7 +33,7 @@ void initialize_storage()
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
|
||||
}
|
||||
return; // Oder entsprechende Fehlerbehandlung
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,24 +47,30 @@ void load_file(const char *filename)
|
||||
return;
|
||||
}
|
||||
|
||||
char line[128]; // Puffer für eine Zeile, vergrößert für mehr Sicherheit
|
||||
char line[128];
|
||||
uint8_t line_number = 0;
|
||||
while (fgets(line, sizeof(line), f))
|
||||
{
|
||||
// Entferne möglichen Zeilenumbruch am Ende
|
||||
char *pos = strchr(line, '\n');
|
||||
if (pos)
|
||||
{
|
||||
*pos = '\0';
|
||||
}
|
||||
|
||||
char time[5] = {0}; // 4 Zeichen + Nullterminator
|
||||
int red, green, blue;
|
||||
char time[10] = {0};
|
||||
int red, green, blue, white, brightness, saturation;
|
||||
|
||||
// Parse die Zeile im Format "HHMM,R,G,B"
|
||||
int items_scanned = sscanf(line, "%4[^,],%d,%d,%d", time, &red, &green, &blue);
|
||||
if (items_scanned == 4)
|
||||
int items_scanned = sscanf(line, "%d,%d,%d,%d,%d,%d", &red, &green, &blue, &white, &brightness, &saturation);
|
||||
if (items_scanned == 6)
|
||||
{
|
||||
add_light_item(time, (uint8_t)red, (uint8_t)green, (uint8_t)blue);
|
||||
int total_minutes = line_number * 30;
|
||||
int hours = total_minutes / 60;
|
||||
int minutes = total_minutes % 60;
|
||||
|
||||
snprintf(time, sizeof(time), "%02d%02d", hours, minutes);
|
||||
|
||||
add_light_item(time, red, green, blue, white, brightness, saturation);
|
||||
line_number++;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -74,4 +80,4 @@ void load_file(const char *filename)
|
||||
|
||||
fclose(f);
|
||||
ESP_LOGI(TAG, "Finished loading file.");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user