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

This commit is contained in:
Clawdia
2026-08-12 13:03:42 +02:00
parent eea6fb7130
commit d385cb8a35
+32 -87
View File
@@ -21,19 +21,20 @@ static rmt_encoder_handle_t led_encoder = NULL;
typedef struct { typedef struct {
const char *name; const char *name;
int freq_hz; int freq_hz;
int duration_min; int duration_sec;
uint32_t hue; uint32_t hue; // 0 = Rot, 20-30 = warmes Orange-Rot
} preset_t; } preset_t;
static const preset_t presets[] = { static const preset_t presets[] = {
// 25-Minuten-Entspannungsprogramm mit absteigender Frequenz und Aufwachphase {"Ankommen / Beta→Alpha", 10, 60, 0}, // 1 min Rot hell
// Alpha -> Theta -> sanfter Aufweck-Anstieg für sicheren Abschluss {"Alpha-Entspannung", 8, 90, 5}, // 1.5 min Rot
{"Einschwingen", 12, 3, 15}, {"Theta-Eintauchen", 5, 90, 10}, // 1.5 min Tiefes Rot
{"Alpha Entspannung", 10, 5, 0}, {"Theta-Tiefe", 6, 90, 15}, // 1.5 min Warmes Rot
{"Alpha/Theta Übergang", 8, 6, 20}, {"Delta-Regeneration", 2,120, 20}, // 2 min Dunkelrot
{"Theta Entspannung", 6, 6, 10}, {"Delta-Tiefschlaf", 1,120, 25}, // 2 min Sehr dunkel Rot
{"Tiefe Theta", 4, 3, 5}, {"Theta-Aufstieg", 4, 90, 18}, // 1.5 min Warmes Rot
{"Aufwachen", 10, 2, 30} {"Alpha-Weckphase", 7, 90, 8}, // 1.5 min Helles Rot
{"Wachwerden / Gamma-Hit",10, 60, 0} // 1 min Hellstes Rot
}; };
#define PRESET_COUNT (sizeof(presets)/sizeof(presets[0])) #define PRESET_COUNT (sizeof(presets)/sizeof(presets[0]))
@@ -42,7 +43,7 @@ static bool led_on = false;
static uint64_t last_toggle_ms = 0; static uint64_t last_toggle_ms = 0;
static uint64_t phase_start_ms = 0; static uint64_t phase_start_ms = 0;
static void hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r, uint32_t *g, uint32_t *b) { static void hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r, uint32_t *g, uint32_t *b){
h %= 360; h %= 360;
uint32_t rgb_max = v * 255 / 100; uint32_t rgb_max = v * 255 / 100;
uint32_t rgb_min = rgb_max * (100 - s) / 100; uint32_t rgb_min = rgb_max * (100 - s) / 100;
@@ -59,11 +60,11 @@ static void hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r, uint32_t *g
} }
} }
static void set_all_pixels(uint32_t r, uint32_t g, uint32_t b) { static void set_all_pixels(uint32_t r, uint32_t g, uint32_t b){
for(int i=0;i<LED_NUMBERS;i++){ for(int i=0;i<LED_NUMBERS;i++){
led_pixels[i*3+0]= (uint8_t)g; led_pixels[i*3+0]=(uint8_t)g; // G
led_pixels[i*3+1]= (uint8_t)b; led_pixels[i*3+1]=(uint8_t)r; // R
led_pixels[i*3+2]= (uint8_t)r; led_pixels[i*3+2]=(uint8_t)b; // B
} }
} }
@@ -83,101 +84,44 @@ static void apply_preset(int idx){
led_on = true; led_on = true;
last_toggle_ms = esp_timer_get_time()/1000; last_toggle_ms = esp_timer_get_time()/1000;
phase_start_ms = esp_timer_get_time()/1000; phase_start_ms = esp_timer_get_time()/1000;
ESP_LOGI(TAG, "Preset %d %s %dHz %dmin", idx, p->name, p->freq_hz, p->duration_min); ESP_LOGI(TAG,"Preset %d %s %dHz %ds", idx, p->name, p->freq_hz, p->duration_sec);
} }
static void stop_leds(void){ static void stop_leds(void){
current_preset = -1; current_preset = -1;
memset(led_pixels,0,sizeof(led_pixels)); memset(led_pixels,0,sizeof(led_pixels));
flush_leds(); flush_leds();
ESP_LOGI(TAG,"LEDs stopped");
}
static void list_presets(void){
printf("\n--- Presets ---\n");
for(int i=0;i<PRESET_COUNT;i++){
const preset_t *p=&presets[i];
printf("%d - %s %dHz %dmin\n", i, p->name, p->freq_hz, p->duration_min);
}
printf("Commands: 0-6 select, next, stop, list\n");
}
static void process_line(char *line){
if(strcmp(line,"stop")==0){ stop_leds(); return; }
if(strcmp(line,"next")==0){
int nxt = (current_preset+1)%PRESET_COUNT;
apply_preset(nxt);
return;
}
if(strcmp(line,"list")==0){ list_presets(); return; }
if(strlen(line)==1 && line[0]>='0' && line[0]<='6'){
int idx = line[0]-'0';
if(idx<PRESET_COUNT){ apply_preset(idx); return; }
}
printf("Unknown: %s\n", line);
} }
void app_main(void){ void app_main(void){
ESP_LOGI(TAG,"LED Laser Frequenz start"); ESP_LOGI(TAG,"LED Laser Frequenz start");
uart_config_t uart_cfg = { 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};
.baud_rate = 115200, ESP_ERROR_CHECK(uart_driver_install(UART_NUM_0,1024,0,0,NULL,0));
.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)); ESP_ERROR_CHECK(uart_param_config(UART_NUM_0,&uart_cfg));
rmt_tx_channel_config_t tx_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};
.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)); ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_cfg,&led_chan));
led_strip_encoder_config_t enc_cfg = { .resolution = RMT_LED_STRIP_RESOLUTION_HZ }; 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)); ESP_ERROR_CHECK(rmt_new_led_strip_encoder(&enc_cfg,&led_encoder));
ESP_ERROR_CHECK(rmt_enable(led_chan)); ESP_ERROR_CHECK(rmt_enable(led_chan));
memset(led_pixels,0,sizeof(led_pixels)); memset(led_pixels,0,sizeof(led_pixels));
flush_leds(); flush_leds();
// Auto-Start des 25-Minuten Entspannungsprogramms beim Einschalten
apply_preset(0); apply_preset(0);
ESP_LOGI(TAG,"Auto-Start: Relaxation Programm gestartet (Pin D10/GPIO18)"); ESP_LOGI(TAG,"Auto-Start Pin D10/GPIO18");
char line_buf[64];
int line_pos=0;
while(1){ while(1){
size_t buffered = 0;
uart_get_buffered_data_len(UART_NUM_0, &buffered);
while(buffered > 0){
uint8_t c;
int len = uart_read_bytes(UART_NUM_0,&c,1,0);
if(len>0){
if(c=='\r' || c=='\n'){
line_buf[line_pos]='\0';
if(line_pos>0){ process_line(line_buf); }
line_pos=0;
}else{
if(line_pos < (int)sizeof(line_buf)-1){ line_buf[line_pos++]=c; }
}
}
}
if(current_preset>=0){ if(current_preset>=0){
const preset_t *p=&presets[current_preset]; const preset_t *p=&presets[current_preset];
uint64_t now = esp_timer_get_time()/1000; uint64_t now=esp_timer_get_time()/1000;
uint64_t period_ms = 1000 / p->freq_hz; uint64_t period_ms = p->freq_hz>0 ? 1000/p->freq_hz : 1000;
uint64_t half = period_ms/2; uint64_t half = period_ms/2;
if(half<1) half=1; if(half<1) half=1;
if(now - last_toggle_ms >= half){ if(now - last_toggle_ms >= half){
led_on = !led_on; led_on=!led_on;
last_toggle_ms = now; last_toggle_ms=now;
if(led_on){ if(led_on){
uint32_t r,g,b; uint32_t r,g,b;
hsv2rgb(p->hue,100,100,&r,&g,&b); hsv2rgb(p->hue,100,100,&r,&g,&b);
@@ -187,14 +131,15 @@ void app_main(void){
} }
flush_leds(); flush_leds();
} }
// Auto-weiter zur nächsten Phase nach Ablauf der Dauer
uint64_t elapsed_ms = now - phase_start_ms; uint64_t elapsed_ms = now - phase_start_ms;
if(elapsed_ms >= (uint64_t)p->duration_min * 60 * 1000){ if(elapsed_ms >= (uint64_t)p->duration_sec * 1000){
if(current_preset + 1 < PRESET_COUNT){ if(current_preset+1 < PRESET_COUNT){
apply_preset(current_preset + 1); apply_preset(current_preset+1);
}else{ }else{
stop_leds(); stop_leds();
ESP_LOGI(TAG,"25-Minuten Programm beendet"); ESP_LOGI(TAG,"Programm beendet");
vTaskDelay(pdMS_TO_TICKS(2000));
apply_preset(0);
} }
} }
} }