]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - quantum/rgb_matrix.c
i2c fix
[qmk_firmware.git] / quantum / rgb_matrix.c
index 6cb0478f71efd872c4bccd28531e89a08e6b02f8..992ce99de986f1b32421423b9c3552019197ee16 100644 (file)
@@ -18,7 +18,7 @@
 
 #include "rgb_matrix.h"
 #include <avr/io.h>
-#include "TWIlib.h"
+#include "i2c_master.h"
 #include <util/delay.h>
 #include <avr/interrupt.h>
 #include "progmem.h"
@@ -69,6 +69,7 @@ void eeconfig_update_rgb_matrix_default(void) {
   rgb_matrix_config.hue = 0;
   rgb_matrix_config.sat = 255;
   rgb_matrix_config.val = 255;
+  rgb_matrix_config.speed = 0;
   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
 }
 void eeconfig_debug_rgb_matrix(void) {
@@ -78,6 +79,7 @@ void eeconfig_debug_rgb_matrix(void) {
   dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
   dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
   dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
+  dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
 }
 
 // Last led hit
@@ -322,8 +324,8 @@ void rgb_matrix_raindrops(bool initialize) {
     HSV hsv;
     RGB rgb;
 
-    // Change one LED every tick
-    uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % DRIVER_LED_TOTAL : 255;
+    // Change one LED every tick, make sure speed is not 0
+    uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
 
     for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
     {
@@ -343,7 +345,7 @@ void rgb_matrix_raindrops(bool initialize) {
 }
 
 void rgb_matrix_cycle_all(void) {
-    uint8_t offset = g_tick & 0xFF;
+    uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
 
     rgb_led led;
 
@@ -364,7 +366,7 @@ void rgb_matrix_cycle_all(void) {
 }
 
 void rgb_matrix_cycle_left_right(void) {
-    uint8_t offset = g_tick & 0xFF;
+    uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
     HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
     RGB rgb;
     Point point;
@@ -388,7 +390,7 @@ void rgb_matrix_cycle_left_right(void) {
 }
 
 void rgb_matrix_cycle_up_down(void) {
-    uint8_t offset = g_tick & 0xFF;
+    uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
     HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
     RGB rgb;
     Point point;
@@ -430,7 +432,7 @@ void rgb_matrix_rainbow_beacon(void) {
     rgb_led led;
     for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
         led = g_rgb_leds[i];
-        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;
+        hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.y - 32.0)* cos(g_tick * PI / 128) + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.x - 112.0) * sin(g_tick * PI / 128) + rgb_matrix_config.hue;
         rgb = hsv_to_rgb( hsv );
         rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
     }
@@ -442,7 +444,7 @@ void rgb_matrix_rainbow_pinwheels(void) {
     rgb_led led;
     for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
         led = g_rgb_leds[i];
-        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;
+        hsv.h = (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.y - 32.0)* cos(g_tick * PI / 128) + (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (66 - abs(led.point.x - 112.0)) * sin(g_tick * PI / 128) + rgb_matrix_config.hue;
         rgb = hsv_to_rgb( hsv );
         rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
     }
@@ -456,7 +458,7 @@ void rgb_matrix_rainbow_moving_chevron(void) {
         led = g_rgb_leds[i];
         // uint8_t r = g_tick;
         uint8_t r = 32;
-        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;
+        hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * abs(led.point.y - 32.0)* sin(r * PI / 128) + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.x - (g_tick / 256.0 * 224)) * cos(r * PI / 128) + rgb_matrix_config.hue;
         rgb = hsv_to_rgb( hsv );
         rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
     }
@@ -467,8 +469,8 @@ void rgb_matrix_jellybean_raindrops( bool initialize ) {
     HSV hsv;
     RGB rgb;
 
-    // Change one LED every tick
-    uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % DRIVER_LED_TOTAL : 255;
+    // Change one LED every tick, make sure speed is not 0
+    uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
 
     for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
     {
@@ -575,8 +577,10 @@ void rgb_matrix_custom(void) {
 }
 
 void rgb_matrix_task(void) {
+    static uint8_t toggle_enable_last = 255;
        if (!rgb_matrix_config.enable) {
        rgb_matrix_all_off();
+        toggle_enable_last = rgb_matrix_config.enable;
        return;
     }
     // delay 1 second before driving LEDs or doing anything else
@@ -616,8 +620,9 @@ void rgb_matrix_task(void) {
     // detect change in effect, so each effect can
     // have an optional initialization.
     static uint8_t effect_last = 255;
-    bool initialize = effect != effect_last;
+    bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last);
     effect_last = effect;
+    toggle_enable_last = rgb_matrix_config.enable;
 
     // this gets ticked at 20 Hz.
     // each effect can opt to do calculations
@@ -626,9 +631,6 @@ void rgb_matrix_task(void) {
         case RGB_MATRIX_SOLID_COLOR:
             rgb_matrix_solid_color();
             break;
-        case RGB_MATRIX_SOLID_REACTIVE:
-            rgb_matrix_solid_reactive();
-            break;
         case RGB_MATRIX_ALPHAS_MODS:
             rgb_matrix_alphas_mods();
             break;
@@ -663,6 +665,9 @@ void rgb_matrix_task(void) {
             rgb_matrix_jellybean_raindrops( initialize );
             break;
         #ifdef RGB_MATRIX_KEYPRESSES
+            case RGB_MATRIX_SOLID_REACTIVE:
+                rgb_matrix_solid_reactive();
+                break;
             case RGB_MATRIX_SPLASH:
                 rgb_matrix_splash();
                 break;
@@ -717,10 +722,8 @@ void rgb_matrix_indicators_user(void) {}
 // }
 
 void rgb_matrix_init_drivers(void) {
-    //sei();
-
     // Initialize TWI
-    TWIInit();
+    i2c_init();
     IS31FL3731_init( DRIVER_ADDR_1 );
     IS31FL3731_init( DRIVER_ADDR_2 );
 
@@ -828,8 +831,8 @@ void rgblight_step(void) {
 
 void rgblight_step_reverse(void) {
     rgb_matrix_config.mode--;
-    if (rgb_matrix_config.mode <= 1)
-        rgb_matrix_config.mode = (RGB_MATRIX_EFFECT_MAX - 1);
+    if (rgb_matrix_config.mode < 1)
+        rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
 }
 
@@ -863,6 +866,16 @@ void rgblight_decrease_val(void) {
     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
 }
 
+void rgblight_increase_speed(void) {
+    rgb_matrix_config.speed = increment( rgb_matrix_config.speed, 1, 0, 3 );
+    eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
+}
+
+void rgblight_decrease_speed(void) {
+    rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 );
+    eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
+}
+
 void rgblight_mode(uint8_t mode) {
     rgb_matrix_config.mode = mode;
     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);