Browse Source
* Add new API/status to track state of the peripheral connection to the central. * Add new peripheral status widget for displaying the current status of the connection to the central.xmkb
Peter Johanson
3 years ago
committed by
Pete Johanson
10 changed files with 172 additions and 18 deletions
@ -0,0 +1,19 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <lvgl.h> |
||||||
|
#include <kernel.h> |
||||||
|
|
||||||
|
struct zmk_widget_peripheral_status { |
||||||
|
sys_snode_t node; |
||||||
|
lv_obj_t *obj; |
||||||
|
}; |
||||||
|
|
||||||
|
int zmk_widget_peripheral_status_init(struct zmk_widget_peripheral_status *widget, |
||||||
|
lv_obj_t *parent); |
||||||
|
lv_obj_t *zmk_widget_peripheral_status_obj(struct zmk_widget_peripheral_status *widget); |
@ -0,0 +1,16 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <zephyr.h> |
||||||
|
#include <zmk/event_manager.h> |
||||||
|
|
||||||
|
struct zmk_split_peripheral_status_changed { |
||||||
|
bool connected; |
||||||
|
}; |
||||||
|
|
||||||
|
ZMK_EVENT_DECLARE(zmk_split_peripheral_status_changed); |
@ -0,0 +1,9 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
bool zmk_split_bt_peripheral_is_connected(void); |
@ -0,0 +1,60 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <kernel.h> |
||||||
|
#include <bluetooth/services/bas.h> |
||||||
|
|
||||||
|
#include <logging/log.h> |
||||||
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); |
||||||
|
|
||||||
|
#include <zmk/display.h> |
||||||
|
#include <zmk/display/widgets/peripheral_status.h> |
||||||
|
#include <zmk/event_manager.h> |
||||||
|
#include <zmk/split/bluetooth/peripheral.h> |
||||||
|
#include <zmk/events/split_peripheral_status_changed.h> |
||||||
|
|
||||||
|
static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets); |
||||||
|
|
||||||
|
struct peripheral_status_state { |
||||||
|
bool connected; |
||||||
|
}; |
||||||
|
|
||||||
|
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_status_symbol(lv_obj_t *label, struct peripheral_status_state state) { |
||||||
|
const char *text = |
||||||
|
state.connected ? (LV_SYMBOL_WIFI " " LV_SYMBOL_OK) : (LV_SYMBOL_WIFI " " LV_SYMBOL_CLOSE); |
||||||
|
|
||||||
|
LOG_DBG("connected? %s", state.connected ? "true" : "false"); |
||||||
|
lv_label_set_text(label, text); |
||||||
|
} |
||||||
|
|
||||||
|
static void output_status_update_cb(struct peripheral_status_state state) { |
||||||
|
struct zmk_widget_peripheral_status *widget; |
||||||
|
SYS_SLIST_FOR_EACH_CONTAINER(&widgets, widget, node) { set_status_symbol(widget->obj, 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_peripheral_status_init(struct zmk_widget_peripheral_status *widget, |
||||||
|
lv_obj_t *parent) { |
||||||
|
widget->obj = lv_label_create(parent, NULL); |
||||||
|
|
||||||
|
lv_obj_set_size(widget->obj, 40, 15); |
||||||
|
|
||||||
|
sys_slist_append(&widgets, &widget->node); |
||||||
|
|
||||||
|
widget_peripheral_status_init(); |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
lv_obj_t *zmk_widget_peripheral_status_obj(struct zmk_widget_peripheral_status *widget) { |
||||||
|
return widget->obj; |
||||||
|
} |
@ -0,0 +1,10 @@ |
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022 The ZMK Contributors |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <kernel.h> |
||||||
|
#include <zmk/events/split_peripheral_status_changed.h> |
||||||
|
|
||||||
|
ZMK_EVENT_IMPL(zmk_split_peripheral_status_changed); |
Loading…
Reference in new issue