keymap.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. #include "ergodox_ez.h"
  2. #include "led.h"
  3. #include "debug.h"
  4. #include "action_layer.h"
  5. #include "action_util.h"
  6. #define BASE 0 // default layer
  7. #define SYMB 1 // symbols layer
  8. #define MDIA 2 // media layer
  9. #define SPEC 3 // special layer
  10. #define LCaps 10 // left caps-shift key
  11. #define LSymb 11 // left symbol-shift key
  12. #define LMdia 12 // left media-shift key
  13. #define LSpec 13 // left special-shift key
  14. #define RCaps 14 // right caps-shift key
  15. #define RSymb 15 // right symbol-shift key
  16. #define RMdia 16 // right media-shift key
  17. #define RSpec 17 // right special-shift key
  18. #define MUL 20 // mouse up left
  19. #define MUR 21 // mouse up right
  20. #define MDL 22 // mouse down left
  21. #define MDR 23 // mouse down right
  22. /*
  23. * The Ordinary Layout for the Ergodox EZ keyboard, v4
  24. *
  25. * Modifications from default by Nicholas Keene ergodoxez@nicholaskeene.com
  26. *
  27. * No rights reserved. This software is in the public domain.
  28. * Credit me if you are friendly but if you're a jerk don't bother.
  29. *
  30. * Details: readme.md
  31. * https://github.com/nrrkeene/qmk_firmware/tree/master/keyboard/ergodox_ez/keymaps/ordinary
  32. */
  33. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  34. /******* Base Layer ********************************************************************************************************
  35. *
  36. * ,------------------------------------------------------. ,------------------------------------------------------.
  37. * | Special `~ | 1 | 2 | 3 | 4 | 5 | ESC | | - | 6 | 7 | 8 | 9 | 0 | =+ Special |
  38. * |------------+------+------+------+------+-------------| |------+------+------+------+------+------+------------|
  39. * | Media Tab | Q | W | E | R | T | [ | | ] | Y | U | I | O | P | \| Media |
  40. * |------------+------+------+------+------+------| | | |------+------+------+------+------+------------|
  41. * | Symbol | A | S | D | F | G |------| |------| H | J | K | L | ; | '" Symbol |
  42. * |------------+------+------+------+------+------|Shift | | Tab |------+------+------+------+------+------------|
  43. * | Capitals | Z | X | C | V | B | -Tab | | | N | M | , | . | / | Capitals |
  44. * `------------+------+------+------+------+-------------' `-------------+------+------+------+------+------------'
  45. * | LCtrl | Meh |Hyper | LAlt | LGui | | RGui | RAlt | Hyper| Meh | RCtrl |
  46. * `-----------------------------------' `-----------------------------------'
  47. * ,-------------. ,-------------.
  48. * | Home | End | | Left | Right|
  49. * ,------|------|------| |------+------+------.
  50. * | | | PgUp | | Up | | |
  51. * |Backsp| Del |------| |------| Enter| Space|
  52. * | | | PgDn | | Down | | |
  53. * `--------------------' `--------------------'
  54. */
  55. [BASE] = KEYMAP(
  56. // left hand
  57. F(LSpec),KC_1,KC_2,KC_3 ,KC_4 ,KC_5 ,KC_ESC
  58. ,F(LMdia) ,KC_Q ,KC_W ,KC_E ,KC_R ,KC_T ,KC_LBRC
  59. ,M(LSymb) ,KC_A ,KC_S ,KC_D ,KC_F ,KC_G
  60. ,M(LCaps) ,KC_Z ,KC_X ,KC_C ,KC_V ,KC_B ,LSFT(KC_TAB)
  61. ,KC_LCTL ,KC_MEH ,KC_HYPR,KC_LALT,KC_LGUI
  62. ,KC_HOME,KC_END
  63. ,KC_PGUP
  64. ,KC_BSPC,KC_DEL ,KC_PGDN
  65. // right hand
  66. ,KC_MINS ,KC_6 ,KC_7 ,KC_8 ,KC_9 ,KC_0 ,F(RSpec)
  67. ,KC_RBRC ,KC_Y ,KC_U ,KC_I ,KC_O ,KC_P ,F(RMdia)
  68. ,KC_H ,KC_J ,KC_K ,KC_L ,KC_SCLN ,F(RSymb)
  69. ,KC_TAB ,KC_N ,KC_M ,KC_COMM,KC_DOT ,KC_SLSH ,M(RCaps)
  70. ,KC_RGUI,KC_RALT,KC_HYPR ,KC_MEH ,KC_RCTL
  71. ,KC_LEFT ,KC_RGHT
  72. ,KC_UP
  73. ,KC_DOWN ,KC_ENT ,KC_SPC
  74. ),
  75. /******* Symbols Layer *****************************************************************************************************
  76. *
  77. * ,-----------------------------------------------------. ,-----------------------------------------------------.
  78. * | | F1 | F2 | F3 | F4 | F5 | Esc | | - | F6 | F7 | F8 | F9 | F10 | |
  79. * |-----------+------+------+------+------+-------------| |------+------+------+------+------+------+-----------|
  80. * | | ! | @ | { | } | & | < | | > | | | 7 | 8 | 9 | / | |
  81. * |-----------+------+------+------+------+------| | | |------+------+------+------+------+-----------|
  82. * | | # | $ | ( | ) | ` |------| |------| / | 4 | 5 | 6 | * | |
  83. * |-----------+------+------+------+------+------| Tab | | Shift|------+------+------+------+------+-----------|
  84. * | | % | ^ | [ | ] | ~ | | | -Tab| \ | 1 | 2 | 3 | - | |
  85. * `-----------+------+------+------+------+-------------' `-------------+------+------+------+------+-----------'
  86. * | LCtrl | Meh |Hyper | LAlt | LGui | |RGui/0|RAlt/.|Hypr/=|Meh/+ |RCtrl/Ent|
  87. * `-----------------------------------' `-------------------------------------'
  88. * ,-------------. ,-------------.
  89. * | Left | Right| | Home | End |
  90. * ,------|------|------| |------+------+------.
  91. * | | | Up | | PgUp | | |
  92. * |Space |Enter |------| |------|BackSp| Del |
  93. * | | | Down | | PgDn | | |
  94. * `--------------------' `--------------------'
  95. */
  96. [SYMB] = KEYMAP(
  97. // left hand
  98. KC_TRNS ,KC_F1 ,KC_F2 ,KC_F3 ,KC_F4 ,KC_F5 ,KC_ESC
  99. ,KC_TRNS ,KC_EXLM ,KC_AT ,KC_LCBR ,KC_RCBR ,KC_AMPR ,LSFT(KC_COMM)
  100. ,KC_TRNS ,KC_HASH ,KC_DLR ,KC_LPRN ,KC_RPRN ,KC_GRV
  101. ,KC_TRNS ,KC_PERC ,KC_CIRC ,KC_LBRC ,KC_RBRC ,KC_TILD ,KC_TAB
  102. ,KC_LCTL ,KC_MEH ,KC_HYPR,KC_LALT ,KC_LGUI
  103. ,KC_LEFT ,KC_RGHT
  104. ,KC_UP
  105. ,KC_SPC ,KC_ENT ,KC_DOWN
  106. // right hand
  107. ,KC_MINS ,KC_F6 ,KC_F7 ,KC_F8 ,KC_F9 ,KC_F10 ,KC_TRNS
  108. ,LSFT(KC_DOT),KC_PIPE ,KC_7 ,KC_8 ,KC_9 ,KC_SLSH ,KC_TRNS
  109. ,KC_SLSH ,KC_4 ,KC_5 ,KC_6 ,KC_ASTR ,KC_TRNS
  110. ,LSFT(KC_TAB),KC_BSLS ,KC_1 ,KC_2 ,KC_3 ,KC_MINS ,KC_TRNS
  111. ,GUI_T(KC_0),ALT_T(KC_DOT),ALL_T(KC_EQL),MEH_T(KC_PLUS),CTL_T(KC_ENT)
  112. ,KC_HOME ,KC_END
  113. ,KC_PGUP
  114. ,KC_PGDN ,KC_BSPC ,KC_DEL
  115. ),
  116. /******* Media Layer *******************************************************************************************************
  117. *
  118. * ,---------------------------------------------------------------. ,---------------------------------------------------------------.
  119. * | | F11 | F12 | F13 | F14 | F15 | Esc | | | F16 | F17 | F18 | F19 | F20 | |
  120. * |------+---------+---------+---------+---------+----------------| |------+---------+---------+---------+---------+---------+------|
  121. * | |Shut Down|MouseUpLf|Mouse Up |MouseUpRg|Volume Up|Scroll| |Scroll|PrintScrn| Home | Up | PgUp | Mail | |
  122. * |------+---------+---------+---------+---------+---------| Up | | Up |---------+---------+---------+---------+---------+------|
  123. * | | Sleep |MouseLeft|MouseDown|MouseRght|Volume Dn|------| |------| Num Lock| Left | Down | Right | MyComp | |
  124. * |------+---------+---------+---------+---------+---------|Scroll| |Scroll|---------+---------+---------+---------+---------+------|
  125. * | | |MouseDnLf|MouseDown|MouseDnRg| Mute | Down | | Down | | End | Down | PgDn | | |
  126. * `------+---------+---------+---------+---------+----------------' `----------------+---------+---------+---------+---------+------'
  127. * |LCtrl| Meh | MClick | LClick | R Click| |Cmd/Insrt|Optn/Del | Hyper | Meh |RCtrl|
  128. * `---------------------------------------------' `---------------------------------------------'
  129. * ,-------------. ,-------------.
  130. * | Stop |Refrsh| | Prev | Next |
  131. * ,------|------|------| |------+------+------.
  132. * |Brwser|Brwser|Search| |VolUp | | |
  133. * |Back | Fwd |------| |------| Stop | Play-|
  134. * | | | Home | |VolDn | | Pause|
  135. * `--------------------' `--------------------'
  136. */
  137. [MDIA] = KEYMAP(
  138. // left hand
  139. KC_TRNS ,KC_F11 ,KC_F12 ,KC_F13 ,KC_F14 ,KC_F15 ,KC_ESC
  140. ,KC_TRNS ,KC_POWER ,M(MUL) ,KC_MS_U ,M(MUR) ,KC_VOLU ,KC_WH_U
  141. ,KC_TRNS ,KC_SLEP ,KC_MS_L ,KC_MS_D ,KC_MS_R ,KC_VOLD
  142. ,KC_TRNS ,KC_NO ,M(MDL) ,KC_MS_D ,M(MDR) ,KC_MUTE ,KC_WH_D
  143. ,KC_LCTL ,KC_MEH ,KC_BTN3 ,KC_BTN1 ,KC_BTN2
  144. ,KC_WSTP ,KC_WREF
  145. ,KC_WSCH
  146. ,KC_WBAK ,KC_NO ,KC_WHOM
  147. // right hand
  148. ,KC_NO ,KC_F16 ,KC_F17 ,KC_F18 ,KC_F19 ,KC_F20 ,KC_TRNS
  149. ,KC_WH_U ,KC_PSCR ,KC_HOME ,KC_UP ,KC_PGUP ,KC_MAIL ,KC_TRNS
  150. ,KC_NLCK ,KC_LEFT ,KC_DOWN ,KC_RIGHT,KC_MYCM ,KC_TRNS
  151. ,KC_WH_D ,KC_NO ,KC_END ,KC_DOWN ,KC_PGDN ,KC_NO ,KC_TRNS
  152. ,GUI_T(KC_INS),ALT_T(KC_DEL),KC_HYPR ,KC_MEH ,KC_RCTL
  153. ,KC_MPRV ,KC_MNXT
  154. ,KC_VOLU
  155. ,KC_VOLD ,KC_MSTP ,KC_MPLY
  156. ),
  157. /******* Special Layer *****************************************************************************************************
  158. *
  159. * ,-----------------------------------------------------. ,-----------------------------------------------------.
  160. * | | Esc | | | | | | | | | | | | Bspc | |
  161. * |-----------+------+------+------+------+-------------| |------+------+------+------+------+------+-----------|
  162. * | | | | | | | | | | | | | | | |
  163. * |-----------+------+------+------+------+------| | | |------+------+------+------+------+-----------|
  164. * | | | | | | |------| |------| | | | | | |
  165. * |-----------+------+------+------+------+------| | | |------+------+------+------+------+-----------|
  166. * | | | | | | | | | | | | | | | RShift |
  167. * `-----------+------+------+------+------+-------------' `-------------+------+------+------+------+-----------'
  168. * | | | | | | | | | | | |
  169. * `----------------------------------' `----------------------------------'
  170. * ,-------------. ,-------------.
  171. * | | | | | |
  172. * ,------|------|------| |------+------+------.
  173. * | | | | | | | |
  174. * | | |------| |------| | |
  175. * | | | | | | | |
  176. * `--------------------' `--------------------'
  177. */
  178. [SPEC] = KEYMAP(
  179. // left hand
  180. KC_TRNS ,KC_ESC ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS
  181. ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS
  182. ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS
  183. ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS
  184. ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS
  185. ,KC_TRNS ,KC_TRNS
  186. ,KC_TRNS
  187. ,KC_TRNS,KC_TRNS ,KC_TRNS
  188. // right hand
  189. ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_BSPC ,KC_TRNS
  190. ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS
  191. ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS
  192. ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_RSFT
  193. ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS ,KC_TRNS
  194. ,KC_TRNS ,KC_TRNS
  195. ,KC_TRNS
  196. ,KC_TRNS ,KC_TRNS ,KC_TRNS
  197. )
  198. };
  199. const uint16_t PROGMEM fn_actions[] = {
  200. // the faux shift keys are implemented as macro taps
  201. [LCaps] = ACTION_MACRO_TAP(LCaps)
  202. ,[LSymb] = ACTION_MACRO_TAP(LSymb)
  203. ,[LMdia] = ACTION_MACRO_TAP(LMdia)
  204. ,[LSpec] = ACTION_MACRO_TAP(LSpec)
  205. ,[RCaps] = ACTION_MACRO_TAP(RCaps)
  206. ,[RSymb] = ACTION_MACRO_TAP(RSymb)
  207. ,[RMdia] = ACTION_MACRO_TAP(RMdia)
  208. ,[RSpec] = ACTION_MACRO_TAP(RSpec)
  209. };
  210. uint16_t caps_shift = 0;
  211. uint16_t symb_shift = 0;
  212. uint16_t mdia_shift = 0;
  213. bool symb_lock = false;
  214. bool mdia_lock = false;
  215. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  216. {
  217. switch(id) {
  218. // There are two shift keys for each layer so we increment a layer_shift var when one
  219. // is pressed and decrement when one is released. If both are pressed at the same time
  220. // then the layer is locked (or unlocked). The shift counts are bound between 0 and 2
  221. // only because sometimes rapid pressing led to irregular events; this way the states
  222. // are self healing during use.
  223. case LCaps: // both caps-shift keys trigger Left Shift
  224. case RCaps: // so they don't interfere with the magic combo
  225. if (record->event.pressed && !record->tap.count) {
  226. if(++caps_shift > 2) caps_shift = 2;
  227. if(caps_shift == 2) {
  228. register_code(KC_CAPS);
  229. unregister_code(KC_CAPS);
  230. } else if(caps_shift == 1) {
  231. register_code(KC_LSFT);
  232. }
  233. } else {
  234. if(--caps_shift < 0) caps_shift = 0;
  235. if(caps_shift == 0) unregister_code(KC_LSFT);
  236. }
  237. break;
  238. case LSymb:
  239. if (record->event.pressed) {
  240. if(++symb_shift > 2) symb_shift = 2;
  241. if(symb_shift == 2) {
  242. symb_lock = !symb_lock;
  243. } else if(symb_shift == 1) {
  244. layer_on(SYMB);
  245. }
  246. } else {
  247. if(--symb_shift < 0) symb_shift = 0;
  248. if((symb_shift == 0) && (!symb_lock)) layer_off(SYMB);
  249. }
  250. break;
  251. case LMdia:
  252. if (record->event.pressed) {
  253. if (record->tap.count && (!mdia_shift) && (!mdia_lock)) {
  254. register_code(KC_TAB);
  255. } else {
  256. if(++mdia_shift > 2) mdia_shift = 2;
  257. if(mdia_shift == 2) {
  258. mdia_lock = !mdia_lock;
  259. } else if(mdia_shift == 1) {
  260. layer_on(MDIA);
  261. }
  262. }
  263. } else {
  264. if(record->tap.count && (!mdia_shift) && (!mdia_lock)) {
  265. unregister_code(KC_TAB);
  266. } else {
  267. if(--mdia_shift < 0) mdia_shift = 0;
  268. if((!mdia_shift) && (!mdia_lock)) layer_off(MDIA);
  269. }
  270. }
  271. break;
  272. case LSpec:
  273. if (record->event.pressed) {
  274. if (record->tap.count && !record->tap.interrupted) {
  275. register_code(KC_GRV);
  276. } else {
  277. layer_on(SPEC);
  278. }
  279. } else {
  280. if(record->tap.count && !record->tap.interrupted) {
  281. unregister_code(KC_GRV);
  282. } else {
  283. layer_off(SPEC);
  284. }
  285. }
  286. break;
  287. case RSymb:
  288. if (record->event.pressed) {
  289. if (record->tap.count && (!symb_shift) && (!symb_lock)) {
  290. register_code(KC_QUOT);
  291. } else {
  292. if(++symb_shift > 2) symb_shift = 2;
  293. if(symb_shift == 2) {
  294. symb_lock = !symb_lock;
  295. } else if(symb_shift == 1) {
  296. layer_on(SYMB);
  297. }
  298. }
  299. } else {
  300. if(record->tap.count && symb_shift == 0) {
  301. unregister_code(KC_QUOT);
  302. } else {
  303. if(--symb_shift < 0) symb_shift = 0;
  304. if((!symb_shift) && (!symb_lock)) layer_off(SYMB);
  305. }
  306. }
  307. break;
  308. case RMdia:
  309. if (record->event.pressed) {
  310. if (record->tap.count && (!mdia_shift) && (!mdia_lock)) {
  311. register_code(KC_BSLS);
  312. } else {
  313. if(++mdia_shift > 2) mdia_shift = 2;
  314. if(mdia_shift == 2) {
  315. mdia_lock = !mdia_lock;
  316. } else if(mdia_shift == 1) {
  317. layer_on(MDIA);
  318. }
  319. }
  320. } else {
  321. if(record->tap.count && (!mdia_shift) && (!mdia_lock)) {
  322. unregister_code(KC_BSLS);
  323. } else {
  324. if(--mdia_shift < 0) mdia_shift = 0;
  325. if((!mdia_shift) && (!mdia_lock)) layer_off(MDIA);
  326. }
  327. }
  328. break;
  329. case RSpec:
  330. if (record->event.pressed) {
  331. if (record->tap.count && !record->tap.interrupted) {
  332. register_code(KC_EQL);
  333. } else {
  334. layer_on(SPEC);
  335. }
  336. } else {
  337. if(record->tap.count && !record->tap.interrupted) {
  338. unregister_code(KC_EQL);
  339. } else {
  340. layer_off(SPEC);
  341. }
  342. }
  343. break;
  344. // mouse diagonals
  345. case MUL: // mouse up left
  346. if (record->event.pressed) {
  347. mousekey_on(KC_MS_UP);
  348. mousekey_on(KC_MS_LEFT);
  349. mousekey_send();
  350. } else {
  351. mousekey_off(KC_MS_UP);
  352. mousekey_off(KC_MS_LEFT);
  353. mousekey_send();
  354. }
  355. break;
  356. case MUR: // mouse up right
  357. if (record->event.pressed) {
  358. mousekey_on(KC_MS_UP);
  359. mousekey_on(KC_MS_RIGHT);
  360. mousekey_send();
  361. } else {
  362. mousekey_off(KC_MS_UP);
  363. mousekey_off(KC_MS_RIGHT);
  364. mousekey_send();
  365. }
  366. break;
  367. case MDL: // mouse down left
  368. if (record->event.pressed) {
  369. mousekey_on(KC_MS_DOWN);
  370. mousekey_on(KC_MS_LEFT);
  371. mousekey_send();
  372. } else {
  373. mousekey_off(KC_MS_DOWN);
  374. mousekey_off(KC_MS_LEFT);
  375. mousekey_send();
  376. }
  377. break;
  378. case MDR: // mouse down right
  379. if (record->event.pressed) {
  380. mousekey_on(KC_MS_DOWN);
  381. mousekey_on(KC_MS_RIGHT);
  382. mousekey_send();
  383. } else {
  384. mousekey_off(KC_MS_DOWN);
  385. mousekey_off(KC_MS_RIGHT);
  386. mousekey_send();
  387. }
  388. break;
  389. default:
  390. // none
  391. break;
  392. }
  393. return MACRO_NONE;
  394. };
  395. // Runs just one time when the keyboard initializes.
  396. void matrix_init_user(void) {
  397. };
  398. // Runs constantly in the background, in a loop.
  399. void matrix_scan_user(void) {
  400. // shift or caps lock turns on red light
  401. if(caps_shift || (keyboard_report->mods & MOD_BIT(KC_RSFT)) || (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK))) {
  402. ergodox_right_led_1_on();
  403. } else {
  404. ergodox_right_led_1_off();
  405. }
  406. // Symbol layer turns on green light
  407. if(layer_state & (1UL<<SYMB)) {
  408. ergodox_right_led_2_on();
  409. } else {
  410. ergodox_right_led_2_off();
  411. }
  412. // Media layer turns on blue light
  413. if(layer_state & (1UL<<MDIA)) {
  414. ergodox_right_led_3_on();
  415. } else {
  416. ergodox_right_led_3_off();
  417. }
  418. };