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

Introduce a new event: onLayerChange #363

Merged
merged 3 commits into from Nov 20, 2018
Merged

Introduce a new event: onLayerChange #363

merged 3 commits into from Nov 20, 2018

Conversation

algernon
Copy link
Contributor

@algernon algernon commented Oct 8, 2018

The intent is to make it easier for plugins to detect layer changes and schedule work accordingly. There event receives no arguments, the current state can always be queried with Layer.getLayerState(), and if a plugin requires the old state too, they can track that on their own.

@algernon algernon added the enhancement New feature or request label Oct 8, 2018
@algernon algernon requested a review from obra October 8, 2018 14:44
@algernon
Copy link
Contributor Author

algernon commented Oct 8, 2018

I plan to use this in ColorMap and possibly in ActiveModColor too. It may make sense to wait with going forward with this until I tried porting those two to use this new event - so that we can verify that this is sufficient. (I think it should be, but trying to port stuff to it will make me more certain.)

@obra
Copy link
Member

obra commented Oct 8, 2018

In theory, this make sense. :)

I'd love to know it it works in practice.

@algernon
Copy link
Contributor Author

algernon commented Oct 9, 2018

Tested with colormap, and... it doesn't buy us much there. The code becomes a little simpler, but speed is about the same (except I see higher latencies with the onLayerChange version than with the original when I'm on a higher layer, even though I shouldn't - need to debug this further).

Might be easier if onLayerChange received either old+new state, or the layer id that changed, but not by much.

@algernon
Copy link
Contributor Author

algernon commented Oct 9, 2018

Actually, scratch that. I see the spikes in both cases, so it is unrelated to onLayerChange (and likely unrelated to Colormap as well).

@algernon
Copy link
Contributor Author

algernon commented Oct 9, 2018

keyboardio/Kaleidoscope-Colormap@a7f6ebff1be373544743e79743267daf65b9b398 shows how Colormap would be ported to onLayerChange. We don't gain much there, just a tiny bit nicer code, and 8 bytes of PROGMEM.

I expect LED-ActiveModColor to provide much more significant benefits once switched. But in that case, most of the benefits will come from architectural changes, which would be possible without onLayerChange too, it just makes them easier.

@algernon
Copy link
Contributor Author

algernon commented Oct 9, 2018

keyboardio/Kaleidoscope-LED-ActiveModColor@ac3527b6e7c85e1b99fc3487689572d51bac3cac is the LED-ActiveModColor use case.

That one buys us ~0.7ms in the idle case, and ~0.3ms with a bunch of modifiers pressed, at the cost of 138 bytes of PROGMEM, and 17 bytes of RAM. I think the speedup is worth this much. Without onLayerChange, we'd use more PROGMEM and RAM 'cos we'd need to track layer changes ourselves, and the speedup would be smaller too (due to doing our own tracking). At that point, it might not be worth it.

@algernon
Copy link
Contributor Author

algernon commented Oct 9, 2018

There are currently no other plugins (under the keyboardio umbrella) that would benefit from this event.

@algernon
Copy link
Contributor Author

Another use case for this is user code, if someone wants to implement their own coloring logic, for example, then this makes it to do that in a simple way. It can be used to send back events to the host, which can then display a notification or somesuch.

I'll rebase this PR on top of current master.

@algernon algernon force-pushed the f/onLayerChange branch 3 times, most recently from 6b26a90 to 900aa9f Compare November 16, 2018 14:31
@algernon
Copy link
Contributor Author

The PR now includes the Colormap and ActiveModColor changes too.

@obra
Copy link
Member

obra commented Nov 20, 2018 via email

@algernon
Copy link
Contributor Author

As soon as I rebased and fixed the conflicts - yes.

The intent is to make it easier for plugins to detect layer changes and schedule
work accordingly. There event receives no arguments, the current state can
always be queried with `Layer.getLayerState()`, and if a plugin requires the old
state too, they can track that on their own.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
Instead of tracking layer changes ourselves, use the new `onLayerChange` event
to do that for us. This makes the code a tiny bit easier to follow.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
…change

Keys normally only change when switching layers, so instead of going through
every key in every cycle to look for modifiers, do that once, when layers
change (using the new `onLayerChange` event), and store the coordinates of keys
we consider modifiers in an array (currently limited to 16 items).

Then, when we want to highlight them, go over this array only, significantly
reducing the work we need to do. In a typical case, on a full-size keyboard, one
would have eight modifiers and a few layer keys, so instead of going through a
hundred keys, we go through sixteen at most, but usually considerably less.

This fixes #403, at the cost of noticeably higher PROGMEM and RAM use.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
@algernon
Copy link
Contributor Author

Rebased, ready to merge in my opinion.

@obra obra merged commit d4b2332 into master Nov 20, 2018
@obra obra deleted the f/onLayerChange branch November 20, 2018 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants