From d555248d41d67c91e9ca99e8537231cc546d837e Mon Sep 17 00:00:00 2001 From: freznel10 Date: Sun, 9 Apr 2023 18:56:15 +0800 Subject: [PATCH] Squashed commit of the following: commit 211a3640b2c1b2eafaba665ee0c6a41bae43b1ea Author: Dasky <32983009+daskygit@users.noreply.github.com> Date: Sat Apr 8 23:28:03 2023 +0100 remove device drivers .c commit c06c4f38c48f32937f9c239692c3ec6b435f571b Author: Dasky <32983009+daskygit@users.noreply.github.com> Date: Sat Apr 8 23:18:35 2023 +0100 modify analog joystick commit 147dffb6291a35167ed9948ecc581fbbe19c5f5f Author: Dasky <32983009+daskygit@users.noreply.github.com> Date: Sat Apr 8 22:17:14 2023 +0100 adns5050 don't use defines for cpi commit bd9d1f32579027d46d531658f4e6c924707dde43 Author: Dasky <32983009+daskygit@users.noreply.github.com> Date: Sat Apr 8 22:16:52 2023 +0100 modify paw3204 commit 40728df2112ad13e7f0225d98cd3a22ef3300f57 Author: Dasky <32983009+daskygit@users.noreply.github.com> Date: Sat Apr 8 21:18:13 2023 +0100 modify adns5050 commit 6be528687f2200faf33eab69407f62a6932e397d Author: Dasky <32983009+daskygit@users.noreply.github.com> Date: Fri Apr 7 21:40:29 2023 +0100 sort counters and add miss report debug print commit 33662c0309ac94ed5ce95c8cb35769949405c3d3 Author: Dasky <32983009+daskygit@users.noreply.github.com> Date: Fri Apr 7 15:39:02 2023 +0100 thank you freznel --- drivers/sensors/adns5050.c | 119 ++-- drivers/sensors/adns5050.h | 86 ++- drivers/sensors/analog_joystick.c | 68 +- drivers/sensors/analog_joystick.h | 35 +- drivers/sensors/paw3204.c | 163 ++--- drivers/sensors/paw3204.h | 20 +- quantum/pointing_device/pointing_device.c | 15 +- quantum/pointing_device/pointing_device.h | 11 + .../pointing_device/pointing_device_drivers.c | 600 ------------------ quantum/split_common/transactions.c | 1 + 10 files changed, 299 insertions(+), 819 deletions(-) delete mode 100644 quantum/pointing_device/pointing_device_drivers.c diff --git a/drivers/sensors/adns5050.c b/drivers/sensors/adns5050.c index b76268fba267..367c54b2afcf 100644 --- a/drivers/sensors/adns5050.c +++ b/drivers/sensors/adns5050.c @@ -45,15 +45,21 @@ #define REG_MOTION_BURST 0x63 // clang-format on -void adns5050_init(void) { +const pointing_device_driver_t adns5050_driver_default = {.init = adns5050_init, .get_report = adns5050_get_report, .set_cpi = adns5050_set_cpi, .get_cpi = adns5050_get_cpi}; +const adns5050_config_t adns5050_config_default = {.cs = ADNS5050_CS_PIN, .sclk = ADNS5050_SCLK_PIN, .sdio = ADNS5050_SDIO_PIN}; + + +void adns5050_init(const void* config) { + adns5050_config_t* adns5050_config = (adns5050_config_t*)config; + // Initialize the ADNS serial pins. - setPinOutput(ADNS5050_SCLK_PIN); - setPinOutput(ADNS5050_SDIO_PIN); - setPinOutput(ADNS5050_CS_PIN); + setPinOutput(adns5050_config->sclk); + setPinOutput(adns5050_config->sdio); + setPinOutput(adns5050_config->cs); // reboot the adns. // if the adns hasn't initialized yet, this is harmless. - adns5050_write_reg(REG_CHIP_RESET, 0x5a); + adns5050_write_reg(adns5050_config, REG_CHIP_RESET, 0x5a); // wait maximum time before adns is ready. // this ensures that the adns is actuall ready after reset. @@ -62,57 +68,57 @@ void adns5050_init(void) { // read a burst from the adns and then discard it. // gets the adns ready for write commands // (for example, setting the dpi). - adns5050_read_burst(); + adns5050_read_burst(adns5050_config); } // Perform a synchronization with the ADNS. // Just as with the serial protocol, this is used by the slave to send a // synchronization signal to the master. -void adns5050_sync(void) { - writePinLow(ADNS5050_CS_PIN); +void adns5050_sync(adns5050_config_t* adns5050_config) { + writePinLow(adns5050_config->cs); wait_us(1); - writePinHigh(ADNS5050_CS_PIN); + writePinHigh(adns5050_config->cs); } -void adns5050_cs_select(void) { - writePinLow(ADNS5050_CS_PIN); +void adns5050_cs_select(adns5050_config_t* adns5050_config) { + writePinLow(adns5050_config->cs); } -void adns5050_cs_deselect(void) { - writePinHigh(ADNS5050_CS_PIN); +void adns5050_cs_deselect(adns5050_config_t* adns5050_config) { + writePinHigh(adns5050_config->cs); } -uint8_t adns5050_serial_read(void) { - setPinInput(ADNS5050_SDIO_PIN); +uint8_t adns5050_serial_read(adns5050_config_t* adns5050_config) { + setPinInput(adns5050_config->sdio); uint8_t byte = 0; for (uint8_t i = 0; i < 8; ++i) { - writePinLow(ADNS5050_SCLK_PIN); + writePinLow(adns5050_config->sclk); wait_us(1); - byte = (byte << 1) | readPin(ADNS5050_SDIO_PIN); + byte = (byte << 1) | readPin(adns5050_config->sdio); - writePinHigh(ADNS5050_SCLK_PIN); + writePinHigh(adns5050_config->sclk); wait_us(1); } return byte; } -void adns5050_serial_write(uint8_t data) { - setPinOutput(ADNS5050_SDIO_PIN); +void adns5050_serial_write(adns5050_config_t* adns5050_config, uint8_t data) { + setPinOutput(adns5050_config->sdio); for (int8_t b = 7; b >= 0; b--) { - writePinLow(ADNS5050_SCLK_PIN); + writePinLow(adns5050_config->sclk); if (data & (1 << b)) - writePinHigh(ADNS5050_SDIO_PIN); + writePinHigh(adns5050_config->sdio); else - writePinLow(ADNS5050_SDIO_PIN); + writePinLow(adns5050_config->sdio); wait_us(2); - writePinHigh(ADNS5050_SCLK_PIN); + writePinHigh(adns5050_config->sclk); } // tSWR. See page 15 of the ADNS spec sheet. @@ -126,17 +132,17 @@ void adns5050_serial_write(uint8_t data) { // Read a byte of data from a register on the ADNS. // Don't forget to use the register map (as defined in the header file). -uint8_t adns5050_read_reg(uint8_t reg_addr) { - adns5050_cs_select(); +uint8_t adns5050_read_reg(adns5050_config_t* adns5050_config, uint8_t reg_addr) { + adns5050_cs_select(adns5050_config); - adns5050_serial_write(reg_addr); + adns5050_serial_write(adns5050_config, reg_addr); // We don't need a minimum tSRAD here. That's because a 4ms wait time is // already included in adns5050_serial_write(), so we're good. // See page 10 and 15 of the ADNS spec sheet. // wait_us(4); - uint8_t byte = adns5050_serial_read(); + uint8_t byte = adns5050_serial_read(adns5050_config); // tSRW & tSRR. See page 15 of the ADNS spec sheet. // Technically, this is only necessary if the next operation is an SDIO @@ -144,38 +150,38 @@ uint8_t adns5050_read_reg(uint8_t reg_addr) { // Honestly, this wait could probably be removed. wait_us(1); - adns5050_cs_deselect(); + adns5050_cs_deselect(adns5050_config); return byte; } -void adns5050_write_reg(uint8_t reg_addr, uint8_t data) { - adns5050_cs_select(); - adns5050_serial_write(0b10000000 | reg_addr); - adns5050_serial_write(data); - adns5050_cs_deselect(); +void adns5050_write_reg(adns5050_config_t* adns5050_config, uint8_t reg_addr, uint8_t data) { + adns5050_cs_select(adns5050_config); + adns5050_serial_write(adns5050_config, 0b10000000 | reg_addr); + adns5050_serial_write(adns5050_config, data); + adns5050_cs_deselect(adns5050_config); } -report_adns5050_t adns5050_read_burst(void) { - adns5050_cs_select(); +report_adns5050_t adns5050_read_burst(adns5050_config_t* adns5050_config) { + adns5050_cs_select(adns5050_config); report_adns5050_t data; data.dx = 0; data.dy = 0; - adns5050_serial_write(REG_MOTION_BURST); + adns5050_serial_write(adns5050_config, REG_MOTION_BURST); // We don't need a minimum tSRAD here. That's because a 4ms wait time is // already included in adns5050_serial_write(), so we're good. // See page 10 and 15 of the ADNS spec sheet. // wait_us(4); - uint8_t x = adns5050_serial_read(); - uint8_t y = adns5050_serial_read(); + uint8_t x = adns5050_serial_read(adns5050_config); + uint8_t y = adns5050_serial_read(adns5050_config); // Burst mode returns a bunch of other shit that we don't really need. // Setting CS to high ends burst mode early. - adns5050_cs_deselect(); + adns5050_cs_deselect(adns5050_config); data.dx = convert_twoscomp(x); data.dy = convert_twoscomp(y); @@ -193,21 +199,38 @@ int8_t convert_twoscomp(uint8_t data) { } // Don't forget to use the definitions for CPI in the header file. -void adns5050_set_cpi(uint16_t cpi) { +void adns5050_set_cpi(const void* config, uint16_t cpi) { + adns5050_config_t* adns5050_config = (adns5050_config_t*)config; uint8_t cpival = constrain((cpi / 125), 0x1, 0xD); // limits to 0--119 - adns5050_write_reg(REG_MOUSE_CONTROL2, 0b10000 | cpival); + adns5050_write_reg(adns5050_config, REG_MOUSE_CONTROL2, 0b10000 | cpival); } -uint16_t adns5050_get_cpi(void) { - uint8_t cpival = adns5050_read_reg(REG_MOUSE_CONTROL2); +uint16_t adns5050_get_cpi(const void* config) { + adns5050_config_t* adns5050_config = (adns5050_config_t*)config; + uint8_t cpival = adns5050_read_reg(adns5050_config, REG_MOUSE_CONTROL2); return (uint16_t)((cpival & 0b10000) * 125); } -bool adns5050_check_signature(void) { - uint8_t pid = adns5050_read_reg(REG_PRODUCT_ID); - uint8_t rid = adns5050_read_reg(REG_REVISION_ID); - uint8_t pid2 = adns5050_read_reg(REG_PRODUCT_ID2); +bool adns5050_check_signature(adns5050_config_t* adns5050_config) { + uint8_t pid = adns5050_read_reg(adns5050_config, REG_PRODUCT_ID); + uint8_t rid = adns5050_read_reg(adns5050_config, REG_REVISION_ID); + uint8_t pid2 = adns5050_read_reg(adns5050_config, REG_PRODUCT_ID2); return (pid == 0x12 && rid == 0x01 && pid2 == 0x26); } + +report_mouse_t adns5050_get_report(const void* config) { + adns5050_config_t* adns5050_config = (adns5050_config_t*)config; + + report_adns5050_t data = adns5050_read_burst(adns5050_config); + report_mouse_t mouse_report = {0}; + + if (data.dx != 0 || data.dy != 0) { + pd_dprintf("Raw ] X: %d, Y: %d\n", data.dx, data.dy); + mouse_report.x = (mouse_xy_report_t)data.dx; + mouse_report.y = (mouse_xy_report_t)data.dy; + } + + return mouse_report; +} \ No newline at end of file diff --git a/drivers/sensors/adns5050.h b/drivers/sensors/adns5050.h index 8ef0f7cc7ce3..25d7b26e0124 100644 --- a/drivers/sensors/adns5050.h +++ b/drivers/sensors/adns5050.h @@ -21,65 +21,53 @@ #include #include +#include "pointing_device.h" -// CPI values -// clang-format off -#define CPI125 0x11 -#define CPI250 0x12 -#define CPI375 0x13 -#define CPI500 0x14 -#define CPI625 0x15 -#define CPI750 0x16 -#define CPI875 0x17 -#define CPI1000 0x18 -#define CPI1125 0x19 -#define CPI1250 0x1a -#define CPI1375 0x1b -// clang-format on +enum adns5050_cpi { + ADNS5050_CPI125 = 0x11, + ADNS5050_CPI250 = 0x12, + ADNS5050_CPI375 = 0x13, + ADNS5050_CPI500 = 0x14, + ADNS5050_CPI625 = 0x15, + ADNS5050_CPI750 = 0x16, + ADNS5050_CPI875 = 0x17, + ADNS5050_CPI1000 = 0x18, + ADNS5050_CPI1125 = 0x19, + ADNS5050_CPI1250 = 0x1a, + ADNS5050_CPI1375 = 0x1b, +}; #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt))) -// Definitions for the ADNS serial line. -#ifndef ADNS5050_SCLK_PIN -# ifdef POINTING_DEVICE_SCLK_PIN -# define ADNS5050_SCLK_PIN POINTING_DEVICE_SCLK_PIN -# else -# error "No clock pin defined -- missing POINTING_DEVICE_SCLK_PIN or ADNS5050_SCLK_PIN" -# endif -#endif - -#ifndef ADNS5050_SDIO_PIN -# ifdef POINTING_DEVICE_SDIO_PIN -# define ADNS5050_SDIO_PIN POINTING_DEVICE_SDIO_PIN -# else -# error "No data pin defined -- missing POINTING_DEVICE_SDIO_PIN or ADNS5050_SDIO_PIN" -# endif -#endif - -#ifndef ADNS5050_CS_PIN -# ifdef POINTING_DEVICE_CS_PIN -# define ADNS5050_CS_PIN POINTING_DEVICE_CS_PIN -# else -# error "No chip select pin defined -- missing POINTING_DEVICE_CS_PIN or ADNS5050_CS_PIN define" -# endif -#endif - typedef struct { int8_t dx; int8_t dy; } report_adns5050_t; +typedef struct { + pin_t sdio; + pin_t sclk; + pin_t cs; +} adns5050_config_t; + +const pointing_device_driver_t adns5050_driver_default; + +#if defined(ADNS5050_SCLK_PIN) & defined(ADNS5050_SDIO_PIN) & defined(ADNS5050_CS_PIN) +const adns5050_config_t adns5050_config_default; +#endif + // A bunch of functions to implement the ADNS5050-specific serial protocol. // Note that the "serial.h" driver is insufficient, because it does not // manually manipulate a serial clock signal. -void adns5050_init(void); -void adns5050_sync(void); -uint8_t adns5050_serial_read(void); -void adns5050_serial_write(uint8_t data); -uint8_t adns5050_read_reg(uint8_t reg_addr); -void adns5050_write_reg(uint8_t reg_addr, uint8_t data); -report_adns5050_t adns5050_read_burst(void); -void adns5050_set_cpi(uint16_t cpi); -uint16_t adns5050_get_cpi(void); +void adns5050_init(const void* config); +void adns5050_sync(adns5050_config_t* adns5050_config); +uint8_t adns5050_serial_read(adns5050_config_t* adns5050_config); +void adns5050_serial_write(adns5050_config_t* adns5050_config, uint8_t data); +uint8_t adns5050_read_reg(adns5050_config_t* adns5050_config, uint8_t reg_addr); +void adns5050_write_reg(adns5050_config_t* adns5050_config, uint8_t reg_addr, uint8_t data); +report_adns5050_t adns5050_read_burst(adns5050_config_t* adns5050_config); +void adns5050_set_cpi(const void* config, uint16_t cpi); +uint16_t adns5050_get_cpi(const void* config); int8_t convert_twoscomp(uint8_t data); -bool adns5050_check_signature(void); +bool adns5050_check_signature(adns5050_config_t* adns5050_config); +report_mouse_t adns5050_get_report(const void* config); diff --git a/drivers/sensors/analog_joystick.c b/drivers/sensors/analog_joystick.c index 12256a8e7ab2..c14aa38a68bf 100644 --- a/drivers/sensors/analog_joystick.c +++ b/drivers/sensors/analog_joystick.c @@ -21,18 +21,15 @@ #include "timer.h" #include -// Set Parameters -uint16_t minAxisValue = ANALOG_JOYSTICK_AXIS_MIN; -uint16_t maxAxisValue = ANALOG_JOYSTICK_AXIS_MAX; - -uint8_t maxCursorSpeed = ANALOG_JOYSTICK_SPEED_MAX; -uint8_t speedRegulator = ANALOG_JOYSTICK_SPEED_REGULATOR; // Lower Values Create Faster Movement - int16_t xOrigin, yOrigin; -uint16_t lastCursor = 0; +const pointing_device_driver_t analog_joystick_driver_default = {.init = analog_joystick_init, .get_report = analog_joystick_get_report, .get_cpi = NULL, .set_cpi = NULL}; + +#if defined(ANALOG_JOYSTICK_X_AXIS_PIN) & defined(ANALOG_JOYSTICK_Y_AXIS_PIN) +const analog_joystick_config_t analog_joystick_config_default = {.x = ANALOG_JOYSTICK_X_AXIS_PIN, .y = ANALOG_JOYSTICK_Y_AXIS_PIN, .axis_min = ANALOG_JOYSTICK_AXIS_MIN, .axis_max = ANALOG_JOYSTICK_AXIS_MAX, .speed_regulator = ANALOG_JOYSTICK_SPEED_REGULATOR, .speed_max = ANALOG_JOYSTICK_SPEED_MAX, .button = ANALOG_JOYSTICK_CLICK_PIN}; +#endif -int16_t axisCoordinate(pin_t pin, uint16_t origin) { +int16_t axisCoordinate(analog_joystick_config_t *analog_config, pin_t pin, uint16_t origin) { int8_t direction; int16_t distanceFromOrigin; int16_t range; @@ -43,11 +40,11 @@ int16_t axisCoordinate(pin_t pin, uint16_t origin) { return 0; } else if (origin > position) { distanceFromOrigin = origin - position; - range = origin - minAxisValue; + range = origin - analog_config->axis_min; direction = -1; } else { distanceFromOrigin = position - origin; - range = maxAxisValue - origin; + range = analog_config->axis_max - origin; direction = 1; } @@ -62,35 +59,50 @@ int16_t axisCoordinate(pin_t pin, uint16_t origin) { } } -int8_t axisToMouseComponent(pin_t pin, int16_t origin, uint8_t maxSpeed) { - int16_t coordinate = axisCoordinate(pin, origin); +int8_t axisToMouseComponent(analog_joystick_config_t *analog_config, pin_t pin, int16_t origin) { + int16_t coordinate = axisCoordinate(analog_config, pin, origin); if (coordinate != 0) { float percent = (float)coordinate / 100; - return percent * maxCursorSpeed * (abs(coordinate) / speedRegulator); + return percent * analog_config->speed_max * (abs(coordinate) / analog_config->speed_regulator); } else { return 0; } } -report_analog_joystick_t analog_joystick_read(void) { +report_analog_joystick_t analog_joystick_read(analog_joystick_config_t *analog_config) { report_analog_joystick_t report = {0}; - if (timer_elapsed(lastCursor) > ANALOG_JOYSTICK_READ_INTERVAL) { - lastCursor = timer_read(); - report.x = axisToMouseComponent(ANALOG_JOYSTICK_X_AXIS_PIN, xOrigin, maxCursorSpeed); - report.y = axisToMouseComponent(ANALOG_JOYSTICK_Y_AXIS_PIN, yOrigin, maxCursorSpeed); + report.x = axisToMouseComponent(analog_config, analog_config->x, xOrigin); + report.y = axisToMouseComponent(analog_config, analog_config->y, yOrigin); + + if (analog_config->button != NO_PIN) { + report.button = !readPin(analog_config->button); } -#ifdef ANALOG_JOYSTICK_CLICK_PIN - report.button = !readPin(ANALOG_JOYSTICK_CLICK_PIN); -#endif return report; } -void analog_joystick_init(void) { -#ifdef ANALOG_JOYSTICK_CLICK_PIN - setPinInputHigh(ANALOG_JOYSTICK_CLICK_PIN); -#endif +void analog_joystick_init(const void *config) { + analog_joystick_config_t *analog_config = (analog_joystick_config_t *)config; + if (analog_config->button != NO_PIN) { + setPinInputHigh(analog_config->button); + } // Account for drift - xOrigin = analogReadPin(ANALOG_JOYSTICK_X_AXIS_PIN); - yOrigin = analogReadPin(ANALOG_JOYSTICK_Y_AXIS_PIN); + xOrigin = analogReadPin(analog_config->x); + yOrigin = analogReadPin(analog_config->y); } + +report_mouse_t analog_joystick_get_report(const void *config) { + analog_joystick_config_t *analog_config = (analog_joystick_config_t *)config; + report_analog_joystick_t data = analog_joystick_read(analog_config); + + report_mouse_t mouse_report = {0}; + + pd_dprintf("Raw ] X: %d, Y: %d\n", data.x, data.y); + + mouse_report.x = data.x; + mouse_report.y = data.y; + + mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, data.button, POINTING_DEVICE_BUTTON1); + + return mouse_report; +} \ No newline at end of file diff --git a/drivers/sensors/analog_joystick.h b/drivers/sensors/analog_joystick.h index 6892a08817f2..c6fcd9fe72b7 100644 --- a/drivers/sensors/analog_joystick.h +++ b/drivers/sensors/analog_joystick.h @@ -18,13 +18,7 @@ #include #include - -#ifndef ANALOG_JOYSTICK_X_AXIS_PIN -# error No pin specified for X Axis -#endif -#ifndef ANALOG_JOYSTICK_Y_AXIS_PIN -# error No pin specified for Y Axis -#endif +#include "pointing_device.h" #ifndef ANALOG_JOYSTICK_AXIS_MIN # define ANALOG_JOYSTICK_AXIS_MIN 0 @@ -35,17 +29,34 @@ #ifndef ANALOG_JOYSTICK_SPEED_REGULATOR # define ANALOG_JOYSTICK_SPEED_REGULATOR 20 #endif -#ifndef ANALOG_JOYSTICK_READ_INTERVAL -# define ANALOG_JOYSTICK_READ_INTERVAL 10 -#endif #ifndef ANALOG_JOYSTICK_SPEED_MAX # define ANALOG_JOYSTICK_SPEED_MAX 2 #endif +#ifndef ANALOG_JOYSTICK_CLICK_PIN +# define ANALOG_JOYSTICK_CLICK_PIN NO_PIN +#endif +typedef struct { + pin_t x; + pin_t y; + pin_t button; + uint16_t axis_min; + uint16_t axis_max; + uint8_t speed_regulator; + uint8_t speed_max; +} analog_joystick_config_t; typedef struct { int8_t x; int8_t y; bool button; } report_analog_joystick_t; -report_analog_joystick_t analog_joystick_read(void); -void analog_joystick_init(void); + +const pointing_device_driver_t analog_joystick_driver_default; + +#if defined(ANALOG_JOYSTICK_X_AXIS_PIN) & defined(ANALOG_JOYSTICK_Y_AXIS_PIN) +const analog_joystick_config_t analog_joystick_config_default; +#endif + +report_analog_joystick_t analog_joystick_read(analog_joystick_config_t* analog_config); +void analog_joystick_init(const void* config); +report_mouse_t analog_joystick_get_report(const void* config); diff --git a/drivers/sensors/paw3204.c b/drivers/sensors/paw3204.c index a13753dd6f1d..7a28213ff37a 100644 --- a/drivers/sensors/paw3204.c +++ b/drivers/sensors/paw3204.c @@ -20,153 +20,174 @@ #include "wait.h" #include "debug.h" #include "gpio.h" +#include "pointing_device.h" -#define REG_PID1 0x00 -#define REG_PID2 0x01 -#define REG_STAT 0x02 -#define REG_X 0x03 -#define REG_Y 0x04 +#define PAW3204_REG_PID1 0x00 +#define PAW3204_REG_PID2 0x01 +#define PAW3204_REG_STAT 0x02 +#define PAW3204_REG_X 0x03 +#define PAW3204_REG_Y 0x04 -#define REG_SETUP 0x06 -#define REG_IMGQUAL 0x07 -#define REG_IMGREC 0x0E -#define REG_IMGTRASH 0x0D +#define PAW3204_REG_SETUP 0x06 +#define PAW3204_REG_IMGQUAL 0x07 +#define PAW3204_REG_IMGREC 0x0E +#define PAW3204_REG_IMGTRASH 0x0D #define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt))) // CPI values -enum cpi_values { - CPI400, // 0b000 - CPI500, // 0b001 - CPI600, // 0b010 - CPI800, // 0b011 - CPI1000, // 0b100 - CPI1200, // 0b101 - CPI1600, // 0b110 +enum paw3204_cpi_values { + PAW3204_CPI400, // 0b000 + PAW3204_CPI500, // 0b001 + PAW3204_CPI600, // 0b010 + PAW3204_CPI800, // 0b011 + PAW3204_CPI1000, // 0b100 + PAW3204_CPI1200, // 0b101 + PAW3204_CPI1600, // 0b110 }; -uint8_t paw3204_serial_read(void); -void paw3204_serial_write(uint8_t reg_addr); -uint8_t paw3204_read_reg(uint8_t reg_addr); -void paw3204_write_reg(uint8_t reg_addr, uint8_t data); +uint8_t paw3204_serial_read(paw3204_config_t* paw3204_config); +void paw3204_serial_write(paw3204_config_t* paw3204_config, uint8_t reg_addr); +uint8_t paw3204_read_reg(paw3204_config_t* paw3204_config, uint8_t reg_addr); +void paw3204_write_reg(paw3204_config_t* paw3204_config, uint8_t reg_addr, uint8_t data); -void paw3204_init(void) { - setPinOutput(PAW3204_SCLK_PIN); // setclockpin to output - setPinInputHigh(PAW3204_SDIO_PIN); // set datapin input high +void paw3204_init(const void* config) { + paw3204_config_t* paw3204_config = (paw3204_config_t*)config; - paw3204_write_reg(REG_SETUP, 0x86); // reset sensor and set 1600cpi + setPinOutput(paw3204_config->sclk); // setclockpin to output + setPinInputHigh(paw3204_config->sdio); // set datapin input high + + paw3204_write_reg(paw3204_config, PAW3204_REG_SETUP, 0x86); // reset sensor and set 1600cpi wait_us(5); - paw3204_read_reg(0x00); // read id - paw3204_read_reg(0x01); // read id2 + paw3204_read_reg(paw3204_config,0x00); // read id + paw3204_read_reg(paw3204_config, 0x01); // read id2 // PAW3204_write_reg(REG_SETUP,0x06); // dont reset sensor and set cpi 1600 - paw3204_write_reg(REG_IMGTRASH, 0x32); // write image trashhold + paw3204_write_reg(paw3204_config, PAW3204_REG_IMGTRASH, 0x32); // write image trashhold } -uint8_t paw3204_serial_read(void) { - setPinInput(PAW3204_SDIO_PIN); +uint8_t paw3204_serial_read(paw3204_config_t* paw3204_config) { + setPinInput(paw3204_config->sdio); uint8_t byte = 0; for (uint8_t i = 0; i < 8; ++i) { - writePinLow(PAW3204_SCLK_PIN); + writePinLow(paw3204_config->sclk); wait_us(1); - byte = (byte << 1) | readPin(PAW3204_SDIO_PIN); + byte = (byte << 1) | readPin(paw3204_config->sdio); - writePinHigh(PAW3204_SCLK_PIN); + writePinHigh(paw3204_config->sclk); wait_us(1); } return byte; } -void paw3204_serial_write(uint8_t data) { - writePinLow(PAW3204_SDIO_PIN); - setPinOutput(PAW3204_SDIO_PIN); +void paw3204_serial_write(paw3204_config_t* paw3204_config, uint8_t data) { + writePinLow(paw3204_config->sdio); + setPinOutput(paw3204_config->sdio); for (int8_t b = 7; b >= 0; b--) { - writePinLow(PAW3204_SCLK_PIN); + writePinLow(paw3204_config->sclk); if (data & (1 << b)) { - writePinHigh(PAW3204_SDIO_PIN); + writePinHigh(paw3204_config->sdio); } else { - writePinLow(PAW3204_SDIO_PIN); + writePinLow(paw3204_config->sdio); } - writePinHigh(PAW3204_SCLK_PIN); + writePinHigh(paw3204_config->sclk); } wait_us(4); } -report_paw3204_t paw3204_read(void) { +report_paw3204_t paw3204_read(paw3204_config_t* paw3204_config) { report_paw3204_t data = {0}; - data.isMotion = paw3204_read_reg(REG_STAT) & (1 << 7); // check for motion only (bit 7 in field) - data.x = (int8_t)paw3204_read_reg(REG_X); - data.y = (int8_t)paw3204_read_reg(REG_Y); + data.isMotion = paw3204_read_reg(paw3204_config, PAW3204_REG_STAT) & (1 << 7); // check for motion only (bit 7 in field) + data.x = (int8_t)paw3204_read_reg(paw3204_config, PAW3204_REG_X); + data.y = (int8_t)paw3204_read_reg(paw3204_config, PAW3204_REG_Y); return data; } -void paw3204_write_reg(uint8_t reg_addr, uint8_t data) { - paw3204_serial_write(0b10000000 | reg_addr); - paw3204_serial_write(data); +void paw3204_write_reg(paw3204_config_t* paw3204_config, uint8_t reg_addr, uint8_t data) { + paw3204_serial_write(paw3204_config, 0b10000000 | reg_addr); + paw3204_serial_write(paw3204_config, data); } -uint8_t paw3204_read_reg(uint8_t reg_addr) { - paw3204_serial_write(reg_addr); +uint8_t paw3204_read_reg(paw3204_config_t* paw3204_config, uint8_t reg_addr) { + paw3204_serial_write(paw3204_config, reg_addr); wait_us(5); - return paw3204_serial_read(); + return paw3204_serial_read(paw3204_config); } -void paw3204_set_cpi(uint16_t cpi) { - uint8_t cpival = CPI1000; +void paw3204_set_cpi(const void* config, uint16_t cpi) { + paw3204_config_t* paw3204_config = (paw3204_config_t*)config; + + uint8_t cpival = PAW3204_CPI1000; if (cpi <= 450) { - cpival = CPI400; + cpival = PAW3204_CPI400; } else if (cpi <= 550) { - cpival = CPI500; + cpival = PAW3204_CPI500; } else if (cpi <= 700) { - cpival = CPI600; + cpival = PAW3204_CPI600; } else if (cpi <= 900) { - cpival = CPI800; + cpival = PAW3204_CPI800; } else if (cpi <= 1100) { - cpival = CPI1000; + cpival = PAW3204_CPI1000; } else if (cpi <= 1400) { - cpival = CPI1200; + cpival = PAW3204_CPI1200; } else if (cpi > 1400) { - cpival = CPI1600; + cpival = PAW3204_CPI1600; } - paw3204_write_reg(REG_SETUP, cpival); + paw3204_write_reg(paw3204_config, PAW3204_REG_SETUP, cpival); } -uint16_t paw3204_get_cpi(void) { +uint16_t paw3204_get_cpi(const void* config) { + paw3204_config_t* paw3204_config = (paw3204_config_t*)config; + uint16_t cpival = 1000; - switch (paw3204_read_reg(REG_SETUP) & 0b111) { - case CPI400: + switch (paw3204_read_reg(paw3204_config, PAW3204_REG_SETUP) & 0b111) { + case PAW3204_CPI400: cpival = 400; break; - case CPI500: + case PAW3204_CPI500: cpival = 500; break; - case CPI600: + case PAW3204_CPI600: cpival = 600; break; - case CPI800: + case PAW3204_CPI800: cpival = 800; break; - case CPI1000: + case PAW3204_CPI1000: cpival = 1000; break; - case CPI1200: + case PAW3204_CPI1200: cpival = 1200; break; - case CPI1600: + case PAW3204_CPI1600: cpival = 1600; break; } return cpival; } -uint8_t read_pid_paw3204(void) { - return paw3204_read_reg(REG_PID1); +uint8_t read_pid_paw3204(paw3204_config_t* paw3204_config) { + return paw3204_read_reg(paw3204_config, PAW3204_REG_PID1); } + +report_mouse_t paw3204_get_report(const void* config) { + paw3204_config_t* paw3204_config = (paw3204_config_t*)config; + report_paw3204_t data = paw3204_read(paw3204_config); + report_mouse_t mouse_report = {0}; + if (data.isMotion) { + pd_dprintf("Raw ] X: %d, Y: %d\n", data.x, data.y); + + mouse_report.x = data.x; + mouse_report.y = data.y; + } + + return mouse_report; +} \ No newline at end of file diff --git a/drivers/sensors/paw3204.h b/drivers/sensors/paw3204.h index 7f487d90dcea..511c9405fa1b 100644 --- a/drivers/sensors/paw3204.h +++ b/drivers/sensors/paw3204.h @@ -18,6 +18,7 @@ #include #include +#include "pointing_device.h" #ifndef PAW3204_SCLK_PIN # ifdef POINTING_DEVICE_SCLK_PIN @@ -40,6 +41,17 @@ typedef struct { bool isMotion; } report_paw3204_t; +typedef struct { + pin_t sdio; + pin_t sclk; +} paw3204_config_t; + +const pointing_device_driver_t paw3204_driver_default; + +#if defined(PAW3204_SCLK_PIN) & defined(PAW3204_SDIO_PIN) +const paw3204_config_t paw3204_config_default; +#endif + /** * @brief Initializes the sensor so it is in a working state and ready to * be polled for data. @@ -47,7 +59,7 @@ typedef struct { * @return true Initialization was a success * @return false Initialization failed, do not proceed operation */ -void paw3204_init(void); +void paw3204_init(const void* config); /** * @brief Reads and clears the current delta, and motion register values on the @@ -57,7 +69,7 @@ void paw3204_init(void); * fields are set to zero */ -report_paw3204_t paw3204_read(void); +report_paw3204_t paw3204_read(paw3204_config_t* config); /** * @brief Sets the given CPI value the sensor. CPI is often refereed to * as the sensors sensitivity. Values outside of the allowed range are @@ -65,7 +77,7 @@ report_paw3204_t paw3204_read(void); * * @param cpi CPI value to set */ -void paw3204_set_cpi(uint16_t cpi); +void paw3204_set_cpi(const void* config, uint16_t cpi); /** * @brief Gets the currently set CPI value from the sensor. CPI is often @@ -73,4 +85,4 @@ void paw3204_set_cpi(uint16_t cpi); * * @return uint16_t Current CPI value of the sensor */ -uint16_t paw3204_get_cpi(void); +uint16_t paw3204_get_cpi(const void* config); diff --git a/quantum/pointing_device/pointing_device.c b/quantum/pointing_device/pointing_device.c index f567027808c0..2e9150b0d259 100644 --- a/quantum/pointing_device/pointing_device.c +++ b/quantum/pointing_device/pointing_device.c @@ -17,6 +17,7 @@ */ #include "pointing_device.h" +#include "pointing_device_internal.h" #include #include "timer.h" #include "util.h" @@ -242,9 +243,13 @@ bool pointing_deivce_task_get_pointing_reports(report_mouse_t* report) { bool pointing_device_task_handle_shared_report(report_mouse_t* local_report, bool* device_was_ready) { static uint8_t counter = 0; - if (is_keyboard_master()) { if (counter != shared_report.counter) { +#if defined(POINTING_DEVICE_DEBUG) + if (shared_report.counter != (((uint16_t)counter + 1) & UINT8_MAX)) { + pd_dprintf("POINTING DEVICE: Missed shared report - last report: %d, new report: %d\n", counter, shared_report.counter); + } +#endif pointing_device_add_and_clamp_report(local_report, &shared_report.report); counter = shared_report.counter; *device_was_ready = true; @@ -254,12 +259,8 @@ bool pointing_device_task_handle_shared_report(report_mouse_t* local_report, boo if (*device_was_ready) { if (pointing_device_report_ready(&shared_report.report, local_report, device_was_ready)) { memcpy(&shared_report, local_report, sizeof(report_mouse_t)); - shared_report.counter = counter; // FIX ME - can't use the report id for this counter. - if (counter == UINT8_MAX) { - counter = 0; - } else { - counter++; - } + shared_report.counter = counter; + counter = (((uint16_t)counter + 1) & UINT8_MAX); return true; } } diff --git a/quantum/pointing_device/pointing_device.h b/quantum/pointing_device/pointing_device.h index e12b679f5693..9ca2de62e893 100644 --- a/quantum/pointing_device/pointing_device.h +++ b/quantum/pointing_device/pointing_device.h @@ -21,6 +21,7 @@ along with this program. If not, see . #include "host.h" #include "report.h" #include "gpio.h" +#include "pointing_device_internal.h" #ifdef POINTING_DEVICE_AUTO_MOUSE_ENABLE # include "pointing_device_auto_mouse.h" @@ -109,15 +110,25 @@ typedef struct { uint8_t counter; } pointing_device_shared_report_t; + +#if defined(POINTING_DEVICE_DRIVER_ADNS5050) +# include "adns5050.h" +#endif #if defined(POINTING_DEVICE_DRIVER_ADNS9800) # include "adns9800.h" #endif +#if defined(POINTING_DEVICE_DRIVER_ANALOG_JOYSTICK) +# include "analog_joystick.h" +#endif #if defined(POINTING_DEVICE_DRIVER_AZOTEQ_IQS5XX) # include "azoteq_iqs5xx.h" #endif #if defined(POINTING_DEVICE_DRIVER_CIRQUE_PINNACLE_I2C) || defined(POINTING_DEVICE_DRIVER_CIRQUE_PINNACLE_SPI) # include "cirque_pinnacle.h" #endif +#if defined(POINTING_DEVICE_DRIVER_PAW3204) +# include "paw3204.h" +#endif #if defined(POINTING_DEVICE_DRIVER_PIMORONI_TRACKBALL) # include "pimoroni_trackball.h" #endif diff --git a/quantum/pointing_device/pointing_device_drivers.c b/quantum/pointing_device/pointing_device_drivers.c deleted file mode 100644 index 78643d82ee62..000000000000 --- a/quantum/pointing_device/pointing_device_drivers.c +++ /dev/null @@ -1,600 +0,0 @@ -/* Copyright 2017 Joshua Broekhuijsen - * 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 - * 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 . - */ - -#include "pointing_device.h" -#include "pointing_device_internal.h" -#include "debug.h" -#include "wait.h" -#include "timer.h" -#include - -#define CONSTRAIN_HID(amt) ((amt) < INT8_MIN ? INT8_MIN : ((amt) > INT8_MAX ? INT8_MAX : (amt))) -#define CONSTRAIN_HID_XY(amt) ((amt) < XY_REPORT_MIN ? XY_REPORT_MIN : ((amt) > XY_REPORT_MAX ? XY_REPORT_MAX : (amt))) - -// get_report functions should probably be moved to their respective drivers. - -#if defined(POINTING_DEVICE_DRIVER_adns5050) -report_mouse_t adns5050_get_report(report_mouse_t mouse_report) { - report_adns5050_t data = adns5050_read_burst(); - - if (data.dx != 0 || data.dy != 0) { - pd_dprintf("Raw ] X: %d, Y: %d\n", data.dx, data.dy); - mouse_report.x = (mouse_xy_report_t)data.dx; - mouse_report.y = (mouse_xy_report_t)data.dy; - } - - return mouse_report; -} - -// clang-format off -const pointing_device_driver_t pointing_device_driver = { - .init = adns5050_init, - .get_report = adns5050_get_report, - .set_cpi = adns5050_set_cpi, - .get_cpi = adns5050_get_cpi, -}; -// clang-format on - -#elif defined(POINTING_DEVICE_DRIVER_pmw3320) -report_mouse_t pmw3320_get_report(report_mouse_t mouse_report) { - report_pmw3320_t data = pmw3320_read_burst(); - - if (data.dx != 0 || data.dy != 0) { - pd_dprintf("Raw ] X: %d, Y: %d\n", data.dx, data.dy); - mouse_report.x = (mouse_xy_report_t)data.dx; - mouse_report.y = (mouse_xy_report_t)data.dy; - } - - return mouse_report; -} - -// clang-format off -const pointing_device_driver_t pointing_device_driver = { - .init = pmw3320_init, - .get_report = pmw3320_get_report, - .set_cpi = pmw3320_set_cpi, - .get_cpi = pmw3320_get_cpi, -}; -// clang-format on - -#elif defined(POINTING_DEVICE_DRIVER_adns9800) - -report_mouse_t adns9800_get_report_driver(report_mouse_t mouse_report) { - report_adns9800_t sensor_report = adns9800_get_report(); - - mouse_report.x = CONSTRAIN_HID_XY(sensor_report.x); - mouse_report.y = CONSTRAIN_HID_XY(sensor_report.y); - - return mouse_report; -} - -// clang-format off -const pointing_device_driver_t pointing_device_driver = { - .init = adns9800_init, - .get_report = adns9800_get_report_driver, - .set_cpi = adns9800_set_cpi, - .get_cpi = adns9800_get_cpi -}; -// clang-format on - -#elif defined(POINTING_DEVICE_DRIVER_analog_joystick) -report_mouse_t analog_joystick_get_report(report_mouse_t mouse_report) { - report_analog_joystick_t data = analog_joystick_read(); - - pd_dprintf("Raw ] X: %d, Y: %d\n", data.x, data.y); - - mouse_report.x = data.x; - mouse_report.y = data.y; - - mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, data.button, POINTING_DEVICE_BUTTON1); - - return mouse_report; -} - -// clang-format off -const pointing_device_driver_t pointing_device_driver = { - .init = analog_joystick_init, - .get_report = analog_joystick_get_report, - .set_cpi = NULL, - .get_cpi = NULL -}; -// clang-format on - -#elif defined(POINTING_DEVICE_DRIVER_ps2_trackpoint) - -#define X_IS_NEG (mouse_report->buttons & (1 << PS2_MOUSE_X_SIGN)) -#define Y_IS_NEG (mouse_report->buttons & (1 << PS2_MOUSE_Y_SIGN)) -#define X_IS_OVF (mouse_report->buttons & (1 << PS2_MOUSE_X_OVFLW)) -#define Y_IS_OVF (mouse_report->buttons & (1 << PS2_MOUSE_Y_OVFLW)) -static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) { - // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value. - // bit: 8 7 ... 0 - // sign \8-bit/ - // - // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used. - // - // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit. - mouse_report->x = X_IS_NEG ? ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) : ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127); - mouse_report->y = Y_IS_NEG ? ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) : ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127); - -#ifdef PS2_MOUSE_INVERT_BUTTONS - // swap left & right buttons - uint8_t needs_left = mouse_report->buttons & PS2_MOUSE_BTN_RIGHT; - uint8_t needs_right = mouse_report->buttons & PS2_MOUSE_BTN_LEFT; - mouse_report->buttons = (mouse_report->buttons & ~(PS2_MOUSE_BTN_MASK)) | (needs_left ? PS2_MOUSE_BTN_LEFT : 0) | (needs_right ? PS2_MOUSE_BTN_RIGHT : 0); -#else - // remove sign and overflow flags - mouse_report->buttons &= PS2_MOUSE_BTN_MASK; -#endif - -#ifdef PS2_MOUSE_INVERT_X - mouse_report->x = -mouse_report->x; -#endif -#ifndef PS2_MOUSE_INVERT_Y // NOTE if not! - // invert coordinate of y to conform to USB HID mouse - mouse_report->y = -mouse_report->y; -#endif - -#ifdef PS2_MOUSE_ROTATE - int8_t x = mouse_report->x; - int8_t y = mouse_report->y; -# if PS2_MOUSE_ROTATE == 90 - mouse_report->x = y; - mouse_report->y = -x; -# elif PS2_MOUSE_ROTATE == 180 - mouse_report->x = -x; - mouse_report->y = -y; -# elif PS2_MOUSE_ROTATE == 270 - mouse_report->x = -y; - mouse_report->y = x; -# endif -#endif -} - -static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report) { - mouse_report->x = 0; - mouse_report->y = 0; - mouse_report->v = 0; - mouse_report->h = 0; - mouse_report->buttons = 0; -} - -static inline void ps2_mouse_print_report(report_mouse_t *mouse_report) { - if (!debug_mouse) return; - print("ps2_mouse: ["); - print_hex8(mouse_report->buttons); - print("|"); - print_hex8((uint8_t)mouse_report->x); - print(" "); - print_hex8((uint8_t)mouse_report->y); - print(" "); - print_hex8((uint8_t)mouse_report->v); - print(" "); - print_hex8((uint8_t)mouse_report->h); - print("]\n"); -} - -static inline void ps2_mouse_enable_scrolling(void) { - PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Initiaing scroll wheel enable: Set sample rate"); - PS2_MOUSE_SEND(200, "200"); - PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate"); - PS2_MOUSE_SEND(100, "100"); - PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate"); - PS2_MOUSE_SEND(80, "80"); - PS2_MOUSE_SEND(PS2_MOUSE_GET_DEVICE_ID, "Finished enabling scroll wheel"); - wait_ms(20); -} - - -#define PRESS_SCROLL_BUTTONS mouse_report->buttons |= (PS2_MOUSE_SCROLL_BTN_MASK) -#define RELEASE_SCROLL_BUTTONS mouse_report->buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK) -static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report) { - static enum { - SCROLL_NONE, - SCROLL_BTN, - SCROLL_SENT, - } scroll_state = SCROLL_NONE; - static uint16_t scroll_button_time = 0; - - if (PS2_MOUSE_SCROLL_BTN_MASK == (mouse_report->buttons & (PS2_MOUSE_SCROLL_BTN_MASK))) { - // All scroll buttons are pressed - - if (scroll_state == SCROLL_NONE) { - scroll_button_time = timer_read(); - scroll_state = SCROLL_BTN; - } - - // If the mouse has moved, update the report to scroll instead of move the mouse - if (mouse_report->x || mouse_report->y) { - scroll_state = SCROLL_SENT; - mouse_report->v = -mouse_report->y / (PS2_MOUSE_SCROLL_DIVISOR_V); - mouse_report->h = mouse_report->x / (PS2_MOUSE_SCROLL_DIVISOR_H); - mouse_report->x = 0; - mouse_report->y = 0; -#ifdef PS2_MOUSE_INVERT_H - mouse_report->h = -mouse_report->h; -#endif -#ifdef PS2_MOUSE_INVERT_V - mouse_report->v = -mouse_report->v; -#endif - } - } else if (0 == (PS2_MOUSE_SCROLL_BTN_MASK & mouse_report->buttons)) { - // None of the scroll buttons are pressed - -#if PS2_MOUSE_SCROLL_BTN_SEND - if (scroll_state == SCROLL_BTN && timer_elapsed(scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) { - PRESS_SCROLL_BUTTONS; - host_mouse_send(mouse_report); - wait_ms(100); - RELEASE_SCROLL_BUTTONS; - } -#endif - scroll_state = SCROLL_NONE; - } - - RELEASE_SCROLL_BUTTONS; -} - -report_mouse_t ps2_trackpoint_get_report(report_mouse_t mouse_report) { - // static uint8_t buttons_prev = 0; - // extern int tp_buttons; - - /* receives packet from mouse */ -#ifdef PS2_MOUSE_USE_REMOTE_MODE - uint8_t rcv; - rcv = ps2_host_send(PS2_MOUSE_READ_DATA); - if (rcv == PS2_ACK) { - mouse_report.buttons = ps2_host_recv_response(); - mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER; - mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER; -// # ifdef PS2_MOUSE_ENABLE_SCROLLING -// mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER; -// # endif - } else { - if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n"); - } -#else - if (pbuf_has_data()) { - mouse_report.buttons = ps2_host_recv_response(); - mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER; - mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER; -// # ifdef PS2_MOUSE_ENABLE_SCROLLING -// mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER; -// # endif - } else { - // if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n"); - } -#endif - - // mouse_report.buttons |= tp_buttons; - /* if mouse moves or buttons state changes */ - if (mouse_report.x || mouse_report.y) { // || mouse_report.v || ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) { -#ifdef PS2_MOUSE_DEBUG_RAW - // Used to debug raw ps2 bytes from mouse - ps2_mouse_print_report(&mouse_report); -#endif - // buttons_prev = mouse_report.buttons; - ps2_mouse_convert_report_to_hid(&mouse_report); -// #if PS2_MOUSE_SCROLL_BTN_MASK -// ps2_mouse_scroll_button_task(&mouse_report); -// #endif - if (mouse_report.x || mouse_report.y || mouse_report.v) { - ps2_mouse_moved_user(&mouse_report); - } -// #ifdef PS2_MOUSE_DEBUG_HID - // Used to debug the bytes sent to the host - ps2_mouse_print_report(&mouse_report); -// #endif - } - - return mouse_report; -} - -// clang-format off -const pointing_device_driver_t pointing_device_driver = { - .init = ps2_mouse_init, - .get_report = ps2_trackpoint_get_report, - .set_cpi = NULL, - .get_cpi = NULL -}; -// clang-format on - -#elif defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_i2c) || defined(POINTING_DEVICE_DRIVER_cirque_pinnacle_spi) -# 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 - -# if CIRQUE_PINNACLE_POSITION_MODE - -# ifdef POINTING_DEVICE_AUTO_MOUSE_ENABLE -static bool is_touch_down; - -bool auto_mouse_activation(report_mouse_t mouse_report) { - return is_touch_down || mouse_report.x != 0 || mouse_report.y != 0 || mouse_report.h != 0 || mouse_report.v != 0 || mouse_report.buttons; -} -# endif - -report_mouse_t cirque_pinnacle_get_report(report_mouse_t mouse_report) { - uint16_t scale = cirque_pinnacle_get_scale(); - pinnacle_data_t touchData = cirque_pinnacle_read_data(); - mouse_xy_report_t report_x = 0, report_y = 0; - static uint16_t x = 0, y = 0, last_scale = 0; - -# if defined(CIRQUE_PINNACLE_TAP_ENABLE) - mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1); -# endif -# 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 (!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; - } - - if (touchData.touchDown) { - pd_dprintf("cirque_pinnacle touchData x=%4d y=%4d z=%2d\n", touchData.xValue, touchData.yValue, touchData.zValue); - } - -# ifdef POINTING_DEVICE_AUTO_MOUSE_ENABLE - is_touch_down = touchData.touchDown; -# endif - - // Scale coordinates to arbitrary X, Y resolution - cirque_pinnacle_scale_data(&touchData, scale, scale); - - if (!cirque_pinnacle_gestures(&mouse_report, touchData)) { - if (last_scale && scale == last_scale && x && y && touchData.xValue && touchData.yValue) { - report_x = CONSTRAIN_HID_XY((int16_t)(touchData.xValue - x)); - report_y = CONSTRAIN_HID_XY((int16_t)(touchData.yValue - y)); - } - x = touchData.xValue; - y = touchData.yValue; - last_scale = scale; - -# 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 - } - -# ifdef POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE -mouse_report_update: -# endif - mouse_report.x = report_x; - mouse_report.y = report_y; - - 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_cpi, - .get_cpi = cirque_pinnacle_get_cpi -}; -// clang-format on -# else -report_mouse_t cirque_pinnacle_get_report(report_mouse_t mouse_report) { - pinnacle_data_t touchData = cirque_pinnacle_read_data(); - - // Scale coordinates to arbitrary X, Y resolution - cirque_pinnacle_scale_data(&touchData, cirque_pinnacle_get_scale(), cirque_pinnacle_get_scale()); - - if (touchData.valid) { - mouse_report.buttons = touchData.buttons; - mouse_report.x = CONSTRAIN_HID_XY(touchData.xDelta); - mouse_report.y = CONSTRAIN_HID_XY(touchData.yDelta); - mouse_report.v = touchData.wheelCount; - } - return mouse_report; -} - -// 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 -}; -// clang-format on -# endif - -#elif defined(POINTING_DEVICE_DRIVER_paw3204) - -report_mouse_t paw3204_get_report(report_mouse_t mouse_report) { - report_paw3204_t data = paw3204_read(); - if (data.isMotion) { - pd_dprintf("Raw ] X: %d, Y: %d\n", data.x, data.y); - - mouse_report.x = data.x; - mouse_report.y = data.y; - } - - return mouse_report; -} -const pointing_device_driver_t pointing_device_driver = { - .init = paw3204_init, - .get_report = paw3204_get_report, - .set_cpi = paw3204_set_cpi, - .get_cpi = paw3204_get_cpi, -}; -#elif defined(POINTING_DEVICE_DRIVER_pimoroni_trackball) - -mouse_xy_report_t pimoroni_trackball_adapt_values(clamp_range_t* offset) { - if (*offset > XY_REPORT_MAX) { - *offset -= XY_REPORT_MAX; - return (mouse_xy_report_t)XY_REPORT_MAX; - } else if (*offset < XY_REPORT_MIN) { - *offset += XY_REPORT_MAX; - return (mouse_xy_report_t)XY_REPORT_MIN; - } else { - mouse_xy_report_t temp_return = *offset; - *offset = 0; - return temp_return; - } -} - -report_mouse_t pimoroni_trackball_get_report(report_mouse_t mouse_report) { - static uint16_t debounce = 0; - static uint8_t error_count = 0; - pimoroni_data_t pimoroni_data = {0}; - static clamp_range_t x_offset = 0, y_offset = 0; - - if (error_count < PIMORONI_TRACKBALL_ERROR_COUNT) { - i2c_status_t status = read_pimoroni_trackball(&pimoroni_data); - - if (status == I2C_STATUS_SUCCESS) { - error_count = 0; - - if (!(pimoroni_data.click & 128)) { - mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1); - if (!debounce) { - x_offset += pimoroni_trackball_get_offsets(pimoroni_data.right, pimoroni_data.left, PIMORONI_TRACKBALL_SCALE); - y_offset += pimoroni_trackball_get_offsets(pimoroni_data.down, pimoroni_data.up, PIMORONI_TRACKBALL_SCALE); - mouse_report.x = pimoroni_trackball_adapt_values(&x_offset); - mouse_report.y = pimoroni_trackball_adapt_values(&y_offset); - } else { - debounce--; - } - } else { - mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, true, POINTING_DEVICE_BUTTON1); - debounce = PIMORONI_TRACKBALL_DEBOUNCE_CYCLES; - } - } else { - error_count++; - } - } - return mouse_report; -} - -// clang-format off -const pointing_device_driver_t pointing_device_driver = { - .init = pimoroni_trackball_device_init, - .get_report = pimoroni_trackball_get_report, - .set_cpi = pimoroni_trackball_set_cpi, - .get_cpi = pimoroni_trackball_get_cpi -}; -// clang-format on - -#elif defined(POINTING_DEVICE_DRIVER_pmw3360) || defined(POINTING_DEVICE_DRIVER_pmw3389) -static void pmw33xx_init_wrapper(void) { - pmw33xx_init(0); -} - -static void pmw33xx_set_cpi_wrapper(uint16_t cpi) { - pmw33xx_set_cpi(0, cpi); -} - -static uint16_t pmw33xx_get_cpi_wrapper(void) { - return pmw33xx_get_cpi(0); -} - -report_mouse_t pmw33xx_get_report(report_mouse_t mouse_report) { - pmw33xx_report_t report = pmw33xx_read_burst(0); - static bool in_motion = false; - - if (report.motion.b.is_lifted) { - return mouse_report; - } - - if (!report.motion.b.is_motion) { - in_motion = false; - return mouse_report; - } - - if (!in_motion) { - in_motion = true; - pd_dprintf("PWM3360 (0): starting motion\n"); - } - - mouse_report.x = CONSTRAIN_HID_XY(report.delta_x); - mouse_report.y = CONSTRAIN_HID_XY(report.delta_y); - return mouse_report; -} - -// clang-format off -const pointing_device_driver_t pointing_device_driver = { - .init = pmw33xx_init_wrapper, - .get_report = pmw33xx_get_report, - .set_cpi = pmw33xx_set_cpi_wrapper, - .get_cpi = pmw33xx_get_cpi_wrapper -}; -// clang-format on - -#else -__attribute__((weak)) void pointing_device_driver_init(void) {} -__attribute__((weak)) report_mouse_t pointing_device_driver_get_report(report_mouse_t mouse_report) { - return mouse_report; -} -__attribute__((weak)) uint16_t pointing_device_driver_get_cpi(void) { - return 0; -} -__attribute__((weak)) void pointing_device_driver_set_cpi(uint16_t cpi) {} - -// clang-format off -const pointing_device_driver_t pointing_device_driver = { - .init = pointing_device_driver_init, - .get_report = pointing_device_driver_get_report, - .get_cpi = pointing_device_driver_get_cpi, - .set_cpi = pointing_device_driver_set_cpi -}; -// clang-format on - -#endif diff --git a/quantum/split_common/transactions.c b/quantum/split_common/transactions.c index 0388c4af605c..059f799ab7d2 100644 --- a/quantum/split_common/transactions.c +++ b/quantum/split_common/transactions.c @@ -647,6 +647,7 @@ static bool pointing_handlers_master(matrix_row_t master_matrix[], matrix_row_t bool okay = read_if_checksum_mismatch(GET_POINTING_CHECKSUM, GET_POINTING_DATA, &last_update, &target_report, &split_shmem->pointing.report, sizeof(pointing_device_shared_report_t)); if (okay) { if (counter != target_report.counter) { + counter = target_report.counter; pointing_device_set_shared_report(target_report); } }