diff options
author | Drashna Jaelre <drashna@live.com> | 2021-11-14 22:03:24 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-14 22:03:24 -0800 |
commit | 56e3f06a26851976e559aacf7a096c61403304be (patch) | |
tree | 1e9ec98ad239fdd241e77ac4c4822fc2721a9cea /keyboards | |
parent | 462c3a615113e84ac3ca837a5caeb928c0ec8505 (diff) |
Rework and expand Pointing Device support (#14343)
Co-authored-by: Dasky <32983009+daskygit@users.noreply.github.com>
Diffstat (limited to 'keyboards')
35 files changed, 169 insertions, 1220 deletions
diff --git a/keyboards/oddball/pmw/pmw.h b/keyboards/40percentclub/nano/keymaps/drashna/config.h index cc3c9ec5eb..411ee8a81b 100644 --- a/keyboards/oddball/pmw/pmw.h +++ b/keyboards/40percentclub/nano/keymaps/drashna/config.h @@ -1,4 +1,4 @@ -/* Copyright 2020 Alexander Tulloh +/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com> * * 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 @@ -16,20 +16,7 @@ #pragma once -#include <stdint.h> +#define ANALOG_JOYSTICK_X_AXIS_PIN B4 +#define ANALOG_JOYSTICK_Y_AXIS_PIN B5 -typedef struct { - /* 100 - 12000 CPI supported */ - uint16_t cpi; -} config_pmw_t; - -typedef struct { - int16_t x; - int16_t y; -} report_pmw_t; - -void pmw_init(void); -config_pmw_t pmw_get_config(void); -void pmw_set_config(config_pmw_t); -/* Reads and clears the current delta values on the PMW sensor */ -report_pmw_t pmw_get_report(void); +#define ANALOG_JOYSTICK_CLICK_PIN E6 diff --git a/keyboards/40percentclub/nano/keymaps/drashna/keymap.c b/keyboards/40percentclub/nano/keymaps/drashna/keymap.c index 13f89dd53d..cc5991d7a2 100644 --- a/keyboards/40percentclub/nano/keymaps/drashna/keymap.c +++ b/keyboards/40percentclub/nano/keymaps/drashna/keymap.c @@ -29,98 +29,3 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { }; // clang-format on - -// Joystick -// Set Pins -// uint8_t xPin = 8; // VRx / /B4 -// uint8_t yPin = 7; // VRy // B5 -uint8_t swPin = E6; // SW - -// Set Parameters -uint16_t minAxisValue = 0; -uint16_t maxAxisValue = 1023; - -uint8_t maxCursorSpeed = 2; -uint8_t precisionSpeed = 1; -uint8_t speedRegulator = 20; // Lower Values Create Faster Movement - -int8_t xPolarity = 1; -int8_t yPolarity = 1; - -uint8_t cursorTimeout = 10; - -int16_t xOrigin, yOrigin; - -uint16_t lastCursor = 0; - -int16_t axisCoordinate(uint8_t pin, uint16_t origin) { - int8_t direction; - int16_t distanceFromOrigin; - int16_t range; - - int16_t position = analogReadPin(pin); - - if (origin == position) { - return 0; - } else if (origin > position) { - distanceFromOrigin = origin - position; - range = origin - minAxisValue; - direction = -1; - } else { - distanceFromOrigin = position - origin; - range = maxAxisValue - origin; - direction = 1; - } - - float percent = (float)distanceFromOrigin / range; - int16_t coordinate = (int16_t)(percent * 100); - if (coordinate < 0) { - return 0; - } else if (coordinate > 100) { - return 100 * direction; - } else { - return coordinate * direction; - } -} - -int8_t axisToMouseComponent(uint8_t pin, int16_t origin, uint8_t maxSpeed, int8_t polarity) { - int coordinate = axisCoordinate(pin, origin); - if (coordinate != 0) { - float percent = (float)coordinate / 100; - if (get_mods() & MOD_BIT(KC_LSFT)) { - return percent * precisionSpeed * polarity * (abs(coordinate) / speedRegulator); - } else { - return percent * maxCursorSpeed * polarity * (abs(coordinate) / speedRegulator); - } - } else { - return 0; - } -} - -void pointing_device_task(void) { - report_mouse_t report = pointing_device_get_report(); - - // todo read as one vector - if (timer_elapsed(lastCursor) > cursorTimeout) { - lastCursor = timer_read(); - report.x = axisToMouseComponent(B4, xOrigin, maxCursorSpeed, xPolarity); - report.y = axisToMouseComponent(B5, yOrigin, maxCursorSpeed, yPolarity); - } - // - if (!readPin(E6)) { - report.buttons |= MOUSE_BTN1; - } else { - report.buttons &= ~MOUSE_BTN1; - } - - pointing_device_set_report(report); - pointing_device_send(); -} - -void matrix_init_keymap(void) { - // init pin? Is needed? - setPinInputHigh(E6); - // Account for drift - xOrigin = analogReadPin(B4); - yOrigin = analogReadPin(B5); -} diff --git a/keyboards/40percentclub/nano/keymaps/drashna/rules.mk b/keyboards/40percentclub/nano/keymaps/drashna/rules.mk index 2b72a112b0..aa7966a8b5 100644 --- a/keyboards/40percentclub/nano/keymaps/drashna/rules.mk +++ b/keyboards/40percentclub/nano/keymaps/drashna/rules.mk @@ -1,7 +1,6 @@ -POINTING_DEVICE_ENABLE = yes -RGBLIGHT_ENABLE = no -CONSOLE_ENABLE = no +POINTING_DEVICE_ENABLE = yes +POINTING_DEVICE_DRIVER = analog_joystick +RGBLIGHT_ENABLE = no +CONSOLE_ENABLE = no -BOOTLOADER = qmk-dfu - -SRC += analog.c +BOOTLOADER = qmk-dfu diff --git a/keyboards/handwired/tractyl_manuform/5x6_right/config.h b/keyboards/handwired/tractyl_manuform/5x6_right/config.h index ce35197f71..6f702bd59e 100644 --- a/keyboards/handwired/tractyl_manuform/5x6_right/config.h +++ b/keyboards/handwired/tractyl_manuform/5x6_right/config.h @@ -31,6 +31,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define DIODE_DIRECTION COL2ROW #define ROTATIONAL_TRANSFORM_ANGLE -25 +#define POINTING_DEVICE_INVERT_X /* Bootmagic Lite key configuration */ #define BOOTMAGIC_LITE_ROW 0 diff --git a/keyboards/handwired/tractyl_manuform/5x6_right/keymaps/drashna/keymap.c b/keyboards/handwired/tractyl_manuform/5x6_right/keymaps/drashna/keymap.c index 5bfc21fd14..1275950766 100644 --- a/keyboards/handwired/tractyl_manuform/5x6_right/keymaps/drashna/keymap.c +++ b/keyboards/handwired/tractyl_manuform/5x6_right/keymaps/drashna/keymap.c @@ -174,7 +174,11 @@ bool tap_toggling = false; # define TAP_CHECK TAPPING_TERM # endif -void process_mouse_user(report_mouse_t* mouse_report, int8_t x, int8_t y) { +report_mouse_t pointing_device_task_user(report_mouse_t mouse_report) { + int8_t x = mouse_report.x, y = mouse_report.y; + mouse_report.x = 0; + mouse_report.y = 0; + if (x != 0 && y != 0) { mouse_timer = timer_read(); # ifdef OLED_ENABLE @@ -185,13 +189,14 @@ void process_mouse_user(report_mouse_t* mouse_report, int8_t x, int8_t y) { x = (x > 0 ? x * x / 16 + x : -x * x / 16 + x); y = (y > 0 ? y * y / 16 + y : -y * y / 16 + y); } - mouse_report->x = x; - mouse_report->y = y; + mouse_report.x = x; + mouse_report.y = y; if (!layer_state_is(_MOUSE)) { layer_on(_MOUSE); } } } + return mouse_report; } void matrix_scan_keymap(void) { diff --git a/keyboards/handwired/tractyl_manuform/5x6_right/rules.mk b/keyboards/handwired/tractyl_manuform/5x6_right/rules.mk index c9bc2dbb99..699e826485 100644 --- a/keyboards/handwired/tractyl_manuform/5x6_right/rules.mk +++ b/keyboards/handwired/tractyl_manuform/5x6_right/rules.mk @@ -16,11 +16,11 @@ AUDIO_ENABLE = no # Audio output SWAP_HANDS_ENABLE = yes POINTING_DEVICE_ENABLE = yes +POINTING_DEVICE_DRIVER = pmw3360 MOUSE_SHARED_EP = no SPLIT_KEYBOARD = yes -SRC += drivers/sensors/pmw3360.c -QUANTUM_LIB_SRC += spi_master.c tm_sync.c +QUANTUM_LIB_SRC += tm_sync.c DEFAULT_FOLDER = handwired/tractyl_manuform/5x6_right/teensy2pp diff --git a/keyboards/handwired/tractyl_manuform/tm_sync.c b/keyboards/handwired/tractyl_manuform/tm_sync.c index 1cb3549469..4739af1e76 100644 --- a/keyboards/handwired/tractyl_manuform/tm_sync.c +++ b/keyboards/handwired/tractyl_manuform/tm_sync.c @@ -17,7 +17,6 @@ #include "tractyl_manuform.h" #include "transactions.h" #include <string.h> -#include "drivers/sensors/pmw3360.h" kb_config_data_t kb_config; kb_mouse_report_t sync_mouse_report; @@ -82,6 +81,6 @@ void housekeeping_task_sync(void) { void trackball_set_cpi(uint16_t cpi) { kb_config.device_cpi = cpi; if (!is_keyboard_left()) { - pmw_set_cpi(cpi); + pointing_device_set_cpi(cpi); } } diff --git a/keyboards/handwired/tractyl_manuform/tractyl_manuform.c b/keyboards/handwired/tractyl_manuform/tractyl_manuform.c index d851528ffb..5f476fcc98 100644 --- a/keyboards/handwired/tractyl_manuform/tractyl_manuform.c +++ b/keyboards/handwired/tractyl_manuform/tractyl_manuform.c @@ -34,55 +34,7 @@ keyboard_config_t keyboard_config; uint16_t dpi_array[] = TRACKBALL_DPI_OPTIONS; #define DPI_OPTION_SIZE (sizeof(dpi_array) / sizeof(uint16_t)) -bool BurstState = false; // init burst state for Trackball module -uint16_t MotionStart = 0; // Timer for accel, 0 is resting state -__attribute__((weak)) void process_mouse_user(report_mouse_t* mouse_report, int8_t x, int8_t y) { - mouse_report->x = x; - mouse_report->y = y; -} - -__attribute__((weak)) void process_mouse(void) { - report_pmw_t data = pmw_read_burst(); - // Reset timer if stopped moving - if (!data.isMotion) { - if (MotionStart != 0) MotionStart = 0; - return; - } - - if (data.isOnSurface) { - // Set timer if new motion - if (MotionStart == 0) { - if (debug_mouse) dprintf("Starting motion.\n"); - MotionStart = timer_read(); - } - - if (debug_mouse) { - dprintf("Delt] d: %d t: %u\n", abs(data.dx) + abs(data.dy), MotionStart); - } - if (debug_mouse) { - dprintf("Pre ] X: %d, Y: %d\n", data.dx, data.dy); - } -#if defined(PROFILE_LINEAR) - float scale = float(timer_elaspsed(MotionStart)) / 1000.0; - data.dx *= scale; - data.dy *= scale; -#elif defined(PROFILE_INVERSE) - // TODO -#else - // no post processing -#endif - - // Wrap to HID size - data.dx = constrain(data.dx, -127, 127); - data.dy = constrain(data.dy, -127, 127); - if (debug_mouse) dprintf("Cons] X: %d, Y: %d\n", data.dx, data.dy); - // dprintf("Elapsed:%u, X: %f Y: %\n", i, pgm_read_byte(firmware_data+i)); - - sync_mouse_report.x = -data.dx; - sync_mouse_report.y = data.dy; - } -} bool process_record_kb(uint16_t keycode, keyrecord_t* record) { if (!process_record_user(keycode, record)) { @@ -109,11 +61,7 @@ bool process_record_kb(uint16_t keycode, keyrecord_t* record) { #ifndef MOUSEKEY_ENABLE if (IS_MOUSEKEY_BUTTON(keycode)) { report_mouse_t currentReport = pointing_device_get_report(); - if (record->event.pressed) { - currentReport.buttons |= 1 << (keycode - KC_MS_BTN1); - } else { - currentReport.buttons &= ~(1 << (keycode - KC_MS_BTN1)); - } + currentReport.buttons = pointing_device_handle_buttons(currentReport.buttons, record->event.pressed, keycode - KC_MS_BTN1); pointing_device_set_report(currentReport); pointing_device_send(); } @@ -145,33 +93,28 @@ void keyboard_post_init_kb(void) { } #ifdef POINTING_DEVICE_ENABLE -void pointing_device_init(void) { - if (!is_keyboard_left()) { - // initialize ball sensor - pmw_spi_init(); - } +void pointing_device_init_kb(void) { trackball_set_cpi(dpi_array[keyboard_config.dpi_config]); + pointing_device_init_user(); } -void pointing_device_task(void) { - report_mouse_t mouse_report = pointing_device_get_report(); - +report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) { if (is_keyboard_left()) { if (is_keyboard_master()) { transaction_rpc_recv(RPC_ID_POINTER_STATE_SYNC, sizeof(sync_mouse_report), &sync_mouse_report); - process_mouse_user(&mouse_report, sync_mouse_report.x, sync_mouse_report.y); + mouse_report.x = sync_mouse_report.x; + mouse_report.y = sync_mouse_report.y; + pointing_device_task_user(mouse_report); } } else { - process_mouse(); if (is_keyboard_master()) { - process_mouse_user(&mouse_report, sync_mouse_report.x, sync_mouse_report.y); - sync_mouse_report.x = 0; - sync_mouse_report.y = 0; + pointing_device_task_user(mouse_report); + } else { + sync_mouse_report.x = mouse_report.x; + sync_mouse_report.y = mouse_report.y; } } - - pointing_device_set_report(mouse_report); - pointing_device_send(); + return mouse_report; } #endif diff --git a/keyboards/oddball/config.h b/keyboards/oddball/config.h index 050084d2d3..4bc9c7997c 100644 --- a/keyboards/oddball/config.h +++ b/keyboards/oddball/config.h @@ -58,3 +58,6 @@ /* Bootmagic Lite key configuration */ // #define BOOTMAGIC_LITE_ROW 0 // #define BOOTMAGIC_LITE_COLUMN 0 + +#define ADNS9800_CS_PIN SPI_SS_PIN +#define PMW3360_CS_PIN SPI_SS_PIN diff --git a/keyboards/oddball/keymaps/default/config.h b/keyboards/oddball/keymaps/default/config.h deleted file mode 100644 index 7eddc070dc..0000000000 --- a/keyboards/oddball/keymaps/default/config.h +++ /dev/null @@ -1 +0,0 @@ -#define ADNS_9800 diff --git a/keyboards/oddball/keymaps/default/rules.mk b/keyboards/oddball/keymaps/default/rules.mk new file mode 100644 index 0000000000..84de35aeb1 --- /dev/null +++ b/keyboards/oddball/keymaps/default/rules.mk @@ -0,0 +1 @@ +POINTING_DEVICE_DRIVER = adns9800 diff --git a/keyboards/oddball/keymaps/pmw3360/config.h b/keyboards/oddball/keymaps/pmw3360/config.h deleted file mode 100644 index f9af5f3911..0000000000 --- a/keyboards/oddball/keymaps/pmw3360/config.h +++ /dev/null @@ -1 +0,0 @@ -#define PMW_3360 diff --git a/keyboards/oddball/keymaps/pmw3360/rules.mk b/keyboards/oddball/keymaps/pmw3360/rules.mk new file mode 100644 index 0000000000..fab9162dc6 --- /dev/null +++ b/keyboards/oddball/keymaps/pmw3360/rules.mk @@ -0,0 +1 @@ +POINTING_DEVICE_DRIVER = pmw3360 diff --git a/keyboards/oddball/oddball.c b/keyboards/oddball/oddball.c index 7ac6d99016..bbc3b3f2e4 100644 --- a/keyboards/oddball/oddball.c +++ b/keyboards/oddball/oddball.c @@ -16,21 +16,17 @@ #include "oddball.h" #include "pointing_device.h" -#include "optical_sensor/optical_sensor.h" - -#define CLAMP_HID(value) value < -127 ? -127 : value > 127 ? 127 : value +extern const pointing_device_driver_t pointing_device_driver; static bool scroll_pressed; static bool mouse_buttons_dirty; static int8_t scroll_h; static int8_t scroll_v; -void pointing_device_init(void){ +void pointing_device_init_kb(void){ if(!is_keyboard_master()) return; - optical_sensor_init(); - // read config from EEPROM and update if needed config_oddball_t kb_config; @@ -41,21 +37,17 @@ void pointing_device_init(void){ eeconfig_update_kb(kb_config.raw); } - optical_sensor_set_config((config_optical_sensor_t){ kb_config.cpi }); + pointing_device_set_cpi(kb_config.cpi); } -void pointing_device_task(void){ - if(!is_keyboard_master()) - return; - - report_mouse_t mouse_report = pointing_device_get_report(); - report_optical_sensor_t sensor_report = optical_sensor_get_report(); - - int8_t clamped_x = CLAMP_HID(sensor_report.x); - int8_t clamped_y = CLAMP_HID(sensor_report.y); +report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) { + if (!is_keyboard_master()) return mouse_report; - if(scroll_pressed) { + int8_t clamped_x = mouse_report.x, clamped_y = mouse_report.y; + mouse_report.x = 0; + mouse_report.y = 0; + if (scroll_pressed) { // accumulate scroll scroll_h += clamped_x; scroll_v += clamped_y; @@ -65,33 +57,21 @@ void pointing_device_task(void){ // clear accumulated scroll on assignment - if(scaled_scroll_h != 0){ + if (scaled_scroll_h != 0) { mouse_report.h = -scaled_scroll_h; - scroll_h = 0; + scroll_h = 0; } - if(scaled_scroll_v != 0){ + if (scaled_scroll_v != 0) { mouse_report.v = -scaled_scroll_v; - scroll_v = 0; + scroll_v = 0; } - } - else { + } else { mouse_report.x = -clamped_x; mouse_report.y = clamped_y; } - pointing_device_set_report(mouse_report); - - // only send report on change as even sending report with no change is treated as movement - if(mouse_buttons_dirty || - mouse_report.x != 0 || - mouse_report.y != 0 || - mouse_report.h != 0 || - mouse_report.v != 0){ - - mouse_buttons_dirty = false; - pointing_device_send(); - } + return mouse_report; } static void on_cpi_button(uint16_t cpi, keyrecord_t *record) { @@ -99,7 +79,7 @@ static void on_cpi_button(uint16_t cpi, keyrecord_t *record) { if(!record->event.pressed) return; - optical_sensor_set_config((config_optical_sensor_t){ cpi }); + pointing_device_set_cpi(cpi); config_oddball_t kb_config; kb_config.cpi = cpi; @@ -165,5 +145,5 @@ bool process_record_kb(uint16_t keycode, keyrecord_t *record) { default: return true; - } + } } diff --git a/keyboards/oddball/optical_sensor/optical_sensor.h b/keyboards/oddball/optical_sensor/optical_sensor.h deleted file mode 100644 index 00955209e2..0000000000 --- a/keyboards/oddball/optical_sensor/optical_sensor.h +++ /dev/null @@ -1,53 +0,0 @@ -/* Copyright 2020 Alexander Tulloh - * - * 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 <http://www.gnu.org/licenses/>. - */ - -/* common interface for opitcal sensors */ - -#if defined ADNS_9800 - #include "drivers/sensors/adns9800.h" - #define config_optical_sensor_t config_adns_t - #define report_optical_sensor_t report_adns_t - #define optical_sensor_init adns_init - #define optical_sensor_get_config adns_get_config - #define optical_sensor_set_config adns_set_config - #define optical_sensor_get_report adns_get_report -#elif defined PMW_3360 - #include "../pmw/pmw.h" - #define config_optical_sensor_t config_pmw_t - #define report_optical_sensor_t report_pmw_t - #define optical_sensor_init pmw_init - #define optical_sensor_get_config pmw_get_config - #define optical_sensor_set_config pmw_set_config - #define optical_sensor_get_report pmw_get_report -#else - /* fallback stub */ - - #include <stdint.h> - - typedef struct { - uint16_t cpi; - } config_optical_sensor_t; - - typedef struct { - int16_t x; - int16_t y; - } report_optical_sensor_t; - - #define optical_sensor_init(){ } - #define optical_sensor_get_config() (config_optical_sensor_t){ } - #define optical_sensor_set_config(config_optical_sensor_t){ } - #define optical_sensor_get_report() (report_optical_sensor_t){ } -#endif diff --git a/keyboards/oddball/pmw/pmw.c b/keyboards/oddball/pmw/pmw.c deleted file mode 100644 index 51d6927024..0000000000 --- a/keyboards/oddball/pmw/pmw.c +++ /dev/null @@ -1,226 +0,0 @@ -/* Copyright 2020 Alexander Tulloh - * - * 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 <http://www.gnu.org/licenses/>. - */ - -#include "spi_master.h" -#include "quantum.h" -#include "pmw3360_srom_0x04.h" -#include "pmw.h" - -// registers -#define Product_ID 0x00 -#define Revision_ID 0x01 -#define Motion 0x02 -#define Delta_X_L 0x03 -#define Delta_X_H 0x04 -#define Delta_Y_L 0x05 -#define Delta_Y_H 0x06 -#define SQUAL 0x07 -#define Raw_Data_Sum 0x08 -#define Maximum_Raw_data 0x09 -#define Minimum_Raw_data 0x0A -#define Shutter_Lower 0x0B -#define Shutter_Upper 0x0C -#define Control 0x0D -#define Config1 0x0F -#define Config2 0x10 -#define Angle_Tune 0x11 -#define Frame_Capture 0x12 -#define SROM_Enable 0x13 -#define Run_Downshift 0x14 -#define Rest1_Rate_Lower 0x15 -#define Rest1_Rate_Upper 0x16 -#define Rest1_Downshift 0x17 -#define Rest2_Rate_Lower 0x18 -#define Rest2_Rate_Upper 0x19 -#define Rest2_Downshift 0x1A -#define Rest3_Rate_Lower 0x1B -#define Rest3_Rate_Upper 0x1C -#define Observation 0x24 -#define Data_Out_Lower 0x25 -#define Data_Out_Upper 0x26 -#define Raw_Data_Dump 0x29 -#define SROM_ID 0x2A -#define Min_SQ_Run 0x2B -#define Raw_Data_Threshold 0x2C -#define Config5 0x2F -#define Power_Up_Reset 0x3A -#define Shutdown 0x3B -#define Inverse_Product_ID 0x3F -#define LiftCutoff_Tune3 0x41 -#define Angle_Snap 0x42 -#define LiftCutoff_Tune1 0x4A -#define Motion_Burst 0x50 -#define LiftCutoff_Tune_Timeout 0x58 -#define LiftCutoff_Tune_Min_Length 0x5A -#define SROM_Load_Burst 0x62 -#define Lift_Config 0x63 -#define Raw_Data_Burst 0x64 -#define LiftCutoff_Tune2 0x65 - -#define PMW_CLOCK_SPEED 70000000 -#define MIN_CPI 100 -#define MAX_CPI 12000 -#define CPI_STEP 100 -#define CLAMP_CPI(value) value < MIN_CPI ? MIN_CPI : value > MAX_CPI ? MAX_CPI : value -#define SPI_MODE 3 -#define SPI_DIVISOR (F_CPU / PMW_CLOCK_SPEED) -#define US_BETWEEN_WRITES 180 -#define US_BETWEEN_READS 20 -#define US_BEFORE_MOTION 35 - -#define MSB1 0x80 - -extern const uint16_t pmw_firmware_length; -extern const uint8_t pmw_firmware_data[]; - -void pmw_spi_start(void){ - spi_start(SPI_SS_PIN, false, SPI_MODE, SPI_DIVISOR); -} - -void pmw_write(uint8_t reg_addr, uint8_t data){ - - pmw_spi_start(); - spi_write(reg_addr | MSB1 ); - spi_write(data); - spi_stop(); - wait_us(US_BETWEEN_WRITES); -} - -uint8_t pmw_read(uint8_t reg_addr){ - - pmw_spi_start(); - spi_write(reg_addr & 0x7f ); - uint8_t data = spi_read(); - spi_stop(); - wait_us(US_BETWEEN_READS); - - return data; -} - -void pmw_init() { - - setPinOutput(SPI_SS_PIN); - - spi_init(); - - // reboot - pmw_write(Power_Up_Reset, 0x5a); - wait_ms(50); - - // read registers and discard - pmw_read(Motion); - pmw_read(Delta_X_L); - pmw_read(Delta_X_H); - pmw_read(Delta_Y_L); - pmw_read(Delta_Y_H); - - // upload firmware - - // disable rest mode - pmw_write(Config2, 0x20); - - // enable initialisation - pmw_write(SROM_Enable, 0x1d); - - // wait a frame - wait_ms(10); - - // start SROM download - pmw_write(SROM_Enable, 0x18); - - // write the SROM file - - pmw_spi_start(); - - spi_write(SROM_Load_Burst | 0x80); - wait_us(15); - - // send all bytes of the firmware - unsigned char c; - for(int i = 0; i < pmw_firmware_length; i++){ - c = (unsigned char)pgm_read_byte(pmw_firmware_data + i); - spi_write(c); - wait_us(15); - } - - spi_stop(); - wait_us(US_BETWEEN_WRITES); - - // read id - pmw_read(SROM_ID); - - // wired mouse - pmw_write(Config2, 0x00); - - // first motion burst; write anything - pmw_write(Motion_Burst, 0xFF); - writePinLow(SPI_SS_PIN); -} - -config_pmw_t pmw_get_config(void) { - uint8_t config_1 = pmw_read(Config1); - return (config_pmw_t){ (config_1 & 0xFF) * CPI_STEP }; -} - -void pmw_set_config(config_pmw_t config) { - uint8_t config_1 = (CLAMP_CPI(config.cpi) / CPI_STEP) & 0xFF; - pmw_write(Config1, config_1); -} - -static int16_t convertDeltaToInt(uint8_t high, uint8_t low){ - - // join bytes into twos compliment - uint16_t twos_comp = (high << 8) | low; - - // convert twos comp to int - if (twos_comp & 0x8000) - return -1 * (~twos_comp + 1); - - return twos_comp; -} - -report_pmw_t pmw_get_report(void) { - - report_pmw_t report = {0, 0}; - - pmw_spi_start(); - - // start burst mode |