You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
zavlaha-kzk/src/screens/screen_cyklus.c

87 lines
2.3 KiB

/**
* Zavlahovy cuklus
*/
//
// Created by MightyPork on 2023/04/09.
//
#include <stdio.h>
#include "app_gui.h"
#include "gui_event.h"
#include "app_io.h"
#include "app_config.h"
#define MIN_CYCLE_TIME_S 70 // more than 60
static int phase = 0;
static uint32_t phase_seconds = 0;
static uint32_t phase_seconds_max = 0;
static void end_cycle()
{
open_valve(0);
}
static void start_branch()
{
phase_seconds_max = ((uint32_t) app_config.circuit_base_time_s * (uint32_t) app_config.circuit_time_percent[phase]) / 100;
open_valve(phase + 1);
}
void screen_cyklus(GuiEvent event)
{
switch (event) {
case GUI_EVENT_SCREEN_INIT:
phase = 0;
phase_seconds = 0;
s_app.last_cycle_time.hour = s_app.rtc_time.hour;
s_app.last_cycle_time.minute = s_app.rtc_time.minute;
s_app.cycle_time_checking = true;
start_branch();
break;
case GUI_EVENT_SCREEN_TICK_1S:
phase_seconds += 1;
if (phase_seconds >= phase_seconds_max) {
phase += 1;
if (phase >= CIRCUIT_COUNT) {
end_cycle();
switch_screen(screen_home, true);
return;
}
start_branch();
phase_seconds = 0;
}
request_paint();
break;
case GUI_EVENT_PAINT:
LcdBuffer_Write(&lcd, 0, 0, "==ZÁVLAHOVÝ CYKLUS==");
LcdBuffer_Write(&lcd, 3, 0, "🅳 Ukončit");
snprintf(sbuf, sbuf_len, "Okruh %d/%d %3d/%d s ", phase + 1, 4, phase_seconds, phase_seconds_max);
LcdBuffer_Write(&lcd, 1, 0, sbuf);
uint32_t pieces = (phase_seconds * 40) / phase_seconds_max;
char *buf = sbuf;
for (int i = 0; i < 20; i++) {
if ((i * 2 + 1) == pieces) {
buf += sprintf(buf, "");
} else if (i * 2 < pieces) {
buf += sprintf(buf, "");
} else {
buf += sprintf(buf, " ");
}
}
LcdBuffer_Write(&lcd, 2, 0, sbuf);
break;
case GUI_EVENT_KEY_D:
end_cycle();
switch_screen(screen_home, true);
return;
}
}