Allow complete state fallthrough

master
Kenny Levinsen 2020-09-20 03:52:32 +02:00
parent 940cf08226
commit 9df941cfd1
1 changed files with 15 additions and 12 deletions

27
main.c
View File

@ -237,6 +237,7 @@ static void update_temperature(struct context *ctx) {
recalc_stops(ctx, now); recalc_stops(ctx, now);
switch (ctx->state) { switch (ctx->state) {
start:
case HIGH_TEMP: case HIGH_TEMP:
if (now <= ctx->stop_time && now > ctx->start_time + ctx->duration) { if (now <= ctx->stop_time && now > ctx->start_time + ctx->duration) {
temp = ctx->high_temp; temp = ctx->high_temp;
@ -249,13 +250,14 @@ static void update_temperature(struct context *ctx) {
} }
// fallthrough // fallthrough
case ANIMATING_TO_LOW: case ANIMATING_TO_LOW:
if (now > ctx->animation_start + ctx->duration) { if (now <= ctx->animation_start + ctx->duration) {
ctx->state = LOW_TEMP; time_pos = clamp(((double)now - (double)ctx->animation_start) / (double)ctx->duration);
temp_pos = (double)(ctx->high_temp - ctx->low_temp) * time_pos;
temp = ctx->high_temp - temp_pos;
break;
} }
time_pos = clamp(((double)now - (double)ctx->animation_start) / (double)ctx->duration); ctx->state = LOW_TEMP;
temp_pos = (double)(ctx->high_temp - ctx->low_temp) * time_pos; // fallthrough
temp = ctx->high_temp - temp_pos;
break;
case LOW_TEMP: case LOW_TEMP:
if (now > ctx->stop_time + ctx->duration || now <= ctx->start_time) { if (now > ctx->stop_time + ctx->duration || now <= ctx->start_time) {
temp = ctx->low_temp; temp = ctx->low_temp;
@ -268,13 +270,14 @@ static void update_temperature(struct context *ctx) {
} }
// fallthrough // fallthrough
case ANIMATING_TO_HIGH: case ANIMATING_TO_HIGH:
if (now > ctx->animation_start + ctx->duration) { if (now <= ctx->animation_start + ctx->duration) {
ctx->state = HIGH_TEMP; time_pos = clamp(((double)now - (double)ctx->animation_start) / (double)ctx->duration);
temp_pos = (double)(ctx->high_temp - ctx->low_temp) * time_pos;
temp = ctx->low_temp + temp_pos;
break;
} }
time_pos = clamp(((double)now - (double)ctx->animation_start) / (double)ctx->duration); ctx->state = HIGH_TEMP;
temp_pos = (double)(ctx->high_temp - ctx->low_temp) * time_pos; goto start;
temp = ctx->low_temp + temp_pos;
break;
} }
if (temp != ctx->cur_temp || ctx->new_output) { if (temp != ctx->cur_temp || ctx->new_output) {