rgb_matrix_drivers.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include "rgb_matrix.h"
  17. /* Each driver needs to define the struct
  18. * const rgb_matrix_driver_t rgb_matrix_driver;
  19. * All members must be provided.
  20. * Keyboard custom drivers can define this in their own files, it should only
  21. * be here if shared between boards.
  22. */
  23. #if defined(IS31FL3731) || defined(IS31FL3733) || defined(IS31FL3737) || defined(IS31FL3741)
  24. # include "i2c_master.h"
  25. static void init(void) {
  26. i2c_init();
  27. # ifdef IS31FL3731
  28. IS31FL3731_init(DRIVER_ADDR_1);
  29. # ifdef DRIVER_ADDR_2
  30. IS31FL3731_init(DRIVER_ADDR_2);
  31. # endif
  32. # ifdef DRIVER_ADDR_3
  33. IS31FL3731_init(DRIVER_ADDR_3);
  34. # endif
  35. # ifdef DRIVER_ADDR_4
  36. IS31FL3731_init(DRIVER_ADDR_4);
  37. # endif
  38. # elif defined(IS31FL3733)
  39. IS31FL3733_init(DRIVER_ADDR_1, 0);
  40. # elif defined(IS31FL3737)
  41. IS31FL3737_init(DRIVER_ADDR_1);
  42. # else
  43. IS31FL3741_init(DRIVER_ADDR_1);
  44. # endif
  45. for (int index = 0; index < DRIVER_LED_TOTAL; index++) {
  46. bool enabled = true;
  47. // This only caches it for later
  48. # ifdef IS31FL3731
  49. IS31FL3731_set_led_control_register(index, enabled, enabled, enabled);
  50. # elif defined(IS31FL3733)
  51. IS31FL3733_set_led_control_register(index, enabled, enabled, enabled);
  52. # elif defined(IS31FL3737)
  53. IS31FL3737_set_led_control_register(index, enabled, enabled, enabled);
  54. # else
  55. IS31FL3741_set_led_control_register(index, enabled, enabled, enabled);
  56. # endif
  57. }
  58. // This actually updates the LED drivers
  59. # ifdef IS31FL3731
  60. IS31FL3731_update_led_control_registers(DRIVER_ADDR_1, 0);
  61. # ifdef DRIVER_ADDR_2
  62. IS31FL3731_update_led_control_registers(DRIVER_ADDR_2, 1);
  63. # endif
  64. # ifdef DRIVER_ADDR_3
  65. IS31FL3731_update_led_control_registers(DRIVER_ADDR_3, 2);
  66. # endif
  67. # ifdef DRIVER_ADDR_4
  68. IS31FL3731_update_led_control_registers(DRIVER_ADDR_4, 3);
  69. # endif
  70. # elif defined(IS31FL3733)
  71. IS31FL3733_update_led_control_registers(DRIVER_ADDR_1, 0);
  72. IS31FL3733_update_led_control_registers(DRIVER_ADDR_2, 1);
  73. # elif defined(IS31FL3737)
  74. IS31FL3737_update_led_control_registers(DRIVER_ADDR_1, DRIVER_ADDR_2);
  75. # else
  76. IS31FL3741_update_led_control_registers(DRIVER_ADDR_1, 0);
  77. # endif
  78. }
  79. # ifdef IS31FL3731
  80. static void flush(void) {
  81. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_1, 0);
  82. # ifdef DRIVER_ADDR_2
  83. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_2, 1);
  84. # endif
  85. # ifdef DRIVER_ADDR_3
  86. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_3, 2);
  87. # endif
  88. # ifdef DRIVER_ADDR_4
  89. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_4, 3);
  90. # endif
  91. }
  92. const rgb_matrix_driver_t rgb_matrix_driver = {
  93. .init = init,
  94. .flush = flush,
  95. .set_color = IS31FL3731_set_color,
  96. .set_color_all = IS31FL3731_set_color_all,
  97. };
  98. # elif defined(IS31FL3733)
  99. static void flush(void) {
  100. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_1, 0);
  101. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_2, 1);
  102. }
  103. const rgb_matrix_driver_t rgb_matrix_driver = {
  104. .init = init,
  105. .flush = flush,
  106. .set_color = IS31FL3733_set_color,
  107. .set_color_all = IS31FL3733_set_color_all,
  108. };
  109. # elif defined(IS31FL3737)
  110. static void flush(void) { IS31FL3737_update_pwm_buffers(DRIVER_ADDR_1, DRIVER_ADDR_2); }
  111. const rgb_matrix_driver_t rgb_matrix_driver = {
  112. .init = init,
  113. .flush = flush,
  114. .set_color = IS31FL3737_set_color,
  115. .set_color_all = IS31FL3737_set_color_all,
  116. };
  117. # else
  118. static void flush(void) { IS31FL3741_update_pwm_buffers(DRIVER_ADDR_1, DRIVER_ADDR_2); }
  119. const rgb_matrix_driver_t rgb_matrix_driver = {
  120. .init = init,
  121. .flush = flush,
  122. .set_color = IS31FL3741_set_color,
  123. .set_color_all = IS31FL3741_set_color_all,
  124. };
  125. # endif
  126. #elif defined(WS2812)
  127. # if defined(RGBLIGHT_ENABLE) && !defined(RGBLIGHT_CUSTOM_DRIVER)
  128. # pragma message "Cannot use RGBLIGHT and RGB Matrix using WS2812 at the same time."
  129. # pragma message "You need to use a custom driver, or re-implement the WS2812 driver to use a different configuration."
  130. # endif
  131. // LED color buffer
  132. LED_TYPE rgb_matrix_ws2812_array[DRIVER_LED_TOTAL];
  133. static void init(void) {}
  134. static void flush(void) {
  135. // Assumes use of RGB_DI_PIN
  136. ws2812_setleds(rgb_matrix_ws2812_array, DRIVER_LED_TOTAL);
  137. }
  138. // Set an led in the buffer to a color
  139. static inline void setled(int i, uint8_t r, uint8_t g, uint8_t b) {
  140. rgb_matrix_ws2812_array[i].r = r;
  141. rgb_matrix_ws2812_array[i].g = g;
  142. rgb_matrix_ws2812_array[i].b = b;
  143. # ifdef RGBW
  144. convert_rgb_to_rgbw(&rgb_matrix_ws2812_array[i]);
  145. # endif
  146. }
  147. static void setled_all(uint8_t r, uint8_t g, uint8_t b) {
  148. for (int i = 0; i < sizeof(rgb_matrix_ws2812_array) / sizeof(rgb_matrix_ws2812_array[0]); i++) {
  149. setled(i, r, g, b);
  150. }
  151. }
  152. const rgb_matrix_driver_t rgb_matrix_driver = {
  153. .init = init,
  154. .flush = flush,
  155. .set_color = setled,
  156. .set_color_all = setled_all,
  157. };
  158. #endif