Move updateLayerStates() and getActiveLayer() into the newly created layer.c
This commit is contained in:
73
right/src/layer.c
Normal file
73
right/src/layer.c
Normal file
@@ -0,0 +1,73 @@
|
||||
#include "layer.h"
|
||||
#include "slot.h"
|
||||
#include "module.h"
|
||||
#include "key_states.h"
|
||||
#include "keymaps.h"
|
||||
|
||||
static bool heldLayers[LAYER_COUNT];
|
||||
static bool pressedLayers[LAYER_COUNT];
|
||||
|
||||
void updateLayerStates(void)
|
||||
{
|
||||
memset(heldLayers, false, LAYER_COUNT);
|
||||
memset(pressedLayers, false, LAYER_COUNT);
|
||||
|
||||
for (uint8_t slotId=0; slotId<SLOT_COUNT; slotId++) {
|
||||
for (uint8_t keyId=0; keyId<MAX_KEY_COUNT_PER_MODULE; keyId++) {
|
||||
key_state_t *keyState = &KeyStates[slotId][keyId];
|
||||
if (keyState->current) {
|
||||
key_action_t action = CurrentKeymap[LayerId_Base][slotId][keyId];
|
||||
if (action.type == KeyActionType_SwitchLayer) {
|
||||
if (!action.switchLayer.isToggle) {
|
||||
heldLayers[action.switchLayer.layer] = true;
|
||||
} else if (!keyState->previous && keyState->current) {
|
||||
pressedLayers[action.switchLayer.layer] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
layer_id_t PreviousHeldLayer = LayerId_Base;
|
||||
|
||||
layer_id_t GetActiveLayer()
|
||||
{
|
||||
updateLayerStates();
|
||||
|
||||
// Handle toggled layers
|
||||
|
||||
static layer_id_t toggledLayer = LayerId_Base;
|
||||
|
||||
for (layer_id_t layerId=LayerId_Mod; layerId<=LayerId_Mouse; layerId++) {
|
||||
if (pressedLayers[layerId]) {
|
||||
if (toggledLayer == layerId) {
|
||||
toggledLayer = LayerId_Base;
|
||||
break;
|
||||
} else if (toggledLayer == LayerId_Base) {
|
||||
toggledLayer = layerId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (toggledLayer != LayerId_Base) {
|
||||
return toggledLayer;
|
||||
}
|
||||
|
||||
// Handle held layers
|
||||
|
||||
layer_id_t heldLayer = LayerId_Base;
|
||||
|
||||
for (layer_id_t layerId=LayerId_Mod; layerId<=LayerId_Mouse; layerId++) {
|
||||
if (heldLayers[layerId]) {
|
||||
heldLayer = layerId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
heldLayer = heldLayer != LayerId_Base && heldLayers[PreviousHeldLayer] ? PreviousHeldLayer : heldLayer;
|
||||
PreviousHeldLayer = heldLayer;
|
||||
|
||||
return heldLayer;
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
#ifndef __LAYER_H__
|
||||
#define __LAYER_H__
|
||||
|
||||
// Includes:
|
||||
|
||||
#include "fsl_common.h"
|
||||
|
||||
// Macros:
|
||||
|
||||
#define LAYER_COUNT 4
|
||||
@@ -14,4 +18,13 @@
|
||||
LayerId_Mouse,
|
||||
} layer_id_t;
|
||||
|
||||
// Variables:
|
||||
|
||||
extern layer_id_t PreviousHeldLayer;
|
||||
|
||||
// Functions:
|
||||
|
||||
layer_id_t GetActiveLayer();
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "macros.h"
|
||||
#include "key_states.h"
|
||||
#include "right_key_matrix.h"
|
||||
#include "layer.h"
|
||||
|
||||
static uint8_t mouseWheelDivisorCounter = 0;
|
||||
static uint8_t mouseSpeedAccelDivisorCounter = 0;
|
||||
@@ -74,74 +75,6 @@ void processMouseAction(key_action_t action)
|
||||
wasPreviousMouseActionWheelAction = isWheelAction;
|
||||
}
|
||||
|
||||
static bool heldLayers[LAYER_COUNT];
|
||||
static bool pressedLayers[LAYER_COUNT];
|
||||
|
||||
void updateLayerStates(void)
|
||||
{
|
||||
memset(heldLayers, false, LAYER_COUNT);
|
||||
memset(pressedLayers, false, LAYER_COUNT);
|
||||
|
||||
for (uint8_t slotId=0; slotId<SLOT_COUNT; slotId++) {
|
||||
for (uint8_t keyId=0; keyId<MAX_KEY_COUNT_PER_MODULE; keyId++) {
|
||||
key_state_t *keyState = &KeyStates[slotId][keyId];
|
||||
if (keyState->current) {
|
||||
key_action_t action = CurrentKeymap[LayerId_Base][slotId][keyId];
|
||||
if (action.type == KeyActionType_SwitchLayer) {
|
||||
if (!action.switchLayer.isToggle) {
|
||||
heldLayers[action.switchLayer.layer] = true;
|
||||
} else if (!keyState->previous && keyState->current) {
|
||||
pressedLayers[action.switchLayer.layer] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static layer_id_t PreviousHeldLayer = LayerId_Base;
|
||||
|
||||
layer_id_t getActiveLayer()
|
||||
{
|
||||
updateLayerStates();
|
||||
|
||||
// Handle toggled layers
|
||||
|
||||
static layer_id_t toggledLayer = LayerId_Base;
|
||||
|
||||
for (layer_id_t layerId=LayerId_Mod; layerId<=LayerId_Mouse; layerId++) {
|
||||
if (pressedLayers[layerId]) {
|
||||
if (toggledLayer == layerId) {
|
||||
toggledLayer = LayerId_Base;
|
||||
break;
|
||||
} else if (toggledLayer == LayerId_Base) {
|
||||
toggledLayer = layerId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (toggledLayer != LayerId_Base) {
|
||||
return toggledLayer;
|
||||
}
|
||||
|
||||
// Handle held layers
|
||||
|
||||
layer_id_t heldLayer = LayerId_Base;
|
||||
|
||||
for (layer_id_t layerId=LayerId_Mod; layerId<=LayerId_Mouse; layerId++) {
|
||||
if (heldLayers[layerId]) {
|
||||
heldLayer = layerId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
heldLayer = heldLayer != LayerId_Base && heldLayers[PreviousHeldLayer] ? PreviousHeldLayer : heldLayer;
|
||||
PreviousHeldLayer = heldLayer;
|
||||
|
||||
return heldLayer;
|
||||
}
|
||||
|
||||
void UpdateActiveUsbReports(void)
|
||||
{
|
||||
uint8_t basicScancodeIndex = 0;
|
||||
@@ -152,7 +85,7 @@ void UpdateActiveUsbReports(void)
|
||||
KeyStates[SlotId_RightKeyboardHalf][keyId].current = RightKeyMatrix.keyStates[keyId];
|
||||
}
|
||||
|
||||
layer_id_t activeLayer = getActiveLayer();
|
||||
layer_id_t activeLayer = GetActiveLayer();
|
||||
LedDisplay_SetLayer(activeLayer);
|
||||
static uint8_t previousModifiers = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user