add rudimentary calib param tuning screen (TODO negative offset support!)

This commit is contained in:
2023-05-07 00:53:22 +02:00
parent 6b94dcb5ee
commit 1ea4cd124d
14 changed files with 432 additions and 28 deletions
+3
View File
@@ -146,6 +146,9 @@ static void draw_common_overlay() {
if (app_heater_get_state()) {
fb_frame(0, 0, FBW, 11, 1, 1);
float perc = app_heater_get_percent();
fb_hline(0, 12, (fbpos_t) ((float)FBW * (perc / 100.0f)), 1);
}
}
+1
View File
@@ -43,6 +43,7 @@ void screen_manual(GuiEvent event);
void screen_manual_menu(GuiEvent event);
void screen_calibration(GuiEvent event);
void screen_pid_tuning(GuiEvent event);
void screen_calib_edit(GuiEvent event);
struct State {
/// Latest oven temp readout
+217
View File
@@ -0,0 +1,217 @@
//
// Created by MightyPork on 2023/05/06
//
#include <string.h>
#include "app_gui.h"
#include "app_heater.h"
#include "ufb/fb_text.h"
#include "snprintf.h"
#include "app_temp.h"
static void apply_calib(bool temporary);
static struct calib_tuning_state {
uint8_t digits[6 * 4]; // a b l r
int cursor; // the digit under cursor (range 0-17)
bool digit_editing; // true if we are editing a digit
} s_tuning;
static void draw_digit_row(int row) {
fbpos_t x = (FBW - (6 * 7 - 1)) / 2;
fbpos_t y = 30 + row * 10;
char buf2[2] = {};
int numofs = row * 6;
const char * labels[4] = {"a","b","L", "R"};
const int decimal_pos[4] = {2, 0, 3, 3};
fb_text(FBW - 4, y + 2, labels[row], FONT_3X5, 1);
bool significant = row == 1;
for (int i = 0; i < 6; i++) {
if (i >= decimal_pos[row]) significant = 1;
int val = s_tuning.digits[numofs + i];
if (val != 0) {
significant = true;
}
buf2[0] = '0' + val;
if (s_tuning.cursor == numofs + i) {
if (s_tuning.digit_editing) {
fb_rect(x-1, y-1, 5+2, 7+2, 1);
fb_text(x, y, buf2, 0, 0);
} else {
fb_hline(x, y + 8, 5, 1);
if (significant) fb_text(x, y, buf2, 0, 1);
}
} else {
if (significant) fb_text(x, y, buf2, 0, 1);
}
x += 6;
if (i == decimal_pos[row]) {
fb_text(x, y, ".", 0, 1);
x += 6;
}
}
}
void screen_calib_edit(GuiEvent event)
{
float A, B, L, R;
uint32_t Ai, Bi, Li, Ri;
if (event == GUI_EVENT_SCREEN_INIT) {
memset(&s_tuning, 0, sizeof(s_tuning));
app_temp_get_calib(&A, &B, &L, &R);
app_temp_backup_calib();
Ai = (uint32_t)(A * 1000.f);
Bi = (uint32_t)(B * 100000.f);
Li = (uint32_t)(L * 100.f);
Ri = (uint32_t)(R * 100.f);
char buf[25];
SNPRINTF(buf, 25, "%06lu%06lu%06lu%06lu", Ai, Bi, Li, Ri);
for(int i = 0; i < 6*4; i++) {
s_tuning.digits[i] = buf[i] - '0';
}
}
switch (event) {
case GUI_EVENT_PAINT: {
fb_text(FBW / 2, 16, "Kalibrace", TEXT_CENTER, 1);
draw_digit_row(0);
draw_digit_row(1);
draw_digit_row(2);
draw_digit_row(3);
//68
#define BTNS_TOP 72
if (s_tuning.cursor == 24) {
fb_rect(0, BTNS_TOP, FBW, 9, 1);
}
fb_text(FBW / 2, BTNS_TOP + 1, "Zrušit", TEXT_CENTER, s_tuning.cursor != 24);
if (s_tuning.cursor == 25) {
fb_rect(0, BTNS_TOP + 9, FBW, 9, 1);
}
fb_text(FBW / 2, BTNS_TOP + 1 + 9, "Uložit", TEXT_CENTER, s_tuning.cursor != 25);
fb_text(2, FBH - 8 * (1 + (s_tuning.cursor < 24)), s_tuning.digit_editing ? "←→Hodnota" : "←→Kurzor", 0, 1);
if (s_tuning.cursor < 24) {
fb_text(2, FBH - 8 * 1, s_tuning.digit_editing ? "> Potvrdit" : "> Změnit", 0, 1);
}
return;
}
case GUI_EVENT_KNOB_PLUS: {
input_sound_effect();
request_paint();
if (s_tuning.digit_editing) {
if (s_tuning.digits[s_tuning.cursor] == 9) {
s_tuning.digits[s_tuning.cursor] = 0;
} else {
s_tuning.digits[s_tuning.cursor]++;
}
apply_calib(false);
} else {
// 24 - cancel
// 25 - save
s_tuning.cursor++;
if (s_tuning.cursor >= 24) {
s_tuning.digit_editing = false;
}
if (s_tuning.cursor > 25) {
s_tuning.cursor = 0;
}
}
break;
}
case GUI_EVENT_KNOB_MINUS: {
input_sound_effect();
request_paint();
if (s_tuning.digit_editing) {
if (s_tuning.digits[s_tuning.cursor] == 0) {
s_tuning.digits[s_tuning.cursor] = 9;
} else {
s_tuning.digits[s_tuning.cursor]--;
}
apply_calib(false);
} else {
// 24 - cancel
// 25 - save
if (s_tuning.cursor == 0) {
s_tuning.cursor = 25;
s_tuning.digit_editing = false;
} else {
s_tuning.cursor--;
}
}
break;
}
case GUI_EVENT_KNOB_RELEASE: {
if (s_tuning.cursor < 24) {
s_tuning.digit_editing = !s_tuning.digit_editing;
} else if (s_tuning.cursor == 24) {
// cancel
app_temp_restore_calib();
switch_screen(screen_home, true);
} else if (s_tuning.cursor == 25) {
// save
apply_calib(true);
switch_screen(screen_home, true);
}
break;
}
}
}
static void apply_calib(bool temporary) {
float A, B, L, R;
uint32_t Ai, Bi, Li, Ri;
Ai = Bi = Li = Ri = 0;
for(int i = 0; i < 6; i++) {
Ai *= 10;
Ai += s_tuning.digits[i];
}
for(int i = 6; i < 12; i++) {
Bi *= 10;
Bi += s_tuning.digits[i];
}
for(int i = 12; i < 18; i++) {
Li *= 10;
Li += s_tuning.digits[i];
}
for(int i = 18; i < 24; i++) {
Ri *= 10;
Ri += s_tuning.digits[i];
}
A = ((float) Ai) / 1000.f;
B = ((float) Bi) / 100000.f;
L = ((float) Li) / 100.f;
R = ((float) Ri) / 100.f;
if (temporary) {
app_temp_set_calib_temporary(A, B);
app_temp_set_calib_temporary_r(L, R);
} else {
app_temp_set_calib_persistent(A, B);
app_temp_set_calib_persistent_r(L, R);
}
}
+1 -1
View File
@@ -215,7 +215,7 @@ void screen_calibration(GuiEvent event)
float a = (corrected1 - corrected2) / (s_calib.sample1 - s_calib.sample2);
float b = corrected1 - a * s_calib.sample1;
app_temp_set_persistent_calib(a, b);
app_temp_set_calib_persistent(a, b);
} else if (s_calib.phase == PH_SAMPLE1) {
app_heater_set_target(100);
app_heater_enable(true);
+5 -2
View File
@@ -12,8 +12,8 @@
static const char* main_menu_opts[] = {
"Ruční režim",
"Ladění PID",
"Kalibrace",
// "Diagnostika",
"Auto kalibrace",
"Ruční kalibrace",
NULL
};
@@ -28,6 +28,9 @@ static void main_menu_cb(int opt) {
case 2:
switch_screen(screen_calibration, true);
break;
case 3:
switch_screen(screen_calib_edit, true);
break;
}
}
+4 -5
View File
@@ -2,17 +2,13 @@
// Created by MightyPork on 2023/04/09.
//
#include <stddef.h>
#include <string.h>
#include "app_gui.h"
#include "app_heater.h"
#include "screen_menu.h"
#include "ufb/fb_text.h"
#include "snprintf.h"
#include "FreeRTOS.h"
struct pid_tuning_state {
static struct calib_tuning_state {
uint8_t digits[6 * 3];
// uint32_t Kp, Ki, Kd; // these are the float x 1000
@@ -29,6 +25,9 @@ static void draw_digit_row(int row) {
int numofs = row * 6;
const char * labels[3] = {"P","I","D"};
fb_text(FBW - 4, y + 2, labels[row], FONT_3X5, 1);
bool significant = false;
for (int i = 0; i < 6; i++) {
if (i >= 2) significant = 1;