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.

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