rgb_matrix_drivers.c 12 KB

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