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.

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