Skip to content

Commit

Permalink
more mouse mode carp
Browse files Browse the repository at this point in the history
  • Loading branch information
ReFil committed Sep 26, 2023
1 parent 372e7ec commit 047d788
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 30 deletions.
114 changes: 85 additions & 29 deletions app/drivers/sensor/gen4/gen4.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,61 @@ static int gen4_normal_read(const struct device *dev, uint8_t *buf, const uint8_
return i2c_read_dt(&config->bus, buf, len);
}

static int gen4_i2c_init(const struct device *dev) {
static int gen4_mouse_mode(const struct device *dev) {
const struct gen4_config *cfg = dev->config;
uint8_t request[2] = {0x20, 0x00};
uint8_t buffer[30];
int ret = i2c_write_read_dt(&cfg->bus, request, 2, buffer, 30);
// enable mouse mode
uint8_t request2[10] = {0x05, 0x00, 0x34, 0x03, 0x06, 0x00, 0x04, 0x00, 0x04, 0x00};
int ret = i2c_write_dt(&cfg->bus, request2, 10);
if (ret < 0) {
LOG_ERR("ext read status: %d", ret);
return ret;
}
LOG_DBG("received value %x", buffer[2]);
// enable absolute mode
return 0;
}

static int gen4_abs_mode(const struct device *dev) {
const struct gen4_config *cfg = dev->config;
// enable mouse mode
uint8_t request2[10] = {0x05, 0x00, 0x34, 0x03, 0x06, 0x00, 0x04, 0x00, 0x04, 0x03};
ret = i2c_write_dt(&cfg->bus, request2, 10);
int ret = i2c_write_dt(&cfg->bus, request2, 10);
if (ret < 0) {
LOG_ERR("ext read status: %d", ret);
return ret;
}
return 0;
}

static int gen4_i2c_init(const struct device *dev) {
const struct gen4_config *cfg = dev->config;
uint8_t request[2] = {0x20, 0x00};
uint8_t buffer[30];
int ret = i2c_write_read_dt(&cfg->bus, request, 2, buffer, 30);
if (ret < 0) {
LOG_ERR("ext read status: %d", ret);
return ret;
}
LOG_DBG("received value %x", buffer[2]);

return gen4_mouse_mode(dev);
}

static int gen4_attr_set(const struct device *dev, enum sensor_channel chan,
enum sensor_attribute attr, const struct sensor_value *val) {
switch (val->val1) {
case 0:
LOG_DBG("Entering abs mode");
return gen4_abs_mode(dev);
break;
case 1:
LOG_DBG("Entering mouse mode");
return gen4_mouse_mode(dev);
break;

default:
break;
}
}

static int gen4_channel_get(const struct device *dev, enum sensor_channel chan,
struct sensor_value *val) {
const struct gen4_data *data = dev->data;
Expand All @@ -59,6 +94,15 @@ static int gen4_channel_get(const struct device *dev, enum sensor_channel chan,
case SENSOR_CHAN_BUTTONS:
val->val1 = data->btns;
break;
case SENSOR_CHAN_XDELTA:
val->val1 = data->mouse.xDelta;
break;
case SENSOR_CHAN_YDELTA:
val->val1 = data->mouse.yDelta;
break;
case SENSOR_CHAN_WHEEL:
val->val1 = data->mouse.scrollDelta;
break;
default:
return -ENOTSUP;
}
Expand All @@ -75,35 +119,46 @@ static int gen4_sample_fetch(const struct device *dev, enum sensor_channel) {
if (ret != 0)
return ret;
}
if (!(packet[REPORT_ID_SHIFT] == PTP_REPORT_ID)) {
return -EAGAIN;
}

uint16_t report_length = packet[LENGTH_LOWBYTE_SHIFT] | (packet[LENGTH_HIGHBYTE_SHIFT] << 8);

struct gen4_data *data = dev->data;

if (report_length == 12) {
data->scan_time = (uint16_t)packet[8] | (uint16_t)(packet[9] << 8);
data->contacts = packet[10];
data->btns = packet[11];
if (data->mousemode) {
if (!(packet[REPORT_ID_SHIFT] == MOUSE_REPORT_ID)) {
return -EAGAIN;
}
data->btns = packet[3];
data->mouse.xDelta = packet[4];
data->mouse.yDelta = packet[5];
data->mouse.scrollDelta = packet[6];
} else {
data->scan_time = (uint16_t)packet[9] | (uint16_t)(packet[10] << 8);
data->contacts = packet[11];
data->btns = packet[12];
}
if (!(packet[REPORT_ID_SHIFT] == PTP_REPORT_ID)) {
return -EAGAIN;
}

data->finger_id = (packet[3] & 0xFC) >> 2;
// LOG_DBG("FINGER ID: %d", data->finger_id);
// Finger data
data->finger.confidence_tip = (packet[3] & 0x03);
data->finger.x = (uint16_t)packet[4] | (uint16_t)(packet[5] << 8);
data->finger.y = (uint16_t)packet[6] | (uint16_t)(packet[7] << 8);
uint16_t report_length =
packet[LENGTH_LOWBYTE_SHIFT] | (packet[LENGTH_HIGHBYTE_SHIFT] << 8);

// LOG_DBG("Finger palm/detected: %d", data->finger.confidence_tip);
// LOG_DBG("Finger x: %d", data->finger.x);
// LOG_DBG("Finger y: %d", data->finger.y);
if (report_length == 12) {
data->scan_time = (uint16_t)packet[8] | (uint16_t)(packet[9] << 8);
data->contacts = packet[10];
data->btns = packet[11];
} else {
data->scan_time = (uint16_t)packet[9] | (uint16_t)(packet[10] << 8);
data->contacts = packet[11];
data->btns = packet[12];
}

data->finger_id = (packet[3] & 0xFC) >> 2;
// LOG_DBG("FINGER ID: %d", data->finger_id);
// Finger data
data->finger.confidence_tip = (packet[3] & 0x03);
data->finger.x = (uint16_t)packet[4] | (uint16_t)(packet[5] << 8);
data->finger.y = (uint16_t)packet[6] | (uint16_t)(packet[7] << 8);

// LOG_DBG("Finger palm/detected: %d", data->finger.confidence_tip);
// LOG_DBG("Finger x: %d", data->finger.x);
// LOG_DBG("Finger y: %d", data->finger.y);
}
return 0;
}

Expand Down Expand Up @@ -203,6 +258,7 @@ static const struct sensor_driver_api gen4_driver_api = {
#if CONFIG_GEN4_TRIGGER
.trigger_set = gen4_trigger_set,
#endif
.attr_set = gen4_attr_set,
.sample_fetch = gen4_sample_fetch,
.channel_get = gen4_channel_get,
};
Expand Down
3 changes: 2 additions & 1 deletion app/drivers/sensor/gen4/gen4.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ struct gen4_finger_data {
};

struct gen4_mouse_data {
uint8_t buttons;
int8_t xDelta;
int8_t yDelta; /**< Change in vertical movement */
int8_t scrollDelta; /**< Vertical Scroll value */
Expand All @@ -30,6 +29,8 @@ struct gen4_data {
uint8_t contacts, btns, finger_id;
uint16_t scan_time;
struct gen4_finger_data finger;
struct gen4_mouse_data mouse;
bool mousemode;
bool in_int;
#ifdef CONFIG_GEN4_TRIGGER
const struct device *dev;
Expand Down
3 changes: 3 additions & 0 deletions app/include/drivers/sensor/gen4.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ enum sensor_channel_gen4 {
SENSOR_CHAN_CONTACTS,
SENSOR_CHAN_SCAN_TIME,
SENSOR_CHAN_BUTTONS,
SENSOR_CHAN_WHEEL,
SENSOR_CHAN_XDELTA,
SENSOR_CHAN_YDELTA,
};

#ifdef __cplusplus
Expand Down
13 changes: 13 additions & 0 deletions app/src/trackpad.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static bool buttonmode;
static bool surfacemode;

static struct zmk_ptp_finger fingers[CONFIG_ZMK_TRACKPAD_MAX_FINGERS];
static struct zmk_hid_touchpad_mouse_report_body mouse;

#if IS_ENABLED(CONFIG_ZMK_TRACKPAD_WORK_QUEUE_DEDICATED)
K_THREAD_STACK_DEFINE(trackpad_work_stack_area, CONFIG_ZMK_TRACKPAD_DEDICATED_THREAD_STACK_SIZE);
Expand Down Expand Up @@ -96,6 +97,18 @@ static void handle_mouse_mode(const struct device *dev, const struct sensor_trig
return;
}
LOG_DBG("Trackpad handler trigd in mouse mode %d", 0);
int ret = sensor_sample_fetch(dev);
if (ret < 0) {
LOG_ERR("fetch: %d", ret);
return;
}
LOG_DBG("Trackpad handler trigd %d", 0);

struct sensor_value x, y, buttons, wheel;
sensor_channel_get(dev, SENSOR_CHAN_XDELTA, &x);
sensor_channel_get(dev, SENSOR_CHAN_YDELTA, &y);
sensor_channel_get(dev, SENSOR_CHAN_BUTTONS, &buttons);
sensor_channel_get(dev, SENSOR_CHAN_WHEEL, &wheel);

k_work_submit_to_queue(zmk_trackpad_work_q(), &trackpad_work);
}
Expand Down

0 comments on commit 047d788

Please sign in to comment.