rgb_matrix_drivers.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. # ifndef DRIVER_SYNC_1
  40. # define DRIVER_SYNC_1 0
  41. # endif
  42. IS31FL3733_init(DRIVER_ADDR_1, DRIVER_SYNC_1);
  43. # if defined DRIVER_ADDR_2 && (DRIVER_ADDR_1 != DRIVER_ADDR_2)
  44. # ifndef DRIVER_SYNC_2
  45. # define DRIVER_SYNC_2 0
  46. # endif
  47. IS31FL3733_init(DRIVER_ADDR_2, DRIVER_SYNC_2);
  48. # endif
  49. # ifdef DRIVER_ADDR_3
  50. # ifndef DRIVER_SYNC_3
  51. # define DRIVER_SYNC_3 0
  52. # endif
  53. IS31FL3733_init(DRIVER_ADDR_3, DRIVER_SYNC_3);
  54. # endif
  55. # ifdef DRIVER_ADDR_4
  56. # ifndef DRIVER_SYNC_4
  57. # define DRIVER_SYNC_4 0
  58. # endif
  59. IS31FL3733_init(DRIVER_ADDR_4, DRIVER_SYNC_4);
  60. # endif
  61. # elif defined(IS31FL3737)
  62. IS31FL3737_init(DRIVER_ADDR_1);
  63. # else
  64. IS31FL3741_init(DRIVER_ADDR_1);
  65. # endif
  66. for (int index = 0; index < DRIVER_LED_TOTAL; index++) {
  67. bool enabled = true;
  68. // This only caches it for later
  69. # ifdef IS31FL3731
  70. IS31FL3731_set_led_control_register(index, enabled, enabled, enabled);
  71. # elif defined(IS31FL3733)
  72. IS31FL3733_set_led_control_register(index, enabled, enabled, enabled);
  73. # elif defined(IS31FL3737)
  74. IS31FL3737_set_led_control_register(index, enabled, enabled, enabled);
  75. # else
  76. IS31FL3741_set_led_control_register(index, enabled, enabled, enabled);
  77. # endif
  78. }
  79. // This actually updates the LED drivers
  80. # ifdef IS31FL3731
  81. IS31FL3731_update_led_control_registers(DRIVER_ADDR_1, 0);
  82. # ifdef DRIVER_ADDR_2
  83. IS31FL3731_update_led_control_registers(DRIVER_ADDR_2, 1);
  84. # endif
  85. # ifdef DRIVER_ADDR_3
  86. IS31FL3731_update_led_control_registers(DRIVER_ADDR_3, 2);
  87. # endif
  88. # ifdef DRIVER_ADDR_4
  89. IS31FL3731_update_led_control_registers(DRIVER_ADDR_4, 3);
  90. # endif
  91. # elif defined(IS31FL3733)
  92. IS31FL3733_update_led_control_registers(DRIVER_ADDR_1, 0);
  93. # ifdef DRIVER_ADDR_2
  94. IS31FL3733_update_led_control_registers(DRIVER_ADDR_2, 1);
  95. # endif
  96. # ifdef DRIVER_ADDR_3
  97. IS31FL3733_update_led_control_registers(DRIVER_ADDR_3, 2);
  98. # endif
  99. # ifdef DRIVER_ADDR_4
  100. IS31FL3733_update_led_control_registers(DRIVER_ADDR_4, 3);
  101. # endif
  102. # elif defined(IS31FL3737)
  103. IS31FL3737_update_led_control_registers(DRIVER_ADDR_1, DRIVER_ADDR_2);
  104. # else
  105. IS31FL3741_update_led_control_registers(DRIVER_ADDR_1, 0);
  106. # endif
  107. }
  108. # ifdef IS31FL3731
  109. static void flush(void) {
  110. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_1, 0);
  111. # ifdef DRIVER_ADDR_2
  112. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_2, 1);
  113. # endif
  114. # ifdef DRIVER_ADDR_3
  115. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_3, 2);
  116. # endif
  117. # ifdef DRIVER_ADDR_4
  118. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_4, 3);
  119. # endif
  120. }
  121. const rgb_matrix_driver_t rgb_matrix_driver = {
  122. .init = init,
  123. .flush = flush,
  124. .set_color = IS31FL3731_set_color,
  125. .set_color_all = IS31FL3731_set_color_all,
  126. };
  127. # elif defined(IS31FL3733)
  128. static void flush(void) {
  129. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_1, 0);
  130. # ifdef DRIVER_ADDR_2
  131. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_2, 1);
  132. # endif
  133. # ifdef DRIVER_ADDR_3
  134. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_3, 2);
  135. # endif
  136. # ifdef DRIVER_ADDR_4
  137. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_4, 3);
  138. # endif
  139. }
  140. const rgb_matrix_driver_t rgb_matrix_driver = {
  141. .init = init,
  142. .flush = flush,
  143. .set_color = IS31FL3733_set_color,
  144. .set_color_all = IS31FL3733_set_color_all,
  145. };
  146. # elif defined(IS31FL3737)
  147. static void flush(void) { IS31FL3737_update_pwm_buffers(DRIVER_ADDR_1, DRIVER_ADDR_2); }
  148. const rgb_matrix_driver_t rgb_matrix_driver = {
  149. .init = init,
  150. .flush = flush,
  151. .set_color = IS31FL3737_set_color,
  152. .set_color_all = IS31FL3737_set_color_all,
  153. };
  154. # else
  155. static void flush(void) { IS31FL3741_update_pwm_buffers(DRIVER_ADDR_1, DRIVER_ADDR_2); }
  156. const rgb_matrix_driver_t rgb_matrix_driver = {
  157. .init = init,
  158. .flush = flush,
  159. .set_color = IS31FL3741_set_color,
  160. .set_color_all = IS31FL3741_set_color_all,
  161. };
  162. # endif
  163. #elif defined(WS2812)
  164. # if defined(RGBLIGHT_ENABLE) && !defined(RGBLIGHT_CUSTOM_DRIVER)
  165. # pragma message "Cannot use RGBLIGHT and RGB Matrix using WS2812 at the same time."
  166. # pragma message "You need to use a custom driver, or re-implement the WS2812 driver to use a different configuration."
  167. # endif
  168. // LED color buffer
  169. LED_TYPE rgb_matrix_ws2812_array[DRIVER_LED_TOTAL];
  170. static void init(void) {}
  171. static void flush(void) {
  172. // Assumes use of RGB_DI_PIN
  173. ws2812_setleds(rgb_matrix_ws2812_array, DRIVER_LED_TOTAL);
  174. }
  175. // Set an led in the buffer to a color
  176. static inline void setled(int i, uint8_t r, uint8_t g, uint8_t b) {
  177. rgb_matrix_ws2812_array[i].r = r;
  178. rgb_matrix_ws2812_array[i].g = g;
  179. rgb_matrix_ws2812_array[i].b = b;
  180. # ifdef RGBW
  181. convert_rgb_to_rgbw(&rgb_matrix_ws2812_array[i]);
  182. # endif
  183. }
  184. static void setled_all(uint8_t r, uint8_t g, uint8_t b) {
  185. for (int i = 0; i < sizeof(rgb_matrix_ws2812_array) / sizeof(rgb_matrix_ws2812_array[0]); i++) {
  186. setled(i, r, g, b);
  187. }
  188. }
  189. const rgb_matrix_driver_t rgb_matrix_driver = {
  190. .init = init,
  191. .flush = flush,
  192. .set_color = setled,
  193. .set_color_all = setled_all,
  194. };
  195. #endif