Skip to content

Optimization

Barryblueice edited this page Dec 31, 2025 · 1 revision

DeadZone (Touch Dead Zone):

#define TAP_DEADZONE 20
...
int dx = abs((int)rx - (int)origin_x[id]);
int dy = abs((int)ry - (int)origin_y[id]);

if (dx > TAP_DEADZONE && dy > TAP_DEADZONE) {
    current_state.fingers[id].x = origin_x[id];
    current_state.fingers[id].y = origin_y[id];
} else {
    current_state.fingers[id].x = fx;
    current_state.fingers[id].y = fy;
}

A touch dead zone is used to eliminate slight jitter and prevent Windows from misinterpreting the initial finger contact as movement.

If the finger movement distance is greater than the DeadZone threshold, the origin coordinates are returned.


First-Order Filtering (Low-Pass Filter):

First-order filter formula:y_new = $\alpha$ * $x_\text{current}$ + (1 - $\alpha$) * $y_\text{last}$

  • $y_\text{new}$:filtered current value
  • $x_\text{current}$:current input value
  • $y_\text{last}$:filtered value from the previous frame
  • $\alpha$:filter coefficient(0~1), A larger value results in faster response.

Implementation:

float dynamic_alpha = (dist > 30.0f) ? 0.8f : 0.3f;
filtered_x[id] = (dynamic_alpha * (float)rx) + ((1.0f - dynamic_alpha) * filtered_x[id]);
filtered_y[id] = (dynamic_alpha * (float)ry) + ((1.0f - dynamic_alpha) * filtered_y[id]);

dynamic_alpha is adjusted dynamically based on movement distance:

  • Large finger movement (fast motion): higher α → faster response
  • Small finger movement (minor jitter): lower α → smoother output

This first-order filtering ensures smooth finger movement and good visual experience, while preventing cursor jitter caused by touchpad noise.


Jump Threshold (Sudden Change Suppression):

if (last_raw_x[id] != 0 && abs((int)rx - (int)last_raw_x[id]) > JUMP_THRESHOLD) {
    current_state.fingers[id].x = last_raw_x[id];
    current_state.fingers[id].y = last_raw_y[id];
}

If the difference between two consecutive readings exceeds the jump suppression threshold, the current value is discarded and the previous frame’s coordinates are retained.

This prevents abnormal jumps caused by chip noise or I²C read errors, improving system robustness and avoiding sudden cursor jumps or gesture anomalies.


Settling (Stabilization Period):

if (active_count > 0 && active_count > 4 && (now - first_touch_time > SETTLING_MS)) {
    continue; 
}

first_touch_time records the initial touch timestamp. During a short settling period (SETTLING_MS), incomplete touch data is ignored.

This avoids false multi-touch detection during initial contact and improves the stability of multi-finger gesture recognition.


Button Locking:

static uint8_t locked_button = 0;
static bool is_detecting_click = false;

if (physical_pressed) {
    if (!is_detecting_click) {
        is_detecting_click = true;
        locked_button = (raw_x > 1700) ? 0x02 : 0x01;
    }
    msg_out->button_mask = locked_button;
} else {
    is_detecting_click = false;
    locked_button = 0;
    msg_out->button_mask = 0;
}

Implements physical button click locking. Once a button is detected, its state is locked until the finger is lifted, preventing repeated triggers within a short time window.

Clone this wiki locally