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.

252 lines
7.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. # 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. # if defined(DRIVER_ADDR_2) && (DRIVER_ADDR_2 != DRIVER_ADDR_1) // provides backward compatibility
  64. IS31FL3737_init(DRIVER_ADDR_2);
  65. # endif
  66. # else
  67. IS31FL3741_init(DRIVER_ADDR_1);
  68. # endif
  69. for (int index = 0; index < DRIVER_LED_TOTAL; index++) {
  70. bool enabled = true;
  71. // This only caches it for later
  72. # ifdef IS31FL3731
  73. IS31FL3731_set_led_control_register(index, enabled, enabled, enabled);
  74. # elif defined(IS31FL3733)
  75. IS31FL3733_set_led_control_register(index, enabled, enabled, enabled);
  76. # elif defined(IS31FL3737)
  77. IS31FL3737_set_led_control_register(index, enabled, enabled, enabled);
  78. # else
  79. IS31FL3741_set_led_control_register(index, enabled, enabled, enabled);
  80. # endif
  81. }
  82. // This actually updates the LED drivers
  83. # ifdef IS31FL3731
  84. IS31FL3731_update_led_control_registers(DRIVER_ADDR_1, 0);
  85. # ifdef DRIVER_ADDR_2
  86. IS31FL3731_update_led_control_registers(DRIVER_ADDR_2, 1);
  87. # endif
  88. # ifdef DRIVER_ADDR_3
  89. IS31FL3731_update_led_control_registers(DRIVER_ADDR_3, 2);
  90. # endif
  91. # ifdef DRIVER_ADDR_4
  92. IS31FL3731_update_led_control_registers(DRIVER_ADDR_4, 3);
  93. # endif
  94. # elif defined(IS31FL3733)
  95. IS31FL3733_update_led_control_registers(DRIVER_ADDR_1, 0);
  96. # ifdef DRIVER_ADDR_2
  97. IS31FL3733_update_led_control_registers(DRIVER_ADDR_2, 1);
  98. # endif
  99. # ifdef DRIVER_ADDR_3
  100. IS31FL3733_update_led_control_registers(DRIVER_ADDR_3, 2);
  101. # endif
  102. # ifdef DRIVER_ADDR_4
  103. IS31FL3733_update_led_control_registers(DRIVER_ADDR_4, 3);
  104. # endif
  105. # elif defined(IS31FL3737)
  106. IS31FL3737_update_led_control_registers(DRIVER_ADDR_1, 0);
  107. # if defined(DRIVER_ADDR_2) && (DRIVER_ADDR_2 != DRIVER_ADDR_1) // provides backward compatibility
  108. IS31FL3737_update_led_control_registers(DRIVER_ADDR_2, 1);
  109. # endif
  110. # else
  111. IS31FL3741_update_led_control_registers(DRIVER_ADDR_1, 0);
  112. # endif
  113. }
  114. # ifdef IS31FL3731
  115. static void flush(void) {
  116. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_1, 0);
  117. # ifdef DRIVER_ADDR_2
  118. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_2, 1);
  119. # endif
  120. # ifdef DRIVER_ADDR_3
  121. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_3, 2);
  122. # endif
  123. # ifdef DRIVER_ADDR_4
  124. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_4, 3);
  125. # endif
  126. }
  127. const rgb_matrix_driver_t rgb_matrix_driver = {
  128. .init = init,
  129. .flush = flush,
  130. .set_color = IS31FL3731_set_color,
  131. .set_color_all = IS31FL3731_set_color_all,
  132. };
  133. # elif defined(IS31FL3733)
  134. static void flush(void) {
  135. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_1, 0);
  136. # ifdef DRIVER_ADDR_2
  137. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_2, 1);
  138. # endif
  139. # ifdef DRIVER_ADDR_3
  140. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_3, 2);
  141. # endif
  142. # ifdef DRIVER_ADDR_4
  143. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_4, 3);
  144. # endif
  145. }
  146. const rgb_matrix_driver_t rgb_matrix_driver = {
  147. .init = init,
  148. .flush = flush,
  149. .set_color = IS31FL3733_set_color,
  150. .set_color_all = IS31FL3733_set_color_all,
  151. };
  152. # elif defined(IS31FL3737)
  153. static void flush(void) {
  154. IS31FL3737_update_pwm_buffers(DRIVER_ADDR_1, 0);
  155. # if defined(DRIVER_ADDR_2) && (DRIVER_ADDR_2 != DRIVER_ADDR_1) // provides backward compatibility
  156. IS31FL3737_update_pwm_buffers(DRIVER_ADDR_2, 1);
  157. # endif
  158. }
  159. const rgb_matrix_driver_t rgb_matrix_driver = {
  160. .init = init,
  161. .flush = flush,
  162. .set_color = IS31FL3737_set_color,
  163. .set_color_all = IS31FL3737_set_color_all,
  164. };
  165. # else
  166. static void flush(void) {
  167. IS31FL3741_update_pwm_buffers(DRIVER_ADDR_1, 0);
  168. # if defined(DRIVER_ADDR_2) && (DRIVER_ADDR_2 != DRIVER_ADDR_1) // provides backward compatibility
  169. IS31FL3741_update_pwm_buffers(DRIVER_ADDR_2, 1);
  170. # endif
  171. }
  172. const rgb_matrix_driver_t rgb_matrix_driver = {
  173. .init = init,
  174. .flush = flush,
  175. .set_color = IS31FL3741_set_color,
  176. .set_color_all = IS31FL3741_set_color_all,
  177. };
  178. # endif
  179. #elif defined(AW20216)
  180. # include "spi_master.h"
  181. static void init(void) {
  182. spi_init();
  183. AW20216_init(DRIVER_1_CS, DRIVER_1_EN);
  184. # ifdef DRIVER_2_CS
  185. AW20216_init(DRIVER_2_CS, DRIVER_2_EN);
  186. # endif
  187. }
  188. static void flush(void) {
  189. AW20216_update_pwm_buffers(DRIVER_1_CS, 0);
  190. # ifdef DRIVER_2_CS
  191. AW20216_update_pwm_buffers(DRIVER_2_CS, 1);
  192. # endif
  193. }
  194. const rgb_matrix_driver_t rgb_matrix_driver = {
  195. .init = init,
  196. .flush = flush,
  197. .set_color = AW20216_set_color,
  198. .set_color_all = AW20216_set_color_all,
  199. };
  200. #elif defined(WS2812)
  201. # if defined(RGBLIGHT_ENABLE) && !defined(RGBLIGHT_CUSTOM_DRIVER)
  202. # pragma message "Cannot use RGBLIGHT and RGB Matrix using WS2812 at the same time."
  203. # pragma message "You need to use a custom driver, or re-implement the WS2812 driver to use a different configuration."
  204. # endif
  205. // LED color buffer
  206. LED_TYPE rgb_matrix_ws2812_array[DRIVER_LED_TOTAL];
  207. static void init(void) {}
  208. static void flush(void) {
  209. // Assumes use of RGB_DI_PIN
  210. ws2812_setleds(rgb_matrix_ws2812_array, DRIVER_LED_TOTAL);
  211. }
  212. // Set an led in the buffer to a color
  213. static inline void setled(int i, uint8_t r, uint8_t g, uint8_t b) {
  214. rgb_matrix_ws2812_array[i].r = r;
  215. rgb_matrix_ws2812_array[i].g = g;
  216. rgb_matrix_ws2812_array[i].b = b;
  217. # ifdef RGBW
  218. convert_rgb_to_rgbw(&rgb_matrix_ws2812_array[i]);
  219. # endif
  220. }
  221. static void setled_all(uint8_t r, uint8_t g, uint8_t b) {
  222. for (int i = 0; i < sizeof(rgb_matrix_ws2812_array) / sizeof(rgb_matrix_ws2812_array[0]); i++) {
  223. setled(i, r, g, b);
  224. }
  225. }
  226. const rgb_matrix_driver_t rgb_matrix_driver = {
  227. .init = init,
  228. .flush = flush,
  229. .set_color = setled,
  230. .set_color_all = setled_all,
  231. };
  232. #endif