day/night cycle on LED 1 from CSV file

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-09-26 23:01:26 +02:00
parent f2e2fe4078
commit 845fdd306e
15 changed files with 340 additions and 80 deletions

View File

@@ -1,6 +1,6 @@
#pragma once
#include "driver/rmt_tx.h"
#include "esp_check.h"
#include <stdint.h>
// Number of LEDs to be controlled

View File

@@ -72,7 +72,8 @@ void event_handler(void *arg, esp_event_base_t base, int32_t id, void *event_dat
}
led_strip_refresh(led_strip);
led_behavior_t led2_behavior = {.mode = LED_MODE_SOLID, .color = {.r = red, .g = green, .b = blue}};
led_behavior_t led2_behavior = {
.mode = LED_MODE_SOLID, .color = {.r = red, .g = green, .b = blue}, .on_time_ms = 0, .off_time_ms = 0};
led_status_set_behavior(2, led2_behavior);
}

View File

@@ -0,0 +1,8 @@
idf_component_register(SRCS
"simulator.c"
"storage.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES
led-manager
spiffs
)

View File

@@ -0,0 +1,17 @@
#pragma once
#include "esp_check.h"
#include <stdint.h>
esp_err_t add_light_item(const char time[5], uint8_t red, uint8_t green, uint8_t blue);
void cleanup_light_items(void);
#ifdef __cplusplus
extern "C"
{
#endif
void simulate(void *args);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,5 @@
#pragma once
void initialize_storage();
void load_file(const char *filename);

View File

@@ -0,0 +1,120 @@
#include "simulator.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "led_status.h"
#include "storage.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
static const char *TAG = "simulator";
// The struct is extended with a 'next' pointer to form a linked list.
typedef struct light_item_node_t
{
char time[5];
uint8_t red;
uint8_t green;
uint8_t blue;
struct light_item_node_t *next;
} light_item_node_t;
// Global pointers for the head and tail of the list.
static light_item_node_t *head = NULL;
static light_item_node_t *tail = NULL;
esp_err_t add_light_item(const char time[5], uint8_t red, uint8_t green, uint8_t blue)
{
// 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);
if (new_node == NULL)
{
ESP_LOGE(TAG, "Failed to allocate memory in PSRAM for new light_item_node_t.");
return ESP_FAIL;
}
// 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->next = NULL;
// Append the new node to the end of the list.
if (head == NULL)
{
// If the list is empty, the new node becomes both head and tail.
head = new_node;
tail = new_node;
}
else
{
// Otherwise, append the new node to the end and update tail.
tail->next = new_node;
tail = new_node;
}
return ESP_OK;
}
void cleanup_light_items(void)
{
light_item_node_t *current = head;
light_item_node_t *next_node;
while (current != NULL)
{
next_node = current->next;
heap_caps_free(current);
current = next_node;
}
head = NULL;
tail = NULL;
ESP_LOGI(TAG, "Cleaned up all light items.");
}
void simulate(void *args)
{
ESP_LOGI(TAG, "Simulation task started with args: %p", args);
initialize_storage();
load_file("/spiffs/schema_02.csv");
if (head == NULL)
{
ESP_LOGW(TAG, "Light schedule is empty. Simulation will not run.");
vTaskDelete(NULL);
return;
}
ESP_LOGI(TAG, "Starting simulation loop.");
light_item_node_t *current_item = head;
while (1)
{
if (current_item == NULL)
{
current_item = head;
ESP_LOGI(TAG, "Reached end of schedule, restarting from head.");
}
ESP_LOGI(TAG, "Simulating time: %s -> R:%d, G:%d, B:%d", current_item->time, current_item->red,
current_item->green, current_item->blue);
led_behavior_t led1_behavior = {
.mode = LED_MODE_SOLID,
.color = {.r = current_item->red, .g = current_item->green, .b = current_item->blue},
.on_time_ms = 0,
.off_time_ms = 0};
led_status_set_behavior(1, led1_behavior);
current_item = current_item->next;
vTaskDelay(pdMS_TO_TICKS(1000));
}
cleanup_light_items();
}

View File

@@ -0,0 +1,77 @@
#include "storage.h"
#include "esp_check.h"
#include "esp_log.h"
#include "esp_spiffs.h"
#include "simulator.h"
#include <errno.h>
#include <stdio.h>
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
};
esp_err_t ret = esp_vfs_spiffs_register(&conf);
if (ret != ESP_OK)
{
if (ret == ESP_FAIL)
{
ESP_LOGE(TAG, "Failed to mount or format filesystem");
}
else if (ret == ESP_ERR_NOT_FOUND)
{
ESP_LOGE(TAG, "Failed to find SPIFFS partition");
}
else
{
ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
}
return; // Oder entsprechende Fehlerbehandlung
}
}
void load_file(const char *filename)
{
ESP_LOGI(TAG, "Loading file: %s", filename);
FILE *f = fopen(filename, "r");
if (f == NULL)
{
ESP_LOGE(TAG, "Failed to open file for reading");
return;
}
char line[128]; // Puffer für eine Zeile, vergrößert für mehr Sicherheit
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;
// 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)
{
add_light_item(time, (uint8_t)red, (uint8_t)green, (uint8_t)blue);
}
else
{
ESP_LOGW(TAG, "Could not parse line: %s", line);
}
}
fclose(f);
ESP_LOGI(TAG, "Finished loading file.");
}