rgb_matrix_drivers.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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) || defined(CKLED2001)
  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. # elif defined(CKLED2001)
  75. CKLED2001_init(DRIVER_ADDR_1);
  76. # if defined(DRIVER_ADDR_2)
  77. CKLED2001_init(DRIVER_ADDR_2);
  78. # if defined(DRIVER_ADDR_3)
  79. CKLED2001_init(DRIVER_ADDR_3);
  80. # if defined(DRIVER_ADDR_4)
  81. CKLED2001_init(DRIVER_ADDR_4);
  82. # endif
  83. # endif
  84. # endif
  85. # endif
  86. for (int index = 0; index < DRIVER_LED_TOTAL; index++) {
  87. bool enabled = true;
  88. // This only caches it for later
  89. # if defined(IS31FL3731)
  90. IS31FL3731_set_led_control_register(index, enabled, enabled, enabled);
  91. # elif defined(IS31FL3733)
  92. IS31FL3733_set_led_control_register(index, enabled, enabled, enabled);
  93. # elif defined(IS31FL3737)
  94. IS31FL3737_set_led_control_register(index, enabled, enabled, enabled);
  95. # elif defined(IS31FL3741)
  96. IS31FL3741_set_led_control_register(index, enabled, enabled, enabled);
  97. # elif defined(CKLED2001)
  98. CKLED2001_set_led_control_register(index, enabled, enabled, enabled);
  99. # endif
  100. }
  101. // This actually updates the LED drivers
  102. # if defined(IS31FL3731)
  103. IS31FL3731_update_led_control_registers(DRIVER_ADDR_1, 0);
  104. # if defined(DRIVER_ADDR_2)
  105. IS31FL3731_update_led_control_registers(DRIVER_ADDR_2, 1);
  106. # if defined(DRIVER_ADDR_3)
  107. IS31FL3731_update_led_control_registers(DRIVER_ADDR_3, 2);
  108. # if defined(DRIVER_ADDR_4)
  109. IS31FL3731_update_led_control_registers(DRIVER_ADDR_4, 3);
  110. # endif
  111. # endif
  112. # endif
  113. # elif defined(IS31FL3733)
  114. IS31FL3733_update_led_control_registers(DRIVER_ADDR_1, 0);
  115. # if defined(DRIVER_ADDR_2)
  116. IS31FL3733_update_led_control_registers(DRIVER_ADDR_2, 1);
  117. # if defined(DRIVER_ADDR_3)
  118. IS31FL3733_update_led_control_registers(DRIVER_ADDR_3, 2);
  119. # if defined(DRIVER_ADDR_4)
  120. IS31FL3733_update_led_control_registers(DRIVER_ADDR_4, 3);
  121. # endif
  122. # endif
  123. # endif
  124. # elif defined(IS31FL3737)
  125. IS31FL3737_update_led_control_registers(DRIVER_ADDR_1, 0);
  126. # if defined(DRIVER_ADDR_2)
  127. IS31FL3737_update_led_control_registers(DRIVER_ADDR_2, 1);
  128. # endif
  129. # elif defined(IS31FL3741)
  130. IS31FL3741_update_led_control_registers(DRIVER_ADDR_1, 0);
  131. # elif defined(CKLED2001)
  132. CKLED2001_update_led_control_registers(DRIVER_ADDR_1, 0);
  133. # if defined(DRIVER_ADDR_2)
  134. CKLED2001_update_led_control_registers(DRIVER_ADDR_2, 1);
  135. # if defined(DRIVER_ADDR_3)
  136. CKLED2001_update_led_control_registers(DRIVER_ADDR_3, 2);
  137. # if defined(DRIVER_ADDR_4)
  138. CKLED2001_update_led_control_registers(DRIVER_ADDR_4, 3);
  139. # endif
  140. # endif
  141. # endif
  142. # endif
  143. }
  144. # if defined(IS31FL3731)
  145. static void flush(void) {
  146. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_1, 0);
  147. # if defined(DRIVER_ADDR_2)
  148. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_2, 1);
  149. # if defined(DRIVER_ADDR_3)
  150. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_3, 2);
  151. # if defined(DRIVER_ADDR_4)
  152. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_4, 3);
  153. # endif
  154. # endif
  155. # endif
  156. }
  157. const rgb_matrix_driver_t rgb_matrix_driver = {
  158. .init = init,
  159. .flush = flush,
  160. .set_color = IS31FL3731_set_color,
  161. .set_color_all = IS31FL3731_set_color_all,
  162. };
  163. # elif defined(IS31FL3733)
  164. static void flush(void) {
  165. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_1, 0);
  166. # if defined(DRIVER_ADDR_2)
  167. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_2, 1);
  168. # if defined(DRIVER_ADDR_3)
  169. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_3, 2);
  170. # if defined(DRIVER_ADDR_4)
  171. IS31FL3733_update_pwm_buffers(DRIVER_ADDR_4, 3);
  172. # endif
  173. # endif
  174. # endif
  175. }
  176. const rgb_matrix_driver_t rgb_matrix_driver = {
  177. .init = init,
  178. .flush = flush,
  179. .set_color = IS31FL3733_set_color,
  180. .set_color_all = IS31FL3733_set_color_all,
  181. };
  182. # elif defined(IS31FL3737)
  183. static void flush(void) {
  184. IS31FL3737_update_pwm_buffers(DRIVER_ADDR_1, 0);
  185. # if defined(DRIVER_ADDR_2)
  186. IS31FL3737_update_pwm_buffers(DRIVER_ADDR_2, 1);
  187. # endif
  188. }
  189. const rgb_matrix_driver_t rgb_matrix_driver = {
  190. .init = init,
  191. .flush = flush,
  192. .set_color = IS31FL3737_set_color,
  193. .set_color_all = IS31FL3737_set_color_all,
  194. };
  195. # elif defined(IS31FL3741)
  196. static void flush(void) {
  197. IS31FL3741_update_pwm_buffers(DRIVER_ADDR_1, 0);
  198. # if defined(DRIVER_ADDR_2)
  199. IS31FL3741_update_pwm_buffers(DRIVER_ADDR_2, 1);
  200. # endif
  201. }
  202. const rgb_matrix_driver_t rgb_matrix_driver = {
  203. .init = init,
  204. .flush = flush,
  205. .set_color = IS31FL3741_set_color,
  206. .set_color_all = IS31FL3741_set_color_all,
  207. };
  208. # elif defined(CKLED2001)
  209. static void flush(void) {
  210. CKLED2001_update_pwm_buffers(DRIVER_ADDR_1, 0);
  211. # if defined(DRIVER_ADDR_2)
  212. CKLED2001_update_pwm_buffers(DRIVER_ADDR_2, 1);
  213. # if defined(DRIVER_ADDR_3)
  214. CKLED2001_update_pwm_buffers(DRIVER_ADDR_3, 2);
  215. # if defined(DRIVER_ADDR_4)
  216. CKLED2001_update_pwm_buffers(DRIVER_ADDR_4, 3);
  217. # endif
  218. # endif
  219. # endif
  220. }
  221. const rgb_matrix_driver_t rgb_matrix_driver = {
  222. .init = init,
  223. .flush = flush,
  224. .set_color = CKLED2001_set_color,
  225. .set_color_all = CKLED2001_set_color_all,
  226. };
  227. # endif
  228. #elif defined(AW20216)
  229. # include "spi_master.h"
  230. static void init(void) {
  231. spi_init();
  232. AW20216_init(DRIVER_1_CS, DRIVER_1_EN);
  233. # if defined(DRIVER_2_CS)
  234. AW20216_init(DRIVER_2_CS, DRIVER_2_EN);
  235. # endif
  236. }
  237. static void flush(void) {
  238. AW20216_update_pwm_buffers(DRIVER_1_CS, 0);
  239. # if defined(DRIVER_2_CS)
  240. AW20216_update_pwm_buffers(DRIVER_2_CS, 1);
  241. # endif
  242. }
  243. const rgb_matrix_driver_t rgb_matrix_driver = {
  244. .init = init,
  245. .flush = flush,
  246. .set_color = AW20216_set_color,
  247. .set_color_all = AW20216_set_color_all,
  248. };
  249. #elif defined(WS2812)
  250. # if defined(RGBLIGHT_ENABLE) && !defined(RGBLIGHT_CUSTOM_DRIVER)
  251. # pragma message "Cannot use RGBLIGHT and RGB Matrix using WS2812 at the same time."
  252. # pragma message "You need to use a custom driver, or re-implement the WS2812 driver to use a different configuration."
  253. # endif
  254. // LED color buffer
  255. LED_TYPE rgb_matrix_ws2812_array[DRIVER_LED_TOTAL];
  256. static void init(void) {}
  257. static void flush(void) {
  258. // Assumes use of RGB_DI_PIN
  259. ws2812_setleds(rgb_matrix_ws2812_array, DRIVER_LED_TOTAL);
  260. }
  261. // Set an led in the buffer to a color
  262. static inline void setled(int i, uint8_t r, uint8_t g, uint8_t b) {
  263. # if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT)
  264. const uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT;
  265. if (!is_keyboard_left() && (i >= k_rgb_matrix_split[0])) {
  266. i -= k_rgb_matrix_split[0];
  267. } else if (is_keyboard_left() && (i >= k_rgb_matrix_split[0]))
  268. return;
  269. # endif
  270. rgb_matrix_ws2812_array[i].r = r;
  271. rgb_matrix_ws2812_array[i].g = g;
  272. rgb_matrix_ws2812_array[i].b = b;
  273. # ifdef RGBW
  274. convert_rgb_to_rgbw(&rgb_matrix_ws2812_array[i]);
  275. # endif
  276. }
  277. static void setled_all(uint8_t r, uint8_t g, uint8_t b) {
  278. for (int i = 0; i < sizeof(rgb_matrix_ws2812_array) / sizeof(rgb_matrix_ws2812_array[0]); i++) {
  279. setled(i, r, g, b);
  280. }
  281. }
  282. const rgb_matrix_driver_t rgb_matrix_driver = {
  283. .init = init,
  284. .flush = flush,
  285. .set_color = setled,
  286. .set_color_all = setled_all,
  287. };
  288. #endif