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 | |
parent | b382076ad1a6d857b6f185077b5f3635801b4ad6 (diff) | |
download | qmk_firmware-a173eda6d28bd09b2d59448a6532edb7a6c8e358.tar.gz qmk_firmware-a173eda6d28bd09b2d59448a6532edb7a6c8e358.zip |
Improved dynamic keymaps (#3972)
* Improved dynamic keymaps
* K&R sucks
-rw-r--r-- | keyboards/zeal60/zeal60.c | 9 | ||||
-rw-r--r-- | keyboards/zeal60/zeal60_api.h | 2 | ||||
-rw-r--r-- | quantum/dynamic_keymap.c | 42 | ||||
-rw-r--r-- | quantum/dynamic_keymap.h | 7 |
4 files changed, 22 insertions, 38 deletions
diff --git a/keyboards/zeal60/zeal60.c b/keyboards/zeal60/zeal60.c index e516c4dbf..092235ca6 100644 --- a/keyboards/zeal60/zeal60.c +++ b/keyboards/zeal60/zeal60.c | |||
@@ -81,9 +81,9 @@ void raw_hid_receive( uint8_t *data, uint8_t length ) | |||
81 | dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] ); | 81 | dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] ); |
82 | break; | 82 | break; |
83 | } | 83 | } |
84 | case id_dynamic_keymap_clear_all: | 84 | case id_dynamic_keymap_reset: |
85 | { | 85 | { |
86 | dynamic_keymap_clear_all(); | 86 | dynamic_keymap_reset(); |
87 | break; | 87 | break; |
88 | } | 88 | } |
89 | #endif // DYNAMIC_KEYMAP_ENABLE | 89 | #endif // DYNAMIC_KEYMAP_ENABLE |
@@ -171,9 +171,8 @@ void matrix_init_kb(void) | |||
171 | #endif // RGB_BACKLIGHT_ENABLED | 171 | #endif // RGB_BACKLIGHT_ENABLED |
172 | 172 | ||
173 | #ifdef DYNAMIC_KEYMAP_ENABLE | 173 | #ifdef DYNAMIC_KEYMAP_ENABLE |
174 | // This saves "empty" keymaps so it falls back to the keymaps | 174 | // This resets the keymaps in EEPROM to what is in flash. |
175 | // in the firmware (aka. progmem/flash) | 175 | dynamic_keymap_reset(); |
176 | dynamic_keymap_clear_all(); | ||
177 | #endif | 176 | #endif |
178 | 177 | ||
179 | // Save the magic number last, in case saving was interrupted | 178 | // Save the magic number last, in case saving was interrupted |
diff --git a/keyboards/zeal60/zeal60_api.h b/keyboards/zeal60/zeal60_api.h index baa8ac09f..eaac3ad7c 100644 --- a/keyboards/zeal60/zeal60_api.h +++ b/keyboards/zeal60/zeal60_api.h | |||
@@ -24,7 +24,7 @@ enum zeal60_command_id | |||
24 | id_set_keyboard_value, | 24 | id_set_keyboard_value, |
25 | id_dynamic_keymap_get_keycode, | 25 | id_dynamic_keymap_get_keycode, |
26 | id_dynamic_keymap_set_keycode, | 26 | id_dynamic_keymap_set_keycode, |
27 | id_dynamic_keymap_clear_all, | 27 | id_dynamic_keymap_reset, |
28 | id_backlight_config_set_value, | 28 | id_backlight_config_set_value, |
29 | id_backlight_config_get_value, | 29 | id_backlight_config_get_value, |
30 | id_backlight_config_save, | 30 | id_backlight_config_save, |
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 |
diff --git a/quantum/dynamic_keymap.h b/quantum/dynamic_keymap.h index b0133aeb8..bd76adae2 100644 --- a/quantum/dynamic_keymap.h +++ b/quantum/dynamic_keymap.h | |||
@@ -13,9 +13,7 @@ | |||
13 | * You should have received a copy of the GNU General Public License | 13 | * You should have received a copy of the GNU General Public License |
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 | */ | 15 | */ |
16 | 16 | #pragma once | |
17 | #ifndef DYNAMIC_KEYMAP_H | ||
18 | #define DYNAMIC_KEYMAP_H | ||
19 | 17 | ||
20 | #include <stdint.h> | 18 | #include <stdint.h> |
21 | #include <stdbool.h> | 19 | #include <stdbool.h> |
@@ -23,9 +21,8 @@ | |||
23 | void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column); | 21 | void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column); |
24 | uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column); | 22 | uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column); |
25 | void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode); | 23 | void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode); |
26 | void dynamic_keymap_clear_all(void); | 24 | void dynamic_keymap_reset(void); |
27 | 25 | ||
28 | // This overrides the one in quantum/keymap_common.c | 26 | // This overrides the one in quantum/keymap_common.c |
29 | // uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key); | 27 | // uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key); |
30 | 28 | ||
31 | #endif //DYNAMIC_KEYMAP_H | ||