From 9fade6368cf83c08e3bd331ae5f6e750c98ab245 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 24 Jun 2018 13:33:59 -0700 Subject: [PATCH 01/19] Correct the type of macro delay variables --- right/src/config_parser/parse_macro.c | 2 +- right/src/macros.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/right/src/config_parser/parse_macro.c b/right/src/config_parser/parse_macro.c index 416d76a..23edb45 100644 --- a/right/src/config_parser/parse_macro.c +++ b/right/src/config_parser/parse_macro.c @@ -60,7 +60,7 @@ parser_error_t parseScrollMouseMacroAction(config_buffer_t *buffer, macro_action parser_error_t parseDelayMacroAction(config_buffer_t *buffer, macro_action_t *macroAction) { - int16_t delay = ReadInt16(buffer); + uint16_t delay = ReadUInt16(buffer); macroAction->type = MacroActionType_Delay; macroAction->delay.delay = delay; diff --git a/right/src/macros.h b/right/src/macros.h index 6eecc1a..1fded26 100644 --- a/right/src/macros.h +++ b/right/src/macros.h @@ -55,7 +55,7 @@ int16_t y; } ATTR_PACKED scrollMouse; struct { - int16_t delay; + uint16_t delay; } ATTR_PACKED delay; struct { const char *text; From 6e11c0b8afc87102685852c5cc66e78db34ce830 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 24 Jun 2018 13:56:58 -0700 Subject: [PATCH 02/19] Handle macro actions and make the macro engine preempt normal behavior --- right/src/usb_report_updater.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index 6a9e9f0..108f9a5 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -278,6 +278,9 @@ static void applyKeyAction(key_state_t *keyState, key_action_t *action) SwitchKeymapById(action->switchKeymap.keymapId); } break; + case KeyActionType_PlayMacro: + Macros_StartMacro(action->playMacro.macroId); + break; } } @@ -288,10 +291,19 @@ static secondary_role_t secondaryRole; static void updateActiveUsbReports(void) { - memset(activeMouseStates, 0, ACTIVE_MOUSE_STATES_COUNT); - static uint8_t previousModifiers = 0; + if (MacroPlaying) { + Macros_ContinueMacro(); + memcpy(&ActiveUsbMouseReport, &MacroMouseReport, sizeof MacroMouseReport); + memcpy(&ActiveUsbBasicKeyboardReport, &MacroBasicKeyboardReport, sizeof MacroBasicKeyboardReport); + memcpy(&ActiveUsbMediaKeyboardReport, &MacroMediaKeyboardReport, sizeof MacroMediaKeyboardReport); + memcpy(&ActiveUsbSystemKeyboardReport, &MacroSystemKeyboardReport, sizeof MacroSystemKeyboardReport); + return; + } + + memset(activeMouseStates, 0, ACTIVE_MOUSE_STATES_COUNT); + basicScancodeIndex = 0; mediaScancodeIndex = 0; systemScancodeIndex = 0; @@ -310,15 +322,6 @@ static void updateActiveUsbReports(void) bool layerGotReleased = previousLayer != LayerId_Base && activeLayer == LayerId_Base; LedDisplay_SetLayer(activeLayer); - if (MacroPlaying) { - Macros_ContinueMacro(); - memcpy(&ActiveUsbMouseReport, &MacroMouseReport, sizeof MacroMouseReport); - memcpy(&ActiveUsbBasicKeyboardReport, &MacroBasicKeyboardReport, sizeof MacroBasicKeyboardReport); - memcpy(&ActiveUsbMediaKeyboardReport, &MacroMediaKeyboardReport, sizeof MacroMediaKeyboardReport); - memcpy(&ActiveUsbSystemKeyboardReport, &MacroSystemKeyboardReport, sizeof MacroSystemKeyboardReport); - return; - } - for (uint8_t slotId=0; slotId Date: Sun, 24 Jun 2018 14:03:39 -0700 Subject: [PATCH 03/19] Rename macro key actions in accordance with Agent --- right/src/macros.c | 4 ++-- right/src/macros.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/right/src/macros.c b/right/src/macros.c index 900b3fc..566cbcf 100644 --- a/right/src/macros.c +++ b/right/src/macros.c @@ -189,7 +189,7 @@ bool processKeyMacroAction(void) static bool pressStarted; switch (currentMacroAction.key.action) { - case MacroSubAction_Press: + case MacroSubAction_Tap: if (!pressStarted) { pressStarted = true; addModifiers(currentMacroAction.key.modifierMask); @@ -234,7 +234,7 @@ bool processKeyMacroAction(void) break; } break; - case MacroSubAction_Hold: + case MacroSubAction_Press: addModifiers(currentMacroAction.key.modifierMask); switch (currentMacroAction.key.type) { case KeystrokeType_Basic: diff --git a/right/src/macros.h b/right/src/macros.h index 1fded26..a4f1c52 100644 --- a/right/src/macros.h +++ b/right/src/macros.h @@ -20,8 +20,8 @@ } macro_reference_t; typedef enum { + MacroSubAction_Tap, MacroSubAction_Press, - MacroSubAction_Hold, MacroSubAction_Release, } macro_sub_action_t; From 09a58b607c1be080d2f07bb315880428574a0b22 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 24 Jun 2018 14:24:10 -0700 Subject: [PATCH 04/19] Reduce code duplication --- right/src/macros.c | 78 ++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 44 deletions(-) diff --git a/right/src/macros.c b/right/src/macros.c index 566cbcf..24590d2 100644 --- a/right/src/macros.c +++ b/right/src/macros.c @@ -184,6 +184,36 @@ void deleteSystemScancode(uint8_t scancode) } } +void addScancode(uint16_t scancode, macro_sub_action_t type) +{ + switch (type) { + case KeystrokeType_Basic: + addBasicScancode(scancode); + break; + case KeystrokeType_Media: + addMediaScancode(scancode); + break; + case KeystrokeType_System: + addSystemScancode(scancode); + break; + } +} + +void deleteScancode(uint16_t scancode, macro_sub_action_t type) +{ + switch (type) { + case KeystrokeType_Basic: + deleteBasicScancode(scancode); + break; + case KeystrokeType_Media: + deleteMediaScancode(scancode); + break; + case KeystrokeType_System: + deleteSystemScancode(scancode); + break; + } +} + bool processKeyMacroAction(void) { static bool pressStarted; @@ -193,60 +223,20 @@ bool processKeyMacroAction(void) if (!pressStarted) { pressStarted = true; addModifiers(currentMacroAction.key.modifierMask); - switch (currentMacroAction.key.type) { - case KeystrokeType_Basic: - addBasicScancode(currentMacroAction.key.scancode); - break; - case KeystrokeType_Media: - // addMediaScancode(currentMacroAction.key.scancode); - break; - case KeystrokeType_System: - addSystemScancode(currentMacroAction.key.scancode); - break; - } + addScancode(currentMacroAction.key.scancode, currentMacroAction.key.type); return true; } pressStarted = false; deleteModifiers(currentMacroAction.key.modifierMask); - switch (currentMacroAction.key.type) { - case KeystrokeType_Basic: - deleteBasicScancode(currentMacroAction.key.scancode); - break; - case KeystrokeType_Media: - // deleteMediaScancode(currentMacroAction.key.scancode); - break; - case KeystrokeType_System: - deleteSystemScancode(currentMacroAction.key.scancode); - break; - } + deleteScancode(currentMacroAction.key.scancode, currentMacroAction.key.type); break; case MacroSubAction_Release: deleteModifiers(currentMacroAction.key.modifierMask); - switch (currentMacroAction.key.type) { - case KeystrokeType_Basic: - deleteBasicScancode(currentMacroAction.key.scancode); - break; - case KeystrokeType_Media: - // deleteMediaScancode(currentMacroAction.key.scancode); - break; - case KeystrokeType_System: - deleteSystemScancode(currentMacroAction.key.scancode); - break; - } + deleteScancode(currentMacroAction.key.scancode, currentMacroAction.key.type); break; case MacroSubAction_Press: addModifiers(currentMacroAction.key.modifierMask); - switch (currentMacroAction.key.type) { - case KeystrokeType_Basic: - addBasicScancode(currentMacroAction.key.scancode); - break; - case KeystrokeType_Media: - // addMediaScancode(currentMacroAction.key.scancode); - break; - case KeystrokeType_System: - addSystemScancode(currentMacroAction.key.scancode); - break; - } + addScancode(currentMacroAction.key.scancode, currentMacroAction.key.type); break; } return false; From 04f4053bde0101410a4736250e7531ae2da0a46c Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 24 Jun 2018 20:21:01 -0700 Subject: [PATCH 05/19] Add a delay function --- right/src/timer.c | 14 ++++++++++++++ right/src/timer.h | 1 + 2 files changed, 15 insertions(+) diff --git a/right/src/timer.c b/right/src/timer.c index 4106877..43779cd 100644 --- a/right/src/timer.c +++ b/right/src/timer.c @@ -3,9 +3,15 @@ static volatile uint32_t CurrentTime; +static volatile uint32_t delayLength; + void PIT_TIMER_HANDLER(void) { CurrentTime++; + + if (delayLength) { + --delayLength; + } PIT_ClearStatusFlags(PIT, PIT_TIMER_CHANNEL, PIT_TFLG_TIF_MASK); } @@ -45,3 +51,11 @@ uint32_t Timer_GetElapsedTimeAndSetCurrent(uint32_t *time) *time = CurrentTime; return elapsedTime; } + +void Timer_Delay(uint32_t length) +{ + delayLength = length; + while (delayLength) { + ; + } +} diff --git a/right/src/timer.h b/right/src/timer.h index 1ce3218..9555838 100644 --- a/right/src/timer.h +++ b/right/src/timer.h @@ -16,5 +16,6 @@ void Timer_SetCurrentTime(uint32_t *time); uint32_t Timer_GetElapsedTime(uint32_t *time); uint32_t Timer_GetElapsedTimeAndSetCurrent(uint32_t *time); + void Timer_Delay(uint32_t length); #endif From 0155447c6ab02211ea4a6165a4599e09784b3b13 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 24 Jun 2018 20:22:19 -0700 Subject: [PATCH 06/19] Rename InitTestLed to TestLed_Init --- left/src/init_peripherals.c | 2 +- left/src/test_led.c | 2 +- left/src/test_led.h | 2 +- right/src/init_peripherals.c | 2 +- right/src/peripherals/test_led.c | 2 +- right/src/peripherals/test_led.h | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/left/src/init_peripherals.c b/left/src/init_peripherals.c index ed352b2..e39916e 100644 --- a/left/src/init_peripherals.c +++ b/left/src/init_peripherals.c @@ -84,7 +84,7 @@ void InitPeripherals(void) { initInterruptPriorities(); InitLedDriver(); - InitTestLed(); + TestLed_Init(); LedPwm_Init(); DebugOverSpi_Init(); initI2c(); diff --git a/left/src/test_led.c b/left/src/test_led.c index acb24cd..a3bd5d1 100644 --- a/left/src/test_led.c +++ b/left/src/test_led.c @@ -1,7 +1,7 @@ #include "test_led.h" #include "fsl_port.h" -extern void InitTestLed(void) +extern void TestLed_Init(void) { CLOCK_EnableClock(TEST_LED_CLOCK); PORT_SetPinMux(TEST_LED_PORT, TEST_LED_PIN, kPORT_MuxAsGpio); diff --git a/left/src/test_led.h b/left/src/test_led.h index 2c3416f..0ca7fd3 100644 --- a/left/src/test_led.h +++ b/left/src/test_led.h @@ -22,6 +22,6 @@ // Functions: - void InitTestLed(void); + void TestLed_Init(void); #endif diff --git a/right/src/init_peripherals.c b/right/src/init_peripherals.c index e45afc7..7efcb20 100644 --- a/right/src/init_peripherals.c +++ b/right/src/init_peripherals.c @@ -156,7 +156,7 @@ void InitPeripherals(void) InitMergeSensor(); ADC_Init(); initI2c(); - InitTestLed(); + TestLed_Init(); LedPwm_Init(); InitI2cWatchdog(); InitKeyDebouncer(); diff --git a/right/src/peripherals/test_led.c b/right/src/peripherals/test_led.c index 50197e1..1ebfe11 100644 --- a/right/src/peripherals/test_led.c +++ b/right/src/peripherals/test_led.c @@ -1,7 +1,7 @@ #include "test_led.h" #include "fsl_port.h" -void InitTestLed(void) +void TestLed_Init(void) { CLOCK_EnableClock(TEST_LED_CLOCK); PORT_SetPinMux(TEST_LED_GPIO_PORT, TEST_LED_GPIO_PIN, kPORT_MuxAsGpio); diff --git a/right/src/peripherals/test_led.h b/right/src/peripherals/test_led.h index cd90365..1c1b7bf 100644 --- a/right/src/peripherals/test_led.h +++ b/right/src/peripherals/test_led.h @@ -22,6 +22,6 @@ // Functions: - void InitTestLed(void); + void TestLed_Init(void); #endif From c6b180b8f5be49bca75bae0dd7acb7390d8f443e Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 24 Jun 2018 20:34:23 -0700 Subject: [PATCH 07/19] Convert macros for controlling test LEDs to functions --- left/src/i2c_watchdog.c | 2 +- left/src/slave_protocol_handler.c | 2 +- left/src/test_led.c | 2 +- left/src/test_led.h | 19 +++++++++++---- right/src/key_debouncer.c | 2 +- right/src/peripherals/test_led.c | 2 +- right/src/peripherals/test_led.h | 23 +++++++++++++++---- .../usb_commands/usb_command_set_test_led.c | 2 +- 8 files changed, 40 insertions(+), 14 deletions(-) diff --git a/left/src/i2c_watchdog.c b/left/src/i2c_watchdog.c index ae7f20e..40718a6 100644 --- a/left/src/i2c_watchdog.c +++ b/left/src/i2c_watchdog.c @@ -21,7 +21,7 @@ void RunWatchdog(void) cntr++; if (cntr==100) { /* we get called from KEY_SCANNER_HANDLER() which runs at 1ms, thus scaling down by 100 here to get 100 ms period */ cntr=0; - TEST_LED_TOGGLE(); + TestLed_Toggle(); I2cWatchdog_WatchCounter++; if (I2cWatchdog_WatchCounter>10) { /* do not check within the first 1000 ms, as I2C might not be running yet */ diff --git a/left/src/slave_protocol_handler.c b/left/src/slave_protocol_handler.c index c52edc0..4ceb40f 100644 --- a/left/src/slave_protocol_handler.c +++ b/left/src/slave_protocol_handler.c @@ -42,7 +42,7 @@ void SlaveRxHandler(void) case SlaveCommand_SetTestLed: TxMessage.length = 0; bool isLedOn = RxMessage.data[1]; - TEST_LED_SET(isLedOn); + TestLed_Set(isLedOn); break; case SlaveCommand_SetLedPwmBrightness: TxMessage.length = 0; diff --git a/left/src/test_led.c b/left/src/test_led.c index a3bd5d1..0f084d3 100644 --- a/left/src/test_led.c +++ b/left/src/test_led.c @@ -6,5 +6,5 @@ extern void TestLed_Init(void) CLOCK_EnableClock(TEST_LED_CLOCK); PORT_SetPinMux(TEST_LED_PORT, TEST_LED_PIN, kPORT_MuxAsGpio); GPIO_PinInit(TEST_LED_GPIO, TEST_LED_PIN, &(gpio_pin_config_t){kGPIO_DigitalOutput, 0}); - TEST_LED_ON(); + TestLed_On(); } diff --git a/left/src/test_led.h b/left/src/test_led.h index 0ca7fd3..444e4d4 100644 --- a/left/src/test_led.h +++ b/left/src/test_led.h @@ -15,10 +15,21 @@ #define TEST_LED_CLOCK kCLOCK_PortB #define TEST_LED_PIN 13 - #define TEST_LED_ON() GPIO_SetPinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN) - #define TEST_LED_OFF() GPIO_ClearPinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN) - #define TEST_LED_SET(state) GPIO_WritePinOutput(TEST_LED_GPIO, TEST_LED_PIN, (state)) - #define TEST_LED_TOGGLE() GPIO_TogglePinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN) + static inline void TestLed_On(void) { + GPIO_SetPinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN); + } + + static inline void TestLed_Off(void) { + GPIO_ClearPinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN); + } + + static inline void TestLed_Set(bool state) { + GPIO_WritePinOutput(TEST_LED_GPIO, TEST_LED_PIN, state); + } + + static inline void TestLed_Toggle(void) { + GPIO_TogglePinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN); + } // Functions: diff --git a/right/src/key_debouncer.c b/right/src/key_debouncer.c index 8a85eb3..1ca0002 100644 --- a/right/src/key_debouncer.c +++ b/right/src/key_debouncer.c @@ -7,7 +7,7 @@ void PIT_KEY_DEBOUNCER_HANDLER(void) { - TEST_LED_TOGGLE(); + TestLed_Toggle(); for (uint8_t slotId=0; slotId Date: Sun, 24 Jun 2018 21:05:00 -0700 Subject: [PATCH 08/19] Add TestLed_Blink --- right/src/peripherals/test_led.c | 21 +++++++++++++++++++++ right/src/peripherals/test_led.h | 1 + 2 files changed, 22 insertions(+) diff --git a/right/src/peripherals/test_led.c b/right/src/peripherals/test_led.c index 03807e9..1d46a3e 100644 --- a/right/src/peripherals/test_led.c +++ b/right/src/peripherals/test_led.c @@ -1,5 +1,6 @@ #include "test_led.h" #include "fsl_port.h" +#include "timer.h" void TestLed_Init(void) { @@ -8,3 +9,23 @@ void TestLed_Init(void) GPIO_PinInit(TEST_LED_GPIO, TEST_LED_GPIO_PIN, &(gpio_pin_config_t){kGPIO_DigitalOutput, 1}); TestLed_On(); } + +void TestLed_Blink(uint8_t times) +{ + TestLed_Off(); + Timer_Delay(500); + if (!times) { + TestLed_On(); + Timer_Delay(500); + TestLed_Off(); + Timer_Delay(500); + return; + } + while (times--) { + TestLed_On(); + Timer_Delay(100); + TestLed_Off(); + Timer_Delay(100); + } + Timer_Delay(400); +} diff --git a/right/src/peripherals/test_led.h b/right/src/peripherals/test_led.h index a54e7a0..6e4efc1 100644 --- a/right/src/peripherals/test_led.h +++ b/right/src/peripherals/test_led.h @@ -38,5 +38,6 @@ // Functions: void TestLed_Init(void); + void TestLed_Blink(uint8_t times); #endif From c1f5a96e1b09256848e6d81107f21f075be23d2b Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Mon, 25 Jun 2018 22:13:27 -0700 Subject: [PATCH 09/19] Correctly pass pointers to the active reports to memcpy --- right/src/usb_report_updater.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index 108f9a5..fc6c132 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -295,10 +295,10 @@ static void updateActiveUsbReports(void) if (MacroPlaying) { Macros_ContinueMacro(); - memcpy(&ActiveUsbMouseReport, &MacroMouseReport, sizeof MacroMouseReport); - memcpy(&ActiveUsbBasicKeyboardReport, &MacroBasicKeyboardReport, sizeof MacroBasicKeyboardReport); - memcpy(&ActiveUsbMediaKeyboardReport, &MacroMediaKeyboardReport, sizeof MacroMediaKeyboardReport); - memcpy(&ActiveUsbSystemKeyboardReport, &MacroSystemKeyboardReport, sizeof MacroSystemKeyboardReport); + memcpy(ActiveUsbMouseReport, &MacroMouseReport, sizeof MacroMouseReport); + memcpy(ActiveUsbBasicKeyboardReport, &MacroBasicKeyboardReport, sizeof MacroBasicKeyboardReport); + memcpy(ActiveUsbMediaKeyboardReport, &MacroMediaKeyboardReport, sizeof MacroMediaKeyboardReport); + memcpy(ActiveUsbSystemKeyboardReport, &MacroSystemKeyboardReport, sizeof MacroSystemKeyboardReport); return; } From 314eb0d771e78146b45a058d64b3d4c511ca3730 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 26 Jun 2018 12:03:08 -0700 Subject: [PATCH 10/19] Stop macros from repeating --- right/src/usb_report_updater.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index fc6c132..ae65116 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -279,7 +279,9 @@ static void applyKeyAction(key_state_t *keyState, key_action_t *action) } break; case KeyActionType_PlayMacro: - Macros_StartMacro(action->playMacro.macroId); + if (!keyState->previous) { + Macros_StartMacro(action->playMacro.macroId); + } break; } } From 5988fce59b7f450a225781ba4f4dbbe0d7210d24 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 26 Jun 2018 12:36:58 -0700 Subject: [PATCH 11/19] Implement the delay macro action --- right/src/macros.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/right/src/macros.c b/right/src/macros.c index 24590d2..75bc73b 100644 --- a/right/src/macros.c +++ b/right/src/macros.c @@ -1,6 +1,7 @@ #include "macros.h" #include "config_parser/parse_macro.h" #include "config_parser/config_globals.h" +#include "timer.h" macro_reference_t AllMacros[MAX_MACRO_NUM]; uint8_t AllMacrosCount; @@ -242,11 +243,27 @@ bool processKeyMacroAction(void) return false; } +bool processDelayMacroAction(void) +{ + static bool inDelay; + static uint32_t delayStart; + + if (inDelay) { + if (Timer_GetElapsedTime(&delayStart) >= currentMacroAction.delay.delay) { + inDelay = false; + } + } else { + Timer_SetCurrentTime(&delayStart); + inDelay = true; + } + return inDelay; +} + bool processCurrentMacroAction(void) { switch (currentMacroAction.type) { case MacroActionType_Delay: - return false; + return processDelayMacroAction(); case MacroActionType_Key: return processKeyMacroAction(); case MacroActionType_MouseButton: From df8792a60d28fd0e0915ec9879c19041252cc309 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 26 Jun 2018 12:40:24 -0700 Subject: [PATCH 12/19] Fix code formatting --- left/src/test_led.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/left/src/test_led.h b/left/src/test_led.h index 444e4d4..7f4df88 100644 --- a/left/src/test_led.h +++ b/left/src/test_led.h @@ -15,19 +15,23 @@ #define TEST_LED_CLOCK kCLOCK_PortB #define TEST_LED_PIN 13 - static inline void TestLed_On(void) { + static inline void TestLed_On(void) + { GPIO_SetPinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN); } - static inline void TestLed_Off(void) { + static inline void TestLed_Off(void) + { GPIO_ClearPinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN); } - static inline void TestLed_Set(bool state) { + static inline void TestLed_Set(bool state) + { GPIO_WritePinOutput(TEST_LED_GPIO, TEST_LED_PIN, state); } - static inline void TestLed_Toggle(void) { + static inline void TestLed_Toggle(void) + { GPIO_TogglePinsOutput(TEST_LED_GPIO, 1U << TEST_LED_PIN); } From 4b9aa0860c45341d2601acfcbeadf33f2303df79 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 26 Jun 2018 12:52:56 -0700 Subject: [PATCH 13/19] Implement the mouse macro actions --- right/src/macros.c | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/right/src/macros.c b/right/src/macros.c index 75bc73b..71e5571 100644 --- a/right/src/macros.c +++ b/right/src/macros.c @@ -259,6 +259,44 @@ bool processDelayMacroAction(void) return inDelay; } +bool processMouseButtonAction(void) +{ + static bool pressStarted; + + switch (currentMacroAction.key.action) { + case MacroSubAction_Tap: + if (!pressStarted) { + pressStarted = true; + MacroMouseReport.buttons |= currentMacroAction.mouseButton.mouseButtonsMask; + return true; + } + pressStarted = false; + MacroMouseReport.buttons &= ~currentMacroAction.mouseButton.mouseButtonsMask; + break; + case MacroSubAction_Release: + MacroMouseReport.buttons &= ~currentMacroAction.mouseButton.mouseButtonsMask; + break; + case MacroSubAction_Press: + MacroMouseReport.buttons |= currentMacroAction.mouseButton.mouseButtonsMask; + break; + } + return false; +} + +bool processMoveMouseAction(void) +{ + MacroMouseReport.x = currentMacroAction.moveMouse.x; + MacroMouseReport.y = currentMacroAction.moveMouse.y; + return false; +} + +bool processScrollMouseAction(void) +{ + MacroMouseReport.wheelX = currentMacroAction.scrollMouse.x; + MacroMouseReport.wheelY = currentMacroAction.scrollMouse.y; + return false; +} + bool processCurrentMacroAction(void) { switch (currentMacroAction.type) { @@ -267,11 +305,11 @@ bool processCurrentMacroAction(void) case MacroActionType_Key: return processKeyMacroAction(); case MacroActionType_MouseButton: - return false; + return processMouseButtonAction(); case MacroActionType_MoveMouse: - return false; + return processMoveMouseAction(); case MacroActionType_ScrollMouse: - return false; + return processScrollMouseAction(); case MacroActionType_Text: return false; } From 54b0a595bc022fbb545cbaa5bec7f4b396017663 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 26 Jun 2018 12:56:33 -0700 Subject: [PATCH 14/19] Rename functions for consistency --- right/src/macros.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/right/src/macros.c b/right/src/macros.c index 71e5571..82f59c1 100644 --- a/right/src/macros.c +++ b/right/src/macros.c @@ -215,7 +215,7 @@ void deleteScancode(uint16_t scancode, macro_sub_action_t type) } } -bool processKeyMacroAction(void) +bool processKeyAction(void) { static bool pressStarted; @@ -243,7 +243,7 @@ bool processKeyMacroAction(void) return false; } -bool processDelayMacroAction(void) +bool processDelayAction(void) { static bool inDelay; static uint32_t delayStart; @@ -301,9 +301,9 @@ bool processCurrentMacroAction(void) { switch (currentMacroAction.type) { case MacroActionType_Delay: - return processDelayMacroAction(); + return processDelayAction(); case MacroActionType_Key: - return processKeyMacroAction(); + return processKeyAction(); case MacroActionType_MouseButton: return processMouseButtonAction(); case MacroActionType_MoveMouse: From 76a91c010be21cae9763fca211068f7b051816de Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 26 Jun 2018 14:30:41 -0700 Subject: [PATCH 15/19] Implement the text macro action --- right/src/macros.c | 114 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 89 insertions(+), 25 deletions(-) diff --git a/right/src/macros.c b/right/src/macros.c index 82f59c1..2859c0b 100644 --- a/right/src/macros.c +++ b/right/src/macros.c @@ -19,69 +19,102 @@ uint8_t characterToScancode(char character) { switch (character) { case 'A' ... 'Z': - return 0; case 'a' ... 'z': - return 0; + return HID_KEYBOARD_SC_A - 1 + (character & 0x1F); case '1' ... '9': - return 0; + return HID_KEYBOARD_SC_1_AND_EXCLAMATION - 1 + (character & 0x0F); case ')': case '0': - return 0; + return HID_KEYBOARD_SC_0_AND_CLOSING_PARENTHESIS; case '!': - return 0; + return HID_KEYBOARD_SC_1_AND_EXCLAMATION; case '@': - return 0; + return HID_KEYBOARD_SC_2_AND_AT; case '#': - return 0; + return HID_KEYBOARD_SC_3_AND_HASHMARK; case '$': - return 0; + return HID_KEYBOARD_SC_4_AND_DOLLAR; case '%': - return 0; + return HID_KEYBOARD_SC_5_AND_PERCENTAGE; case '^': - return 0; + return HID_KEYBOARD_SC_6_AND_CARET; case '&': - return 0; + return HID_KEYBOARD_SC_7_AND_AMPERSAND; case '*': - return 0; + return HID_KEYBOARD_SC_8_AND_ASTERISK; case '(': - return 0; + return HID_KEYBOARD_SC_9_AND_OPENING_PARENTHESIS; case '`': case '~': - return 0; + return HID_KEYBOARD_SC_GRAVE_ACCENT_AND_TILDE; case '[': case '{': - return 0; + return HID_KEYBOARD_SC_OPENING_BRACKET_AND_OPENING_BRACE; case ']': case '}': - return 0; + return HID_KEYBOARD_SC_CLOSING_BRACKET_AND_CLOSING_BRACE; case ';': case ':': - return 0; + return HID_KEYBOARD_SC_SEMICOLON_AND_COLON; case '\'': case '\"': - return 0; + return HID_KEYBOARD_SC_APOSTROPHE_AND_QUOTE; case '+': case '=': - return 0; + return HID_KEYBOARD_SC_EQUAL_AND_PLUS; case '\\': case '|': - return 0; + return HID_KEYBOARD_SC_BACKSLASH_AND_PIPE; case '.': case '>': - return 0; + return HID_KEYBOARD_SC_DOT_AND_GREATER_THAN_SIGN; case ',': case '<': - return 0; + return HID_KEYBOARD_SC_KEYPAD_LESS_THAN_SIGN; case '/': case '\?': - return 0; + return HID_KEYBOARD_SC_SLASH_AND_QUESTION_MARK; case '-': case '_': - return 0; + return HID_KEYBOARD_SC_MINUS_AND_UNDERSCORE; + case '\n': + return HID_KEYBOARD_SC_ENTER; + case ' ': + return HID_KEYBOARD_SC_SPACE; } return 0; } +bool characterToShift(char character) +{ + switch (character) { + case 'A' ... 'Z': + case ')': + case '!': + case '@': + case '#': + case '$': + case '%': + case '^': + case '&': + case '*': + case '(': + case '~': + case '{': + case '}': + case ':': + case '\"': + case '+': + case '|': + case '>': + case '<': + case '\?': + case '_': + return true; + } + return false; +} + void addBasicScancode(uint8_t scancode) { if (!scancode) { @@ -297,6 +330,37 @@ bool processScrollMouseAction(void) return false; } +bool processTextAction(void) +{ + static uint16_t textIndex; + static uint8_t reportIndex = USB_BASIC_KEYBOARD_MAX_KEYS; + char character; + uint8_t scancode; + + if (textIndex == currentMacroAction.text.textLen) { + textIndex = 0; + reportIndex = USB_BASIC_KEYBOARD_MAX_KEYS; + return false; + } + if (reportIndex == USB_BASIC_KEYBOARD_MAX_KEYS) { + reportIndex = 0; + memset(&MacroBasicKeyboardReport, 0, sizeof MacroBasicKeyboardReport); + return true; + } + character = currentMacroAction.text.text[textIndex]; + scancode = characterToScancode(character); + for (uint8_t i = 0; i < reportIndex; i++) { + if (MacroBasicKeyboardReport.scancodes[i] == scancode) { + reportIndex = USB_BASIC_KEYBOARD_MAX_KEYS; + return true; + } + } + MacroBasicKeyboardReport.scancodes[reportIndex++] = scancode; + MacroBasicKeyboardReport.modifiers = characterToShift(character) ? HID_KEYBOARD_MODIFIER_LEFTSHIFT : 0; + ++textIndex; + return true; +} + bool processCurrentMacroAction(void) { switch (currentMacroAction.type) { @@ -311,7 +375,7 @@ bool processCurrentMacroAction(void) case MacroActionType_ScrollMouse: return processScrollMouseAction(); case MacroActionType_Text: - return false; + return processTextAction(); } return false; } From d35a7cc6444617eb4b800bce24f8abb481e255b2 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 26 Jun 2018 15:14:21 -0700 Subject: [PATCH 16/19] Clear the report at the end of a text macro --- right/src/macros.c | 1 + 1 file changed, 1 insertion(+) diff --git a/right/src/macros.c b/right/src/macros.c index 2859c0b..99839c6 100644 --- a/right/src/macros.c +++ b/right/src/macros.c @@ -340,6 +340,7 @@ bool processTextAction(void) if (textIndex == currentMacroAction.text.textLen) { textIndex = 0; reportIndex = USB_BASIC_KEYBOARD_MAX_KEYS; + memset(&MacroBasicKeyboardReport, 0, sizeof MacroBasicKeyboardReport); return false; } if (reportIndex == USB_BASIC_KEYBOARD_MAX_KEYS) { From 633a6cec777b156271d719de32c041410273c5a8 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 26 Jun 2018 15:32:16 -0700 Subject: [PATCH 17/19] Clear the report at the end of a move mouse or scroll mouse macro --- right/src/macros.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/right/src/macros.c b/right/src/macros.c index 99839c6..558c4ea 100644 --- a/right/src/macros.c +++ b/right/src/macros.c @@ -318,16 +318,32 @@ bool processMouseButtonAction(void) bool processMoveMouseAction(void) { - MacroMouseReport.x = currentMacroAction.moveMouse.x; - MacroMouseReport.y = currentMacroAction.moveMouse.y; - return false; + static bool inMotion; + + if (inMotion) { + memset(&MacroMouseReport, 0, sizeof MacroMouseReport); + inMotion = false; + } else { + MacroMouseReport.x = currentMacroAction.moveMouse.x; + MacroMouseReport.y = currentMacroAction.moveMouse.y; + inMotion = true; + } + return inMotion; } bool processScrollMouseAction(void) { - MacroMouseReport.wheelX = currentMacroAction.scrollMouse.x; - MacroMouseReport.wheelY = currentMacroAction.scrollMouse.y; - return false; + static bool inMotion; + + if (inMotion) { + memset(&MacroMouseReport, 0, sizeof MacroMouseReport); + inMotion = false; + } else { + MacroMouseReport.wheelX = currentMacroAction.scrollMouse.x; + MacroMouseReport.wheelY = currentMacroAction.scrollMouse.y; + inMotion = true; + } + return inMotion; } bool processTextAction(void) From 0e9525ce9a5dea756afd6231b58792636eefc2da Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 26 Jun 2018 15:59:51 -0700 Subject: [PATCH 18/19] Don't clear the whole report --- right/src/macros.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/right/src/macros.c b/right/src/macros.c index 558c4ea..97df053 100644 --- a/right/src/macros.c +++ b/right/src/macros.c @@ -321,7 +321,8 @@ bool processMoveMouseAction(void) static bool inMotion; if (inMotion) { - memset(&MacroMouseReport, 0, sizeof MacroMouseReport); + MacroMouseReport.x = 0; + MacroMouseReport.y = 0; inMotion = false; } else { MacroMouseReport.x = currentMacroAction.moveMouse.x; @@ -336,7 +337,8 @@ bool processScrollMouseAction(void) static bool inMotion; if (inMotion) { - memset(&MacroMouseReport, 0, sizeof MacroMouseReport); + MacroMouseReport.wheelX = 0; + MacroMouseReport.wheelY = 0; inMotion = false; } else { MacroMouseReport.wheelX = currentMacroAction.scrollMouse.x; From 02bbeb717791432f1c51ec2b3a7721d2670c0f82 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Tue, 26 Jun 2018 22:13:13 -0700 Subject: [PATCH 19/19] Fix the formatting in timer.c --- right/src/timer.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/right/src/timer.c b/right/src/timer.c index 43779cd..671ec06 100644 --- a/right/src/timer.c +++ b/right/src/timer.c @@ -1,14 +1,11 @@ #include "fsl_pit.h" #include "timer.h" -static volatile uint32_t CurrentTime; - -static volatile uint32_t delayLength; +static volatile uint32_t currentTime, delayLength; void PIT_TIMER_HANDLER(void) { - CurrentTime++; - + currentTime++; if (delayLength) { --delayLength; } @@ -31,24 +28,24 @@ void Timer_Init(void) } uint32_t Timer_GetCurrentTime() { - return CurrentTime; + return currentTime; } void Timer_SetCurrentTime(uint32_t *time) { - *time = CurrentTime; + *time = currentTime; } uint32_t Timer_GetElapsedTime(uint32_t *time) { - uint32_t elapsedTime = CurrentTime - *time; + uint32_t elapsedTime = currentTime - *time; return elapsedTime; } uint32_t Timer_GetElapsedTimeAndSetCurrent(uint32_t *time) { uint32_t elapsedTime = Timer_GetElapsedTime(time); - *time = CurrentTime; + *time = currentTime; return elapsedTime; }