Compare commits
2 Commits
968fb626bb
...
23e23f63a7
| Author | SHA1 | Date | |
|---|---|---|---|
| 23e23f63a7 | |||
| ff8f0d81d7 |
@@ -10,6 +10,7 @@ include(nanopb)
|
|||||||
zephyr_include_directories(
|
zephyr_include_directories(
|
||||||
inc
|
inc
|
||||||
inc/events
|
inc/events
|
||||||
|
src
|
||||||
)
|
)
|
||||||
add_subdirectory(drivers)
|
add_subdirectory(drivers)
|
||||||
|
|
||||||
@@ -29,6 +30,9 @@ target_sources(app PRIVATE
|
|||||||
src/encoder_module.c
|
src/encoder_module.c
|
||||||
src/hid_flowctrl_module.c
|
src/hid_flowctrl_module.c
|
||||||
src/keyboard_core_module.c
|
src/keyboard_core_module.c
|
||||||
|
src/led_effect/led_effect_registry.c
|
||||||
|
src/led_effect/effects/led_effect_key_fade.c
|
||||||
|
src/led_strip_module.c
|
||||||
src/ui/ui_main.c
|
src/ui/ui_main.c
|
||||||
src/cdc_wrapper_module.c
|
src/cdc_wrapper_module.c
|
||||||
src/protocol_module.c
|
src/protocol_module.c
|
||||||
@@ -45,6 +49,7 @@ target_sources(app PRIVATE
|
|||||||
src/events/hid_report_sent_event.c
|
src/events/hid_report_sent_event.c
|
||||||
src/events/hid_transport_state_event.c
|
src/events/hid_transport_state_event.c
|
||||||
src/events/hid_tx_report_event.c
|
src/events/hid_tx_report_event.c
|
||||||
|
src/events/led_strip_en_event.c
|
||||||
src/events/key_function_event.c
|
src/events/key_function_event.c
|
||||||
src/mode_switch_module.c
|
src/mode_switch_module.c
|
||||||
src/events/keyboard_hid_report_event.c
|
src/events/keyboard_hid_report_event.c
|
||||||
|
|||||||
@@ -46,6 +46,21 @@
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
spi1_ws2812_default: spi1_ws2812_default {
|
||||||
|
group1 {
|
||||||
|
psels = <NRF_PSEL(SPIM_SCK, 1, 4)>,
|
||||||
|
<NRF_PSEL(SPIM_MOSI, 0, 20)>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
spi1_ws2812_sleep: spi1_ws2812_sleep {
|
||||||
|
group1 {
|
||||||
|
psels = <NRF_PSEL(SPIM_SCK, 1, 4)>,
|
||||||
|
<NRF_PSEL(SPIM_MOSI, 0, 20)>;
|
||||||
|
low-power-enable;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
pwm0_default: pwm0_default {
|
pwm0_default: pwm0_default {
|
||||||
group1 {
|
group1 {
|
||||||
psels = <NRF_PSEL(PWM_OUT0, 1, 11)>;
|
psels = <NRF_PSEL(PWM_OUT0, 1, 11)>;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <nordic/nrf52840_qiaa.dtsi>
|
#include <nordic/nrf52840_qiaa.dtsi>
|
||||||
#include "mini_keyboard-pinctrl.dtsi"
|
#include "mini_keyboard-pinctrl.dtsi"
|
||||||
#include <zephyr/dt-bindings/adc/adc.h>
|
#include <zephyr/dt-bindings/adc/adc.h>
|
||||||
|
#include <zephyr/dt-bindings/led/led.h>
|
||||||
#include <zephyr/dt-bindings/mipi_dbi/mipi_dbi.h>
|
#include <zephyr/dt-bindings/mipi_dbi/mipi_dbi.h>
|
||||||
#include <zephyr/dt-bindings/pwm/pwm.h>
|
#include <zephyr/dt-bindings/pwm/pwm.h>
|
||||||
|
|
||||||
@@ -14,10 +15,12 @@
|
|||||||
zephyr,flash = &flash0;
|
zephyr,flash = &flash0;
|
||||||
zephyr,code-partition = &slot0_partition;
|
zephyr,code-partition = &slot0_partition;
|
||||||
zephyr,display = &screen_lcd;
|
zephyr,display = &screen_lcd;
|
||||||
|
zephyr,led-strip = &led_strip;
|
||||||
};
|
};
|
||||||
|
|
||||||
aliases {
|
aliases {
|
||||||
led0 = &myled0;
|
led0 = &myled0;
|
||||||
|
led-strip = &led_strip;
|
||||||
qdec0 = &qdec;
|
qdec0 = &qdec;
|
||||||
backlight = &backlight;
|
backlight = &backlight;
|
||||||
};
|
};
|
||||||
@@ -206,6 +209,28 @@
|
|||||||
cs-gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
|
cs-gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
&spi1 {
|
||||||
|
status = "okay";
|
||||||
|
pinctrl-0 = <&spi1_ws2812_default>;
|
||||||
|
pinctrl-1 = <&spi1_ws2812_sleep>;
|
||||||
|
pinctrl-names = "default", "sleep";
|
||||||
|
|
||||||
|
led_strip: ws2812@0 {
|
||||||
|
compatible = "worldsemi,ws2812-spi";
|
||||||
|
reg = <0>;
|
||||||
|
spi-max-frequency = <6400000>;
|
||||||
|
chain-length = <17>;
|
||||||
|
color-mapping = <LED_COLOR_ID_GREEN
|
||||||
|
LED_COLOR_ID_RED
|
||||||
|
LED_COLOR_ID_BLUE>;
|
||||||
|
spi-one-frame = <0x70>;
|
||||||
|
spi-zero-frame = <0x40>;
|
||||||
|
bits-per-symbol = <8>;
|
||||||
|
reset-delay = <8>;
|
||||||
|
supply-gpios = <&gpio0 13 GPIO_ACTIVE_HIGH>;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
&pwm0 {
|
&pwm0 {
|
||||||
status = "okay";
|
status = "okay";
|
||||||
pinctrl-0 = <&pwm0_default>;
|
pinctrl-0 = <&pwm0_default>;
|
||||||
|
|||||||
24
inc/events/led_strip_en_event.h
Normal file
24
inc/events/led_strip_en_event.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#ifndef BLINKY_LED_STRIP_EN_EVENT_H_
|
||||||
|
#define BLINKY_LED_STRIP_EN_EVENT_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct led_strip_en_event {
|
||||||
|
struct app_event_header header;
|
||||||
|
bool enabled;
|
||||||
|
};
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DECLARE(led_strip_en_event);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_LED_STRIP_EN_EVENT_H_ */
|
||||||
24
inc/theme_color.h
Normal file
24
inc/theme_color.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#ifndef BLINKY_THEME_COLOR_H_
|
||||||
|
#define BLINKY_THEME_COLOR_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define BLINKY_THEME_DEFAULT_R 0x4CU
|
||||||
|
#define BLINKY_THEME_DEFAULT_G 0x9EU
|
||||||
|
#define BLINKY_THEME_DEFAULT_B 0xF5U
|
||||||
|
|
||||||
|
struct theme_rgb {
|
||||||
|
uint8_t r;
|
||||||
|
uint8_t g;
|
||||||
|
uint8_t b;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_THEME_COLOR_H_ */
|
||||||
2
prj.conf
2
prj.conf
@@ -28,6 +28,8 @@ CONFIG_HEAP_MEM_POOL_SIZE=4096
|
|||||||
CONFIG_LOG=y
|
CONFIG_LOG=y
|
||||||
CONFIG_ASSERT=y
|
CONFIG_ASSERT=y
|
||||||
CONFIG_APP_EVENT_MANAGER_MAX_EVENT_CNT=64
|
CONFIG_APP_EVENT_MANAGER_MAX_EVENT_CNT=64
|
||||||
|
CONFIG_LED_STRIP=y
|
||||||
|
CONFIG_WS2812_STRIP_SPI=y
|
||||||
|
|
||||||
# USB HID next stack
|
# USB HID next stack
|
||||||
CONFIG_USB_DEVICE_STACK_NEXT=y
|
CONFIG_USB_DEVICE_STACK_NEXT=y
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include "bat_state_event.h"
|
#include "bat_state_event.h"
|
||||||
#include "hid_led_event.h"
|
#include "hid_led_event.h"
|
||||||
#include "mode_switch_event.h"
|
#include "mode_switch_event.h"
|
||||||
|
#include "theme_color.h"
|
||||||
#include "ui/ui_main.h"
|
#include "ui/ui_main.h"
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
@@ -30,7 +31,9 @@ static const struct device *const backlight_dev =
|
|||||||
DEVICE_DT_GET(DT_PARENT(DT_ALIAS(backlight)));
|
DEVICE_DT_GET(DT_PARENT(DT_ALIAS(backlight)));
|
||||||
static const uint32_t backlight_idx = DT_NODE_CHILD_IDX(DT_ALIAS(backlight));
|
static const uint32_t backlight_idx = DT_NODE_CHILD_IDX(DT_ALIAS(backlight));
|
||||||
static struct ui_main_model ui_model = {
|
static struct ui_main_model ui_model = {
|
||||||
.theme_color = LV_COLOR_MAKE(0x4C, 0x9E, 0xF5),
|
.theme_color = LV_COLOR_MAKE(BLINKY_THEME_DEFAULT_R,
|
||||||
|
BLINKY_THEME_DEFAULT_G,
|
||||||
|
BLINKY_THEME_DEFAULT_B),
|
||||||
.inactive_border_color = LV_COLOR_MAKE(0x3A, 0x44, 0x52),
|
.inactive_border_color = LV_COLOR_MAKE(0x3A, 0x44, 0x52),
|
||||||
.mode = MODE_SWITCH_BLE,
|
.mode = MODE_SWITCH_BLE,
|
||||||
};
|
};
|
||||||
|
|||||||
27
src/events/led_strip_en_event.c
Normal file
27
src/events/led_strip_en_event.c
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include "led_strip_en_event.h"
|
||||||
|
|
||||||
|
static void log_led_strip_en_event(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct led_strip_en_event *event = cast_led_strip_en_event(aeh);
|
||||||
|
|
||||||
|
APP_EVENT_MANAGER_LOG(aeh, "enabled:%u", event->enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void profile_led_strip_en_event(struct log_event_buf *buf,
|
||||||
|
const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
const struct led_strip_en_event *event = cast_led_strip_en_event(aeh);
|
||||||
|
|
||||||
|
nrf_profiler_log_encode_uint8(buf, event->enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_INFO_DEFINE(led_strip_en_event,
|
||||||
|
ENCODE(NRF_PROFILER_ARG_U8),
|
||||||
|
ENCODE("enabled"),
|
||||||
|
profile_led_strip_en_event);
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DEFINE(led_strip_en_event,
|
||||||
|
log_led_strip_en_event,
|
||||||
|
&led_strip_en_event_info,
|
||||||
|
APP_EVENT_FLAGS_CREATE(
|
||||||
|
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||||
155
src/led_effect/effects/led_effect_key_fade.c
Normal file
155
src/led_effect/effects/led_effect_key_fade.c
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <zephyr/sys/util.h>
|
||||||
|
|
||||||
|
#include "led_effect/led_effect.h"
|
||||||
|
|
||||||
|
#define KEY_FADE_LEVEL_MAX 255U
|
||||||
|
#define KEY_FADE_STEP_PER_TICK 12U
|
||||||
|
|
||||||
|
struct led_effect_key_fade_state {
|
||||||
|
const struct led_key_map *key_map;
|
||||||
|
size_t key_map_len;
|
||||||
|
size_t pixel_count;
|
||||||
|
uint8_t default_brightness;
|
||||||
|
uint8_t level[17];
|
||||||
|
struct theme_rgb theme;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct led_effect_key_fade_state key_fade_state;
|
||||||
|
|
||||||
|
static const struct led_key_map *find_key(const struct led_effect *effect,
|
||||||
|
uint16_t key_id)
|
||||||
|
{
|
||||||
|
const struct led_effect_key_fade_state *state = effect->state;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < state->key_map_len; i++) {
|
||||||
|
if (state->key_map[i].key_id == key_id) {
|
||||||
|
return &state->key_map[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int led_effect_key_fade_init(struct led_effect *effect,
|
||||||
|
const struct led_effect_config *cfg)
|
||||||
|
{
|
||||||
|
struct led_effect_key_fade_state *state = effect->state;
|
||||||
|
|
||||||
|
if ((effect == NULL) || (cfg == NULL) || (cfg->key_map == NULL) ||
|
||||||
|
(cfg->pixel_count > ARRAY_SIZE(state->level))) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(state, 0, sizeof(*state));
|
||||||
|
state->key_map = cfg->key_map;
|
||||||
|
state->key_map_len = cfg->key_map_len;
|
||||||
|
state->pixel_count = cfg->pixel_count;
|
||||||
|
state->default_brightness = cfg->default_brightness;
|
||||||
|
state->theme.r = BLINKY_THEME_DEFAULT_R;
|
||||||
|
state->theme.g = BLINKY_THEME_DEFAULT_G;
|
||||||
|
state->theme.b = BLINKY_THEME_DEFAULT_B;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void led_effect_key_fade_reset(struct led_effect *effect)
|
||||||
|
{
|
||||||
|
struct led_effect_key_fade_state *state = effect->state;
|
||||||
|
|
||||||
|
memset(state->level, 0, sizeof(state->level));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void led_effect_key_fade_set_theme(struct led_effect *effect,
|
||||||
|
const struct theme_rgb *theme)
|
||||||
|
{
|
||||||
|
struct led_effect_key_fade_state *state = effect->state;
|
||||||
|
|
||||||
|
if (theme == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
state->theme = *theme;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void led_effect_key_fade_on_key_press(struct led_effect *effect,
|
||||||
|
uint16_t key_id)
|
||||||
|
{
|
||||||
|
struct led_effect_key_fade_state *state = effect->state;
|
||||||
|
const struct led_key_map *map = find_key(effect, key_id);
|
||||||
|
|
||||||
|
if ((map == NULL) || (map->led_idx >= state->pixel_count)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
state->level[map->led_idx] =
|
||||||
|
(state->default_brightness == 0U) ? KEY_FADE_LEVEL_MAX :
|
||||||
|
state->default_brightness;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool led_effect_key_fade_tick(struct led_effect *effect, uint32_t dt_ms,
|
||||||
|
struct led_rgb *pixels, size_t pixel_count)
|
||||||
|
{
|
||||||
|
struct led_effect_key_fade_state *state = effect->state;
|
||||||
|
bool active = false;
|
||||||
|
bool visible = false;
|
||||||
|
uint8_t decay;
|
||||||
|
|
||||||
|
ARG_UNUSED(dt_ms);
|
||||||
|
|
||||||
|
if ((pixels == NULL) || (pixel_count < state->pixel_count)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(pixels, 0, sizeof(*pixels) * pixel_count);
|
||||||
|
decay = KEY_FADE_STEP_PER_TICK;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < state->pixel_count; i++) {
|
||||||
|
uint8_t level = state->level[i];
|
||||||
|
|
||||||
|
if (level == 0U) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
visible = true;
|
||||||
|
pixels[i].r = (uint8_t)(((uint16_t)state->theme.r * level) / 255U);
|
||||||
|
pixels[i].g = (uint8_t)(((uint16_t)state->theme.g * level) / 255U);
|
||||||
|
pixels[i].b = (uint8_t)(((uint16_t)state->theme.b * level) / 255U);
|
||||||
|
|
||||||
|
state->level[i] = (level > decay) ? (uint8_t)(level - decay) : 0U;
|
||||||
|
active = active || (state->level[i] > 0U);
|
||||||
|
}
|
||||||
|
|
||||||
|
return visible || active;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool led_effect_key_fade_is_active(const struct led_effect *effect)
|
||||||
|
{
|
||||||
|
const struct led_effect_key_fade_state *state = effect->state;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < state->pixel_count; i++) {
|
||||||
|
if (state->level[i] > 0U) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct led_effect_ops key_fade_ops = {
|
||||||
|
.init = led_effect_key_fade_init,
|
||||||
|
.reset = led_effect_key_fade_reset,
|
||||||
|
.set_theme = led_effect_key_fade_set_theme,
|
||||||
|
.on_key_press = led_effect_key_fade_on_key_press,
|
||||||
|
.tick = led_effect_key_fade_tick,
|
||||||
|
.is_active = led_effect_key_fade_is_active,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct led_effect led_effect_key_fade = {
|
||||||
|
.ops = &key_fade_ops,
|
||||||
|
.state = &key_fade_state,
|
||||||
|
};
|
||||||
50
src/led_effect/led_effect.h
Normal file
50
src/led_effect/led_effect.h
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#ifndef BLINKY_LED_EFFECT_H_
|
||||||
|
#define BLINKY_LED_EFFECT_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <zephyr/drivers/led_strip.h>
|
||||||
|
|
||||||
|
#include "led_effect_types.h"
|
||||||
|
#include "theme_color.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct led_effect;
|
||||||
|
|
||||||
|
struct led_effect_config {
|
||||||
|
const struct led_key_map *key_map;
|
||||||
|
size_t key_map_len;
|
||||||
|
size_t pixel_count;
|
||||||
|
uint8_t default_brightness;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct led_effect_ops {
|
||||||
|
int (*init)(struct led_effect *effect,
|
||||||
|
const struct led_effect_config *cfg);
|
||||||
|
void (*reset)(struct led_effect *effect);
|
||||||
|
void (*set_theme)(struct led_effect *effect,
|
||||||
|
const struct theme_rgb *theme);
|
||||||
|
void (*on_key_press)(struct led_effect *effect, uint16_t key_id);
|
||||||
|
bool (*tick)(struct led_effect *effect, uint32_t dt_ms,
|
||||||
|
struct led_rgb *pixels, size_t pixel_count);
|
||||||
|
bool (*is_active)(const struct led_effect *effect);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct led_effect {
|
||||||
|
const struct led_effect_ops *ops;
|
||||||
|
void *state;
|
||||||
|
};
|
||||||
|
|
||||||
|
const struct led_effect *led_effect_get(enum led_effect_id id);
|
||||||
|
struct led_effect *led_effect_get_mutable(enum led_effect_id id);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_LED_EFFECT_H_ */
|
||||||
27
src/led_effect/led_effect_registry.c
Normal file
27
src/led_effect/led_effect_registry.c
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "led_effect.h"
|
||||||
|
|
||||||
|
extern struct led_effect led_effect_key_fade;
|
||||||
|
|
||||||
|
static struct led_effect *const effect_registry[LED_EFFECT_ID_COUNT] = {
|
||||||
|
[LED_EFFECT_ID_KEY_FADE] = &led_effect_key_fade,
|
||||||
|
};
|
||||||
|
|
||||||
|
const struct led_effect *led_effect_get(enum led_effect_id id)
|
||||||
|
{
|
||||||
|
if (id >= LED_EFFECT_ID_COUNT) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return effect_registry[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
struct led_effect *led_effect_get_mutable(enum led_effect_id id)
|
||||||
|
{
|
||||||
|
if (id >= LED_EFFECT_ID_COUNT) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return effect_registry[id];
|
||||||
|
}
|
||||||
24
src/led_effect/led_effect_types.h
Normal file
24
src/led_effect/led_effect_types.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#ifndef BLINKY_LED_EFFECT_TYPES_H_
|
||||||
|
#define BLINKY_LED_EFFECT_TYPES_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct led_key_map {
|
||||||
|
uint16_t key_id;
|
||||||
|
uint8_t led_idx;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum led_effect_id {
|
||||||
|
LED_EFFECT_ID_KEY_FADE = 0,
|
||||||
|
LED_EFFECT_ID_COUNT,
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_LED_EFFECT_TYPES_H_ */
|
||||||
347
src/led_strip_module.c
Normal file
347
src/led_strip_module.c
Normal file
@@ -0,0 +1,347 @@
|
|||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
|
||||||
|
#define MODULE led_strip_module
|
||||||
|
#include <caf/events/button_event.h>
|
||||||
|
#include <caf/events/module_state_event.h>
|
||||||
|
#include <caf/events/power_event.h>
|
||||||
|
#include <caf/key_id.h>
|
||||||
|
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
#include <zephyr/drivers/gpio.h>
|
||||||
|
#include <zephyr/drivers/led_strip.h>
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include "led_effect/led_effect.h"
|
||||||
|
#include "led_strip_en_event.h"
|
||||||
|
#include "theme_color.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
#define LED_STRIP_NODE DT_CHOSEN(zephyr_led_strip)
|
||||||
|
#define LED_STRIP_NUM_PIXELS DT_PROP(LED_STRIP_NODE, chain_length)
|
||||||
|
#define LED_STRIP_TICK_INTERVAL K_MSEC(20)
|
||||||
|
|
||||||
|
BUILD_ASSERT(DT_NODE_HAS_STATUS(LED_STRIP_NODE, okay),
|
||||||
|
"Missing zephyr,led-strip chosen node");
|
||||||
|
BUILD_ASSERT(DT_NODE_HAS_PROP(LED_STRIP_NODE, supply_gpios),
|
||||||
|
"Missing supply-gpios on zephyr,led-strip node");
|
||||||
|
BUILD_ASSERT(LED_STRIP_NUM_PIXELS == 17U,
|
||||||
|
"LED strip key map expects 17 pixels");
|
||||||
|
|
||||||
|
static const struct device *const strip = DEVICE_DT_GET(LED_STRIP_NODE);
|
||||||
|
static const struct gpio_dt_spec strip_en =
|
||||||
|
GPIO_DT_SPEC_GET(LED_STRIP_NODE, supply_gpios);
|
||||||
|
|
||||||
|
static const struct led_key_map led_key_map[] = {
|
||||||
|
{ KEY_ID(0, 1), 0U },
|
||||||
|
{ KEY_ID(1, 1), 1U },
|
||||||
|
{ KEY_ID(2, 1), 2U },
|
||||||
|
{ KEY_ID(3, 1), 3U },
|
||||||
|
{ KEY_ID(0, 2), 4U },
|
||||||
|
{ KEY_ID(1, 2), 5U },
|
||||||
|
{ KEY_ID(2, 2), 6U },
|
||||||
|
{ KEY_ID(0, 3), 7U },
|
||||||
|
{ KEY_ID(1, 3), 8U },
|
||||||
|
{ KEY_ID(2, 3), 9U },
|
||||||
|
{ KEY_ID(3, 3), 10U },
|
||||||
|
{ KEY_ID(0, 4), 11U },
|
||||||
|
{ KEY_ID(1, 4), 12U },
|
||||||
|
{ KEY_ID(2, 4), 13U },
|
||||||
|
{ KEY_ID(0, 5), 14U },
|
||||||
|
{ KEY_ID(1, 5), 15U },
|
||||||
|
{ KEY_ID(3, 5), 16U },
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct led_effect_config effect_cfg = {
|
||||||
|
.key_map = led_key_map,
|
||||||
|
.key_map_len = ARRAY_SIZE(led_key_map),
|
||||||
|
.pixel_count = LED_STRIP_NUM_PIXELS,
|
||||||
|
.default_brightness = 0xFFU,
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct led_rgb pixels[LED_STRIP_NUM_PIXELS];
|
||||||
|
static struct k_work_delayable effect_work;
|
||||||
|
static struct led_effect *effect;
|
||||||
|
static struct theme_rgb current_theme = {
|
||||||
|
.r = BLINKY_THEME_DEFAULT_R,
|
||||||
|
.g = BLINKY_THEME_DEFAULT_G,
|
||||||
|
.b = BLINKY_THEME_DEFAULT_B,
|
||||||
|
};
|
||||||
|
static bool initialized;
|
||||||
|
static bool running;
|
||||||
|
static bool enabled = true;
|
||||||
|
|
||||||
|
static void clear_pixels(void)
|
||||||
|
{
|
||||||
|
memset(pixels, 0, sizeof(pixels));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int submit_frame(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = led_strip_update_rgb(strip, pixels, ARRAY_SIZE(pixels));
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("led_strip_update_rgb failed (%d)", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int set_strip_power(bool on)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = gpio_pin_set_dt(&strip_en, on ? 1 : 0);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("LED strip EN set failed (%d)", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int refresh_idle_frame(void)
|
||||||
|
{
|
||||||
|
clear_pixels();
|
||||||
|
return submit_frame();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void stop_effect_work(void)
|
||||||
|
{
|
||||||
|
k_work_cancel_delayable(&effect_work);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void schedule_effect_tick(k_timeout_t delay)
|
||||||
|
{
|
||||||
|
k_work_reschedule(&effect_work, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int enable_strip_output(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = set_strip_power(true);
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
return refresh_idle_frame();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void disable_strip_output(void)
|
||||||
|
{
|
||||||
|
stop_effect_work();
|
||||||
|
clear_pixels();
|
||||||
|
(void)submit_frame();
|
||||||
|
(void)set_strip_power(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void effect_work_handler(struct k_work *work)
|
||||||
|
{
|
||||||
|
bool keep_running;
|
||||||
|
|
||||||
|
ARG_UNUSED(work);
|
||||||
|
|
||||||
|
if (!running || !enabled || (effect == NULL)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
keep_running = effect->ops->tick(effect, k_ticks_to_ms_floor32(
|
||||||
|
LED_STRIP_TICK_INTERVAL.ticks),
|
||||||
|
pixels, ARRAY_SIZE(pixels));
|
||||||
|
(void)submit_frame();
|
||||||
|
|
||||||
|
if (keep_running) {
|
||||||
|
schedule_effect_tick(LED_STRIP_TICK_INTERVAL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_init(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (!device_is_ready(strip)) {
|
||||||
|
LOG_ERR("LED strip device %s not ready", strip->name);
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!gpio_is_ready_dt(&strip_en)) {
|
||||||
|
LOG_ERR("LED strip supply GPIO not ready");
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = gpio_pin_configure_dt(&strip_en, GPIO_OUTPUT_INACTIVE);
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("Failed to configure LED strip supply GPIO (%d)", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
k_work_init_delayable(&effect_work, effect_work_handler);
|
||||||
|
clear_pixels();
|
||||||
|
|
||||||
|
effect = led_effect_get_mutable(LED_EFFECT_ID_KEY_FADE);
|
||||||
|
if ((effect == NULL) || (effect->ops == NULL)) {
|
||||||
|
LOG_ERR("LED effect not available");
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = effect->ops->init(effect, &effect_cfg);
|
||||||
|
if (err) {
|
||||||
|
LOG_ERR("LED effect init failed (%d)", err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
effect->ops->set_theme(effect, ¤t_theme);
|
||||||
|
|
||||||
|
LOG_INF("LED strip ready len:%u", (uint32_t)led_strip_length(strip));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int module_start(void)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (running) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
|
||||||
|
if (!enabled) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = enable_strip_output();
|
||||||
|
if (err) {
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void module_pause(void)
|
||||||
|
{
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((effect != NULL) && (effect->ops != NULL)) {
|
||||||
|
effect->ops->reset(effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
disable_strip_output();
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_button_event(const struct button_event *event)
|
||||||
|
{
|
||||||
|
if (!running || !enabled || (effect == NULL) || !event->pressed) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
effect->ops->on_key_press(effect, event->key_id);
|
||||||
|
schedule_effect_tick(K_NO_WAIT);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_led_strip_en_event(const struct led_strip_en_event *event)
|
||||||
|
{
|
||||||
|
enabled = event->enabled;
|
||||||
|
|
||||||
|
if (!running) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (enabled) {
|
||||||
|
int err = enable_strip_output();
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ((effect != NULL) && (effect->ops != NULL)) {
|
||||||
|
effect->ops->reset(effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
disable_strip_output();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
if (is_button_event(aeh)) {
|
||||||
|
return handle_button_event(cast_button_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_led_strip_en_event(aeh)) {
|
||||||
|
return handle_led_strip_en_event(cast_led_strip_en_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_module_state_event(aeh)) {
|
||||||
|
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||||
|
|
||||||
|
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (!initialized) {
|
||||||
|
err = module_init();
|
||||||
|
if (err) {
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = module_start();
|
||||||
|
if (err) {
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
} else {
|
||||||
|
module_set_state(MODULE_STATE_READY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_power_down_event(aeh)) {
|
||||||
|
if (initialized) {
|
||||||
|
module_pause();
|
||||||
|
module_set_state(MODULE_STATE_STANDBY);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_wake_up_event(aeh)) {
|
||||||
|
if (initialized) {
|
||||||
|
int err = module_start();
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
module_set_state(MODULE_STATE_ERROR);
|
||||||
|
} else {
|
||||||
|
module_set_state(MODULE_STATE_READY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, button_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, led_strip_en_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
Reference in New Issue
Block a user