Description
I have two variables (uint) that I want to change with just one button depending on the position of a switch button. The button should add a certain numerical value to the variable. This value is shown on the display using a label. I have tried a wide variety of methods. If statement for calling two different events. Calling the event and querying what the switch button is in. I manage to get one numerical value to change and be displayed. If I then change the switch, the label changes from the previous numerical value to the numerical value that is actually assigned to the other label.
What MCU/Processor/Board and compiler are you using?
Arduino IDE (latest), Arduino Giga R1 WiFi with Display Shield
What LVGL version are you using?
9.1.0
What do you want to achieve?
That I can use a switch and a button to change two variables based on the switch state.
What have you tried so far?
see above.
Code to reproduce
int setpressurecrucible = 234;
int setpressurespraychamber = 52;
bool switch_state = false;
uint helper_uint = 0;
uint cpressure_uint = 0;
uint spressure_uint = 0;
lv_obj_t* tile = lv_tileview_add_tile(tv, 0, 0, LV_DIR_BOTTOM);
lv_obj_t* lbl_cpressure = lv_label_create(tile);
lv_obj_t* arc = lv_arc_create(tile);
lv_obj_set_size(arc, 200, 200);
lv_arc_set_angles(arc, 150, 45);
lv_arc_set_value(arc, 62);
lv_arc_set_range(arc, 0, 2500);
lv_obj_remove_style(arc, NULL, LV_PART_KNOB);
lv_obj_align(arc, LV_ALIGN_TOP_LEFT, 30, 30);
lv_label_set_text_fmt(lbl_cpressure, "%d\nmbar", 1276);
lv_obj_set_width(lbl_cpressure, 100);
lv_obj_set_style_text_align(lbl_cpressure, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align_to(lbl_cpressure, arc, LV_ALIGN_CENTER, 0, -45);
lv_obj_set_style_text_font(lbl_cpressure, &lv_font_montserrat_32, 0);
lv_obj_add_event_cb(arc, value_changed_event_cb, LV_EVENT_VALUE_CHANGED, lbl_cpressure);
/*Manually update the label for the first time*/
lv_obj_send_event(arc, LV_EVENT_VALUE_CHANGED, NULL);
lv_obj_t* lbl_setcpressure = lv_label_create(tile);
lv_label_set_text_fmt(lbl_setcpressure, "(%d)", setpressurecrucible);
lv_obj_set_width(lbl_setcpressure, 100);
lv_obj_set_style_text_align(lbl_setcpressure, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align_to(lbl_setcpressure, arc, LV_ALIGN_CENTER, 0, 20);
lv_obj_set_style_text_font(lbl_setcpressure, &lv_font_montserrat_28, 0);
lv_obj_t* lbl_spressure = lv_label_create(tile);
lv_obj_t* arcs = lv_arc_create(tile);
lv_obj_set_size(arcs, 200, 200);
lv_arc_set_angles(arcs, 170, 310);
lv_arc_set_value(arcs, 62);
lv_arc_set_range(arcs, 0, 1500);
lv_obj_remove_style(arcs, NULL, LV_PART_KNOB);
lv_obj_align(arcs, LV_ALIGN_TOP_RIGHT, -30, 30);
lv_label_set_text_fmt(lbl_spressure, "%d\nmbar", 106);
lv_obj_set_width(lbl_spressure, 100);
lv_obj_set_style_text_align(lbl_spressure, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align_to(lbl_spressure, arcs, LV_ALIGN_CENTER, 0, -45);
lv_obj_set_style_text_font(lbl_spressure, &lv_font_montserrat_32, 0);
lv_obj_add_event_cb(arcs, value_changed_event_cb_s, LV_EVENT_VALUE_CHANGED, lbl_spressure);
/*Manually update the label for the first time*/
lv_obj_send_event(arcs, LV_EVENT_VALUE_CHANGED, NULL);
lv_obj_t* lbl_setspressure = lv_label_create(tile);
lv_label_set_text_fmt(lbl_setspressure, "(%d)", setpressurespraychamber);
lv_obj_set_width(lbl_setspressure, 100);
lv_obj_set_style_text_align(lbl_setspressure, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align_to(lbl_setspressure, arcs, LV_ALIGN_CENTER, 0, 20);
lv_obj_set_style_text_font(lbl_setspressure, &lv_font_montserrat_28, 0);
//---------------------------------
lv_obj_t* label;
static lv_obj_t* btn_5mbar = lv_button_create(tile);
lv_obj_add_event_cb(btn_5mbar, plus_5_event_handler, LV_EVENT_CLICKED, lbl_setcpressure);
lv_obj_align(btn_5mbar, LV_ALIGN_OUT_TOP_MID, 250, 30);
lv_obj_remove_flag(btn_5mbar, LV_OBJ_FLAG_PRESS_LOCK);
label = lv_label_create(btn_5mbar);
lv_label_set_text(label, "+5");
lv_obj_center(label);
lv_obj_set_style_text_font(label, &lv_font_montserrat_32, 0);
//----------------------------------
lv_obj_t* sw;
sw = lv_switch_create(tile);
lv_obj_add_event_cb(sw, sw_event_handler, LV_EVENT_VALUE_CHANGED, lbl_toggle);
lv_obj_add_flag(sw, LV_OBJ_FLAG_EVENT_BUBBLE);
lv_obj_align(sw, LV_ALIGN_CENTER, 0, -78);
lv_obj_t* label7;
label7 = lv_label_create(tile);
lv_label_set_text(label7, "Crucible");
lv_obj_center(label7);
lv_obj_align(label7, LV_ALIGN_CENTER, -70, -78);
lv_obj_t* label8;
label8 = lv_label_create(tile);
lv_label_set_text(label8, "Spraychamber");
lv_obj_center(label8);
lv_obj_align(label8, LV_ALIGN_CENTER, 90, -78);
static void plus_5_event_handler(lv_event_t* e) {
lv_event_code_t code = lv_event_get_code(e);
if (code == LV_EVENT_SHORT_CLICKED) {
if (!switch_state) {
lv_obj_t* lbl_setcpressure = (lv_obj_t*)lv_event_get_param(e);
setpressurecrucible = pressure_set(5);
lv_label_set_text_fmt(lbl_setcpressure, "(%d)", setpressurecrucible);
} else if (switch_state) {
lv_obj_t* lbl_setspressure = (lv_obj_t*)lv_event_get_param(e);
setpressurespraychamber = pressure_set(5);
lv_label_set_text_fmt(lbl_setspressure, "(%d)", setpressurespraychamber);
}
}
}
static void value_changed_event_cb(lv_event_t* e) {
lv_obj_t* arc = (lv_obj_t*)lv_event_get_target(e);
lv_obj_t* lbl_cpressure = (lv_obj_t*)lv_event_get_user_data(e);
helper_uint = lv_arc_get_value(arc);
cpressure_uint = helper_uint - 1000;
lv_label_set_text_fmt(lbl_cpressure, "%d\nmbar", cpressure_uint);
}
static void value_changed_event_cb_s(lv_event_t* e) {
lv_obj_t* arcs = (lv_obj_t*)lv_event_get_target(e);
lv_obj_t* lbl_spressure = (lv_obj_t*)lv_event_get_user_data(e);
helper_uint = lv_arc_get_value(arcs);
spressure_uint = helper_uint - 1000;
lv_label_set_text_fmt(lbl_spressure, "%d\nmbar", spressure_uint);
}
Screenshot and/or video
Not at hand right now