From 94d5fe6f908077e22a2445b3accda8d130135156 Mon Sep 17 00:00:00 2001
From: Thomas <mail@tpreisner.de>
Date: Sat, 24 Sep 2022 17:44:14 +0200
Subject: [PATCH] Allow Active High for Pointing Device Motion Pin (#18404)

Needed by the Cirque Trackpad for motion detection
---
 docs/feature_pointing_device.md           | 1 +
 quantum/pointing_device/pointing_device.c | 8 ++++++++
 quantum/pointing_device/pointing_device.h | 6 ++++++
 3 files changed, 15 insertions(+)

diff --git a/docs/feature_pointing_device.md b/docs/feature_pointing_device.md
index 4a2919168c..0771203cdd 100644
--- a/docs/feature_pointing_device.md
+++ b/docs/feature_pointing_device.md
@@ -289,6 +289,7 @@ void           pointing_device_driver_set_cpi(uint16_t cpi) {}
 | `POINTING_DEVICE_INVERT_X`                     | (Optional) Inverts the X axis report.                                                                                            | _not defined_ |
 | `POINTING_DEVICE_INVERT_Y`                     | (Optional) Inverts the Y axis report.                                                                                            | _not defined_ |
 | `POINTING_DEVICE_MOTION_PIN`                   | (Optional) If supported, will only read from sensor if pin is active.                                                            | _not defined_ |
+| `POINTING_DEVICE_MOTION_PIN_ACTIVE_LOW`        | (Optional) If defined then the motion pin is active-low.                                                                         | _varies_      |
 | `POINTING_DEVICE_TASK_THROTTLE_MS`             | (Optional) Limits the frequency that the sensor is polled for motion.                                                            | _not defined_ |
 | `POINTING_DEVICE_GESTURES_CURSOR_GLIDE_ENABLE` | (Optional) Enable inertial cursor. Cursor continues moving after a flick gesture and slows down by kinetic friction.             | _not defined_ |
 | `POINTING_DEVICE_GESTURES_SCROLL_ENABLE`       | (Optional) Enable scroll gesture. The gesture that activates the scroll is device dependent.                                     | _not defined_ |
diff --git a/quantum/pointing_device/pointing_device.c b/quantum/pointing_device/pointing_device.c
index e91de018f1..6981f850d0 100644
--- a/quantum/pointing_device/pointing_device.c
+++ b/quantum/pointing_device/pointing_device.c
@@ -144,7 +144,11 @@ __attribute__((weak)) void pointing_device_init(void) {
     {
         pointing_device_driver.init();
 #ifdef POINTING_DEVICE_MOTION_PIN
+#    ifdef POINTING_DEVICE_MOTION_PIN_ACTIVE_LOW
         setPinInputHigh(POINTING_DEVICE_MOTION_PIN);
+#    else
+        setPinInput(POINTING_DEVICE_MOTION_PIN);
+#    endif
 #endif
     }
 
@@ -236,7 +240,11 @@ __attribute__((weak)) void pointing_device_task(void) {
 #    if defined(SPLIT_POINTING_ENABLE)
 #        error POINTING_DEVICE_MOTION_PIN not supported when sharing the pointing device report between sides.
 #    endif
+#    ifdef POINTING_DEVICE_MOTION_PIN_ACTIVE_LOW
     if (!readPin(POINTING_DEVICE_MOTION_PIN))
+#    else
+    if (readPin(POINTING_DEVICE_MOTION_PIN))
+#    endif
 #endif
 
 #if defined(SPLIT_POINTING_ENABLE)
diff --git a/quantum/pointing_device/pointing_device.h b/quantum/pointing_device/pointing_device.h
index a002bd4b25..27a3f95e32 100644
--- a/quantum/pointing_device/pointing_device.h
+++ b/quantum/pointing_device/pointing_device.h
@@ -27,18 +27,22 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #if defined(POINTING_DEVICE_DRIVER_adns5050)
 #    include "drivers/sensors/adns5050.h"
+#    define POINTING_DEVICE_MOTION_PIN_ACTIVE_LOW
 #elif defined(POINTING_DEVICE_DRIVER_adns9800)
 #    include "spi_master.h"
 #    include "drivers/sensors/adns9800.h"
+#    define POINTING_DEVICE_MOTION_PIN_ACTIVE_LOW
 #elif defined(POINTING_DEVICE_DRIVER_analog_joystick)
 #    include "analog.h"
 #    include "drivers/sensors/analog_joystick.h"
+#    define POINTING_DEVICE_MOTION_PIN_ACTIVE_LOW
 #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_paw3204)
 #    include "drivers/sensors/paw3204.h"
+#    define POINTING_DEVICE_MOTION_PIN_ACTIVE_LOW
 #elif defined(POINTING_DEVICE_DRIVER_pimoroni_trackball)
 #    include "i2c_master.h"
 #    include "drivers/sensors/pimoroni_trackball.h"
@@ -52,9 +56,11 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #    ifdef PIMORONI_TRACKBALL_ROTATE
 #        define POINTING_DEVICE_ROTATION_90
 #    endif
+#    define POINTING_DEVICE_MOTION_PIN_ACTIVE_LOW
 #elif defined(POINTING_DEVICE_DRIVER_pmw3360) || defined(POINTING_DEVICE_DRIVER_pmw3389)
 #    include "spi_master.h"
 #    include "drivers/sensors/pmw33xx_common.h"
+#    define POINTING_DEVICE_MOTION_PIN_ACTIVE_LOW
 #else
 void           pointing_device_driver_init(void);
 report_mouse_t pointing_device_driver_get_report(report_mouse_t mouse_report);