]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/ergodox/keymap.c
Merge remote-tracking branch 'tmk/master' into cub_layout
[tmk_firmware.git] / keyboard / ergodox / keymap.c
1 /*
2 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include <avr/pgmspace.h>
20 #include <util/delay.h>
21 #include "keycode.h"
22 #include "action.h"
23 #include "action_code.h"
24 #include "action_macro.h"
25 #include "bootloader.h"
26 #include "report.h"
27 #include "host.h"
28 #include "print.h"
29 #include "debug.h"
30 #include "keymap.h"
31 #include "ergodox.h"
32
33
34 /* ErgoDox keymap definition macro */
35 #define KEYMAP(                                                 \
36                                                                 \
37     /* left hand, spatial positions */                          \
38     k00,k01,k02,k03,k04,k05,k06,                                \
39     k10,k11,k12,k13,k14,k15,k16,                                \
40     k20,k21,k22,k23,k24,k25,                                    \
41     k30,k31,k32,k33,k34,k35,k36,                                \
42     k40,k41,k42,k43,k44,                                        \
43                             k55,k56,                            \
44                                 k54,                            \
45                         k53,k52,k51,                            \
46                                                                 \
47     /* right hand, spatial positions */                         \
48         k07,k08,k09,k0A,k0B,k0C,k0D,                            \
49         k17,k18,k19,k1A,k1B,k1C,k1D,                            \
50             k28,k29,k2A,k2B,k2C,k2D,                            \
51         k37,k38,k39,k3A,k3B,k3C,k3D,                            \
52                 k49,k4A,k4B,k4C,k4D,                            \
53     k57,k58,                                                    \
54     k59,                                                        \
55     k5C,k5B,k5A )                                               \
56                                                                 \
57    /* matrix positions */                                       \
58    {                                                            \
59     { KC_##k00,KC_##k10,KC_##k20,KC_##k30,KC_##k40,KC_NO   },   \
60     { KC_##k01,KC_##k11,KC_##k21,KC_##k31,KC_##k41,KC_##k51},   \
61     { KC_##k02,KC_##k12,KC_##k22,KC_##k32,KC_##k42,KC_##k52},   \
62     { KC_##k03,KC_##k13,KC_##k23,KC_##k33,KC_##k43,KC_##k53},   \
63     { KC_##k04,KC_##k14,KC_##k24,KC_##k34,KC_##k44,KC_##k54},   \
64     { KC_##k05,KC_##k15,KC_##k25,KC_##k35,KC_NO,   KC_##k55},   \
65     { KC_##k06,KC_##k16,KC_NO,   KC_##k36,KC_NO,   KC_##k56},   \
66                                                                 \
67     { KC_##k07,KC_##k17,KC_NO,   KC_##k37,KC_NO,   KC_##k57},   \
68     { KC_##k08,KC_##k18,KC_##k28,KC_##k38,KC_NO,   KC_##k58},   \
69     { KC_##k09,KC_##k19,KC_##k29,KC_##k39,KC_##k49,KC_##k59},   \
70     { KC_##k0A,KC_##k1A,KC_##k2A,KC_##k3A,KC_##k4A,KC_##k5A},   \
71     { KC_##k0B,KC_##k1B,KC_##k2B,KC_##k3B,KC_##k4B,KC_##k5B},   \
72     { KC_##k0C,KC_##k1C,KC_##k2C,KC_##k3C,KC_##k4C,KC_##k5C},   \
73     { KC_##k0D,KC_##k1D,KC_##k2D,KC_##k3D,KC_##k4D,KC_NO   }    \
74    }
75
76 #if defined(KEYMAP_DVORAK)
77 #include "keymap_dvorak.h"
78 #elif defined(KEYMAP_COLEMAK)
79 #include "keymap_colemak.h"
80 #elif defined(KEYMAP_WORKMAN)
81 #include "keymap_workman.h"
82 #elif defined(KEYMAP_CUB)
83 #include "keymap_cub.h"
84 #else
85
86 static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
87     KEYMAP(  // layer 0 : default
88         // left hand
89         EQL, 1,   2,   3,   4,   5,   ESC,
90         BSLS,Q,   W,   E,   R,   T,   FN2,
91         TAB, A,   S,   D,   F,   G,
92         LSFT,Z,   X,   C,   V,   B,   FN1,
93         LGUI,GRV, BSLS,LEFT,RGHT,
94                                       LCTL,LALT,
95                                            HOME,
96                                  BSPC,DEL, END,
97         // right hand
98              FN3, 6,   7,   8,   9,   0,   MINS,
99              LBRC,Y,   U,   I,   O,   P,   RBRC,
100                   H,   J,   K,   L,   SCLN,QUOT,
101              FN1, N,   M,   COMM,DOT, SLSH,RSFT,
102                        LEFT,DOWN,UP,  RGHT,RGUI,
103         RALT,RCTL,
104         PGUP,
105         PGDN,ENT, SPC
106     ),
107
108     KEYMAP(  // layer 1 : function and symbol keys
109         // left hand
110         TRNS,F1,  F2,  F3,  F4,  F5,  F11,
111         TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,FN4,
112         TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
113         TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
114         TRNS,TRNS,TRNS,TRNS,TRNS,
115                                       TRNS,TRNS,
116                                            TRNS,
117                                  TRNS,TRNS,TRNS,
118         // right hand
119              F12, F6,  F7,  F8,  F9,  F10, TRNS,
120              TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
121                   TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
122              TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
123                        TRNS,TRNS,TRNS,TRNS,TRNS,
124         TRNS,TRNS,
125         TRNS,
126         TRNS,TRNS,TRNS
127     ),
128
129     KEYMAP(  // layer 2 : keyboard functions
130         // left hand
131         FN0, TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
132         TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
133         TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
134         TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
135         TRNS,TRNS,TRNS,TRNS,TRNS,
136                                       TRNS,TRNS,
137                                            TRNS,
138                                  TRNS,TRNS,TRNS,
139         // right hand
140              FN4, TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
141              TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
142                   TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
143              TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
144                        TRNS,TRNS,TRNS,TRNS,TRNS,
145         TRNS,TRNS,
146         TRNS,
147         TRNS,TRNS,TRNS
148     ),
149
150     KEYMAP(  // layer 3: numpad
151         // left hand
152         TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
153         TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
154         TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
155         TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,
156         TRNS,TRNS,TRNS,TRNS,TRNS,
157                                       TRNS,TRNS,
158                                            TRNS,
159                                  TRNS,TRNS,TRNS,
160         // right hand
161              SLCK,NLCK,PSLS,PAST,PAST,PMNS,BSPC,
162              TRNS,NO,  P7,  P8,  P9,  PMNS,BSPC,
163                   NO,  P4,  P5,  P6,  PPLS,PENT,
164              TRNS,NO,  P1,  P2,  P3,  PPLS,PENT,
165                        P0,  PDOT,SLSH,PENT,PENT,
166         TRNS,TRNS,
167         TRNS,
168         TRNS,TRNS,TRNS
169     ),
170
171 };
172
173 /* id for user defined functions */
174 enum function_id {
175     TEENSY_KEY,
176 };
177
178 /*
179  * Fn action definition
180  */
181 static const uint16_t PROGMEM fn_actions[] = {
182     ACTION_FUNCTION(TEENSY_KEY),                    // FN0 - Teensy key
183     ACTION_LAYER_MOMENTARY(1),                      // FN1 - switch to Layer1
184     ACTION_LAYER_SET(2, ON_PRESS),                  // FN2 - push Layer2
185     ACTION_LAYER_SET(3, ON_PRESS),                  // FN3 - push Layer3
186     ACTION_LAYER_SET(0, ON_PRESS),                  // FN4 - push Layer0
187 };
188
189 void action_function(keyrecord_t *event, uint8_t id, uint8_t opt)
190 {
191     if (id == TEENSY_KEY) {
192         clear_keyboard();
193         print("\n\nJump to bootloader... ");
194         _delay_ms(250);
195         bootloader_jump(); // should not return
196         print("not supported.\n");
197     }
198 }
199
200 #endif
201
202
203 #define KEYMAPS_SIZE    (sizeof(keymaps) / sizeof(keymaps[0]))
204 #define FN_ACTIONS_SIZE (sizeof(fn_actions) / sizeof(fn_actions[0]))
205
206 /* translates key to keycode */
207 uint8_t keymap_key_to_keycode(uint8_t layer, key_t key)
208 {
209     if (layer < KEYMAPS_SIZE) {
210         return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]);
211     } else {
212         // fall back to layer 0
213         return pgm_read_byte(&keymaps[0][(key.row)][(key.col)]);
214     }
215 }
216
217 /* translates Fn keycode to action */
218 action_t keymap_fn_to_action(uint8_t keycode)
219 {
220     action_t action;
221     if (FN_INDEX(keycode) < FN_ACTIONS_SIZE) {
222         action.code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]);
223     } else {
224         action.code = ACTION_NO;
225     }
226     return action;
227 }
228