Skip to content

Commit

Permalink
Move pointing device driver code (qmk#24445)
Browse files Browse the repository at this point in the history
Co-authored-by: Drashna Jaelre <[email protected]>
  • Loading branch information
2 people authored and smallketchup82 committed Dec 1, 2024
1 parent d6df61e commit d03199b
Show file tree
Hide file tree
Showing 23 changed files with 544 additions and 546 deletions.
2 changes: 1 addition & 1 deletion builddefs/common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ ifeq ($(strip $(POINTING_DEVICE_ENABLE)), yes)
MOUSE_ENABLE := yes
VPATH += $(QUANTUM_DIR)/pointing_device
SRC += $(QUANTUM_DIR)/pointing_device/pointing_device.c
SRC += $(QUANTUM_DIR)/pointing_device/pointing_device_drivers.c
SRC += $(QUANTUM_DIR)/pointing_device/pointing_device_auto_mouse.c
ifneq ($(strip $(POINTING_DEVICE_DRIVER)), custom)
SRC += drivers/sensors/$(strip $(POINTING_DEVICE_DRIVER)).c
OPT_DEFS += -DPOINTING_DEVICE_DRIVER_$(strip $(shell echo $(POINTING_DEVICE_DRIVER) | tr '[:lower:]' '[:upper:]'))
endif
OPT_DEFS += -DPOINTING_DEVICE_DRIVER_$(strip $(POINTING_DEVICE_DRIVER))
OPT_DEFS += -DPOINTING_DEVICE_DRIVER_NAME=$(strip $(POINTING_DEVICE_DRIVER))
ifeq ($(strip $(POINTING_DEVICE_DRIVER)), adns9800)
SPI_DRIVER_REQUIRED = yes
else ifeq ($(strip $(POINTING_DEVICE_DRIVER)), analog_joystick)
Expand Down
20 changes: 20 additions & 0 deletions drivers/sensors/adns5050.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "wait.h"
#include "debug.h"
#include "gpio.h"
#include "pointing_device_internal.h"

// Registers
// clang-format off
Expand All @@ -45,6 +46,13 @@
#define REG_MOTION_BURST 0x63
// clang-format on

const pointing_device_driver_t adns5050_pointing_device_driver = {
.init = adns5050_init,
.get_report = adns5050_get_report,
.set_cpi = adns5050_set_cpi,
.get_cpi = adns5050_get_cpi,
};

static bool powered_down = false;

void adns5050_init(void) {
Expand Down Expand Up @@ -226,3 +234,15 @@ void adns5050_power_down(void) {
adns5050_write_reg(REG_MOUSE_CONTROL, 0b10);
}
}

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;
}
4 changes: 4 additions & 0 deletions drivers/sensors/adns5050.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <stdbool.h>
#include <stdint.h>
#include "pointing_device.h"

// CPI values
// clang-format off
Expand Down Expand Up @@ -69,6 +70,8 @@ typedef struct {
int8_t dy;
} report_adns5050_t;

const pointing_device_driver_t adns5050_pointing_device_driver;

// 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.
Expand All @@ -84,3 +87,4 @@ uint16_t adns5050_get_cpi(void);
int8_t convert_twoscomp(uint8_t data);
bool adns5050_check_signature(void);
void adns5050_power_down(void);
report_mouse_t adns5050_get_report(report_mouse_t mouse_report);
16 changes: 16 additions & 0 deletions drivers/sensors/adns9800.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@
#define MSB1 0x80
// clang-format on

const pointing_device_driver_t adns9800_pointing_device_driver = {
.init = adns9800_init,
.get_report = adns9800_get_report_driver,
.set_cpi = adns9800_set_cpi,
.get_cpi = adns9800_get_cpi,
};

uint16_t __attribute__((weak)) adns9800_srom_get_length(void) {
return 0;
}
Expand Down Expand Up @@ -236,3 +243,12 @@ report_adns9800_t adns9800_get_report(void) {

return report;
}

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;
}
4 changes: 4 additions & 0 deletions drivers/sensors/adns9800.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#pragma once

#include <stdint.h>
#include "pointing_device.h"

#ifndef ADNS9800_CPI
# define ADNS9800_CPI 1600
Expand Down Expand Up @@ -60,10 +61,13 @@ typedef struct {
int16_t y;
} report_adns9800_t;

const pointing_device_driver_t adns9800_pointing_device_driver;

void adns9800_init(void);
config_adns9800_t adns9800_get_config(void);
void adns9800_set_config(config_adns9800_t);
uint16_t adns9800_get_cpi(void);
void adns9800_set_cpi(uint16_t cpi);
/* Reads and clears the current delta values on the ADNS sensor */
report_adns9800_t adns9800_get_report(void);
report_mouse_t adns9800_get_report_driver(report_mouse_t mouse_report);
21 changes: 21 additions & 0 deletions drivers/sensors/analog_joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
#include "wait.h"
#include "timer.h"
#include <stdlib.h>
#include "pointing_device_internal.h"

const pointing_device_driver_t analog_joystick_pointing_device_driver = {
.init = analog_joystick_init,
.get_report = analog_joystick_get_report,
.set_cpi = NULL,
.get_cpi = NULL,
};

// Set Parameters
#ifndef ANALOG_JOYSTICK_AUTO_AXIS
Expand Down Expand Up @@ -145,3 +153,16 @@ void analog_joystick_init(void) {
maxAxisValues[1] = yOrigin + 100;
#endif
}

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;
}
4 changes: 4 additions & 0 deletions drivers/sensors/analog_joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <stdbool.h>
#include <stdint.h>
#include "pointing_device.h"

#ifndef ANALOG_JOYSTICK_X_AXIS_PIN
# error No pin specified for X Axis
Expand All @@ -42,10 +43,13 @@
# define ANALOG_JOYSTICK_SPEED_MAX 2
#endif

const pointing_device_driver_t analog_joystick_pointing_device_driver;

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);
report_mouse_t analog_joystick_get_report(report_mouse_t mouse_report);
109 changes: 109 additions & 0 deletions drivers/sensors/azoteq_iqs5xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@
#define AZOTEQ_IQS5XX_INCH_TO_RESOLUTION_Y(inch) (DIVIDE_UNSIGNED_ROUND((inch) * (uint32_t)AZOTEQ_IQS5XX_HEIGHT_MM * 10, 254))
#define AZOTEQ_IQS5XX_RESOLUTION_Y_TO_INCH(px) (DIVIDE_UNSIGNED_ROUND((px) * (uint32_t)254, AZOTEQ_IQS5XX_HEIGHT_MM * 10))

const pointing_device_driver_t azoteq_iqs5xx_pointing_device_driver = {
.init = azoteq_iqs5xx_init,
.get_report = azoteq_iqs5xx_get_report,
.set_cpi = azoteq_iqs5xx_set_cpi,
.get_cpi = azoteq_iqs5xx_get_cpi,
};

static uint16_t azoteq_iqs5xx_product_number = AZOTEQ_IQS5XX_UNKNOWN;

static struct {
Expand Down Expand Up @@ -312,3 +319,105 @@ void azoteq_iqs5xx_setup_resolution(void) {
azoteq_iqs5xx_device_resolution_t.resolution_y = AZOTEQ_IQS5XX_RESOLUTION_Y;
#endif
}

static i2c_status_t azoteq_iqs5xx_init_status = 1;

void azoteq_iqs5xx_init(void) {
i2c_init();
azoteq_iqs5xx_wake();
azoteq_iqs5xx_reset_suspend(true, false, true);
wait_ms(100);
azoteq_iqs5xx_wake();
if (azoteq_iqs5xx_get_product() != AZOTEQ_IQS5XX_UNKNOWN) {
azoteq_iqs5xx_setup_resolution();
azoteq_iqs5xx_init_status = azoteq_iqs5xx_set_report_rate(AZOTEQ_IQS5XX_REPORT_RATE, AZOTEQ_IQS5XX_ACTIVE, false);
azoteq_iqs5xx_init_status |= azoteq_iqs5xx_set_event_mode(false, false);
azoteq_iqs5xx_init_status |= azoteq_iqs5xx_set_reati(true, false);
#if defined(AZOTEQ_IQS5XX_ROTATION_90)
azoteq_iqs5xx_init_status |= azoteq_iqs5xx_set_xy_config(false, true, true, true, false);
#elif defined(AZOTEQ_IQS5XX_ROTATION_180)
azoteq_iqs5xx_init_status |= azoteq_iqs5xx_set_xy_config(true, true, false, true, false);
#elif defined(AZOTEQ_IQS5XX_ROTATION_270)
azoteq_iqs5xx_init_status |= azoteq_iqs5xx_set_xy_config(true, false, true, true, false);
#else
azoteq_iqs5xx_init_status |= azoteq_iqs5xx_set_xy_config(false, false, false, true, false);
#endif
azoteq_iqs5xx_init_status |= azoteq_iqs5xx_set_gesture_config(true);
wait_ms(AZOTEQ_IQS5XX_REPORT_RATE + 1);
}
};

report_mouse_t azoteq_iqs5xx_get_report(report_mouse_t mouse_report) {
report_mouse_t temp_report = {0};
static uint8_t previous_button_state = 0;
static uint8_t read_error_count = 0;

if (azoteq_iqs5xx_init_status == I2C_STATUS_SUCCESS) {
azoteq_iqs5xx_base_data_t base_data = {0};
#if !defined(POINTING_DEVICE_MOTION_PIN)
azoteq_iqs5xx_wake();
#endif
i2c_status_t status = azoteq_iqs5xx_get_base_data(&base_data);
bool ignore_movement = false;

if (status == I2C_STATUS_SUCCESS) {
// pd_dprintf("IQS5XX - previous cycle time: %d \n", base_data.previous_cycle_time);
read_error_count = 0;
if (base_data.gesture_events_0.single_tap || base_data.gesture_events_0.press_and_hold) {
pd_dprintf("IQS5XX - Single tap/hold.\n");
temp_report.buttons = pointing_device_handle_buttons(temp_report.buttons, true, POINTING_DEVICE_BUTTON1);
} else if (base_data.gesture_events_1.two_finger_tap) {
pd_dprintf("IQS5XX - Two finger tap.\n");
temp_report.buttons = pointing_device_handle_buttons(temp_report.buttons, true, POINTING_DEVICE_BUTTON2);
} else if (base_data.gesture_events_0.swipe_x_neg) {
pd_dprintf("IQS5XX - X-.\n");
temp_report.buttons = pointing_device_handle_buttons(temp_report.buttons, true, POINTING_DEVICE_BUTTON4);
ignore_movement = true;
} else if (base_data.gesture_events_0.swipe_x_pos) {
pd_dprintf("IQS5XX - X+.\n");
temp_report.buttons = pointing_device_handle_buttons(temp_report.buttons, true, POINTING_DEVICE_BUTTON5);
ignore_movement = true;
} else if (base_data.gesture_events_0.swipe_y_neg) {
pd_dprintf("IQS5XX - Y-.\n");
temp_report.buttons = pointing_device_handle_buttons(temp_report.buttons, true, POINTING_DEVICE_BUTTON6);
ignore_movement = true;
} else if (base_data.gesture_events_0.swipe_y_pos) {
pd_dprintf("IQS5XX - Y+.\n");
temp_report.buttons = pointing_device_handle_buttons(temp_report.buttons, true, POINTING_DEVICE_BUTTON3);
ignore_movement = true;
} else if (base_data.gesture_events_1.zoom) {
if (AZOTEQ_IQS5XX_COMBINE_H_L_BYTES(base_data.x.h, base_data.x.l) < 0) {
pd_dprintf("IQS5XX - Zoom out.\n");
temp_report.buttons = pointing_device_handle_buttons(temp_report.buttons, true, POINTING_DEVICE_BUTTON7);
} else if (AZOTEQ_IQS5XX_COMBINE_H_L_BYTES(base_data.x.h, base_data.x.l) > 0) {
pd_dprintf("IQS5XX - Zoom in.\n");
temp_report.buttons = pointing_device_handle_buttons(temp_report.buttons, true, POINTING_DEVICE_BUTTON8);
}
} else if (base_data.gesture_events_1.scroll) {
pd_dprintf("IQS5XX - Scroll.\n");
temp_report.h = CONSTRAIN_HID(AZOTEQ_IQS5XX_COMBINE_H_L_BYTES(base_data.x.h, base_data.x.l));
temp_report.v = CONSTRAIN_HID(AZOTEQ_IQS5XX_COMBINE_H_L_BYTES(base_data.y.h, base_data.y.l));
}
if (base_data.number_of_fingers == 1 && !ignore_movement) {
temp_report.x = CONSTRAIN_HID_XY(AZOTEQ_IQS5XX_COMBINE_H_L_BYTES(base_data.x.h, base_data.x.l));
temp_report.y = CONSTRAIN_HID_XY(AZOTEQ_IQS5XX_COMBINE_H_L_BYTES(base_data.y.h, base_data.y.l));
}

previous_button_state = temp_report.buttons;

} else {
if (read_error_count > 10) {
read_error_count = 0;
previous_button_state = 0;
} else {
read_error_count++;
}
temp_report.buttons = previous_button_state;
pd_dprintf("IQS5XX - get report failed: %d \n", status);
}
} else {
pd_dprintf("IQS5XX - Init failed: %d \n", azoteq_iqs5xx_init_status);
}

return temp_report;
}
2 changes: 2 additions & 0 deletions drivers/sensors/azoteq_iqs5xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ typedef struct {
# define POINTING_DEVICE_TASK_THROTTLE_MS AZOTEQ_IQS5XX_REPORT_RATE
#endif

const pointing_device_driver_t azoteq_iqs5xx_pointing_device_driver;

void azoteq_iqs5xx_init(void);
i2c_status_t azoteq_iqs5xx_wake(void);
report_mouse_t azoteq_iqs5xx_get_report(report_mouse_t mouse_report);
Expand Down
Loading

0 comments on commit d03199b

Please sign in to comment.