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