Skip to content

Commit

Permalink
feat(lighting): Kinesis lighting functionality
Browse files Browse the repository at this point in the history
moved peripheral updating from behaviour bnased to dedicated bluetooth services, added custom lighting effects to the adv360
  • Loading branch information
ReFil committed Sep 13, 2022
1 parent 9031dd9 commit 10b605e
Show file tree
Hide file tree
Showing 14 changed files with 680 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ config ZMK_RGB_UNDERGLOW_SPD_START

config ZMK_RGB_UNDERGLOW_EFF_START
int "RGB underglow start effect int value related to the effect enum list"
range 0 3
range 0 5
default 0

config ZMK_RGB_UNDERGLOW_ON_START
Expand Down
5 changes: 4 additions & 1 deletion app/include/dt-bindings/zmk/rgb.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#define RGB_EFF_CMD 11
#define RGB_EFR_CMD 12
#define RGB_EFS_CMD 13
#define RGB_COLOR_HSB_CMD 14
#define RGB_MEFS_CMD 14
#define RGB_COLOR_HSB_CMD 15

#define RGB_TOG RGB_TOG_CMD 0
#define RGB_ON RGB_ON_CMD 0
Expand All @@ -33,6 +34,8 @@
#define RGB_SPD RGB_SPD_CMD 0
#define RGB_EFF RGB_EFF_CMD 0
#define RGB_EFR RGB_EFR_CMD 0
#define RGB_EFS RGB_EFS_CMD
#define RFB_MEFS RGB_MEFS_CMD
#define RGB_COLOR_HSB_VAL(h, s, v) (((h) << 16) + ((s) << 8) + (v))
#define RGB_COLOR_HSB(h, s, v) RGB_COLOR_HSB_CMD##(RGB_COLOR_HSB_VAL(h, s, v))
#define RGB_COLOR_HSV RGB_COLOR_HSB
6 changes: 6 additions & 0 deletions app/include/zmk/backlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@

#pragma once

struct backlight_state {
uint8_t brightness;
bool on;
};

int zmk_backlight_on();
int zmk_backlight_off();
int zmk_backlight_toggle();
bool zmk_backlight_is_on();

int zmk_backlight_update_vals(struct backlight_state new_state);
int zmk_backlight_set_brt(uint8_t brightness);
uint8_t zmk_backlight_get_brt();
uint8_t zmk_backlight_calc_brt(int direction);
Expand Down
8 changes: 8 additions & 0 deletions app/include/zmk/rgb_underglow.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@

#pragma once

#include <zmk/led_indicators_types.h>

struct zmk_led_hsb {
uint16_t h;
uint8_t s;
uint8_t b;
};

struct zmk_periph_led {
uint8_t layer;
zmk_leds_flags_t indicators;
};

int zmk_rgb_underglow_toggle();
int zmk_rgb_underglow_get_state(bool *state);
int zmk_rgb_underglow_on();
int zmk_rgb_underglow_off();
int zmk_rgb_underglow_cycle_effect(int direction);
int zmk_rgb_underglow_calc_effect(int direction);
int zmk_rgb_underglow_select_effect(int effect);
int zmk_rgb_underglow_set_periph(struct zmk_periph_led periph);
struct zmk_led_hsb zmk_rgb_underglow_calc_hue(int direction);
struct zmk_led_hsb zmk_rgb_underglow_calc_sat(int direction);
struct zmk_led_hsb zmk_rgb_underglow_calc_brt(int direction);
Expand Down
5 changes: 4 additions & 1 deletion app/include/zmk/split/bluetooth/central.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
#include <zmk/behavior.h>

int zmk_split_bt_invoke_behavior(uint8_t source, struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event, bool state);
struct zmk_behavior_binding_event event, bool state);

int zmk_split_bt_update_led(struct zmk_periph_led *periph);
int zmk_split_bt_update_bl(struct backlight_state *periph);
10 changes: 10 additions & 0 deletions app/include/zmk/split/bluetooth/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,15 @@ struct zmk_split_run_behavior_payload {
char behavior_dev[ZMK_SPLIT_RUN_BEHAVIOR_DEV_LEN];
} __packed;

struct zmk_split_update_led_data {
uint8_t layer;
uint8_t indicators;
} __packed;

struct zmk_split_update_bl_data {
uint8_t brightness;
bool on;
} __packed;

int zmk_split_bt_position_pressed(uint8_t position);
int zmk_split_bt_position_released(uint8_t position);
2 changes: 2 additions & 0 deletions app/include/zmk/split/bluetooth/uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
#define ZMK_SPLIT_BT_SERVICE_UUID ZMK_BT_SPLIT_UUID(0x00000000)
#define ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ZMK_BT_SPLIT_UUID(0x00000001)
#define ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID ZMK_BT_SPLIT_UUID(0x00000002)
#define ZMK_SPLIT_BT_CHAR_UPDATE_LED_UUID ZMK_BT_SPLIT_UUID(0x00000003)
#define ZMK_SPLIT_BT_CHAR_UPDATE_BL_UUID ZMK_BT_SPLIT_UUID(0x00000004)
60 changes: 51 additions & 9 deletions app/src/backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
#include <zmk/event_manager.h>
#include <zmk/events/activity_state_changed.h>
#include <zmk/events/usb_conn_state_changed.h>
#include <zmk/events/split_peripheral_status_changed.h>
#include <zmk/ble.h>

#if ZMK_BLE_IS_CENTRAL
#include <zmk/split/bluetooth/central.h>
#endif

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

Expand All @@ -34,15 +40,27 @@ static const struct device *const backlight_dev = DEVICE_DT_GET(DT_CHOSEN(zmk_ba

#define BRT_MAX 100

struct backlight_state {
uint8_t brightness;
bool on;
};

static struct backlight_state state = {.brightness = CONFIG_ZMK_BACKLIGHT_BRT_START,
.on = IS_ENABLED(CONFIG_ZMK_BACKLIGHT_ON_START)};

#if ZMK_BLE_IS_CENTRAL
static struct k_work_delayable bl_update_work;

static void zmk_backlight_central_send() {
//#if IS_ENABLED(CONFIG_ZMK_BACKLIGHT_AUTO_OFF_IDLE)
// state.on = state.on || (!state.on && (zmk_activity_get_state() == ZMK_ACTIVITY_IDLE);
//#endif
int err = zmk_split_bt_update_bl(&state);
if (err) {
LOG_ERR("send failed (err %d)", err);
}
}
#endif

static int zmk_backlight_update() {
#if ZMK_BLE_IS_CENTRAL
zmk_backlight_central_send();
#endif
uint8_t brt = zmk_backlight_get_brt();
LOG_DBG("Update backlight brightness: %d%%", brt);

Expand Down Expand Up @@ -75,7 +93,7 @@ static void backlight_save_work_handler(struct k_work *work) {
settings_save_one("backlight/state", &state, sizeof(state));
}

static K_DELAYED_WORK_DEFINE(backlight_save_work, backlight_save_work_handler);
static struct k_work_delayable backlight_save_work;
#endif

static int zmk_backlight_init(const struct device *_arg) {
Expand All @@ -90,6 +108,10 @@ static int zmk_backlight_init(const struct device *_arg) {
if (rc != 0) {
LOG_ERR("Failed to load backlight settings: %d", rc);
}
k_work_init_delayable(&backlight_save_work, backlight_save_work_handler);
#endif
#if ZMK_BLE_IS_CENTRAL
k_work_init_delayable(&bl_update_work, zmk_backlight_central_send);
#endif
#if IS_ENABLED(CONFIG_ZMK_BACKLIGHT_AUTO_OFF_USB)
state.on = zmk_usb_is_powered();
Expand All @@ -104,8 +126,8 @@ static int zmk_backlight_update_and_save() {
}

#if IS_ENABLED(CONFIG_SETTINGS)
k_delayed_work_cancel(&backlight_save_work);
return k_delayed_work_submit(&backlight_save_work, K_MSEC(CONFIG_ZMK_SETTINGS_SAVE_DEBOUNCE));
int ret = k_work_reschedule(&backlight_save_work, K_MSEC(CONFIG_ZMK_SETTINGS_SAVE_DEBOUNCE));
return MIN(ret, 0);
#else
return 0;
#endif
Expand All @@ -124,6 +146,12 @@ int zmk_backlight_off() {

int zmk_backlight_toggle() { return state.on ? zmk_backlight_off() : zmk_backlight_on(); }

int zmk_backlight_update_vals(struct backlight_state new_state) {
state.on = new_state.on;
state.brightness = new_state.brightness;
return zmk_backlight_update_and_save();
}

bool zmk_backlight_is_on() { return state.on; }

int zmk_backlight_set_brt(uint8_t brightness) {
Expand Down Expand Up @@ -172,7 +200,17 @@ static int backlight_event_listener(const zmk_event_t *eh) {
return backlight_auto_state(&prev_state, zmk_usb_is_powered());
}
#endif

#if ZMK_BLE_IS_CENTRAL
if (as_zmk_split_peripheral_status_changed(eh)) {
LOG_DBG("event called");
const struct zmk_split_peripheral_status_changed *ev;
ev = as_zmk_split_peripheral_status_changed(eh);
if (ev->connected)
return k_work_schedule(&bl_update_work, K_MSEC(2500));
else
return k_work_cancel_delayable(&bl_update_work);
}
#endif
return -ENOTSUP;
}

Expand All @@ -188,4 +226,8 @@ ZMK_SUBSCRIPTION(backlight, zmk_activity_state_changed);
ZMK_SUBSCRIPTION(backlight, zmk_usb_conn_state_changed);
#endif

#if ZMK_BLE_IS_CENTRAL
ZMK_SUBSCRIPTION(backlight, zmk_split_peripheral_status_changed);
#endif

SYS_INIT(zmk_backlight_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
2 changes: 1 addition & 1 deletion app/src/behaviors/behavior_backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static const struct behavior_driver_api behavior_backlight_driver_api = {
on_keymap_binding_convert_central_state_dependent_params,
.binding_pressed = on_keymap_binding_pressed,
.binding_released = on_keymap_binding_released,
.locality = BEHAVIOR_LOCALITY_GLOBAL,
.locality = BEHAVIOR_LOCALITY_CENTRAL,
};

DEVICE_DT_INST_DEFINE(0, behavior_backlight_init, NULL, NULL, NULL, APPLICATION,
Expand Down
7 changes: 7 additions & 0 deletions app/src/behaviors/behavior_rgb_underglow.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)

static int behavior_rgb_underglow_init(const struct device *dev) { return 0; }
static uint8_t old_effect;

static int
on_keymap_binding_convert_central_state_dependent_params(struct zmk_behavior_binding *binding,
Expand Down Expand Up @@ -98,6 +99,7 @@ on_keymap_binding_convert_central_state_dependent_params(struct zmk_behavior_bin

static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
LOG_DBG("RGB behaviour triggered (%d/%d)", binding->param1, binding->param2);
switch (binding->param1) {
case RGB_TOG_CMD:
return zmk_rgb_underglow_toggle();
Expand All @@ -123,6 +125,9 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
return zmk_rgb_underglow_change_spd(-1);
case RGB_EFS_CMD:
return zmk_rgb_underglow_select_effect(binding->param2);
case RGB_MEFS_CMD:
old_effect = zmk_rgb_underglow_calc_effect(0);
return zmk_rgb_underglow_select_effect(binding->param2);
case RGB_EFF_CMD:
return zmk_rgb_underglow_cycle_effect(1);
case RGB_EFR_CMD:
Expand All @@ -138,6 +143,8 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,

static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
if (binding->param1 == RGB_MEFS_CMD)
return zmk_rgb_underglow_select_effect(old_effect);
return ZMK_BEHAVIOR_OPAQUE;
}

Expand Down
Loading

0 comments on commit 10b605e

Please sign in to comment.