Inline isKeyPressed(). Rename key to action within HandleMouseKey() and a few other identifiers to make the code more intuitive.
This commit is contained in:
@@ -5,75 +5,65 @@
|
||||
#include "usb_interface_mouse.h"
|
||||
#include "current_keymap.h"
|
||||
|
||||
static uint8_t ActiveLayer = LAYER_ID_BASE;
|
||||
|
||||
static bool isKeyPressed(const uint8_t *currKeyStates, uint8_t keyId)
|
||||
{
|
||||
return currKeyStates[keyId];
|
||||
}
|
||||
|
||||
static uint8_t activeLayer = LAYER_ID_BASE;
|
||||
static uint8_t mouseWheelDivisorCounter = 0;
|
||||
static uint8_t mouseSpeedAccelDivisorCounter = 0;
|
||||
static uint8_t mouseSpeed = 3;
|
||||
static bool wasPreviousMouseActionWheelAction = false;
|
||||
|
||||
void HandleMouseKey(usb_mouse_report_t *report, key_action_t key, const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId)
|
||||
void HandleMouseKey(key_action_t action)
|
||||
{
|
||||
if (!isKeyPressed(currKeyStates, keyId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool isWheelAction = key.mouse.scrollActions && !key.mouse.moveActions && !key.mouse.buttonActions;
|
||||
bool isWheelAction = action.mouse.scrollActions && !action.mouse.moveActions && !action.mouse.buttonActions;
|
||||
|
||||
if (isWheelAction && wasPreviousMouseActionWheelAction) {
|
||||
mouseWheelDivisorCounter++;
|
||||
}
|
||||
|
||||
if (key.mouse.scrollActions) {
|
||||
if (action.mouse.scrollActions) {
|
||||
if (mouseWheelDivisorCounter == MOUSE_WHEEL_DIVISOR) {
|
||||
mouseWheelDivisorCounter = 0;
|
||||
if (key.mouse.scrollActions & MOUSE_SCROLL_UP) {
|
||||
report->wheelX = 1;
|
||||
if (action.mouse.scrollActions & MOUSE_SCROLL_UP) {
|
||||
UsbMouseReport.wheelX = 1;
|
||||
}
|
||||
if (key.mouse.scrollActions & MOUSE_SCROLL_DOWN) {
|
||||
report->wheelX = -1;
|
||||
if (action.mouse.scrollActions & MOUSE_SCROLL_DOWN) {
|
||||
UsbMouseReport.wheelX = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (key.mouse.moveActions & MOUSE_ACCELERATE || key.mouse.moveActions & MOUSE_DECELERATE) {
|
||||
if (action.mouse.moveActions & MOUSE_ACCELERATE || action.mouse.moveActions & MOUSE_DECELERATE) {
|
||||
mouseSpeedAccelDivisorCounter++;
|
||||
|
||||
if (mouseSpeedAccelDivisorCounter == MOUSE_SPEED_ACCEL_DIVISOR) {
|
||||
mouseSpeedAccelDivisorCounter = 0;
|
||||
|
||||
if (key.mouse.moveActions & MOUSE_ACCELERATE) {
|
||||
if (action.mouse.moveActions & MOUSE_ACCELERATE) {
|
||||
if (mouseSpeed < MOUSE_MAX_SPEED) {
|
||||
mouseSpeed++;
|
||||
}
|
||||
}
|
||||
if (key.mouse.moveActions & MOUSE_DECELERATE) {
|
||||
if (action.mouse.moveActions & MOUSE_DECELERATE) {
|
||||
if (mouseSpeed > 1) {
|
||||
mouseSpeed--;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (key.mouse.moveActions) {
|
||||
if (key.mouse.moveActions & MOUSE_MOVE_LEFT) {
|
||||
report->x = -mouseSpeed;
|
||||
} else if (action.mouse.moveActions) {
|
||||
if (action.mouse.moveActions & MOUSE_MOVE_LEFT) {
|
||||
UsbMouseReport.x = -mouseSpeed;
|
||||
}
|
||||
if (key.mouse.moveActions & MOUSE_MOVE_RIGHT) {
|
||||
report->x = mouseSpeed;
|
||||
if (action.mouse.moveActions & MOUSE_MOVE_RIGHT) {
|
||||
UsbMouseReport.x = mouseSpeed;
|
||||
}
|
||||
if (key.mouse.moveActions & MOUSE_MOVE_UP) {
|
||||
report->y = -mouseSpeed;
|
||||
if (action.mouse.moveActions & MOUSE_MOVE_UP) {
|
||||
UsbMouseReport.y = -mouseSpeed;
|
||||
}
|
||||
if (key.mouse.moveActions & MOUSE_MOVE_DOWN) {
|
||||
report->y = mouseSpeed;
|
||||
if (action.mouse.moveActions & MOUSE_MOVE_DOWN) {
|
||||
UsbMouseReport.y = mouseSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
report->buttons |= key.mouse.buttonActions;
|
||||
UsbMouseReport.buttons |= action.mouse.buttonActions;
|
||||
|
||||
wasPreviousMouseActionWheelAction = isWheelAction;
|
||||
}
|
||||
@@ -81,13 +71,13 @@ void HandleMouseKey(usb_mouse_report_t *report, key_action_t key, const uint8_t
|
||||
void HandleKeyboardEvents() {
|
||||
int scancodeIdx = 0;
|
||||
|
||||
ActiveLayer = LAYER_ID_BASE;
|
||||
activeLayer = LAYER_ID_BASE;
|
||||
for (uint8_t slotId=0; slotId<SLOT_COUNT; slotId++) {
|
||||
for (uint8_t keyId=0; keyId<MAX_KEY_COUNT_PER_MODULE; keyId++) {
|
||||
if (CurrentKeyStates[slotId][keyId]) {
|
||||
key_action_t action = CurrentKeymap[LAYER_ID_BASE][slotId][keyId];
|
||||
if (action.type == KEY_ACTION_SWITCH_LAYER) {
|
||||
ActiveLayer = action.switchLayer.layer;
|
||||
activeLayer = action.switchLayer.layer;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -100,14 +90,14 @@ void HandleKeyboardEvents() {
|
||||
continue;
|
||||
}
|
||||
|
||||
key_action_t action = CurrentKeymap[ActiveLayer][slotId][keyId];
|
||||
key_action_t action = CurrentKeymap[activeLayer][slotId][keyId];
|
||||
switch (action.type) {
|
||||
case KEY_ACTION_KEYSTROKE:
|
||||
ActiveUsbKeyboardReport->scancodes[scancodeIdx++] = action.keystroke.key;
|
||||
ActiveUsbKeyboardReport->modifiers |= action.keystroke.mods;
|
||||
break;
|
||||
case KEY_ACTION_MOUSE:
|
||||
HandleMouseKey(&UsbMouseReport, action, PreviousKeyStates[slotId], CurrentKeyStates[slotId], keyId);
|
||||
HandleMouseKey(action);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ typedef struct {
|
||||
};
|
||||
} __attribute__ ((packed)) key_action_t;
|
||||
|
||||
void HandleMouseKey(usb_mouse_report_t *report, key_action_t key, const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId);
|
||||
void HandleMouseKey(key_action_t action);
|
||||
void HandleKeyboardEvents();
|
||||
|
||||
#endif
|
||||
|
||||
@@ -74,7 +74,7 @@ void UpdateUsbReports()
|
||||
// I2cRead(I2C_MAIN_BUS_BASEADDR, I2C_ADDRESS_LEFT_KEYBOARD_HALF, CurrentKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], LEFT_KEYBOARD_HALF_KEY_COUNT);
|
||||
// }
|
||||
|
||||
HandleKeyboardEvents(ActiveUsbKeyboardReport, &UsbMouseReport);
|
||||
HandleKeyboardEvents();
|
||||
|
||||
SwitchActiveUsbKeyboardReport();
|
||||
}
|
||||
|
||||
@@ -53,11 +53,6 @@ static volatile usb_status_t UsbMouseAction(void)
|
||||
return ret;
|
||||
}
|
||||
|
||||
void fillMouseReport(key_action_t key, const uint8_t *prevKeyStates, const uint8_t *currKeyStates, uint8_t keyId)
|
||||
{
|
||||
HandleMouseKey(&UsbMouseReport, key, prevKeyStates, currKeyStates, keyId);
|
||||
}
|
||||
|
||||
usb_status_t UsbMouseCallback(class_handle_t handle, uint32_t event, void *param)
|
||||
{
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
|
||||
Reference in New Issue
Block a user