logo

qmk_firmware

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

winry315.c (6197B)


  1. // Copyright 2022 Sergey Vlasov (@sigprof)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "winry315.h"
  4. #if !defined(WINRY315_DEFAULT_ORIENTATION)
  5. # define WINRY315_DEFAULT_ORIENTATION WINRY315_ORIENTATION_TOP
  6. #endif
  7. #if defined(ENCODER_ENABLE) && !defined(ENCODER_MAP_ENABLE)
  8. # ifndef MEDIA_KEY_DELAY
  9. # define MEDIA_KEY_DELAY 10
  10. # endif
  11. bool encoder_update_kb(uint8_t index, bool clockwise) {
  12. if (!encoder_update_user(index, clockwise)) {
  13. return false;
  14. }
  15. if (index == 0) {
  16. // Left encoder (assuming the default "top" orientation)
  17. if (clockwise) {
  18. tap_code(KC_PGDN);
  19. } else {
  20. tap_code(KC_PGUP);
  21. }
  22. } else if (index == 1) {
  23. // Center encoder
  24. if (clockwise) {
  25. tap_code_delay(KC_VOLU, MEDIA_KEY_DELAY);
  26. } else {
  27. tap_code_delay(KC_VOLD, MEDIA_KEY_DELAY);
  28. }
  29. } else if (index == 2) {
  30. // Right encoder
  31. if (clockwise) {
  32. tap_code_delay(KC_MNXT, MEDIA_KEY_DELAY);
  33. } else {
  34. tap_code_delay(KC_MPRV, MEDIA_KEY_DELAY);
  35. }
  36. }
  37. return true;
  38. }
  39. #endif // defined(ENCODER_ENABLE) && !defined(ENCODER_MAP_ENABLE)
  40. #if defined(RGB_MATRIX_ENABLE)
  41. // LED mapping (assuming the default "top" orientation):
  42. // 0 - right encoder, top right
  43. // 1 - right encoder, top left
  44. // 2 - center encoder, top right
  45. // 3 - center encoder, top left
  46. // 4 - left encoder, top right
  47. // 5 - left encoder, top left
  48. // 6 - row 0, column 0
  49. // 7 - row 1, column 0
  50. // 8 - row 2, column 0
  51. // 9 - row 2, column 1
  52. // 10 - row 1, column 1
  53. // 11 - row 0, column 1
  54. // 12 - row 0, column 2
  55. // 13 - row 1, column 2
  56. // 14 - row 2, column 2
  57. // 15 - row 2, column 3
  58. // 16 - row 1, column 3
  59. // 17 - row 0, column 3
  60. // 18 - row 0, column 4
  61. // 19 - row 1, column 4
  62. // 20 - row 2, column 4
  63. // 21 - underglow, right bottom
  64. // 22 - underglow, left bottom
  65. // 23 - underglow, left middle
  66. // 24 - underglow, left top
  67. // 25 - underglow, right top
  68. // 26 - underglow, right middle
  69. # define X_MM_MIN (-42)
  70. # define X_MM_MAX 42
  71. # define Y_MM_MIN (-40) // actually -35, but adjusted to get height = width
  72. # define Y_MM_MAX 44 // actually 40, but adjusted to get height = width
  73. # define WIDTH_MM (X_MM_MAX - X_MM_MIN)
  74. # define HEIGHT_MM (Y_MM_MAX - Y_MM_MIN)
  75. # define WIDTH_UNITS (35 * 2) // needs to match RGB_MATRIX_CENTER
  76. # define HEIGHT_UNITS (35 * 2) // needs to match RGB_MATRIX_CENTER
  77. // Convert the LED physical coordinates from millimeters with the origin at the
  78. // PCB center to the form expected by the RGB Matrix code.
  79. # define LED(x_mm, y_mm) \
  80. { ((x_mm - X_MM_MIN) * WIDTH_UNITS + WIDTH_MM / 2) / WIDTH_MM, ((Y_MM_MAX - y_mm) * HEIGHT_UNITS + HEIGHT_MM / 2) / HEIGHT_MM }
  81. // clang-format off
  82. static const led_config_t PROGMEM initial_led_config = {
  83. {
  84. { 6, 11, 12, 17, 18, 7, 10, 13, 16, 19, 8, 9, 14, 15, 20, 2, 0, 4, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED }
  85. },
  86. {
  87. LED( 35, 36),
  88. LED( 21, 36),
  89. LED( 8, 34),
  90. LED(-8, 34),
  91. LED(-21, 36),
  92. LED(-35, 36),
  93. LED(-38, 5),
  94. LED(-38, -14),
  95. LED(-38, -33),
  96. LED(-19, -33),
  97. LED(-19, -14),
  98. LED(-19, 5),
  99. LED( 0, 5),
  100. LED( 0, -14),
  101. LED( 0, -33),
  102. LED( 19, -33),
  103. LED( 19, -14),
  104. LED( 19, 5),
  105. LED( 38, 5),
  106. LED( 38, -14),
  107. LED( 38, -33),
  108. LED( 28, -35),
  109. LED(-28, -35),
  110. LED(-37, -9),
  111. LED(-42, 40),
  112. LED( 42, 40),
  113. LED( 37, -9)
  114. },
  115. {
  116. 1, 1, 1, 1, 1, 1, // encoders (colored as modifiers)
  117. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // regular keys
  118. 2, 2, 2, 2, 2, 2 // underglow
  119. }
  120. };
  121. // clang-format on
  122. led_config_t g_led_config;
  123. void keyboard_pre_init_kb(void) {
  124. // To be safe against any possible changes to rgb_matrix_init(),
  125. // g_led_config should be initialized before rgb_matrix_init() is called;
  126. // matrix_init_kb() would be too late, so keyboard_pre_init_kb() is used.
  127. memcpy_P(&g_led_config, &initial_led_config, sizeof(g_led_config));
  128. winry315_set_orientation(WINRY315_DEFAULT_ORIENTATION);
  129. keyboard_pre_init_user();
  130. }
  131. // Encoders have two associated LEDs on this board; supporting more than one
  132. // LED per key requires defining rgb_matrix_map_row_column_to_led_kb() to
  133. // report any extra LEDs.
  134. uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) {
  135. if (row == 0) {
  136. switch (column) {
  137. case 15: // center encoder
  138. led_i[0] = 3;
  139. return 1;
  140. case 16: // right encoder
  141. led_i[0] = 1;
  142. return 1;
  143. case 17: // left encoder
  144. led_i[0] = 5;
  145. return 1;
  146. }
  147. }
  148. return 0;
  149. }
  150. #endif // defined(RGB_MATRIX_ENABLE)
  151. void winry315_set_orientation(uint8_t orientation) {
  152. (void)orientation;
  153. #if defined(RGB_MATRIX_ENABLE)
  154. for (uint8_t i = 0; i < RGB_MATRIX_LED_COUNT; ++i) {
  155. led_point_t * dst_point = &g_led_config.point[i];
  156. const led_point_t *src_point = &initial_led_config.point[i];
  157. uint8_t x = pgm_read_byte(&src_point->x);
  158. uint8_t y = pgm_read_byte(&src_point->y);
  159. switch (orientation) {
  160. case WINRY315_ORIENTATION_TOP:
  161. default:
  162. dst_point->x = x;
  163. dst_point->y = y;
  164. break;
  165. case WINRY315_ORIENTATION_LEFT:
  166. dst_point->x = y;
  167. dst_point->y = WIDTH_UNITS - x;
  168. break;
  169. case WINRY315_ORIENTATION_RIGHT:
  170. dst_point->x = HEIGHT_UNITS - y;
  171. dst_point->y = x;
  172. break;
  173. case WINRY315_ORIENTATION_BOTTOM:
  174. dst_point->x = WIDTH_UNITS - x;
  175. dst_point->y = HEIGHT_UNITS - y;
  176. break;
  177. }
  178. }
  179. #endif // defined(RGB_MATRIX_ENABLE)
  180. }