Skip to content

Commit

Permalink
clean m66
Browse files Browse the repository at this point in the history
  • Loading branch information
alin elena committed Nov 7, 2023
1 parent cb0a18c commit 6ce37b8
Show file tree
Hide file tree
Showing 10 changed files with 936 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config/boards/shields/mlego_m66_rev4/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
if(CONFIG_ZMK_DISPLAY AND CONFIG_NICE_VIEW_WIDGET_STATUS)
zephyr_library_include_directories(${CMAKE_SOURCE_DIR}/include)
zephyr_library_sources(custom_status_screen.c)
zephyr_library_sources(widgets/bolt.c)
zephyr_library_sources(widgets/util.c)

if(NOT CONFIG_ZMK_SPLIT OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
zephyr_library_sources(widgets/status.c)
else()
zephyr_library_sources(widgets/art.c)
zephyr_library_sources(widgets/peripheral_status.c)
endif()
endif()
28 changes: 28 additions & 0 deletions config/boards/shields/mlego_m66_rev4/custom_status_screen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
*
* Copyright (c) 2023 The ZMK Contributors
* SPDX-License-Identifier: MIT
*
*/

#include "widgets/status.h"

#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#if IS_ENABLED(CONFIG_NICE_VIEW_WIDGET_STATUS)
static struct zmk_widget_status status_widget;
#endif

lv_obj_t *zmk_display_status_screen() {

lv_obj_t *screen;
screen = lv_obj_create(NULL);

#if IS_ENABLED(CONFIG_NICE_VIEW_WIDGET_STATUS)
zmk_widget_status_init(&status_widget, screen);
lv_obj_align(zmk_widget_status_obj(&status_widget), LV_ALIGN_TOP_LEFT, 0, 0);
#endif

return screen;
}
229 changes: 229 additions & 0 deletions config/boards/shields/mlego_m66_rev4/widgets/art.c

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions config/boards/shields/mlego_m66_rev4/widgets/bolt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
*
* Copyright (c) 2023 The ZMK Contributors
* SPDX-License-Identifier: MIT
*
*/

#include <lvgl.h>

#ifndef LV_ATTRIBUTE_MEM_ALIGN
#define LV_ATTRIBUTE_MEM_ALIGN
#endif

#ifndef LV_ATTRIBUTE_IMG_BOLT
#define LV_ATTRIBUTE_IMG_BOLT
#endif

const LV_ATTRIBUTE_MEM_ALIGN LV_ATTRIBUTE_LARGE_CONST LV_ATTRIBUTE_IMG_BOLT uint8_t bolt_map[] = {
#if CONFIG_NICE_VIEW_WIDGET_INVERTED
0x00, 0x00, 0x00, 0x00, /*Color of index 0*/
0x00, 0x00, 0x00, 0xff, /*Color of index 1*/
0xff, 0xff, 0xff, 0xff, /*Color of index 2*/
0x00, 0x00, 0x00, 0x00, /*Color of index 3*/
#else
0x00, 0x00, 0x00, 0x00, /*Color of index 0*/
0xff, 0xff, 0xff, 0xff, /*Color of index 1*/
0x00, 0x00, 0x00, 0xff, /*Color of index 2*/
0x00, 0x00, 0x00, 0x00, /*Color of index 3*/
#endif

0x00, 0x14, 0x00, 0x00, 0x64, 0x00, 0x00, 0x64, 0x00, 0x01, 0xa4, 0x00, 0x01, 0xa4,
0x00, 0x06, 0xa4, 0x00, 0x06, 0xa4, 0x00, 0x1a, 0xa5, 0x54, 0x1a, 0xaa, 0xa4, 0x6a,
0xaa, 0x90, 0x55, 0x6a, 0x90, 0x00, 0x6a, 0x40, 0x00, 0x6a, 0x40, 0x00, 0x69, 0x00,
0x00, 0x69, 0x00, 0x00, 0x64, 0x00, 0x00, 0x64, 0x00, 0x00, 0x50, 0x00,
};

const lv_img_dsc_t bolt = {
.header.cf = LV_IMG_CF_INDEXED_2BIT,
.header.always_zero = 0,
.header.reserved = 0,
.header.w = 11,
.header.h = 18,
.data_size = 70,
.data = bolt_map,
};
128 changes: 128 additions & 0 deletions config/boards/shields/mlego_m66_rev4/widgets/peripheral_status.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
*
* Copyright (c) 2023 The ZMK Contributors
* SPDX-License-Identifier: MIT
*
*/

#include <zephyr/kernel.h>
#include <zephyr/bluetooth/services/bas.h>
#include <zephyr/random/rand32.h>

#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#include <zmk/display.h>
#include "peripheral_status.h"
#include <zmk/events/usb_conn_state_changed.h>
#include <zmk/event_manager.h>
#include <zmk/events/battery_state_changed.h>
#include <zmk/split/bluetooth/peripheral.h>
#include <zmk/events/split_peripheral_status_changed.h>
#include <zmk/usb.h>
#include <zmk/ble.h>

LV_IMG_DECLARE(balloon);
LV_IMG_DECLARE(mountain);

static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets);

struct peripheral_status_state {
bool connected;
};

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);

lv_draw_label_dsc_t label_dsc;
init_label_dsc(&label_dsc, LVGL_FOREGROUND, &lv_font_montserrat_16, LV_TEXT_ALIGN_RIGHT);
lv_draw_rect_dsc_t rect_black_dsc;
init_rect_dsc(&rect_black_dsc, LVGL_BACKGROUND);

// Fill background
lv_canvas_draw_rect(canvas, 0, 0, CANVAS_SIZE, CANVAS_SIZE, &rect_black_dsc);

// Draw battery
draw_battery(canvas, state);

// Draw output status
lv_canvas_draw_text(canvas, 0, 0, CANVAS_SIZE, &label_dsc,
state->connected ? LV_SYMBOL_WIFI : LV_SYMBOL_CLOSE);

// Rotate canvas
rotate_canvas(canvas, cbuf);
}

static void set_battery_status(struct zmk_widget_status *widget,
struct battery_status_state state) {
#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_top(widget->obj, widget->cbuf, &widget->state);
}

static void battery_status_update_cb(struct battery_status_state state) {
struct zmk_widget_status *widget;
SYS_SLIST_FOR_EACH_CONTAINER(&widgets, widget, node) { set_battery_status(widget, state); }
}

static struct battery_status_state battery_status_get_state(const zmk_event_t *eh) {
return (struct battery_status_state) {
.level = bt_bas_get_battery_level(),
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK)
.usb_present = zmk_usb_is_powered(),
#endif /* IS_ENABLED(CONFIG_USB_DEVICE_STACK) */
};
}

ZMK_DISPLAY_WIDGET_LISTENER(widget_battery_status, struct battery_status_state,
battery_status_update_cb, battery_status_get_state)

ZMK_SUBSCRIPTION(widget_battery_status, zmk_battery_state_changed);
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK)
ZMK_SUBSCRIPTION(widget_battery_status, zmk_usb_conn_state_changed);
#endif /* IS_ENABLED(CONFIG_USB_DEVICE_STACK) */

static struct peripheral_status_state get_state(const zmk_event_t *_eh) {
return (struct peripheral_status_state){.connected = zmk_split_bt_peripheral_is_connected()};
}

static void set_connection_status(struct zmk_widget_status *widget,
struct peripheral_status_state state) {
widget->state.connected = state.connected;

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

static void output_status_update_cb(struct peripheral_status_state state) {
struct zmk_widget_status *widget;
SYS_SLIST_FOR_EACH_CONTAINER(&widgets, widget, node) { set_connection_status(widget, state); }
}

ZMK_DISPLAY_WIDGET_LISTENER(widget_peripheral_status, struct peripheral_status_state,
output_status_update_cb, get_state)
ZMK_SUBSCRIPTION(widget_peripheral_status, zmk_split_peripheral_status_changed);

int zmk_widget_status_init(struct zmk_widget_status *widget, lv_obj_t *parent) {
widget->obj = lv_obj_create(parent);
lv_obj_set_size(widget->obj, 160, 68);
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, CANVAS_SIZE, CANVAS_SIZE, LV_IMG_CF_TRUE_COLOR);

lv_obj_t *art = lv_img_create(widget->obj);
bool random = sys_rand32_get() & 1;
lv_img_set_src(art, random ? &balloon : &mountain);
lv_obj_align(art, LV_ALIGN_TOP_LEFT, 0, 0);

sys_slist_append(&widgets, &widget->node);
widget_battery_status_init();
widget_peripheral_status_init();

return 0;
}

lv_obj_t *zmk_widget_status_obj(struct zmk_widget_status *widget) { return widget->obj; }
22 changes: 22 additions & 0 deletions config/boards/shields/mlego_m66_rev4/widgets/peripheral_status.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
*
* Copyright (c) 2023 The ZMK Contributors
* SPDX-License-Identifier: MIT
*
*/

#pragma once

#include <lvgl.h>
#include <zephyr/kernel.h>
#include "util.h"

struct zmk_widget_status {
sys_snode_t node;
lv_obj_t *obj;
lv_color_t cbuf[CANVAS_SIZE * CANVAS_SIZE];
struct status_state state;
};

int zmk_widget_status_init(struct zmk_widget_status *widget, lv_obj_t *parent);
lv_obj_t *zmk_widget_status_obj(struct zmk_widget_status *widget);
Loading

0 comments on commit 6ce37b8

Please sign in to comment.