Skip to content

Instantly share code, notes, and snippets.

@petejohanson
Created September 3, 2021 03:37
Show Gist options
  • Save petejohanson/4e1e9fed08a674f6134f85048ca93a1a to your computer and use it in GitHub Desktop.
Save petejohanson/4e1e9fed08a674f6134f85048ca93a1a to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2020 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/output_status.h>
#include <zmk/event_manager.h>
#include <zmk/events/usb_conn_state_changed.h>
#include <zmk/events/ble_active_profile_changed.h>
#include <zmk/events/endpoint_selection_changed.h>
#include <zmk/usb.h>
#include <zmk/ble.h>
#include <zmk/endpoints.h>
static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets);
static lv_style_t label_style;
static bool style_initialized = false;
struct output_status_data {
enum zmk_endpoint selected_endpoint;
bool active_profile_connected;
bool active_profile_bonded;
uint8_t active_profile_index;
};
static void output_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_16);
lv_style_set_text_letter_space(&label_style, LV_STATE_DEFAULT, 1);
lv_style_set_text_line_space(&label_style, LV_STATE_DEFAULT, 1);
}
static struct output_status_data get_state() {
return (struct output_status_data){
.selected_endpoint = zmk_endpoints_selected(),
.active_profile_connected = zmk_ble_active_profile_is_connected(),
.active_profile_bonded = !zmk_ble_active_profile_is_open(),
.active_profile_index = zmk_ble_active_profile_index()
};;
}
static void set_status_symbol(lv_obj_t *label);
static void output_status_update_cb() {
struct zmk_widget_output_status *widget;
SYS_SLIST_FOR_EACH_CONTAINER(&widgets, widget, node) { set_status_symbol(widget->obj); }
}
ZMK_DISPLAY_WIDGET_LISTENER(widget_output_status, struct output_status_data, output_status_update_cb, get_state)
ZMK_SUBSCRIPTION(widget_output_status, zmk_endpoint_selection_changed);
#if defined(CONFIG_USB)
ZMK_SUBSCRIPTION(widget_output_status, zmk_usb_conn_state_changed);
#endif
#if defined(CONFIG_ZMK_BLE)
ZMK_SUBSCRIPTION(widget_output_status, zmk_ble_active_profile_changed);
#endif
static void set_status_symbol(lv_obj_t *label) {
char text[6] = {};
struct output_status_data local_state = widget_output_status_get_local_state();
switch (local_state.selected_endpoint) {
case ZMK_ENDPOINT_USB:
strcat(text, LV_SYMBOL_USB " ");
break;
case ZMK_ENDPOINT_BLE:
if (local_state.active_profile_bonded) {
if (local_state.active_profile_connected) {
sprintf(text, LV_SYMBOL_WIFI "%i " LV_SYMBOL_OK, local_state.active_profile_index);
} else {
sprintf(text, LV_SYMBOL_WIFI "%i " LV_SYMBOL_CLOSE, local_state.active_profile_index);
}
} else {
sprintf(text, LV_SYMBOL_WIFI "%i " LV_SYMBOL_SETTINGS, local_state.active_profile_index);
}
break;
}
lv_label_set_text(label, text);
}
int zmk_widget_output_status_init(struct zmk_widget_output_status *widget, lv_obj_t *parent) {
output_status_init();
widget_output_status_refresh_state();
widget->obj = lv_label_create(parent, NULL);
lv_obj_add_style(widget->obj, LV_LABEL_PART_MAIN, &label_style);
lv_obj_set_size(widget->obj, 40, 15);
set_status_symbol(widget->obj);
sys_slist_append(&widgets, &widget->node);
return 0;
}
lv_obj_t *zmk_widget_output_status_obj(struct zmk_widget_output_status *widget) {
return widget->obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment