From 55b035be86f835222a7ecf69b6bb270474d5af9f Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 27 Mar 2017 10:40:27 +0200 Subject: [PATCH 1/5] Introduce a way to describe a tap step Instead of having to use a keydown & keyup step each time we tap a key, use a combined event that does both. While this adds a tiny bit of code to `Macros.play`, if our macros have many key taps (which by and large the most common thing), we save a lot more. Three bytes per tap! Signed-off-by: Gergely Nagy --- src/Kaleidoscope-Macros.cpp | 8 ++++++++ src/MacroSteps.h | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Kaleidoscope-Macros.cpp b/src/Kaleidoscope-Macros.cpp index d9a271b2..128faf5c 100644 --- a/src/Kaleidoscope-Macros.cpp +++ b/src/Kaleidoscope-Macros.cpp @@ -37,6 +37,14 @@ void Macros_::play(const macro_t *macro_p) { handle_key_event(key, 255, 255, WAS_PRESSED | INJECTED); Keyboard.sendReport(); break; + case MACRO_ACTION_STEP_TAP: + key.flags = pgm_read_byte(macro_p++); + key.keyCode = pgm_read_byte(macro_p++); + handle_key_event(key, 255, 255, IS_PRESSED | INJECTED); + Keyboard.sendReport(); + handle_key_event(key, 255, 255, WAS_PRESSED | INJECTED); + Keyboard.sendReport(); + break; case END: default: return; diff --git a/src/MacroSteps.h b/src/MacroSteps.h index 2bef68cb..9a0fd379 100644 --- a/src/MacroSteps.h +++ b/src/MacroSteps.h @@ -7,6 +7,7 @@ typedef enum { MACRO_ACTION_STEP_WAIT, MACRO_ACTION_STEP_KEYDOWN, MACRO_ACTION_STEP_KEYUP, + MACRO_ACTION_STEP_TAP } MacroActionStepType; typedef uint8_t macro_t; @@ -21,6 +22,6 @@ typedef uint8_t macro_t; #define D(k) Dr(Key_ ## k) #define Ur(k) MACRO_ACTION_STEP_KEYUP, (k).flags, (k).keyCode #define U(k) Ur(Key_ ## k) -#define Tr(k) Dr(k), Ur(k) -#define T(k) D(k), U(k) +#define Tr(k) MACRO_ACTION_STEP_TAP, (k).flags, (k).keyCode +#define T(k) Tr(Key_ ## k) #define END MACRO_ACTION_END From f86700bdc3181051e7e28390b2dfed0a9466c8af Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 27 Mar 2017 10:53:09 +0200 Subject: [PATCH 2/5] Introduce new step variants The new step variants only use a one-byte argument, the `keyCode` part of a `Key`, and they implicitly set flags to zero. This allows us to make macros even more compact, by not having to use the flags when they are zero anyway. Signed-off-by: Gergely Nagy --- src/Kaleidoscope-Macros.cpp | 22 ++++++++++++++++++++++ src/MacroSteps.h | 13 ++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Kaleidoscope-Macros.cpp b/src/Kaleidoscope-Macros.cpp index 128faf5c..15f31adf 100644 --- a/src/Kaleidoscope-Macros.cpp +++ b/src/Kaleidoscope-Macros.cpp @@ -45,6 +45,28 @@ void Macros_::play(const macro_t *macro_p) { handle_key_event(key, 255, 255, WAS_PRESSED | INJECTED); Keyboard.sendReport(); break; + + case MACRO_ACTION_STEP_KEYCODEDOWN: + key.flags = 0; + key.keyCode = pgm_read_byte(macro_p++); + handle_key_event(key, 255, 255, IS_PRESSED | INJECTED); + Keyboard.sendReport(); + break; + case MACRO_ACTION_STEP_KEYCODEUP: + key.flags = 0; + key.keyCode = pgm_read_byte(macro_p++); + handle_key_event(key, 255, 255, WAS_PRESSED | INJECTED); + Keyboard.sendReport(); + break; + case MACRO_ACTION_STEP_TAPCODE: + key.flags = 0; + key.keyCode = pgm_read_byte(macro_p++); + handle_key_event(key, 255, 255, IS_PRESSED | INJECTED); + Keyboard.sendReport(); + handle_key_event(key, 255, 255, WAS_PRESSED | INJECTED); + Keyboard.sendReport(); + break; + case END: default: return; diff --git a/src/MacroSteps.h b/src/MacroSteps.h index 9a0fd379..7b92f526 100644 --- a/src/MacroSteps.h +++ b/src/MacroSteps.h @@ -5,9 +5,14 @@ typedef enum { MACRO_ACTION_STEP_INTERVAL, MACRO_ACTION_STEP_WAIT, + MACRO_ACTION_STEP_KEYDOWN, MACRO_ACTION_STEP_KEYUP, - MACRO_ACTION_STEP_TAP + MACRO_ACTION_STEP_TAP, + + MACRO_ACTION_STEP_KEYCODEDOWN, + MACRO_ACTION_STEP_KEYCODEUP, + MACRO_ACTION_STEP_TAPCODE, } MacroActionStepType; typedef uint8_t macro_t; @@ -18,10 +23,16 @@ typedef uint8_t macro_t; #define I(n) MACRO_ACTION_STEP_INTERVAL, n #define W(n) MACRO_ACTION_STEP_WAIT, n + #define Dr(k) MACRO_ACTION_STEP_KEYDOWN, (k).flags, (k).keyCode #define D(k) Dr(Key_ ## k) #define Ur(k) MACRO_ACTION_STEP_KEYUP, (k).flags, (k).keyCode #define U(k) Ur(Key_ ## k) #define Tr(k) MACRO_ACTION_STEP_TAP, (k).flags, (k).keyCode #define T(k) Tr(Key_ ## k) + +#define Dc(k) MACRO_ACTION_STEP_KEYCODEDOWN, (Key_ ## k).keyCode +#define Uc(k) MACRO_ACTION_STEP_KEYCODEUP, (Key_ ## k).keyCode +#define Tc(k) MACRO_ACTION_STEP_TAPCODE, (Key_ ## k).keyCode + #define END MACRO_ACTION_END From 7418eb92b6d87f80393ab61931c7694f7c3e2df5 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 27 Mar 2017 11:03:02 +0200 Subject: [PATCH 3/5] Code optimalization Lift out the keyCode reading, event handling, and report sending into a small helper function. Pretty much the same code has been called in a number of different cases, lifting them out into a common helper improves clarity, and reduces the size of the code, too. Signed-off-by: Gergely Nagy --- src/Kaleidoscope-Macros.cpp | 54 +++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/src/Kaleidoscope-Macros.cpp b/src/Kaleidoscope-Macros.cpp index 15f31adf..bcc9b7b2 100644 --- a/src/Kaleidoscope-Macros.cpp +++ b/src/Kaleidoscope-Macros.cpp @@ -7,10 +7,25 @@ const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) { byte Macros_::row, Macros_::col; +static void readAndPlay (const macro_t *macro_p, uint8_t flags, uint8_t keyStates) { + Key key; + key.flags = flags; + key.keyCode = pgm_read_byte(macro_p++); + + if (keyStates & IS_PRESSED) { + handle_key_event(key, 255, 255, IS_PRESSED | INJECTED); + Keyboard.sendReport(); + } + if (keyStates & WAS_PRESSED) { + handle_key_event(key, 255, 255, WAS_PRESSED | INJECTED); + Keyboard.sendReport(); + } +} + void Macros_::play(const macro_t *macro_p) { macro_t macro = END; uint8_t interval = 0; - Key key; + uint8_t flags; if (!macro_p) return; @@ -26,45 +41,26 @@ void Macros_::play(const macro_t *macro_p) { break; } case MACRO_ACTION_STEP_KEYDOWN: - key.flags = pgm_read_byte(macro_p++); - key.keyCode = pgm_read_byte(macro_p++); - handle_key_event(key, 255, 255, IS_PRESSED | INJECTED); - Keyboard.sendReport(); + flags = pgm_read_byte(macro_p++); + readAndPlay (macro_p++, flags, IS_PRESSED); break; case MACRO_ACTION_STEP_KEYUP: - key.flags = pgm_read_byte(macro_p++); - key.keyCode = pgm_read_byte(macro_p++); - handle_key_event(key, 255, 255, WAS_PRESSED | INJECTED); - Keyboard.sendReport(); + flags = pgm_read_byte(macro_p++); + readAndPlay (macro_p++, flags, WAS_PRESSED); break; case MACRO_ACTION_STEP_TAP: - key.flags = pgm_read_byte(macro_p++); - key.keyCode = pgm_read_byte(macro_p++); - handle_key_event(key, 255, 255, IS_PRESSED | INJECTED); - Keyboard.sendReport(); - handle_key_event(key, 255, 255, WAS_PRESSED | INJECTED); - Keyboard.sendReport(); + flags = pgm_read_byte(macro_p++); + readAndPlay (macro_p++, flags, IS_PRESSED | WAS_PRESSED); break; case MACRO_ACTION_STEP_KEYCODEDOWN: - key.flags = 0; - key.keyCode = pgm_read_byte(macro_p++); - handle_key_event(key, 255, 255, IS_PRESSED | INJECTED); - Keyboard.sendReport(); + readAndPlay (macro_p++, 0, IS_PRESSED); break; case MACRO_ACTION_STEP_KEYCODEUP: - key.flags = 0; - key.keyCode = pgm_read_byte(macro_p++); - handle_key_event(key, 255, 255, WAS_PRESSED | INJECTED); - Keyboard.sendReport(); + readAndPlay (macro_p++, 0, WAS_PRESSED); break; case MACRO_ACTION_STEP_TAPCODE: - key.flags = 0; - key.keyCode = pgm_read_byte(macro_p++); - handle_key_event(key, 255, 255, IS_PRESSED | INJECTED); - Keyboard.sendReport(); - handle_key_event(key, 255, 255, WAS_PRESSED | INJECTED); - Keyboard.sendReport(); + readAndPlay (macro_p++, 0, IS_PRESSED | WAS_PRESSED); break; case END: From de6cfa7f1c0b9de083b41df3a246f5408cd5d4e8 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 27 Mar 2017 11:05:43 +0200 Subject: [PATCH 4/5] Rename readAndPlay to readKeyCodeAndPlay The new name reflects it better what the function does. Signed-off-by: Gergely Nagy --- src/Kaleidoscope-Macros.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Kaleidoscope-Macros.cpp b/src/Kaleidoscope-Macros.cpp index bcc9b7b2..018fa540 100644 --- a/src/Kaleidoscope-Macros.cpp +++ b/src/Kaleidoscope-Macros.cpp @@ -7,7 +7,7 @@ const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) { byte Macros_::row, Macros_::col; -static void readAndPlay (const macro_t *macro_p, uint8_t flags, uint8_t keyStates) { +static void readKeyCodeAndPlay (const macro_t *macro_p, uint8_t flags, uint8_t keyStates) { Key key; key.flags = flags; key.keyCode = pgm_read_byte(macro_p++); @@ -42,25 +42,25 @@ void Macros_::play(const macro_t *macro_p) { } case MACRO_ACTION_STEP_KEYDOWN: flags = pgm_read_byte(macro_p++); - readAndPlay (macro_p++, flags, IS_PRESSED); + readKeyCodeAndPlay (macro_p++, flags, IS_PRESSED); break; case MACRO_ACTION_STEP_KEYUP: flags = pgm_read_byte(macro_p++); - readAndPlay (macro_p++, flags, WAS_PRESSED); + readKeyCodeAndPlay (macro_p++, flags, WAS_PRESSED); break; case MACRO_ACTION_STEP_TAP: flags = pgm_read_byte(macro_p++); - readAndPlay (macro_p++, flags, IS_PRESSED | WAS_PRESSED); + readKeyCodeAndPlay (macro_p++, flags, IS_PRESSED | WAS_PRESSED); break; case MACRO_ACTION_STEP_KEYCODEDOWN: - readAndPlay (macro_p++, 0, IS_PRESSED); + readKeyCodeAndPlay (macro_p++, 0, IS_PRESSED); break; case MACRO_ACTION_STEP_KEYCODEUP: - readAndPlay (macro_p++, 0, WAS_PRESSED); + readKeyCodeAndPlay (macro_p++, 0, WAS_PRESSED); break; case MACRO_ACTION_STEP_TAPCODE: - readAndPlay (macro_p++, 0, IS_PRESSED | WAS_PRESSED); + readKeyCodeAndPlay (macro_p++, 0, IS_PRESSED | WAS_PRESSED); break; case END: From 215d7de8ce0fd51801b166575663669b1bbcf94e Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 27 Mar 2017 22:39:57 +0200 Subject: [PATCH 5/5] Use UNKNOWN_KEYSWITCH_LOCATION instead of magic numbers Signed-off-by: Gergely Nagy --- src/Kaleidoscope-Macros.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Kaleidoscope-Macros.cpp b/src/Kaleidoscope-Macros.cpp index 018fa540..2e658a0c 100644 --- a/src/Kaleidoscope-Macros.cpp +++ b/src/Kaleidoscope-Macros.cpp @@ -13,11 +13,11 @@ static void readKeyCodeAndPlay (const macro_t *macro_p, uint8_t flags, uint8_t k key.keyCode = pgm_read_byte(macro_p++); if (keyStates & IS_PRESSED) { - handle_key_event(key, 255, 255, IS_PRESSED | INJECTED); + handle_key_event(key, UNKNOWN_KEYSWITCH_LOCATION, IS_PRESSED | INJECTED); Keyboard.sendReport(); } if (keyStates & WAS_PRESSED) { - handle_key_event(key, 255, 255, WAS_PRESSED | INJECTED); + handle_key_event(key, UNKNOWN_KEYSWITCH_LOCATION, WAS_PRESSED | INJECTED); Keyboard.sendReport(); } }