update: fixed shake animation
The shake animation figures have been fixed. However, the petting emotion working with touch remains an issue. The Lottie JSON for the emotion is unable not parsable for some reason and my next step is to fix that for tomorrow's meeting.
This commit is contained in:
317
main/sensors/glide_detector.c
Normal file
317
main/sensors/glide_detector.c
Normal file
@@ -0,0 +1,317 @@
|
||||
/**
|
||||
* @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 <math.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
136
main/sensors/glide_detector.h
Normal file
136
main/sensors/glide_detector.h
Normal file
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* @file glide_detector.h
|
||||
* @brief Glide gesture detection interface for touch input
|
||||
*
|
||||
* This module provides glide gesture detection using touch coordinates.
|
||||
* It detects when a finger glides across the screen with continuous motion
|
||||
* and can trigger emotional responses in the display system.
|
||||
*/
|
||||
|
||||
#ifndef GLIDE_DETECTOR_H
|
||||
#define GLIDE_DETECTOR_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Configuration
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Minimum distance (in pixels) to qualify as a glide gesture
|
||||
#define GLIDE_MIN_DISTANCE 80
|
||||
|
||||
// Maximum time (in milliseconds) between touch points to consider continuous
|
||||
#define GLIDE_MAX_TIME_BETWEEN_POINTS 300
|
||||
|
||||
// Maximum time (in milliseconds) for a complete glide gesture
|
||||
#define GLIDE_MAX_GESTURE_TIME 1000
|
||||
|
||||
// Maximum number of touch points to track per gesture
|
||||
#define GLIDE_MAX_TOUCH_POINTS 32
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Type Definitions
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Glide direction types
|
||||
*/
|
||||
typedef enum {
|
||||
GLIDE_DIRECTION_NONE = 0, // No direction
|
||||
GLIDE_DIRECTION_HORIZONTAL, // Horizontal glide (left or right)
|
||||
GLIDE_DIRECTION_VERTICAL, // Vertical glide (up or down)
|
||||
GLIDE_DIRECTION_DIAGONAL // Diagonal glide
|
||||
} GlideDirection;
|
||||
|
||||
/**
|
||||
* @brief Touch point data structure
|
||||
*/
|
||||
typedef struct {
|
||||
uint16_t x; // X coordinate
|
||||
uint16_t y; // Y coordinate
|
||||
uint32_t timestamp; // Timestamp in milliseconds
|
||||
} TouchPoint;
|
||||
|
||||
/**
|
||||
* @brief Glide event data
|
||||
*/
|
||||
typedef struct {
|
||||
GlideDirection direction; // Direction of glide
|
||||
float distance; // Total distance in pixels
|
||||
float deltaX; // X displacement
|
||||
float deltaY; // Y displacement
|
||||
uint32_t duration; // Gesture duration in milliseconds
|
||||
} GlideEvent;
|
||||
|
||||
/**
|
||||
* @brief Glide event callback function type
|
||||
*
|
||||
* @param event Pointer to the glide event data
|
||||
* @param user_data User-provided data passed during registration
|
||||
*/
|
||||
typedef void (*GlideCallback)(const GlideEvent *event, void *user_data);
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Public API
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Initialize the glide detector
|
||||
*
|
||||
* Initializes internal state and prepares the detector for touch events.
|
||||
* Call this once during system initialization.
|
||||
*
|
||||
* @return true on successful initialization, false on failure
|
||||
*/
|
||||
bool glideDetectorInit(void);
|
||||
|
||||
/**
|
||||
* @brief Update the glide detector with a touch event
|
||||
*
|
||||
* Should be called for each touch event (press, move, release) from the
|
||||
* touch input system. The detector tracks touch points and determines if
|
||||
* a glide gesture has occurred.
|
||||
*
|
||||
* @param x Touch X coordinate (0 to DISPLAY_WIDTH)
|
||||
* @param y Touch Y coordinate (0 to DISPLAY_HEIGHT)
|
||||
* @param pressed true if touch is pressed, false if released
|
||||
* @return true if a glide was detected in this update, false otherwise
|
||||
*/
|
||||
bool glideDetectorUpdate(uint16_t x, uint16_t y, bool pressed);
|
||||
|
||||
/**
|
||||
* @brief Register a callback for glide events
|
||||
*
|
||||
* The callback will be invoked when a glide gesture is detected.
|
||||
* Only one callback can be registered at a time.
|
||||
*
|
||||
* @param callback Function pointer to the callback
|
||||
* @param user_data User data to pass to the callback (can be NULL)
|
||||
*/
|
||||
void glideDetectorRegisterCallback(GlideCallback callback, void *user_data);
|
||||
|
||||
/**
|
||||
* @brief Reset the glide detector state
|
||||
*
|
||||
* Clears all tracked touch points and resets the state machine to IDLE.
|
||||
* Use this to abort an in-progress gesture or after a timeout.
|
||||
*/
|
||||
void glideDetectorReset(void);
|
||||
|
||||
/**
|
||||
* @brief Get the current number of tracked touch points
|
||||
*
|
||||
* @return Number of touch points currently being tracked
|
||||
*/
|
||||
uint8_t glideDetectorGetPointCount(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // GLIDE_DETECTOR_H
|
||||
@@ -21,8 +21,8 @@ extern "C" {
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// Acceleration deviation threshold to detect shake (in G)
|
||||
// Set above 1G to account for gravity and require actual vigorous shaking
|
||||
#define SHAKE_ACCEL_THRESHOLD 1.3f
|
||||
// Set above 2.2 to require vigorous shaking
|
||||
#define SHAKE_ACCEL_THRESHOLD 2.2f
|
||||
|
||||
// Number of consecutive readings above threshold to confirm shake
|
||||
#define SHAKE_CONTINUOUS_THRESHOLD 10
|
||||
|
||||
Reference in New Issue
Block a user