From 74e202300c28c8e45c1816b614c4e9ade8d5b412 Mon Sep 17 00:00:00 2001 From: noteolvides Date: Thu, 19 Oct 2023 11:34:21 +0200 Subject: [PATCH] fix: fix get sides and wake up when connected Signed-off-by: noteolvides --- .../device/dygma/Defy_wireless.cpp | 39 ++++++++----------- .../device/dygma/defy_wireless/Focus.cpp | 14 +++++++ src/kaleidoscope/plugin/IdleLEDsDefy.cpp | 10 ++++- src/kaleidoscope/plugin/IdleLEDsDefy.h | 6 ++- 4 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/kaleidoscope/device/dygma/Defy_wireless.cpp b/src/kaleidoscope/device/dygma/Defy_wireless.cpp index 5a3a765..d52e0bc 100644 --- a/src/kaleidoscope/device/dygma/Defy_wireless.cpp +++ b/src/kaleidoscope/device/dygma/Defy_wireless.cpp @@ -35,12 +35,15 @@ #include "Defy_wireless.h" #include "LED-Palette-Theme-Defy.h" #include "Radio_manager.h" +#include "Status_leds.h" #include "Wire.h" // Arduino Wire wrapper for the NRF52 chips #include "defy_wireless/Focus.h" #include "nrf_gpio.h" Twi_master twi_master(TWI_MASTER_SCL_PIN, TWI_MASTER_SDA_PIN); +Status_leds status_leds(LED_GREEN_PIN, LED_RED_PIN); +#define NEURON_LED_BRIGHTNESS 2 namespace kaleidoscope @@ -156,9 +159,9 @@ auto checkBrightness = [](const Packet &) auto deviceLeft = keyScanner.leftHandDevice(); auto deviceRight = keyScanner.rightHandDevice(); - auto isEitherUnknown = deviceLeft == Communications_protocol::UNKNOWN && deviceRight == Communications_protocol::UNKNOWN; - auto isDefyLeftWired = deviceLeft == Communications_protocol::KEYSCANNER_DEFY_LEFT || deviceLeft == Communications_protocol::UNKNOWN; - auto isDefyRightWired = deviceRight == Communications_protocol::KEYSCANNER_DEFY_RIGHT || deviceRight == Communications_protocol::UNKNOWN; + volatile auto isEitherUnknown = deviceLeft == Communications_protocol::UNKNOWN && deviceRight == Communications_protocol::UNKNOWN; + volatile auto isDefyLeftWired = deviceLeft == Communications_protocol::KEYSCANNER_DEFY_LEFT || deviceLeft == Communications_protocol::UNKNOWN; + volatile auto isDefyRightWired = deviceRight == Communications_protocol::KEYSCANNER_DEFY_RIGHT || deviceRight == Communications_protocol::UNKNOWN; ColormapEffectDefy.updateBrigthness((isDefyLeftWired && isDefyRightWired) && !isEitherUnknown); }; @@ -170,38 +173,26 @@ void DefyHands::setup() Communications.callbacks.bind(CONNECTED, ( [](const Packet &p) { - if (ble_innited()) - { - if (p.header.device == RF_DEFY_LEFT) leftConnection[0] = BLE_DEFY_LEFT; - if (p.header.device == RF_DEFY_RIGHT) rightConnection[0] = BLE_DEFY_RIGHT; - if (p.header.device == KEYSCANNER_DEFY_LEFT) leftConnection[0] = BLE_DEFY_LEFT; - if (p.header.device == KEYSCANNER_DEFY_RIGHT) rightConnection[0] = BLE_DEFY_RIGHT; - return; - } + if (p.header.device == BLE_DEFY_RIGHT) rightConnection[0] = BLE_DEFY_RIGHT; + if (p.header.device == BLE_DEFY_LEFT) leftConnection[0] = BLE_DEFY_LEFT; if (p.header.device == RF_DEFY_LEFT) leftConnection[2] = RF_DEFY_LEFT; if (p.header.device == RF_DEFY_RIGHT) rightConnection[2] = RF_DEFY_RIGHT; - if (p.header.device == KEYSCANNER_DEFY_LEFT) leftConnection[1] = KEYSCANNER_DEFY_LEFT; - if (p.header.device == KEYSCANNER_DEFY_RIGHT) rightConnection[1] = KEYSCANNER_DEFY_RIGHT; + if (p.header.device == KEYSCANNER_DEFY_LEFT) leftConnection[1] = ble_innited() ? BLE_DEFY_LEFT: KEYSCANNER_DEFY_LEFT; + if (p.header.device == KEYSCANNER_DEFY_RIGHT) rightConnection[1] = ble_innited() ? BLE_DEFY_RIGHT: KEYSCANNER_DEFY_RIGHT; })); Communications.callbacks.bind(DISCONNECTED, ( [](const Packet &p) { - if (ble_innited()) - { - if (p.header.device == RF_DEFY_LEFT) leftConnection[0] = UNKNOWN; - if (p.header.device == RF_DEFY_RIGHT) rightConnection[0] = UNKNOWN; - if (p.header.device == KEYSCANNER_DEFY_LEFT) leftConnection[0] = UNKNOWN; - if (p.header.device == KEYSCANNER_DEFY_RIGHT) rightConnection[0] = UNKNOWN; - return; - } + if (p.header.device == BLE_DEFY_RIGHT) rightConnection[0] = UNKNOWN; + if (p.header.device == BLE_DEFY_LEFT) leftConnection[0] = UNKNOWN; if (p.header.device == RF_DEFY_LEFT) leftConnection[2] = UNKNOWN; if (p.header.device == RF_DEFY_RIGHT) rightConnection[2] = UNKNOWN; if (p.header.device == KEYSCANNER_DEFY_LEFT) leftConnection[1] = UNKNOWN; if (p.header.device == KEYSCANNER_DEFY_RIGHT) rightConnection[1] = UNKNOWN; })); - Communications.callbacks.bind(CONNECTED, checkBrightness); Communications.callbacks.bind(DISCONNECTED, checkBrightness); + Communications.callbacks.bind(CONNECTED, checkBrightness); Communications.callbacks.bind(CONNECTED, ([](const Packet &) { ::LEDControl.set_mode(::LEDControl.get_mode_index()); })); @@ -367,6 +358,7 @@ void DefyLEDDriver::syncLeds() leds_enabled_ = is_enabled; Communications_protocol::Packet p{}; p.header.command = Communications_protocol::SLEEP; + status_leds.stop_all(); Communications.sendPacket(p); } @@ -376,6 +368,7 @@ void DefyLEDDriver::syncLeds() Communications_protocol::Packet p{}; p.header.command = Communications_protocol::WAKE_UP; Communications.sendPacket(p); + status_leds.static_green(NEURON_LED_BRIGHTNESS); } if (isLEDChangedNeuron) @@ -728,6 +721,8 @@ void DefyNrf::setup() { // Check if we can live without this reset sides // DefyNrf::side::reset_sides(); + status_leds.init(); + status_leds.static_green(NEURON_LED_BRIGHTNESS); DefyHands::setup(); DefyFocus.init(); KeyScanner::setup(); diff --git a/src/kaleidoscope/device/dygma/defy_wireless/Focus.cpp b/src/kaleidoscope/device/dygma/defy_wireless/Focus.cpp index 85a6b60..ce62dd3 100644 --- a/src/kaleidoscope/device/dygma/defy_wireless/Focus.cpp +++ b/src/kaleidoscope/device/dygma/defy_wireless/Focus.cpp @@ -46,6 +46,20 @@ EventHandlerResult Focus::onFocusEvent(const char *command) { return EventHandlerResult::OK; } + if (strcmp(command + 9, "sideLeft") == 0) { + auto &keyScanner = Runtime.device().keyScanner(); + auto deviceLeft = keyScanner.leftHandDevice(); + ::Focus.send(static_cast(deviceLeft)); + return EventHandlerResult::OK; + } + + if (strcmp(command + 9, "sideRight") == 0) { + auto &keyScanner = Runtime.device().keyScanner(); + auto deviceRight = keyScanner.rightHandDevice(); + ::Focus.send(static_cast(deviceRight)); + return EventHandlerResult::OK; + } + if (strcmp(command + 9, "version") == 0) { NRF_LOG_DEBUG("read request: hardware.version"); diff --git a/src/kaleidoscope/plugin/IdleLEDsDefy.cpp b/src/kaleidoscope/plugin/IdleLEDsDefy.cpp index 8379577..1135a5d 100644 --- a/src/kaleidoscope/plugin/IdleLEDsDefy.cpp +++ b/src/kaleidoscope/plugin/IdleLEDsDefy.cpp @@ -17,6 +17,7 @@ */ #ifdef ARDUINO_ARCH_NRF52 +#include "Communications.h" #include "Defy_wireless.h" #include #include @@ -52,7 +53,6 @@ EventHandlerResult IdleLEDsDefy::beforeEachCycle() if ((isDefyLeftWired && isDefyRightWired) && !isEitherUnknown) { - start_time_wireless = Runtime.millisAtCycleStart(); if (::LEDControl.isEnabled() && Runtime.hasTimeExpired(start_time_wired, idle_time_limit.wired_)) { ::LEDControl.disable(); @@ -61,7 +61,6 @@ EventHandlerResult IdleLEDsDefy::beforeEachCycle() } else { - start_time_wired = Runtime.millisAtCycleStart(); if (::LEDControl.isEnabled() && Runtime.hasTimeExpired(start_time_wireless, idle_time_limit.wireless_)) { ::LEDControl.disable(); @@ -91,6 +90,13 @@ uint16_t PersistentIdleDefyLEDs::settings_base_; EventHandlerResult PersistentIdleDefyLEDs::onSetup() { + Communications.callbacks.bind(CONNECTED, ( + [this](const Packet &) + { + start_time_wired = Runtime.millisAtCycleStart(); + start_time_wireless = Runtime.millisAtCycleStart(); + ::LEDControl.enable(); + })); settings_base_ = ::EEPROMSettings.requestSlice(sizeof(IdleTime)); // If idleTime is max, assume that EEPROM is uninitialized, and store the diff --git a/src/kaleidoscope/plugin/IdleLEDsDefy.h b/src/kaleidoscope/plugin/IdleLEDsDefy.h index 72586f5..6e1542b 100644 --- a/src/kaleidoscope/plugin/IdleLEDsDefy.h +++ b/src/kaleidoscope/plugin/IdleLEDsDefy.h @@ -42,8 +42,10 @@ class IdleLEDsDefy : public kaleidoscope::Plugin { private: static bool idle_; - static uint32_t start_time_wired; - static uint32_t start_time_wireless; + + protected: + static uint32_t start_time_wired; + static uint32_t start_time_wireless; }; class PersistentIdleDefyLEDs : public IdleLEDsDefy