rgb_matrix_drivers.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // TODO: Remove this at some later date
  26. # if defined(DRIVER_ADDR_1) && defined(DRIVER_ADDR_2)
  27. # if DRIVER_ADDR_1 == DRIVER_ADDR_2
  28. # error "Setting DRIVER_ADDR_2 == DRIVER_ADDR_1 is obsolete. If you are only using one ISSI driver, set DRIVER_COUNT to 1 and remove DRIVER_ADDR_2"
  29. # endif
  30. # endif
  31. static void init(void) {
  32. i2c_init();
  33. # if defined(IS31FL3731)
  34. IS31FL3731_init(DRIVER_ADDR_1);
  35. # if defined(DRIVER_ADDR_2)
  36. IS31FL3731_init(DRIVER_ADDR_2);
  37. # if defined(DRIVER_ADDR_3)
  38. IS31FL3731_init(DRIVER_ADDR_3);
  39. # if defined(DRIVER_ADDR_4)
  40. IS31FL3731_init(DRIVER_ADDR_4);
  41. # endif
  42. # endif
  43. # endif
  44. # elif defined(IS31FL3733)
  45. # if !defined(DRIVER_SYNC_1)
  46. # define DRIVER_SYNC_1 0
  47. # endif
  48. IS31FL3733_init(DRIVER_ADDR_1, DRIVER_SYNC_1);
  49. # if defined(DRIVER_ADDR_2)
  50. # if !defined(DRIVER_SYNC_2)
  51. # define DRIVER_SYNC_2 0
  52. # endif
  53. IS31FL3733_init(DRIVER_ADDR_2, DRIVER_SYNC_2);
  54. # if defined(DRIVER_ADDR_3)
  55. # if !defined(DRIVER_SYNC_3)
  56. # define DRIVER_SYNC_3 0
  57. # endif
  58. IS31FL3733_init(DRIVER_ADDR_3, DRIVER_SYNC_3);
  59. # if defined(DRIVER_ADDR_4)
  60. # if !defined(DRIVER_SYNC_4)
  61. # define DRIVER_SYNC_4 0
  62. # endif
  63. IS31FL3733_init(DRIVER_ADDR_4, DRIVER_SYNC_4);
  64. # endif
  65. # endif
  66. # endif
  67. # elif defined(IS31FL3737)
  68. IS31FL3737_init(DRIVER_ADDR_1);
  69. # if defined(DRIVER_ADDR_2)
  70. IS31FL3737_init(DRIVER_ADDR_2);
  71. # endif
  72. # elif defined(IS31FL3741)
  73. IS31FL3741_init(DRIVER_ADDR_1);
  74. # endif
  75. for (int index = 0; index < DRIVER_LED_TOTAL; index++) {
  76. bool enabled = true;
  77. // This only caches it for later
  78. # if defined(IS31FL3731)
  79. IS31FL3731_set_led_control_register(index, enabled, enabled, enabled);
  80. # elif defined(IS31FL3733)
  81. IS31FL3733_set_led_control_register(index, enabled, enabled, enabled);
  82. # elif defined(IS31FL3737)
  83. IS31FL3737_set_led_control_register(index, enabled, enabled, enabled);
  84. # elif defined(IS31FL3741)
  85. IS31FL3741_set_led_control_register(index, enabled, enabled, enabled);
  86. # endif
  87. }
  88. // This actually updates the LED drivers
  89. # if defined(IS31FL3731)
  90. IS31FL3731_update_led_control_registers(DRIVER_ADDR_1, 0);
  91. # if defined(DRIVER_ADDR_2)
  92. IS31FL3731_update_led_control_registers(DRIVER_ADDR_2, 1);
  93. # if defined(DRIVER_ADDR_3)
  94. IS31FL3731_update_led_control_registers(DRIVER_ADDR_3, 2);
  95. # if defined(DRIVER_ADDR_4)
  96. IS31FL3731_update_led_control_registers(DRIVER_ADDR_4, 3);
  97. # endif
  98. # endif
  99. # endif
  100. # elif defined(IS31FL3733)
  101. IS31FL3733_update_led_control_registers(DRIVER_ADDR_1, 0);
  102. # if defined(DRIVER_ADDR_2)
  103. IS31FL3733_update_led_control_registers(DRIVER_ADDR_2, 1);
  104. # if defined(DRIVER_ADDR_3)
  105. IS31FL3733_update_led_control_registers(DRIVER_ADDR_3, 2);
  106. # if defined(DRIVER_ADDR_4)
  107. IS31FL3733_update_led_control_registers(DRIVER_ADDR_4, 3);
  108. # endif
  109. # endif
  110. # endif
  111. # elif defined(IS31FL3737)
  112. IS31FL3737_update_led_control_registers(DRIVER_ADDR_1, 0);
  113. # if defined(DRIVER_ADDR_2)
  114. IS31FL3737_update_led_control_registers(DRIVER_ADDR_2, 1);
  115. # endif
  116. # elif defined(IS31FL3741)
  117. IS31FL3741_update_led_control_registers(DRIVER_ADDR_1, 0);
  118. # endif
  119. }
  120. # if defined(IS31FL3731)
  121. static void flush(void) {
  122. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_1, 0);
  123. # if defined(DRIVER_ADDR_2)
  124. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_2, 1);
  125. # if defined(DRIVER_ADDR_3)
  126. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_3, 2);
  127. # if defined(DRIVER_ADDR_4)
  128. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_4, 3);
  129. # endif
  130. # endif
  131. # endif
  132. }
  133. const rgb_matrix_driver_t rgb_matrix_driver = {
  134. .init = init,
  135. .flush = flush,
  136. .set_color = IS31FL3731_set_color,
  137. .set_color_all = IS31FL3731_set_color_all,
  138. };
  139. # elif defined(IS31FL3733)
  140. static void flush(void) {
  141. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_1, 0);
  142. # if defined(DRIVER_ADDR_2)
  143. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_2, 1);
  144. # if defined(DRIVER_ADDR_3)
  145. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_3, 2);
  146. # if defined(DRIVER_ADDR_4)
  147. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_4, 3);
  148. # endif
  149. # endif
  150. # endif
  151. }
  152. const rgb_matrix_driver_t rgb_matrix_driver = {
  153. .init = init,
  154. .flush = flush,
  155. .set_color = IS31FL3733_set_color,
  156. .set_color_all = IS31FL3733_set_color_all,
  157. };
  158. # elif defined(IS31FL3737)
  159. static void flush(void) {
  160. IS31FL3737_update_pwm_buffers(DRIVER_ADDR_1, 0);
  161. # if defined(DRIVER_ADDR_2)
  162. IS31FL3737_update_pwm_buffers(DRIVER_ADDR_2, 1);
  163. # endif
  164. }
  165. const rgb_matrix_driver_t rgb_matrix_driver = {
  166. .init = init,
  167. .flush = flush,
  168. .set_color = IS31FL3737_set_color,
  169. .set_color_all = IS31FL3737_set_color_all,
  170. };
  171. # elif defined(IS31FL3741)
  172. static void flush(void) {
  173. IS31FL3741_update_pwm_buffers(DRIVER_ADDR_1, 0);
  174. # if defined(DRIVER_ADDR_2)
  175. IS31FL3741_update_pwm_buffers(DRIVER_ADDR_2, 1);
  176. # endif
  177. }
  178. const rgb_matrix_driver_t rgb_matrix_driver = {
  179. .init = init,
  180. .flush = flush,
  181. .set_color = IS31FL3741_set_color,
  182. .set_color_all = IS31FL3741_set_color_all,
  183. };
  184. # endif
  185. #elif defined(AW20216)
  186. # include "spi_master.h"
  187. static void init(void) {
  188. spi_init();
  189. AW20216_init(DRIVER_1_CS, DRIVER_1_EN);
  190. # if defined(DRIVER_2_CS)
  191. AW20216_init(DRIVER_2_CS, DRIVER_2_EN);
  192. # endif
  193. }
  194. static void flush(void) {
  195. AW20216_update_pwm_buffers(DRIVER_1_CS, 0);
  196. # if defined(DRIVER_2_CS)
  197. AW20216_update_pwm_buffers(DRIVER_2_CS, 1);
  198. # endif
  199. }
  200. const rgb_matrix_driver_t rgb_matrix_driver = {
  201. .init = init,
  202. .flush = flush,
  203. .set_color = AW20216_set_color,
  204. .set_color_all = AW20216_set_color_all,
  205. };
  206. #elif defined(WS2812)
  207. # if defined(RGBLIGHT_ENABLE) && !defined(RGBLIGHT_CUSTOM_DRIVER)
  208. # pragma message "Cannot use RGBLIGHT and RGB Matrix using WS2812 at the same time."
  209. # pragma message "You need to use a custom driver, or re-implement the WS2812 driver to use a different configuration."
  210. # endif
  211. // LED color buffer
  212. LED_TYPE rgb_matrix_ws2812_array[DRIVER_LED_TOTAL];
  213. static void init(void) {}
  214. static void flush(void) {
  215. // Assumes use of RGB_DI_PIN
  216. ws2812_setleds(rgb_matrix_ws2812_array, DRIVER_LED_TOTAL);
  217. }
  218. // Set an led in the buffer to a color
  219. static inline void setled(int i, uint8_t r, uint8_t g, uint8_t b) {
  220. # if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  221. const uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT;
  222. if (!is_keyboard_left() && (i >= k_rgb_matrix_split[0])) {
  223. i -= k_rgb_matrix_split[0];
  224. } else if (is_keyboard_left() && (i >= k_rgb_matrix_split[0]))
  225. return;
  226. # endif
  227. rgb_matrix_ws2812_array[i].r = r;
  228. rgb_matrix_ws2812_array[i].g = g;
  229. rgb_matrix_ws2812_array[i].b = b;
  230. # ifdef RGBW
  231. convert_rgb_to_rgbw(&rgb_matrix_ws2812_array[i]);
  232. # endif
  233. }
  234. static void setled_all(uint8_t r, uint8_t g, uint8_t b) {
  235. for (int i = 0; i < sizeof(rgb_matrix_ws2812_array) / sizeof(rgb_matrix_ws2812_array[0]); i++) {
  236. setled(i, r, g, b);
  237. }
  238. }
  239. const rgb_matrix_driver_t rgb_matrix_driver = {
  240. .init = init,
  241. .flush = flush,
  242. .set_color = setled,
  243. .set_color_all = setled_all,
  244. };
  245. #endif