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.c

114 lines
2.5 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_analog.h"
#include "app_knob.h"
#include "app_buzzer.h"
#include "app_heater.h"
static struct App {
float oven_temp;
int16_t set_temp;
int16_t wheel_normed;
uint16_t wheel;
bool heating;
} s_app = {};
static void hw_init()
{
app_analog_init();
app_buzzer_init();
app_heater_init();
app_knob_init();
/* Prepare the framebuffer and OLED interface */
oled_init();
fb_clear();
}
void app_main_task(void *argument)
{
hw_init();
/* Infinite loop */
for (;;) {
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
s_app.oven_temp = app_analog_get_temp();
for (int i = 0; i < 50; i++) {
uint16_t old_wheel = s_app.wheel;
s_app.wheel = app_knob_get_raw();
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();
}
}
//s_app.push = 0 == HAL_GPIO_ReadPin(KNOB_PUSH_GPIO_Port, KNOB_PUSH_Pin);
if (wheel_change != 0 || i == 0) {
fb_clear();
char tmp[100];
sprintf(tmp, "Mereni: %d°C", (int) s_app.oven_temp);
fb_text(10, 10, tmp, 0, 1);
sprintf(tmp, " Cil: %d°C", s_app.set_temp);
fb_text(10, 25, tmp, 0, 1);
if (s_app.heating) {
fb_frame(0, 0, FBW, FBH, 2, 1);
}
fb_blit();
}
vTaskDelay(10);
}
// regulation
float set_f = (float) s_app.set_temp;
if (!s_app.heating && s_app.oven_temp < set_f - 5.0f) { /* hysteresis */
s_app.heating = true;
}
if (s_app.heating && s_app.oven_temp >= set_f) {
s_app.heating = false;
}
app_heater_set(s_app.heating);
// feed dogs
HAL_IWDG_Refresh(&hiwdg);
}
}