logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

keymap.c (3497B)


  1. /* Copyright 2023 Tom Parker-Shemilt <palfrey@tevp.net>
  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, version 3 of the License.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <stdlib.h>
  16. #include QMK_KEYBOARD_H
  17. // clang-format off
  18. // Exact keymap is irrelevant as we're using rows/cols
  19. // but we need _something_ set so we're using no-ops
  20. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  21. [0] = LAYOUT(
  22. KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,
  23. KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,
  24. KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,
  25. KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,
  26. KC_NO, KC_NO, KC_NO, KC_NO, KC_NO
  27. )};
  28. bool tiles[5][5] = {
  29. {false, false, false, false, false},
  30. {false, false, false, false, false},
  31. {false, false, false, false, false},
  32. {false, false, false, false, false},
  33. {false, false, false, false, false},
  34. };
  35. /* Because snake pattern of leds */
  36. const uint8_t remap[25] = {
  37. 20,21,22,23,24,
  38. 19,6,7,8,9,
  39. 18,5,0,1,10,
  40. 17,4,3,2,11,
  41. 16,15,14,13,12,
  42. };
  43. // clang-format on
  44. bool is_blank(void) {
  45. for (uint8_t y = 0; y < 5; y++) {
  46. for (uint8_t x = 0; x < 5; x++) {
  47. if (tiles[x][y]) {
  48. return false;
  49. }
  50. }
  51. }
  52. return true;
  53. }
  54. void do_move(uint8_t x, uint8_t y) {
  55. tiles[x][y] ^= true;
  56. if (x > 0) {
  57. tiles[x - 1][y] ^= true;
  58. }
  59. if (y > 0) {
  60. tiles[x][y - 1] ^= true;
  61. }
  62. if (x < 4) {
  63. tiles[x + 1][y] ^= true;
  64. }
  65. if (y < 4) {
  66. tiles[x][y + 1] ^= true;
  67. }
  68. }
  69. void refresh_leds(void) {
  70. for (uint8_t y = 0; y < 5; y++) {
  71. for (uint8_t x = 0; x < 5; x++) {
  72. uint8_t tile = tiles[x][y];
  73. uint8_t index = (y * 5) + x;
  74. if (tile) {
  75. rgblight_setrgb_at(RGB_RED, remap[index]);
  76. } else {
  77. rgblight_setrgb_at(RGB_WHITE, remap[index]);
  78. }
  79. }
  80. }
  81. rgblight_set();
  82. }
  83. uint8_t initial_moves = 1;
  84. void start_game(void) {
  85. rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
  86. srand(timer_read32());
  87. while (true) {
  88. for (uint8_t i = 0; i < initial_moves; i++) {
  89. do_move(rand() % 5, rand() % 5);
  90. }
  91. if (!is_blank()) {
  92. // Catch the "we picked the same location 2*N times" case
  93. break;
  94. }
  95. }
  96. refresh_leds();
  97. }
  98. void keyboard_post_init_user(void) {
  99. rgblight_enable_noeeprom();
  100. start_game();
  101. }
  102. bool won = false;
  103. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  104. if (record->event.pressed) {
  105. if (won) {
  106. initial_moves++;
  107. won = false;
  108. start_game();
  109. } else {
  110. uint8_t x = record->event.key.col;
  111. uint8_t y = record->event.key.row;
  112. do_move(x, y);
  113. if (is_blank()) {
  114. rgblight_mode(RGBLIGHT_MODE_RAINBOW_SWIRL);
  115. won = true;
  116. }
  117. }
  118. }
  119. refresh_leds();
  120. return true;
  121. }