Skip to content

Commit

Permalink
Starting mode switching work
Browse files Browse the repository at this point in the history
  • Loading branch information
ReFil committed Apr 7, 2024
1 parent f6367e1 commit 2d0152f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 24 deletions.
5 changes: 4 additions & 1 deletion app/include/zmk/trackpad.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include <zephyr/kernel.h>
#include <zmk/endpoints_types.h>

typedef uint8_t zmk_trackpad_finger_contacts_t;

Expand All @@ -24,4 +25,6 @@ void zmk_trackpad_set_mouse_mode(bool mouse_mode);

void zmk_trackpad_selective_set(uint8_t selective);

struct k_work_q *zmk_trackpad_work_q();
struct k_work_q *zmk_trackpad_work_q();

void zmk_trackpad_set_mode_report(uint8_t *report, struct zmk_endpoint_instance endpoint);
13 changes: 1 addition & 12 deletions app/src/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,20 +548,9 @@ struct zmk_hid_ptp_feature_mode_report *zmk_hid_ptp_get_feature_mode_report() {
return &ptp_feature_mode_report;
}

static void zmk_hid_trackpad_mouse_mode(struct k_work *work) {
if (ptp_feature_mode_report.mode == 3)
zmk_trackpad_set_mouse_mode(false);
else
zmk_trackpad_set_mouse_mode(true);
}

K_WORK_DEFINE(mouse_mode_work, zmk_hid_trackpad_mouse_mode);

void zmk_hid_ptp_set_feature_mode_report(uint8_t mode) {
LOG_DBG("Setting mouse mode to: %d", mode);

LOG_DBG("Setting mode report to: %d", mode);
ptp_feature_mode_report.mode = mode;
k_work_submit(&mouse_mode_work);
}

struct zmk_hid_ptp_feature_certification_report *zmk_hid_ptp_get_feature_certification_report() {
Expand Down
14 changes: 13 additions & 1 deletion app/src/hog.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
#include <zmk/hid_indicators.h>
#endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
#if IS_ENABLED(CONFIG_ZMK_TRACKPAD)
#include <zmk/trackpad.h>
#endif

enum {
HIDS_REMOTE_WAKE = BIT(0),
Expand Down Expand Up @@ -221,10 +224,19 @@ static ssize_t write_hids_trackpad_mode_feature_report(struct bt_conn *conn,
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
}

int profile = zmk_ble_profile_index(bt_conn_get_dst(conn));
if (profile < 0) {
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
}

struct zmk_endpoint_instance endpoint = {.transport = ZMK_TRANSPORT_BLE,
.ble = {
.profile_index = profile,
}};
uint8_t *report = (uint8_t *)buf;

LOG_DBG("Mode report set ble %d", *report);
zmk_hid_ptp_set_feature_mode_report(*report);
zmk_trackpad_set_mode_report(report, endpoint);

return len;
}
Expand Down
33 changes: 24 additions & 9 deletions app/src/trackpad.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <zephyr/logging/log.h>
#include <zephyr/kernel.h>

#include "zmk/mouse.h"
#include "zmk/trackpad.h"
#include <zmk/mouse.h>
#include <zmk/trackpad.h>
#include <zmk/hid.h>
#include <zmk/endpoints.h>
#include <zmk/event_manager.h>
Expand All @@ -29,9 +29,11 @@ static bool enabled;

static int8_t xDelta, yDelta, scrollDelta;

static struct zmk_ptp_finger fingers[5];
static struct zmk_ptp_finger fingers[CONFIG_ZMK_TRACKPAD_MAX_FINGERS];
static const struct zmk_ptp_finger empty_finger = {0};

static bool mouse_modes[ZMK_ENDPOINT_COUNT];

#if IS_ENABLED(CONFIG_ZMK_TRACKPAD_WORK_QUEUE_DEDICATED)
K_THREAD_STACK_DEFINE(trackpad_work_stack_area, CONFIG_ZMK_TRACKPAD_DEDICATED_THREAD_STACK_SIZE);
static struct k_work_q trackpad_work_q;
Expand Down Expand Up @@ -167,22 +169,19 @@ void zmk_trackpad_set_mouse_mode(bool mouse_mode) {
mousemode = mouse_mode;
sensor_attr_set(trackpad, SENSOR_CHAN_ALL, SENSOR_ATTR_CONFIGURATION, &attr);
if (mouse_mode) {
// k_timer_stop(&trackpad_tick);

if (sensor_trigger_set(trackpad, &trigger, handle_mouse_mode) < 0) {
LOG_ERR("can't set trigger mouse mode");
};
} else {
zmk_hid_mouse_clear();
zmk_endpoints_send_mouse_report();
// k_timer_start(&trackpad_tick, K_NO_WAIT, K_MSEC(CONFIG_ZMK_TRACKPAD_TICK_DURATION));
if (sensor_trigger_set(trackpad, &trigger, handle_trackpad_ptp) < 0) {
LOG_ERR("can't set trigger");
};
}
}

static int trackpad_init() {
static int trackpad_init(void) {

#if IS_ENABLED(CONFIG_ZMK_TRACKPAD_WORK_QUEUE_DEDICATED)
k_work_queue_start(&trackpad_work_q, trackpad_work_stack_area,
Expand All @@ -196,13 +195,29 @@ static int trackpad_init() {
return 0;
}

static void process_mode_report(struct k_work *_work) {
bool state = mouse_modes[zmk_endpoint_instance_to_index(zmk_endpoints_selected())];
if (mousemode != state) {
zmk_trackpad_set_mouse_mode(state);
zmk_hid_ptp_set_feature_mode_report(state ? 0 : 3);
}
}

static K_WORK_DEFINE(mode_changed_work, process_mode_report);

static int trackpad_event_listener(const zmk_event_t *eh) {
// zmk_trackpad_set_mouse_mode(true);
// zmk_hid_ptp_set_feature_mode_report(0);
k_work_submit(&mode_changed_work);
return 0;
}

static ZMK_LISTENER(trackpad, trackpad_event_listener);
static ZMK_SUBSCRIPTION(trackpad, zmk_endpoint_changed);

void zmk_trackpad_set_mode_report(uint8_t *report, struct zmk_endpoint_instance endpoint) {
int profile = zmk_endpoint_instance_to_index(endpoint);
LOG_DBG("Received report %d on endpoint %d", *report, profile);
mouse_modes[profile] = *report ? false : true;
k_work_submit(&mode_changed_work);
}

SYS_INIT(trackpad_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
8 changes: 7 additions & 1 deletion app/src/usb_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#if IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
#include <zmk/hid_indicators.h>
#endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
#if IS_ENABLED(CONFIG_ZMK_TRACKPAD)
#include <zmk/trackpad.h>
#endif
#include <zmk/event_manager.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
Expand Down Expand Up @@ -152,7 +155,10 @@ static int set_report_cb(const struct device *dev, struct usb_setup_packet *setu
} else {
struct zmk_hid_ptp_feature_mode_report *report =
(struct zmk_hid_ptp_feature_mode_report *)*data;
zmk_hid_ptp_set_feature_mode_report(report->mode);
struct zmk_endpoint_instance endpoint = {
.transport = ZMK_TRANSPORT_USB,
};
zmk_trackpad_set_mode_report(&report->mode, endpoint);
}
break;
}
Expand Down

0 comments on commit 2d0152f

Please sign in to comment.