From 960e46caa5b124bd7d919f0578317b3f97cd7043 Mon Sep 17 00:00:00 2001 From: daskygit <32983009+daskygit@users.noreply.github.com> Date: Sat, 31 Jul 2021 17:14:48 +0100 Subject: [PATCH 1/6] rejigger --- drivers/sensors/pimoroni_trackball.c | 169 ++++++++++++++++----------- drivers/sensors/pimoroni_trackball.h | 20 +++- 2 files changed, 116 insertions(+), 73 deletions(-) diff --git a/drivers/sensors/pimoroni_trackball.c b/drivers/sensors/pimoroni_trackball.c index c0ac644f708e..4cea7e6f0a6b 100644 --- a/drivers/sensors/pimoroni_trackball.c +++ b/drivers/sensors/pimoroni_trackball.c @@ -1,4 +1,5 @@ /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) + * Copyright 2021 Dasky (@daskygit) * * 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 @@ -13,41 +14,66 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - #include "pimoroni_trackball.h" #include "i2c_master.h" +#include "print.h" -static uint8_t scrolling = 0; -static int16_t x_offset = 0; -static int16_t y_offset = 0; -static int16_t h_offset = 0; -static int16_t v_offset = 0; -static float precisionSpeed = 1; +#define TRACKBALL_TIMEOUT 100 +#define TRACKBALL_REG_LED_RED 0x00 +#define TRACKBALL_REG_LED_GRN 0x01 +#define TRACKBALL_REG_LED_BLU 0x02 +#define TRACKBALL_REG_LED_WHT 0x03 +#define TRACKBALL_REG_LEFT 0x04 +#define TRACKBALL_REG_RIGHT 0x05 +#define TRACKBALL_REG_UP 0x06 +#define TRACKBALL_REG_DOWN 0x07 -static uint16_t i2c_timeout_timer; +static pimoroni_data current_pimoroni_data; +static report_mouse_t mouse_report; +static bool scrolling = false; +static int16_t x_offset = 0; +static int16_t y_offset = 0; +static int16_t h_offset = 0; +static int16_t v_offset = 0; +static uint16_t precision = 128; -#ifndef PIMORONI_I2C_TIMEOUT -# define PIMORONI_I2C_TIMEOUT 100 -#endif -#ifndef I2C_WAITCHECK -# define I2C_WAITCHECK 1000 -#endif -#ifndef MOUSE_DEBOUNCE -# define MOUSE_DEBOUNCE 5 +float trackball_get_precision(void) { return (float)(precision >> 7); } +void trackball_set_precision(float floatprecision) { precision = (floatprecision * 128); } +bool trackball_is_scrolling(void) { return scrolling; } +void trackball_set_scrolling(bool scroll) { scrolling = scroll; } + +void trackball_set_rgbw(uint8_t r, uint8_t g, uint8_t b, uint8_t w) { + uint8_t data[4] = {r, g, b, w}; + __attribute__((unused)) i2c_status_t status = i2c_writeReg(TRACKBALL_ADDRESS << 1, TRACKBALL_REG_LED_RED, data, sizeof(data), TRACKBALL_TIMEOUT); + dprintf("Trackball RGBW i2c_status_t: %d\n", status); +} + +void read_pimoroni_trackball(pimoroni_data* data) { + __attribute__((unused)) i2c_status_t status = i2c_readReg(TRACKBALL_ADDRESS << 1, TRACKBALL_REG_LEFT, (uint8_t*)data, sizeof(*data), TRACKBALL_TIMEOUT); +#ifdef TRACKBALL_DEBUG_READ + dprintf("Trackball READ i2c_status_t: %d\nLeft: %d\nRight: %d\nUp: %d\nDown: %d\nSwtich: %d\n", status, data->left, data->right, data->up, data->down, data->click); #endif +} -void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) { - uint8_t data[] = {0x00, red, green, blue, white}; - i2c_transmit(TRACKBALL_WRITE, data, sizeof(data), PIMORONI_I2C_TIMEOUT); +__attribute__((weak)) void pointing_device_init(void) { + i2c_init(); + trackball_set_rgbw(0x00, 0x00, 0x00, 0x00); } -int16_t mouse_offset(uint8_t positive, uint8_t negative, int16_t scale) { - int16_t offset = (int16_t)positive - (int16_t)negative; - int16_t magnitude = (int16_t)(scale * offset * offset * precisionSpeed); - return offset < 0 ? -magnitude : magnitude; +int16_t get_offsets(uint8_t negative_dir, uint8_t positive_dir, uint8_t scale) { + uint8_t offset = 0; + bool isnegative = false; + if (negative_dir > positive_dir) { + offset = negative_dir - positive_dir; + isnegative = true; + } else { + offset = positive_dir - negative_dir; + } + uint16_t magnitude = (scale * offset * offset * precision) >> 7; + return isnegative ? -(int16_t)(magnitude) : (int16_t)(magnitude); } -void update_member(int8_t* member, int16_t* offset) { +void update_values(int8_t* member, int16_t* offset) { if (*offset > 127) { *member = 127; *offset -= 127; @@ -68,73 +94,78 @@ __attribute__((weak)) void trackball_check_click(bool pressed, report_mouse_t* m } } -float trackball_get_precision(void) { return precisionSpeed; } -void trackball_set_precision(float precision) { precisionSpeed = precision; } -bool trackball_is_scrolling(void) { return scrolling; } -void trackball_set_scrolling(bool scroll) { scrolling = scroll; } +void pointing_device_task() { + static fast_timer_t throttle = 0; + static uint8_t debounce = 0; + + if (timer_elapsed_fast(throttle) > TRACKBALL_INTERVAL_MS) { + static pimoroni_data last_read_data = {0}; + mouse_report = pointing_device_get_report(); + read_pimoroni_trackball(¤t_pimoroni_data); -__attribute__((weak)) void pointing_device_init(void) { i2c_init(); trackball_set_rgbw(0x00, 0x00, 0x00, 0x00); } + if (!debounce) { + if (memcmp(&last_read_data, ¤t_pimoroni_data, sizeof(current_pimoroni_data))) { + memcpy(&last_read_data, ¤t_pimoroni_data, sizeof(current_pimoroni_data)); -void pointing_device_task(void) { - static bool debounce; - static uint16_t debounce_timer; - uint8_t state[5] = {}; - if (timer_elapsed(i2c_timeout_timer) > I2C_WAITCHECK) { - if (i2c_readReg(TRACKBALL_READ, 0x04, state, 5, PIMORONI_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) { - if (!state[4] && !debounce) { +#ifdef PIMORONI_TRACKBALL_CLICK + if (current_pimoroni_data.click & 128) { + trackball_check_click(true, &mouse_report); + debounce = TRACKBALL_DEBOUNCE_ONCLICK; + } else { + trackball_check_click(false, &mouse_report); + } +#endif if (scrolling) { #ifdef PIMORONI_TRACKBALL_INVERT_X - h_offset += mouse_offset(state[2], state[3], 1); + h_offset += get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, 5); #else - h_offset -= mouse_offset(state[2], state[3], 1); + h_offset -= get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, 5); #endif #ifdef PIMORONI_TRACKBALL_INVERT_Y - v_offset += mouse_offset(state[1], state[0], 1); + v_offset += get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, 5); #else - v_offset -= mouse_offset(state[1], state[0], 1); + v_offset -= get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, 5); #endif } else { #ifdef PIMORONI_TRACKBALL_INVERT_X - x_offset -= mouse_offset(state[2], state[3], 5); + x_offset -= get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, 5); #else - x_offset += mouse_offset(state[2], state[3], 5); + x_offset += get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, 5); #endif #ifdef PIMORONI_TRACKBALL_INVERT_Y - y_offset -= mouse_offset(state[1], state[0], 5); + y_offset -= get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, 5); #else - y_offset += mouse_offset(state[1], state[0], 5); + y_offset += get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, 5); #endif } - } else { - if (state[4]) { - debounce = true; - debounce_timer = timer_read(); - } } } else { - i2c_timeout_timer = timer_read(); + debounce--; } - } - - if (timer_elapsed(debounce_timer) > MOUSE_DEBOUNCE) debounce = false; - - report_mouse_t mouse = pointing_device_get_report(); - -#ifdef PIMORONI_TRACKBALL_CLICK - trackball_check_click(state[4] & (1 << 7), &mouse); + if (scrolling) { +#ifndef PIMORONI_TRACKBALL_ROTATE + update_values(&mouse_report.h, &h_offset); + update_values(&mouse_report.v, &v_offset); +#else + update_values(&mouse_report.h, &v_offset); + update_values(&mouse_report.v, &h_offset); #endif - + mouse_report.x = 0; + mouse_report.y = 0; + } else { #ifndef PIMORONI_TRACKBALL_ROTATE - update_member(&mouse.x, &x_offset); - update_member(&mouse.y, &y_offset); - update_member(&mouse.h, &h_offset); - update_member(&mouse.v, &v_offset); + update_values(&mouse_report.x, &x_offset); + update_values(&mouse_report.y, &y_offset); #else - update_member(&mouse.x, &y_offset); - update_member(&mouse.y, &x_offset); - update_member(&mouse.h, &v_offset); - update_member(&mouse.v, &h_offset); + update_values(&mouse_report.x, &y_offset); + update_values(&mouse_report.y, &x_offset); #endif - pointing_device_set_report(mouse); - pointing_device_send(); + mouse_report.h = 0; + mouse_report.v = 0; + } + pointing_device_set_report(mouse_report); + pointing_device_send(); + + throttle = timer_read_fast(); + } } diff --git a/drivers/sensors/pimoroni_trackball.h b/drivers/sensors/pimoroni_trackball.h index 9e3c64c19c61..792bf1577ad7 100644 --- a/drivers/sensors/pimoroni_trackball.h +++ b/drivers/sensors/pimoroni_trackball.h @@ -1,4 +1,5 @@ /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) + * Copyright 2021 Dasky (@daskygit) * * 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 @@ -14,16 +15,27 @@ * along with this program. If not, see . */ -#pragma once - #include "quantum.h" #include "pointing_device.h" #ifndef TRACKBALL_ADDRESS # define TRACKBALL_ADDRESS 0x0A #endif -#define TRACKBALL_WRITE ((TRACKBALL_ADDRESS << 1) | I2C_WRITE) -#define TRACKBALL_READ (TRACKBALL_ADDRESS << 1) + +#ifndef TRACKBALL_INTERVAL_MS +# define TRACKBALL_INTERVAL_MS 8 +#endif + +#ifndef TRACKBALL_DEBOUNCE_ONCLICK +# define TRACKBALL_DEBOUNCE_ONCLICK 5 +#endif +typedef struct pimoroni_data { + uint8_t left; + uint8_t right; + uint8_t up; + uint8_t down; + uint8_t click; +} pimoroni_data; void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white); void trackball_check_click(bool pressed, report_mouse_t *mouse); From ce30b34a9ed706b3cc72aff3ddc6fd192aab71db Mon Sep 17 00:00:00 2001 From: daskygit <32983009+daskygit@users.noreply.github.com> Date: Sat, 31 Jul 2021 17:40:03 +0100 Subject: [PATCH 2/6] disappeared --- drivers/sensors/pimoroni_trackball.h | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/sensors/pimoroni_trackball.h b/drivers/sensors/pimoroni_trackball.h index 792bf1577ad7..c9c7d55fddf4 100644 --- a/drivers/sensors/pimoroni_trackball.h +++ b/drivers/sensors/pimoroni_trackball.h @@ -14,6 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#pragma once #include "quantum.h" #include "pointing_device.h" From a322a0a415deaef01f53023c78876bead7e2e413 Mon Sep 17 00:00:00 2001 From: daskygit <32983009+daskygit@users.noreply.github.com> Date: Sat, 31 Jul 2021 18:41:03 +0100 Subject: [PATCH 3/6] apply suggestion --- drivers/sensors/pimoroni_trackball.c | 18 ++++++++++++++++++ drivers/sensors/pimoroni_trackball.h | 20 -------------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/drivers/sensors/pimoroni_trackball.c b/drivers/sensors/pimoroni_trackball.c index 4cea7e6f0a6b..0ff413fdef67 100644 --- a/drivers/sensors/pimoroni_trackball.c +++ b/drivers/sensors/pimoroni_trackball.c @@ -18,6 +18,16 @@ #include "i2c_master.h" #include "print.h" +#ifndef TRACKBALL_ADDRESS +# define TRACKBALL_ADDRESS 0x0A +#endif +#ifndef TRACKBALL_INTERVAL_MS +# define TRACKBALL_INTERVAL_MS 8 +#endif +#ifndef TRACKBALL_DEBOUNCE_ONCLICK +# define TRACKBALL_DEBOUNCE_ONCLICK 5 +#endif + #define TRACKBALL_TIMEOUT 100 #define TRACKBALL_REG_LED_RED 0x00 #define TRACKBALL_REG_LED_GRN 0x01 @@ -28,6 +38,14 @@ #define TRACKBALL_REG_UP 0x06 #define TRACKBALL_REG_DOWN 0x07 +typedef struct pimoroni_data { + uint8_t left; + uint8_t right; + uint8_t up; + uint8_t down; + uint8_t click; +} pimoroni_data; + static pimoroni_data current_pimoroni_data; static report_mouse_t mouse_report; static bool scrolling = false; diff --git a/drivers/sensors/pimoroni_trackball.h b/drivers/sensors/pimoroni_trackball.h index c9c7d55fddf4..22670fe3e94f 100644 --- a/drivers/sensors/pimoroni_trackball.h +++ b/drivers/sensors/pimoroni_trackball.h @@ -19,28 +19,8 @@ #include "quantum.h" #include "pointing_device.h" -#ifndef TRACKBALL_ADDRESS -# define TRACKBALL_ADDRESS 0x0A -#endif - -#ifndef TRACKBALL_INTERVAL_MS -# define TRACKBALL_INTERVAL_MS 8 -#endif - -#ifndef TRACKBALL_DEBOUNCE_ONCLICK -# define TRACKBALL_DEBOUNCE_ONCLICK 5 -#endif -typedef struct pimoroni_data { - uint8_t left; - uint8_t right; - uint8_t up; - uint8_t down; - uint8_t click; -} pimoroni_data; - void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white); void trackball_check_click(bool pressed, report_mouse_t *mouse); -void trackball_register_button(bool pressed, enum mouse_buttons button); float trackball_get_precision(void); void trackball_set_precision(float precision); From e5cf4524961cc835a42f21aab9eeafc8af329928 Mon Sep 17 00:00:00 2001 From: daskygit <32983009+daskygit@users.noreply.github.com> Date: Sun, 1 Aug 2021 00:08:06 +0100 Subject: [PATCH 4/6] fix get precision --- drivers/sensors/pimoroni_trackball.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/sensors/pimoroni_trackball.c b/drivers/sensors/pimoroni_trackball.c index 0ff413fdef67..5b2b684f687b 100644 --- a/drivers/sensors/pimoroni_trackball.c +++ b/drivers/sensors/pimoroni_trackball.c @@ -55,7 +55,7 @@ static int16_t h_offset = 0; static int16_t v_offset = 0; static uint16_t precision = 128; -float trackball_get_precision(void) { return (float)(precision >> 7); } +float trackball_get_precision(void) { return ((float)precision / 128); } void trackball_set_precision(float floatprecision) { precision = (floatprecision * 128); } bool trackball_is_scrolling(void) { return scrolling; } void trackball_set_scrolling(bool scroll) { scrolling = scroll; } From 5641f63f68ed09fde7d1dc557ea7443b0a93fd87 Mon Sep 17 00:00:00 2001 From: daskygit <32983009+daskygit@users.noreply.github.com> Date: Sun, 1 Aug 2021 18:31:03 +0100 Subject: [PATCH 5/6] tweak logic and add callback --- drivers/sensors/pimoroni_trackball.c | 140 ++++++++++++++------------- drivers/sensors/pimoroni_trackball.h | 21 ++-- 2 files changed, 86 insertions(+), 75 deletions(-) diff --git a/drivers/sensors/pimoroni_trackball.c b/drivers/sensors/pimoroni_trackball.c index 5b2b684f687b..04224e3e1ab2 100644 --- a/drivers/sensors/pimoroni_trackball.c +++ b/drivers/sensors/pimoroni_trackball.c @@ -18,14 +18,20 @@ #include "i2c_master.h" #include "print.h" -#ifndef TRACKBALL_ADDRESS -# define TRACKBALL_ADDRESS 0x0A +#ifndef PIMORONI_TRACKBALL_ADDRESS +# define PIMORONI_TRACKBALL_ADDRESS 0x0A #endif -#ifndef TRACKBALL_INTERVAL_MS -# define TRACKBALL_INTERVAL_MS 8 +#ifndef PIMORONI_TRACKBALL_INTERVAL_MS +# define PIMORONI_TRACKBALL_INTERVAL_MS 8 #endif -#ifndef TRACKBALL_DEBOUNCE_ONCLICK -# define TRACKBALL_DEBOUNCE_ONCLICK 5 +#ifndef PIMORONI_TRACKBALL_MOUSE_SCALE +# define PIMORONI_TRACKBALL_MOUSE_SCALE 5 +#endif +#ifndef PIMORONI_TRACKBALL_SCROLL_SCALE +# define PIMORONI_TRACKBALL_SCROLL_SCALE 1 +#endif +#ifndef PIMORONI_TRACKBALL_DEBOUNCE_CYCLES +# define PIMORONI_TRACKBALL_DEBOUNCE_CYCLES 20 #endif #define TRACKBALL_TIMEOUT 100 @@ -38,14 +44,6 @@ #define TRACKBALL_REG_UP 0x06 #define TRACKBALL_REG_DOWN 0x07 -typedef struct pimoroni_data { - uint8_t left; - uint8_t right; - uint8_t up; - uint8_t down; - uint8_t click; -} pimoroni_data; - static pimoroni_data current_pimoroni_data; static report_mouse_t mouse_report; static bool scrolling = false; @@ -62,15 +60,18 @@ void trackball_set_scrolling(bool scroll) { scrolling = scroll; } void trackball_set_rgbw(uint8_t r, uint8_t g, uint8_t b, uint8_t w) { uint8_t data[4] = {r, g, b, w}; - __attribute__((unused)) i2c_status_t status = i2c_writeReg(TRACKBALL_ADDRESS << 1, TRACKBALL_REG_LED_RED, data, sizeof(data), TRACKBALL_TIMEOUT); + __attribute__((unused)) i2c_status_t status = i2c_writeReg(PIMORONI_TRACKBALL_ADDRESS << 1, TRACKBALL_REG_LED_RED, data, sizeof(data), TRACKBALL_TIMEOUT); +#ifdef TRACKBALL_DEBUG dprintf("Trackball RGBW i2c_status_t: %d\n", status); +#endif } -void read_pimoroni_trackball(pimoroni_data* data) { - __attribute__((unused)) i2c_status_t status = i2c_readReg(TRACKBALL_ADDRESS << 1, TRACKBALL_REG_LEFT, (uint8_t*)data, sizeof(*data), TRACKBALL_TIMEOUT); -#ifdef TRACKBALL_DEBUG_READ +i2c_status_t read_pimoroni_trackball(pimoroni_data* data) { + i2c_status_t status = i2c_readReg(PIMORONI_TRACKBALL_ADDRESS << 1, TRACKBALL_REG_LEFT, (uint8_t*)data, sizeof(*data), TRACKBALL_TIMEOUT); +#ifdef TRACKBALL_DEBUG dprintf("Trackball READ i2c_status_t: %d\nLeft: %d\nRight: %d\nUp: %d\nDown: %d\nSwtich: %d\n", status, data->left, data->right, data->up, data->down, data->click); #endif + return status; } __attribute__((weak)) void pointing_device_init(void) { @@ -78,7 +79,7 @@ __attribute__((weak)) void pointing_device_init(void) { trackball_set_rgbw(0x00, 0x00, 0x00, 0x00); } -int16_t get_offsets(uint8_t negative_dir, uint8_t positive_dir, uint8_t scale) { +int16_t trackball_get_offsets(uint8_t negative_dir, uint8_t positive_dir, uint8_t scale) { uint8_t offset = 0; bool isnegative = false; if (negative_dir > positive_dir) { @@ -91,96 +92,97 @@ int16_t get_offsets(uint8_t negative_dir, uint8_t positive_dir, uint8_t scale) { return isnegative ? -(int16_t)(magnitude) : (int16_t)(magnitude); } -void update_values(int8_t* member, int16_t* offset) { +void trackball_adapt_values(int8_t* mouse, int16_t* offset) { if (*offset > 127) { - *member = 127; + *mouse = 127; *offset -= 127; } else if (*offset < -127) { - *member = -127; + *mouse = -127; *offset += 127; } else { - *member = *offset; + *mouse = *offset; *offset = 0; } } -__attribute__((weak)) void trackball_check_click(bool pressed, report_mouse_t* mouse) { +__attribute__((weak)) void trackball_click(bool pressed, report_mouse_t* mouse) { +#ifdef PIMORONI_TRACKBALL_CLICK if (pressed) { mouse->buttons |= MOUSE_BTN1; } else { mouse->buttons &= ~MOUSE_BTN1; } +#endif } +__attribute__((weak)) bool pointing_device_task_user(pimoroni_data* trackball_data) { return true; }; + void pointing_device_task() { static fast_timer_t throttle = 0; - static uint8_t debounce = 0; + static uint16_t debounce = 0; - if (timer_elapsed_fast(throttle) > TRACKBALL_INTERVAL_MS) { - static pimoroni_data last_read_data = {0}; - mouse_report = pointing_device_get_report(); - read_pimoroni_trackball(¤t_pimoroni_data); + if (timer_elapsed_fast(throttle) >= PIMORONI_TRACKBALL_INTERVAL_MS) { + i2c_status_t status = read_pimoroni_trackball(¤t_pimoroni_data); - if (!debounce) { - if (memcmp(&last_read_data, ¤t_pimoroni_data, sizeof(current_pimoroni_data))) { - memcpy(&last_read_data, ¤t_pimoroni_data, sizeof(current_pimoroni_data)); + if (status == I2C_STATUS_SUCCESS && pointing_device_task_user(¤t_pimoroni_data)) { + mouse_report = pointing_device_get_report(); -#ifdef PIMORONI_TRACKBALL_CLICK - if (current_pimoroni_data.click & 128) { - trackball_check_click(true, &mouse_report); - debounce = TRACKBALL_DEBOUNCE_ONCLICK; - } else { - trackball_check_click(false, &mouse_report); - } -#endif - if (scrolling) { + if (!(current_pimoroni_data.click & 128)) { + trackball_click(false, &mouse_report); + if (!debounce) { + if (scrolling) { #ifdef PIMORONI_TRACKBALL_INVERT_X - h_offset += get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, 5); + h_offset += trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_SCROLL_SCALE); #else - h_offset -= get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, 5); + h_offset -= trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_SCROLL_SCALE); #endif #ifdef PIMORONI_TRACKBALL_INVERT_Y - v_offset += get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, 5); + v_offset += trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_SCROLL_SCALE); #else - v_offset -= get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, 5); + v_offset -= trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_SCROLL_SCALE); #endif - } else { + } else { #ifdef PIMORONI_TRACKBALL_INVERT_X - x_offset -= get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, 5); + x_offset -= trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_MOUSE_SCALE); #else - x_offset += get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, 5); + x_offset += trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_MOUSE_SCALE); #endif #ifdef PIMORONI_TRACKBALL_INVERT_Y - y_offset -= get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, 5); + y_offset -= trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_MOUSE_SCALE); #else - y_offset += get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, 5); + y_offset += trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_MOUSE_SCALE); #endif - } - } - } else { - debounce--; - } - if (scrolling) { + } + if (scrolling) { #ifndef PIMORONI_TRACKBALL_ROTATE - update_values(&mouse_report.h, &h_offset); - update_values(&mouse_report.v, &v_offset); + trackball_adapt_values(&mouse_report.h, &h_offset); + trackball_adapt_values(&mouse_report.v, &v_offset); #else - update_values(&mouse_report.h, &v_offset); - update_values(&mouse_report.v, &h_offset); + trackball_adapt_values(&mouse_report.h, &v_offset); + trackball_adapt_values(&mouse_report.v, &h_offset); #endif - mouse_report.x = 0; - mouse_report.y = 0; - } else { + mouse_report.x = 0; + mouse_report.y = 0; + } else { #ifndef PIMORONI_TRACKBALL_ROTATE - update_values(&mouse_report.x, &x_offset); - update_values(&mouse_report.y, &y_offset); + trackball_adapt_values(&mouse_report.x, &x_offset); + trackball_adapt_values(&mouse_report.y, &y_offset); #else - update_values(&mouse_report.x, &y_offset); - update_values(&mouse_report.y, &x_offset); + trackball_adapt_values(&mouse_report.x, &y_offset); + trackball_adapt_values(&mouse_report.y, &x_offset); #endif - mouse_report.h = 0; - mouse_report.v = 0; + mouse_report.h = 0; + mouse_report.v = 0; + } + } else { + debounce--; + } + } else { + trackball_click(true, &mouse_report); + debounce = PIMORONI_TRACKBALL_DEBOUNCE_CYCLES; + } } + pointing_device_set_report(mouse_report); pointing_device_send(); diff --git a/drivers/sensors/pimoroni_trackball.h b/drivers/sensors/pimoroni_trackball.h index 22670fe3e94f..6b2a41425d41 100644 --- a/drivers/sensors/pimoroni_trackball.h +++ b/drivers/sensors/pimoroni_trackball.h @@ -19,10 +19,19 @@ #include "quantum.h" #include "pointing_device.h" -void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white); -void trackball_check_click(bool pressed, report_mouse_t *mouse); +typedef struct pimoroni_data { + uint8_t left; + uint8_t right; + uint8_t up; + uint8_t down; + uint8_t click; +} pimoroni_data; -float trackball_get_precision(void); -void trackball_set_precision(float precision); -bool trackball_is_scrolling(void); -void trackball_set_scrolling(bool scroll); +void trackball_set_rgbw(uint8_t red, uint8_t green, uint8_t blue, uint8_t white); +void trackball_click(bool pressed, report_mouse_t* mouse); +int16_t trackball_get_offsets(uint8_t negative_dir, uint8_t positive_dir, uint8_t scale); +void trackball_adapt_values(int8_t* mouse, int16_t* offset); +float trackball_get_precision(void); +void trackball_set_precision(float precision); +bool trackball_is_scrolling(void); +void trackball_set_scrolling(bool scroll); From d541e36d5a0843c7475fc15d05ea89c5c1a50b92 Mon Sep 17 00:00:00 2001 From: daskygit <32983009+daskygit@users.noreply.github.com> Date: Sat, 14 Aug 2021 20:34:15 +0100 Subject: [PATCH 6/6] Make split friendly --- drivers/sensors/pimoroni_trackball.c | 94 +++++++++++++++------------- 1 file changed, 52 insertions(+), 42 deletions(-) diff --git a/drivers/sensors/pimoroni_trackball.c b/drivers/sensors/pimoroni_trackball.c index 04224e3e1ab2..48098ff0cca8 100644 --- a/drivers/sensors/pimoroni_trackball.c +++ b/drivers/sensors/pimoroni_trackball.c @@ -33,6 +33,9 @@ #ifndef PIMORONI_TRACKBALL_DEBOUNCE_CYCLES # define PIMORONI_TRACKBALL_DEBOUNCE_CYCLES 20 #endif +#ifndef PIMORONI_TRACKBALL_ERROR_COUNT +# define PIMORONI_TRACKBALL_ERROR_COUNT 10 +#endif #define TRACKBALL_TIMEOUT 100 #define TRACKBALL_REG_LED_RED 0x00 @@ -46,12 +49,13 @@ static pimoroni_data current_pimoroni_data; static report_mouse_t mouse_report; -static bool scrolling = false; -static int16_t x_offset = 0; -static int16_t y_offset = 0; -static int16_t h_offset = 0; -static int16_t v_offset = 0; -static uint16_t precision = 128; +static bool scrolling = false; +static int16_t x_offset = 0; +static int16_t y_offset = 0; +static int16_t h_offset = 0; +static int16_t v_offset = 0; +static uint16_t precision = 128; +static uint8_t error_count = 0; float trackball_get_precision(void) { return ((float)precision / 128); } void trackball_set_precision(float floatprecision) { precision = (floatprecision * 128); } @@ -117,70 +121,76 @@ __attribute__((weak)) void trackball_click(bool pressed, report_mouse_t* mouse) __attribute__((weak)) bool pointing_device_task_user(pimoroni_data* trackball_data) { return true; }; -void pointing_device_task() { +__attribute__((weak)) void pointing_device_task() { static fast_timer_t throttle = 0; static uint16_t debounce = 0; - if (timer_elapsed_fast(throttle) >= PIMORONI_TRACKBALL_INTERVAL_MS) { + if (error_count < PIMORONI_TRACKBALL_ERROR_COUNT && timer_elapsed_fast(throttle) >= PIMORONI_TRACKBALL_INTERVAL_MS) { i2c_status_t status = read_pimoroni_trackball(¤t_pimoroni_data); - if (status == I2C_STATUS_SUCCESS && pointing_device_task_user(¤t_pimoroni_data)) { - mouse_report = pointing_device_get_report(); + if (status == I2C_STATUS_SUCCESS) { + error_count = 0; + + if (pointing_device_task_user(¤t_pimoroni_data)) { + mouse_report = pointing_device_get_report(); - if (!(current_pimoroni_data.click & 128)) { - trackball_click(false, &mouse_report); - if (!debounce) { - if (scrolling) { + if (!(current_pimoroni_data.click & 128)) { + trackball_click(false, &mouse_report); + if (!debounce) { + if (scrolling) { #ifdef PIMORONI_TRACKBALL_INVERT_X - h_offset += trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_SCROLL_SCALE); + h_offset += trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_SCROLL_SCALE); #else - h_offset -= trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_SCROLL_SCALE); + h_offset -= trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_SCROLL_SCALE); #endif #ifdef PIMORONI_TRACKBALL_INVERT_Y - v_offset += trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_SCROLL_SCALE); + v_offset += trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_SCROLL_SCALE); #else - v_offset -= trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_SCROLL_SCALE); + v_offset -= trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_SCROLL_SCALE); #endif - } else { + } else { #ifdef PIMORONI_TRACKBALL_INVERT_X - x_offset -= trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_MOUSE_SCALE); + x_offset -= trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_MOUSE_SCALE); #else - x_offset += trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_MOUSE_SCALE); + x_offset += trackball_get_offsets(current_pimoroni_data.right, current_pimoroni_data.left, PIMORONI_TRACKBALL_MOUSE_SCALE); #endif #ifdef PIMORONI_TRACKBALL_INVERT_Y - y_offset -= trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_MOUSE_SCALE); + y_offset -= trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_MOUSE_SCALE); #else - y_offset += trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_MOUSE_SCALE); + y_offset += trackball_get_offsets(current_pimoroni_data.down, current_pimoroni_data.up, PIMORONI_TRACKBALL_MOUSE_SCALE); #endif - } - if (scrolling) { + } + if (scrolling) { #ifndef PIMORONI_TRACKBALL_ROTATE - trackball_adapt_values(&mouse_report.h, &h_offset); - trackball_adapt_values(&mouse_report.v, &v_offset); + trackball_adapt_values(&mouse_report.h, &h_offset); + trackball_adapt_values(&mouse_report.v, &v_offset); #else - trackball_adapt_values(&mouse_report.h, &v_offset); - trackball_adapt_values(&mouse_report.v, &h_offset); + trackball_adapt_values(&mouse_report.h, &v_offset); + trackball_adapt_values(&mouse_report.v, &h_offset); #endif - mouse_report.x = 0; - mouse_report.y = 0; - } else { + mouse_report.x = 0; + mouse_report.y = 0; + } else { #ifndef PIMORONI_TRACKBALL_ROTATE - trackball_adapt_values(&mouse_report.x, &x_offset); - trackball_adapt_values(&mouse_report.y, &y_offset); + trackball_adapt_values(&mouse_report.x, &x_offset); + trackball_adapt_values(&mouse_report.y, &y_offset); #else - trackball_adapt_values(&mouse_report.x, &y_offset); - trackball_adapt_values(&mouse_report.y, &x_offset); + trackball_adapt_values(&mouse_report.x, &y_offset); + trackball_adapt_values(&mouse_report.y, &x_offset); #endif - mouse_report.h = 0; - mouse_report.v = 0; + mouse_report.h = 0; + mouse_report.v = 0; + } + } else { + debounce--; } } else { - debounce--; + trackball_click(true, &mouse_report); + debounce = PIMORONI_TRACKBALL_DEBOUNCE_CYCLES; } - } else { - trackball_click(true, &mouse_report); - debounce = PIMORONI_TRACKBALL_DEBOUNCE_CYCLES; } + } else { + error_count++; } pointing_device_set_report(mouse_report);