Skip to content

Commit

Permalink
Merge pull request #24 from treellama/fix-jumpy-mouse
Browse files Browse the repository at this point in the history
smoother mouse handling
  • Loading branch information
aperezbios authored Jan 31, 2024
2 parents 0cd2e83 + 3369417 commit 5ef3f34
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/firmware/lib/QuokkADB/include/platform_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define PLATFORM_FW_VER_STRING PRODUCT_NAME " firmware: " PLATFORM_FW_VERSION " " __DATE__ " " __TIME__

#ifndef DEFAULT_MOUSE_SENSITIVITY_DIVISOR
#define DEFAULT_MOUSE_SENSITIVITY_DIVISOR 4
#define DEFAULT_MOUSE_SENSITIVITY_DIVISOR 2
#endif

#define SAVE_TO_FLASH_BLINK_COUNT 4
Expand All @@ -41,4 +41,4 @@
// Must be a power of 2
#ifndef LOGBUFSIZE
#define LOGBUFSIZE 65536
#endif
#endif
9 changes: 4 additions & 5 deletions src/firmware/lib/QuokkADB/include/platformmouseparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,17 @@ class PlatformMouseParser {
virtual void OnMiddleButtonDown(MOUSEINFO *mi __attribute__((unused))) {
};

int8_t AdjustMovement(int32_t coarse, int32_t fine);
int8_t AdjustMovement(int32_t& axis);

SCQueue<MOUSE_CLICK*, MOUSE_CLICK_QUEUE_CAPACITY> m_click_events;

PlatformKbdParser* m_keyboard;

bool m_processed = false;
bool m_ready = false;
int32_t m_coarse_x = 0;
int32_t m_coarse_y = 0;
int32_t m_fine_x = 0;
int32_t m_fine_y = 0;

int32_t m_x = 0;
int32_t m_y = 0;

MouseRightBtnMode m_right_btn_mode = MouseRightBtnMode::ctrl_click;
};
41 changes: 18 additions & 23 deletions src/firmware/lib/QuokkADB/src/platformmouseparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
//
//---------------------------------------------------------------------------

#include <algorithm>

#include "platformmouseparser.h"
#include <platform_logmsg.h>
#include <adb_platform.h>
Expand All @@ -35,34 +37,30 @@

extern FlashSettings setting_storage;

int8_t PlatformMouseParser::AdjustMovement(int32_t coarse, int32_t fine)
int8_t PlatformMouseParser::AdjustMovement(int32_t& axis)
{
uint8_t sensitivity_divisor = setting_storage.settings()->sensitivity_divisor;
// adb_move is currently the sum of all usb movement
// this is where the sensitivity is adjusted
int32_t adjusted = coarse / sensitivity_divisor;
// axis is the current sum of all usb movement
auto sensitivity_divisor = setting_storage.settings()->sensitivity_divisor;

// Adjust adb movement with accumulated fine movements
int8_t increment = fine / sensitivity_divisor;
adjusted += increment;
int32_t scaled = 0;

// Limits of a 7 bit number
if (adjusted > 63) adjusted = 63;
if (adjusted < -64) adjusted = -64;
// take whatever we can fit into this report (after scaling); leave the
// rest for the next report. Limits are a signed 7 bit number
if (axis > 0) {
scaled = std::min(axis / sensitivity_divisor, 63L);
} else if (axis < 0) {
scaled = std::max(axis / sensitivity_divisor, -64L);
}
axis -= scaled * sensitivity_divisor;

return (int8_t) adjusted & 0x7F;
return static_cast<int8_t>(scaled & 0x7F);
}

void PlatformMouseParser::Parse(const hid_mouse_report_t *report){
uint8_t sensitivity_divisor = setting_storage.settings()->sensitivity_divisor;
MOUSEINFO mouse_info = {0};

if (m_processed)
{
m_coarse_x = 0;
m_coarse_y = 0;
m_fine_x %= sensitivity_divisor;
m_fine_y %= sensitivity_divisor;
m_processed = false;
}

Expand All @@ -72,11 +70,8 @@ void PlatformMouseParser::Parse(const hid_mouse_report_t *report){
mouse_info.dX = report->x;
mouse_info.dY = report->y;

m_coarse_x += mouse_info.dX;
m_coarse_y += mouse_info.dY;

if (mouse_info.dX / sensitivity_divisor == 0) m_fine_x += mouse_info.dX;
if (mouse_info.dY / sensitivity_divisor == 0) m_fine_y += mouse_info.dY;
m_x += mouse_info.dX;
m_y += mouse_info.dY;

if(mouse_info.dX != 0 || mouse_info.dY != 0) {
OnMouseMove(&mouse_info);
Expand Down Expand Up @@ -204,4 +199,4 @@ void PlatformMouseParser::Parse(const hid_mouse_report_t *report){
}
memcpy(prevState.bInfo, &mouse_info, sizeof(prevState.bInfo));
m_ready = true;
}
}
4 changes: 2 additions & 2 deletions src/firmware/lib/adb/include/adb.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#define KBD_DEFAULT_ADDR 0x02
#define KBD_DEFAULT_HANDLER_ID 0x02
#define MOUSE_DEFAULT_ADDR 0x03
#define MOUSE_DEFAULT_HANDLER_ID 0x01
#define MOUSE_DEFAULT_HANDLER_ID 0x02
#define KDB_EXTENDED_HANDLER_ID 0x3

extern volatile bool adb_collision;
Expand Down Expand Up @@ -192,4 +192,4 @@ inline void AdbInterface::ResetCollision(void)
adb_irq_disable();
collision_detection = false;
adb_collision = false;
}
}
4 changes: 2 additions & 2 deletions src/firmware/lib/adb/src/adbmouseparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ uint16_t ADBMouseRptParser::GetAdbRegister0()
reg_value |= (1 << 7);
}
// Bits 14-8 = Y move Counts (Two's compliment. Negative = up, positive = down)
reg_value |= AdjustMovement(m_coarse_y, m_fine_y) << 8;
reg_value |= AdjustMovement(m_y) << 8;

// Bits 6-0 = X move counts (Two's compliment. Negative = left, positive = right)
reg_value |= AdjustMovement(m_coarse_x, m_fine_x) << 0;
reg_value |= AdjustMovement(m_x) << 0;

if (click != nullptr)
{
Expand Down

0 comments on commit 5ef3f34

Please sign in to comment.