diff options
Diffstat (limited to 'quantum')
| -rw-r--r-- | quantum/color.c | 87 | ||||
| -rw-r--r-- | quantum/color.h | 55 | ||||
| -rw-r--r-- | quantum/quantum.c | 23 | ||||
| -rw-r--r-- | quantum/quantum.h | 6 | ||||
| -rw-r--r-- | quantum/rgb.h | 47 | ||||
| -rw-r--r-- | quantum/rgb_matrix.c | 873 | ||||
| -rw-r--r-- | quantum/rgb_matrix.h | 135 |
7 files changed, 1224 insertions, 2 deletions
diff --git a/quantum/color.c b/quantum/color.c new file mode 100644 index 000000000..8ede053e7 --- /dev/null +++ b/quantum/color.c | |||
| @@ -0,0 +1,87 @@ | |||
| 1 | /* Copyright 2017 Jason Williams | ||
| 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 | |||
| 18 | #include "color.h" | ||
| 19 | #include "led_tables.h" | ||
| 20 | #include "progmem.h" | ||
| 21 | |||
| 22 | RGB hsv_to_rgb( HSV hsv ) | ||
| 23 | { | ||
| 24 | RGB rgb; | ||
| 25 | uint8_t region, p, q, t; | ||
| 26 | uint16_t h, s, v, remainder; | ||
| 27 | |||
| 28 | if ( hsv.s == 0 ) | ||
| 29 | { | ||
| 30 | rgb.r = hsv.v; | ||
| 31 | rgb.g = hsv.v; | ||
| 32 | rgb.b = hsv.v; | ||
| 33 | return rgb; | ||
| 34 | } | ||
| 35 | |||
| 36 | h = hsv.h; | ||
| 37 | s = hsv.s; | ||
| 38 | v = hsv.v; | ||
| 39 | |||
| 40 | region = h / 43; | ||
| 41 | remainder = (h - (region * 43)) * 6; | ||
| 42 | |||
| 43 | p = (v * (255 - s)) >> 8; | ||
| 44 | q = (v * (255 - ((s * remainder) >> 8))) >> 8; | ||
| 45 | t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8; | ||
| 46 | |||
| 47 | switch ( region ) | ||
| 48 | { | ||
| 49 | case 0: | ||
| 50 | rgb.r = v; | ||
| 51 | rgb.g = t; | ||
| 52 | rgb.b = p; | ||
| 53 | break; | ||
| 54 | case 1: | ||
| 55 | rgb.r = q; | ||
| 56 | rgb.g = v; | ||
| 57 | rgb.b = p; | ||
| 58 | break; | ||
| 59 | case 2: | ||
| 60 | rgb.r = p; | ||
| 61 | rgb.g = v; | ||
| 62 | rgb.b = t; | ||
| 63 | break; | ||
| 64 | case 3: | ||
| 65 | rgb.r = p; | ||
| 66 | rgb.g = q; | ||
| 67 | rgb.b = v; | ||
| 68 | break; | ||
| 69 | case 4: | ||
| 70 | rgb.r = t; | ||
| 71 | rgb.g = p; | ||
| 72 | rgb.b = v; | ||
| 73 | break; | ||
| 74 | default: | ||
| 75 | rgb.r = v; | ||
| 76 | rgb.g = p; | ||
| 77 | rgb.b = q; | ||
| 78 | break; | ||
| 79 | } | ||
| 80 | |||
| 81 | rgb.r = pgm_read_byte( &CIE1931_CURVE[rgb.r] ); | ||
| 82 | rgb.g = pgm_read_byte( &CIE1931_CURVE[rgb.g] ); | ||
| 83 | rgb.b = pgm_read_byte( &CIE1931_CURVE[rgb.b] ); | ||
| 84 | |||
| 85 | return rgb; | ||
| 86 | } | ||
| 87 | |||
diff --git a/quantum/color.h b/quantum/color.h new file mode 100644 index 000000000..9d51d45ad --- /dev/null +++ b/quantum/color.h | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | /* Copyright 2017 Jason Williams | ||
| 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 | |||
| 18 | #ifndef COLOR_H | ||
| 19 | #define COLOR_H | ||
| 20 | |||
| 21 | #include <stdint.h> | ||
| 22 | #include <stdbool.h> | ||
| 23 | |||
| 24 | |||
| 25 | #if defined(__GNUC__) | ||
| 26 | #define PACKED __attribute__ ((__packed__)) | ||
| 27 | #else | ||
| 28 | #define PACKED | ||
| 29 | #endif | ||
| 30 | |||
| 31 | #if defined(_MSC_VER) | ||
| 32 | #pragma pack( push, 1 ) | ||
| 33 | #endif | ||
| 34 | |||
| 35 | typedef struct PACKED | ||
| 36 | { | ||
| 37 | uint8_t r; | ||
| 38 | uint8_t g; | ||
| 39 | uint8_t b; | ||
| 40 | } RGB; | ||
| 41 | |||
| 42 | typedef struct PACKED | ||
| 43 | { | ||
| 44 | uint8_t h; | ||
| 45 | uint8_t s; | ||
| 46 | uint8_t v; | ||
| 47 | } HSV; | ||
| 48 | |||
| 49 | #if defined(_MSC_VER) | ||
| 50 | #pragma pack( pop ) | ||
| 51 | #endif | ||
| 52 | |||
| 53 | RGB hsv_to_rgb( HSV hsv ); | ||
| 54 | |||
| 55 | #endif // COLOR_H | ||
diff --git a/quantum/quantum.c b/quantum/quantum.c index 2662e5ef1..e1bc8b242 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c | |||
| @@ -230,6 +230,9 @@ bool process_record_quantum(keyrecord_t *record) { | |||
| 230 | process_clicky(keycode, record) && | 230 | process_clicky(keycode, record) && |
| 231 | #endif //AUDIO_CLICKY | 231 | #endif //AUDIO_CLICKY |
| 232 | process_record_kb(keycode, record) && | 232 | process_record_kb(keycode, record) && |
| 233 | #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_KEYPRESSES) | ||
| 234 | process_rgb_matrix(keycode, record) && | ||
| 235 | #endif | ||
| 233 | #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED) | 236 | #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED) |
| 234 | process_midi(keycode, record) && | 237 | process_midi(keycode, record) && |
| 235 | #endif | 238 | #endif |
| @@ -307,7 +310,7 @@ bool process_record_quantum(keyrecord_t *record) { | |||
| 307 | } | 310 | } |
| 308 | return false; | 311 | return false; |
| 309 | #endif | 312 | #endif |
| 310 | #ifdef RGBLIGHT_ENABLE | 313 | #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE) |
| 311 | case RGB_TOG: | 314 | case RGB_TOG: |
| 312 | if (record->event.pressed) { | 315 | if (record->event.pressed) { |
| 313 | rgblight_toggle(); | 316 | rgblight_toggle(); |
| @@ -835,9 +838,18 @@ void matrix_init_quantum() { | |||
| 835 | #ifdef AUDIO_ENABLE | 838 | #ifdef AUDIO_ENABLE |
| 836 | audio_init(); | 839 | audio_init(); |
| 837 | #endif | 840 | #endif |
| 841 | #ifdef RGB_MATRIX_ENABLE | ||
| 842 | rgb_matrix_init_drivers(); | ||
| 843 | #endif | ||
| 838 | matrix_init_kb(); | 844 | matrix_init_kb(); |
| 839 | } | 845 | } |
| 840 | 846 | ||
| 847 | uint8_t rgb_matrix_task_counter = 0; | ||
| 848 | |||
| 849 | #ifndef RGB_MATRIX_SKIP_FRAMES | ||
| 850 | #define RGB_MATRIX_SKIP_FRAMES 1 | ||
| 851 | #endif | ||
| 852 | |||
| 841 | void matrix_scan_quantum() { | 853 | void matrix_scan_quantum() { |
| 842 | #if defined(AUDIO_ENABLE) | 854 | #if defined(AUDIO_ENABLE) |
| 843 | matrix_scan_music(); | 855 | matrix_scan_music(); |
| @@ -855,9 +867,16 @@ void matrix_scan_quantum() { | |||
| 855 | backlight_task(); | 867 | backlight_task(); |
| 856 | #endif | 868 | #endif |
| 857 | 869 | ||
| 870 | #ifdef RGB_MATRIX_ENABLE | ||
| 871 | rgb_matrix_task(); | ||
| 872 | if (rgb_matrix_task_counter == 0) { | ||
| 873 | rgb_matrix_update_pwm_buffers(); | ||
| 874 | } | ||
| 875 | rgb_matrix_task_counter = ((rgb_matrix_task_counter + 1) % (RGB_MATRIX_SKIP_FRAMES + 1)); | ||
| 876 | #endif | ||
| 877 | |||
| 858 | matrix_scan_kb(); | 878 | matrix_scan_kb(); |
| 859 | } | 879 | } |
| 860 | |||
| 861 | #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN) | 880 | #if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN) |
| 862 | 881 | ||
| 863 | static const uint8_t backlight_pin = BACKLIGHT_PIN; | 882 | static const uint8_t backlight_pin = BACKLIGHT_PIN; |
diff --git a/quantum/quantum.h b/quantum/quantum.h index 195f578de..2958a0abd 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h | |||
| @@ -27,9 +27,15 @@ | |||
| 27 | #ifdef BACKLIGHT_ENABLE | 27 | #ifdef BACKLIGHT_ENABLE |
| 28 | #include "backlight.h" | 28 | #include "backlight.h" |
| 29 | #endif | 29 | #endif |
| 30 | #if !defined(RGBLIGHT_ENABLE) && !defined(RGB_MATRIX_ENABLE) | ||
| 31 | #include "rgb.h" | ||
| 32 | #endif | ||
| 30 | #ifdef RGBLIGHT_ENABLE | 33 | #ifdef RGBLIGHT_ENABLE |
| 31 | #include "rgblight.h" | 34 | #include "rgblight.h" |
| 32 | #endif | 35 | #endif |
| 36 | #ifdef RGB_MATRIX_ENABLE | ||
| 37 | #include "rgb_matrix.h" | ||
| 38 | #endif | ||
| 33 | #include "action_layer.h" | 39 | #include "action_layer.h" |
| 34 | #include "eeconfig.h" | 40 | #include "eeconfig.h" |
| 35 | #include <stddef.h> | 41 | #include <stddef.h> |
diff --git a/quantum/rgb.h b/quantum/rgb.h new file mode 100644 index 000000000..fbdda293f --- /dev/null +++ b/quantum/rgb.h | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | /* Copyright 2017 Jack Humbert | ||
| 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 | #ifndef RGB_H | ||
| 18 | #define RGB_H | ||
| 19 | |||
| 20 | __attribute__((weak)) | ||
| 21 | void rgblight_toggle(void) {}; | ||
| 22 | |||
| 23 | __attribute__((weak)) | ||
| 24 | void rgblight_step(void) {}; | ||
| 25 | |||
| 26 | __attribute__((weak)) | ||
| 27 | void rgblight_step_reverse(void) {}; | ||
| 28 | |||
| 29 | __attribute__((weak)) | ||
| 30 | void rgblight_increase_hue(void) {}; | ||
| 31 | |||
| 32 | __attribute__((weak)) | ||
| 33 | void rgblight_decrease_hue(void) {}; | ||
| 34 | |||
| 35 | __attribute__((weak)) | ||
| 36 | void rgblight_increase_sat(void) {}; | ||
| 37 | |||
| 38 | __attribute__((weak)) | ||
| 39 | void rgblight_decrease_sat(void) {}; | ||
| 40 | |||
| 41 | __attribute__((weak)) | ||
| 42 | void rgblight_increase_val(void) {}; | ||
| 43 | |||
| 44 | __attribute__((weak)) | ||
| 45 | void rgblight_decrease_val(void) {}; | ||
| 46 | |||
| 47 | #endif \ No newline at end of file | ||
diff --git a/quantum/rgb_matrix.c b/quantum/rgb_matrix.c new file mode 100644 index 000000000..6cb0478f7 --- /dev/null +++ b/quantum/rgb_matrix.c | |||
| @@ -0,0 +1,873 @@ | |||
| 1 | /* Copyright 2017 Jason Williams | ||
| 2 | * Copyright 2017 Jack Humbert | ||
| 3 | * | ||
| 4 | * This program is free software: you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License as published by | ||
| 6 | * the Free Software Foundation, either version 2 of the License, or | ||
| 7 | * (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | |||
| 19 | #include "rgb_matrix.h" | ||
| 20 | #include <avr/io.h> | ||
| 21 | #include "TWIlib.h" | ||
| 22 | #include <util/delay.h> | ||
| 23 | #include <avr/interrupt.h> | ||
| 24 | #include "progmem.h" | ||
| 25 | #include "config.h" | ||
| 26 | #include "eeprom.h" | ||
| 27 | #include "lufa.h" | ||
| 28 | #include <math.h> | ||
| 29 | |||
| 30 | rgb_config_t rgb_matrix_config; | ||
| 31 | |||
| 32 | #ifndef RGB_DISABLE_AFTER_TIMEOUT | ||
| 33 | #define RGB_DISABLE_AFTER_TIMEOUT 0 | ||
| 34 | #endif | ||
| 35 | |||
| 36 | #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED | ||
| 37 | #define RGB_DISABLE_WHEN_USB_SUSPENDED false | ||
| 38 | #endif | ||
| 39 | |||
| 40 | #ifndef EECONFIG_RGB_MATRIX | ||
| 41 | #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT | ||
| 42 | #endif | ||
| 43 | |||
| 44 | bool g_suspend_state = false; | ||
| 45 | |||
| 46 | // Global tick at 20 Hz | ||
| 47 | uint32_t g_tick = 0; | ||
| 48 | |||
| 49 | // Ticks since this key was last hit. | ||
| 50 | uint8_t g_key_hit[DRIVER_LED_TOTAL]; | ||
| 51 | |||
| 52 | // Ticks since any key was last hit. | ||
| 53 | uint32_t g_any_key_hit = 0; | ||
| 54 | |||
| 55 | #ifndef PI | ||
| 56 | #define PI 3.14159265 | ||
| 57 | #endif | ||
| 58 | |||
| 59 | uint32_t eeconfig_read_rgb_matrix(void) { | ||
| 60 | return eeprom_read_dword(EECONFIG_RGB_MATRIX); | ||
| 61 | } | ||
| 62 | void eeconfig_update_rgb_matrix(uint32_t val) { | ||
| 63 | eeprom_update_dword(EECONFIG_RGB_MATRIX, val); | ||
| 64 | } | ||
| 65 | void eeconfig_update_rgb_matrix_default(void) { | ||
| 66 | dprintf("eeconfig_update_rgb_matrix_default\n"); | ||
| 67 | rgb_matrix_config.enable = 1; | ||
| 68 | rgb_matrix_config.mode = RGB_MATRIX_CYCLE_LEFT_RIGHT; | ||
| 69 | rgb_matrix_config.hue = 0; | ||
| 70 | rgb_matrix_config.sat = 255; | ||
| 71 | rgb_matrix_config.val = 255; | ||
| 72 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | ||
| 73 | } | ||
| 74 | void eeconfig_debug_rgb_matrix(void) { | ||
| 75 | dprintf("rgb_matrix_config eprom\n"); | ||
| 76 | dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable); | ||
| 77 | dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode); | ||
| 78 | dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue); | ||
| 79 | dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat); | ||
| 80 | dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val); | ||
| 81 | } | ||
| 82 | |||
| 83 | // Last led hit | ||
| 84 | #define LED_HITS_TO_REMEMBER 8 | ||
| 85 | uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255}; | ||
| 86 | uint8_t g_last_led_count = 0; | ||
| 87 | |||
| 88 | void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) { | ||
| 89 | rgb_led led; | ||
| 90 | *led_count = 0; | ||
| 91 | |||
| 92 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
| 93 | // map_index_to_led(i, &led); | ||
| 94 | led = g_rgb_leds[i]; | ||
| 95 | if (row == led.matrix_co.row && column == led.matrix_co.col) { | ||
| 96 | led_i[*led_count] = i; | ||
| 97 | (*led_count)++; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | void rgb_matrix_update_pwm_buffers(void) { | ||
| 104 | IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 ); | ||
| 105 | IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 ); | ||
| 106 | } | ||
| 107 | |||
| 108 | void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) { | ||
| 109 | IS31FL3731_set_color( index, red, green, blue ); | ||
| 110 | } | ||
| 111 | |||
| 112 | void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) { | ||
| 113 | IS31FL3731_set_color_all( red, green, blue ); | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) { | ||
| 118 | if ( record->event.pressed ) { | ||
| 119 | uint8_t led[8], led_count; | ||
| 120 | map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); | ||
| 121 | if (led_count > 0) { | ||
| 122 | for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) { | ||
| 123 | g_last_led_hit[i - 1] = g_last_led_hit[i - 2]; | ||
| 124 | } | ||
| 125 | g_last_led_hit[0] = led[0]; | ||
| 126 | g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1); | ||
| 127 | } | ||
| 128 | for(uint8_t i = 0; i < led_count; i++) | ||
| 129 | g_key_hit[led[i]] = 0; | ||
| 130 | g_any_key_hit = 0; | ||
| 131 | } else { | ||
| 132 | #ifdef RGB_MATRIX_KEYRELEASES | ||
| 133 | uint8_t led[8], led_count; | ||
| 134 | map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count); | ||
| 135 | for(uint8_t i = 0; i < led_count; i++) | ||
| 136 | g_key_hit[led[i]] = 255; | ||
| 137 | |||
| 138 | g_any_key_hit = 255; | ||
| 139 | #endif | ||
| 140 | } | ||
| 141 | return true; | ||
| 142 | } | ||
| 143 | |||
| 144 | void rgb_matrix_set_suspend_state(bool state) { | ||
| 145 | g_suspend_state = state; | ||
| 146 | } | ||
| 147 | |||
| 148 | void rgb_matrix_test(void) { | ||
| 149 | // Mask out bits 4 and 5 | ||
| 150 | // This 2-bit value will stay the same for 16 ticks. | ||
| 151 | switch ( (g_tick & 0x30) >> 4 ) | ||
| 152 | { | ||
| 153 | case 0: | ||
| 154 | { | ||
| 155 | rgb_matrix_set_color_all( 20, 0, 0 ); | ||
| 156 | break; | ||
| 157 | } | ||
| 158 | case 1: | ||
| 159 | { | ||
| 160 | rgb_matrix_set_color_all( 0, 20, 0 ); | ||
| 161 | break; | ||
| 162 | } | ||
| 163 | case 2: | ||
| 164 | { | ||
| 165 | rgb_matrix_set_color_all( 0, 0, 20 ); | ||
| 166 | break; | ||
| 167 | } | ||
| 168 | case 3: | ||
| 169 | { | ||
| 170 | rgb_matrix_set_color_all( 20, 20, 20 ); | ||
| 171 | break; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | // This tests the LEDs | ||
| 177 | // Note that it will change the LED control registers | ||
| 178 | // in the LED drivers, and leave them in an invalid | ||
| 179 | // state for other backlight effects. | ||
| 180 | // ONLY USE THIS FOR TESTING LEDS! | ||
| 181 | void rgb_matrix_single_LED_test(void) { | ||
| 182 | static uint8_t color = 0; // 0,1,2 for R,G,B | ||
| 183 | static uint8_t row = 0; | ||
| 184 | static uint8_t column = 0; | ||
| 185 | |||
| 186 | static uint8_t tick = 0; | ||
| 187 | tick++; | ||
| 188 | |||
| 189 | if ( tick > 2 ) | ||
| 190 | { | ||
| 191 | tick = 0; | ||
| 192 | column++; | ||
| 193 | } | ||
| 194 | if ( column > MATRIX_COLS ) | ||
| 195 | { | ||
| 196 | column = 0; | ||
| 197 | row++; | ||
| 198 | } | ||
| 199 | if ( row > MATRIX_ROWS ) | ||
| 200 | { | ||
| 201 | row = 0; | ||
| 202 | color++; | ||
| 203 | } | ||
| 204 | if ( color > 2 ) | ||
| 205 | { | ||
| 206 | color = 0; | ||
| 207 | } | ||
| 208 | |||
| 209 | uint8_t led[8], led_count; | ||
| 210 | map_row_column_to_led(row,column,led,&led_count); | ||
| 211 | for(uint8_t i = 0; i < led_count; i++) { | ||
| 212 | rgb_matrix_set_color_all( 40, 40, 40 ); | ||
| 213 | rgb_matrix_test_led( led[i], color==0, color==1, color==2 ); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | // All LEDs off | ||
| 218 | void rgb_matrix_all_off(void) { | ||
| 219 | rgb_matrix_set_color_all( 0, 0, 0 ); | ||
| 220 | } | ||
| 221 | |||
| 222 | // Solid color | ||
| 223 | void rgb_matrix_solid_color(void) { | ||
| 224 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | ||
| 225 | RGB rgb = hsv_to_rgb( hsv ); | ||
| 226 | rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b ); | ||
| 227 | } | ||
| 228 | |||
| 229 | void rgb_matrix_solid_reactive(void) { | ||
| 230 | // Relies on hue being 8-bit and wrapping | ||
| 231 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | ||
| 232 | { | ||
| 233 | uint16_t offset2 = g_key_hit[i]<<2; | ||
| 234 | offset2 = (offset2<=130) ? (130-offset2) : 0; | ||
| 235 | |||
| 236 | HSV hsv = { .h = rgb_matrix_config.hue+offset2, .s = 255, .v = rgb_matrix_config.val }; | ||
| 237 | RGB rgb = hsv_to_rgb( hsv ); | ||
| 238 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | // alphas = color1, mods = color2 | ||
| 243 | void rgb_matrix_alphas_mods(void) { | ||
| 244 | |||
| 245 | RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } ); | ||
| 246 | RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } ); | ||
| 247 | |||
| 248 | rgb_led led; | ||
| 249 | for (int i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
| 250 | led = g_rgb_leds[i]; | ||
| 251 | if ( led.matrix_co.raw < 0xFF ) { | ||
| 252 | if ( led.modifier ) | ||
| 253 | { | ||
| 254 | rgb_matrix_set_color( i, rgb2.r, rgb2.g, rgb2.b ); | ||
| 255 | } | ||
| 256 | else | ||
| 257 | { | ||
| 258 | rgb_matrix_set_color( i, rgb1.r, rgb1.g, rgb1.b ); | ||
| 259 | } | ||
| 260 | } | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | void rgb_matrix_gradient_up_down(void) { | ||
| 265 | int16_t h1 = rgb_matrix_config.hue; | ||
| 266 | int16_t h2 = (rgb_matrix_config.hue + 180) % 360; | ||
| 267 | int16_t deltaH = h2 - h1; | ||
| 268 | |||
| 269 | // Take the shortest path between hues | ||
| 270 | if ( deltaH > 127 ) | ||
| 271 | { | ||
| 272 | deltaH -= 256; | ||
| 273 | } | ||
| 274 | else if ( deltaH < -127 ) | ||
| 275 | { | ||
| 276 | deltaH += 256; | ||
| 277 | } | ||
| 278 | // Divide delta by 4, this gives the delta per row | ||
| 279 | deltaH /= 4; | ||
| 280 | |||
| 281 | int16_t s1 = rgb_matrix_config.sat; | ||
| 282 | int16_t s2 = rgb_matrix_config.hue; | ||
| 283 | int16_t deltaS = ( s2 - s1 ) / 4; | ||
| 284 | |||
| 285 | HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val }; | ||
| 286 | RGB rgb; | ||
| 287 | Point point; | ||
| 288 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | ||
| 289 | { | ||
| 290 | // map_led_to_point( i, &point ); | ||
| 291 | point = g_rgb_leds[i].point; | ||
| 292 | // The y range will be 0..64, map this to 0..4 | ||
| 293 | uint8_t y = (point.y>>4); | ||
| 294 | // Relies on hue being 8-bit and wrapping | ||
| 295 | hsv.h = rgb_matrix_config.hue + ( deltaH * y ); | ||
| 296 | hsv.s = rgb_matrix_config.sat + ( deltaS * y ); | ||
| 297 | rgb = hsv_to_rgb( hsv ); | ||
| 298 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | void rgb_matrix_raindrops(bool initialize) { | ||
| 303 | int16_t h1 = rgb_matrix_config.hue; | ||
| 304 | int16_t h2 = (rgb_matrix_config.hue + 180) % 360; | ||
| 305 | int16_t deltaH = h2 - h1; | ||
| 306 | deltaH /= 4; | ||
| 307 | |||
| 308 | // Take the shortest path between hues | ||
| 309 | if ( deltaH > 127 ) | ||
| 310 | { | ||
| 311 | deltaH -= 256; | ||
| 312 | } | ||
| 313 | else if ( deltaH < -127 ) | ||
| 314 | { | ||
| 315 | deltaH += 256; | ||
| 316 | } | ||
| 317 | |||
| 318 | int16_t s1 = rgb_matrix_config.sat; | ||
| 319 | int16_t s2 = rgb_matrix_config.sat; | ||
| 320 | int16_t deltaS = ( s2 - s1 ) / 4; | ||
| 321 | |||
| 322 | HSV hsv; | ||
| 323 | RGB rgb; | ||
| 324 | |||
| 325 | // Change one LED every tick | ||
| 326 | uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % DRIVER_LED_TOTAL : 255; | ||
| 327 | |||
| 328 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | ||
| 329 | { | ||
| 330 | // If initialize, all get set to random colors | ||
| 331 | // If not, all but one will stay the same as before. | ||
| 332 | if ( initialize || i == led_to_change ) | ||
| 333 | { | ||
| 334 | hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) ); | ||
| 335 | hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) ); | ||
| 336 | // Override brightness with global brightness control | ||
| 337 | hsv.v = rgb_matrix_config.val; | ||
| 338 | |||
| 339 | rgb = hsv_to_rgb( hsv ); | ||
| 340 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 341 | } | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | void rgb_matrix_cycle_all(void) { | ||
| 346 | uint8_t offset = g_tick & 0xFF; | ||
| 347 | |||
| 348 | rgb_led led; | ||
| 349 | |||
| 350 | // Relies on hue being 8-bit and wrapping | ||
| 351 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | ||
| 352 | { | ||
| 353 | // map_index_to_led(i, &led); | ||
| 354 | led = g_rgb_leds[i]; | ||
| 355 | if (led.matrix_co.raw < 0xFF) { | ||
| 356 | uint16_t offset2 = g_key_hit[i]<<2; | ||
| 357 | offset2 = (offset2<=63) ? (63-offset2) : 0; | ||
| 358 | |||
| 359 | HSV hsv = { .h = offset+offset2, .s = 255, .v = rgb_matrix_config.val }; | ||
| 360 | RGB rgb = hsv_to_rgb( hsv ); | ||
| 361 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 362 | } | ||
| 363 | } | ||
| 364 | } | ||
| 365 | |||
| 366 | void rgb_matrix_cycle_left_right(void) { | ||
| 367 | uint8_t offset = g_tick & 0xFF; | ||
| 368 | HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val }; | ||
| 369 | RGB rgb; | ||
| 370 | Point point; | ||
| 371 | rgb_led led; | ||
| 372 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | ||
| 373 | { | ||
| 374 | // map_index_to_led(i, &led); | ||
| 375 | led = g_rgb_leds[i]; | ||
| 376 | if (led.matrix_co.raw < 0xFF) { | ||
| 377 | uint16_t offset2 = g_key_hit[i]<<2; | ||
| 378 | offset2 = (offset2<=63) ? (63-offset2) : 0; | ||
| 379 | |||
| 380 | // map_led_to_point( i, &point ); | ||
| 381 | point = g_rgb_leds[i].point; | ||
| 382 | // Relies on hue being 8-bit and wrapping | ||
| 383 | hsv.h = point.x + offset + offset2; | ||
| 384 | rgb = hsv_to_rgb( hsv ); | ||
| 385 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 386 | } | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | void rgb_matrix_cycle_up_down(void) { | ||
| 391 | uint8_t offset = g_tick & 0xFF; | ||
| 392 | HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val }; | ||
| 393 | RGB rgb; | ||
| 394 | Point point; | ||
| 395 | rgb_led led; | ||
| 396 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | ||
| 397 | { | ||
| 398 | // map_index_to_led(i, &led); | ||
| 399 | led = g_rgb_leds[i]; | ||
| 400 | if (led.matrix_co.raw < 0xFF) { | ||
| 401 | uint16_t offset2 = g_key_hit[i]<<2; | ||
| 402 | offset2 = (offset2<=63) ? (63-offset2) : 0; | ||
| 403 | |||
| 404 | // map_led_to_point( i, &point ); | ||
| 405 | point = g_rgb_leds[i].point; | ||
| 406 | // Relies on hue being 8-bit and wrapping | ||
| 407 | hsv.h = point.y + offset + offset2; | ||
| 408 | rgb = hsv_to_rgb( hsv ); | ||
| 409 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 410 | } | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | void rgb_matrix_dual_beacon(void) { | ||
| 416 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | ||
| 417 | RGB rgb; | ||
| 418 | rgb_led led; | ||
| 419 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
| 420 | led = g_rgb_leds[i]; | ||
| 421 | hsv.h = ((led.point.y - 32.0)* cos(g_tick * PI / 128) / 32 + (led.point.x - 112.0) * sin(g_tick * PI / 128) / (112)) * (180) + rgb_matrix_config.hue; | ||
| 422 | rgb = hsv_to_rgb( hsv ); | ||
| 423 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 424 | } | ||
| 425 | } | ||
| 426 | |||
| 427 | void rgb_matrix_rainbow_beacon(void) { | ||
| 428 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | ||
| 429 | RGB rgb; | ||
| 430 | rgb_led led; | ||
| 431 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
| 432 | led = g_rgb_leds[i]; | ||
| 433 | hsv.h = 1.5 * (led.point.y - 32.0)* cos(g_tick * PI / 128) + 1.5 * (led.point.x - 112.0) * sin(g_tick * PI / 128) + rgb_matrix_config.hue; | ||
| 434 | rgb = hsv_to_rgb( hsv ); | ||
| 435 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | void rgb_matrix_rainbow_pinwheels(void) { | ||
| 440 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | ||
| 441 | RGB rgb; | ||
| 442 | rgb_led led; | ||
| 443 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
| 444 | led = g_rgb_leds[i]; | ||
| 445 | hsv.h = 2 * (led.point.y - 32.0)* cos(g_tick * PI / 128) + 2 * (66 - abs(led.point.x - 112.0)) * sin(g_tick * PI / 128) + rgb_matrix_config.hue; | ||
| 446 | rgb = hsv_to_rgb( hsv ); | ||
| 447 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 448 | } | ||
| 449 | } | ||
| 450 | |||
| 451 | void rgb_matrix_rainbow_moving_chevron(void) { | ||
| 452 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | ||
| 453 | RGB rgb; | ||
| 454 | rgb_led led; | ||
| 455 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
| 456 | led = g_rgb_leds[i]; | ||
| 457 | // uint8_t r = g_tick; | ||
| 458 | uint8_t r = 32; | ||
| 459 | hsv.h = 1.5 * abs(led.point.y - 32.0)* sin(r * PI / 128) + 1.5 * (led.point.x - (g_tick / 256.0 * 224)) * cos(r * PI / 128) + rgb_matrix_config.hue; | ||
| 460 | rgb = hsv_to_rgb( hsv ); | ||
| 461 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 462 | } | ||
| 463 | } | ||
| 464 | |||
| 465 | |||
| 466 | void rgb_matrix_jellybean_raindrops( bool initialize ) { | ||
| 467 | HSV hsv; | ||
| 468 | RGB rgb; | ||
| 469 | |||
| 470 | // Change one LED every tick | ||
| 471 | uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % DRIVER_LED_TOTAL : 255; | ||
| 472 | |||
| 473 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | ||
| 474 | { | ||
| 475 | // If initialize, all get set to random colors | ||
| 476 | // If not, all but one will stay the same as before. | ||
| 477 | if ( initialize || i == led_to_change ) | ||
| 478 | { | ||
| 479 | hsv.h = rand() & 0xFF; | ||
| 480 | hsv.s = rand() & 0xFF; | ||
| 481 | // Override brightness with global brightness control | ||
| 482 | hsv.v = rgb_matrix_config.val; | ||
| 483 | |||
| 484 | rgb = hsv_to_rgb( hsv ); | ||
| 485 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 486 | } | ||
| 487 | } | ||
| 488 | } | ||
| 489 | |||
| 490 | void rgb_matrix_multisplash(void) { | ||
| 491 | // if (g_any_key_hit < 0xFF) { | ||
| 492 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | ||
| 493 | RGB rgb; | ||
| 494 | rgb_led led; | ||
| 495 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
| 496 | led = g_rgb_leds[i]; | ||
| 497 | uint16_t c = 0, d = 0; | ||
| 498 | rgb_led last_led; | ||
| 499 | // if (g_last_led_count) { | ||
| 500 | for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) { | ||
| 501 | last_led = g_rgb_leds[g_last_led_hit[last_i]]; | ||
| 502 | uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2)); | ||
| 503 | uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist; | ||
| 504 | c += MIN(MAX(effect, 0), 255); | ||
| 505 | d += 255 - MIN(MAX(effect, 0), 255); | ||
| 506 | } | ||
| 507 | // } else { | ||
| 508 | // d = 255; | ||
| 509 | // } | ||
| 510 | hsv.h = (rgb_matrix_config.hue + c) % 256; | ||
| 511 | hsv.v = MAX(MIN(d, 255), 0); | ||
| 512 | rgb = hsv_to_rgb( hsv ); | ||
| 513 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 514 | } | ||
| 515 | // } else { | ||
| 516 | // rgb_matrix_set_color_all( 0, 0, 0 ); | ||
| 517 | // } | ||
| 518 | } | ||
| 519 | |||
| 520 | |||
| 521 | void rgb_matrix_splash(void) { | ||
| 522 | g_last_led_count = MIN(g_last_led_count, 1); | ||
| 523 | rgb_matrix_multisplash(); | ||
| 524 | } | ||
| 525 | |||
| 526 | |||
| 527 | void rgb_matrix_solid_multisplash(void) { | ||
| 528 | // if (g_any_key_hit < 0xFF) { | ||
| 529 | HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val }; | ||
| 530 | RGB rgb; | ||
| 531 | rgb_led led; | ||
| 532 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) { | ||
| 533 | led = g_rgb_leds[i]; | ||
| 534 | uint16_t d = 0; | ||
| 535 | rgb_led last_led; | ||
| 536 | // if (g_last_led_count) { | ||
| 537 | for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) { | ||
| 538 | last_led = g_rgb_leds[g_last_led_hit[last_i]]; | ||
| 539 | uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2)); | ||
| 540 | uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist; | ||
| 541 | d += 255 - MIN(MAX(effect, 0), 255); | ||
| 542 | } | ||
| 543 | // } else { | ||
| 544 | // d = 255; | ||
| 545 | // } | ||
| 546 | hsv.v = MAX(MIN(d, 255), 0); | ||
| 547 | rgb = hsv_to_rgb( hsv ); | ||
| 548 | rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 549 | } | ||
| 550 | // } else { | ||
| 551 | // rgb_matrix_set_color_all( 0, 0, 0 ); | ||
| 552 | // } | ||
| 553 | } | ||
| 554 | |||
| 555 | |||
| 556 | void rgb_matrix_solid_splash(void) { | ||
| 557 | g_last_led_count = MIN(g_last_led_count, 1); | ||
| 558 | rgb_matrix_solid_multisplash(); | ||
| 559 | } | ||
| 560 | |||
| 561 | |||
| 562 | // Needs eeprom access that we don't have setup currently | ||
| 563 | |||
| 564 | void rgb_matrix_custom(void) { | ||
| 565 | // HSV hsv; | ||
| 566 | // RGB rgb; | ||
| 567 | // for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | ||
| 568 | // { | ||
| 569 | // backlight_get_key_color(i, &hsv); | ||
| 570 | // // Override brightness with global brightness control | ||
| 571 | // hsv.v = rgb_matrix_config.val; | ||
| 572 | // rgb = hsv_to_rgb( hsv ); | ||
| 573 | // rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b ); | ||
| 574 | // } | ||
| 575 | } | ||
| 576 | |||
| 577 | void rgb_matrix_task(void) { | ||
| 578 | if (!rgb_matrix_config.enable) { | ||
| 579 | rgb_matrix_all_off(); | ||
| 580 | return; | ||
| 581 | } | ||
| 582 | // delay 1 second before driving LEDs or doing anything else | ||
| 583 | static uint8_t startup_tick = 0; | ||
| 584 | if ( startup_tick < 20 ) { | ||
| 585 | startup_tick++; | ||
| 586 | return; | ||
| 587 | } | ||
| 588 | |||
| 589 | g_tick++; | ||
| 590 | |||
| 591 | if ( g_any_key_hit < 0xFFFFFFFF ) { | ||
| 592 | g_any_key_hit++; | ||
| 593 | } | ||
| 594 | |||
| 595 | for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) { | ||
| 596 | if ( g_key_hit[led] < 255 ) { | ||
| 597 | if (g_key_hit[led] == 254) | ||
| 598 | g_last_led_count = MAX(g_last_led_count - 1, 0); | ||
| 599 | g_key_hit[led]++; | ||
| 600 | } | ||
| 601 | } | ||
| 602 | |||
| 603 | // Factory default magic value | ||
| 604 | if ( rgb_matrix_config.mode == 255 ) { | ||
| 605 | rgb_matrix_test(); | ||
| 606 | return; | ||
| 607 | } | ||
| 608 | |||
| 609 | // Ideally we would also stop sending zeros to the LED driver PWM buffers | ||
| 610 | // while suspended and just do a software shutdown. This is a cheap hack for now. | ||
| 611 | bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) || | ||
| 612 | (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20)); | ||
| 613 | uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode; | ||
| 614 | |||
| 615 | // Keep track of the effect used last time, | ||
| 616 | // detect change in effect, so each effect can | ||
| 617 | // have an optional initialization. | ||
| 618 | static uint8_t effect_last = 255; | ||
| 619 | bool initialize = effect != effect_last; | ||
| 620 | effect_last = effect; | ||
| 621 | |||
| 622 | // this gets ticked at 20 Hz. | ||
| 623 | // each effect can opt to do calculations | ||
| 624 | // and/or request PWM buffer updates. | ||
| 625 | switch ( effect ) { | ||
| 626 | case RGB_MATRIX_SOLID_COLOR: | ||
| 627 | rgb_matrix_solid_color(); | ||
| 628 | break; | ||
| 629 | case RGB_MATRIX_SOLID_REACTIVE: | ||
| 630 | rgb_matrix_solid_reactive(); | ||
| 631 | break; | ||
| 632 | case RGB_MATRIX_ALPHAS_MODS: | ||
| 633 | rgb_matrix_alphas_mods(); | ||
| 634 | break; | ||
| 635 | case RGB_MATRIX_DUAL_BEACON: | ||
| 636 | rgb_matrix_dual_beacon(); | ||
| 637 | break; | ||
| 638 | case RGB_MATRIX_GRADIENT_UP_DOWN: | ||
| 639 | rgb_matrix_gradient_up_down(); | ||
| 640 | break; | ||
| 641 | case RGB_MATRIX_RAINDROPS: | ||
| 642 | rgb_matrix_raindrops( initialize ); | ||
| 643 | break; | ||
| 644 | case RGB_MATRIX_CYCLE_ALL: | ||
| 645 | rgb_matrix_cycle_all(); | ||
| 646 | break; | ||
| 647 | case RGB_MATRIX_CYCLE_LEFT_RIGHT: | ||
| 648 | rgb_matrix_cycle_left_right(); | ||
| 649 | break; | ||
| 650 | case RGB_MATRIX_CYCLE_UP_DOWN: | ||
| 651 | rgb_matrix_cycle_up_down(); | ||
| 652 | break; | ||
| 653 | case RGB_MATRIX_RAINBOW_BEACON: | ||
| 654 | rgb_matrix_rainbow_beacon(); | ||
| 655 | break; | ||
| 656 | case RGB_MATRIX_RAINBOW_PINWHEELS: | ||
| 657 | rgb_matrix_rainbow_pinwheels(); | ||
| 658 | break; | ||
| 659 | case RGB_MATRIX_RAINBOW_MOVING_CHEVRON: | ||
| 660 | rgb_matrix_rainbow_moving_chevron(); | ||
| 661 | break; | ||
| 662 | case RGB_MATRIX_JELLYBEAN_RAINDROPS: | ||
| 663 | rgb_matrix_jellybean_raindrops( initialize ); | ||
| 664 | break; | ||
| 665 | #ifdef RGB_MATRIX_KEYPRESSES | ||
| 666 | case RGB_MATRIX_SPLASH: | ||
| 667 | rgb_matrix_splash(); | ||
| 668 | break; | ||
| 669 | case RGB_MATRIX_MULTISPLASH: | ||
| 670 | rgb_matrix_multisplash(); | ||
| 671 | break; | ||
| 672 | case RGB_MATRIX_SOLID_SPLASH: | ||
| 673 | rgb_matrix_solid_splash(); | ||
| 674 | break; | ||
| 675 | case RGB_MATRIX_SOLID_MULTISPLASH: | ||
| 676 | rgb_matrix_solid_multisplash(); | ||
| 677 | break; | ||
| 678 | #endif | ||
| 679 | default: | ||
| 680 | rgb_matrix_custom(); | ||
| 681 | break; | ||
| 682 | } | ||
| 683 | |||
| 684 | if ( ! suspend_backlight ) { | ||
| 685 | rgb_matrix_indicators(); | ||
| 686 | } | ||
| 687 | |||
| 688 | } | ||
| 689 | |||
| 690 | void rgb_matrix_indicators(void) { | ||
| 691 | rgb_matrix_indicators_kb(); | ||
| 692 | rgb_matrix_indicators_user(); | ||
| 693 | } | ||
| 694 | |||
| 695 | __attribute__((weak)) | ||
| 696 | void rgb_matrix_indicators_kb(void) {} | ||
| 697 | |||
| 698 | __attribute__((weak)) | ||
| 699 | void rgb_matrix_indicators_user(void) {} | ||
| 700 | |||
| 701 | |||
| 702 | // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column ) | ||
| 703 | // { | ||
| 704 | // if ( row >= MATRIX_ROWS ) | ||
| 705 | // { | ||
| 706 | // // Special value, 255=none, 254=all | ||
| 707 | // *index = row; | ||
| 708 | // } | ||
| 709 | // else | ||
| 710 | // { | ||
| 711 | // // This needs updated to something like | ||
| 712 | // // uint8_t led[8], led_count; | ||
| 713 | // // map_row_column_to_led(row,column,led,&led_count); | ||
| 714 | // // for(uint8_t i = 0; i < led_count; i++) | ||
| 715 | // map_row_column_to_led( row, column, index ); | ||
| 716 | // } | ||
| 717 | // } | ||
| 718 | |||
| 719 | void rgb_matrix_init_drivers(void) { | ||
| 720 | //sei(); | ||
| 721 | |||
| 722 | // Initialize TWI | ||
| 723 | TWIInit(); | ||
| 724 | IS31FL3731_init( DRIVER_ADDR_1 ); | ||
| 725 | IS31FL3731_init( DRIVER_ADDR_2 ); | ||
| 726 | |||
| 727 | for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) { | ||
| 728 | bool enabled = true; | ||
| 729 | // This only caches it for later | ||
| 730 | IS31FL3731_set_led_control_register( index, enabled, enabled, enabled ); | ||
| 731 | } | ||
| 732 | // This actually updates the LED drivers | ||
| 733 | IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 ); | ||
| 734 | |||
| 735 | // TODO: put the 1 second startup delay here? | ||
| 736 | |||
| 737 | // clear the key hits | ||
| 738 | for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) { | ||
| 739 | g_key_hit[led] = 255; | ||
| 740 | } | ||
| 741 | |||
| 742 | |||
| 743 | if (!eeconfig_is_enabled()) { | ||
| 744 | dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n"); | ||
| 745 | eeconfig_init(); | ||
| 746 | eeconfig_update_rgb_matrix_default(); | ||
| 747 | } | ||
| 748 | rgb_matrix_config.raw = eeconfig_read_rgb_matrix(); | ||
| 749 | if (!rgb_matrix_config.mode) { | ||
| 750 | dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n"); | ||
| 751 | eeconfig_update_rgb_matrix_default(); | ||
| 752 | rgb_matrix_config.raw = eeconfig_read_rgb_matrix(); | ||
| 753 | } | ||
| 754 | eeconfig_debug_rgb_matrix(); // display current eeprom values | ||
| 755 | } | ||
| 756 | |||
| 757 | // Deals with the messy details of incrementing an integer | ||
| 758 | uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) { | ||
| 759 | int16_t new_value = value; | ||
| 760 | new_value += step; | ||
| 761 | return MIN( MAX( new_value, min ), max ); | ||
| 762 | } | ||
| 763 | |||
| 764 | uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) { | ||
| 765 | int16_t new_value = value; | ||
| 766 | new_value -= step; | ||
| 767 | return MIN( MAX( new_value, min ), max ); | ||
| 768 | } | ||
| 769 | |||
| 770 | // void *backlight_get_custom_key_color_eeprom_address( uint8_t led ) | ||
| 771 | // { | ||
| 772 | // // 3 bytes per color | ||
| 773 | // return EECONFIG_RGB_MATRIX + ( led * 3 ); | ||
| 774 | // } | ||
| 775 | |||
| 776 | // void backlight_get_key_color( uint8_t led, HSV *hsv ) | ||
| 777 | // { | ||
| 778 | // void *address = backlight_get_custom_key_color_eeprom_address( led ); | ||
| 779 | // hsv->h = eeprom_read_byte(address); | ||
| 780 | // hsv->s = eeprom_read_byte(address+1); | ||
| 781 | // hsv->v = eeprom_read_byte(address+2); | ||
| 782 | // } | ||
| 783 | |||
| 784 | // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv ) | ||
| 785 | // { | ||
| 786 | // uint8_t led[8], led_count; | ||
| 787 | // map_row_column_to_led(row,column,led,&led_count); | ||
| 788 | // for(uint8_t i = 0; i < led_count; i++) { | ||
| 789 | // if ( led[i] < DRIVER_LED_TOTAL ) | ||
| 790 | // { | ||
| 791 | // void *address = backlight_get_custom_key_color_eeprom_address(led[i]); | ||
| 792 | // eeprom_update_byte(address, hsv.h); | ||
| 793 | // eeprom_update_byte(address+1, hsv.s); | ||
| 794 | // eeprom_update_byte(address+2, hsv.v); | ||
| 795 | // } | ||
| 796 | // } | ||
| 797 | // } | ||
| 798 | |||
| 799 | void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) { | ||
| 800 | for ( int i=0; i<DRIVER_LED_TOTAL; i++ ) | ||
| 801 | { | ||
| 802 | if ( i == index ) | ||
| 803 | { | ||
| 804 | IS31FL3731_set_led_control_register( i, red, green, blue ); | ||
| 805 | } | ||
| 806 | else | ||
| 807 | { | ||
| 808 | IS31FL3731_set_led_control_register( i, false, false, false ); | ||
| 809 | } | ||
| 810 | } | ||
| 811 | } | ||
| 812 | |||
| 813 | uint32_t rgb_matrix_get_tick(void) { | ||
| 814 | return g_tick; | ||
| 815 | } | ||
| 816 | |||
| 817 | void rgblight_toggle(void) { | ||
| 818 | rgb_matrix_config.enable ^= 1; | ||
| 819 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | ||
| 820 | } | ||
| 821 | |||
| 822 | void rgblight_step(void) { | ||
| 823 | rgb_matrix_config.mode++; | ||
| 824 | if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX) | ||
| 825 | rgb_matrix_config.mode = 1; | ||
| 826 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | ||
| 827 | } | ||
| 828 | |||
| 829 | void rgblight_step_reverse(void) { | ||
| 830 | rgb_matrix_config.mode--; | ||
| 831 | if (rgb_matrix_config.mode <= 1) | ||
| 832 | rgb_matrix_config.mode = (RGB_MATRIX_EFFECT_MAX - 1); | ||
| 833 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | ||
| 834 | } | ||
| 835 | |||
| 836 | void rgblight_increase_hue(void) { | ||
| 837 | rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 ); | ||
| 838 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | ||
| 839 | } | ||
| 840 | |||
| 841 | void rgblight_decrease_hue(void) { | ||
| 842 | rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 ); | ||
| 843 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | ||
| 844 | } | ||
| 845 | |||
| 846 | void rgblight_increase_sat(void) { | ||
| 847 | rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 ); | ||
| 848 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | ||
| 849 | } | ||
| 850 | |||
| 851 | void rgblight_decrease_sat(void) { | ||
| 852 | rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 ); | ||
| 853 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | ||
| 854 | } | ||
| 855 | |||
| 856 | void rgblight_increase_val(void) { | ||
| 857 | rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, 255 ); | ||
| 858 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | ||
| 859 | } | ||
| 860 | |||
| 861 | void rgblight_decrease_val(void) { | ||
| 862 | rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, 255 ); | ||
| 863 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | ||
| 864 | } | ||
| 865 | |||
| 866 | void rgblight_mode(uint8_t mode) { | ||
| 867 | rgb_matrix_config.mode = mode; | ||
| 868 | eeconfig_update_rgb_matrix(rgb_matrix_config.raw); | ||
| 869 | } | ||
| 870 | |||
| 871 | uint32_t rgblight_get_mode(void) { | ||
| 872 | return rgb_matrix_config.mode; | ||
| 873 | } | ||
diff --git a/quantum/rgb_matrix.h b/quantum/rgb_matrix.h new file mode 100644 index 000000000..ef93c6d5c --- /dev/null +++ b/quantum/rgb_matrix.h | |||
| @@ -0,0 +1,135 @@ | |||
| 1 | /* Copyright 2017 Jason Williams | ||
| 2 | * Copyright 2017 Jack Humbert | ||
| 3 | * | ||
| 4 | * This program is free software: you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License as published by | ||
| 6 | * the Free Software Foundation, either version 2 of the License, or | ||
| 7 | * (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #ifndef RGB_MATRIX_H | ||
| 19 | #define RGB_MATRIX_H | ||
| 20 | |||
| 21 | #include <stdint.h> | ||
| 22 | #include <stdbool.h> | ||
| 23 | #include "color.h" | ||
| 24 | #include "is31fl3731.h" | ||
| 25 | #include "quantum.h" | ||
| 26 | |||
| 27 | typedef struct Point { | ||
| 28 | uint8_t x; | ||
| 29 | uint8_t y; | ||
| 30 | } __attribute__((packed)) Point; | ||
| 31 | |||
| 32 | typedef struct rgb_led { | ||
| 33 | union { | ||
| 34 | uint8_t raw; | ||
| 35 | struct { | ||
| 36 | uint8_t row:4; // 16 max | ||
| 37 | uint8_t col:4; // 16 max | ||
| 38 | }; | ||
| 39 | } matrix_co; | ||
| 40 | Point point; | ||
| 41 | uint8_t modifier:1; | ||
| 42 | } __attribute__((packed)) rgb_led; | ||
| 43 | |||
| 44 | |||
| 45 | extern const rgb_led g_rgb_leds[DRIVER_LED_TOTAL]; | ||
| 46 | |||
| 47 | typedef struct | ||
| 48 | { | ||
| 49 | HSV color; | ||
| 50 | uint8_t index; | ||
| 51 | } rgb_indicator; | ||
| 52 | |||
| 53 | typedef union { | ||
| 54 | uint32_t raw; | ||
| 55 | struct { | ||
| 56 | bool enable :1; | ||
| 57 | uint8_t mode :6; | ||
| 58 | uint16_t hue :9; | ||
| 59 | uint8_t sat :8; | ||
| 60 | uint8_t val :8; | ||
| 61 | }; | ||
| 62 | } rgb_config_t; | ||
| 63 | |||
| 64 | enum rgb_matrix_effects { | ||
| 65 | RGB_MATRIX_SOLID_COLOR = 1, | ||
| 66 | RGB_MATRIX_SOLID_REACTIVE, | ||
| 67 | RGB_MATRIX_ALPHAS_MODS, | ||
| 68 | RGB_MATRIX_DUAL_BEACON, | ||
| 69 | RGB_MATRIX_GRADIENT_UP_DOWN, | ||
| 70 | RGB_MATRIX_RAINDROPS, | ||
| 71 | RGB_MATRIX_CYCLE_ALL, | ||
| 72 | RGB_MATRIX_CYCLE_LEFT_RIGHT, | ||
| 73 | RGB_MATRIX_CYCLE_UP_DOWN, | ||
| 74 | RGB_MATRIX_RAINBOW_BEACON, | ||
| 75 | RGB_MATRIX_RAINBOW_PINWHEELS, | ||
| 76 | RGB_MATRIX_RAINBOW_MOVING_CHEVRON, | ||
| 77 | RGB_MATRIX_JELLYBEAN_RAINDROPS, | ||
| 78 | #ifdef RGB_MATRIX_KEYPRESSES | ||
| 79 | RGB_MATRIX_SPLASH, | ||
| 80 | RGB_MATRIX_MULTISPLASH, | ||
| 81 | RGB_MATRIX_SOLID_SPLASH, | ||
| 82 | RGB_MATRIX_SOLID_MULTISPLASH, | ||
| 83 | #endif | ||
| 84 | RGB_MATRIX_EFFECT_MAX | ||
| 85 | }; | ||
| 86 | |||
| 87 | void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ); | ||
| 88 | |||
| 89 | // This runs after another backlight effect and replaces | ||
| 90 | // colors already set | ||
| 91 | void rgb_matrix_indicators(void); | ||
| 92 | void rgb_matrix_indicators_kb(void); | ||
| 93 | void rgb_matrix_indicators_user(void); | ||
| 94 | |||
| 95 | void rgb_matrix_single_LED_test(void); | ||
| 96 | |||
| 97 | void rgb_matrix_init_drivers(void); | ||
| 98 | |||
| 99 | void rgb_matrix_set_suspend_state(bool state); | ||
| 100 | void rgb_matrix_set_indicator_state(uint8_t state); | ||
| 101 | |||
| 102 | |||
| 103 | void rgb_matrix_task(void); | ||
| 104 | |||
| 105 | // This should not be called from an interrupt | ||
| 106 | // (eg. from a timer interrupt). | ||
| 107 | // Call this while idle (in between matrix scans). | ||
| 108 | // If the buffer is dirty, it will update the driver with the buffer. | ||
| 109 | void rgb_matrix_update_pwm_buffers(void); | ||
| 110 | |||
| 111 | bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record); | ||
| 112 | |||
| 113 | void rgb_matrix_increase(void); | ||
| 114 | void rgb_matrix_decrease(void); | ||
| 115 | |||
| 116 | // void *backlight_get_key_color_eeprom_address(uint8_t led); | ||
| 117 | // void backlight_get_key_color( uint8_t led, HSV *hsv ); | ||
| 118 | // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv ); | ||
| 119 | |||
| 120 | void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ); | ||
| 121 | uint32_t rgb_matrix_get_tick(void); | ||
| 122 | |||
| 123 | void rgblight_toggle(void); | ||
| 124 | void rgblight_step(void); | ||
| 125 | void rgblight_step_reverse(void); | ||
| 126 | void rgblight_increase_hue(void); | ||
| 127 | void rgblight_decrease_hue(void); | ||
| 128 | void rgblight_increase_sat(void); | ||
| 129 | void rgblight_decrease_sat(void); | ||
| 130 | void rgblight_increase_val(void); | ||
| 131 | void rgblight_decrease_val(void); | ||
| 132 | void rgblight_mode(uint8_t mode); | ||
| 133 | uint32_t rgblight_get_mode(void); | ||
| 134 | |||
| 135 | #endif | ||
