Files
spincoat-plater-firmware/main/ui.c
2025-12-31 11:56:54 -05:00

99 lines
3.0 KiB
C

#include "ui.h"
// #include "lv_font.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "motor.h"
#define BTN_INCREMENT 100
static const char *TAG = "spincoat-plater-firmware/ui";
static lv_obj_t * rpm_label = NULL;
void top_cb(lv_event_t * e) {
update_throttle(get_throttle() + BTN_INCREMENT);
lv_label_set_text_fmt(rpm_label, "RPM: %d", get_throttle());
}
void bottom_cb(lv_event_t * e) {
uint16_t throttle = get_throttle();
if(throttle >= BTN_INCREMENT) {
update_throttle(get_throttle() - BTN_INCREMENT);
lv_label_set_text_fmt(rpm_label, "RPM: %d", get_throttle());
}
}
void build_numberstack(lv_obj_t * parent,
const char * label_text,
lv_obj_t ** label_value,
ns_btn_cb_t top_btn_cb,
ns_btn_cb_t bottom_btn_cb) {
lv_obj_t * container = lv_obj_create(parent);
lv_obj_set_size(container , lv_pct(100), lv_pct(100));
lv_obj_set_flex_flow(container, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(container,
LV_FLEX_ALIGN_START,
LV_FLEX_ALIGN_CENTER,
LV_FLEX_ALIGN_CENTER);
lv_obj_set_style_pad_all(container, 10, 0);
lv_obj_set_style_pad_gap(container, 8, 0);
/* -------- Top label -------- */
lv_obj_t * label_cont = lv_obj_create(container);
lv_obj_set_flex_grow(label_cont, 1);
lv_obj_set_width(label_cont, lv_pct(100));
lv_obj_t * label = lv_label_create(label_cont);
lv_label_set_text(label, label_text);
lv_obj_set_style_bg_opa(label, LV_OPA_COVER, 0);
lv_obj_set_style_text_font(label, &lv_font_montserrat_14, 0);
lv_obj_center(label);
/* -------- Top button -------- */
lv_obj_t * btn_top = lv_button_create(container);
lv_obj_t * lbl_top = lv_label_create(btn_top);
lv_label_set_text(lbl_top, "+100");
lv_obj_add_event_cb(btn_top, top_cb, LV_EVENT_CLICKED, (void *) 1);
lv_obj_set_flex_grow(btn_top, 1);
lv_obj_set_width(btn_top, lv_pct(100));
lv_obj_center(lbl_top);
/* -------- Center label -------- */
lv_obj_t * label2_cont = lv_obj_create(container);
lv_obj_set_flex_grow(label2_cont, 1);
lv_obj_set_width(label2_cont, lv_pct(100));
* label_value = lv_label_create(label2_cont);
lv_label_set_text(*label_value, "");
lv_obj_set_style_bg_opa(*label_value, LV_OPA_COVER, 0);
lv_obj_set_style_text_font(*label_value, &lv_font_montserrat_14, 0);
lv_obj_center(*label_value);
/* -------- Bottom button -------- */
lv_obj_t * btn_bottom = lv_button_create(container);
lv_obj_t * lbl_bottom = lv_label_create(btn_bottom);
lv_label_set_text(lbl_bottom, "-100");
lv_obj_add_event_cb(btn_bottom, bottom_cb, LV_EVENT_CLICKED, (void *) 2);
lv_obj_set_flex_grow(btn_bottom, 1);
lv_obj_set_width(btn_bottom, lv_pct(100));
lv_obj_center(lbl_bottom);
}
void build_ui(void) {
rpm_label = NULL;
build_numberstack(lv_screen_active(), "RPM", &rpm_label, top_cb, bottom_cb);
lv_label_set_text_fmt(rpm_label, "RPM: %d", get_throttle());
}