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

Improved dynamic keymaps #3972

Merged
merged 2 commits into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions keyboards/zeal60/zeal60.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ void raw_hid_receive( uint8_t *data, uint8_t length )
dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
break;
}
case id_dynamic_keymap_clear_all:
case id_dynamic_keymap_reset:
{
dynamic_keymap_clear_all();
dynamic_keymap_reset();
break;
}
#endif // DYNAMIC_KEYMAP_ENABLE
Expand Down Expand Up @@ -171,9 +171,8 @@ void matrix_init_kb(void)
#endif // RGB_BACKLIGHT_ENABLED

#ifdef DYNAMIC_KEYMAP_ENABLE
// This saves "empty" keymaps so it falls back to the keymaps
// in the firmware (aka. progmem/flash)
dynamic_keymap_clear_all();
// This resets the keymaps in EEPROM to what is in flash.
dynamic_keymap_reset();
#endif

// Save the magic number last, in case saving was interrupted
Expand Down
2 changes: 1 addition & 1 deletion keyboards/zeal60/zeal60_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum zeal60_command_id
id_set_keyboard_value,
id_dynamic_keymap_get_keycode,
id_dynamic_keymap_set_keycode,
id_dynamic_keymap_clear_all,
id_dynamic_keymap_reset,
id_backlight_config_set_value,
id_backlight_config_get_value,
id_backlight_config_save,
Expand Down
42 changes: 15 additions & 27 deletions quantum/dynamic_keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "config.h"
#include "keymap.h" // to get keymaps[][][]
#include "tmk_core/common/eeprom.h"
#include "progmem.h"// to read default from flash

#include "dynamic_keymap.h"

Expand All @@ -29,8 +31,6 @@
#error DYNAMIC_KEYMAP_LAYER_COUNT not defined
#endif

#define KC_EENULL 0xFFFF // TODO: move to enum quantum_keycodes

void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column)
{
// TODO: optimize this with some left shifts
Expand All @@ -55,16 +55,15 @@ void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint
eeprom_update_byte(address+1, (uint8_t)(keycode & 0xFF));
}

void dynamic_keymap_clear_all(void)
void dynamic_keymap_reset(void)
{
// Save "empty" keymaps.
for ( int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++ )
{
for ( int row = 0; row < MATRIX_ROWS; row++ )
{
for ( int column = 0; column < MATRIX_COLS; column++ )
{
dynamic_keymap_set_keycode(layer, row, column, KC_EENULL);
// Reset the keymaps in EEPROM to what is in flash.
// All keyboards using dynamic keymaps should define a layout
// for the same number of layers as DYNAMIC_KEYMAP_LAYER_COUNT.
for ( int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++ ) {
for ( int row = 0; row < MATRIX_ROWS; row++ ) {
for ( int column = 0; column < MATRIX_COLS; column++ ) {
dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column]));
}
}
}
Expand All @@ -73,24 +72,13 @@ void dynamic_keymap_clear_all(void)
// This overrides the one in quantum/keymap_common.c
uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
{
// This used to test EEPROM for magic bytes, but it was redundant.
// Test for EEPROM usage change (fresh install, address change, etc.)
// externally and call dynamic_keymap_default_save()
if ( layer < DYNAMIC_KEYMAP_LAYER_COUNT &&
key.row < MATRIX_ROWS && // possibly redundant
key.col < MATRIX_COLS ) // possibly redundant
{
uint16_t keycode = dynamic_keymap_get_keycode(layer, key.row, key.col);

// If keycode is not "empty", return it, otherwise
// drop down to return the one in flash
if ( keycode != KC_EENULL)
{
return keycode;
}
key.row < MATRIX_ROWS &&
key.col < MATRIX_COLS ) {
return dynamic_keymap_get_keycode(layer, key.row, key.col);
} else {
return KC_NO;
}

return pgm_read_word(&keymaps[layer][key.row][key.col]);
}
wilba marked this conversation as resolved.
Show resolved Hide resolved

#endif // DYNAMIC_KEYMAP_ENABLE
Expand Down
7 changes: 2 additions & 5 deletions quantum/dynamic_keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef DYNAMIC_KEYMAP_H
#define DYNAMIC_KEYMAP_H
#pragma once

#include <stdint.h>
#include <stdbool.h>

void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column);
uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column);
void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode);
void dynamic_keymap_clear_all(void);
void dynamic_keymap_reset(void);

// This overrides the one in quantum/keymap_common.c
// uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key);

#endif //DYNAMIC_KEYMAP_H