Skip to content

Commit

Permalink
compass app: Add button to calibrate magnetometer.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkra committed Nov 26, 2023
1 parent 94ea45e commit cc7b56d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
16 changes: 11 additions & 5 deletions app/src/applications/compass/compass_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)
Expand All @@ -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 &&
Expand Down
26 changes: 25 additions & 1 deletion app/src/applications/compass/compass_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion app/src/applications/compass/compass_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <inttypes.h>
#include <lvgl.h>

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);

Expand Down

0 comments on commit cc7b56d

Please sign in to comment.