Ally Parker
4 years ago
committed by
Pete Johanson
20 changed files with 306 additions and 0 deletions
@ -0,0 +1,18 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <lvgl.h> |
||||||
|
#include <kernel.h> |
||||||
|
|
||||||
|
struct zmk_widget_wpm_status { |
||||||
|
sys_snode_t node; |
||||||
|
lv_obj_t *obj; |
||||||
|
}; |
||||||
|
|
||||||
|
int zmk_widget_wpm_status_init(struct zmk_widget_wpm_status *widget, lv_obj_t *parent); |
||||||
|
lv_obj_t *zmk_widget_wpm_status_obj(struct zmk_widget_wpm_status *widget); |
@ -0,0 +1,17 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <zephyr.h> |
||||||
|
#include <zmk/event_manager.h> |
||||||
|
#include <zmk/wpm.h> |
||||||
|
|
||||||
|
struct zmk_wpm_state_changed { |
||||||
|
int state; |
||||||
|
}; |
||||||
|
|
||||||
|
ZMK_EVENT_DECLARE(zmk_wpm_state_changed); |
@ -0,0 +1,9 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
int zmk_wpm_get_state(); |
@ -0,0 +1,67 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <logging/log.h> |
||||||
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); |
||||||
|
|
||||||
|
#include <zmk/display/widgets/wpm_status.h> |
||||||
|
#include <zmk/events/wpm_state_changed.h> |
||||||
|
#include <zmk/event_manager.h> |
||||||
|
#include <zmk/endpoints.h> |
||||||
|
#include <zmk/wpm.h> |
||||||
|
|
||||||
|
static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets); |
||||||
|
static lv_style_t label_style; |
||||||
|
|
||||||
|
static bool style_initialized = false; |
||||||
|
|
||||||
|
void wpm_status_init() { |
||||||
|
if (style_initialized) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
style_initialized = true; |
||||||
|
lv_style_init(&label_style); |
||||||
|
lv_style_set_text_color(&label_style, LV_STATE_DEFAULT, LV_COLOR_BLACK); |
||||||
|
lv_style_set_text_font(&label_style, LV_STATE_DEFAULT, &lv_font_montserrat_12); |
||||||
|
lv_style_set_text_letter_space(&label_style, LV_STATE_DEFAULT, 1); |
||||||
|
lv_style_set_text_line_space(&label_style, LV_STATE_DEFAULT, 1); |
||||||
|
} |
||||||
|
|
||||||
|
void set_wpm_symbol(lv_obj_t *label, int wpm) { |
||||||
|
char text[4] = {}; |
||||||
|
|
||||||
|
LOG_DBG("WPM changed to %i", wpm); |
||||||
|
sprintf(text, "%i ", wpm); |
||||||
|
|
||||||
|
lv_label_set_text(label, text); |
||||||
|
} |
||||||
|
|
||||||
|
int zmk_widget_wpm_status_init(struct zmk_widget_wpm_status *widget, lv_obj_t *parent) { |
||||||
|
wpm_status_init(); |
||||||
|
widget->obj = lv_label_create(parent, NULL); |
||||||
|
lv_obj_add_style(widget->obj, LV_LABEL_PART_MAIN, &label_style); |
||||||
|
lv_label_set_align(widget->obj, LV_LABEL_ALIGN_RIGHT); |
||||||
|
|
||||||
|
lv_obj_set_size(widget->obj, 40, 15); |
||||||
|
set_wpm_symbol(widget->obj, 0); |
||||||
|
|
||||||
|
sys_slist_append(&widgets, &widget->node); |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
lv_obj_t *zmk_widget_wpm_status_obj(struct zmk_widget_wpm_status *widget) { return widget->obj; } |
||||||
|
|
||||||
|
int wpm_status_listener(const zmk_event_t *eh) { |
||||||
|
struct zmk_wpm_state_changed *ev = as_zmk_wpm_state_changed(eh); |
||||||
|
struct zmk_widget_wpm_status *widget; |
||||||
|
SYS_SLIST_FOR_EACH_CONTAINER(&widgets, widget, node) { set_wpm_symbol(widget->obj, ev->state); } |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
ZMK_LISTENER(widget_wpm_status, wpm_status_listener) |
||||||
|
ZMK_SUBSCRIPTION(widget_wpm_status, zmk_wpm_state_changed); |
@ -0,0 +1,10 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <kernel.h> |
||||||
|
#include <zmk/events/wpm_state_changed.h> |
||||||
|
|
||||||
|
ZMK_EVENT_IMPL(zmk_wpm_state_changed); |
@ -0,0 +1,86 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2020 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <device.h> |
||||||
|
#include <init.h> |
||||||
|
#include <kernel.h> |
||||||
|
|
||||||
|
#include <logging/log.h> |
||||||
|
|
||||||
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); |
||||||
|
|
||||||
|
#include <zmk/event_manager.h> |
||||||
|
#include <zmk/events/wpm_state_changed.h> |
||||||
|
#include <zmk/events/keycode_state_changed.h> |
||||||
|
|
||||||
|
#include <zmk/wpm.h> |
||||||
|
|
||||||
|
#define WPM_UPDATE_INTERVAL_SECONDS 1 |
||||||
|
#define WPM_RESET_INTERVAL_SECONDS 5 |
||||||
|
|
||||||
|
// See https://en.wikipedia.org/wiki/Words_per_minute
|
||||||
|
// "Since the length or duration of words is clearly variable, for the purpose of measurement of
|
||||||
|
// text entry, the definition of each "word" is often standardized to be five characters or
|
||||||
|
// keystrokes long in English"
|
||||||
|
#define CHARS_PER_WORD 5.0 |
||||||
|
|
||||||
|
static uint8_t wpm_state = -1; |
||||||
|
static uint8_t last_wpm_state; |
||||||
|
static uint8_t wpm_update_counter; |
||||||
|
static uint32_t key_pressed_count; |
||||||
|
|
||||||
|
int zmk_wpm_get_state() { return wpm_state; } |
||||||
|
|
||||||
|
int wpm_event_listener(const zmk_event_t *eh) { |
||||||
|
const struct zmk_keycode_state_changed *ev = as_zmk_keycode_state_changed(eh); |
||||||
|
if (ev) { |
||||||
|
// count only key up events
|
||||||
|
if (!ev->state) { |
||||||
|
key_pressed_count++; |
||||||
|
LOG_DBG("key_pressed_count %d keycode %d", key_pressed_count, ev->keycode); |
||||||
|
} |
||||||
|
} |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
void wpm_work_handler(struct k_work *work) { |
||||||
|
wpm_update_counter++; |
||||||
|
wpm_state = (key_pressed_count / CHARS_PER_WORD) / |
||||||
|
(wpm_update_counter * WPM_UPDATE_INTERVAL_SECONDS / 60.0); |
||||||
|
|
||||||
|
if (last_wpm_state != wpm_state) { |
||||||
|
LOG_DBG("Raised WPM state changed %d wpm_update_counter %d", wpm_state, wpm_update_counter); |
||||||
|
|
||||||
|
ZMK_EVENT_RAISE( |
||||||
|
new_zmk_wpm_state_changed((struct zmk_wpm_state_changed){.state = wpm_state})); |
||||||
|
|
||||||
|
last_wpm_state = wpm_state; |
||||||
|
} |
||||||
|
|
||||||
|
if (wpm_update_counter >= WPM_RESET_INTERVAL_SECONDS) { |
||||||
|
wpm_update_counter = 0; |
||||||
|
key_pressed_count = 0; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
K_WORK_DEFINE(wpm_work, wpm_work_handler); |
||||||
|
|
||||||
|
void wpm_expiry_function() { k_work_submit(&wpm_work); } |
||||||
|
|
||||||
|
K_TIMER_DEFINE(wpm_timer, wpm_expiry_function, NULL); |
||||||
|
|
||||||
|
int wpm_init() { |
||||||
|
wpm_state = 0; |
||||||
|
wpm_update_counter = 0; |
||||||
|
k_timer_start(&wpm_timer, K_SECONDS(WPM_UPDATE_INTERVAL_SECONDS), |
||||||
|
K_SECONDS(WPM_UPDATE_INTERVAL_SECONDS)); |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
ZMK_LISTENER(wpm, wpm_event_listener); |
||||||
|
ZMK_SUBSCRIPTION(wpm, zmk_keycode_state_changed); |
||||||
|
|
||||||
|
SYS_INIT(wpm_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); |
@ -0,0 +1,2 @@ |
|||||||
|
s/.*wpm_work_handler: //p |
||||||
|
s/.*wpm_event_listener: //p |
@ -0,0 +1,7 @@ |
|||||||
|
key_pressed_count 1 keycode 5 |
||||||
|
Raised WPM state changed 12 wpm_update_counter 1 |
||||||
|
Raised WPM state changed 6 wpm_update_counter 2 |
||||||
|
Raised WPM state changed 4 wpm_update_counter 3 |
||||||
|
Raised WPM state changed 3 wpm_update_counter 4 |
||||||
|
Raised WPM state changed 2 wpm_update_counter 5 |
||||||
|
Raised WPM state changed 0 wpm_update_counter 1 |
@ -0,0 +1,9 @@ |
|||||||
|
CONFIG_KSCAN=n |
||||||
|
CONFIG_ZMK_KSCAN_MOCK_DRIVER=y |
||||||
|
CONFIG_ZMK_KSCAN_GPIO_DRIVER=n |
||||||
|
CONFIG_GPIO=n |
||||||
|
CONFIG_ZMK_BLE=n |
||||||
|
CONFIG_LOG=y |
||||||
|
CONFIG_LOG_BACKEND_SHOW_COLOR=n |
||||||
|
CONFIG_ZMK_LOG_LEVEL_DBG=y |
||||||
|
CONFIG_ZMK_WPM=y |
@ -0,0 +1,10 @@ |
|||||||
|
#include "../behavior_keymap.dtsi" |
||||||
|
|
||||||
|
&kscan { |
||||||
|
events = < |
||||||
|
ZMK_MOCK_PRESS(0,0,10) |
||||||
|
ZMK_MOCK_RELEASE(0,0,10) |
||||||
|
/* Wait for the worker to trigger and reset after 5 seconds, followed by a 0 at 6 seconds */ |
||||||
|
ZMK_MOCK_PRESS(0,0,6000) |
||||||
|
>; |
||||||
|
}; |
@ -0,0 +1,2 @@ |
|||||||
|
s/.*wpm_work_handler: //p |
||||||
|
s/.*wpm_event_listener: //p |
@ -0,0 +1,4 @@ |
|||||||
|
key_pressed_count 1 keycode 5 |
||||||
|
Raised WPM state changed 12 wpm_update_counter 1 |
||||||
|
key_pressed_count 2 keycode 5 |
||||||
|
Raised WPM state changed 8 wpm_update_counter 3 |
@ -0,0 +1,9 @@ |
|||||||
|
CONFIG_KSCAN=n |
||||||
|
CONFIG_ZMK_KSCAN_MOCK_DRIVER=y |
||||||
|
CONFIG_ZMK_KSCAN_GPIO_DRIVER=n |
||||||
|
CONFIG_GPIO=n |
||||||
|
CONFIG_ZMK_BLE=n |
||||||
|
CONFIG_LOG=y |
||||||
|
CONFIG_LOG_BACKEND_SHOW_COLOR=n |
||||||
|
CONFIG_ZMK_LOG_LEVEL_DBG=y |
||||||
|
CONFIG_ZMK_WPM=y |
@ -0,0 +1,15 @@ |
|||||||
|
#include "../behavior_keymap.dtsi" |
||||||
|
|
||||||
|
&kscan { |
||||||
|
events = < |
||||||
|
ZMK_MOCK_PRESS(0,0,10) |
||||||
|
ZMK_MOCK_RELEASE(0,0,10) |
||||||
|
//1st WPM worker call - 12wpm - 1 key press in 1 second |
||||||
|
ZMK_MOCK_PRESS(0,0,1000) |
||||||
|
ZMK_MOCK_RELEASE(0,0,10) |
||||||
|
// 2nd WPM worker call - 12wpm - 2 key press in 2 second |
||||||
|
// note there is no event for this as WPM hasn't changed |
||||||
|
// 3rd WPM worker call - 8wpm - 2 key press in 3 seconds |
||||||
|
ZMK_MOCK_PRESS(0,0,2000) |
||||||
|
>; |
||||||
|
}; |
@ -0,0 +1,17 @@ |
|||||||
|
#include <dt-bindings/zmk/keys.h> |
||||||
|
#include <behaviors.dtsi> |
||||||
|
#include <dt-bindings/zmk/kscan_mock.h> |
||||||
|
|
||||||
|
/ { |
||||||
|
keymap { |
||||||
|
compatible = "zmk,keymap"; |
||||||
|
label ="Default keymap"; |
||||||
|
|
||||||
|
default_layer { |
||||||
|
bindings = < |
||||||
|
&kp B &none |
||||||
|
&none &none |
||||||
|
>; |
||||||
|
}; |
||||||
|
}; |
||||||
|
}; |
Loading…
Reference in new issue