Skip to content

Commit

Permalink
Remove sending timer in favour of work
Browse files Browse the repository at this point in the history
  • Loading branch information
ReFil committed Mar 10, 2024
1 parent 41fc8e1 commit 6c3d622
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions app/src/trackpad.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ void zmk_trackpad_set_enabled(bool enabled) {}

bool zmk_trackpad_get_enabled() { return enabled; }

static void zmk_trackpad_tick(struct k_work *work) {
if (mousemode) {
zmk_hid_mouse_set(btns, xDelta, yDelta, scrollDelta);
zmk_endpoints_send_mouse_report();
} else if (contacts_to_send) {
// LOG_DBG("Trackpad sendy thing trigd %d", 0);
for (int i = 0; i < CONFIG_ZMK_TRACKPAD_MAX_FINGERS; i++)
if (contacts_to_send & BIT(i)) {
LOG_DBG("Trackpad sendy thing trigd %d", i);
zmk_hid_ptp_set(fingers[i], present_contacts, scantime, button_mode ? btns : 0);
zmk_endpoints_send_ptp_report();
contacts_to_send &= !BIT(i);
return;
}
} else if (contacts_to_send && !surface_mode) {
// report buttons only
LOG_DBG("Trackpad button thing trigd");
zmk_hid_ptp_set(empty_finger, present_contacts, scantime, button_mode ? btns : 0);
zmk_endpoints_send_ptp_report();
contacts_to_send = 0;
}
}

K_WORK_DEFINE(trackpad_work, zmk_trackpad_tick);

static void handle_trackpad_ptp(const struct device *dev, const struct sensor_trigger *trig) {
int ret = sensor_sample_fetch(dev);
if (ret < 0) {
Expand Down Expand Up @@ -78,6 +103,8 @@ static void handle_trackpad_ptp(const struct device *dev, const struct sensor_tr
fingers[id.val1].y = y.val1;
contacts_to_send |= BIT(id.val1);

k_work_submit_to_queue(zmk_trackpad_work_q(), &trackpad_work);

raise_zmk_sensor_event(
(struct zmk_sensor_event){.sensor_index = 0,
.channel_data_size = 1,
Expand All @@ -86,31 +113,6 @@ static void handle_trackpad_ptp(const struct device *dev, const struct sensor_tr
.timestamp = k_uptime_get()});
}

static void zmk_trackpad_tick(struct k_work *work) {
if (mousemode) {
zmk_hid_mouse_set(btns, xDelta, yDelta, scrollDelta);
zmk_endpoints_send_mouse_report();
} else if (contacts_to_send) {
// LOG_DBG("Trackpad sendy thing trigd %d", 0);
for (int i = 0; i < CONFIG_ZMK_TRACKPAD_MAX_FINGERS; i++)
if (contacts_to_send & BIT(i)) {
LOG_DBG("Trackpad sendy thing trigd %d", i);
zmk_hid_ptp_set(fingers[i], present_contacts, scantime, button_mode ? btns : 0);
zmk_endpoints_send_ptp_report();
contacts_to_send &= !BIT(i);
return;
}
} else if (contacts_to_send && !surface_mode) {
// report buttons only
LOG_DBG("Trackpad button thing trigd");
zmk_hid_ptp_set(empty_finger, present_contacts, scantime, button_mode ? btns : 0);
zmk_endpoints_send_ptp_report();
contacts_to_send = 0;
}
}

K_WORK_DEFINE(trackpad_work, zmk_trackpad_tick);

static void handle_mouse_mode(const struct device *dev, const struct sensor_trigger *trig) {
LOG_DBG("Trackpad handler trigd in mouse mode %d", 0);
int ret = sensor_sample_fetch(dev);
Expand Down Expand Up @@ -165,15 +167,15 @@ 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);
// 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));
// 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");
};
Expand Down

0 comments on commit 6c3d622

Please sign in to comment.