Skip to content

Commit

Permalink
Merge pull request #1062 from UltimateHackingKeyboard/chance_test_led…
Browse files Browse the repository at this point in the history
…_ligth_all_behavior

Light entire matrix at the beginning of switch test mode.
  • Loading branch information
mondalaci authored Dec 31, 2024
2 parents eb12223 + ce69d89 commit ead30c6
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 90 deletions.
5 changes: 2 additions & 3 deletions device/src/keyboard/key_scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "attributes.h"
#include "layouts/key_layout.h"
#include "layouts/key_layout_80_to_universal.h"
#include "test_switches.h"

// Thread definitions

Expand Down Expand Up @@ -108,8 +109,6 @@ static void scanKeys() {
memset(compressedBuffer, 0, compressedLength);
}

backlighting_mode_t currentBacklightingMode = Ledmap_GetEffectiveBacklightMode();

uint8_t slotId = DEVICE_IS_UHK80_LEFT ? SlotId_LeftKeyboardHalf : SlotId_RightKeyboardHalf;
for (uint8_t rowId=0; rowId<KEY_MATRIX_ROWS; rowId++) {
for (uint8_t colId=0; colId<KEY_MATRIX_COLS; colId++) {
Expand All @@ -123,7 +122,7 @@ static void scanKeys() {
}

if (targetKeyId < MAX_KEY_COUNT_PER_MODULE) {
if (currentBacklightingMode == BacklightingMode_LedTest) {
if (TestSwitches) {
if ( keyStateBuffer[sourceIndex] ) {
Ledmap_ActivateTestled(slotId, targetKeyId);
EventVector_WakeMain();
Expand Down
16 changes: 16 additions & 0 deletions device/src/state_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <zephyr/kernel.h>
#include "peripherals/merge_sensor.h"
#include "power_mode.h"
#include "test_switches.h"

#define WAKE(TID) if (TID != 0) { k_wakeup(TID); }

Expand Down Expand Up @@ -128,6 +129,7 @@ static state_sync_prop_t stateSyncProps[StateSyncPropertyId_Count] = {
SIMPLE(FunctionalColors, SyncDirection_RightToLeft, DirtyState_Clean, &Cfg.KeyActionColors),
SIMPLE(PowerMode, SyncDirection_RightToLeft, DirtyState_Clean, &CurrentPowerMode),
CUSTOM(Config, SyncDirection_RightToLeft, DirtyState_Clean),
CUSTOM(SwitchTestMode, SyncDirection_RightToLeft, DirtyState_Clean),
};

static void invalidateProperty(state_sync_prop_id_t propId) {
Expand Down Expand Up @@ -352,6 +354,15 @@ static void receiveProperty(device_id_t src, state_sync_prop_id_t propId, const
break;
case StateSyncPropertyId_MergeSensor:
break;
case StateSyncPropertyId_SwitchTestMode:
if (!isLocalUpdate) {
bool newMode = *(bool*)data;
if (newMode != TestSwitches) {
newMode ? TestSwitches_Activate() : TestSwitches_Deactivate();
Main_Wake();
}
}
break;
default:
printk("Property %i ('%s') has no receive handler. If this is correct, please add a "
"separate empty case...\n",
Expand Down Expand Up @@ -493,6 +504,10 @@ static void prepareData(device_id_t dst, const uint8_t *propDataPtr, state_sync_
submitPreparedData(dst, propId, (const uint8_t *)&buffer, sizeof(buffer));
return;
} break;
case StateSyncPropertyId_SwitchTestMode: {
submitPreparedData(dst, propId, (const uint8_t *)&TestSwitches, sizeof(TestSwitches));
return;
}
default:
break;
}
Expand Down Expand Up @@ -536,6 +551,7 @@ static void updateProperty(state_sync_prop_id_t propId) {
static bool handlePropertyUpdateRightToLeft() {
UPDATE_AND_RETURN_IF_DIRTY(StateSyncPropertyId_ResetRightLeftLink);
UPDATE_AND_RETURN_IF_DIRTY(StateSyncPropertyId_Config);
UPDATE_AND_RETURN_IF_DIRTY(StateSyncPropertyId_SwitchTestMode);

if (KeyBacklightBrightness != 0) {
// Update relevant data
Expand Down
1 change: 1 addition & 0 deletions device/src/state_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
StateSyncPropertyId_FunctionalColors = 25,
StateSyncPropertyId_PowerMode = 26,
StateSyncPropertyId_Config = 27,
StateSyncPropertyId_SwitchTestMode = 28,
StateSyncPropertyId_Count,
} state_sync_prop_id_t;

Expand Down
52 changes: 17 additions & 35 deletions right/src/ledmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "slot.h"
#include "config_manager.h"
#include "event_scheduler.h"
#include "test_switches.h"

#ifdef __ZEPHYR__
#include "keyboard/leds.h"
Expand All @@ -29,14 +30,7 @@ static const rgb_t white = RGB(0xff, 0xff, 0xff);
bool Ledmap_AlwaysOn = false;
backlighting_mode_t TemporaryBacklightingMode = BacklightingMode_Unspecified;

typedef enum {
BacklightingLedTestModeState_All,
BacklightingLedTestModeState_Additive,
} backlighting_led_test_mode_state_t;

bool Ledmap_LedTestActive = false;
static uint32_t backlightingLedTestStart = 0;
static backlighting_led_test_mode_state_t backlightingLedTestModeState = BacklightingLedTestModeState_All;

#if DEVICE_ID == DEVICE_ID_UHK60V1

Expand Down Expand Up @@ -594,38 +588,26 @@ static void setEntireMatrix(uint8_t v) {
#endif
}

static void setAllTo(const rgb_t* color) {
for (uint8_t slotId=0; slotId<SLOT_COUNT; slotId++) {
color_mode_t colorMode = determineMode(slotId);
for (uint8_t keyId=0; keyId<MAX_KEY_COUNT_PER_MODULE; keyId++) {
setPerKeyColor(color, colorMode, slotId, keyId);
}
}
}

static void updateLedsByLedTestStragegy() {
if (backlightingLedTestModeState == BacklightingLedTestModeState_All) {
setAllTo(&white);
}
}


static void updateLedsByLightAllStragegy() {
setEntireMatrix(255);
}

void Ledmap_ActivateTestled(uint8_t slotId, uint8_t keyId) {
if (CurrentTime < backlightingLedTestStart + 1000) {
if (CurrentTime < backlightingLedTestStart + 1000 || !TestSwitches) {
return;
}

if (backlightingLedTestModeState == BacklightingLedTestModeState_All) {
setAllTo(&black);
backlightingLedTestModeState = BacklightingLedTestModeState_Additive;
if (TemporaryBacklightingMode == BacklightingMode_LightAll) {
Ledmap_SetTemporaryLedBacklightingMode(BacklightingMode_LedTest);
setEntireMatrix(0);
}

color_mode_t colorMode = determineMode(slotId);
setPerKeyColor(&white, colorMode, slotId, keyId);
EventVector_Set(EventVector_LedMapUpdateNeeded);
EventVector_Set(EventVector_LedManagerFullUpdateNeeded);
}

backlighting_mode_t Ledmap_GetEffectiveBacklightMode() {
Expand All @@ -637,25 +619,24 @@ backlighting_mode_t Ledmap_GetEffectiveBacklightMode() {
}

void handleModeChange(backlighting_mode_t from, backlighting_mode_t to) {
if (from == BacklightingMode_LightAll) {
setEntireMatrix(0);
if (to == BacklightingMode_LightAll) {
setEntireMatrix(255);
}
if (to == BacklightingMode_LedTest) {
backlightingLedTestModeState = BacklightingLedTestModeState_All;
backlightingLedTestStart = CurrentTime;

if (from == BacklightingMode_LightAll && to != BacklightingMode_LedTest) {
setEntireMatrix(0);
}
}

void Ledmap_ActivateTestLedMode(bool active) {
if (active) {
Ledmap_LedTestActive = true;
Ledmap_SetTemporaryLedBacklightingMode(BacklightingMode_LedTest);
EventVector_Set(EventVector_LedMapUpdateNeeded);
backlightingLedTestStart = CurrentTime;
Ledmap_SetTemporaryLedBacklightingMode(BacklightingMode_LightAll);
EventVector_Set(EventVector_LedManagerFullUpdateNeeded);
EventVector_WakeMain();
} else {
Ledmap_LedTestActive = false;
Ledmap_ResetTemporaryLedBacklightingMode();
EventVector_Set(EventVector_LedMapUpdateNeeded);
EventVector_Set(EventVector_LedManagerFullUpdateNeeded);
EventVector_WakeMain();
}
}
Expand Down Expand Up @@ -760,6 +741,7 @@ void Ledmap_SetTemporaryLedBacklightingMode(backlighting_mode_t newMode) {

void Ledmap_ResetTemporaryLedBacklightingMode() {
TemporaryBacklightingMode = BacklightingMode_Unspecified;
updateAlwaysOn();
#ifdef __ZEPHYR__
StateSync_UpdateProperty(StateSyncPropertyId_Backlight, NULL);
#endif
Expand Down
2 changes: 2 additions & 0 deletions right/src/macros/status_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

// Typedefs:

#define PRINTM(...) Macros_ReportPrintf(NULL, __VA_ARGS__)

// Variables:
//
extern bool Macros_ConsumeStatusCharDirtyFlag;
Expand Down
2 changes: 1 addition & 1 deletion right/src/slave_drivers/uhk_module_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static void reloadKeymapIfNeeded()
EventVector_WakeMain();
}
#else
if (!someoneElseWillDoTheJob && !TestSwitches) {
if (!someoneElseWillDoTheJob) {
EventVector_Set(EventVector_KeymapReloadNeeded);
}

Expand Down
68 changes: 34 additions & 34 deletions right/src/slave_drivers/uhk_module_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,60 +31,60 @@
typedef enum {

// Sync communication
UhkModulePhase_RequestSync,
UhkModulePhase_ReceiveSync,
UhkModulePhase_ProcessSync,
UhkModulePhase_RequestSync = 0,
UhkModulePhase_ReceiveSync = 1,
UhkModulePhase_ProcessSync = 2,

// Get protocol version
UhkModulePhase_RequestModuleProtocolVersion,
UhkModulePhase_ReceiveModuleProtocolVersion,
UhkModulePhase_ProcessModuleProtocolVersion,
UhkModulePhase_RequestModuleProtocolVersion = 3,
UhkModulePhase_ReceiveModuleProtocolVersion = 4,
UhkModulePhase_ProcessModuleProtocolVersion = 5,

// Get firmware version
UhkModulePhase_RequestFirmwareVersion,
UhkModulePhase_ReceiveFirmwareVersion,
UhkModulePhase_ProcessFirmwareVersion,
UhkModulePhase_RequestFirmwareVersion = 6,
UhkModulePhase_ReceiveFirmwareVersion = 7,
UhkModulePhase_ProcessFirmwareVersion = 8,

// Get module id
UhkModulePhase_RequestModuleId,
UhkModulePhase_ReceiveModuleId,
UhkModulePhase_ProcessModuleId,
UhkModulePhase_RequestModuleId = 9,
UhkModulePhase_ReceiveModuleId = 10,
UhkModulePhase_ProcessModuleId = 11,

// Get module key count
UhkModulePhase_RequestModuleKeyCount,
UhkModulePhase_ReceiveModuleKeyCount,
UhkModulePhase_ProcessModuleKeyCount,
UhkModulePhase_RequestModuleKeyCount = 12,
UhkModulePhase_ReceiveModuleKeyCount = 13,
UhkModulePhase_ProcessModuleKeyCount = 14,

// Get module key count
UhkModulePhase_RequestModulePointerCount,
UhkModulePhase_ReceiveModulePointerCount,
UhkModulePhase_ProcessModulePointerCount,
UhkModulePhase_RequestModulePointerCount = 15,
UhkModulePhase_ReceiveModulePointerCount = 16,
UhkModulePhase_ProcessModulePointerCount = 17,

// Get key states
UhkModulePhase_RequestKeyStates,
UhkModulePhase_ReceiveKeystates,
UhkModulePhase_ProcessKeystates,
UhkModulePhase_RequestKeyStates = 18,
UhkModulePhase_ReceiveKeystates = 19,
UhkModulePhase_ProcessKeystates = 20,

// Get git tag
UhkModulePhase_RequestGitTag,
UhkModulePhase_ReceiveGitTag,
UhkModulePhase_ProcessGitTag,
UhkModulePhase_RequestGitTag = 21,
UhkModulePhase_ReceiveGitTag = 22,
UhkModulePhase_ProcessGitTag = 23,

// Get git repo
UhkModulePhase_RequestGitRepo,
UhkModulePhase_ReceiveGitRepo,
UhkModulePhase_ProcessGitRepo,
UhkModulePhase_RequestGitRepo = 24,
UhkModulePhase_ReceiveGitRepo = 25,
UhkModulePhase_ProcessGitRepo = 26,

// Get firmware checksum
UhkModulePhase_RequestFirmwareChecksum,
UhkModulePhase_ReceiveFirmwareChecksum,
UhkModulePhase_ProcessFirmwareChecksum,
UhkModulePhase_RequestFirmwareChecksum = 27,
UhkModulePhase_ReceiveFirmwareChecksum = 28,
UhkModulePhase_ProcessFirmwareChecksum = 29,

// Misc phases
UhkModulePhase_SetTestLed,
UhkModulePhase_SetLedPwmBrightness,
UhkModulePhase_JumpToBootloader,
UhkModulePhase_ResetTrackpoint,
UhkModulePhase_SetTestLed = 30,
UhkModulePhase_SetLedPwmBrightness = 31,
UhkModulePhase_JumpToBootloader = 32,
UhkModulePhase_ResetTrackpoint = 33,

} uhk_module_phase_t;

Expand Down
29 changes: 29 additions & 0 deletions right/src/test_switches.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#include "keymap.h"
#include "segment_display.h"
#include "slave_drivers/is31fl3xxx_driver.h"
#include "ledmap.h"
#include "led_manager.h"
#include "event_scheduler.h"

#ifdef __ZEPHYR__
#include "state_sync.h"
#endif

bool TestSwitches = false;

Expand Down Expand Up @@ -111,9 +118,31 @@ static const key_action_t TestKeymap[1][2][MAX_KEY_COUNT_PER_MODULE] = {

void TestSwitches_Activate(void)
{
TestSwitches = true;
memcpy(&CurrentKeymap, &TestKeymap, sizeof TestKeymap);
SegmentDisplay_SetText(3, "TES", SegmentDisplaySlot_Keymap);

#ifndef __ZEPHYR__
LedSlaveDriver_EnableAllLeds();
#endif

Ledmap_ActivateTestLedMode(true);

#if DEVICE_IS_UHK80_RIGHT
StateSync_UpdateProperty(StateSyncPropertyId_SwitchTestMode, NULL);
#endif
}

void TestSwitches_Deactivate(void)
{
TestSwitches = false;
Ledmap_ActivateTestLedMode(false);

#if DEVICE_IS_UHK80_RIGHT
StateSync_UpdateProperty(StateSyncPropertyId_SwitchTestMode, NULL);
#endif

#if DEVICE_IS_MASTER
EventVector_Set(EventVector_KeymapReloadNeeded);
#endif
}
1 change: 1 addition & 0 deletions right/src/test_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Functions:

void TestSwitches_Activate(void);
void TestSwitches_Deactivate(void);

// Variables:

Expand Down
8 changes: 8 additions & 0 deletions right/src/usb_commands/usb_command_launch_storage_transfer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "usb_commands/usb_command_launch_storage_transfer.h"
#include "event_scheduler.h"
#include "ledmap.h"
#include "usb_protocol_handler.h"
#include "config_parser/config_globals.h"

Expand Down Expand Up @@ -39,6 +41,12 @@ void UsbCommand_LaunchStorageTransfer(const uint8_t *GenericHidOutBuffer, uint8_
status_t status = EEPROM_LaunchTransfer(storageOperation, configBufferId, NULL);
#endif

if (configBufferId == ConfigBufferId_HardwareConfig) {
// reload is/ansi led settings
Ledmap_InitLedLayout();
EventVector_Set(EventVector_LedMapUpdateNeeded);
}

if (status != kStatus_Success) {
SetUsbTxBufferUint8(0, UsbStatusCode_LaunchStorageTransferTransferError);
SetUsbTxBufferUint32(1, status);
Expand Down
7 changes: 1 addition & 6 deletions right/src/usb_commands/usb_command_set_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ void UsbCommand_SetVariable(const uint8_t *GenericHidOutBuffer, uint8_t *Generic
switch (variableId) {
case UsbVariable_TestSwitches:
if (GetUsbRxBufferUint8(2)) {
TestSwitches = true;
TestSwitches_Activate();
Ledmap_ActivateTestLedMode(true);
} else {
TestSwitches = false;
Ledmap_ActivateTestLedMode(false);
SwitchKeymapById(CurrentKeymapIndex);
LedManager_FullUpdate();
TestSwitches_Deactivate();
}
break;
case UsbVariable_TestUsbStack:
Expand Down
Loading

0 comments on commit ead30c6

Please sign in to comment.