Skip to content

Commit

Permalink
Merge pull request #1392 from keyboardio/f/dynamic-macro-refactor
Browse files Browse the repository at this point in the history
Refactor  DynamicMacros->play to shave a fair number of bytes off the code size on AVR. Tested on an Atreus.
  • Loading branch information
obra authored Feb 24, 2024
2 parents 583045a + 4cbe584 commit 7c27ac1
Showing 1 changed file with 37 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,36 @@ void DynamicMacros::play(uint8_t macro_id) {
if (macro_id >= macro_count_)
return;

auto &storage = Runtime.storage();
// Define a lambda function for common key operations to reduce redundancy
auto setKeyAndAction = [this, &key, &macro, &pos]() {
// Keycode variants of actions don't have flags to set, but we want to make sure
// we're still initializing them properly.

key.setFlags((macro == MACRO_ACTION_STEP_KEYCODEDOWN || macro == MACRO_ACTION_STEP_KEYCODEUP || MACRO_ACTION_STEP_TAPCODE) ? 0
: Runtime.storage().read(pos++));

key.setKeyCode(Runtime.storage().read(pos++));

switch (macro) {
case MACRO_ACTION_STEP_KEYCODEDOWN:
case MACRO_ACTION_STEP_KEYDOWN:
this->press(key);
break;
case MACRO_ACTION_STEP_KEYCODEUP:
case MACRO_ACTION_STEP_KEYUP:
this->release(key);
break;
case MACRO_ACTION_STEP_TAP:
case MACRO_ACTION_STEP_TAPCODE:
this->tap(key);
break;
default:
break;
}
};


pos = storage_base_ + map_[macro_id];

while (pos < storage_base_ + storage_size_) {
Expand All @@ -126,60 +156,27 @@ void DynamicMacros::play(uint8_t macro_id) {
}

case MACRO_ACTION_STEP_KEYDOWN:
key.setFlags(Runtime.storage().read(pos++));
key.setKeyCode(Runtime.storage().read(pos++));
press(key);
break;
case MACRO_ACTION_STEP_KEYUP:
key.setFlags(Runtime.storage().read(pos++));
key.setKeyCode(Runtime.storage().read(pos++));
release(key);
break;
case MACRO_ACTION_STEP_TAP:
key.setFlags(Runtime.storage().read(pos++));
key.setKeyCode(Runtime.storage().read(pos++));
tap(key);
break;

case MACRO_ACTION_STEP_KEYCODEDOWN:
key.setFlags(0);
key.setKeyCode(Runtime.storage().read(pos++));
press(key);
break;
case MACRO_ACTION_STEP_KEYCODEUP:
key.setFlags(0);
key.setKeyCode(Runtime.storage().read(pos++));
release(key);
break;
case MACRO_ACTION_STEP_TAPCODE:
key.setFlags(0);
key.setKeyCode(Runtime.storage().read(pos++));
tap(key);
case MACRO_ACTION_STEP_KEYCODEDOWN:
setKeyAndAction();
break;

case MACRO_ACTION_STEP_TAP_SEQUENCE: {
while (true) {
key.setFlags(Runtime.storage().read(pos++));
key.setKeyCode(Runtime.storage().read(pos++));
if (key == Key_NoKey)
break;
tap(key);
delay(interval);
}
break;
}
case MACRO_ACTION_STEP_TAP_SEQUENCE:
case MACRO_ACTION_STEP_TAP_CODE_SEQUENCE: {
bool isKeycodeSequence = macro == MACRO_ACTION_STEP_TAP_CODE_SEQUENCE;
while (true) {
key.setFlags(0);
key.setKeyCode(Runtime.storage().read(pos++));
if (key.getKeyCode() == 0)
key.setFlags(isKeycodeSequence ? 0 : storage.read(pos++));
key.setKeyCode(storage.read(pos++));
if (key == Key_NoKey)
break;
tap(key);
delay(interval);
}
break;
}

case MACRO_ACTION_END:
default:
return;
Expand Down

0 comments on commit 7c27ac1

Please sign in to comment.