terrazzo.c (5398B)
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 17 #include <math.h> 18 #include "terrazzo.h" 19 20 #ifdef LED_MATRIX_ENABLE 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 43 #define TERRAZZO_EFFECT(name) 44 #define TERRAZZO_EFFECT_IMPLS 45 46 #include "terrazzo_effects/terrazzo_effects.inc" 47 48 #undef TERRAZZO_EFFECT_IMPLS 49 #undef TERRAZZO_EFFECT 50 51 uint8_t terrazzo_led_index = 0; 52 uint8_t terrazzo_dir = 1; 53 uint8_t terrazzo_effect = 1; 54 55 void terrazzo_set_pixel(uint8_t x, uint8_t y, uint8_t value) { 56 uint8_t target = y * LED_MATRIX_COLS + x; 57 if (target < LED_MATRIX_LED_COUNT && target >= 0) { 58 led_matrix_set_value(y * LED_MATRIX_COLS + x, value); 59 } 60 } 61 62 void terrazzo_draw_at(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t image[]) { 63 uint8_t index = 0; 64 for (int v = 0; v < height; v++) { 65 for (int h = 0; h < width; h++) { 66 uint8_t pixel_value = image[index]; 67 if (pixel_value != 0) { 68 terrazzo_set_pixel(x + h, y + v, image[index]); 69 } 70 index++; 71 } 72 } 73 } 74 75 void terrazzo_scroll_pixel(bool clockwise) { 76 terrazzo_dir = clockwise; 77 78 if (clockwise) { 79 terrazzo_led_index = terrazzo_led_index + 1; 80 } else { 81 terrazzo_led_index = terrazzo_led_index - 1; 82 } 83 84 if (terrazzo_led_index >= LED_MATRIX_LED_COUNT) { 85 terrazzo_led_index = 0; 86 } else if (terrazzo_led_index <= 0 ) { 87 terrazzo_led_index = LED_MATRIX_LED_COUNT - 1; 88 } 89 } 90 91 void terrazzo_step_mode(void) { 92 terrazzo_effect++; 93 if (terrazzo_effect >= TERRAZZO_EFFECT_MAX) { 94 terrazzo_effect = 1; 95 } 96 } 97 98 void terrazzo_step_mode_reverse(void) { 99 terrazzo_effect--; 100 if (terrazzo_effect < 1) { 101 terrazzo_effect = TERRAZZO_EFFECT_MAX - 1; 102 } 103 } 104 105 void terrazzo_mode_off(void) { 106 terrazzo_effect = TERRAZZO_NONE; 107 } 108 109 void terrazzo_render(void) { 110 switch(terrazzo_effect) { 111 case TERRAZZO_NONE: 112 led_matrix_set_value_all(0); 113 break; 114 #define TERRAZZO_EFFECT(name, ...) \ 115 case TERRAZZO_EFFECT_##name: \ 116 name(terrazzo_led_index, terrazzo_dir); \ 117 break; 118 #include "terrazzo_effects/terrazzo_effects.inc" 119 #undef TERRAZZO_EFFECT 120 } 121 } 122 123 bool led_matrix_indicators_kb(void) { 124 if (!led_matrix_indicators_user()) { 125 return false; 126 } 127 terrazzo_render(); 128 return true; 129 } 130 131 bool process_record_kb(uint16_t keycode, keyrecord_t *record) { 132 if (record->event.pressed) { 133 switch(keycode) { 134 case TZ_NXT: 135 terrazzo_step_mode(); 136 return true; 137 case TZ_PRV: 138 terrazzo_step_mode_reverse(); 139 return true; 140 case TZ_OFF: 141 terrazzo_mode_off(); 142 return true; 143 // Reverse animation on backspace 144 case KC_BSPC: 145 terrazzo_scroll_pixel(0); 146 return true; 147 // Any keycode increments counter 148 default: 149 terrazzo_scroll_pixel(1); 150 break; 151 } 152 } 153 return process_record_user(keycode, record); 154 } 155 156 void suspend_power_down_kb(void) { 157 led_matrix_set_suspend_state(true); 158 } 159 160 void suspend_wakeup_init_kb(void) { 161 led_matrix_set_suspend_state(false); 162 } 163 164 165 #endif