]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - quantum/rgb_matrix.c
fix mousekey call
[qmk_firmware.git] / quantum / rgb_matrix.c
index 6cb0478f71efd872c4bccd28531e89a08e6b02f8..197bc1ac5e054541d9538982172b35aa6b2da838 100644 (file)
@@ -1,5 +1,6 @@
 /* Copyright 2017 Jason Williams
  * Copyright 2017 Jack Humbert
+ * Copyright 2018 Yiancar
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 
 #include "rgb_matrix.h"
-#include <avr/io.h>
-#include "TWIlib.h"
-#include <util/delay.h>
-#include <avr/interrupt.h>
+#include "i2c_master.h"
 #include "progmem.h"
 #include "config.h"
 #include "eeprom.h"
-#include "lufa.h"
 #include <math.h>
 
 rgb_config_t rgb_matrix_config;
 
+#ifndef MAX
+    #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
+#endif
+
+#ifndef MIN
+    #define MIN(a,b) ((a) < (b)? (a): (b))
+#endif
+
 #ifndef RGB_DISABLE_AFTER_TIMEOUT
     #define RGB_DISABLE_AFTER_TIMEOUT 0
 #endif
@@ -41,6 +46,10 @@ rgb_config_t rgb_matrix_config;
     #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
 #endif
 
+#if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > 255
+    #define RGB_MATRIX_MAXIMUM_BRIGHTNESS 255
+#endif
+
 bool g_suspend_state = false;
 
 // Global tick at 20 Hz
@@ -68,7 +77,8 @@ void eeconfig_update_rgb_matrix_default(void) {
   rgb_matrix_config.mode = RGB_MATRIX_CYCLE_LEFT_RIGHT;
   rgb_matrix_config.hue = 0;
   rgb_matrix_config.sat = 255;
-  rgb_matrix_config.val = 255;
+  rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
+  rgb_matrix_config.speed = 0;
   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
 }
 void eeconfig_debug_rgb_matrix(void) {
@@ -78,6 +88,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
@@ -99,21 +110,32 @@ void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t
     }
 }
 
-
 void rgb_matrix_update_pwm_buffers(void) {
+#ifdef IS31FL3731
     IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
     IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
+#elif defined(IS31FL3733)
+    IS31FL3733_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
+    IS31FL3733_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
+#endif
 }
 
 void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
+#ifdef IS31FL3731
     IS31FL3731_set_color( index, red, green, blue );
+#elif defined(IS31FL3733)
+    IS31FL3733_set_color( index, red, green, blue );
+#endif
 }
 
 void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
+#ifdef IS31FL3731
     IS31FL3731_set_color_all( red, green, blue );
+#elif defined(IS31FL3733)
+    IS31FL3733_set_color_all( red, green, blue );
+#endif
 }
 
-
 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
     if ( record->event.pressed ) {
         uint8_t led[8], led_count;
@@ -147,8 +169,9 @@ void rgb_matrix_set_suspend_state(bool state) {
 
 void rgb_matrix_test(void) {
     // Mask out bits 4 and 5
-    // This 2-bit value will stay the same for 16 ticks.
-    switch ( (g_tick & 0x30) >> 4 )
+    // Increase the factor to make the test animation slower (and reduce to make it faster)
+    uint8_t factor = 10;
+    switch ( (g_tick & (0b11 << factor)) >> factor )
     {
         case 0:
         {
@@ -215,7 +238,7 @@ void rgb_matrix_single_LED_test(void) {
 }
 
 // All LEDs off
-void rgb_matrix_all_off(void) { 
+void rgb_matrix_all_off(void) {
     rgb_matrix_set_color_all( 0, 0, 0 );
 }
 
@@ -241,7 +264,7 @@ void rgb_matrix_solid_reactive(void) {
 
 // alphas = color1, mods = color2
 void rgb_matrix_alphas_mods(void) {
+
     RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
     RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
 
@@ -322,8 +345,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 +366,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 +387,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 +411,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 +453,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 +465,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 );
     }
@@ -455,8 +478,8 @@ void rgb_matrix_rainbow_moving_chevron(void) {
     for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
         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;
+        uint8_t r = 128;
+        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 +490,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 +598,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 +641,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 +652,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 +686,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;
@@ -716,42 +742,56 @@ void rgb_matrix_indicators_user(void) {}
 //  }
 // }
 
-void rgb_matrix_init_drivers(void) {
-    //sei();
-
-    // Initialize TWI
-    TWIInit();
-    IS31FL3731_init( DRIVER_ADDR_1 );
-    IS31FL3731_init( DRIVER_ADDR_2 );
-
-    for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
-        bool enabled = true;
-        // This only caches it for later
-        IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
-    }
-    // This actually updates the LED drivers
-    IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
-
-    // TODO: put the 1 second startup delay here?
-
-    // clear the key hits
-    for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
-        g_key_hit[led] = 255;
-    }
-
+void rgb_matrix_init(void) {
+  rgb_matrix_setup_drivers();
+
+  // TODO: put the 1 second startup delay here?
+
+  // clear the key hits
+  for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
+      g_key_hit[led] = 255;
+  }
+
+
+  if (!eeconfig_is_enabled()) {
+      dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
+      eeconfig_init();
+      eeconfig_update_rgb_matrix_default();
+  }
+  rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
+  if (!rgb_matrix_config.mode) {
+      dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
+      eeconfig_update_rgb_matrix_default();
+      rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
+  }
+  eeconfig_debug_rgb_matrix(); // display current eeprom values
+}
+
+void rgb_matrix_setup_drivers(void) {
+  // Initialize TWI
+  i2c_init();
+#ifdef IS31FL3731
+  IS31FL3731_init( DRIVER_ADDR_1 );
+  IS31FL3731_init( DRIVER_ADDR_2 );
+#elif defined (IS31FL3733)
+  IS31FL3733_init( DRIVER_ADDR_1 );
+#endif
 
-    if (!eeconfig_is_enabled()) {
-        dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
-        eeconfig_init();
-        eeconfig_update_rgb_matrix_default();
-    }
-    rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
-    if (!rgb_matrix_config.mode) {
-        dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
-        eeconfig_update_rgb_matrix_default();
-        rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
-    }
-    eeconfig_debug_rgb_matrix(); // display current eeprom values
+  for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
+    bool enabled = true;
+    // This only caches it for later
+#ifdef IS31FL3731
+    IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
+#elif defined (IS31FL3733)
+    IS31FL3733_set_led_control_register( index, enabled, enabled, enabled );
+#endif
+  }
+  // This actually updates the LED drivers
+#ifdef IS31FL3731
+  IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
+#elif defined (IS31FL3733)
+  IS31FL3733_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
+#endif
 }
 
 // Deals with the messy details of incrementing an integer
@@ -801,11 +841,19 @@ void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) {
     {
         if ( i == index )
         {
+#ifdef IS31FL3731
             IS31FL3731_set_led_control_register( i, red, green, blue );
+#elif defined (IS31FL3733)
+            IS31FL3733_set_led_control_register( i, red, green, blue );
+#endif
         }
         else
         {
+#ifdef IS31FL3731
             IS31FL3731_set_led_control_register( i, false, false, false );
+#elif defined (IS31FL3733)
+            IS31FL3733_set_led_control_register( i, false, false, false );
+#endif
         }
     }
 }
@@ -828,8 +876,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);
 }
 
@@ -854,15 +902,25 @@ void rgblight_decrease_sat(void) {
 }
 
 void rgblight_increase_val(void) {
-    rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, 255 );
+    rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
 }
 
 void rgblight_decrease_val(void) {
-    rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, 255 );
+    rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
     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);