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.
 
 
 
toaster-oven-bluepill/Core/Src/app_main.c

122 lines
2.7 KiB

/**
* Main task
*/
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "app.h"
#include "ufb/framebuffer.h"
#include "iwdg.h"
#include "oled.h"
#include "ufb/fb_text.h"
#include "app_temp.h"
#include "app_knob.h"
#include "app_buzzer.h"
#include "app_heater.h"
#include "cmsis_os2.h"
static struct App {
float oven_temp;
int16_t set_temp;
int16_t wheel_normed;
uint16_t wheel;
bool run;
} s_app = {};
static void redraw_display() {
fb_clear();
char tmp[100];
sprintf(tmp, "Mereni: %d°C", (int) s_app.oven_temp);
fb_text(10, 5, tmp, 0, 1);
sprintf(tmp, " Cil: %d°C", s_app.set_temp);
fb_text(10, 20, tmp, 0, 1);
sprintf(tmp, " Stav: %s", s_app.run ? "ZAP" : "VYP");
fb_text(10, 35, tmp, 0, 1);
if (s_app.run) {
fb_frame(0, 0, FBW, FBH, 2, 1);
}
fb_blit();
}
void app_task_main(void *argument)
{
app_analog_init();
app_buzzer_init();
app_knob_init();
/* Prepare the framebuffer and OLED interface */
oled_init();
fb_clear();
/* Infinite loop */
bool old_pushed = app_knob_pushed();
bool any_change = true;
uint32_t last_redraw = osKernelGetTickCount();
for (;;) {
// sampling is done in the heater loop
s_app.oven_temp = app_temp_read_oven();
uint16_t old_wheel = s_app.wheel;
s_app.wheel = app_knob_get_raw();
// TODO do this with interrupt and/or debouncing
bool pushed = app_knob_pushed();
if (pushed && !old_pushed) {
s_app.run ^= 1;
app_heater_enable(s_app.run);
app_buzzer_beep();
any_change = true;
}
old_pushed = pushed;
int16_t wheel_change = (int16_t)(s_app.wheel - old_wheel);
if (wheel_change != 0) {
s_app.wheel_normed += wheel_change;
if (s_app.wheel_normed < 0) {
s_app.wheel_normed = 0;
}
if (s_app.wheel_normed > 500) {
s_app.wheel_normed = 500;
}
int16_t old_temp = s_app.set_temp;
s_app.set_temp = (s_app.wheel_normed / 2) * 5;
if (old_temp != s_app.set_temp) {
app_buzzer_beep();
app_heater_set_target((float) s_app.set_temp);
any_change = true;
}
}
uint32_t now = osKernelGetTickCount();
if (any_change || (now - last_redraw > pdMS_TO_TICKS(500))) {
last_redraw = now;
redraw_display();
any_change = false;
// Blink
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
}
vTaskDelay(pdMS_TO_TICKS(10));
// feed dogs
HAL_IWDG_Refresh(&hiwdg);
}
}