Fix phase durations & colors for 9-phase relaxation, use duration_sec and red hues

This commit is contained in:
Clawdia
2026-08-12 14:05:01 +02:00
parent d385cb8a35
commit a27b91851f
+36 -59
View File
@@ -1,10 +1,8 @@
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "driver/uart.h"
#include "driver/rmt_tx.h"
#include "led_strip_encoder.h"
@@ -22,49 +20,32 @@ typedef struct {
const char *name;
int freq_hz;
int duration_sec;
uint32_t hue; // 0 = Rot, 20-30 = warmes Orange-Rot
uint8_t r, g, b;
} preset_t;
static const preset_t presets[] = {
{"Ankommen / Beta→Alpha", 10, 60, 0}, // 1 min Rot hell
{"Alpha-Entspannung", 8, 90, 5}, // 1.5 min Rot
{"Theta-Eintauchen", 5, 90, 10}, // 1.5 min Tiefes Rot
{"Theta-Tiefe", 6, 90, 15}, // 1.5 min Warmes Rot
{"Delta-Regeneration", 2,120, 20}, // 2 min Dunkelrot
{"Delta-Tiefschlaf", 1,120, 25}, // 2 min Sehr dunkel Rot
{"Theta-Aufstieg", 4, 90, 18}, // 1.5 min Warmes Rot
{"Alpha-Weckphase", 7, 90, 8}, // 1.5 min Helles Rot
{"Wachwerden / Gamma-Hit",10, 60, 0} // 1 min Hellstes Rot
{"Ankommen / Beta→Alpha", 10, 60, 255, 80, 0},
{"Alpha-Entspannung", 8, 90, 255, 40, 0},
{"Theta-Eintauchen", 5, 90, 200, 0, 0},
{"Theta-Tiefe", 6, 90, 220, 30, 0},
{"Delta-Regeneration", 2,120, 180, 0, 0},
{"Delta-Tiefschlaf", 1,120, 150, 0, 0},
{"Theta-Aufstieg", 4, 90, 220, 30, 0},
{"Alpha-Weckphase", 7, 90, 255, 60, 0},
{"Wachwerden / Gamma-Hit",10, 60, 255, 100, 0}
};
#define PRESET_COUNT (sizeof(presets)/sizeof(presets[0]))
static int current_preset = -1;
static bool led_on = false;
static uint64_t last_toggle_ms = 0;
static uint64_t phase_start_ms = 0;
static int64_t last_toggle_us = 0;
static int64_t phase_start_us = 0;
static void hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r, uint32_t *g, uint32_t *b){
h %= 360;
uint32_t rgb_max = v * 255 / 100;
uint32_t rgb_min = rgb_max * (100 - s) / 100;
uint32_t i = h / 60;
uint32_t diff = h % 60;
uint32_t adj = (rgb_max - rgb_min) * diff / 60;
switch(i){
case 0: *r=rgb_max; *g=rgb_min+adj; *b=rgb_min; break;
case 1: *r=rgb_max-adj; *g=rgb_max; *b=rgb_min; break;
case 2: *r=rgb_min; *g=rgb_max; *b=rgb_min+adj; break;
case 3: *r=rgb_min; *g=rgb_max-adj; *b=rgb_max; break;
case 4: *r=rgb_min+adj; *g=rgb_min; *b=rgb_max; break;
default:*r=rgb_max; *g=rgb_min; *b=rgb_max-adj; break;
}
}
static void set_all_pixels(uint32_t r, uint32_t g, uint32_t b){
static void set_all_pixels(uint8_t r, uint8_t g, uint8_t b){
for(int i=0;i<LED_NUMBERS;i++){
led_pixels[i*3+0]=(uint8_t)g; // G
led_pixels[i*3+1]=(uint8_t)r; // R
led_pixels[i*3+2]=(uint8_t)b; // B
led_pixels[i*3+0]=g;
led_pixels[i*3+1]=r;
led_pixels[i*3+2]=b;
}
}
@@ -78,29 +59,29 @@ static void apply_preset(int idx){
if(idx<0 || idx>=PRESET_COUNT) return;
current_preset = idx;
const preset_t *p = &presets[idx];
uint32_t r,g,b;
hsv2rgb(p->hue, 100, 100, &r,&g,&b);
set_all_pixels(r,g,b);
set_all_pixels(p->r,p->g,p->b);
led_on = true;
last_toggle_ms = esp_timer_get_time()/1000;
phase_start_ms = esp_timer_get_time()/1000;
last_toggle_us = esp_timer_get_time();
phase_start_us = esp_timer_get_time();
ESP_LOGI(TAG,"Preset %d %s %dHz %ds", idx, p->name, p->freq_hz, p->duration_sec);
}
static void stop_leds(void){
current_preset = -1;
memset(led_pixels,0,sizeof(led_pixels));
flush_leds();
current_preset = -1;
}
void app_main(void){
ESP_LOGI(TAG,"LED Laser Frequenz start");
uart_config_t uart_cfg = {.baud_rate=115200,.data_bits=UART_DATA_8_BITS,.parity=UART_PARITY_DISABLE,.stop_bits=UART_STOP_BITS_1,.flow_ctrl=UART_HW_FLOWCTRL_DISABLE};
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_0,1024,0,0,NULL,0));
ESP_ERROR_CHECK(uart_param_config(UART_NUM_0,&uart_cfg));
rmt_tx_channel_config_t tx_cfg = {.clk_src=RMT_CLK_SRC_DEFAULT,.gpio_num=LED_GPIO_NUM,.mem_block_symbols=64,.resolution_hz=RMT_LED_STRIP_RESOLUTION_HZ,.trans_queue_depth=4};
rmt_tx_channel_config_t tx_cfg = {
.clk_src=RMT_CLK_SRC_DEFAULT,
.gpio_num=LED_GPIO_NUM,
.mem_block_symbols=64,
.resolution_hz=RMT_LED_STRIP_RESOLUTION_HZ,
.trans_queue_depth=4
};
ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_cfg,&led_chan));
led_strip_encoder_config_t enc_cfg={.resolution=RMT_LED_STRIP_RESOLUTION_HZ};
ESP_ERROR_CHECK(rmt_new_led_strip_encoder(&enc_cfg,&led_encoder));
@@ -115,34 +96,30 @@ void app_main(void){
while(1){
if(current_preset>=0){
const preset_t *p=&presets[current_preset];
uint64_t now=esp_timer_get_time()/1000;
uint64_t period_ms = p->freq_hz>0 ? 1000/p->freq_hz : 1000;
uint64_t half = period_ms/2;
if(half<1) half=1;
if(now - last_toggle_ms >= half){
int64_t now = esp_timer_get_time();
int64_t period_us = p->freq_hz>0 ? 1000000LL / p->freq_hz : 1000000LL;
int64_t half_us = period_us/2;
if(half_us<1000) half_us=1000;
if(now - last_toggle_us >= half_us){
led_on = !led_on;
last_toggle_ms=now;
last_toggle_us = now;
if(led_on){
uint32_t r,g,b;
hsv2rgb(p->hue,100,100,&r,&g,&b);
set_all_pixels(r,g,b);
set_all_pixels(p->r,p->g,p->b);
}else{
memset(led_pixels,0,sizeof(led_pixels));
}
flush_leds();
}
uint64_t elapsed_ms = now - phase_start_ms;
if(elapsed_ms >= (uint64_t)p->duration_sec * 1000){
if(now - phase_start_us >= (int64_t)p->duration_sec * 1000000LL){
if(current_preset+1 < PRESET_COUNT){
apply_preset(current_preset+1);
}else{
stop_leds();
ESP_LOGI(TAG,"Programm beendet");
vTaskDelay(pdMS_TO_TICKS(2000));
apply_preset(0);
}
}
}
vTaskDelay(pdMS_TO_TICKS(5));
vTaskDelay(pdMS_TO_TICKS(2));
}
}