keymap.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright 2020 Sam Reinehr
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  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/>.
  15. */
  16. #include QMK_KEYBOARD_H
  17. enum my_keycodes {
  18. K00 = SAFE_RANGE,
  19. K01,
  20. K02,
  21. K03,
  22. K04,
  23. K05,
  24. K06,
  25. K07,
  26. K08,
  27. K09,
  28. K10,
  29. K11,
  30. K12,
  31. K13,
  32. K14,
  33. K15,
  34. };
  35. /* just a simple way to give each key a unique code */
  36. //clang-format off
  37. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  38. /* Base */
  39. [0] = LAYOUT_ortho_4x4(
  40. K00, K01, K02, K03,
  41. K04, K05, K06, K07,
  42. K08, K09, K10, K11,
  43. K12, K13, K14, K15
  44. )
  45. };
  46. /* flags describing current free square/0 */
  47. uint8_t current = 0;
  48. /* r g and b describe the colors for the initial map,
  49. currently blank for free, and evenly spaced hues with maximum sat/value */
  50. const uint8_t r[16] = {
  51. 0x00, 0xFF, 0xFF, 0xFF,
  52. 0xCC, 0x66, 0x00, 0x00,
  53. 0x00, 0x00, 0x00, 0x00,
  54. 0x66, 0xCC, 0xFF, 0xFF
  55. };
  56. const uint8_t g[16] = {
  57. 0x00, 0x00, 0x66, 0xCC,
  58. 0xFF, 0xFF, 0xFF, 0xFF,
  59. 0xFF, 0xCC, 0x66, 0x00,
  60. 0x00, 0x00, 0x00, 0x00
  61. };
  62. const uint8_t b[16] = {
  63. 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x66,
  65. 0xCC, 0xFF, 0xFF, 0xFF,
  66. 0xFF, 0xFF, 0xCC, 0x66
  67. };
  68. /* pos contains the current positions, could technically be compressed to 4 bits per, but not worth it
  69. index into pos is the position we're looking at, output is the tile that is currently there */
  70. uint8_t tiles[16] = {
  71. 0, 1, 2, 3,
  72. 4, 5, 6, 7,
  73. 8, 9, 10, 11,
  74. 12, 13, 14, 15
  75. };
  76. /* default led array for super 16 has them in a snake, so we must do some remapping/flipping of the 2nd and 4th rows */
  77. uint8_t remap[16] = {
  78. 0, 1, 2, 3,
  79. 7, 6, 5, 4,
  80. 8, 9, 10, 11,
  81. 15, 14, 13, 12
  82. };
  83. //clang-format on
  84. /* function to refresh the led coloring with the positions with current tiles */
  85. void refresh_leds(void) {
  86. for (uint8_t index = 0; index < 16; ++index) {
  87. uint8_t tile = tiles[index];
  88. setrgb(r[tile], g[tile], b[tile], (LED_TYPE *)&led[remap[index]]);
  89. }
  90. rgblight_set();
  91. }
  92. void keyboard_post_init_user(void) {
  93. rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  94. rgblight_enable_noeeprom();
  95. refresh_leds();
  96. }
  97. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  98. uint8_t offset = keycode - K00;
  99. uint8_t x = offset & 0x03;
  100. uint8_t y = (offset & 0x0C) >> 2;
  101. /* if the adjacent space exists and is empty, */
  102. if ((x > 0 && 0 == tiles[offset - 1]) || (y > 0 && 0 == tiles[offset - 4]) || (x < 3 && 0 == tiles[offset + 1]) || (y < 3 && 0 == tiles[offset + 4])) {
  103. /* set the currently blank tile to this tile, and make this one blank */
  104. tiles[current] = tiles[offset];
  105. tiles[offset] = 0;
  106. current = offset;
  107. }
  108. refresh_leds();
  109. return false;
  110. }