Skip to content

Commit

Permalink
Merge pull request #105 from koron/scroll-snap-feature
Browse files Browse the repository at this point in the history
scroll snap feature
  • Loading branch information
koron authored Apr 2, 2022
2 parents cfa8abc + 54b00fd commit ea80e4e
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 58 deletions.
14 changes: 10 additions & 4 deletions qmk_firmware/keyboards/keyball/drivers/pmw3360/pmw3360.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define PMW3360_SPI_DIVISOR (F_CPU / PMW3360_CLOCKS)
#define PMW3360_CLOCKS 70000000

bool pmw3360_spi_start(void) { return spi_start(PMW3360_NCS_PIN, false, PMW3360_SPI_MODE, PMW3360_SPI_DIVISOR); }
bool pmw3360_spi_start(void) {
return spi_start(PMW3360_NCS_PIN, false, PMW3360_SPI_MODE, PMW3360_SPI_DIVISOR);
}

uint8_t pmw3360_reg_read(uint8_t addr) {
pmw3360_spi_start();
Expand All @@ -42,7 +44,9 @@ void pmw3360_reg_write(uint8_t addr, uint8_t data) {
wait_us(180);
}

uint8_t pmw3360_cpi_get(void) { return pmw3360_reg_read(pmw3360_Config1); }
uint8_t pmw3360_cpi_get(void) {
return pmw3360_reg_read(pmw3360_Config1);
}

void pmw3360_cpi_set(uint8_t cpi) {
if (cpi > pmw3360_MAXCPI) {
Expand All @@ -68,7 +72,9 @@ void pmw3360_scan_perf_task(void) {
}
}

uint32_t pmw3360_scan_rate_get(void) { return pmw3360_last_count; }
uint32_t pmw3360_scan_rate_get(void) {
return pmw3360_last_count;
}

bool pmw3360_motion_read(pmw3360_motion_t *d) {
#ifdef DEBUG_PMW3360_SCAN_RATE
Expand Down Expand Up @@ -97,7 +103,7 @@ bool pmw3360_motion_burst(pmw3360_motion_t *d) {
spi_stop();
return false;
}
spi_read(); // skip Observation
spi_read(); // skip Observation
d->x = spi_read();
d->x |= spi_read() << 8;
d->y = spi_read();
Expand Down
14 changes: 10 additions & 4 deletions qmk_firmware/keyboards/keyball/drivers/pmw3360/pmw3360.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,22 @@ typedef enum {
} pmw3360_reg_t;

enum {
pmw3360_MAXCPI = 0x77, // = 119: 12000 CPI
pmw3360_MAXCPI = 0x77, // = 119: 12000 CPI
};

//////////////////////////////////////////////////////////////////////////////
// SPI operations

bool pmw3360_spi_start(void);

void inline pmw3360_spi_stop(void) { spi_stop(); }
void inline pmw3360_spi_stop(void) {
spi_stop();
}

spi_status_t inline pmw3360_spi_write(uint8_t data) { return spi_write(data); }
spi_status_t inline pmw3360_spi_write(uint8_t data) {
return spi_write(data);
}

spi_status_t inline pmw3360_spi_read(void) { return spi_read(); }
spi_status_t inline pmw3360_spi_read(void) {
return spi_read();
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ layer_state_t layer_state_set_user(layer_state_t state) {

#ifdef OLED_ENABLE

#include "lib/oledkit/oledkit.h"
# include "lib/oledkit/oledkit.h"

void oledkit_render_info_user(void) {
keyball_oled_render_keyinfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {

#ifdef OLED_ENABLE

#include "lib/oledkit/oledkit.h"
# include "lib/oledkit/oledkit.h"

void oledkit_render_info_user(void) {
keyball_oled_render_keyinfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ layer_state_t layer_state_set_user(layer_state_t state) {

#ifdef OLED_ENABLE

#include "lib/oledkit/oledkit.h"
# include "lib/oledkit/oledkit.h"

void oledkit_render_info_user(void) {
keyball_oled_render_keyinfo();
Expand Down
4 changes: 2 additions & 2 deletions qmk_firmware/keyboards/keyball/keyball46/keyball46.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ static uint8_t peek_matrix_intersection(pin_t out_pin, pin_t in_pin) {
}

bool is_keyboard_left(void) {
return !peek_matrix_intersection(keyball.this_have_ball ? F7: F6, B5);
return !peek_matrix_intersection(keyball.this_have_ball ? F7 : F6, B5);
}

//////////////////////////////////////////////////////////////////////////////

void keyball_on_adjust_layout(keyball_adjust_t v) {
if (v == KEYBALL_ADJUST_PRIMARY) {
// adjust matrix mask
bool is_left = is_keyboard_left();
bool is_left = is_keyboard_left();
matrix_mask[(is_left ? 2 : 6) + (keyball.this_have_ball ? 0 : 1)] = 0b0111111;
matrix_mask[(is_left ? 6 : 2) + (keyball.that_have_ball ? 0 : 1)] = 0b0111111;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void keyboard_post_init_user(void) {

#ifdef OLED_ENABLE

#include "lib/oledkit/oledkit.h"
# include "lib/oledkit/oledkit.h"

void oledkit_render_info_user(void) {
keyball_oled_render_keyinfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ layer_state_t layer_state_set_user(layer_state_t state) {

#ifdef OLED_ENABLE

#include "lib/oledkit/oledkit.h"
# include "lib/oledkit/oledkit.h"

void oledkit_render_info_user(void) {
keyball_oled_render_keyinfo();
Expand Down
22 changes: 12 additions & 10 deletions qmk_firmware/keyboards/keyball/keyball61/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "split_common/split_util.h"
#include "split_common/transactions.h"

#define PINNUM_ROW (MATRIX_ROWS / 2)
#define PINNUM_COL (MATRIX_COLS / 2)
#define PINNUM_ROW (MATRIX_ROWS / 2)
#define PINNUM_COL (MATRIX_COLS / 2)

#define ROWS_PER_HAND (MATRIX_ROWS / 2)
#define MATRIXSIZE_PER_HAND (ROWS_PER_HAND * sizeof(matrix_row_t))
#define ROWS_PER_HAND (MATRIX_ROWS / 2)
#define MATRIXSIZE_PER_HAND (ROWS_PER_HAND * sizeof(matrix_row_t))

static pin_t row_pins[PINNUM_ROW] = MATRIX_ROW_PINS;
static pin_t col_pins[PINNUM_COL] = MATRIX_COL_PINS;
Expand Down Expand Up @@ -61,7 +61,7 @@ static bool duplex_scan(matrix_row_t current_matrix[]) {
for (uint8_t col = 0; col < PINNUM_COL; col++) {
bool on = !get_pin(col_pins[col]);
if (on) {
next |= 1 << col;
next |= 1 << col;
} else {
next &= ~(1 << col);
}
Expand All @@ -70,7 +70,7 @@ static bool duplex_scan(matrix_row_t current_matrix[]) {
matrix_output_unselect_delay(row, next != 0);
if (current_matrix[row] != next) {
current_matrix[row] = next;
changed = true;
changed = true;
}
}

Expand All @@ -81,7 +81,7 @@ static bool duplex_scan(matrix_row_t current_matrix[]) {
matrix_output_select_delay();
matrix_row_t shifter = ((matrix_row_t)1) << (col + PINNUM_COL);
for (uint8_t row = 0; row < PINNUM_ROW; row++) {
bool on = !get_pin(row_pins[row]);
bool on = !get_pin(row_pins[row]);
matrix_row_t prev = current_matrix[row];
if (on) {
current_matrix[row] |= shifter;
Expand Down Expand Up @@ -115,7 +115,9 @@ void matrix_init_custom(void) {
}

// user-defined overridable functions
__attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); }
__attribute__((weak)) void matrix_slave_scan_kb(void) {
matrix_slave_scan_user();
}
__attribute__((weak)) void matrix_slave_scan_user(void) {}

// override quantum/matrix_common.c
Expand All @@ -135,8 +137,8 @@ uint8_t matrix_scan(void) {
}

// receive from secondary.
static bool last_connected = false;
matrix_row_t *that_raw = raw_matrix + ROWS_PER_HAND;
static bool last_connected = false;
matrix_row_t* that_raw = raw_matrix + ROWS_PER_HAND;
memset(that_raw, 0, MATRIXSIZE_PER_HAND);
if (transport_master_if_connected(matrix + thisHand, that_raw)) {
last_connected = true;
Expand Down
68 changes: 55 additions & 13 deletions qmk_firmware/keyboards/keyball/lib/keyball/keyball.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ static int16_t add16(int16_t a, int16_t b) {
}

// 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; }
static inline int8_t clip2int8(int16_t v) {
return (v) < -127 ? -127 : (v) > 127 ? 127 : (int8_t)v;
}

static const char *format_4d(int8_t d) {
static char buf[5] = {0}; // max width (4) + NUL (1)
static char buf[5] = {0}; // max width (4) + NUL (1)
char lead = ' ';
if (d < 0) {
d = -d;
Expand Down Expand Up @@ -124,9 +126,13 @@ void pointing_device_driver_init(void) {
}
}

uint16_t pointing_device_driver_get_cpi(void) { return keyball_get_cpi(); }
uint16_t pointing_device_driver_get_cpi(void) {
return keyball_get_cpi();
}

void pointing_device_driver_set_cpi(uint16_t cpi) { keyball_set_cpi(cpi); }
void pointing_device_driver_set_cpi(uint16_t cpi) {
keyball_set_cpi(cpi);
}

static void motion_to_mouse_move(keyball_motion_t *m, report_mouse_t *r, bool is_left) {
#if KEYBALL_MODEL == 61 || KEYBALL_MODEL == 39
Expand All @@ -147,16 +153,42 @@ static void motion_to_mouse_move(keyball_motion_t *m, report_mouse_t *r, bool is
m->y = 0;
}

static inline int16_t abs16(int16_t v) {
return v < 0 ? -v : v;
}

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 >> div;
m->x -= x << div;
int16_t y = m->y >> div;
m->y -= y << div;

#if KEYBALL_SCROLLSNAP_ENABLE
// scroll snap.
uint32_t now = timer_read32();
if (x != 0 || y != 0) {
keyball.scroll_snap_last = now;
} else if (TIMER_DIFF_32(now, keyball.scroll_snap_last) >= KEYBALL_SCROLLSNAP_RESET_TIMER) {
keyball.scroll_snap_tension.x = 0;
keyball.scroll_snap_tension.y = 0;
}
if (abs16(keyball.scroll_snap_tension.x) < KEYBALL_SCROLLSNAP_TENSION_THRESHOLD) {
keyball.scroll_snap_tension.x += x;
x = 0;
}
if (abs16(keyball.scroll_snap_tension.y) < KEYBALL_SCROLLSNAP_TENSION_THRESHOLD) {
keyball.scroll_snap_tension.y += y;
y = 0;
}
#endif

// apply to mouse report.
#if KEYBALL_MODEL == 61 || KEYBALL_MODEL == 39
r->h = clip2int8(y);
r->v = clip2int8(x);
if (!is_left) {
r->v = -clip2int8(x);
if (is_left) {
r->h = -r->h;
r->v = -r->v;
}
Expand Down Expand Up @@ -187,7 +219,7 @@ static inline bool should_report(void) {
last = now;
#endif
#if defined(KEYBALL_SCROLLBALL_INHIVITOR) && KEYBALL_SCROLLBALL_INHIVITOR > 0
if (TIMER_DIFF_32(now, keyball.scroll_changed) < KEYBALL_SCROLLBALL_INHIVITOR) {
if (TIMER_DIFF_32(now, keyball.scroll_mode_changed) < KEYBALL_SCROLLBALL_INHIVITOR) {
keyball.this_motion.x = 0;
keyball.this_motion.y = 0;
keyball.that_motion.x = 0;
Expand Down Expand Up @@ -289,7 +321,9 @@ static void rpc_get_motion_invoke(void) {
return;
}

static void rpc_set_cpi_handler(uint8_t in_buflen, const void *in_data, uint8_t out_buflen, void *out_data) { keyball_set_cpi(*(keyball_cpi_t *)in_data); }
static void rpc_set_cpi_handler(uint8_t in_buflen, const void *in_data, uint8_t out_buflen, void *out_data) {
keyball_set_cpi(*(keyball_cpi_t *)in_data);
}

static void rpc_set_cpi_invoke(void) {
if (!keyball.cpi_changed) {
Expand Down Expand Up @@ -379,20 +413,28 @@ void keyball_oled_render_keyinfo(void) {
//////////////////////////////////////////////////////////////////////////////
// Public API functions

bool keyball_get_scroll_mode(void) { return keyball.scroll_mode; }
bool keyball_get_scroll_mode(void) {
return keyball.scroll_mode;
}

void keyball_set_scroll_mode(bool mode) {
if (mode != keyball.scroll_mode) {
keyball.scroll_changed = timer_read32();
keyball.scroll_mode_changed = timer_read32();
}
keyball.scroll_mode = mode;
}

uint8_t keyball_get_scroll_div(void) { return keyball.scroll_div == 0 ? KEYBALL_SCROLL_DIV_DEFAULT : keyball.scroll_div; }
uint8_t keyball_get_scroll_div(void) {
return keyball.scroll_div == 0 ? KEYBALL_SCROLL_DIV_DEFAULT : keyball.scroll_div;
}

void keyball_set_scroll_div(uint8_t div) { keyball.scroll_div = div > SCROLL_DIV_MAX ? SCROLL_DIV_MAX : div; }
void keyball_set_scroll_div(uint8_t div) {
keyball.scroll_div = div > SCROLL_DIV_MAX ? SCROLL_DIV_MAX : div;
}

uint8_t keyball_get_cpi(void) { return keyball.cpi_value == 0 ? CPI_DEFAULT : keyball.cpi_value; }
uint8_t keyball_get_cpi(void) {
return keyball.cpi_value == 0 ? CPI_DEFAULT : keyball.cpi_value;
}

void keyball_set_cpi(uint8_t cpi) {
if (cpi > CPI_MAX) {
Expand Down
Loading

0 comments on commit ea80e4e

Please sign in to comment.