diff options
| author | Wilba6582 <Wilba6582@users.noreply.github.com> | 2018-09-28 14:09:14 +1000 |
|---|---|---|
| committer | Jack Humbert <jack.humb@gmail.com> | 2018-09-28 00:09:14 -0400 |
| commit | a173eda6d28bd09b2d59448a6532edb7a6c8e358 (patch) | |
| tree | d8f1f40684eaf4dad48502fc75cd383d53b34011 /quantum/dynamic_keymap.c | |
| parent | b382076ad1a6d857b6f185077b5f3635801b4ad6 (diff) | |
| download | qmk_firmware-a173eda6d28bd09b2d59448a6532edb7a6c8e358.tar.gz qmk_firmware-a173eda6d28bd09b2d59448a6532edb7a6c8e358.zip | |
Improved dynamic keymaps (#3972)
* Improved dynamic keymaps
* K&R sucks
Diffstat (limited to 'quantum/dynamic_keymap.c')
| -rw-r--r-- | quantum/dynamic_keymap.c | 42 |
1 files changed, 15 insertions, 27 deletions
diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index 9f18612d5..2c989d691 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c | |||
| @@ -16,6 +16,8 @@ | |||
| 16 | 16 | ||
| 17 | #include "config.h" | 17 | #include "config.h" |
| 18 | #include "keymap.h" // to get keymaps[][][] | 18 | #include "keymap.h" // to get keymaps[][][] |
| 19 | #include "tmk_core/common/eeprom.h" | ||
| 20 | #include "progmem.h"// to read default from flash | ||
| 19 | 21 | ||
| 20 | #include "dynamic_keymap.h" | 22 | #include "dynamic_keymap.h" |
| 21 | 23 | ||
| @@ -29,8 +31,6 @@ | |||
| 29 | #error DYNAMIC_KEYMAP_LAYER_COUNT not defined | 31 | #error DYNAMIC_KEYMAP_LAYER_COUNT not defined |
| 30 | #endif | 32 | #endif |
| 31 | 33 | ||
| 32 | #define KC_EENULL 0xFFFF // TODO: move to enum quantum_keycodes | ||
| 33 | |||
| 34 | void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column) | 34 | void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column) |
| 35 | { | 35 | { |
| 36 | // TODO: optimize this with some left shifts | 36 | // TODO: optimize this with some left shifts |
| @@ -55,16 +55,15 @@ void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint | |||
| 55 | eeprom_update_byte(address+1, (uint8_t)(keycode & 0xFF)); | 55 | eeprom_update_byte(address+1, (uint8_t)(keycode & 0xFF)); |
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | void dynamic_keymap_clear_all(void) | 58 | void dynamic_keymap_reset(void) |
| 59 | { | 59 | { |
| 60 | // Save "empty" keymaps. | 60 | // Reset the keymaps in EEPROM to what is in flash. |
| 61 | for ( int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++ ) | 61 | // All keyboards using dynamic keymaps should define a layout |
| 62 | { | 62 | // for the same number of layers as DYNAMIC_KEYMAP_LAYER_COUNT. |
| 63 | for ( int row = 0; row < MATRIX_ROWS; row++ ) | 63 | for ( int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++ ) { |
| 64 | { | 64 | for ( int row = 0; row < MATRIX_ROWS; row++ ) { |
| 65 | for ( int column = 0; column < MATRIX_COLS; column++ ) | 65 | for ( int column = 0; column < MATRIX_COLS; column++ ) { |
| 66 | { | 66 | dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column])); |
| 67 | dynamic_keymap_set_keycode(layer, row, column, KC_EENULL); | ||
| 68 | } | 67 | } |
| 69 | } | 68 | } |
| 70 | } | 69 | } |
| @@ -73,24 +72,13 @@ void dynamic_keymap_clear_all(void) | |||
| 73 | // This overrides the one in quantum/keymap_common.c | 72 | // This overrides the one in quantum/keymap_common.c |
| 74 | uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) | 73 | uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) |
| 75 | { | 74 | { |
| 76 | // This used to test EEPROM for magic bytes, but it was redundant. | ||
| 77 | // Test for EEPROM usage change (fresh install, address change, etc.) | ||
| 78 | // externally and call dynamic_keymap_default_save() | ||
| 79 | if ( layer < DYNAMIC_KEYMAP_LAYER_COUNT && | 75 | if ( layer < DYNAMIC_KEYMAP_LAYER_COUNT && |
| 80 | key.row < MATRIX_ROWS && // possibly redundant | 76 | key.row < MATRIX_ROWS && |
| 81 | key.col < MATRIX_COLS ) // possibly redundant | 77 | key.col < MATRIX_COLS ) { |
| 82 | { | 78 | return dynamic_keymap_get_keycode(layer, key.row, key.col); |
| 83 | uint16_t keycode = dynamic_keymap_get_keycode(layer, key.row, key.col); | 79 | } else { |
| 84 | 80 | return KC_NO; | |
| 85 | // If keycode is not "empty", return it, otherwise | ||
| 86 | // drop down to return the one in flash | ||
| 87 | if ( keycode != KC_EENULL) | ||
| 88 | { | ||
| 89 | return keycode; | ||
| 90 | } | ||
| 91 | } | 81 | } |
| 92 | |||
| 93 | return pgm_read_word(&keymaps[layer][key.row][key.col]); | ||
| 94 | } | 82 | } |
| 95 | 83 | ||
| 96 | #endif // DYNAMIC_KEYMAP_ENABLE | 84 | #endif // DYNAMIC_KEYMAP_ENABLE |
