is31fl3737.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2018 Jack Humbert
  3. * Copyright 2018 Yiancar
  4. * Copyright 2021 Doni Crosby
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #pragma once
  20. #include <stdint.h>
  21. #include <stdbool.h>
  22. #include "progmem.h"
  23. #include "util.h"
  24. // ======== DEPRECATED DEFINES - DO NOT USE ========
  25. #ifdef DRIVER_ADDR_1
  26. # define IS31FL3737_I2C_ADDRESS_1 DRIVER_ADDR_1
  27. #endif
  28. #ifdef DRIVER_ADDR_2
  29. # define IS31FL3737_I2C_ADDRESS_2 DRIVER_ADDR_2
  30. #endif
  31. #ifdef DRIVER_ADDR_3
  32. # define IS31FL3737_I2C_ADDRESS_3 DRIVER_ADDR_3
  33. #endif
  34. #ifdef DRIVER_ADDR_4
  35. # define IS31FL3737_I2C_ADDRESS_4 DRIVER_ADDR_4
  36. #endif
  37. #ifdef ISSI_TIMEOUT
  38. # define IS31FL3737_I2C_TIMEOUT ISSI_TIMEOUT
  39. #endif
  40. #ifdef ISSI_PERSISTENCE
  41. # define IS31FL3737_I2C_PERSISTENCE ISSI_PERSISTENCE
  42. #endif
  43. #ifdef ISSI_PWM_FREQUENCY
  44. # define IS31FL3737_PWM_FREQUENCY ISSI_PWM_FREQUENCY
  45. #endif
  46. #ifdef ISSI_SWPULLUP
  47. # define IS31FL3737_SW_PULLUP ISSI_SWPULLUP
  48. #endif
  49. #ifdef ISSI_CSPULLUP
  50. # define IS31FL3737_CS_PULLDOWN ISSI_CSPULLUP
  51. #endif
  52. #ifdef ISSI_GLOBALCURRENT
  53. # define IS31FL3737_GLOBAL_CURRENT ISSI_GLOBALCURRENT
  54. #endif
  55. #define is31_led is31fl3737_led_t
  56. #define g_is31_leds g_is31fl3737_leds
  57. #define PUR_0R IS31FL3737_PUR_0_OHM
  58. #define PUR_05KR IS31FL3737_PUR_0K5_OHM
  59. #define PUR_1KR IS31FL3737_PUR_1K_OHM
  60. #define PUR_2KR IS31FL3737_PUR_2K_OHM
  61. #define PUR_4KR IS31FL3737_PUR_4K_OHM
  62. #define PUR_8KR IS31FL3737_PUR_8K_OHM
  63. #define PUR_16KR IS31FL3737_PUR_16K_OHM
  64. #define PUR_32KR IS31FL3737_PUR_32K_OHM
  65. // ========
  66. #define IS31FL3737_REG_INTERRUPT_MASK 0xF0
  67. #define IS31FL3737_REG_INTERRUPT_STATUS 0xF1
  68. #define IS31FL3737_REG_COMMAND 0xFD
  69. #define IS31FL3737_COMMAND_LED_CONTROL 0x00
  70. #define IS31FL3737_COMMAND_PWM 0x01
  71. #define IS31FL3737_COMMAND_AUTO_BREATH 0x02
  72. #define IS31FL3737_COMMAND_FUNCTION 0x03
  73. #define IS31FL3737_FUNCTION_REG_CONFIGURATION 0x00
  74. #define IS31FL3737_FUNCTION_REG_GLOBAL_CURRENT 0x01
  75. #define IS31FL3737_FUNCTION_REG_SW_PULLUP 0x0F
  76. #define IS31FL3737_FUNCTION_REG_CS_PULLDOWN 0x10
  77. #define IS31FL3737_FUNCTION_REG_RESET 0x11
  78. #define IS31FL3737_REG_COMMAND_WRITE_LOCK 0xFE
  79. #define IS31FL3737_COMMAND_WRITE_LOCK_MAGIC 0xC5
  80. #define IS31FL3737_I2C_ADDRESS_GND 0x50
  81. #define IS31FL3737_I2C_ADDRESS_SCL 0x55
  82. #define IS31FL3737_I2C_ADDRESS_SDA 0x5A
  83. #define IS31FL3737_I2C_ADDRESS_VCC 0x5F
  84. #if defined(RGB_MATRIX_IS31FL3737)
  85. # define IS31FL3737_LED_COUNT RGB_MATRIX_LED_COUNT
  86. #endif
  87. #if defined(IS31FL3737_I2C_ADDRESS_4)
  88. # define IS31FL3737_DRIVER_COUNT 4
  89. #elif defined(IS31FL3737_I2C_ADDRESS_3)
  90. # define IS31FL3737_DRIVER_COUNT 3
  91. #elif defined(IS31FL3737_I2C_ADDRESS_2)
  92. # define IS31FL3737_DRIVER_COUNT 2
  93. #elif defined(IS31FL3737_I2C_ADDRESS_1)
  94. # define IS31FL3737_DRIVER_COUNT 1
  95. #endif
  96. typedef struct is31fl3737_led_t {
  97. uint8_t driver : 2;
  98. uint8_t r;
  99. uint8_t g;
  100. uint8_t b;
  101. } PACKED is31fl3737_led_t;
  102. extern const is31fl3737_led_t PROGMEM g_is31fl3737_leds[IS31FL3737_LED_COUNT];
  103. void is31fl3737_init_drivers(void);
  104. void is31fl3737_init(uint8_t index);
  105. void is31fl3737_write_register(uint8_t index, uint8_t reg, uint8_t data);
  106. void is31fl3737_select_page(uint8_t index, uint8_t page);
  107. void is31fl3737_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
  108. void is31fl3737_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
  109. void is31fl3737_set_led_control_register(uint8_t index, bool red, bool green, bool blue);
  110. // This should not be called from an interrupt
  111. // (eg. from a timer interrupt).
  112. // Call this while idle (in between matrix scans).
  113. // If the buffer is dirty, it will update the driver with the buffer.
  114. void is31fl3737_update_pwm_buffers(uint8_t index);
  115. void is31fl3737_update_led_control_registers(uint8_t index);
  116. void is31fl3737_flush(void);
  117. #define IS31FL3737_PDR_0_OHM 0b000 // No pull-down resistor
  118. #define IS31FL3737_PDR_0K5_OHM 0b001 // 0.5 kOhm resistor
  119. #define IS31FL3737_PDR_1K_OHM 0b010 // 1 kOhm resistor
  120. #define IS31FL3737_PDR_2K_OHM 0b011 // 2 kOhm resistor
  121. #define IS31FL3737_PDR_4K_OHM 0b100 // 4 kOhm resistor
  122. #define IS31FL3737_PDR_8K_OHM 0b101 // 8 kOhm resistor
  123. #define IS31FL3737_PDR_16K_OHM 0b110 // 16 kOhm resistor
  124. #define IS31FL3737_PDR_32K_OHM 0b111 // 32 kOhm resistor
  125. #define IS31FL3737_PUR_0_OHM 0b000 // No pull-up resistor
  126. #define IS31FL3737_PUR_0K5_OHM 0b001 // 0.5 kOhm resistor
  127. #define IS31FL3737_PUR_1K_OHM 0b010 // 1 kOhm resistor
  128. #define IS31FL3737_PUR_2K_OHM 0b011 // 2 kOhm resistor
  129. #define IS31FL3737_PUR_4K_OHM 0b100 // 4 kOhm resistor
  130. #define IS31FL3737_PUR_8K_OHM 0b101 // 8 kOhm resistor
  131. #define IS31FL3737_PUR_16K_OHM 0b110 // 16 kOhm resistor
  132. #define IS31FL3737_PUR_32K_OHM 0b111 // 32 kOhm resistor
  133. #define IS31FL3737_PWM_FREQUENCY_8K4_HZ 0b000
  134. #define IS31FL3737_PWM_FREQUENCY_4K2_HZ 0b001
  135. #define IS31FL3737_PWM_FREQUENCY_26K7_HZ 0b010
  136. #define IS31FL3737_PWM_FREQUENCY_2K1_HZ 0b011
  137. #define IS31FL3737_PWM_FREQUENCY_1K05_HZ 0b100
  138. #define SW1_CS1 0x00
  139. #define SW1_CS2 0x01
  140. #define SW1_CS3 0x02
  141. #define SW1_CS4 0x03
  142. #define SW1_CS5 0x04
  143. #define SW1_CS6 0x05
  144. #define SW1_CS7 0x08
  145. #define SW1_CS8 0x09
  146. #define SW1_CS9 0x0A
  147. #define SW1_CS10 0x0B
  148. #define SW1_CS11 0x0C
  149. #define SW1_CS12 0x0D
  150. #define SW2_CS1 0x10
  151. #define SW2_CS2 0x11
  152. #define SW2_CS3 0x12
  153. #define SW2_CS4 0x13
  154. #define SW2_CS5 0x14
  155. #define SW2_CS6 0x15
  156. #define SW2_CS7 0x18
  157. #define SW2_CS8 0x19
  158. #define SW2_CS9 0x1A
  159. #define SW2_CS10 0x1B
  160. #define SW2_CS11 0x1C
  161. #define SW2_CS12 0x1D
  162. #define SW3_CS1 0x20
  163. #define SW3_CS2 0x21
  164. #define SW3_CS3 0x22
  165. #define SW3_CS4 0x23
  166. #define SW3_CS5 0x24
  167. #define SW3_CS6 0x25
  168. #define SW3_CS7 0x28
  169. #define SW3_CS8 0x29
  170. #define SW3_CS9 0x2A
  171. #define SW3_CS10 0x2B
  172. #define SW3_CS11 0x2C
  173. #define SW3_CS12 0x2D
  174. #define SW4_CS1 0x30
  175. #define SW4_CS2 0x31
  176. #define SW4_CS3 0x32
  177. #define SW4_CS4 0x33
  178. #define SW4_CS5 0x34
  179. #define SW4_CS6 0x35
  180. #define SW4_CS7 0x38
  181. #define SW4_CS8 0x39
  182. #define SW4_CS9 0x3A
  183. #define SW4_CS10 0x3B
  184. #define SW4_CS11 0x3C
  185. #define SW4_CS12 0x3D
  186. #define SW5_CS1 0x40
  187. #define SW5_CS2 0x41
  188. #define SW5_CS3 0x42
  189. #define SW5_CS4 0x43
  190. #define SW5_CS5 0x44
  191. #define SW5_CS6 0x45
  192. #define SW5_CS7 0x48
  193. #define SW5_CS8 0x49
  194. #define SW5_CS9 0x4A
  195. #define SW5_CS10 0x4B
  196. #define SW5_CS11 0x4C
  197. #define SW5_CS12 0x4D
  198. #define SW6_CS1 0x50
  199. #define SW6_CS2 0x51
  200. #define SW6_CS3 0x52
  201. #define SW6_CS4 0x53
  202. #define SW6_CS5 0x54
  203. #define SW6_CS6 0x55
  204. #define SW6_CS7 0x58
  205. #define SW6_CS8 0x59
  206. #define SW6_CS9 0x5A
  207. #define SW6_CS10 0x5B
  208. #define SW6_CS11 0x5C
  209. #define SW6_CS12 0x5D
  210. #define SW7_CS1 0x60
  211. #define SW7_CS2 0x61
  212. #define SW7_CS3 0x62
  213. #define SW7_CS4 0x63
  214. #define SW7_CS5 0x64
  215. #define SW7_CS6 0x65
  216. #define SW7_CS7 0x68
  217. #define SW7_CS8 0x69
  218. #define SW7_CS9 0x6A
  219. #define SW7_CS10 0x6B
  220. #define SW7_CS11 0x6C
  221. #define SW7_CS12 0x6D
  222. #define SW8_CS1 0x70
  223. #define SW8_CS2 0x71
  224. #define SW8_CS3 0x72
  225. #define SW8_CS4 0x73
  226. #define SW8_CS5 0x74
  227. #define SW8_CS6 0x75
  228. #define SW8_CS7 0x78
  229. #define SW8_CS8 0x79
  230. #define SW8_CS9 0x7A
  231. #define SW8_CS10 0x7B
  232. #define SW8_CS11 0x7C
  233. #define SW8_CS12 0x7D
  234. #define SW9_CS1 0x80
  235. #define SW9_CS2 0x81
  236. #define SW9_CS3 0x82
  237. #define SW9_CS4 0x83
  238. #define SW9_CS5 0x84
  239. #define SW9_CS6 0x85
  240. #define SW9_CS7 0x88
  241. #define SW9_CS8 0x89
  242. #define SW9_CS9 0x8A
  243. #define SW9_CS10 0x8B
  244. #define SW9_CS11 0x8C
  245. #define SW9_CS12 0x8D
  246. #define SW10_CS1 0x90
  247. #define SW10_CS2 0x91
  248. #define SW10_CS3 0x92
  249. #define SW10_CS4 0x93
  250. #define SW10_CS5 0x94
  251. #define SW10_CS6 0x95
  252. #define SW10_CS7 0x98
  253. #define SW10_CS8 0x99
  254. #define SW10_CS9 0x9A
  255. #define SW10_CS10 0x9B
  256. #define SW10_CS11 0x9C
  257. #define SW10_CS12 0x9D
  258. #define SW11_CS1 0xA0
  259. #define SW11_CS2 0xA1
  260. #define SW11_CS3 0xA2
  261. #define SW11_CS4 0xA3
  262. #define SW11_CS5 0xA4
  263. #define SW11_CS6 0xA5
  264. #define SW11_CS7 0xA8
  265. #define SW11_CS8 0xA9
  266. #define SW11_CS9 0xAA
  267. #define SW11_CS10 0xAB
  268. #define SW11_CS11 0xAC
  269. #define SW11_CS12 0xAD
  270. #define SW12_CS1 0xB0
  271. #define SW12_CS2 0xB1
  272. #define SW12_CS3 0xB2
  273. #define SW12_CS4 0xB3
  274. #define SW12_CS5 0xB4
  275. #define SW12_CS6 0xB5
  276. #define SW12_CS7 0xB8
  277. #define SW12_CS8 0xB9
  278. #define SW12_CS9 0xBA
  279. #define SW12_CS10 0xBB
  280. #define SW12_CS11 0xBC
  281. #define SW12_CS12 0xBD
  282. // DEPRECATED - DO NOT USE
  283. #define A_1 SW1_CS1
  284. #define A_2 SW1_CS2
  285. #define A_3 SW1_CS3
  286. #define A_4 SW1_CS4
  287. #define A_5 SW1_CS5
  288. #define A_6 SW1_CS6
  289. #define A_7 SW1_CS7
  290. #define A_8 SW1_CS8
  291. #define A_9 SW1_CS9
  292. #define A_10 SW1_CS10
  293. #define A_11 SW1_CS11
  294. #define A_12 SW1_CS12
  295. #define B_1 SW2_CS1
  296. #define B_2 SW2_CS2
  297. #define B_3 SW2_CS3
  298. #define B_4 SW2_CS4
  299. #define B_5 SW2_CS5
  300. #define B_6 SW2_CS6
  301. #define B_7 SW2_CS7
  302. #define B_8 SW2_CS8
  303. #define B_9 SW2_CS9
  304. #define B_10 SW2_CS10
  305. #define B_11 SW2_CS11
  306. #define B_12 SW2_CS12
  307. #define C_1 SW3_CS1
  308. #define C_2 SW3_CS2
  309. #define C_3 SW3_CS3
  310. #define C_4 SW3_CS4
  311. #define C_5 SW3_CS5
  312. #define C_6 SW3_CS6
  313. #define C_7 SW3_CS7
  314. #define C_8 SW3_CS8
  315. #define C_9 SW3_CS9
  316. #define C_10 SW3_CS10
  317. #define C_11 SW3_CS11
  318. #define C_12 SW3_CS12
  319. #define D_1 SW4_CS1
  320. #define D_2 SW4_CS2
  321. #define D_3 SW4_CS3
  322. #define D_4 SW4_CS4
  323. #define D_5 SW4_CS5
  324. #define D_6 SW4_CS6
  325. #define D_7 SW4_CS7
  326. #define D_8 SW4_CS8
  327. #define D_9 SW4_CS9
  328. #define D_10 SW4_CS10
  329. #define D_11 SW4_CS11
  330. #define D_12 SW4_CS12
  331. #define E_1 SW5_CS1
  332. #define E_2 SW5_CS2
  333. #define E_3 SW5_CS3
  334. #define E_4 SW5_CS4
  335. #define E_5 SW5_CS5
  336. #define E_6 SW5_CS6
  337. #define E_7 SW5_CS7
  338. #define E_8 SW5_CS8
  339. #define E_9 SW5_CS9
  340. #define E_10 SW5_CS10
  341. #define E_11 SW5_CS11
  342. #define E_12 SW5_CS12
  343. #define F_1 SW6_CS1
  344. #define F_2 SW6_CS2
  345. #define F_3 SW6_CS3
  346. #define F_4 SW6_CS4
  347. #define F_5 SW6_CS5
  348. #define F_6 SW6_CS6
  349. #define F_7 SW6_CS7
  350. #define F_8 SW6_CS8
  351. #define F_9 SW6_CS9
  352. #define F_10 SW6_CS10
  353. #define F_11 SW6_CS11
  354. #define F_12 SW6_CS12
  355. #define G_1 SW7_CS1
  356. #define G_2 SW7_CS2
  357. #define G_3 SW7_CS3
  358. #define G_4 SW7_CS4
  359. #define G_5 SW7_CS5
  360. #define G_6 SW7_CS6
  361. #define G_7 SW7_CS7
  362. #define G_8 SW7_CS8
  363. #define G_9 SW7_CS9
  364. #define G_10 SW7_CS10
  365. #define G_11 SW7_CS11
  366. #define G_12 SW7_CS12
  367. #define H_1 SW8_CS1
  368. #define H_2 SW8_CS2
  369. #define H_3 SW8_CS3
  370. #define H_4 SW8_CS4
  371. #define H_5 SW8_CS5
  372. #define H_6 SW8_CS6
  373. #define H_7 SW8_CS7
  374. #define H_8 SW8_CS8
  375. #define H_9 SW8_CS9
  376. #define H_10 SW8_CS10
  377. #define H_11 SW8_CS11
  378. #define H_12 SW8_CS12
  379. #define I_1 SW9_CS1
  380. #define I_2 SW9_CS2
  381. #define I_3 SW9_CS3
  382. #define I_4 SW9_CS4
  383. #define I_5 SW9_CS5
  384. #define I_6 SW9_CS6
  385. #define I_7 SW9_CS7
  386. #define I_8 SW9_CS8
  387. #define I_9 SW9_CS9
  388. #define I_10 SW9_CS10
  389. #define I_11 SW9_CS11
  390. #define I_12 SW9_CS12
  391. #define J_1 SW10_CS1
  392. #define J_2 SW10_CS2
  393. #define J_3 SW10_CS3
  394. #define J_4 SW10_CS4
  395. #define J_5 SW10_CS5
  396. #define J_6 SW10_CS6
  397. #define J_7 SW10_CS7
  398. #define J_8 SW10_CS8
  399. #define J_9 SW10_CS9
  400. #define J_10 SW10_CS10
  401. #define J_11 SW10_CS11
  402. #define J_12 SW10_CS12
  403. #define K_1 SW11_CS1
  404. #define K_2 SW11_CS2
  405. #define K_3 SW11_CS3
  406. #define K_4 SW11_CS4
  407. #define K_5 SW11_CS5
  408. #define K_6 SW11_CS6
  409. #define K_7 SW11_CS7
  410. #define K_8 SW11_CS8
  411. #define K_9 SW11_CS9
  412. #define K_10 SW11_CS10
  413. #define K_11 SW11_CS11
  414. #define K_12 SW11_CS12
  415. #define L_1 SW12_CS1
  416. #define L_2 SW12_CS2
  417. #define L_3 SW12_CS3
  418. #define L_4 SW12_CS4
  419. #define L_5 SW12_CS5
  420. #define L_6 SW12_CS6
  421. #define L_7 SW12_CS7
  422. #define L_8 SW12_CS8
  423. #define L_9 SW12_CS9
  424. #define L_10 SW12_CS10
  425. #define L_11 SW12_CS11
  426. #define L_12 SW12_CS12