1
0
Fork 0

Cirque trackpad features: circular scroll, inertial cursor (#17482)

This commit is contained in:
Daniel Kao 2022-07-12 21:17:40 -07:00 committed by GitHub
parent 904ec0ce78
commit 5db705d054
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 1122 additions and 148 deletions

View file

@ -31,6 +31,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# include "drivers/sensors/analog_joystick.h"
#elif defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_i2c) || defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi)
# include "drivers/sensors/cirque_pinnacle.h"
# include "drivers/sensors/cirque_pinnacle_gestures.h"
# include "pointing_device_gestures.h"
#elif defined(POINTING_DEVICE_DRIVER_pimoroni_trackball)
# include "i2c_master.h"
# include "drivers/sensors/pimoroni_trackball.h"

View file

@ -97,27 +97,48 @@ const pointing_device_driver_t pointing_device_driver = {
// clang-format on
#elif defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_i2c) || defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi)
# ifndef CIRQUE_PINNACLE_TAPPING_TERM
# include "action.h"
# include "action_tapping.h"
# define CIRQUE_PINNACLE_TAPPING_TERM GET_TAPPING_TERM(KC_BTN1, &(keyrecord_t){})
# endif
# ifndef CIRQUE_PINNACLE_TOUCH_DEBOUNCE
# define CIRQUE_PINNACLE_TOUCH_DEBOUNCE (CIRQUE_PINNACLE_TAPPING_TERM * 8)
# ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
static bool cursor_glide_enable = true;
static cursor_glide_context_t glide = {.config = {
.coef = 102, /* Good default friction coef */
.interval = 10, /* 100sps */
.trigger_px = 10, /* Default threshold in case of hover, set to 0 if you'd like */
}};
void cirque_pinnacle_enable_cursor_glide(bool enable) {
cursor_glide_enable = enable;
}
void cirque_pinnacle_configure_cursor_glide(float trigger_px) {
glide.config.trigger_px = trigger_px;
}
# endif
report_mouse_t cirque_pinnacle_get_report(report_mouse_t mouse_report) {
pinnacle_data_t touchData = cirque_pinnacle_read_data();
mouse_xy_report_t report_x = 0, report_y = 0;
static mouse_xy_report_t x = 0, y = 0;
static uint16_t mouse_timer = 0;
static bool is_z_down = false;
# ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
cursor_glide_t glide_report = {0};
if (cursor_glide_enable) {
glide_report = cursor_glide_check(&glide);
}
# endif
# if !CIRQUE_PINNACLE_POSITION_MODE
# error Cirque Pinnacle with relative mode not implemented yet.
# endif
if (!touchData.valid) {
# ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
if (cursor_glide_enable && glide_report.valid) {
report_x = glide_report.dx;
report_y = glide_report.dy;
goto mouse_report_update;
}
# endif
return mouse_report;
}
@ -130,45 +151,51 @@ report_mouse_t cirque_pinnacle_get_report(report_mouse_t mouse_report) {
// Scale coordinates to arbitrary X, Y resolution
cirque_pinnacle_scale_data(&touchData, cirque_pinnacle_get_scale(), cirque_pinnacle_get_scale());
if (x && y && touchData.xValue && touchData.yValue) {
report_x = (mouse_xy_report_t)(touchData.xValue - x);
report_y = (mouse_xy_report_t)(touchData.yValue - y);
if (!cirque_pinnacle_gestures(&mouse_report, touchData)) {
if (x && y && touchData.xValue && touchData.yValue) {
report_x = (mouse_xy_report_t)(touchData.xValue - x);
report_y = (mouse_xy_report_t)(touchData.yValue - y);
}
x = touchData.xValue;
y = touchData.yValue;
# ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
if (cursor_glide_enable) {
if (touchData.touchDown) {
cursor_glide_update(&glide, report_x, report_y, touchData.zValue);
} else if (!glide_report.valid) {
glide_report = cursor_glide_start(&glide);
if (glide_report.valid) {
report_x = glide_report.dx;
report_y = glide_report.dy;
}
}
}
# endif
}
x = touchData.xValue;
y = touchData.yValue;
# ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
mouse_report_update:
# endif
mouse_report.x = report_x;
mouse_report.y = report_y;
if (touchData.touchDown != is_z_down) {
is_z_down = touchData.touchDown;
if (!touchData.zValue) {
if (timer_elapsed(mouse_timer) < CIRQUE_PINNACLE_TAPPING_TERM && mouse_timer != 0) {
mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, true, POINTING_DEVICE_BUTTON1);
pointing_device_set_report(mouse_report);
pointing_device_send();
# if TAP_CODE_DELAY > 0
wait_ms(TAP_CODE_DELAY);
# endif
mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1);
pointing_device_set_report(mouse_report);
pointing_device_send();
}
}
mouse_timer = timer_read();
}
if (timer_elapsed(mouse_timer) > (CIRQUE_PINNACLE_TOUCH_DEBOUNCE)) {
mouse_timer = 0;
}
return mouse_report;
}
uint16_t cirque_pinnacle_get_cpi(void) {
return CIRQUE_PINNACLE_PX_TO_INCH(cirque_pinnacle_get_scale());
}
void cirque_pinnacle_set_cpi(uint16_t cpi) {
cirque_pinnacle_set_scale(CIRQUE_PINNACLE_INCH_TO_PX(cpi));
}
// clang-format off
const pointing_device_driver_t pointing_device_driver = {
.init = cirque_pinnacle_init,
.get_report = cirque_pinnacle_get_report,
.set_cpi = cirque_pinnacle_set_scale,
.get_cpi = cirque_pinnacle_get_scale
.set_cpi = cirque_pinnacle_set_cpi,
.get_cpi = cirque_pinnacle_get_cpi
};
// clang-format on

View file

@ -0,0 +1,133 @@
/* Copyright 2022 Daniel Kao <daniel.m.kao@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "pointing_device_gestures.h"
#include "timer.h"
#ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
# ifdef POINTING_DEVICE_MOTION_PIN
# error POINTING_DEVICE_MOTION_PIN not supported when using inertial cursor. Need repeated calls to get_report() to generate glide events.
# endif
static void cursor_glide_stop(cursor_glide_context_t* glide) {
memset(&glide->status, 0, sizeof(glide->status));
}
static cursor_glide_t cursor_glide(cursor_glide_context_t* glide) {
cursor_glide_status_t* status = &glide->status;
cursor_glide_t report;
int32_t p;
int32_t x, y;
if (status->v0 == 0) {
report.dx = 0;
report.dy = 0;
report.valid = false;
cursor_glide_stop(glide);
goto exit;
}
status->counter++;
/* Calculate current 1D position */
p = status->v0 * status->counter - (int32_t)glide->config.coef * status->counter * status->counter / 2;
/*
* Translate to x & y axes
* Done this way instead of applying friction to each axis separately, so we don't end up with the shorter axis stuck at 0 towards the end of diagonal movements.
*/
x = (int32_t)(p * status->dx0 / status->v0);
y = (int32_t)(p * status->dy0 / status->v0);
report.dx = (mouse_xy_report_t)(x - status->x);
report.dy = (mouse_xy_report_t)(y - status->y);
report.valid = true;
if (report.dx <= 1 && report.dx >= -1 && report.dy <= 1 && report.dy >= -1) {
/* Stop gliding once speed is low enough */
cursor_glide_stop(glide);
goto exit;
}
status->x = x;
status->y = y;
status->timer = timer_read();
exit:
return report;
}
cursor_glide_t cursor_glide_check(cursor_glide_context_t* glide) {
cursor_glide_t invalid_report = {0, 0, false};
cursor_glide_status_t* status = &glide->status;
if (status->z || (status->dx0 == 0 && status->dy0 == 0) || timer_elapsed(status->timer) < glide->config.interval) {
return invalid_report;
} else {
return cursor_glide(glide);
}
}
static inline uint16_t sqrt32(uint32_t x) {
uint32_t l, m, h;
if (x == 0) {
return 0;
} else if (x > (UINT16_MAX >> 2)) {
/* Safe upper bound to avoid integer overflow with m * m */
h = UINT16_MAX;
} else {
/* Upper bound based on closest log2 */
h = (1 << (((__builtin_clzl(1) - __builtin_clzl(x) + 1) + 1) >> 1));
}
/* Lower bound based on closest log2 */
l = (1 << ((__builtin_clzl(1) - __builtin_clzl(x)) >> 1));
/* Binary search to find integer square root */
while (l != h - 1) {
m = (l + h) / 2;
if (m * m <= x) {
l = m;
} else {
h = m;
}
}
return l;
}
cursor_glide_t cursor_glide_start(cursor_glide_context_t* glide) {
cursor_glide_t invalid_report = {0, 0, false};
cursor_glide_status_t* status = &glide->status;
status->timer = timer_read();
status->counter = 0;
status->v0 = (status->dx0 == 0 && status->dy0 == 0) ? 0.0 : sqrt32(((int32_t)status->dx0 * 256 * status->dx0 * 256) + ((int32_t)status->dy0 * 256 * status->dy0 * 256)); // skip trigonometry if not needed, calculate distance in Q8
status->x = 0;
status->y = 0;
status->z = 0;
if (status->v0 < ((uint32_t)glide->config.trigger_px * 256)) { /* Q8 comparison */
/* Not enough velocity to be worth gliding, abort */
cursor_glide_stop(glide);
return invalid_report;
}
return cursor_glide(glide);
}
void cursor_glide_update(cursor_glide_context_t* glide, mouse_xy_report_t dx, mouse_xy_report_t dy, uint16_t z) {
cursor_glide_status_t* status = &glide->status;
status->dx0 = dx;
status->dy0 = dy;
status->z = z;
}
#endif

View file

@ -0,0 +1,58 @@
/* Copyright 2022 Daniel Kao <daniel.m.kao@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include "report.h"
#ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE
typedef struct {
mouse_xy_report_t dx;
mouse_xy_report_t dy;
bool valid;
} cursor_glide_t;
typedef struct {
uint16_t trigger_px; /* Pixels of movement needed to trigger cursor glide */
uint16_t coef; /* Coefficient of friction */
uint16_t interval; /* Glide report interval, in milliseconds */
} cursor_glide_config_t;
typedef struct {
int32_t v0;
int32_t x;
int32_t y;
uint16_t z;
uint16_t timer;
uint16_t counter;
mouse_xy_report_t dx0;
mouse_xy_report_t dy0;
} cursor_glide_status_t;
typedef struct {
cursor_glide_config_t config;
cursor_glide_status_t status;
} cursor_glide_context_t;
/* Check glide report conditions, calculates glide coordinates */
cursor_glide_t cursor_glide_check(cursor_glide_context_t* glide);
/* Start glide reporting, gives first set of glide coordinates */
cursor_glide_t cursor_glide_start(cursor_glide_context_t* glide);
/* Update glide engine on the latest cursor movement, cursor glide is based on the final movement */
void cursor_glide_update(cursor_glide_context_t* glide, mouse_xy_report_t dx, mouse_xy_report_t dy, uint16_t z);
#endif