diff --git a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c index 18581e486..fca7dd01d 100644 --- a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c +++ b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c @@ -65,6 +65,13 @@ static int16_t add16(int16_t a, int16_t b) { return r; } +// divmod16 divides *v and returns remainder. +static int16_t divmod16(int16_t *v, int16_t div) { + int16_t r = *v / div; + *v -= r * div; + return r; +} + // clip2int8 clips an integer fit into int8_t. static inline int8_t clip2int8(int16_t v) { return (v) < -127 ? -127 : (v) > 127 ? 127 : (int8_t)v; @@ -171,11 +178,9 @@ static void motion_to_mouse_move(keyball_motion_t *m, report_mouse_t *r, bool is static void motion_to_mouse_scroll(keyball_motion_t *m, report_mouse_t *r, bool is_left) { // consume motion of trackball. - uint8_t div = keyball_get_scroll_div() - 1; - int16_t x = m->x / (1 << div); - m->x -= x << div; - int16_t y = m->y / (1 << div); - m->y -= y << div; + int16_t div = 1 << (keyball_get_scroll_div() - 1); + int16_t x = divmod16(&m->x, div); + int16_t y = divmod16(&m->y, div); // apply to mouse report. #if KEYBALL_MODEL == 61 || KEYBALL_MODEL == 39 || KEYBALL_MODEL == 147 || KEYBALL_MODEL == 44