aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Laird-Wah <james@laird-wah.net>2018-09-28 00:40:18 +1000
committerJack Humbert <jack.humb@gmail.com>2018-09-27 10:40:18 -0400
commitf70f45ee677a2a39a759052a356e0c5d82e25424 (patch)
treec0d6a6d5a5791a57309b3deb398ca1dfcc499f78
parent12ad59f99de0ecd2c81b92587c2858b3fb39523c (diff)
downloadqmk_firmware-f70f45ee677a2a39a759052a356e0c5d82e25424.tar.gz
qmk_firmware-f70f45ee677a2a39a759052a356e0c5d82e25424.zip
RGB Matrix refactoring to open up for new drivers (#3913)
* rgb_matrix: use a driver ops struct This is intended to avoid #ifdef proliferation on adding more drivers, eg. model01, which use different architectures. * rgb_matrix: document driver struct members * rgb_matrix: remove unused LED testing code * rgb_matrix: don't build into IS31x drivers unless being used * rgb_matrix: refactor make config options This ensures that the necessary files are included for any custom RGB_MATRIX_ENABLE value, without having to add entries here for specific boards. This particularly affects model01 because its controller is integrated and won't be used anywhere else, so it's preferable not to put it in common_features.mk. This now validates the value of RGB_MATRIX_ENABLE. It was necessary to fix an error in ergodox_ez rules.mk using the wrong comment separator, yielding an invalid value. * IS31x drivers: don't write the control registers all the time This is only needed when they are changed. This is done in init() and board- or keymap-specific code is free to make further changes. * rgb_matrix: move structs from chip drivers to rgb_matrix_drivers.c This approach is specific to the rgb_matrix functionality, so keep it neatly separated from the raw chip drivers.
-rw-r--r--common_features.mk24
-rw-r--r--drivers/issi/is31fl3731.c1
-rw-r--r--drivers/issi/is31fl3733.c3
-rw-r--r--keyboards/ergodox_ez/rules.mk2
-rw-r--r--quantum/rgb_matrix.c113
-rw-r--r--quantum/rgb_matrix.h17
-rw-r--r--quantum/rgb_matrix_drivers.c82
7 files changed, 113 insertions, 129 deletions
diff --git a/common_features.mk b/common_features.mk
index 7af778980..6c835abde 100644
--- a/common_features.mk
+++ b/common_features.mk
@@ -114,37 +114,35 @@ ifeq ($(strip $(RGBLIGHT_ENABLE)), yes)
114 endif 114 endif
115endif 115endif
116 116
117ifeq ($(strip $(RGB_MATRIX_ENABLE)), yes) 117RGB_MATRIX_ENABLE ?= no
118VALID_MATRIX_TYPES := yes IS31FL3731L IS31FL3733L custom
119ifneq ($(strip $(RGB_MATRIX_ENABLE)), no)
120ifeq ($(filter $(RGB_MATRIX_ENABLE),$(VALID_MATRIX_TYPES)),)
121 $(error RGB_MATRIX_ENABLE="$(RGB_MATRIX_ENABLE)" is not a valid matrix type)
122endif
118 OPT_DEFS += -DRGB_MATRIX_ENABLE 123 OPT_DEFS += -DRGB_MATRIX_ENABLE
119 OPT_DEFS += -DIS31FL3731
120 COMMON_VPATH += $(DRIVER_PATH)/issi
121 SRC += is31fl3731.c
122 SRC += i2c_master.c
123 SRC += $(QUANTUM_DIR)/color.c 124 SRC += $(QUANTUM_DIR)/color.c
124 SRC += $(QUANTUM_DIR)/rgb_matrix.c 125 SRC += $(QUANTUM_DIR)/rgb_matrix.c
126 SRC += $(QUANTUM_DIR)/rgb_matrix_drivers.c
125 CIE1931_CURVE = yes 127 CIE1931_CURVE = yes
126endif 128endif
127 129
130ifeq ($(strip $(RGB_MATRIX_ENABLE)), yes)
131 RGB_MATRIX_ENABLE = IS31FL3731
132endif
133
128ifeq ($(strip $(RGB_MATRIX_ENABLE)), IS31FL3731) 134ifeq ($(strip $(RGB_MATRIX_ENABLE)), IS31FL3731)
129 OPT_DEFS += -DRGB_MATRIX_ENABLE
130 OPT_DEFS += -DIS31FL3731 135 OPT_DEFS += -DIS31FL3731
131 COMMON_VPATH += $(DRIVER_PATH)/issi 136 COMMON_VPATH += $(DRIVER_PATH)/issi
132 SRC += is31fl3731.c 137 SRC += is31fl3731.c
133 SRC += i2c_master.c 138 SRC += i2c_master.c
134 SRC += $(QUANTUM_DIR)/color.c
135 SRC += $(QUANTUM_DIR)/rgb_matrix.c
136 CIE1931_CURVE = yes
137endif 139endif
138 140
139ifeq ($(strip $(RGB_MATRIX_ENABLE)), IS31FL3733) 141ifeq ($(strip $(RGB_MATRIX_ENABLE)), IS31FL3733)
140 OPT_DEFS += -DRGB_MATRIX_ENABLE
141 OPT_DEFS += -DIS31FL3733 142 OPT_DEFS += -DIS31FL3733
142 COMMON_VPATH += $(DRIVER_PATH)/issi 143 COMMON_VPATH += $(DRIVER_PATH)/issi
143 SRC += is31fl3733.c 144 SRC += is31fl3733.c
144 SRC += i2c_master.c 145 SRC += i2c_master.c
145 SRC += $(QUANTUM_DIR)/color.c
146 SRC += $(QUANTUM_DIR)/rgb_matrix.c
147 CIE1931_CURVE = yes
148endif 146endif
149 147
150ifeq ($(strip $(TAP_DANCE_ENABLE)), yes) 148ifeq ($(strip $(TAP_DANCE_ENABLE)), yes)
diff --git a/drivers/issi/is31fl3731.c b/drivers/issi/is31fl3731.c
index 4d0d6b8a5..c9155f5a3 100644
--- a/drivers/issi/is31fl3731.c
+++ b/drivers/issi/is31fl3731.c
@@ -268,4 +268,3 @@ void IS31FL3731_update_led_control_registers( uint8_t addr1, uint8_t addr2 )
268 } 268 }
269 } 269 }
270} 270}
271
diff --git a/drivers/issi/is31fl3733.c b/drivers/issi/is31fl3733.c
index 4098b5468..c198ec517 100644
--- a/drivers/issi/is31fl3733.c
+++ b/drivers/issi/is31fl3733.c
@@ -24,10 +24,10 @@
24#include "wait.h" 24#include "wait.h"
25#endif 25#endif
26 26
27#include "is31fl3733.h"
28#include <string.h> 27#include <string.h>
29#include "i2c_master.h" 28#include "i2c_master.h"
30#include "progmem.h" 29#include "progmem.h"
30#include "rgb_matrix.h"
31 31
32// This is a 7-bit address, that gets left-shifted and bit 0 32// This is a 7-bit address, that gets left-shifted and bit 0
33// set to 0 for write, 1 for read (as per I2C protocol) 33// set to 0 for write, 1 for read (as per I2C protocol)
@@ -250,4 +250,3 @@ void IS31FL3733_update_led_control_registers( uint8_t addr1, uint8_t addr2 )
250 } 250 }
251 } 251 }
252} 252}
253
diff --git a/keyboards/ergodox_ez/rules.mk b/keyboards/ergodox_ez/rules.mk
index dfbdba10d..ef2aefbb9 100644
--- a/keyboards/ergodox_ez/rules.mk
+++ b/keyboards/ergodox_ez/rules.mk
@@ -83,6 +83,6 @@ SWAP_HANDS_ENABLE= yes # Allow swapping hands of keyboard
83SLEEP_LED_ENABLE = no 83SLEEP_LED_ENABLE = no
84API_SYSEX_ENABLE = no 84API_SYSEX_ENABLE = no
85RGBLIGHT_ENABLE = yes 85RGBLIGHT_ENABLE = yes
86RGB_MATRIX_ENABLE = no // enable later 86RGB_MATRIX_ENABLE = no # enable later
87 87
88LAYOUTS = ergodox 88LAYOUTS = ergodox
diff --git a/quantum/rgb_matrix.c b/quantum/rgb_matrix.c
index 5fb59af8c..807e4d62d 100644
--- a/quantum/rgb_matrix.c
+++ b/quantum/rgb_matrix.c
@@ -18,7 +18,6 @@
18 18
19 19
20#include "rgb_matrix.h" 20#include "rgb_matrix.h"
21#include "i2c_master.h"
22#include "progmem.h" 21#include "progmem.h"
23#include "config.h" 22#include "config.h"
24#include "eeprom.h" 23#include "eeprom.h"
@@ -111,29 +110,15 @@ void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t
111} 110}
112 111
113void rgb_matrix_update_pwm_buffers(void) { 112void rgb_matrix_update_pwm_buffers(void) {
114#ifdef IS31FL3731 113 rgb_matrix_driver.flush();
115 IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
116 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
117#elif defined(IS31FL3733)
118 IS31FL3733_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
119 IS31FL3733_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
120#endif
121} 114}
122 115
123void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) { 116void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
124#ifdef IS31FL3731 117 rgb_matrix_driver.set_color(index, red, green, blue);
125 IS31FL3731_set_color( index, red, green, blue );
126#elif defined(IS31FL3733)
127 IS31FL3733_set_color( index, red, green, blue );
128#endif
129} 118}
130 119
131void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) { 120void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
132#ifdef IS31FL3731 121 rgb_matrix_driver.set_color_all(red, green, blue);
133 IS31FL3731_set_color_all( red, green, blue );
134#elif defined(IS31FL3733)
135 IS31FL3733_set_color_all( red, green, blue );
136#endif
137} 122}
138 123
139bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) { 124bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
@@ -196,47 +181,6 @@ void rgb_matrix_test(void) {
196 } 181 }
197} 182}
198 183
199// This tests the LEDs
200// Note that it will change the LED control registers
201// in the LED drivers, and leave them in an invalid
202// state for other backlight effects.
203// ONLY USE THIS FOR TESTING LEDS!
204void rgb_matrix_single_LED_test(void) {
205 static uint8_t color = 0; // 0,1,2 for R,G,B
206 static uint8_t row = 0;
207 static uint8_t column = 0;
208
209 static uint8_t tick = 0;
210 tick++;
211
212 if ( tick > 2 )
213 {
214 tick = 0;
215 column++;
216 }
217 if ( column > MATRIX_COLS )
218 {
219 column = 0;
220 row++;
221 }
222 if ( row > MATRIX_ROWS )
223 {
224 row = 0;
225 color++;
226 }
227 if ( color > 2 )
228 {
229 color = 0;
230 }
231
232 uint8_t led[8], led_count;
233 map_row_column_to_led(row,column,led,&led_count);
234 for(uint8_t i = 0; i < led_count; i++) {
235 rgb_matrix_set_color_all( 40, 40, 40 );
236 rgb_matrix_test_led( led[i], color==0, color==1, color==2 );
237 }
238}
239
240// All LEDs off 184// All LEDs off
241void rgb_matrix_all_off(void) { 185void rgb_matrix_all_off(void) {
242 rgb_matrix_set_color_all( 0, 0, 0 ); 186 rgb_matrix_set_color_all( 0, 0, 0 );
@@ -817,7 +761,7 @@ void rgb_matrix_indicators_user(void) {}
817// } 761// }
818 762
819void rgb_matrix_init(void) { 763void rgb_matrix_init(void) {
820 rgb_matrix_setup_drivers(); 764 rgb_matrix_driver.init();
821 765
822 // TODO: put the 1 second startup delay here? 766 // TODO: put the 1 second startup delay here?
823 767
@@ -841,33 +785,6 @@ void rgb_matrix_init(void) {
841 eeconfig_debug_rgb_matrix(); // display current eeprom values 785 eeconfig_debug_rgb_matrix(); // display current eeprom values
842} 786}
843 787
844void rgb_matrix_setup_drivers(void) {
845 // Initialize TWI
846 i2c_init();
847#ifdef IS31FL3731
848 IS31FL3731_init( DRIVER_ADDR_1 );
849 IS31FL3731_init( DRIVER_ADDR_2 );
850#elif defined (IS31FL3733)
851 IS31FL3733_init( DRIVER_ADDR_1 );
852#endif
853
854 for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
855 bool enabled = true;
856 // This only caches it for later
857#ifdef IS31FL3731
858 IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
859#elif defined (IS31FL3733)
860 IS31FL3733_set_led_control_register( index, enabled, enabled, enabled );
861#endif
862 }
863 // This actually updates the LED drivers
864#ifdef IS31FL3731
865 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
866#elif defined (IS31FL3733)
867 IS31FL3733_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
868#endif
869}
870
871// Deals with the messy details of incrementing an integer 788// Deals with the messy details of incrementing an integer
872uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) { 789uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
873 int16_t new_value = value; 790 int16_t new_value = value;
@@ -910,28 +827,6 @@ uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
910// } 827// }
911// } 828// }
912 829
913void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) {
914 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
915 {
916 if ( i == index )
917 {
918#ifdef IS31FL3731
919 IS31FL3731_set_led_control_register( i, red, green, blue );
920#elif defined (IS31FL3733)
921 IS31FL3733_set_led_control_register( i, red, green, blue );
922#endif
923 }
924 else
925 {
926#ifdef IS31FL3731
927 IS31FL3731_set_led_control_register( i, false, false, false );
928#elif defined (IS31FL3733)
929 IS31FL3733_set_led_control_register( i, false, false, false );
930#endif
931 }
932 }
933}
934
935uint32_t rgb_matrix_get_tick(void) { 830uint32_t rgb_matrix_get_tick(void) {
936 return g_tick; 831 return g_tick;
937} 832}
diff --git a/quantum/rgb_matrix.h b/quantum/rgb_matrix.h
index 046d98790..45caaac42 100644
--- a/quantum/rgb_matrix.h
+++ b/quantum/rgb_matrix.h
@@ -100,8 +100,6 @@ void rgb_matrix_indicators(void);
100void rgb_matrix_indicators_kb(void); 100void rgb_matrix_indicators_kb(void);
101void rgb_matrix_indicators_user(void); 101void rgb_matrix_indicators_user(void);
102 102
103void rgb_matrix_single_LED_test(void);
104
105void rgb_matrix_init(void); 103void rgb_matrix_init(void);
106void rgb_matrix_setup_drivers(void); 104void rgb_matrix_setup_drivers(void);
107 105
@@ -126,7 +124,6 @@ void rgb_matrix_decrease(void);
126// void backlight_get_key_color( uint8_t led, HSV *hsv ); 124// void backlight_get_key_color( uint8_t led, HSV *hsv );
127// void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv ); 125// void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv );
128 126
129void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue );
130uint32_t rgb_matrix_get_tick(void); 127uint32_t rgb_matrix_get_tick(void);
131 128
132void rgblight_toggle(void); 129void rgblight_toggle(void);
@@ -143,4 +140,18 @@ void rgblight_decrease_speed(void);
143void rgblight_mode(uint8_t mode); 140void rgblight_mode(uint8_t mode);
144uint32_t rgblight_get_mode(void); 141uint32_t rgblight_get_mode(void);
145 142
143typedef struct {
144 /* Perform any initialisation required for the other driver functions to work. */
145 void (*init)(void);
146
147 /* Set the colour of a single LED in the buffer. */
148 void (*set_color)(int index, uint8_t r, uint8_t g, uint8_t b);
149 /* Set the colour of all LEDS on the keyboard in the buffer. */
150 void (*set_color_all)(uint8_t r, uint8_t g, uint8_t b);
151 /* Flush any buffered changes to the hardware. */
152 void (*flush)(void);
153} rgb_matrix_driver_t;
154
155extern const rgb_matrix_driver_t rgb_matrix_driver;
156
146#endif 157#endif
diff --git a/quantum/rgb_matrix_drivers.c b/quantum/rgb_matrix_drivers.c
new file mode 100644
index 000000000..70b80293d
--- /dev/null
+++ b/quantum/rgb_matrix_drivers.c
@@ -0,0 +1,82 @@
1/* Copyright 2018 James Laird-Wah
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 "rgb_matrix.h"
18
19/* Each driver needs to define the struct
20 * const rgb_matrix_driver_t rgb_matrix_driver;
21 * All members must be provided.
22 * Keyboard custom drivers can define this in their own files, it should only
23 * be here if shared between boards.
24 */
25
26#if defined(IS31FL3731) || defined(IS31FL3733)
27
28#include "i2c_master.h"
29
30static void init( void )
31{
32 i2c_init();
33#ifdef IS31FL3731
34 IS31FL3731_init( DRIVER_ADDR_1 );
35 IS31FL3731_init( DRIVER_ADDR_2 );
36#else
37 IS31FL3733_init( DRIVER_ADDR_1 );
38#endif
39 for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
40 bool enabled = true;
41 // This only caches it for later
42#ifdef IS31FL3731
43 IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
44#else
45 IS31FL3733_set_led_control_register( index, enabled, enabled, enabled );
46#endif
47 }
48 // This actually updates the LED drivers
49#ifdef IS31FL3731
50 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
51#else
52 IS31FL3733_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
53#endif
54}
55
56#ifdef IS31FL3731
57static void flush( void )
58{
59 IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
60}
61
62const rgb_matrix_driver_t rgb_matrix_driver = {
63 .init = init,
64 .flush = flush,
65 .set_color = IS31FL3731_set_color,
66 .set_color_all = IS31FL3731_set_color_all,
67};
68#else
69static void flush( void )
70{
71 IS31FL3733_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
72}
73
74const rgb_matrix_driver_t rgb_matrix_driver = {
75 .init = init,
76 .flush = flush,
77 .set_color = IS31FL3733_set_color,
78 .set_color_all = IS31FL3733_set_color_all,
79};
80#endif
81
82#endif