/** * @file glide_detector.c * @brief Glide gesture detection implementation for touch input * * This module implements glide gesture detection using touch coordinates. * It tracks consecutive touch points during a touch session and determines * if the motion qualifies as a glide gesture based on distance and timing. */ #include "sensors/glide_detector.h" #include "esp_log.h" #include "esp_timer.h" #include #include static const char *TAG = "glide_detector"; // ---------------------------------------------------------------------------- // Private State // ---------------------------------------------------------------------------- /** * @brief Detector state machine states */ typedef enum { GLIDE_STATE_IDLE = 0, // No touch detected GLIDE_STATE_TRACKING, // Touch detected, tracking movement GLIDE_STATE_GLIDE_DETECTED // Glide gesture recognized } GlideState; // Current detector state static GlideState state = GLIDE_STATE_IDLE; // Touch point buffer for tracking gesture static TouchPoint touchPoints[GLIDE_MAX_TOUCH_POINTS]; static uint8_t pointCount = 0; // Gesture timing static uint32_t gestureStartTime = 0; // Callback registration static GlideCallback glideCallback = NULL; static void *callbackUserData = NULL; // Statistics for debugging static float totalDistance = 0.0f; static float deltaX = 0.0f; static float deltaY = 0.0f; // ---------------------------------------------------------------------------- // Helper Functions // ---------------------------------------------------------------------------- /** * @brief Get current time in milliseconds */ static uint32_t getMillis(void) { return (uint32_t)(esp_timer_get_time() / 1000); } /** * @brief Calculate Euclidean distance between two points */ static float calculateDistance(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { float dx = (float)(x2 - x1); float dy = (float)(y2 - y1); return sqrtf(dx * dx + dy * dy); } /** * @brief Determine glide direction based on delta values */ static GlideDirection determineDirection(float dx, float dy) { float absDx = fabsf(dx); float absDy = fabsf(dy); // Use a ratio threshold to determine if motion is primarily horizontal, // vertical, or diagonal const float directionRatio = 0.5f; // 50% tolerance if (absDx > absDy * (1.0f + directionRatio)) { return GLIDE_DIRECTION_HORIZONTAL; } else if (absDy > absDx * (1.0f + directionRatio)) { return GLIDE_DIRECTION_VERTICAL; } else { return GLIDE_DIRECTION_DIAGONAL; } } /** * @brief Check if a new point is valid (not too close to previous point) */ static bool isValidNewPoint(uint16_t x, uint16_t y) { if (pointCount == 0) { return true; } TouchPoint *lastPoint = &touchPoints[pointCount - 1]; float distance = calculateDistance(x, y, lastPoint->x, lastPoint->y); // Require minimum movement between points to avoid jitter const float minPointDistance = 5.0f; // 5 pixels minimum return distance >= minPointDistance; } /** * @brief Check if gesture has timed out */ static bool isGestureTimedOut(uint32_t now) { if (pointCount == 0) { return false; } uint32_t elapsed = now - gestureStartTime; return elapsed > GLIDE_MAX_GESTURE_TIME; } /** * @brief Check if time between points is too long for continuous motion */ static bool isTimeBetweenPointsTooLong(uint32_t now) { if (pointCount < 2) { return false; } TouchPoint *lastPoint = &touchPoints[pointCount - 1]; uint32_t elapsed = now - lastPoint->timestamp; return elapsed > GLIDE_MAX_TIME_BETWEEN_POINTS; } /** * @brief Calculate total distance and delta for current gesture */ static void calculateGestureMetrics(void) { if (pointCount < 2) { totalDistance = 0.0f; deltaX = 0.0f; deltaY = 0.0f; return; } TouchPoint *firstPoint = &touchPoints[0]; TouchPoint *lastPoint = &touchPoints[pointCount - 1]; // Calculate total path distance totalDistance = 0.0f; for (uint8_t i = 1; i < pointCount; i++) { totalDistance += calculateDistance(touchPoints[i - 1].x, touchPoints[i - 1].y, touchPoints[i].x, touchPoints[i].y); } // Calculate net displacement deltaX = (float)(lastPoint->x - firstPoint->x); deltaY = (float)(lastPoint->y - firstPoint->y); } /** * @brief Check if current tracked points qualify as a glide */ static bool isGlideDetected(void) { if (pointCount < 2) { return false; } calculateGestureMetrics(); // Check minimum distance threshold if (totalDistance < GLIDE_MIN_DISTANCE) { return false; } return true; } /** * @brief Invoke the registered callback with glide event data */ static void invokeGlideCallback(void) { if (glideCallback == NULL) { return; } GlideEvent event = { .direction = determineDirection(deltaX, deltaY), .distance = totalDistance, .deltaX = deltaX, .deltaY = deltaY, .duration = getMillis() - gestureStartTime }; glideCallback(&event, callbackUserData); } // ---------------------------------------------------------------------------- // Public API Implementation // ---------------------------------------------------------------------------- bool glideDetectorInit(void) { ESP_LOGI(TAG, "Initializing glide detector"); // Reset state state = GLIDE_STATE_IDLE; pointCount = 0; gestureStartTime = 0; totalDistance = 0.0f; deltaX = 0.0f; deltaY = 0.0f; ESP_LOGI(TAG, "Glide detector initialized (min_distance=%d, max_time=%dms)", GLIDE_MIN_DISTANCE, GLIDE_MAX_GESTURE_TIME); return true; } bool glideDetectorUpdate(uint16_t x, uint16_t y, bool pressed) { uint32_t now = getMillis(); bool glideDetected = false; // Handle release event if (!pressed) { if (state == GLIDE_STATE_TRACKING) { // Check if we have a valid glide on release if (isGlideDetected()) { state = GLIDE_STATE_GLIDE_DETECTED; invokeGlideCallback(); glideDetected = true; ESP_LOGI(TAG, "Glide detected on release: dist=%.1f, delta=(%.1f,%.1f), " "dir=%d, duration=%lums", totalDistance, deltaX, deltaY, determineDirection(deltaX, deltaY), (unsigned long)(now - gestureStartTime)); } } // Reset state on any release glideDetectorReset(); return glideDetected; } // Handle press/move event switch (state) { case GLIDE_STATE_IDLE: // Start tracking on first touch point if (pointCount == 0) { touchPoints[pointCount].x = x; touchPoints[pointCount].y = y; touchPoints[pointCount].timestamp = now; pointCount++; gestureStartTime = now; state = GLIDE_STATE_TRACKING; ESP_LOGD(TAG, "Started tracking gesture at (%d,%d)", x, y); } break; case GLIDE_STATE_TRACKING: // Check for timeout if (isGestureTimedOut(now)) { ESP_LOGD(TAG, "Gesture timed out, resetting"); glideDetectorReset(); return false; } // Check if time between points is too long if (isTimeBetweenPointsTooLong(now)) { ESP_LOGD(TAG, "Time between points too long, resetting"); glideDetectorReset(); return false; } // Add new point if valid and buffer not full if (pointCount < GLIDE_MAX_TOUCH_POINTS && isValidNewPoint(x, y)) { touchPoints[pointCount].x = x; touchPoints[pointCount].y = y; touchPoints[pointCount].timestamp = now; pointCount++; // Check if we have a glide already (detect during motion) if (isGlideDetected()) { // Continue tracking to get more accurate metrics ESP_LOGD(TAG, "Glide in progress: dist=%.1f, points=%d", totalDistance, pointCount); } } break; case GLIDE_STATE_GLIDE_DETECTED: // Should not reach here - reset after detection glideDetectorReset(); break; } return glideDetected; } void glideDetectorRegisterCallback(GlideCallback callback, void *user_data) { glideCallback = callback; callbackUserData = user_data; ESP_LOGI(TAG, "Glide callback registered: %s", callback != NULL ? "yes" : "no"); } void glideDetectorReset(void) { if (state != GLIDE_STATE_IDLE) { ESP_LOGD(TAG, "Resetting detector (had %d points)", pointCount); } state = GLIDE_STATE_IDLE; pointCount = 0; gestureStartTime = 0; totalDistance = 0.0f; deltaX = 0.0f; deltaY = 0.0f; } uint8_t glideDetectorGetPointCount(void) { return pointCount; }