From 3081f90bda93b17433daf7cbaa5306fee73366cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Tu=C4=8Dek?= Date: Fri, 11 Oct 2024 15:36:42 +0200 Subject: [PATCH] Make dongle leds functional. --- device/src/device_state.c | 2 ++ device/src/dongle_leds.c | 29 +++++++++++++++++++++++++++++ device/src/dongle_leds.h | 6 ++++++ device/src/main.c | 22 +++------------------- device/src/settings.c | 14 ++++++++++++++ device/src/settings.h | 9 +++++++++ right/src/stubs.h | 1 + 7 files changed, 64 insertions(+), 19 deletions(-) diff --git a/device/src/device_state.c b/device/src/device_state.c index e3efa5e7f..d4f14d5bc 100644 --- a/device/src/device_state.c +++ b/device/src/device_state.c @@ -8,6 +8,7 @@ #include "shared/slave_protocol.h" #include "legacy/event_scheduler.h" #include "legacy/power_mode.h" +#include "dongle_leds.h" static connection_type_t isConnected[ConnectionId_Count] = {}; @@ -75,6 +76,7 @@ void handleStateTransition(connection_id_t remoteId, bool connected) { if (remoteId == ConnectionId_Right && connected) { StateSync_ResetRightDongleLink(true); } + DongleLeds_Update(); break; default: break; diff --git a/device/src/dongle_leds.c b/device/src/dongle_leds.c index 3e000c020..10c9fa34b 100644 --- a/device/src/dongle_leds.c +++ b/device/src/dongle_leds.c @@ -1,13 +1,42 @@ #include "device.h" #if DEVICE_IS_UHK_DONGLE #include "dongle_leds.h" +#include "device_state.h" +#include "settings.h" const struct pwm_dt_spec red_pwm_led = PWM_DT_SPEC_GET(DT_ALIAS(red_pwm_led)); const struct pwm_dt_spec green_pwm_led = PWM_DT_SPEC_GET(DT_ALIAS(green_pwm_led)); const struct pwm_dt_spec blue_pwm_led = PWM_DT_SPEC_GET(DT_ALIAS(blue_pwm_led)); + +// There is also the following zero led. +// const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(DT_ALIAS(led0_green), gpios); +// gpio_pin_configure_dt(&led0, GPIO_OUTPUT); +// gpio_pin_set_dt(&led0, true); +// k_sleep(K_MSEC(1000)); +// gpio_pin_set_dt(&led0, false); + void set_dongle_led(const struct pwm_dt_spec *device, uint8_t percentage) { pwm_set_pulse_dt(device, percentage * device->period / 100); } + +void DongleLeds_Set(bool r, bool g, bool b) { + set_dongle_led(&red_pwm_led, r ? 100 : 0); + set_dongle_led(&green_pwm_led, g ? 100 : 0); + set_dongle_led(&blue_pwm_led, b ? 100 : 0); +} + +void DongleLeds_Update() { + if (DeviceState_IsConnected(ConnectionId_Right)) { + DongleLeds_Set(false, true, false); + return; + } + if (RightAddressIsSet) { + DongleLeds_Set(false, false, true); + return; + } + DongleLeds_Set(true, false, false); +} + #endif // DEVICE_IS_UHK_DONGLE diff --git a/device/src/dongle_leds.h b/device/src/dongle_leds.h index 3e93d0d18..3dd7ceda9 100644 --- a/device/src/dongle_leds.h +++ b/device/src/dongle_leds.h @@ -5,9 +5,15 @@ // Includes: #include + #include + #include + +// Typedefs: // Functions: + extern void DongleLeds_Set(bool r, bool g, bool b); + extern void DongleLeds_Update(); extern void set_dongle_led(const struct pwm_dt_spec *device, uint8_t percentage); // Variables: diff --git a/device/src/main.c b/device/src/main.c index a7e4dc6d4..20d765f60 100644 --- a/device/src/main.c +++ b/device/src/main.c @@ -34,9 +34,10 @@ #include "state_sync.h" #include "keyboard/charger.h" #include +#include "dongle_leds.h" #include "debug_eventloop_timing.h" -// #include -// #include "dongle_leds.h" +#include +#include "dongle_leds.h" k_tid_t Main_ThreadId = 0; @@ -98,23 +99,6 @@ int main(void) { Main_ThreadId = k_current_get(); printk("----------\n" DEVICE_NAME " started\n"); - // const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(DT_ALIAS(led0_green), gpios); - // gpio_pin_configure_dt(&led0, GPIO_OUTPUT); - // while (true) { - // gpio_pin_set_dt(&led0, true); - // k_sleep(K_MSEC(1000)); - // gpio_pin_set_dt(&led0, false); - // set_dongle_led(&red_pwm_led, 100); - // k_sleep(K_MSEC(1000)); - // set_dongle_led(&red_pwm_led, 0); - // set_dongle_led(&green_pwm_led, 100); - // k_sleep(K_MSEC(1000)); - // set_dongle_led(&green_pwm_led, 0); - // set_dongle_led(&blue_pwm_led, 100); - // k_sleep(K_MSEC(1000)); - // set_dongle_led(&blue_pwm_led, 0); - // } - if (DEVICE_IS_UHK80_RIGHT) { flash_area_open(FLASH_AREA_ID(hardware_config_partition), &hardwareConfigArea); flash_area_open(FLASH_AREA_ID(user_config_partition), &userConfigArea); diff --git a/device/src/settings.c b/device/src/settings.c index 4726febfa..d5a454e34 100644 --- a/device/src/settings.c +++ b/device/src/settings.c @@ -1,12 +1,18 @@ #include #include #include "bt_conn.h" +#include "dongle_leds.h" +#include "stubs.h" + +bool RightAddressIsSet = false; static int peerAddressSet(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg) { static char foo_val[BT_ADDR_SIZE]; read_cb(cb_arg, &foo_val, len); + bool rightAddressIsSet = false; + for (uint8_t i=0; ia.val[j]); } printk("\n"); + if (i == PeerIdRight) { + rightAddressIsSet = true; + } break; } } + if (rightAddressIsSet != RightAddressIsSet) { + RightAddressIsSet = rightAddressIsSet; + DongleLeds_Update(); + } + return 0; } diff --git a/device/src/settings.h b/device/src/settings.h index e74ac730b..f182b947c 100644 --- a/device/src/settings.h +++ b/device/src/settings.h @@ -1,8 +1,17 @@ #ifndef __SETTINGS_H__ #define __SETTINGS_H__ +// Includes + + #include + #include + // Functions: extern void InitSettings(void); +// Variables: + + extern bool RightAddressIsSet; + #endif // __SETTINGS_H__ diff --git a/right/src/stubs.h b/right/src/stubs.h index cbf55cc24..55132486e 100644 --- a/right/src/stubs.h +++ b/right/src/stubs.h @@ -41,6 +41,7 @@ ATTRS void Uart_Log(const char *fmt, ...) {}; ATTRS void Log(const char *fmt, ...) {}; ATTRS void LogBt(const char *fmt, ...) {}; + ATTRS void DongleLeds_Update() {}; #if DEVICE_HAS_OLED #define WIDGET_REFRESH(W) Widget_Refresh(W)