logo

qmk_firmware

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

macrocat.c (14383B)


  1. // Copyright 2022 catmunch (@catmunch)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "quantum.h"
  4. static bool encoder_pressed = 0;
  5. static bool encoder_switched_layer = 0;
  6. static uint32_t encoder_last_release_time = 0;
  7. static uint32_t encoder_press_combo = 0;
  8. static uint8_t current_layer = 0;
  9. #ifdef OLED_ENABLE
  10. static const char PROGMEM bongo_release[] = {
  11. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128,128,192,192, 96, 96, 48, 48, 24, 12, 6, 14, 60,112, 96,224,192,192,192,192,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  12. 0,192,192,128,128,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,192,240,124, 12, 12, 12, 12, 24,120,108, 14, 7, 3, 1, 0,128,192,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3, 7, 6, 14, 12, 28, 24, 48, 48, 24, 24, 12,252,252, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  13. 0, 0, 1, 1, 1, 1, 3, 3, 3, 3, 7, 6, 6, 6, 6, 12, 12, 15, 15, 24, 24, 24, 24, 48, 48, 48, 48,112, 96, 96, 96,224,193,193,193,192,131,134,134,134, 12, 12, 0, 0, 0, 0, 0, 48, 56, 16,240,252, 12, 6,134, 6, 12, 60, 48, 32, 0, 0, 48,126,239,131, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  14. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3, 3, 3, 3, 3, 6, 6, 6, 6, 12, 12, 15, 15, 24, 24, 24, 24, 56, 48, 48, 48,112, 96, 96, 96,225,207,254,248,192,128,128,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  15. 0,
  16. };
  17. static const char PROGMEM bongo_press_r[] = {
  18. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128,128,192,192, 96, 96, 48, 48, 24, 12, 6, 14, 60,112, 96,224,192,192,192,192,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  19. 0,192,192,128,128,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,192,240,124, 12, 12, 12, 12, 24,120,108, 14, 7, 3, 1, 0,128,192,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3, 7, 6, 14, 12, 28, 24, 48, 48, 24, 24, 12,252,252, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  20. 0, 0, 1, 1, 1, 1, 3, 3, 3, 3, 7, 6, 6, 6, 6, 12, 12, 15, 15, 24, 24, 24, 24, 48, 48, 48, 48,112, 96, 96, 96,224,193,193,193,192,131,134,134,134, 12, 12, 0, 0, 0, 0, 0, 48, 56, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48,126,239,131, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3, 3, 3, 3, 3, 6, 6, 22,126,111,227,192,224, 96, 96, 96, 96,112, 48, 48, 48,112, 96, 96, 96,225,207,254,248,192,128,128,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  22. 0,
  23. };
  24. static const char PROGMEM bongo_press_l[] = {
  25. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128,128,192,192, 96, 96, 48, 48, 24, 12, 6, 14, 60,112, 96,224,192,192,192,192,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. 0,192,192,128,128,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128,192, 96, 48, 24, 12, 14, 7, 3, 1, 0,128,192,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3, 7, 6, 14, 12, 28, 24, 48, 48, 24, 24, 12,252,252, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  27. 0, 0, 1, 1, 1, 1, 3, 3, 3, 3, 7, 6, 6, 6, 6, 12, 12,124,252,206,131,129,192,192,192,192,224, 96, 96, 96, 96,224,193,193,193,192,131,134,134,134, 12, 12, 0, 0, 0, 0, 0, 48, 56, 16,240,252, 12, 6,134, 6, 12, 60, 48, 32, 0, 0, 48,126,239,131, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3, 3, 3, 3, 3, 6, 6, 6, 6, 12, 12, 15, 15, 24, 24, 24, 24, 56, 48, 48, 48,112, 96, 96, 96,225,207,254,248,192,128,128,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29. 0,
  30. };
  31. static const char PROGMEM bongo_press_lr[] = {
  32. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128,128,192,192, 96, 96, 48, 48, 24, 12, 6, 14, 60,112, 96,224,192,192,192,192,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  33. 0,192,192,128,128,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,128,192, 96, 48, 24, 12, 14, 7, 3, 1, 0,128,192,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 3, 3, 7, 6, 14, 12, 28, 24, 48, 48, 24, 24, 12,252,252, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  34. 0, 0, 1, 1, 1, 1, 3, 3, 3, 3, 7, 6, 6, 6, 6, 12, 12,124,252,206,131,129,192,192,192,192,224, 96, 96, 96, 96,224,193,193,193,192,131,134,134,134, 12, 12, 0, 0, 0, 0, 0, 48, 56, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48,126,239,131, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  35. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3, 3, 3, 3, 3, 6, 6, 22,126,111,227,192,224, 96, 96, 96, 96,112, 48, 48, 48,112, 96, 96, 96,225,207,254,248,192,128,128,128,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  36. 0,
  37. };
  38. static const char PROGMEM layer[][116] = {
  39. [0] = {
  40. 248, 0, 0, 0, 0, 0, 0,160,160,192, 0, 0, 96,128,128,128,224, 0,192,160,160,160,192, 0,224, 64, 32, 32, 64,
  41. 3, 2, 2, 2, 2, 0, 1, 2, 2, 3, 2,128,130,132,132,132,131, 0, 1, 2, 2, 2, 0, 0, 3, 0, 0, 0, 0,
  42. 0, 0, 0, 0, 0, 0, 0, 0, 0,254,254,129,129, 97, 97, 25, 25,254,254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 25, 25, 24, 24, 24, 24, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44. },
  45. [1] = {
  46. 248, 0, 0, 0, 0, 0, 0,160,160,192, 0, 0, 96,128,128,128,224, 0,192,160,160,160,192, 0,224, 64, 32, 32, 64,
  47. 3, 2, 2, 2, 2, 0, 1, 2, 2, 3, 2, 0, 2,132,132, 4, 3, 0, 1, 2, 2, 2, 0, 0, 3, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6,255,255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 24, 31, 31, 24, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  50. },
  51. [2] = {
  52. 248, 0, 0, 0, 0, 0, 0,160,160,192, 0, 0, 96,128,128,128,224, 0,192,160,160,160,192, 0,224, 64, 32, 32, 64,
  53. 3, 2, 2, 2, 2, 0, 1, 2, 2, 3, 2,128,130,132,132,132,131, 0, 1, 2, 2, 2, 0, 0, 3, 0, 0, 0, 0,
  54. 0, 0, 0, 0, 0, 0, 0, 0, 0,134,134, 97, 97, 97, 97, 97, 97, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 31, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  56. },
  57. [3] = {
  58. 248, 0, 0, 0, 0, 0, 0,160,160,192, 0, 0, 96,128,128,128,224, 0,192,160,160,160,192, 0,224, 64, 32, 32, 64,
  59. 3, 2, 2, 2, 2, 0, 1, 2, 2,131,130,128,130,132,132,132,131,128,129, 2, 2, 2, 0, 0, 3, 0, 0, 0, 0,
  60. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 97, 97,121,121,135,135, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  61. 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 24, 24, 24, 24, 24, 24, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  62. }
  63. };
  64. static uint8_t key_pressed_l = 0;
  65. static uint8_t key_pressed_r = 0;
  66. oled_rotation_t oled_init_kb(oled_rotation_t rotation) {
  67. return OLED_ROTATION_180;
  68. }
  69. bool oled_task_kb(void) {
  70. if (!oled_task_user()) {
  71. return false;
  72. }
  73. char buffer[512];
  74. if (key_pressed_l && key_pressed_r)
  75. memcpy_P(buffer, bongo_press_lr, 512);
  76. else if (key_pressed_l)
  77. memcpy_P(buffer, bongo_press_l, 512);
  78. else if (key_pressed_r)
  79. memcpy_P(buffer, bongo_press_r, 512);
  80. else
  81. memcpy_P(buffer, bongo_release, 512);
  82. int highest_layer = 0;
  83. for(int i=3;i>=0;--i){
  84. if (IS_LAYER_ON(i)) {
  85. highest_layer = i;
  86. break;
  87. }
  88. }
  89. // Apply the layer info (4*29)
  90. for(int i=0;i<4;i++){
  91. for(int j=0;j<29;j++){
  92. buffer[i*128+85+j] = pgm_read_byte_near(layer[highest_layer]+i*29+j);
  93. }
  94. }
  95. oled_write_raw(buffer, 512);
  96. return false;
  97. }
  98. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  99. if (!process_record_user(keycode, record)) {
  100. return false;
  101. }
  102. if (record->event.pressed) {
  103. if (record->event.key.col <= 1)
  104. key_pressed_l++;
  105. else
  106. key_pressed_r++;
  107. } else {
  108. if (record->event.key.col <= 1)
  109. key_pressed_l--;
  110. else
  111. key_pressed_r--;
  112. }
  113. return true;
  114. }
  115. #endif
  116. void encoder_single_click(void) {
  117. tap_code(KC_MPLY);
  118. }
  119. void encoder_double_click(void) {
  120. tap_code(KC_MNXT);
  121. }
  122. void encoder_triple_click(void) {
  123. tap_code(KC_MPRV);
  124. }
  125. void matrix_init_kb(void) {
  126. matrix_init_user();
  127. gpio_set_pin_input_high(ENCODER_SWITCH);
  128. }
  129. void matrix_scan_kb(void) {
  130. matrix_scan_user();
  131. if (gpio_read_pin(ENCODER_SWITCH)) {
  132. if (encoder_pressed) { // release switch
  133. encoder_pressed = 0;
  134. encoder_press_combo += 1;
  135. encoder_last_release_time = timer_read32();
  136. }
  137. if (encoder_press_combo && timer_elapsed(encoder_last_release_time) > 300) {
  138. // combo timeout
  139. if (encoder_switched_layer) { // switch layer
  140. encoder_switched_layer = 0;
  141. } else { // click
  142. switch (encoder_press_combo) {
  143. case 1:
  144. encoder_single_click();
  145. break;
  146. case 2:
  147. encoder_double_click();
  148. break;
  149. default:
  150. encoder_triple_click();
  151. }
  152. }
  153. encoder_press_combo = 0;
  154. encoder_last_release_time = 0;
  155. }
  156. } else {
  157. if (!encoder_pressed) { // press switch
  158. encoder_pressed = 1;
  159. }
  160. }
  161. }
  162. bool encoder_update_kb(uint8_t index, bool clockwise) {
  163. if (!encoder_update_user(index, clockwise)) {
  164. return false;
  165. }
  166. if (clockwise) {
  167. if (encoder_pressed) {
  168. if (current_layer < 3) {
  169. current_layer += 1;
  170. layer_move(current_layer);
  171. }
  172. encoder_switched_layer = 1;
  173. } else {
  174. tap_code(KC_VOLU);
  175. }
  176. } else {
  177. if (encoder_pressed) {
  178. if (current_layer > 0) {
  179. current_layer -= 1;
  180. layer_move(current_layer);
  181. }
  182. encoder_switched_layer = 1;
  183. } else {
  184. tap_code(KC_VOLD);
  185. }
  186. }
  187. return false;
  188. }