feat(widget): add bongo cat

This commit is contained in:
down 2022-05-28 17:58:59 +07:00
parent 3f28a55452
commit 5513f16d71
6 changed files with 372 additions and 0 deletions

View file

@ -0,0 +1,21 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <lvgl.h>
#include <kernel.h>
struct zmk_widget_bongo_cat {
sys_snode_t node;
lv_obj_t *obj;
#if !IS_ENABLED(CONFIG_ZMK_WIDGET_BONGO_CAT_INTERACTIVE)
lv_anim_t anim;
#endif
};
int zmk_widget_bongo_cat_init(struct zmk_widget_bongo_cat *widget, lv_obj_t *parent);
lv_obj_t *zmk_widget_bongo_cat_obj(struct zmk_widget_bongo_cat *widget);

View file

@ -9,6 +9,7 @@
#include <zmk/display/widgets/battery_status.h>
#include <zmk/display/widgets/layer_status.h>
#include <zmk/display/widgets/wpm_status.h>
#include <zmk/display/widgets/bongo_cat.h>
#include <zmk/display/status_screen.h>
#include <logging/log.h>
@ -34,6 +35,10 @@ static struct zmk_widget_layer_status layer_status_widget;
static struct zmk_widget_wpm_status wpm_status_widget;
#endif
#if IS_ENABLED(CONFIG_ZMK_WIDGET_BONGO_CAT)
static struct zmk_widget_bongo_cat bongo_cat_widget;
#endif
lv_obj_t *zmk_display_status_screen() {
lv_obj_t *screen;
@ -70,6 +75,10 @@ lv_obj_t *zmk_display_status_screen() {
zmk_widget_wpm_status_init(&wpm_status_widget, screen);
lv_obj_align(zmk_widget_wpm_status_obj(&wpm_status_widget), NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -12,
0);
#endif
#if IS_ENABLED(CONFIG_ZMK_WIDGET_BONGO_CAT)
zmk_widget_bongo_cat_init(&bongo_cat_widget, screen);
lv_obj_align(zmk_widget_bongo_cat_obj(&bongo_cat_widget), NULL, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
#endif
return screen;
}

View file

@ -6,3 +6,5 @@ target_sources_ifdef(CONFIG_ZMK_WIDGET_OUTPUT_STATUS app PRIVATE output_status.c
target_sources_ifdef(CONFIG_ZMK_WIDGET_PERIPHERAL_STATUS app PRIVATE peripheral_status.c)
target_sources_ifdef(CONFIG_ZMK_WIDGET_LAYER_STATUS app PRIVATE layer_status.c)
target_sources_ifdef(CONFIG_ZMK_WIDGET_WPM_STATUS app PRIVATE wpm_status.c)
target_sources_ifdef(CONFIG_ZMK_WIDGET_BONGO_CAT app PRIVATE bongo_cat.c)
target_sources_ifdef(CONFIG_ZMK_WIDGET_BONGO_CAT app PRIVATE bongo_img.c)

View file

@ -33,4 +33,27 @@ config ZMK_WIDGET_WPM_STATUS
select LVGL_USE_LABEL
select ZMK_WPM
config ZMK_WIDGET_BONGO_CAT
bool "Widget for displaying bongo cat"
depends on !ZMK_SPLIT || ZMK_SPLIT_BLE_ROLE_CENTRAL
select LVGL_USE_LABEL
select LVGL_USE_IMG
select LVGL_USE_ANIMATION if !ZMK_WIDGET_BONGO_CAT_INTERACTIVE
if ZMK_WIDGET_BONGO_CAT
config ZMK_WIDGET_BONGO_CAT_INTERACTIVE
bool "Bongo cat responds to key press"
default y
if !ZMK_WIDGET_BONGO_CAT_INTERACTIVE
config ZMK_WIDGET_BONGO_CAT_ANIMATION_INTERVAL
int "Time delay in ms for bongo cat animation"
default 531
endif
endif # ZMK_WIDGET_BONGO_CAT
endmenu

View file

@ -0,0 +1,106 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/display.h>
#include <zmk/display/widgets/bongo_cat.h>
#include <zmk/event_manager.h>
#if IS_ENABLED(CONFIG_ZMK_WIDGET_BONGO_CAT_INTERACTIVE)
#include <zmk/events/position_state_changed.h>
#endif
static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets);
LV_IMG_DECLARE(left);
LV_IMG_DECLARE(right);
#if IS_ENABLED(CONFIG_ZMK_WIDGET_BONGO_CAT_INTERACTIVE)
LV_IMG_DECLARE(none);
LV_IMG_DECLARE(both);
enum bongo_state {
bongo_state_none,
bongo_state_left,
bongo_state_right,
} current_bongo_state;
const void *images[] = {
&none,
&left,
&right,
&both
};
#else
const void *images[] = {
&left,
&right
};
void set_bongo_state(void *var, lv_anim_value_t val) {
lv_img_set_src( (lv_obj_t *)var, images[val]);
}
#endif /* IS_ENABLED(CONFIG_ZMK_WIDGET_BONGO_CAT_INTERACTIVE) */
int zmk_widget_bongo_cat_init(struct zmk_widget_bongo_cat *widget, lv_obj_t *parent) {
widget->obj = lv_img_create(parent, NULL);
#if IS_ENABLED(CONFIG_ZMK_WIDGET_BONGO_CAT_INTERACTIVE)
lv_img_set_src(widget->obj, &none);
current_bongo_state = bongo_state_none;
#else
lv_anim_init(&widget->anim);
lv_anim_set_exec_cb(&widget->anim, (lv_anim_exec_xcb_t) set_bongo_state);
lv_anim_set_var(&widget->anim, widget->obj);
lv_anim_set_time(&widget->anim, CONFIG_ZMK_WIDGET_BONGO_CAT_ANIMATION_INTERVAL);
lv_anim_set_values(&widget->anim, 0, 1);
lv_anim_set_repeat_count(&widget->anim, LV_ANIM_REPEAT_INFINITE);
lv_anim_set_repeat_delay(&widget->anim, CONFIG_ZMK_WIDGET_BONGO_CAT_ANIMATION_INTERVAL);
lv_anim_start(&widget->anim);
#endif /* IS_ENABLED(CONFIG_ZMK_WIDGET_BONGO_CAT_INTERACTIVE) */
sys_slist_append(&widgets, &widget->node);
return 0;
}
lv_obj_t *zmk_widget_bongo_cat_obj(struct zmk_widget_bongo_cat *widget) {
return widget->obj;
}
#if IS_ENABLED(CONFIG_ZMK_WIDGET_BONGO_CAT_INTERACTIVE)
void set_bongo_state(lv_obj_t *img, struct zmk_position_state_changed *ev) {
if (ev == NULL) {
return;
}
uint8_t tmp = bongo_state_left;
if (ev->state) {
if (current_bongo_state & (bongo_state_left | bongo_state_right)) {
tmp = bongo_state_left | bongo_state_right;
}
} else {
if (current_bongo_state ^ (bongo_state_left | bongo_state_right)) {
tmp = bongo_state_none;
}
}
current_bongo_state = tmp;
lv_img_set_src(img, images[current_bongo_state]);
}
int bongo_cat_listener(const zmk_event_t *eh) {
struct zmk_widget_bongo_cat *widget;
struct zmk_position_state_changed *ev = as_zmk_position_state_changed(eh);
SYS_SLIST_FOR_EACH_CONTAINER(&widgets, widget, node) { set_bongo_state(widget->obj, ev); }
return ZMK_EV_EVENT_BUBBLE;
}
ZMK_LISTENER(widget_bongo_cat, bongo_cat_listener)
ZMK_SUBSCRIPTION(widget_bongo_cat, zmk_position_state_changed);
#endif /* IS_ENABLED(CONFIG_ZMK_WIDGET_BONGO_CAT_INTERACTIVE) */

View file

@ -0,0 +1,211 @@
#include <lvgl.h>
#ifndef LV_ATTRIBUTE_MEM_ALIGN
#define LV_ATTRIBUTE_MEM_ALIGN
#endif
#ifndef LV_ATTRIBUTE_IMG_LEFT
#define LV_ATTRIBUTE_IMG_LEFT
#endif
const LV_ATTRIBUTE_MEM_ALIGN LV_ATTRIBUTE_LARGE_CONST LV_ATTRIBUTE_IMG_LEFT uint8_t left_map[] = {
0xff, 0xff, 0xff, 0xff, /*Color of index 0*/
0x00, 0x00, 0x00, 0xff, /*Color of index 1*/
0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x80, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x06, 0x00, 0x70, 0x00, 0x00, 0x00,
0x00, 0x00, 0x18, 0x00, 0x0e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x60, 0x00, 0x01, 0xc0, 0x00, 0x00,
0x00, 0x38, 0x80, 0x00, 0x00, 0x30, 0x30, 0x00,
0x00, 0x45, 0x00, 0x00, 0x00, 0x0c, 0xc8, 0x00,
0x00, 0x82, 0x00, 0x00, 0x00, 0x03, 0x08, 0x00,
0x00, 0x82, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x80, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x80, 0x06, 0x00, 0x00, 0x00, 0x10, 0x00,
0xf0, 0x80, 0x00, 0x48, 0x00, 0x00, 0x10, 0x00,
0x0f, 0x80, 0x00, 0x31, 0x00, 0x00, 0x10, 0x00,
0x00, 0xf0, 0x00, 0x06, 0x30, 0x00, 0x20, 0x00,
0x00, 0x0f, 0x00, 0x00, 0x30, 0x00, 0x20, 0x00,
0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x20, 0x00,
0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x10, 0x00,
0x00, 0x00, 0x00, 0xf0, 0x02, 0x00, 0x10, 0x00,
0x00, 0x00, 0x00, 0x0f, 0x04, 0x00, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x0f, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x30, 0xf4, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0xc0, 0x0f, 0x00,
0x00, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0xf0,
0x00, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
};
const lv_img_dsc_t left = {
.header.cf = LV_IMG_CF_INDEXED_1BIT,
.header.always_zero = 0,
.header.reserved = 0,
.header.w = 60,
.header.h = 31,
.data_size = 256,
.data = left_map,
};
#ifndef LV_ATTRIBUTE_IMG_RIGHT
#define LV_ATTRIBUTE_IMG_RIGHT
#endif
const LV_ATTRIBUTE_MEM_ALIGN LV_ATTRIBUTE_LARGE_CONST LV_ATTRIBUTE_IMG_RIGHT uint8_t right_map[] = {
0xff, 0xff, 0xff, 0xff, /*Color of index 0*/
0x00, 0x00, 0x00, 0xff, /*Color of index 1*/
0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x80, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x06, 0x00, 0x70, 0x00, 0x00, 0x00,
0x00, 0x00, 0x18, 0x00, 0x0e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x60, 0x00, 0x01, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x80, 0x00, 0x00, 0x30, 0x30, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x0c, 0xc8, 0x00,
0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x08, 0x00,
0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x10, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x20, 0x06, 0x00, 0x00, 0x00, 0x10, 0x00,
0xf0, 0x40, 0x00, 0x48, 0x00, 0x00, 0x10, 0x00,
0x0f, 0x40, 0x00, 0x31, 0x00, 0x70, 0x10, 0x00,
0x00, 0x80, 0x20, 0x06, 0x30, 0x88, 0x20, 0x00,
0x00, 0x80, 0x40, 0x00, 0x31, 0x04, 0x20, 0x00,
0x01, 0x00, 0xf0, 0x00, 0x01, 0x04, 0x20, 0x00,
0x01, 0x01, 0x0f, 0x00, 0x01, 0x00, 0x10, 0x00,
0x01, 0x02, 0x00, 0xf0, 0x01, 0x00, 0x10, 0x00,
0x00, 0x8c, 0x00, 0x0f, 0x01, 0x00, 0x08, 0x00,
0x00, 0x70, 0x00, 0x00, 0xf1, 0x00, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const lv_img_dsc_t right = {
.header.cf = LV_IMG_CF_INDEXED_1BIT,
.header.always_zero = 0,
.header.reserved = 0,
.header.w = 60,
.header.h = 31,
.data_size = 256,
.data = right_map,
};
#if IS_ENABLED(CONFIG_ZMK_WIDGET_BONGO_CAT_INTERACTIVE)
#ifndef LV_ATTRIBUTE_IMG_NONE
#define LV_ATTRIBUTE_IMG_NONE
#endif
const LV_ATTRIBUTE_MEM_ALIGN LV_ATTRIBUTE_LARGE_CONST LV_ATTRIBUTE_IMG_NONE uint8_t none_map[] = {
0xff, 0xff, 0xff, 0xff, /*Color of index 0*/
0x00, 0x00, 0x00, 0xff, /*Color of index 1*/
0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x80, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x06, 0x00, 0x70, 0x00, 0x00, 0x00,
0x00, 0x00, 0x18, 0x00, 0x0e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x60, 0x00, 0x01, 0xc0, 0x00, 0x00,
0x00, 0x38, 0x80, 0x00, 0x00, 0x30, 0x30, 0x00,
0x00, 0x45, 0x00, 0x00, 0x00, 0x0c, 0xc8, 0x00,
0x00, 0x82, 0x00, 0x00, 0x00, 0x03, 0x08, 0x00,
0x00, 0x82, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x80, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x80, 0x06, 0x00, 0x00, 0x00, 0x10, 0x00,
0xf0, 0x80, 0x00, 0x48, 0x00, 0x00, 0x10, 0x00,
0x0f, 0x80, 0x00, 0x31, 0x00, 0x70, 0x10, 0x00,
0x00, 0xf0, 0x00, 0x06, 0x30, 0x88, 0x20, 0x00,
0x00, 0x0f, 0x00, 0x00, 0x31, 0x04, 0x20, 0x00,
0x00, 0x00, 0xf0, 0x00, 0x01, 0x04, 0x20, 0x00,
0x00, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x10, 0x00,
0x00, 0x00, 0x00, 0xf0, 0x01, 0x00, 0x10, 0x00,
0x00, 0x00, 0x00, 0x0f, 0x01, 0x00, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0xf1, 0x00, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf4, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const lv_img_dsc_t none = {
.header.cf = LV_IMG_CF_INDEXED_1BIT,
.header.always_zero = 0,
.header.reserved = 0,
.header.w = 60,
.header.h = 31,
.data_size = 256,
.data = none_map,
};
#ifndef LV_ATTRIBUTE_IMG_BOTH
#define LV_ATTRIBUTE_IMG_BOTH
#endif
const LV_ATTRIBUTE_MEM_ALIGN LV_ATTRIBUTE_LARGE_CONST LV_ATTRIBUTE_IMG_BOTH uint8_t both_map[] = {
0xff, 0xff, 0xff, 0xff, /*Color of index 0*/
0x00, 0x00, 0x00, 0xff, /*Color of index 1*/
0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x80, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x06, 0x00, 0x70, 0x00, 0x00, 0x00,
0x00, 0x00, 0x18, 0x00, 0x0e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x60, 0x00, 0x01, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x80, 0x00, 0x00, 0x30, 0x30, 0x00,
0x00, 0x01, 0x00, 0x00, 0x00, 0x0c, 0xc8, 0x00,
0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x08, 0x00,
0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x10, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00,
0x00, 0x20, 0x06, 0x00, 0x00, 0x00, 0x10, 0x00,
0xf0, 0x40, 0x00, 0x48, 0x00, 0x00, 0x10, 0x00,
0x0f, 0x40, 0x00, 0x31, 0x00, 0x00, 0x10, 0x00,
0x00, 0x80, 0x20, 0x06, 0x30, 0x00, 0x20, 0x00,
0x00, 0x80, 0x40, 0x00, 0x30, 0x00, 0x20, 0x00,
0x01, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x20, 0x00,
0x01, 0x01, 0x0f, 0x00, 0x00, 0x00, 0x10, 0x00,
0x01, 0x02, 0x00, 0xf0, 0x02, 0x00, 0x10, 0x00,
0x00, 0x8c, 0x00, 0x0f, 0x04, 0x00, 0x08, 0x00,
0x00, 0x70, 0x00, 0x00, 0xf8, 0x00, 0x08, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x0f, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x30, 0xf4, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0xc0, 0x0f, 0x00,
0x00, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0xf0,
0x00, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
};
const lv_img_dsc_t both = {
.header.cf = LV_IMG_CF_INDEXED_1BIT,
.header.always_zero = 0,
.header.reserved = 0,
.header.w = 60,
.header.h = 31,
.data_size = 256,
.data = both_map,
};
#endif /* IS_ENABLED(CONFIG_ZMK_WIDGET_BONGO_CAT_INTERACTIVE) */