/** * @file calibration_screen.c * @brief IMU calibration screen implementation * * This module provides the first-boot calibration experience. It displays * a full-screen UI with the Geist font prompting the user to place the * device on a flat surface and tap to calibrate. The calibration captures * the IMU baseline and stores it to NVS for persistence. */ #include "ui/calibration_screen.h" #include "drivers/display_driver.h" #include "drivers/nvs_driver.h" #include "sensors/shake_detector.h" #include "ui/assets/fonts/Geist.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "lvgl.h" static const char *TAG = "calibration_screen"; // ---------------------------------------------------------------------------- // Private State // ---------------------------------------------------------------------------- static volatile bool tapDetected = false; // ---------------------------------------------------------------------------- // Private Functions // ---------------------------------------------------------------------------- /** * @brief Touch event callback for detecting user tap */ static void onScreenTap(lv_event_t *event) { lv_event_code_t code = lv_event_get_code(event); if (code == LV_EVENT_CLICKED) { tapDetected = true; } } // ---------------------------------------------------------------------------- // Public API Implementation // ---------------------------------------------------------------------------- bool calibrationScreenShow(void) { ESP_LOGI(TAG, "Showing calibration screen"); tapDetected = false; // Create calibration screen lv_obj_t *calibScreen = lv_obj_create(NULL); if (calibScreen == NULL) { ESP_LOGE(TAG, "Failed to create calibration screen"); return false; } // Black background with no border lv_obj_set_style_bg_color(calibScreen, lv_color_black(), LV_PART_MAIN | LV_STATE_DEFAULT); lv_obj_set_style_bg_opa(calibScreen, LV_OPA_COVER, LV_PART_MAIN | LV_STATE_DEFAULT); lv_obj_set_style_border_width(calibScreen, 0, LV_PART_MAIN | LV_STATE_DEFAULT); lv_obj_set_style_pad_all(calibScreen, 0, LV_PART_MAIN | LV_STATE_DEFAULT); // Make the screen clickable to detect taps lv_obj_add_flag(calibScreen, LV_OBJ_FLAG_CLICKABLE); lv_obj_add_event_cb(calibScreen, onScreenTap, LV_EVENT_CLICKED, NULL); // Main instruction text - minimalist white on black lv_obj_t *mainLabel = lv_label_create(calibScreen); lv_label_set_text(mainLabel, "Place device on\na flat surface"); lv_obj_set_style_text_font(mainLabel, &Geist, LV_PART_MAIN | LV_STATE_DEFAULT); lv_obj_set_style_text_color(mainLabel, lv_color_white(), LV_PART_MAIN | LV_STATE_DEFAULT); lv_obj_set_style_text_align(mainLabel, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT); lv_obj_set_style_text_line_space(mainLabel, 12, LV_PART_MAIN | LV_STATE_DEFAULT); lv_obj_align(mainLabel, LV_ALIGN_CENTER, 0, -60); // Sub instruction text - subtle gray lv_obj_t *subLabel = lv_label_create(calibScreen); lv_label_set_text(subLabel, "Tap to calibrate"); lv_obj_set_style_text_font(subLabel, &Geist, LV_PART_MAIN | LV_STATE_DEFAULT); lv_obj_set_style_text_color(subLabel, lv_color_make(100, 100, 100), LV_PART_MAIN | LV_STATE_DEFAULT); lv_obj_set_style_text_align(subLabel, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT); lv_obj_align(subLabel, LV_ALIGN_CENTER, 0, 80); // Load and display the calibration screen lv_scr_load(calibScreen); lv_refr_now(NULL); ESP_LOGI(TAG, "Waiting for user tap..."); // Release the display lock so LVGL timer handler can process touch events displayDriverUnlock(); // Wait for tap - LVGL timer handler runs in background task while (!tapDetected) { vTaskDelay(pdMS_TO_TICKS(50)); } // Reacquire lock for UI updates displayDriverLock(-1); ESP_LOGI(TAG, "Tap detected, starting calibration..."); // Update UI to show calibrating state lv_label_set_text(mainLabel, "Calibrating..."); lv_obj_align(mainLabel, LV_ALIGN_CENTER, 0, 0); lv_obj_add_flag(subLabel, LV_OBJ_FLAG_HIDDEN); lv_refr_now(NULL); // Release lock during calibration (sensor reads) displayDriverUnlock(); // Perform calibration bool calibSuccess = shakeDetectorCalibrateBaseline(); // Reacquire lock for final UI updates displayDriverLock(-1); if (calibSuccess) { // Get the calibrated baseline values float baseX, baseY, baseZ; shakeDetectorGetBaseline(&baseX, &baseY, &baseZ); ESP_LOGI(TAG, "Calibration complete: x=%.3f y=%.3f z=%.3f", baseX, baseY, baseZ); // Store to NVS if (nvsDriverSaveImuCalibration(baseX, baseY, baseZ)) { ESP_LOGI(TAG, "Calibration saved to NVS"); } else { ESP_LOGW(TAG, "Failed to save calibration to NVS"); } // Show success lv_label_set_text(mainLabel, "Done!"); lv_refr_now(NULL); displayDriverUnlock(); vTaskDelay(pdMS_TO_TICKS(800)); displayDriverLock(-1); } else { ESP_LOGE(TAG, "Calibration failed"); lv_label_set_text(mainLabel, "Calibration failed"); lv_refr_now(NULL); displayDriverUnlock(); vTaskDelay(pdMS_TO_TICKS(1500)); displayDriverLock(-1); } // Clean up - create blank screen before deleting calibration screen lv_obj_t *blankScreen = lv_obj_create(NULL); if (blankScreen != NULL) { lv_obj_set_style_bg_color(blankScreen, lv_color_black(), 0); lv_obj_set_style_bg_opa(blankScreen, LV_OPA_COVER, 0); lv_scr_load(blankScreen); } lv_obj_del(calibScreen); ESP_LOGI(TAG, "Calibration screen complete"); return calibSuccess; }