You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.9 KiB

  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. IS31FL3731_init(DRIVER_ADDR_2);
  30. # elif defined(IS31FL3733)
  31. IS31FL3733_init(DRIVER_ADDR_1, 0);
  32. # elif defined(IS31FL3737)
  33. IS31FL3737_init(DRIVER_ADDR_1);
  34. # else
  35. IS31FL3741_init(DRIVER_ADDR_1);
  36. # endif
  37. for (int index = 0; index < DRIVER_LED_TOTAL; index++) {
  38. bool enabled = true;
  39. // This only caches it for later
  40. # ifdef IS31FL3731
  41. IS31FL3731_set_led_control_register(index, enabled, enabled, enabled);
  42. # elif defined(IS31FL3733)
  43. IS31FL3733_set_led_control_register(index, enabled, enabled, enabled);
  44. # elif defined(IS31FL3737)
  45. IS31FL3737_set_led_control_register(index, enabled, enabled, enabled);
  46. # else
  47. IS31FL3741_set_led_control_register(index, enabled, enabled, enabled);
  48. # endif
  49. }
  50. // This actually updates the LED drivers
  51. # ifdef IS31FL3731
  52. IS31FL3731_update_led_control_registers(DRIVER_ADDR_1, 0);
  53. IS31FL3731_update_led_control_registers(DRIVER_ADDR_2, 1);
  54. # elif defined(IS31FL3733)
  55. IS31FL3733_update_led_control_registers(DRIVER_ADDR_1, 0);
  56. IS31FL3733_update_led_control_registers(DRIVER_ADDR_2, 1);
  57. # elif defined(IS31FL3737)
  58. IS31FL3737_update_led_control_registers(DRIVER_ADDR_1, DRIVER_ADDR_2);
  59. # else
  60. IS31FL3741_update_led_control_registers(DRIVER_ADDR_1, 0);
  61. # endif
  62. }
  63. # ifdef IS31FL3731
  64. static void flush(void) {
  65. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_1, 0);
  66. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_2, 1);
  67. }
  68. const rgb_matrix_driver_t rgb_matrix_driver = {
  69. .init = init,
  70. .flush = flush,
  71. .set_color = IS31FL3731_set_color,
  72. .set_color_all = IS31FL3731_set_color_all,
  73. };
  74. # elif defined(IS31FL3733)
  75. static void flush(void) {
  76. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_1, 0);
  77. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_2, 1);
  78. }
  79. const rgb_matrix_driver_t rgb_matrix_driver = {
  80. .init = init,
  81. .flush = flush,
  82. .set_color = IS31FL3733_set_color,
  83. .set_color_all = IS31FL3733_set_color_all,
  84. };
  85. # elif defined(IS31FL3737)
  86. static void flush(void) { IS31FL3737_update_pwm_buffers(DRIVER_ADDR_1, DRIVER_ADDR_2); }
  87. const rgb_matrix_driver_t rgb_matrix_driver = {
  88. .init = init,
  89. .flush = flush,
  90. .set_color = IS31FL3737_set_color,
  91. .set_color_all = IS31FL3737_set_color_all,
  92. };
  93. # else
  94. static void flush(void) { IS31FL3741_update_pwm_buffers(DRIVER_ADDR_1, DRIVER_ADDR_2); }
  95. const rgb_matrix_driver_t rgb_matrix_driver = {
  96. .init = init,
  97. .flush = flush,
  98. .set_color = IS31FL3741_set_color,
  99. .set_color_all = IS31FL3741_set_color_all,
  100. };
  101. # endif
  102. #elif defined(WS2812)
  103. # if defined(RGBLIGHT_ENABLE) && !defined(RGBLIGHT_CUSTOM_DRIVER)
  104. # pragma message "Cannot use RGBLIGHT and RGB Matrix using WS2812 at the same time."
  105. # pragma message "You need to use a custom driver, or re-implement the WS2812 driver to use a different configuration."
  106. # endif
  107. // LED color buffer
  108. LED_TYPE rgb_matrix_ws2812_array[DRIVER_LED_TOTAL];
  109. static void init(void) {}
  110. static void flush(void) {
  111. // Assumes use of RGB_DI_PIN
  112. ws2812_setleds(rgb_matrix_ws2812_array, DRIVER_LED_TOTAL);
  113. }
  114. // Set an led in the buffer to a color
  115. static inline void setled(int i, uint8_t r, uint8_t g, uint8_t b) {
  116. rgb_matrix_ws2812_array[i].r = r;
  117. rgb_matrix_ws2812_array[i].g = g;
  118. rgb_matrix_ws2812_array[i].b = b;
  119. # ifdef RGBW
  120. convert_rgb_to_rgbw(&rgb_matrix_ws2812_array[i]);
  121. # endif
  122. }
  123. static void setled_all(uint8_t r, uint8_t g, uint8_t b) {
  124. for (int i = 0; i < sizeof(rgb_matrix_ws2812_array) / sizeof(rgb_matrix_ws2812_array[0]); i++) {
  125. setled(i, r, g, b);
  126. }
  127. }
  128. const rgb_matrix_driver_t rgb_matrix_driver = {
  129. .init = init,
  130. .flush = flush,
  131. .set_color = setled,
  132. .set_color_all = setled_all,
  133. };
  134. #endif