Skip to content

Commit

Permalink
input IMU driver + initial look at BLE handling across split
Browse files Browse the repository at this point in the history
  • Loading branch information
hlord2000 committed Jul 9, 2024
1 parent 3d57c8a commit 3ae412d
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 21 deletions.
3 changes: 3 additions & 0 deletions JAZZHANDS_README
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Enable ZSL with...
west config manifest.project-filter -- +zscilib
west update
4 changes: 4 additions & 0 deletions app/boards/shields/jazz_hands/debug.dtsi
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
&uart0 {
current-speed = <921600>;
};

&uart0_default {
group1 {
psels = <NRF_PSEL(UART_TX, 1, 15)>;
Expand Down
21 changes: 20 additions & 1 deletion app/boards/shields/jazz_hands/jazz_hands.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,29 @@

chosen {
zmk,matrix-transform = &default_transform;
zmk,battery = &vbatt;
};

aliases {
status-led = &pwm_led0;
r-led = &pwm_led1;
g-led = &pwm_led2;
b-led = &pwm_led3;
imu = &lsm6ds3tr_c;
};

imu_input: imu_input {
status = "okay";
compatible = "zmk,input-imu";
imu = <&lsm6ds3tr_c>;
sample-frequency = <104>;
};

mouse_input_listener: mouse_input_listener {
status = "okay";
compatible = "zmk,input-listener";
device = <&imu_input>;
};

pwmleds {
compatible = "pwm-leds";
pwm_led1: pwm_led_1 {
Expand All @@ -33,6 +46,10 @@
};
};

vbatt: vbatt {
compatible = "zmk,battery-nrf-vddh";
};

default_transform: keymap_transform_0 {
compatible = "zmk,matrix-transform";
columns = <10>;
Expand Down Expand Up @@ -82,3 +99,5 @@
};
};
};


1 change: 0 additions & 1 deletion app/boards/shields/jazz_hands/jazz_hands.keymap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

Expand Down
27 changes: 25 additions & 2 deletions app/boards/shields/jazz_hands/jazz_hands_left.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY=25

CONFIG_MPU_ALLOW_FLASH_WRITE=y
CONFIG_NVS=y
CONFIG_SETTINGS=y
CONFIG_SETTINGS_NVS=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
Expand All @@ -19,11 +20,33 @@ CONFIG_ZMK_SPLIT_BLE_CENTRAL_SPLIT_RUN_STACK_SIZE=1024

# IMU config
CONFIG_I2C=y
# Set to user configurable at runtime
CONFIG_LSM6DSL_ACCEL_ODR=0
CONFIG_LSM6DSL_GYRO_ODR=0
CONFIG_LSM6DSL_TRIGGER_OWN_THREAD=y
CONFIG_INPUT_IMU_ALPHA=800
CONFIG_INPUT_IMU_GYRO_X_OFFSET=7475
CONFIG_INPUT_IMU_GYRO_Y_OFFSET=-32131
CONFIG_INPUT_IMU_GYRO_Z_OFFSET=-34294
CONFIG_IMU_MOUSE_SENSITIVITY=5
CONFIG_IMU_MOUSE_DEADZONE=3
CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_INPUT_IMU_REPORT_INTERVAL_MS=2
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=16384

CONFIG_FPU=y
CONFIG_ZSL=y
CONFIG_ZSL_SINGLE_PRECISION=y
CONFIG_ZSL_PLATFORM_OPT=2

# FOR DEBUG ONLY
CONFIG_ZMK_SETTINGS_RESET_ON_START=y
CONFIG_INPUT=y
CONFIG_INPUT_LOG_LEVEL_INF=y
CONFIG_INPUT_IMU=y

CONFIG_ZMK_MOUSE=y
CONFIG_ZMK_LOG_LEVEL_INF=y
CONFIG_ZMK_BLE_CONSUMER_REPORT_QUEUE_SIZE=100

#DEBUG
CONFIG_LOG_PROCESS_THREAD_STACK_SIZE=4096
CONFIG_ZMK_BLE_EXPERIMENTAL_CONN=y
13 changes: 11 additions & 2 deletions app/boards/shields/jazz_hands/jazz_hands_right.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY=25

CONFIG_MPU_ALLOW_FLASH_WRITE=y
CONFIG_NVS=y
CONFIG_SETTINGS=y
CONFIG_SETTINGS_NVS=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
Expand All @@ -20,11 +21,19 @@ CONFIG_ZMK_SPLIT_BLE_PERIPHERAL_STACK_SIZE=1024
# IMU config
CONFIG_I2C=y
CONFIG_LSM6DSL_TRIGGER_OWN_THREAD=y
CONFIG_INPUT_IMU_GYRO_X_OFFSET=7475
CONFIG_INPUT_IMU_GYRO_Y_OFFSET=-32131
CONFIG_INPUT_IMU_GYRO_Z_OFFSET=-34294
CONFIG_CBPRINTF_FP_SUPPORT=y

CONFIG_FPU=y
CONFIG_ZSL=y

# FOR DEBUG ONLY
CONFIG_ZMK_SETTINGS_RESET_ON_START=y
CONFIG_INPUT=y
CONFIG_INPUT_IMU=y

CONFIG_ZMK_MOUSE=y

#DEBUG
CONFIG_ZMK_LOG_LEVEL_WRN=y

40 changes: 38 additions & 2 deletions app/boards/shields/jazz_hands/src/jazz_hands_layer_indicator.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,52 @@ struct rgb_color layer_colors[] = {
{255, 255, 255},
};

static void disable_color() {
pwm_set_dt(&r_led, 255, 0);
pwm_set_dt(&g_led, 255, 0);
pwm_set_dt(&b_led, 255, 0);
}

static void set_color_rgb(struct rgb_color color) {
pwm_set_dt(&r_led, 255, color.r);
pwm_set_dt(&g_led, 255, color.g);
pwm_set_dt(&b_led, 255, color.b);
}

static void set_color_rgb_timeout(struct rgb_color color, int timeout) {
set_color_rgb(color);
k_sleep(K_MSEC(timeout));
disable_color();
}

//
// #if ZMK_BLE_IS_CENTRAL
// ZMK_SUBSCRIPTION(backlight, zmk_split_peripheral_status_changed);
// #endif
//
// #if IS_ENABLED(CONFIG_ZMK_SPLIT) && !IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
//
// static int backlight_data_event_listener(const zmk_event_t *eh) {
// const struct zmk_split_data_xfer_event *ev = as_zmk_split_data_xfer_event(eh);
// if (ev->data_xfer.data_tag == DATA_TAG_BACKLIGHT_STATE) {
// LOG_DBG("Backlight Data received of size: %d", ev->data_xfer.data_size);
// LOG_HEXDUMP_DBG(ev->data_xfer.data, sizeof(struct zmk_split_data_xfer_event),
// "received event:");
// memcpy(&state, ev->data_xfer.data, sizeof(struct backlight_state));
// LOG_HEXDUMP_DBG(&state, sizeof(struct backlight_state), "backlight state");
// zmk_backlight_update_and_save();
// }
// return 0;
// }
//
// ZMK_LISTENER(backlight_data, backlight_data_event_listener);
// ZMK_SUBSCRIPTION(backlight_data, zmk_split_data_xfer_event);
// #endif

#if CONFIG_SHIELD_JAZZ_HANDS_LEFT
static int layer_state_changed_listener(const zmk_event_t *eh) {
uint8_t index = zmk_keymap_highest_layer_active();
set_color_rgb(layer_colors[index]);
set_color_rgb_timeout(layer_colors[index], 2000);
return 0;
}

Expand All @@ -46,7 +82,7 @@ ZMK_SUBSCRIPTION(foo, zmk_layer_state_changed);
#endif

static int startup(void) {
set_color_rgb(layer_colors[0]);
set_color_rgb_timeout(layer_colors[0], 5000);
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions app/include/zmk/split/bluetooth/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ enum data_tag {
DATA_TAG_HID_INDICATORS_STATE,
// Keymap state
DATA_TAG_KEYMAP_STATE,
// Mouse state
DATA_TAG_MOUSE_STATE,
// Start of custom tags
DATA_TAG_CUSTOM_START,
};
Expand Down
17 changes: 4 additions & 13 deletions app/module/drivers/input/input_imu.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct input_imu_data {
uint32_t deadzone;
int32_t accumulated_x;
int32_t accumulated_y;
time_t last_report_time;
};

static uint32_t zsl_fus_comp_freq = 0;
Expand Down Expand Up @@ -175,11 +174,6 @@ void zmk_fus_comp_error(int error)
static int input_imu_quat_convert(struct input_imu_data *data, struct zsl_quat *q) {
int err;
struct zsl_euler e;
int64_t current_time = k_uptime_get();

if (current_time - data->last_report_time <= CONFIG_INPUT_IMU_REPORT_INTERVAL_MS) {
return 0;
}

err = zsl_quat_to_euler(q, &e);
if (err < 0) {
Expand Down Expand Up @@ -214,20 +208,19 @@ static int input_imu_quat_convert(struct input_imu_data *data, struct zsl_quat *
}

if (data->accumulated_x != 0) {
err = input_report_rel(data->dev, INPUT_REL_X, data->accumulated_x, true, K_FOREVER);
err = input_report_rel(data->dev, INPUT_REL_X, CLAMP(data->accumulated_x, -2, 2), true, K_FOREVER);
if (err < 0) {
LOG_ERR("Failed to report x axis: %d", err);
}
data->accumulated_x = 0;
}
if (data->accumulated_y != 0) {
err = input_report_rel(data->dev, INPUT_REL_Y, data->accumulated_y, true, K_FOREVER);
err = input_report_rel(data->dev, INPUT_REL_Y, CLAMP(data->accumulated_y, -2, 2), true, K_FOREVER);
if (err < 0) {
LOG_ERR("Failed to report y axis: %d", err);
}
data->accumulated_y = 0;
}
data->last_report_time = current_time;

return 0;
}
Expand Down Expand Up @@ -278,8 +271,7 @@ static void input_imu_thread(void *p1, void *p2, void *p3) {
LOG_ERR("Failed to convert quat to euler: %d", err);
return;
}
k_mutex_lock(&imu_mutex, K_MSEC(10));
k_yield();
k_mutex_lock(&imu_mutex, K_FOREVER);
}
}

Expand Down Expand Up @@ -326,7 +318,6 @@ static int input_imu_init(const struct device *dev)

data->accumulated_x = 0;
data->accumulated_y = 0;
data->last_report_time = k_uptime_get();

data->dev = dev;

Expand Down Expand Up @@ -367,4 +358,4 @@ static int input_imu_init(const struct device *dev)
&input_imu_data_##inst, &input_imu_config_##inst, \
POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, NULL);

DT_INST_FOREACH_STATUS_OKAY(INPUT_IMU_INIT)
DT_INST_FOREACH_STATUS_OKAY(INPUT_IMU_INIT)
19 changes: 19 additions & 0 deletions app/module/dts/bindings/input/zmk,input-imu.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2024, Kelly Helmut Lord
# SPDX-License-Identifier: Apache-2.0

description: IL0323 EPD display controller

compatible: "zmk,input-imu"

include: sensor-device.yaml

properties:
imu:
type: phandle
description: |
IMU sensor
sample-frequency:
type: int
description: |
The frequency at which the IMU sensor should be sampled.
default: 104

0 comments on commit 3ae412d

Please sign in to comment.