You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
7.8 KiB

  1. /* Copyright 2020 ademey "MsMustard"
  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 "terrazzo.h"
  17. #ifdef LED_MATRIX_ENABLE
  18. #include <math.h>
  19. #include "print.h"
  20. #include "quantum.h"
  21. const is31fl3731_led_t PROGMEM g_is31fl3731_leds[IS31FL3731_LED_COUNT] = {
  22. /* Refer to IS31 manual for these locations
  23. * https://cdn-learn.adafruit.com/downloads/pdf/adafruit-15x7-7x15-charlieplex-led-matrix-charliewing-featherwing.pdf
  24. */
  25. {0, C1_2}, {0, C1_3}, {0, C1_4}, {0, C1_5}, {0, C1_6}, {0, C1_7}, {0, C1_8},
  26. {0, C2_2}, {0, C2_3}, {0, C2_4}, {0, C2_5}, {0, C2_6}, {0, C2_7}, {0, C2_8},
  27. {0, C3_2}, {0, C3_3}, {0, C3_4}, {0, C3_5}, {0, C3_6}, {0, C3_7}, {0, C3_8},
  28. {0, C4_2}, {0, C4_3}, {0, C4_4}, {0, C4_5}, {0, C4_6}, {0, C4_7}, {0, C4_8},
  29. {0, C5_2}, {0, C5_3}, {0, C5_4}, {0, C5_5}, {0, C5_6}, {0, C5_7}, {0, C5_8},
  30. {0, C6_2}, {0, C6_3}, {0, C6_4}, {0, C6_5}, {0, C6_6}, {0, C6_7}, {0, C6_8},
  31. {0, C7_2}, {0, C7_3}, {0, C7_4}, {0, C7_5}, {0, C7_6}, {0, C7_7}, {0, C7_8},
  32. {0, C8_2}, {0, C8_3}, {0, C8_4}, {0, C8_5}, {0, C8_6}, {0, C8_7}, {0, C8_8},
  33. //
  34. {0, C8_15},{0, C8_14},{0, C8_13},{0, C8_12},{0, C8_11},{0, C8_10},{0, C8_9},
  35. {0, C7_15},{0, C7_14},{0, C7_13},{0, C7_12},{0, C7_11},{0, C7_10},{0, C7_9},
  36. {0, C6_15},{0, C6_14},{0, C6_13},{0, C6_12},{0, C6_11},{0, C6_10},{0, C6_9},
  37. {0, C5_15},{0, C5_14},{0, C5_13},{0, C5_12},{0, C5_11},{0, C5_10},{0, C5_9},
  38. {0, C4_15},{0, C4_14},{0, C4_13},{0, C4_12},{0, C4_11},{0, C4_10},{0, C4_9},
  39. {0, C3_15},{0, C3_14},{0, C3_13},{0, C3_12},{0, C3_11},{0, C3_10},{0, C3_9},
  40. {0, C2_15},{0, C2_14},{0, C2_13},{0, C2_12},{0, C2_11},{0, C2_10},{0, C2_9}
  41. };
  42. led_config_t g_led_config = {
  43. {
  44. // Key Matrix to LED Index
  45. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED },
  46. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED },
  47. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED },
  48. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED },
  49. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED },
  50. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED },
  51. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED },
  52. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED },
  53. { NO_LED, NO_LED, NO_LED, NO_LED, NO_LED, NO_LED }
  54. }, {
  55. // LED Index to Physical Position
  56. { 0, 0 }, { 16, 0 }, { 32, 0 }, { 48, 0 }, { 64, 0 }, { 80, 0 }, { 96, 0 }, { 112, 0 }, { 128, 0 }, { 144, 0 }, { 160, 0 }, { 176, 0 }, { 192, 0 }, { 208, 0 }, { 224, 0 },
  57. { 0, 11 }, { 16, 11 }, { 32, 11 }, { 48, 11 }, { 64, 11 }, { 80, 11 }, { 96, 11 }, { 112, 11 }, { 128, 11 }, { 144, 11 }, { 160, 11 }, { 176, 11 }, { 192, 11 }, { 208, 11 }, { 224, 11 },
  58. { 0, 21 }, { 16, 21 }, { 32, 21 }, { 48, 21 }, { 64, 21 }, { 80, 21 }, { 96, 21 }, { 112, 21 }, { 128, 21 }, { 144, 21 }, { 160, 21 }, { 176, 21 }, { 192, 21 }, { 208, 21 }, { 224, 21 },
  59. { 0, 32 }, { 16, 32 }, { 32, 32 }, { 48, 32 }, { 64, 32 }, { 80, 32 }, { 96, 32 }, { 112, 32 }, { 128, 32 }, { 144, 32 }, { 160, 32 }, { 176, 32 }, { 192, 32 }, { 208, 32 }, { 224, 32 },
  60. { 0, 43 }, { 16, 43 }, { 32, 43 }, { 48, 43 }, { 64, 43 }, { 80, 43 }, { 96, 43 }, { 112, 43 }, { 128, 43 }, { 144, 43 }, { 160, 43 }, { 176, 43 }, { 192, 43 }, { 208, 43 }, { 224, 43 },
  61. { 0, 53 }, { 16, 53 }, { 32, 53 }, { 48, 53 }, { 64, 53 }, { 80, 53 }, { 96, 53 }, { 112, 53 }, { 128, 53 }, { 144, 53 }, { 160, 53 }, { 176, 53 }, { 192, 53 }, { 208, 53 }, { 224, 53 },
  62. { 0, 64 }, { 16, 64 }, { 32, 64 }, { 48, 64 }, { 64, 64 }, { 80, 64 }, { 96, 64 }, { 112, 64 }, { 128, 64 }, { 144, 64 }, { 160, 64 }, { 176, 64 }, { 192, 64 }, { 208, 64 }, { 224, 64 }
  63. }, {
  64. // LED Index to Flag
  65. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  66. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  67. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  68. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  69. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  70. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  71. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
  72. }
  73. };
  74. #define TERRAZZO_EFFECT(name)
  75. #define TERRAZZO_EFFECT_IMPLS
  76. #include "terrazzo_effects/terrazzo_effects.inc"
  77. #undef TERRAZZO_EFFECT_IMPLS
  78. #undef TERRAZZO_EFFECT
  79. uint8_t terrazzo_led_index = 0;
  80. uint8_t terrazzo_dir = 1;
  81. uint8_t terrazzo_effect = 1;
  82. void terrazzo_set_pixel(uint8_t x, uint8_t y, uint8_t value) {
  83. uint8_t target = y * LED_MATRIX_COLS + x;
  84. if (target < LED_MATRIX_LED_COUNT && target >= 0) {
  85. led_matrix_set_value(y * LED_MATRIX_COLS + x, value);
  86. }
  87. }
  88. void terrazzo_draw_at(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t image[]) {
  89. uint8_t index = 0;
  90. for (int v = 0; v < height; v++) {
  91. for (int h = 0; h < width; h++) {
  92. uint8_t pixel_value = image[index];
  93. if (pixel_value != 0) {
  94. terrazzo_set_pixel(x + h, y + v, image[index]);
  95. }
  96. index++;
  97. }
  98. }
  99. }
  100. void terrazzo_scroll_pixel(bool clockwise) {
  101. terrazzo_dir = clockwise;
  102. if (clockwise) {
  103. terrazzo_led_index = terrazzo_led_index + 1;
  104. } else {
  105. terrazzo_led_index = terrazzo_led_index - 1;
  106. }
  107. if (terrazzo_led_index >= LED_MATRIX_LED_COUNT) {
  108. terrazzo_led_index = 0;
  109. } else if (terrazzo_led_index <= 0 ) {
  110. terrazzo_led_index = LED_MATRIX_LED_COUNT - 1;
  111. }
  112. }
  113. void terrazzo_step_mode(void) {
  114. terrazzo_effect++;
  115. if (terrazzo_effect >= TERRAZZO_EFFECT_MAX) {
  116. terrazzo_effect = 1;
  117. }
  118. }
  119. void terrazzo_step_mode_reverse(void) {
  120. terrazzo_effect--;
  121. if (terrazzo_effect < 1) {
  122. terrazzo_effect = TERRAZZO_EFFECT_MAX - 1;
  123. }
  124. }
  125. void terrazzo_mode_off(void) {
  126. terrazzo_effect = TERRAZZO_NONE;
  127. }
  128. void terrazzo_render(void) {
  129. switch(terrazzo_effect) {
  130. case TERRAZZO_NONE:
  131. led_matrix_set_value_all(0);
  132. break;
  133. #define TERRAZZO_EFFECT(name, ...) \
  134. case TERRAZZO_EFFECT_##name: \
  135. name(terrazzo_led_index, terrazzo_dir); \
  136. break;
  137. #include "terrazzo_effects/terrazzo_effects.inc"
  138. #undef TERRAZZO_EFFECT
  139. }
  140. }
  141. bool led_matrix_indicators_kb(void) {
  142. if (!led_matrix_indicators_user()) {
  143. return false;
  144. }
  145. terrazzo_render();
  146. return true;
  147. }
  148. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  149. if (record->event.pressed) {
  150. switch(keycode) {
  151. case TZ_NXT:
  152. terrazzo_step_mode();
  153. return true;
  154. case TZ_PRV:
  155. terrazzo_step_mode_reverse();
  156. return true;
  157. case TZ_OFF:
  158. terrazzo_mode_off();
  159. return true;
  160. // Reverse animation on backspace
  161. case KC_BSPC:
  162. terrazzo_scroll_pixel(0);
  163. return true;
  164. // Any keycode increments counter
  165. default:
  166. terrazzo_scroll_pixel(1);
  167. break;
  168. }
  169. }
  170. return process_record_user(keycode, record);
  171. }
  172. void suspend_power_down_kb(void) {
  173. led_matrix_set_suspend_state(true);
  174. }
  175. void suspend_wakeup_init_kb(void) {
  176. led_matrix_set_suspend_state(false);
  177. }
  178. #endif