Skip to content

Commit

Permalink
refactor: improved communications
Browse files Browse the repository at this point in the history
Signed-off-by: noteolvides <[email protected]>
  • Loading branch information
Noteolvides committed Oct 11, 2023
1 parent a9b491c commit 19d2012
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 70 deletions.
102 changes: 88 additions & 14 deletions src/kaleidoscope/device/dygma/Defy_wireless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "Defy_wireless.h"
#include "LED-Palette-Theme-Defy.h"
#include "Radio_manager.h"
#include "Wire.h" // Arduino Wire wrapper for the NRF52 chips
#include "Wire.h" // Arduino Wire wrapper for the NRF52 chips
#include "defy_wireless/Focus.h"
#include "nrf_gpio.h"

Expand Down Expand Up @@ -105,8 +105,8 @@ struct DefyHands
uint8_t led_brightness_underglow_wireless_;
uint8_t flag;
} bright;
private:

private:
static uint8_t keyscan_interval_;
static bool side_power_;
static uint16_t settings_interval_;
Expand Down Expand Up @@ -144,12 +144,66 @@ void DefyHands::setSidePower(bool power)

side_power_ = power;
}
// BLE WIRED RF
Communications_protocol::Devices leftConnection[3]{UNKNOWN, UNKNOWN, UNKNOWN};
Communications_protocol::Devices rightConnection[3]{UNKNOWN, UNKNOWN, UNKNOWN};

void DefyHands::setup()
{
rightHand.init();
leftHand.init();

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 == 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;
}));
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 == 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;
}));

auto checkBrightness = [](const Packet &)
{
auto &keyScanner = Runtime.device().keyScanner();
auto &ledDriver = Runtime.device().ledDriver();

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;
ColormapEffectDefy.updateBrigthness((isDefyLeftWired && isDefyRightWired) && !isEitherUnknown);
};
Communications.callbacks.bind(CONNECTED, checkBrightness);
Communications.callbacks.bind(DISCONNECTED, checkBrightness);



settings_interval_ = ::EEPROMSettings.requestSlice(sizeof(keyscan_interval_));
settings_base = ::EEPROMSettings.requestSlice(sizeof(DefyHands::Brightness));
// If keyscan is max, assume that EEPROM is uninitialized, and store the defaults.
Expand Down Expand Up @@ -223,12 +277,12 @@ void DefyHands::ledBrightnessUGWireless(uint8_t brightnessUG)

void DefyHands::sendPacketBrightness()
{
auto& keyScanner = Runtime.device().keyScanner();
auto &keyScanner = Runtime.device().keyScanner();

auto deviceLeft = keyScanner.leftHandDevice();
auto devicesRight = keyScanner.rightHandDevice();

bool checkWiredLeftSide = (deviceLeft == KEYSCANNER_DEFY_RIGHT || deviceLeft == Communications_protocol::KEYSCANNER_DEFY_LEFT);
bool checkWiredLeftSide = (deviceLeft == KEYSCANNER_DEFY_RIGHT || deviceLeft == Communications_protocol::KEYSCANNER_DEFY_LEFT);
bool checkWiredRightSide = (devicesRight == KEYSCANNER_DEFY_RIGHT || devicesRight == Communications_protocol::KEYSCANNER_DEFY_LEFT);

if (checkWiredLeftSide && checkWiredRightSide)
Expand Down Expand Up @@ -272,8 +326,8 @@ void DefyHands::get_chip_info(char *cstring, uint16_t len)
returns a cstring.
*/

snprintf(cstring, len, "DEVICEID=%8lx%8lx\nPART=%lx\nVARIANT=%lx\nPACKAGE=%lx\nRAM=%ld\nFLASH=%ld", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0], NRF_FICR->INFO.PART,
NRF_FICR->INFO.VARIANT, NRF_FICR->INFO.PACKAGE, NRF_FICR->INFO.RAM, NRF_FICR->INFO.FLASH);
snprintf(cstring, len, "DEVICEID=%8lx%8lx\nPART=%lx\nVARIANT=%lx\nPACKAGE=%lx\nRAM=%ld\nFLASH=%ld", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0],
NRF_FICR->INFO.PART, NRF_FICR->INFO.VARIANT, NRF_FICR->INFO.PACKAGE, NRF_FICR->INFO.RAM, NRF_FICR->INFO.FLASH);
}


Expand Down Expand Up @@ -641,12 +695,26 @@ void DefyKeyScanner::reset(void)

Communications_protocol::Devices DefyKeyScanner::leftHandDevice(void)
{
return DefyHands::leftHand.getConnectedDevice();
for (const auto &connection : leftConnection)
{
if (connection != UNKNOWN)
{
return connection;
}
}
return UNKNOWN;
}

Communications_protocol::Devices DefyKeyScanner::rightHandDevice(void)
{
return DefyHands::rightHand.getConnectedDevice();
for (const auto &connection : rightConnection)
{
if (connection != UNKNOWN)
{
return connection;
}
}
return UNKNOWN;
}

void DefyKeyScanner::usbConnectionsStateMachine()
Expand All @@ -657,16 +725,21 @@ void DefyKeyScanner::usbConnectionsStateMachine()
bool radioInitiated = kaleidoscope::plugin::RadioManager::isInited();

// For 100ms at the 700ms mark, check whether to initialize BLE or RF
if ((actualTime > 700 && actualTime < 800) && !bleInitiated && !radioInitiated) {
if (usbMounted) {
if ((actualTime > 700 && actualTime < 800) && !bleInitiated && !radioInitiated)
{
if (usbMounted)
{
kaleidoscope::plugin::RadioManager::init();
} else {
}
else
{
kaleidoscope::plugin::BleManager::init();
}
}

// If USB state and BLE or RF state are mismatched, reboot
if ((bleInitiated && usbMounted) || (radioInitiated && !usbMounted)) {
if ((bleInitiated && usbMounted) || (radioInitiated && !usbMounted))
{
reset_mcu();
}
}
Expand All @@ -676,7 +749,8 @@ void DefyKeyScanner::usbConnectionsStateMachine()

void DefyNrf::setup()
{
DefyNrf::side::reset_sides();
// Check if we can live without this reset sides
// DefyNrf::side::reset_sides();
DefyHands::setup();
DefyFocus.init();
KeyScanner::setup();
Expand Down Expand Up @@ -722,7 +796,7 @@ void DefyNrf::side::reset_sides()
delay(10);
nrf_gpio_cfg_input(SIDE_NRESET_1, NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_input(SIDE_NRESET_2, NRF_GPIO_PIN_PULLUP);
delay(10); //We should give a bit more time but for now lest leave it like this
delay(10); // We should give a bit more time but for now lest leave it like this
}

void DefyNrf::side::prepareForFlash()
Expand Down
49 changes: 2 additions & 47 deletions src/kaleidoscope/device/dygma/defy_wireless/Hand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ extern "C"
#endif

#include "Communications.h"
#include <Kaleidoscope-IdleLEDs.h>
#include "Colormap-Defy.h"


namespace kaleidoscope
Expand Down Expand Up @@ -47,55 +45,12 @@ void Hand::init()
{
if (filterHand(packet.header.device, this_device_))
{
if (memcmp(key_data_.rows, packet.data, sizeof(key_data)) == 0) return;
if (memcmp(key_data_.rows, packet.data, sizeof(key_data_.rows)) == 0) return;
new_key_ = true;
memcpy(key_data_.rows, packet.data, sizeof(key_data));
// NRF_LOG_DEBUG("New key %lu",key_data_.all);
memcpy(key_data_.rows, packet.data, sizeof(key_data_.rows));
}
};
Communications.callbacks.bind(HAS_KEYS, keyScanFunction);
auto keyScanFunctionIsAlive = [this](Packet const &packet)
{
if(packet.data[0] != HAS_KEYS) return;
if (filterHand(packet.header.device, this_device_))
{
if (memcmp(key_data_.rows, &packet.data[1], sizeof(key_data)) == 0) return;
new_key_ = true;
memcpy(key_data_.rows, &packet.data[1], sizeof(key_data));
NRF_LOG_DEBUG("New key is alive %lu",key_data_.all);
}
};
Communications.callbacks.bind(IS_ALIVE, keyScanFunctionIsAlive);
auto checkConnected = [this](Packet const &packet)
{
if (filterHand(packet.header.device, this_device_))
{
NRF_LOG_DEBUG("Connected device %i",packet.header.device);
connected_ = packet.header.device;
if(ble_innited()){
if(this_device_==RIGHT){
connected_ = BLE_DEFY_RIGHT;
}else{
connected_ = BLE_DEFY_LEFT;
}
}
}
};
Communications.callbacks.bind(CONNECTED, checkConnected);
auto checkDisconnected = [this](Packet const &packet)
{
if (filterHand(packet.header.device, this_device_))
{
NRF_LOG_DEBUG("Disconnected device %i",packet.header.device);
connected_ = UNKNOWN;
}
};
Communications.callbacks.bind(DISCONNECTED, checkDisconnected);
}

Devices Hand::getConnectedDevice() const
{
return connected_;
}


Expand Down
3 changes: 0 additions & 3 deletions src/kaleidoscope/device/dygma/defy_wireless/Hand.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ typedef union {
uint8_t rows[5];
uint64_t all;
} key_data;
using namespace Communications_protocol;
class Hand {
public:
enum HandSide {
Expand All @@ -68,10 +67,8 @@ class Hand {
private:
defy_wireless::key_data key_data_{};
bool new_key_;
Communications_protocol::Devices connected_{UNKNOWN};

public:
Devices getConnectedDevice() const;

public:
const key_data &getKeyData() {
Expand Down
12 changes: 6 additions & 6 deletions src/kaleidoscope/plugin/IdleLEDsDefy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace kaleidoscope
namespace plugin
{

uint32_t IdleLEDsDefy::idle_time_limit_default = 600000; // 10 minutes
uint32_t IdleLEDsDefy::idle_time_limit_default = 600000; // 10 minutes
uint32_t IdleLEDsDefy::idle_time_limit_default_wireless = 300000; // 5 minutes
IdleLEDsDefy::IdleTime IdleLEDsDefy::idle_time_limit;
uint32_t IdleLEDsDefy::start_time_wired = 0;
Expand All @@ -43,14 +43,14 @@ uint32_t IdleLEDsDefy::idleTimeoutSeconds(uint32_t time_in_ms)
EventHandlerResult IdleLEDsDefy::beforeEachCycle()
{
if (idle_time_limit.wired_ == 0 || idle_time_limit.wireless_ == 0) return EventHandlerResult::OK;
auto const& keyScanner = Runtime.device().keyScanner();
auto const &keyScanner = Runtime.device().keyScanner();
auto deviceLeft = keyScanner.leftHandDevice();
auto devicesRight = keyScanner.rightHandDevice();
auto isEitherUnknown = deviceLeft == Communications_protocol::UNKNOWN && devicesRight == Communications_protocol::UNKNOWN;
auto isDefyLeftWired = deviceLeft == Communications_protocol::KEYSCANNER_DEFY_LEFT || deviceLeft == Communications_protocol::UNKNOWN;
auto isDefyRightWired = devicesRight == Communications_protocol::KEYSCANNER_DEFY_RIGHT || devicesRight == Communications_protocol::UNKNOWN;

bool checkWiredLeftSide = (deviceLeft == Communications_protocol::KEYSCANNER_DEFY_RIGHT || deviceLeft == Communications_protocol::KEYSCANNER_DEFY_LEFT);
bool checkWiredRightSide = (devicesRight == Communications_protocol::KEYSCANNER_DEFY_RIGHT || devicesRight == Communications_protocol::KEYSCANNER_DEFY_LEFT);

if (checkWiredLeftSide && checkWiredRightSide)
if ((isDefyLeftWired && isDefyRightWired) && !isEitherUnknown)
{
start_time_wireless = Runtime.millisAtCycleStart();
if (::LEDControl.isEnabled() && Runtime.hasTimeExpired(start_time_wired, idle_time_limit.wired_))
Expand Down

0 comments on commit 19d2012

Please sign in to comment.