Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep leftover movement after aplying dividor #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dts/bindings/pixart,pmw3610.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ properties:
snipe-layers:
type: array
default: []
text-layers:
type: array
default: []
automouse-layer:
type: int
default: -1
4 changes: 3 additions & 1 deletion src/pixart.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
extern "C" {
#endif

enum pixart_input_mode { MOVE = 0, SCROLL, SNIPE };
enum pixart_input_mode { MOVE = 0, SCROLL, SNIPE, TEXT };

/* device data structure */
struct pixart_data {
Expand Down Expand Up @@ -59,6 +59,8 @@ struct pixart_config {
int32_t *scroll_layers;
size_t snipe_layers_len;
int32_t *snipe_layers;
size_t text_layers_len;
int32_t *text_layers;
};

#ifdef __cplusplus
Expand Down
86 changes: 70 additions & 16 deletions src/pmw3610.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
// adapted from https://stackoverflow.com/questions/70802306/convert-a-12-bit-signed-number-in-c
#define TOINT16(val, bits) (((struct { int16_t value : bits; }){val}).value)

#include <drivers/behavior.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/input/input.h>
#include <zmk/keymap.h>
#include <zmk/behavior_queue.h>
#include "pmw3610.h"

#include <zephyr/logging/log.h>
Expand Down Expand Up @@ -59,8 +61,26 @@ static int (*const async_init_fn[ASYNC_INIT_STEP_COUNT])(const struct device *de
[ASYNC_INIT_STEP_CONFIGURE] = pmw3610_async_init_configure,
};

struct move_precision_leftover {
uint16_t x;
uint16_t y;
};
static struct move_precision_leftover leftover = {
.x = 0,
.y = 0
};

//////// Function definitions //////////

static inline void tap_key(uint32_t param1) {
struct zmk_behavior_binding behavior = {
.behavior_dev = "key_press",
.param1 = param1,
};
zmk_behavior_queue_add(0, behavior, true, 1);
zmk_behavior_queue_add(0, behavior, false, 1);
}

// checked and keep
static int spi_cs_ctrl(const struct device *dev, bool enable) {
const struct pixart_config *config = dev->config;
Expand Down Expand Up @@ -574,6 +594,11 @@ static enum pixart_input_mode get_input_mode_for_current_layer(const struct devi
return SNIPE;
}
}
for (size_t i = 0; i < config->text_layers_len; i++) {
if (curr_layer == config->text_layers[i]) {
return TEXT;
}
}
return MOVE;
}

Expand Down Expand Up @@ -606,6 +631,14 @@ static int pmw3610_report_data(const struct device *dev) {
set_cpi_if_needed(dev, CONFIG_PMW3610_SNIPE_CPI);
dividor = CONFIG_PMW3610_SNIPE_CPI_DIVIDOR;
break;
case TEXT:
set_cpi_if_needed(dev, CONFIG_PMW3610_CPI);
if (input_mode_changed) {
data->scroll_delta_x = 0;
data->scroll_delta_y = 0;
}
dividor = 1; // this should be handled with the ticks rather than dividors
break;
default:
return -ENOTSUP;
}
Expand All @@ -626,25 +659,29 @@ static int pmw3610_report_data(const struct device *dev) {
}

int16_t raw_x =
TOINT16((buf[PMW3610_X_L_POS] + ((buf[PMW3610_XY_H_POS] & 0xF0) << 4)), 12) / dividor;
TOINT16((buf[PMW3610_X_L_POS] + ((buf[PMW3610_XY_H_POS] & 0xF0) << 4)), 12) + leftover.x;
leftover.x = raw_x % dividor;
int16_t adjusted_x = raw_x / dividor;
int16_t raw_y =
TOINT16((buf[PMW3610_Y_L_POS] + ((buf[PMW3610_XY_H_POS] & 0x0F) << 8)), 12) / dividor;
TOINT16((buf[PMW3610_Y_L_POS] + ((buf[PMW3610_XY_H_POS] & 0x0F) << 8)), 12) + leftover.y;
leftover.y = raw_y % dividor;
int16_t adjusted_y = raw_y / dividor;

int16_t x;
int16_t y;

if (IS_ENABLED(CONFIG_PMW3610_ORIENTATION_0)) {
x = -raw_x;
y = raw_y;
x = -adjusted_x;
y = adjusted_y;
} else if (IS_ENABLED(CONFIG_PMW3610_ORIENTATION_90)) {
x = raw_y;
y = -raw_x;
x = adjusted_y;
y = -adjusted_x;
} else if (IS_ENABLED(CONFIG_PMW3610_ORIENTATION_180)) {
x = raw_x;
y = -raw_y;
x = adjusted_x;
y = -adjusted_y;
} else if (IS_ENABLED(CONFIG_PMW3610_ORIENTATION_270)) {
x = -raw_y;
y = raw_x;
x = -adjusted_y;
y = adjusted_x;
}

if (IS_ENABLED(CONFIG_PMW3610_INVERT_X)) {
Expand Down Expand Up @@ -688,25 +725,39 @@ static int pmw3610_report_data(const struct device *dev) {
#endif

if (x != 0 || y != 0) {
if (input_mode != SCROLL) {
input_report_rel(dev, INPUT_REL_X, x, false, K_FOREVER);
input_report_rel(dev, INPUT_REL_Y, y, true, K_FOREVER);
} else {
if (input_mode == SCROLL) {
data->scroll_delta_x += x;
data->scroll_delta_y += y;
if (abs(data->scroll_delta_y) > CONFIG_PMW3610_SCROLL_TICK) {
input_report_rel(dev, INPUT_REL_WHEEL,
data->scroll_delta_y > 0 ? PMW3610_SCROLL_Y_NEGATIVE : PMW3610_SCROLL_Y_POSITIVE,
true, K_FOREVER);
data->scroll_delta_x = 0;
//data->scroll_delta_x = 0;
data->scroll_delta_y = 0;
} else if (abs(data->scroll_delta_x) > CONFIG_PMW3610_SCROLL_TICK) {
}
if (abs(data->scroll_delta_x) > CONFIG_PMW3610_SCROLL_TICK) {
input_report_rel(dev, INPUT_REL_HWHEEL,
data->scroll_delta_x > 0 ? PMW3610_SCROLL_X_NEGATIVE : PMW3610_SCROLL_X_POSITIVE,
true, K_FOREVER);
data->scroll_delta_x = 0;
//data->scroll_delta_y = 0;
}
} else if (input_mode == TEXT) {
data->scroll_delta_x += x;
data->scroll_delta_y += y;
if (abs(data->scroll_delta_x) > CONFIG_PMW3610_SCROLL_TICK) {
tap_key(data->scroll_delta_x > 0 ? 0x4F : 0x50);
data->scroll_delta_x = 0;
data->scroll_delta_y = 0;
}
if (abs(data->scroll_delta_y) > CONFIG_PMW3610_SCROLL_TICK) {
tap_key(data->scroll_delta_y > 0 ? 0x51 : 0x52);
data->scroll_delta_y = 0;
data->scroll_delta_y = 0;
}
} else {
input_report_rel(dev, INPUT_REL_X, x, false, K_FOREVER);
input_report_rel(dev, INPUT_REL_Y, y, true, K_FOREVER);
}
}

Expand Down Expand Up @@ -815,6 +866,7 @@ static int pmw3610_init(const struct device *dev) {
static struct pixart_data data##n; \
static int32_t scroll_layers##n[] = DT_PROP(DT_DRV_INST(n), scroll_layers); \
static int32_t snipe_layers##n[] = DT_PROP(DT_DRV_INST(n), snipe_layers); \
static int32_t text_layers##n[] = DT_PROP(DT_DRV_INST(n), text_layers); \
static const struct pixart_config config##n = { \
.irq_gpio = GPIO_DT_SPEC_INST_GET(n, irq_gpios), \
.bus = \
Expand All @@ -833,6 +885,8 @@ static int pmw3610_init(const struct device *dev) {
.scroll_layers_len = DT_PROP_LEN(DT_DRV_INST(n), scroll_layers), \
.snipe_layers = snipe_layers##n, \
.snipe_layers_len = DT_PROP_LEN(DT_DRV_INST(n), snipe_layers), \
.text_layers = text_layers##n, \
.text_layers_len = DT_PROP_LEN(DT_DRV_INST(n), text_layers), \
}; \
\
DEVICE_DT_INST_DEFINE(n, pmw3610_init, NULL, &data##n, &config##n, POST_KERNEL, \
Expand Down