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.

228 lines
7.1 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. # 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(AW20216)
  164. # include "spi_master.h"
  165. static void init(void) {
  166. spi_init();
  167. AW20216_init();
  168. }
  169. static void flush(void) { AW20216_update_pwm_buffers(); }
  170. const rgb_matrix_driver_t rgb_matrix_driver = {
  171. .init = init,
  172. .flush = flush,
  173. .set_color = AW20216_set_color,
  174. .set_color_all = AW20216_set_color_all,
  175. };
  176. #elif defined(WS2812)
  177. # if defined(RGBLIGHT_ENABLE) && !defined(RGBLIGHT_CUSTOM_DRIVER)
  178. # pragma message "Cannot use RGBLIGHT and RGB Matrix using WS2812 at the same time."
  179. # pragma message "You need to use a custom driver, or re-implement the WS2812 driver to use a different configuration."
  180. # endif
  181. // LED color buffer
  182. LED_TYPE rgb_matrix_ws2812_array[DRIVER_LED_TOTAL];
  183. static void init(void) {}
  184. static void flush(void) {
  185. // Assumes use of RGB_DI_PIN
  186. ws2812_setleds(rgb_matrix_ws2812_array, DRIVER_LED_TOTAL);
  187. }
  188. // Set an led in the buffer to a color
  189. static inline void setled(int i, uint8_t r, uint8_t g, uint8_t b) {
  190. rgb_matrix_ws2812_array[i].r = r;
  191. rgb_matrix_ws2812_array[i].g = g;
  192. rgb_matrix_ws2812_array[i].b = b;
  193. # ifdef RGBW
  194. convert_rgb_to_rgbw(&rgb_matrix_ws2812_array[i]);
  195. # endif
  196. }
  197. static void setled_all(uint8_t r, uint8_t g, uint8_t b) {
  198. for (int i = 0; i < sizeof(rgb_matrix_ws2812_array) / sizeof(rgb_matrix_ws2812_array[0]); i++) {
  199. setled(i, r, g, b);
  200. }
  201. }
  202. const rgb_matrix_driver_t rgb_matrix_driver = {
  203. .init = init,
  204. .flush = flush,
  205. .set_color = setled,
  206. .set_color_all = setled_all,
  207. };
  208. #endif