Skip to content

Commit

Permalink
pointing keycodes and automouse for k03pro
Browse files Browse the repository at this point in the history
  • Loading branch information
danil-tolkachev committed Jan 5, 2025
1 parent 4848351 commit ef49412
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 268 deletions.
13 changes: 12 additions & 1 deletion keyboards/ergohaven/ergohaven.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,18 @@ enum custom_keycodes {
WPREV,
LAYER_NEXT,
LAYER_PREV,
EH_RESERV1,
EH_RSRV1,

EH_RSRV2 = QK_KB + 51,
EH_RSRV3,
EH_RSRV4,
EH_RSRV5,
EH_RSRV6,
EH_RSRV7,
EH_RSRV8,
EH_RSRV9,
EH_RSRV10,
EH_RSRV11,
};

// 1st layer on the cycle
Expand Down
5 changes: 5 additions & 0 deletions keyboards/ergohaven/ergohaven_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "ergohaven_oled.h"
#include "ergohaven_rgb.h"
#include "ergohaven_display.h"
#include "ergohaven_pointing.h"
#include "hid.h"
#include "version.h"

Expand Down Expand Up @@ -169,6 +170,10 @@ bool process_record_kb(uint16_t keycode, keyrecord_t* record) {

if (!process_record_ruen(keycode, record)) return false;

#ifdef EH_POINTING_KEYCODES
if (!process_record_pointing(keycode, record)) return false;
#endif

return process_record_user(keycode, record);
}

Expand Down
157 changes: 157 additions & 0 deletions keyboards/ergohaven/ergohaven_pointing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#include "ergohaven_pointing.h"
#include "quantum.h"

pointing_mode_t pointing_mode = POINTING_MODE_NORMAL;

static int32_t accumulated_h = 0;
static int32_t accumulated_v = 0;

static int32_t sens[4] = {1, 2, 16, 32};

void set_sniper_sens(int32_t s) {
sens[POINTING_MODE_SNIPER] = s;
}

void set_scroll_sens(int32_t s) {
sens[POINTING_MODE_SCROLL] = s;
}

void set_text_sens(int32_t s) {
sens[POINTING_MODE_TEXT] = s;
}

void set_automouse(uint8_t layer) {
if (layer == 0) {
set_auto_mouse_enable(false);
} else {
set_auto_mouse_layer(layer);
set_auto_mouse_enable(true);
}
}

static bool invert_scroll = false;

void set_invert_scroll(bool invert) {
invert_scroll = invert;
}

bool is_mouse_active = false;

bool auto_mouse_activation(report_mouse_t mouse_report) {
return is_mouse_active;
}

bool is_mouse_record_kb(uint16_t keycode, keyrecord_t *record) {
switch (keycode) {
case EH_SCR:
case EH_SNP:
case EH_TXT:
return true;
default:
return false;
}

return is_mouse_record_user(keycode, record);
}

bool process_record_pointing(uint16_t keycode, keyrecord_t *record) {
switch (keycode) {
case EH_SCR:
case EH_TXT:
case EH_SNP: {
static uint16_t press_timer = 0;
static pointing_mode_t prev_pointing_mode = POINTING_MODE_NORMAL;

const pointing_mode_t NEW_MODE = POINTING_MODE_SNIPER + (keycode - EH_SNP);

if (record->event.pressed) {
prev_pointing_mode = pointing_mode;
pointing_mode = NEW_MODE;
press_timer = timer_read();
} else {
if (timer_elapsed(press_timer) < get_tapping_term(keycode, record)) {
if (prev_pointing_mode == NEW_MODE)
pointing_mode = POINTING_MODE_NORMAL;
else
pointing_mode = NEW_MODE;
} else
pointing_mode = POINTING_MODE_NORMAL;
}
return false;
}
}
return true;
}

report_mouse_t pointing_device_task_user(report_mouse_t mrpt) {
is_mouse_active = abs(mrpt.x) > 1 || abs(mrpt.y) > 1 || abs(mrpt.v) > 1 || abs(mrpt.h) > 1 || mrpt.buttons;

pointing_mode_t pmode = pointing_mode;

// dealing with two finger gesture on touch
if (mrpt.h != 0 || mrpt.v != 0) {
pmode = POINTING_MODE_SCROLL;
mrpt.x = mrpt.h;
mrpt.y = mrpt.v;
mrpt.h = 0;
mrpt.v = 0;
}

int32_t divisor = sens[pmode];

if (pmode != POINTING_MODE_NORMAL) {
accumulated_h += mrpt.x;
accumulated_v += mrpt.y;

int shift_x = accumulated_h / divisor;
int shift_y = accumulated_v / divisor;

accumulated_h -= shift_x * divisor;
accumulated_v -= shift_y * divisor;

mrpt.x = 0;
mrpt.y = 0;

switch (pmode) {
case POINTING_MODE_SNIPER:
mrpt.x = shift_x;
mrpt.y = shift_y;
break;

case POINTING_MODE_SCROLL:
mrpt.h = shift_x;
mrpt.v = -shift_y;
break;

case POINTING_MODE_TEXT:
while (shift_x > 0) {
tap_code(KC_RIGHT);
shift_x--;
}
while (shift_x < 0) {
tap_code(KC_LEFT);
shift_x++;
}
while (shift_y < 0) {
tap_code(KC_UP);
shift_y++;
}
while (shift_y > 0) {
tap_code(KC_DOWN);
shift_y--;
}
break;

default:
case POINTING_MODE_NORMAL:
break;
}
}

if (invert_scroll) {
mrpt.v = -mrpt.v;
mrpt.h = -mrpt.h;
}

return mrpt;
}
29 changes: 29 additions & 0 deletions keyboards/ergohaven/ergohaven_pointing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "quantum.h"
#include "ergohaven.h"

typedef enum {
POINTING_MODE_NORMAL = 0,
POINTING_MODE_SNIPER,
POINTING_MODE_SCROLL,
POINTING_MODE_TEXT,
} pointing_mode_t;

enum {
EH_SNP = EH_RSRV11 + 1,
EH_SCR,
EH_TXT,
};

bool process_record_pointing(uint16_t keycode, keyrecord_t *record);

void set_scroll_sens(int32_t sens);

void set_sniper_sens(int32_t sens);

void set_text_sens(int32_t sens);

void set_automouse(uint8_t layer);

void set_invert_scroll(bool invert);
7 changes: 6 additions & 1 deletion keyboards/ergohaven/k03pro/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#define AZOTEQ_IQS5XX_TAP_DISTANCE 50
#define AZOTEQ_IQS5XX_ZOOM_ENABLE true


#define I2C_DRIVER I2CD1
#define I2C1_SDA_PIN GP2
#define I2C1_SCL_PIN GP3
Expand Down Expand Up @@ -68,5 +67,11 @@
#define EH_HAS_DISPLAY
#define EH_SHORT_PRODUCT_NAME "K:03 PRO"

#define VIA_EEPROM_LAYOUT_OPTIONS_SIZE 2
#define VIA_EEPROM_LAYOUT_OPTIONS_DEFAULT 0x0000025d

#define POINTING_DEVICE_AUTO_MOUSE_ENABLE
#define EH_POINTING_KEYCODES

// #undef NO_DEBUG
// #define DEBUG_MATRIX_SCAN_RATE
75 changes: 18 additions & 57 deletions keyboards/ergohaven/k03pro/k03pro.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
#include "ergohaven_ruen.h"
#include "ergohaven_rgb.h"
#include "ergohaven_display.h"
#include "ergohaven_pointing.h"

typedef union {
uint32_t raw;
struct {
uint8_t scroll_divisor : 3;
bool invert_scroll : 1;
uint8_t text_mode : 3;
uint8_t scroll_mode : 3;
uint8_t sniper_mode : 2;
uint8_t dpi_mode : 3;
bool invert_scroll : 1;
uint8_t auto_mouse_layer : 4;
};
} vial_config_t;

Expand Down Expand Up @@ -48,48 +52,30 @@ bool split_get_caps_word(void) {
return is_keyboard_master() ? is_caps_word_on() : display_config.caps_word;
}

static const uint16_t DPI_TABLE[] = {320, 400, 500, 630, 800, 1000};
static const uint16_t DPI_TABLE[] = {320, 400, 500, 630, 800, 1000};
const int32_t SNIPER_TABLE[15] = {2, 3, 4, 5};
const int32_t SCROLL_TABLE[15] = {6, 8, 11, 16, 23, 32, 45, 64};
const int32_t TEXT_TABLE[15] = {6, 8, 11, 16, 23, 32, 45, 64};

uint16_t get_dpi(uint8_t dpi_mode) {
if (dpi_mode < ARRAY_SIZE(DPI_TABLE))
return DPI_TABLE[dpi_mode];
else
return 400;
}

static int32_t scroll_divisor_x = 8;
static int32_t scroll_divisor_y = 8;

int get_scroll_div(uint8_t div_mode) {
switch (div_mode) {
case 0:
return 6;
case 1:
return 8;
case 2:
return 11;
case 3:
return 16;
default:
case 4:
return 23;
case 5:
return 32;
case 6:
return 45;
case 7:
return 64;
}
return DPI_TABLE[0];
}

void via_set_layout_options_kb(uint32_t value) {
dprintf("via_set_layout_options_kb %lx\n", value);
vial_config.raw = value;
scroll_divisor_x = get_scroll_div(vial_config.scroll_divisor);
scroll_divisor_y = get_scroll_div(vial_config.scroll_divisor);
vial_config.raw = value;

touch_config.raw = 0;
touch_config.dpi = get_dpi(vial_config.dpi_mode);

set_scroll_sens(SCROLL_TABLE[vial_config.scroll_mode]);
set_sniper_sens(SNIPER_TABLE[vial_config.sniper_mode]);
set_text_sens(TEXT_TABLE[vial_config.text_mode]);
set_invert_scroll(vial_config.invert_scroll);
set_automouse(vial_config.auto_mouse_layer);
}

void sync_touch(uint8_t in_buflen, const void* in_data, uint8_t out_buflen, void* out_data) {
Expand Down Expand Up @@ -206,31 +192,6 @@ void housekeeping_task_user(void) {
}
}

report_mouse_t pointing_device_task_user(report_mouse_t mouse_report) {
static int32_t scroll_accumulated_h = 0;
static int32_t scroll_accumulated_v = 0;

if (mouse_report.h != 0 || mouse_report.v != 0) {
if (vial_config.invert_scroll) {
scroll_accumulated_h -= mouse_report.h;
scroll_accumulated_v += mouse_report.v;
} else {
scroll_accumulated_h += mouse_report.h;
scroll_accumulated_v -= mouse_report.v;
}

mouse_report.h = scroll_accumulated_h / scroll_divisor_x;
mouse_report.v = scroll_accumulated_v / scroll_divisor_y;

scroll_accumulated_h -= mouse_report.h * scroll_divisor_x;
scroll_accumulated_v -= mouse_report.v * scroll_divisor_y;

mouse_report.x = 0;
mouse_report.y = 0;
}
return mouse_report;
}

void keyboard_post_init_user(void) {
if (is_display_side()) {
display_init_kb();
Expand Down
Loading

0 comments on commit ef49412

Please sign in to comment.