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:
@@ -11,9 +11,11 @@ idf_component_register(
|
||||
"ui/assets/splash_screen/logo_iro.c"
|
||||
"ui/assets/fonts/Geist.c"
|
||||
"sensors/shake_detector.cpp"
|
||||
"sensors/glide_detector.c"
|
||||
"emotion-engine/emotion_manager.c"
|
||||
"emotion-engine/blink/blink_emotion.c"
|
||||
"emotion-engine/shake/shake_emotion.c"
|
||||
"emotion-engine/shy/shy_emotion.c"
|
||||
INCLUDE_DIRS
|
||||
"."
|
||||
"config"
|
||||
|
||||
@@ -173,6 +173,7 @@ static bool initI2cBus(void) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "I2C bus initialized successfully");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ static void touchInputCallback(lv_indev_drv_t *drv, lv_indev_data_t *data);
|
||||
|
||||
bool touchDriverInit(void) {
|
||||
ESP_LOGI(TAG, "Initializing touch controller");
|
||||
ESP_LOGI(TAG, "DEBUG: touchHandle before init: %p", (void*)touchHandle);
|
||||
|
||||
// Create I2C IO handle for touch controller
|
||||
esp_lcd_panel_io_handle_t tpIoHandle = NULL;
|
||||
@@ -50,6 +51,9 @@ bool touchDriverInit(void) {
|
||||
(esp_lcd_i2c_bus_handle_t)I2C_MASTER_PORT, &tpIoConfig, &tpIoHandle);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to create touch panel IO: %s", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "DEBUG: touchHandle after I2C IO failure: %p", (void*)touchHandle);
|
||||
// Ensure touchHandle is NULL on any initialization failure
|
||||
touchHandle = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -72,10 +76,16 @@ bool touchDriverInit(void) {
|
||||
},
|
||||
};
|
||||
|
||||
ESP_LOGI(TAG, "DEBUG: About to call esp_lcd_touch_new_i2c_ft5x06, current touchHandle: %p", (void*)touchHandle);
|
||||
err = esp_lcd_touch_new_i2c_ft5x06(tpIoHandle, &touchConfig, &touchHandle);
|
||||
ESP_LOGI(TAG, "DEBUG: esp_lcd_touch_new_i2c_ft5x06 returned err=%d, touchHandle=%p", err, (void*)touchHandle);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize touch controller: %s",
|
||||
esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "DEBUG: touchHandle after FT5x06 init failure: %p", (void*)touchHandle);
|
||||
// CRITICAL: Set touchHandle to NULL to prevent dangling pointer access
|
||||
// The esp_lcd_touch_new_i2c_ft5x06 function may set touchHandle to a freed pointer on error
|
||||
touchHandle = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -87,6 +97,7 @@ bool touchDriverInit(void) {
|
||||
lv_indev_drv_register(&inputDriver);
|
||||
|
||||
ESP_LOGI(TAG, "Touch controller initialized successfully");
|
||||
ESP_LOGI(TAG, "DEBUG: Final touchHandle: %p", (void*)touchHandle);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "drivers/display_driver.h"
|
||||
#include "emotion-engine/blink/blink_emotion.h"
|
||||
#include "emotion-engine/shake/shake_emotion.h"
|
||||
#include "emotion-engine/shy/shy_emotion.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char *TAG = "emotion_manager";
|
||||
@@ -40,6 +41,12 @@ bool emotionManagerInit(void) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize shy emotion
|
||||
if (!shyEmotionInit()) {
|
||||
ESP_LOGE(TAG, "Failed to initialize shy emotion");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Future: Initialize other emotions here
|
||||
// surprisedEmotionInit();
|
||||
// sadEmotionInit();
|
||||
@@ -101,6 +108,13 @@ void emotionManagerSetEmotion(EmotionType emotion) {
|
||||
ESP_LOGI(TAG, "Emotion %d not yet implemented", emotion);
|
||||
break;
|
||||
|
||||
case EMOTION_SHY:
|
||||
// Stop any other animations and play shy
|
||||
shakeEmotionStop();
|
||||
blinkEmotionPause();
|
||||
shyEmotionPlay();
|
||||
break;
|
||||
|
||||
default:
|
||||
ESP_LOGW(TAG, "Unknown emotion type: %d", emotion);
|
||||
break;
|
||||
@@ -117,6 +131,7 @@ void emotionManagerStop(void) {
|
||||
ESP_LOGI(TAG, "Stopping emotion manager");
|
||||
|
||||
blinkEmotionStop();
|
||||
shyEmotionStop();
|
||||
// Future: Stop other emotions
|
||||
|
||||
initialized = false;
|
||||
|
||||
@@ -32,6 +32,7 @@ typedef enum {
|
||||
EMOTION_SURPRISED, // Surprised expression
|
||||
EMOTION_SAD, // Sad expression
|
||||
EMOTION_ANGRY, // Angry expression
|
||||
EMOTION_SHY, // Shy expression
|
||||
EMOTION_COUNT
|
||||
} EmotionType;
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
140
main/emotion-engine/shy/shy_emotion.c
Normal file
140
main/emotion-engine/shy/shy_emotion.c
Normal file
File diff suppressed because one or more lines are too long
60
main/emotion-engine/shy/shy_emotion.h
Normal file
60
main/emotion-engine/shy/shy_emotion.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* @file shy_emotion.h
|
||||
* @brief Shy emotion animation controller
|
||||
*
|
||||
* Manages the shy Lottie animation that plays when the device
|
||||
* responds to touch with a shy expression.
|
||||
*/
|
||||
|
||||
#ifndef SHY_EMOTION_H
|
||||
#define SHY_EMOTION_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Initialize the shy emotion module
|
||||
*
|
||||
* @return true on success, false on failure
|
||||
*/
|
||||
bool shyEmotionInit(void);
|
||||
|
||||
/**
|
||||
* @brief Start playing the shy animation
|
||||
*
|
||||
* Animation will play once (not looping) and transition back to idle
|
||||
* when complete.
|
||||
*/
|
||||
void shyEmotionPlay(void);
|
||||
|
||||
/**
|
||||
* @brief Update the shy emotion state
|
||||
*
|
||||
* Must be called periodically to check if the shy animation has completed.
|
||||
*
|
||||
* @return true if animation is complete and ready for idle, false if still
|
||||
* playing
|
||||
*/
|
||||
bool shyEmotionUpdate(void);
|
||||
|
||||
/**
|
||||
* @brief Stop the shy animation immediately (for cleanup)
|
||||
*/
|
||||
void shyEmotionStop(void);
|
||||
|
||||
/**
|
||||
* @brief Check if shy animation is currently playing
|
||||
*
|
||||
* @return true if playing, false otherwise
|
||||
*/
|
||||
bool shyEmotionIsPlaying(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // SHY_EMOTION_H
|
||||
126
main/main.c
126
main/main.c
@@ -25,7 +25,9 @@
|
||||
#include "drivers/touch_driver.h"
|
||||
#include "emotion-engine/emotion_manager.h"
|
||||
#include "emotion-engine/shake/shake_emotion.h"
|
||||
#include "emotion-engine/shy/shy_emotion.h"
|
||||
#include "sensors/shake_detector.h"
|
||||
#include "sensors/glide_detector.h"
|
||||
#include "ui/calibration_screen.h"
|
||||
#include "ui/loading_anim.h"
|
||||
#include "ui/splash_screen.h"
|
||||
@@ -37,6 +39,106 @@
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
// Track touch initialization status
|
||||
static bool touchInitialized = false;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Glide Detection Callback
|
||||
// Triggers shy emotion when a glide gesture is detected.
|
||||
// Only triggers if currently in idle state for calm, subtle interactions.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static void onGlideDetected(const GlideEvent *event, void *user_data) {
|
||||
(void)event; // Unused parameter
|
||||
(void)user_data; // Unused parameter
|
||||
|
||||
// Only trigger shy emotion if currently in idle state
|
||||
if (emotionManagerGetCurrent() == EMOTION_IDLE) {
|
||||
ESP_LOGI(TAG, "Glide gesture detected, triggering shy emotion");
|
||||
if (displayDriverLock(500)) {
|
||||
emotionManagerSetEmotion(EMOTION_SHY);
|
||||
displayDriverUnlock();
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Failed to acquire lock for SHY emotion");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Glide Detection Task
|
||||
// Polls the touch controller for glide gestures and triggers shy emotion.
|
||||
// Only triggers shy emotion when currently in idle state.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static void glideTask(void *arg) {
|
||||
ESP_LOGI(TAG, "Glide detection task started");
|
||||
|
||||
// Check if touch was initialized successfully
|
||||
if (!touchInitialized) {
|
||||
ESP_LOGW(TAG, "Touch controller not initialized, glide detection disabled");
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
esp_lcd_touch_handle_t touchHandle = touchDriverGetHandle();
|
||||
if (touchHandle == NULL) {
|
||||
ESP_LOGE(TAG, "Touch handle is NULL even though initialization succeeded, disabling glide detection");
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Glide detection task running with touch handle %p", (void*)touchHandle);
|
||||
|
||||
while (1) {
|
||||
uint16_t x, y;
|
||||
uint8_t count = 0;
|
||||
|
||||
// Read touch data from controller
|
||||
esp_lcd_touch_read_data(touchHandle);
|
||||
bool pressed = esp_lcd_touch_get_coordinates(touchHandle, &x, &y, NULL, &count, 1);
|
||||
|
||||
// Feed touch events to glide detector
|
||||
glideDetectorUpdate(x, y, pressed && count > 0);
|
||||
|
||||
// Poll at 60Hz for responsive touch detection
|
||||
vTaskDelay(pdMS_TO_TICKS(16));
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Shy Emotion Task
|
||||
// Monitors shy emotion state and transitions back to idle when complete.
|
||||
// The shy emotion is a one-shot animation that plays once then returns to idle.
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static void shyTask(void *arg) {
|
||||
ESP_LOGI(TAG, "Shy emotion task started");
|
||||
|
||||
while (1) {
|
||||
// Check if shy emotion is currently playing
|
||||
if (shyEmotionIsPlaying()) {
|
||||
// Update shy emotion state
|
||||
bool animationComplete = false;
|
||||
if (displayDriverLock(100)) {
|
||||
animationComplete = shyEmotionUpdate();
|
||||
displayDriverUnlock();
|
||||
}
|
||||
|
||||
// Transition back to idle when animation completes
|
||||
if (animationComplete) {
|
||||
ESP_LOGI(TAG, "Shy animation complete, returning to idle");
|
||||
if (displayDriverLock(500)) {
|
||||
emotionManagerSetEmotion(EMOTION_IDLE);
|
||||
displayDriverUnlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Poll at 20Hz for emotion state updates
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Shake Detection Task
|
||||
// Polls the accelerometer for shake events and triggers emotions.
|
||||
@@ -138,6 +240,10 @@ static void ui_entry_task(void *arg) {
|
||||
// Initialize touch controller
|
||||
if (!touchDriverInit()) {
|
||||
ESP_LOGW(TAG, "Touch initialization failed (continuing)");
|
||||
touchInitialized = false;
|
||||
} else {
|
||||
touchInitialized = true;
|
||||
ESP_LOGI(TAG, "Touch controller initialized successfully");
|
||||
}
|
||||
|
||||
// Initialize shake detector
|
||||
@@ -145,6 +251,14 @@ static void ui_entry_task(void *arg) {
|
||||
ESP_LOGW(TAG, "Shake detector initialization failed (continuing)");
|
||||
}
|
||||
|
||||
// Initialize glide detector
|
||||
if (!glideDetectorInit()) {
|
||||
ESP_LOGW(TAG, "Glide detector initialization failed (continuing)");
|
||||
} else {
|
||||
// Register callback to trigger shy emotion on glide detection
|
||||
glideDetectorRegisterCallback(onGlideDetected, NULL);
|
||||
}
|
||||
|
||||
// Initialize NVS (Critical for first boot check)
|
||||
nvsDriverInit();
|
||||
|
||||
@@ -210,6 +324,18 @@ static void ui_entry_task(void *arg) {
|
||||
// Start shake detection task
|
||||
xTaskCreate(shakeTask, "shake_task", 4096, NULL, 5, NULL);
|
||||
|
||||
// Start glide detection task only if touch controller is initialized
|
||||
if (touchInitialized) {
|
||||
xTaskCreate(glideTask, "glide_task", 4096, NULL, 5, NULL);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Skipping glide detection task - touch controller not initialized");
|
||||
}
|
||||
|
||||
// Start shy emotion task
|
||||
// Use 8KB stack (same as ui_entry_task) to handle complex shy animation
|
||||
// Shy emotion has large JSON (~3KB) and 300 frames requiring more stack
|
||||
xTaskCreate(shyTask, "shy_task", 8192, NULL, 5, NULL);
|
||||
|
||||
ESP_LOGI(TAG, "iroBot Display System initialized successfully");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
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