diff --git a/src/main.c b/src/main.c index 555135f..a5bd189 100644 --- a/src/main.c +++ b/src/main.c @@ -7,16 +7,16 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include +#include -#include -#include +#include +#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include "simplems.h" @@ -82,8 +82,52 @@ static void sm_buttons_cb(uint8_t buttons) } } +// Velocity is ~ *2 px/s +static int32_t _dx; +static int32_t _dy; +static int16_t _vx; +static int16_t _vy; + +static void sm_velocity_xy_cb(int16_t vx, int16_t vy) { + printk("Velocity (x,y) = (%d, %d)\n", vx, vy); + + _vx = vx; + _vy = vy; +} + +#define ABS(x) (x > 0 ? x : -x) + +static int16_t overflow(uint32_t *delta, uint16_t velocity) +{ + int16_t overflow; + *delta += velocity; + + overflow = *delta / 256; + + *delta = *delta % 0xff; + + return overflow; +} + +void apply_velocity(struct k_timer *dummy) +{ + int8_t mx; + int8_t my; + + mx = (int8_t)overflow(&_dx, _vx); + my = (int8_t)overflow(&_dy, _vy); + + if (mx || my) { + sm_move_xy_cb(mx, my); + } +} + +K_TIMER_DEFINE(velocity_timer, apply_velocity, NULL); + + struct bt_simple_mouse_cb sm_cbs = { .move_xy = sm_move_xy_cb, + .velocity_xy = sm_velocity_xy_cb, .buttons = sm_buttons_cb, }; @@ -110,12 +154,16 @@ static void connected(struct bt_conn *conn, uint8_t err) printk("Connection failed (err 0x%02x)\n", err); } else { printk("Connected\n"); + _dx = 0; + _dy = 0; + k_timer_start(&velocity_timer, K_MSEC(8), K_MSEC(8)); } } static void disconnected(struct bt_conn *conn, uint8_t reason) { printk("Disconnected (reason 0x%02x)\n", reason); + k_timer_stop(&velocity_timer); } BT_CONN_CB_DEFINE(conn_callbacks) = { diff --git a/src/simplems.c b/src/simplems.c index 3ab29c1..a7e02bc 100644 --- a/src/simplems.c +++ b/src/simplems.c @@ -1,15 +1,15 @@ -#include #include #include #include -#include -#include +#include +#include +#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include "simplems.h" @@ -17,12 +17,16 @@ static struct bt_uuid_128 simplems_uuid = BT_UUID_INIT_128( BT_UUID_SIMPLE_MOUSE_SERVICE); // 0x56beb2d8, 0x64eb, 0x4e33, 0x96d4, 0xe3f394041d0b (svc) -// 0x83986548, 0x8703, 0x4272, 0xa124, 0x84abb9d03217 (xy) +// 0x83986548, 0x8703, 0x4272, 0xa124, 0x84abb9d03217 (move xy) +// 0x3cabb56e, 0x27a7, 0x45fa, 0x996a, 0x582f581d6aa3 (velocity xy) // 0x5062c9c1, 0xca09, 0x47f9, 0x84f6, 0x725ef8091bf9 (btn) static const struct bt_uuid_128 simplems_move_xy_uuid = BT_UUID_INIT_128( BT_UUID_128_ENCODE(0x83986548, 0x8703, 0x4272, 0xa124, 0x84abb9d03217)); +static const struct bt_uuid_128 simplems_velocity_xy_uuid = BT_UUID_INIT_128( + BT_UUID_128_ENCODE(0x3cabb56e, 0x27a7, 0x45fa, 0x996a, 0x582f581d6aa3)); + static const struct bt_uuid_128 simplems_buttons_uuid = BT_UUID_INIT_128( BT_UUID_128_ENCODE(0x5062c9c1, 0xca09, 0x47f9, 0x84f6, 0x725ef8091bf9)); @@ -56,6 +60,28 @@ static ssize_t write_move_xy(struct bt_conn *conn, return len; } +static ssize_t write_velocity_xy(struct bt_conn *conn, + const struct bt_gatt_attr *attr, + const void *buf, uint16_t len, + uint16_t offset, uint8_t flags) +{ + int16_t val[2]; + + if (offset != 0) { + return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); + } else if (len != sizeof(val)) { + return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN); + } + + (void)memcpy(&val, buf, len); + + if (sm_cbs->velocity_xy) { + sm_cbs->velocity_xy(val[0], val[1]); + } + + return len; +} + static ssize_t write_buttons(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, uint16_t len, @@ -85,6 +111,10 @@ BT_GATT_SERVICE_DEFINE(sm_svc, BT_GATT_PERM_WRITE, NULL, write_move_xy, NULL), BT_GATT_CUD("Move XY", BT_GATT_PERM_READ), + BT_GATT_CHARACTERISTIC(&simplems_velocity_xy_uuid.uuid, BT_GATT_CHRC_WRITE, + BT_GATT_PERM_WRITE, + NULL, write_velocity_xy, NULL), + BT_GATT_CUD("Velocity XY", BT_GATT_PERM_READ), BT_GATT_CHARACTERISTIC(&simplems_buttons_uuid.uuid, BT_GATT_CHRC_WRITE, BT_GATT_PERM_WRITE, NULL, write_buttons, NULL), diff --git a/src/simplems.h b/src/simplems.h index 24b551f..c9d39d2 100644 --- a/src/simplems.h +++ b/src/simplems.h @@ -9,17 +9,20 @@ extern "C" { // 56beb2d8-64eb-4e33-96d4-e3f394041d0b (service) // 83986548-8703-4272-a124-84abb9d03217 (move x & y) +// 3cabb56e-27a7-45fa-996a-582f581d6aa3 (velocity x & y) // 5062c9c1-ca09-47f9-84f6-725ef8091bf9 (buttons) #define BT_UUID_SIMPLE_MOUSE_SERVICE \ BT_UUID_128_ENCODE(0x56beb2d8, 0x64eb, 0x4e33, 0x96d4, 0xe3f394041d0b) typedef void (*simple_mouse_move_xy_cb)(int8_t distx, int8_t disty); +typedef void (*simple_mouse_velocity_xy_cb)(int16_t vx, int16_t vy); typedef void (*simple_mouse_buttons_cb)(uint8_t buttons); struct bt_simple_mouse_cb { - simple_mouse_move_xy_cb move_xy; - simple_mouse_buttons_cb buttons; + simple_mouse_move_xy_cb move_xy; + simple_mouse_velocity_xy_cb velocity_xy; + simple_mouse_buttons_cb buttons; }; void bt_simple_mouse_register_cb(struct bt_simple_mouse_cb *cb); diff --git a/web/main-app.js b/web/main-app.js index 816dada..713ce9c 100644 --- a/web/main-app.js +++ b/web/main-app.js @@ -2,6 +2,8 @@ import { MultiJoystick } from './multi-joystick.js'; import { SimpleDriver } from './simple-driver.js'; +const _use_velocity = false; + const template = document.createElement('template'); template.innerHTML = `