Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(encoder): use behavior queue to archive key taps for encoder #1658

Closed
wants to merge 1 commit into from

Conversation

xqinx
Copy link

@xqinx xqinx commented Feb 10, 2023

Remove k_msleep() call used in sensor trigger callback that's meant to simulate a key tap (press and release), instead we can use the existing behavior queue mechanism, where a press and a release events are queued up separately. The trick is to add .binging_pressed and .binding_released callbacks for the sensor driver api, which will get called when the queued events are processed accordingly.

Board/Shield Check-list

  • This board/shield is tested working on real hardware
  • Definitions follow the general style of other shields/boards upstream (Reference)
  • .zmk.yml metadata file added
  • Proper Copyright + License headers added to applicable files (Generally, we stick to "The ZMK Contributors" for copyrights to help avoid churn when files get edited)
  • General consistent formatting of DeviceTree files
  • Keymaps do not use deprecated key defines (Check using the upgrader tool)
  • &pro_micro used in favor of &pro_micro_d/a if applicable
  • If split, no name added for the right/peripheral half
  • Kconfig.defconfig file correctly wraps all configuration in conditional on the shield symbol
  • .conf file has optional extra features commented out
  • Keyboard/PCB is part of a shipped group buy or is generally available in stock to purchase (OSH/personal projects without general availability should create a zmk-config repo instead)

Remove k_msleep() call used in sensor trigger callback that's meant to simulate a key
tap (press and release), instead we can use the existing behavior queue
mechanism, where a press and a release events are queued up separately.
The trick is to add .binging_pressed and .binding_released callbacks for
the sensor driver api, which will gets called when the queued events are
processed accordingly.
k_msleep(5);

return ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(keycode, false, timestamp));
zmk_behavior_queue_add(keycode_position, *binding, true, 20);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will delay both the press and release by 20 ms instead of immediately pressing and then releasing after 20 ms. Was that intended?


return ZMK_EVENT_RAISE(zmk_keycode_state_changed_from_encoded(keycode, false, timestamp));
zmk_behavior_queue_add(keycode_position, *binding, true, 20);
return zmk_behavior_queue_add(keycode_position, *binding, false, 20);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this hold duration configurable?

@@ -36,26 +73,22 @@ static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding,

switch (value.val1) {
case 1:
keycode = binding->param1;
keycode_position = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would cause every encoder to overlap with the second and third keys in the keymap.

I had a change of my own which was going to be part of a larger PR, but since it would fix this problem for you, I've split it out and created #1659. This gives each sensor a virtual key position which it can use for queueing behaviors.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1659 looks great! Thank you!

struct zmk_behavior_binding_event event) {
uint32_t keycode;

switch (event.position) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted below, using the position like this would make it appear to be the same key position as the second and third keys in the keymap.

Maybe a better way to handle this would be to rebase onto #1275, and then instead of giving the sensor behavior itself press/release functions and needing some way to identify whether a press/release corresponds to clockwise or counterclockwise, you would just queue up a press/release for the wrapped behavior?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, I didn't know about #1275, should have looked more carefully on outstanding PRs. #1275 is a better design and it also uses the behavior queue to achieve the same idea, I feel like this PR is redundant and we should probably just close this PR and wait on #1275. WDYT?

@xqinx
Copy link
Author

xqinx commented Feb 11, 2023

@joelspadin Thank you so much for reviewing and your feedback!

@xqinx xqinx closed this Jul 20, 2023
@xqinx
Copy link
Author

xqinx commented Jul 20, 2023

Closing this PR since #1758 (which contains #1275 changes) was merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants