Skip to content

Commit

Permalink
fix: fix get sides and wake up when connected
Browse files Browse the repository at this point in the history
Signed-off-by: noteolvides <[email protected]>
  • Loading branch information
Noteolvides committed Oct 19, 2023
1 parent 3fdd8eb commit 74e2023
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
39 changes: 17 additions & 22 deletions src/kaleidoscope/device/dygma/Defy_wireless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
};

Expand All @@ -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()); }));


Expand Down Expand Up @@ -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);
}

Expand All @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions src/kaleidoscope/device/dygma/defy_wireless/Focus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(deviceLeft));
return EventHandlerResult::OK;
}

if (strcmp(command + 9, "sideRight") == 0) {
auto &keyScanner = Runtime.device().keyScanner();
auto deviceRight = keyScanner.rightHandDevice();
::Focus.send(static_cast<uint8_t>(deviceRight));
return EventHandlerResult::OK;
}

if (strcmp(command + 9, "version") == 0) {
NRF_LOG_DEBUG("read request: hardware.version");

Expand Down
10 changes: 8 additions & 2 deletions src/kaleidoscope/plugin/IdleLEDsDefy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
#ifdef ARDUINO_ARCH_NRF52

#include "Communications.h"
#include "Defy_wireless.h"
#include <Kaleidoscope-EEPROM-Settings.h>
#include <Kaleidoscope-FocusSerial.h>
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/kaleidoscope/plugin/IdleLEDsDefy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 74e2023

Please sign in to comment.