From cc7b56d13a5011195b55bd775796e72121d39887 Mon Sep 17 00:00:00 2001 From: Jakob Krantz Date: Sun, 26 Nov 2023 23:48:56 +0100 Subject: [PATCH] compass app: Add button to calibrate magnetometer. --- app/src/applications/compass/compass_app.c | 16 ++++++++----- app/src/applications/compass/compass_ui.c | 26 +++++++++++++++++++++- app/src/applications/compass/compass_ui.h | 4 +++- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/app/src/applications/compass/compass_app.c b/app/src/applications/compass/compass_app.c index 243c40e5..47d74ed9 100644 --- a/app/src/applications/compass/compass_app.c +++ b/app/src/applications/compass/compass_app.c @@ -15,6 +15,7 @@ static void compass_app_stop(void); // Functions related to app functionality static void timer_callback(lv_timer_t *timer); +static void on_start_calibration(void); LV_IMG_DECLARE(move); @@ -31,13 +32,9 @@ static uint32_t cal_start_ms; static void compass_app_start(lv_obj_t *root, lv_group_t *group) { - compass_ui_show(root); + compass_ui_show(root, on_start_calibration); refresh_timer = lv_timer_create(timer_callback, CONFIG_DEFAULT_CONFIGURATION_COMPASS_REFRESH_INTERVAL_MS, NULL); zsw_magnetometer_set_enable(true); - zsw_magnetometer_start_calibration(); - is_calibrating = true; - cal_start_ms = lv_tick_get(); - zsw_popup_show("Calibration", "Spin around 360 degrees for 10.", NULL, 100, false); } static void compass_app_stop(void) @@ -51,6 +48,15 @@ static void compass_app_stop(void) } } +static void on_start_calibration(void) +{ + zsw_magnetometer_start_calibration(); + is_calibrating = true; + cal_start_ms = lv_tick_get(); + zsw_popup_show("Calibration", + "Spin the watch around 360 degrees\nand do some random rotations in 3D space for 10 seconds.", NULL, 10, false); +} + static void timer_callback(lv_timer_t *timer) { if (is_calibrating && diff --git a/app/src/applications/compass/compass_ui.c b/app/src/applications/compass/compass_ui.c index 5f3ad47b..176f36ef 100644 --- a/app/src/applications/compass/compass_ui.c +++ b/app/src/applications/compass/compass_ui.c @@ -7,8 +7,20 @@ static lv_obj_t *compass_panel; static lv_obj_t *compass_img; static lv_obj_t *compass_label; +static on_start_calibraion_cb_t start_cal; + +static void calibrate_button_event_cb(lv_event_t *e) +{ + if (start_cal) { + start_cal(); + } +} + static void create_ui(lv_obj_t *parent) { + lv_obj_t *cal_btn; + lv_obj_t *cal_btn_label; + LV_IMG_DECLARE(cardinal_point) compass_panel = lv_obj_create(parent); lv_obj_set_width(compass_panel, 240); @@ -19,6 +31,16 @@ static void create_ui(lv_obj_t *parent) lv_obj_set_style_bg_opa(compass_panel, LV_OPA_TRANSP, LV_PART_MAIN | LV_STATE_DEFAULT); lv_obj_set_style_border_width(compass_panel, 0, LV_PART_MAIN | LV_STATE_DEFAULT); + cal_btn = lv_btn_create(compass_panel); + lv_obj_set_style_pad_all(cal_btn, 3, LV_PART_MAIN); + lv_obj_set_align(cal_btn, LV_ALIGN_CENTER); + lv_obj_set_pos(cal_btn, 0, 80); + lv_obj_set_size(cal_btn, 70, 25); + lv_obj_set_style_bg_color(cal_btn, lv_palette_main(LV_PALETTE_ORANGE), LV_PART_MAIN | LV_STATE_DEFAULT); + cal_btn_label = lv_label_create(cal_btn); + lv_label_set_text(cal_btn_label, "Calibrate"); + lv_obj_add_event_cb(cal_btn, calibrate_button_event_cb, LV_EVENT_CLICKED, NULL); + compass_img = lv_img_create(compass_panel); lv_img_set_src(compass_img, &cardinal_point); lv_obj_set_width(compass_img, LV_SIZE_CONTENT); @@ -36,7 +58,7 @@ static void create_ui(lv_obj_t *parent) lv_obj_set_style_text_opa(compass_label, 255, LV_PART_MAIN | LV_STATE_DEFAULT); } -void compass_ui_show(lv_obj_t *root) +void compass_ui_show(lv_obj_t *root, on_start_calibraion_cb_t start_cal_cb) { assert(root_page == NULL); @@ -48,6 +70,8 @@ void compass_ui_show(lv_obj_t *root) lv_obj_set_size(root_page, LV_PCT(100), LV_PCT(100)); lv_obj_clear_flag(root_page, LV_OBJ_FLAG_SCROLLABLE); + start_cal = start_cal_cb; + create_ui(root_page); } diff --git a/app/src/applications/compass/compass_ui.h b/app/src/applications/compass/compass_ui.h index 50cbe5cf..9ed5b9ea 100644 --- a/app/src/applications/compass/compass_ui.h +++ b/app/src/applications/compass/compass_ui.h @@ -3,7 +3,9 @@ #include #include -void compass_ui_show(lv_obj_t *root); +typedef void(*on_start_calibraion_cb_t)(void); + +void compass_ui_show(lv_obj_t *root, on_start_calibraion_cb_t start_cal_cb); void compass_ui_remove(void);