Skip to content

Commit

Permalink
Fix input lag
Browse files Browse the repository at this point in the history
  • Loading branch information
M165437 committed Oct 25, 2024
1 parent 1ba56e3 commit 3f38221
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 48 deletions.
6 changes: 3 additions & 3 deletions boards/shields/nice_view_gem/widgets/animation.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ const lv_img_dsc_t *anim_imgs[] = {
&crystal_13, &crystal_14, &crystal_15, &crystal_16,
};

void draw_animation(lv_obj_t *canvas, struct zmk_widget_screen *widget) {
void draw_animation(lv_obj_t *canvas) {
#if IS_ENABLED(CONFIG_NICE_VIEW_GEM_ANIMATION)
lv_obj_t *art = lv_animimg_create(widget->obj);
lv_obj_t *art = lv_animimg_create(canvas);
lv_obj_center(art);

lv_animimg_set_src(art, (const void **)anim_imgs, 16);
lv_animimg_set_duration(art, CONFIG_NICE_VIEW_GEM_ANIMATION_MS);
lv_animimg_set_repeat_count(art, LV_ANIM_REPEAT_INFINITE);
lv_animimg_start(art);
#else
lv_obj_t *art = lv_img_create(widget->obj);
lv_obj_t *art = lv_img_create(canvas);

int length = sizeof(anim_imgs) / sizeof(anim_imgs[0]);
srand(k_uptime_get_32());
Expand Down
2 changes: 1 addition & 1 deletion boards/shields/nice_view_gem/widgets/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
#include "util.h"
#include "screen_peripheral.h"

void draw_animation(lv_obj_t *canvas, struct zmk_widget_screen *widget);
void draw_animation(lv_obj_t *canvas);
2 changes: 1 addition & 1 deletion boards/shields/nice_view_gem/widgets/layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ void draw_layer_status(lv_obj_t *canvas, const struct status_state *state) {
to_uppercase(text);
}

lv_canvas_draw_text(canvas, 0, 146, 68, &label_dsc, text);
lv_canvas_draw_text(canvas, 0, 146 + BUFFER_OFFSET_BOTTOM, 68, &label_dsc, text);
}
4 changes: 2 additions & 2 deletions boards/shields/nice_view_gem/widgets/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static void draw_inactive_profiles(lv_obj_t *canvas, const struct status_state *
lv_draw_img_dsc_t img_dsc;
lv_draw_img_dsc_init(&img_dsc);

lv_canvas_draw_img(canvas, 18, 129, &profiles, &img_dsc);
lv_canvas_draw_img(canvas, 18, 129 + BUFFER_OFFSET_BOTTOM, &profiles, &img_dsc);
}

static void draw_active_profile(lv_obj_t *canvas, const struct status_state *state) {
Expand All @@ -16,7 +16,7 @@ static void draw_active_profile(lv_obj_t *canvas, const struct status_state *sta

int offset = state->active_profile_index * 7;

lv_canvas_draw_rect(canvas, 18 + offset, 129, 3, 3, &rect_white_dsc);
lv_canvas_draw_rect(canvas, 18 + offset, 129 + BUFFER_OFFSET_BOTTOM, 3, 3, &rect_white_dsc);
}

void draw_profile_status(lv_obj_t *canvas, const struct status_state *state) {
Expand Down
52 changes: 40 additions & 12 deletions boards/shields/nice_view_gem/widgets/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,37 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets);

/**
* Draw canvas
* Draw buffers
**/

static void draw_canvas(lv_obj_t *widget, lv_color_t cbuf[], const struct status_state *state) {
static void draw_top(lv_obj_t *widget, lv_color_t cbuf[], const struct status_state *state) {
lv_obj_t *canvas = lv_obj_get_child(widget, 0);
fill_background(canvas);

// Draw widgets
draw_background(canvas);
draw_output_status(canvas, state);
draw_battery_status(canvas, state);

// Rotate for horizontal display
rotate_canvas(canvas, cbuf);
}

static void draw_middle(lv_obj_t *widget, lv_color_t cbuf[], const struct status_state *state) {
lv_obj_t *canvas = lv_obj_get_child(widget, 1);
fill_background(canvas);

// Draw widgets
draw_wpm_status(canvas, state);

// Rotate for horizontal display
rotate_canvas(canvas, cbuf);
}

static void draw_bottom(lv_obj_t *widget, lv_color_t cbuf[], const struct status_state *state) {
lv_obj_t *canvas = lv_obj_get_child(widget, 2);
fill_background(canvas);

// Draw widgets
draw_profile_status(canvas, state);
draw_layer_status(canvas, state);

Expand All @@ -55,10 +75,9 @@ static void set_battery_status(struct zmk_widget_screen *widget,
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK)
widget->state.charging = state.usb_present;
#endif /* IS_ENABLED(CONFIG_USB_DEVICE_STACK) */

widget->state.battery = state.level;

draw_canvas(widget->obj, widget->cbuf, &widget->state);
draw_top(widget->obj, widget->cbuf, &widget->state);
}

static void battery_status_update_cb(struct battery_status_state state) {
Expand Down Expand Up @@ -93,7 +112,7 @@ static void set_layer_status(struct zmk_widget_screen *widget, struct layer_stat
widget->state.layer_index = state.index;
widget->state.layer_label = state.label;

draw_canvas(widget->obj, widget->cbuf, &widget->state);
draw_bottom(widget->obj, widget->cbuf3, &widget->state);
}

static void layer_status_update_cb(struct layer_status_state state) {
Expand Down Expand Up @@ -122,7 +141,8 @@ static void set_output_status(struct zmk_widget_screen *widget,
widget->state.active_profile_connected = state->active_profile_connected;
widget->state.active_profile_bonded = state->active_profile_bonded;

draw_canvas(widget->obj, widget->cbuf, &widget->state);
draw_top(widget->obj, widget->cbuf, &widget->state);
draw_bottom(widget->obj, widget->cbuf3, &widget->state);
}

static void output_status_update_cb(struct output_status_state state) {
Expand Down Expand Up @@ -160,7 +180,7 @@ static void set_wpm_status(struct zmk_widget_screen *widget, struct wpm_status_s
}
widget->state.wpm[9] = state.wpm;

draw_canvas(widget->obj, widget->cbuf, &widget->state);
draw_middle(widget->obj, widget->cbuf2, &widget->state);
}

static void wpm_status_update_cb(struct wpm_status_state state) {
Expand All @@ -182,11 +202,19 @@ ZMK_SUBSCRIPTION(widget_wpm_status, zmk_wpm_state_changed);

int zmk_widget_screen_init(struct zmk_widget_screen *widget, lv_obj_t *parent) {
widget->obj = lv_obj_create(parent);
lv_obj_set_size(widget->obj, CANVAS_HEIGHT, CANVAS_WIDTH);
lv_obj_set_size(widget->obj, SCREEN_HEIGHT, SCREEN_WIDTH);

lv_obj_t *top = lv_canvas_create(widget->obj);
lv_obj_align(top, LV_ALIGN_TOP_RIGHT, 0, 0);
lv_canvas_set_buffer(top, widget->cbuf, BUFFER_SIZE, BUFFER_SIZE, LV_IMG_CF_TRUE_COLOR);

lv_obj_t *middle = lv_canvas_create(widget->obj);
lv_obj_align(middle, LV_ALIGN_TOP_RIGHT, BUFFER_OFFSET_MIDDLE, 0);
lv_canvas_set_buffer(middle, widget->cbuf2, BUFFER_SIZE, BUFFER_SIZE, LV_IMG_CF_TRUE_COLOR);

lv_obj_t *canvas = lv_canvas_create(widget->obj);
lv_obj_align(canvas, LV_ALIGN_TOP_LEFT, 0, 0);
lv_canvas_set_buffer(canvas, widget->cbuf, CANVAS_HEIGHT, CANVAS_HEIGHT, LV_IMG_CF_TRUE_COLOR);
lv_obj_t *bottom = lv_canvas_create(widget->obj);
lv_obj_align(bottom, LV_ALIGN_TOP_RIGHT, BUFFER_OFFSET_BOTTOM, 0);
lv_canvas_set_buffer(bottom, widget->cbuf3, BUFFER_SIZE, BUFFER_SIZE, LV_IMG_CF_TRUE_COLOR);

sys_slist_append(&widgets, &widget->node);
widget_battery_status_init();
Expand Down
4 changes: 3 additions & 1 deletion boards/shields/nice_view_gem/widgets/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
struct zmk_widget_screen {
sys_snode_t node;
lv_obj_t *obj;
lv_color_t cbuf[CANVAS_HEIGHT * CANVAS_HEIGHT];
lv_color_t cbuf[BUFFER_SIZE * BUFFER_SIZE];
lv_color_t cbuf2[BUFFER_SIZE * BUFFER_SIZE];
lv_color_t cbuf3[BUFFER_SIZE * BUFFER_SIZE];
struct status_state state;
};

Expand Down
20 changes: 10 additions & 10 deletions boards/shields/nice_view_gem/widgets/screen_peripheral.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets);

/**
* Draw canvas
* Draw buffers
**/

static void draw_canvas(lv_obj_t *widget, lv_color_t cbuf[], const struct status_state *state) {
static void draw_top(lv_obj_t *widget, lv_color_t cbuf[], const struct status_state *state) {
lv_obj_t *canvas = lv_obj_get_child(widget, 0);
fill_background(canvas);

// Draw widgets
draw_background(canvas);
draw_output_status(canvas, state);
draw_battery_status(canvas, state);

Expand All @@ -48,7 +48,7 @@ static void set_battery_status(struct zmk_widget_screen *widget,

widget->state.battery = state.level;

draw_canvas(widget->obj, widget->cbuf, &widget->state);
draw_top(widget->obj, widget->cbuf, &widget->state);
}

static void battery_status_update_cb(struct battery_status_state state) {
Expand Down Expand Up @@ -87,7 +87,7 @@ static void set_connection_status(struct zmk_widget_screen *widget,
struct peripheral_status_state state) {
widget->state.connected = state.connected;

draw_canvas(widget->obj, widget->cbuf, &widget->state);
draw_top(widget->obj, widget->cbuf, &widget->state);
}

static void output_status_update_cb(struct peripheral_status_state state) {
Expand All @@ -105,13 +105,13 @@ ZMK_SUBSCRIPTION(widget_peripheral_status, zmk_split_peripheral_status_changed);

int zmk_widget_screen_init(struct zmk_widget_screen *widget, lv_obj_t *parent) {
widget->obj = lv_obj_create(parent);
lv_obj_set_size(widget->obj, CANVAS_HEIGHT, CANVAS_WIDTH);
lv_obj_set_size(widget->obj, SCREEN_HEIGHT, SCREEN_WIDTH);

lv_obj_t *canvas = lv_canvas_create(widget->obj);
lv_obj_align(canvas, LV_ALIGN_TOP_LEFT, 0, 0);
lv_canvas_set_buffer(canvas, widget->cbuf, CANVAS_HEIGHT, CANVAS_HEIGHT, LV_IMG_CF_TRUE_COLOR);
lv_obj_t *top = lv_canvas_create(widget->obj);
lv_obj_align(top, LV_ALIGN_TOP_RIGHT, 0, 0);
lv_canvas_set_buffer(top, widget->cbuf, BUFFER_SIZE, BUFFER_SIZE, LV_IMG_CF_TRUE_COLOR);

draw_animation(canvas, widget);
draw_animation(widget->obj);

sys_slist_append(&widgets, &widget->node);
widget_battery_status_init();
Expand Down
2 changes: 1 addition & 1 deletion boards/shields/nice_view_gem/widgets/screen_peripheral.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
struct zmk_widget_screen {
sys_snode_t node;
lv_obj_t *obj;
lv_color_t cbuf[CANVAS_HEIGHT * CANVAS_HEIGHT];
lv_color_t cbuf[BUFFER_SIZE * BUFFER_SIZE];
struct status_state state;
};

Expand Down
14 changes: 7 additions & 7 deletions boards/shields/nice_view_gem/widgets/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ void to_uppercase(char *str) {
}

void rotate_canvas(lv_obj_t *canvas, lv_color_t cbuf[]) {
static lv_color_t cbuf_tmp[CANVAS_HEIGHT * CANVAS_HEIGHT];
static lv_color_t cbuf_tmp[BUFFER_SIZE * BUFFER_SIZE];
memcpy(cbuf_tmp, cbuf, sizeof(cbuf_tmp));

lv_img_dsc_t img;
img.data = (void *)cbuf_tmp;
img.header.cf = LV_IMG_CF_TRUE_COLOR;
img.header.w = CANVAS_HEIGHT;
img.header.h = CANVAS_HEIGHT;
img.header.w = BUFFER_SIZE;
img.header.h = BUFFER_SIZE;

lv_canvas_fill_bg(canvas, LVGL_BACKGROUND, LV_OPA_COVER);
lv_canvas_transform(canvas, &img, 900, LV_IMG_ZOOM_NONE, -1, 0, CANVAS_HEIGHT / 2,
CANVAS_HEIGHT / 2, false);
lv_canvas_transform(canvas, &img, 900, LV_IMG_ZOOM_NONE, -1, 0, BUFFER_SIZE / 2,
BUFFER_SIZE / 2, false);
}

void draw_background(lv_obj_t *canvas) {
void fill_background(lv_obj_t *canvas) {
lv_draw_rect_dsc_t rect_black_dsc;
init_rect_dsc(&rect_black_dsc, LVGL_BACKGROUND);

lv_canvas_draw_rect(canvas, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, &rect_black_dsc);
lv_canvas_draw_rect(canvas, 0, 0, BUFFER_SIZE, BUFFER_SIZE, &rect_black_dsc);
}

void init_label_dsc(lv_draw_label_dsc_t *label_dsc, lv_color_t color, const lv_font_t *font,
Expand Down
10 changes: 7 additions & 3 deletions boards/shields/nice_view_gem/widgets/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#include <lvgl.h>
#include <zmk/endpoints.h>

#define CANVAS_WIDTH 68
#define CANVAS_HEIGHT 160
#define SCREEN_WIDTH 68
#define SCREEN_HEIGHT 160

#define BUFFER_SIZE 68
#define BUFFER_OFFSET_MIDDLE -44
#define BUFFER_OFFSET_BOTTOM -129

#define LVGL_BACKGROUND \
IS_ENABLED(CONFIG_NICE_VIEW_WIDGET_INVERTED) ? lv_color_black() : lv_color_white()
Expand All @@ -29,7 +33,7 @@ struct status_state {

void to_uppercase(char *str);
void rotate_canvas(lv_obj_t *canvas, lv_color_t cbuf[]);
void draw_background(lv_obj_t *canvas);
void fill_background(lv_obj_t *canvas);
void init_rect_dsc(lv_draw_rect_dsc_t *rect_dsc, lv_color_t bg_color);
void init_line_dsc(lv_draw_line_dsc_t *line_dsc, lv_color_t color, uint8_t width);
void init_label_dsc(lv_draw_label_dsc_t *label_dsc, lv_color_t color, const lv_font_t *font,
Expand Down
16 changes: 9 additions & 7 deletions boards/shields/nice_view_gem/widgets/wpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ static void draw_gauge(lv_obj_t *canvas, const struct status_state *state) {
lv_draw_img_dsc_t img_dsc;
lv_draw_img_dsc_init(&img_dsc);

lv_canvas_draw_img(canvas, 16, 43, &gauge, &img_dsc);
lv_canvas_draw_img(canvas, 16, 44 + BUFFER_OFFSET_MIDDLE, &gauge, &img_dsc);
}

static void draw_needle(lv_obj_t *canvas, const struct status_state *state) {
lv_draw_line_dsc_t line_dsc;
init_line_dsc(&line_dsc, LVGL_FOREGROUND, 1);

int centerX = 33;
int centerY = 66;
int centerY = 67 + BUFFER_OFFSET_MIDDLE;
int offset = 13;
int value = state->wpm[9];

Expand Down Expand Up @@ -56,14 +56,16 @@ static void draw_grid(lv_obj_t *canvas) {
lv_draw_img_dsc_t img_dsc;
lv_draw_img_dsc_init(&img_dsc);

lv_canvas_draw_img(canvas, 0, 65, &grid, &img_dsc);
lv_canvas_draw_img(canvas, 0, 65 + BUFFER_OFFSET_MIDDLE, &grid, &img_dsc);
}

static void draw_graph(lv_obj_t *canvas, const struct status_state *state) {
lv_draw_line_dsc_t line_dsc;
init_line_dsc(&line_dsc, LVGL_FOREGROUND, 2);
lv_point_t points[10];

int baselineY = 97 + BUFFER_OFFSET_MIDDLE;

#if IS_ENABLED(CONFIG_NICE_VIEW_GEM_WPM_FIXED_RANGE)
int max = CONFIG_NICE_VIEW_GEM_WPM_FIXED_RANGE_MAX;
if (max == 0) {
Expand All @@ -77,7 +79,7 @@ static void draw_graph(lv_obj_t *canvas, const struct status_state *state) {
value = max;
}
points[i].x = 0 + i * 7.4;
points[i].y = 97 - (value * 32 / max);
points[i].y = baselineY - (value * 32 / max);
}
#else
int max = 0;
Expand All @@ -99,7 +101,7 @@ static void draw_graph(lv_obj_t *canvas, const struct status_state *state) {

for (int i = 0; i < 10; i++) {
points[i].x = 0 + i * 7.4;
points[i].y = 97 - (state->wpm[i] - min) * 32 / range;
points[i].y = baselineY - (state->wpm[i] - min) * 32 / range;
}
#endif

Expand All @@ -109,15 +111,15 @@ static void draw_graph(lv_obj_t *canvas, const struct status_state *state) {
static void draw_label(lv_obj_t *canvas, const struct status_state *state) {
lv_draw_label_dsc_t label_left_dsc;
init_label_dsc(&label_left_dsc, LVGL_FOREGROUND, &pixel_operator_mono, LV_TEXT_ALIGN_LEFT);
lv_canvas_draw_text(canvas, 0, 103, 25, &label_left_dsc, "WPM");
lv_canvas_draw_text(canvas, 0, 101 + BUFFER_OFFSET_MIDDLE, 25, &label_left_dsc, "WPM");

lv_draw_label_dsc_t label_dsc_wpm;
init_label_dsc(&label_dsc_wpm, LVGL_FOREGROUND, &pixel_operator_mono, LV_TEXT_ALIGN_RIGHT);

char wpm_text[6] = {};

snprintf(wpm_text, sizeof(wpm_text), "%d", state->wpm[9]);
lv_canvas_draw_text(canvas, 26, 103, 42, &label_dsc_wpm, wpm_text);
lv_canvas_draw_text(canvas, 26, 101 + BUFFER_OFFSET_MIDDLE, 42, &label_dsc_wpm, wpm_text);
}

void draw_wpm_status(lv_obj_t *canvas, const struct status_state *state) {
Expand Down

0 comments on commit 3f38221

Please sign in to comment.