rgb_backlight.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  1. /* Copyright 2017 Jason Williams (Wilba)
  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. #if RGB_BACKLIGHT_ENABLED
  17. #if defined(RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_ZEAL65) || defined(RGB_BACKLIGHT_M60_A) || defined(RGB_BACKLIGHT_M6_B) || defined(RGB_BACKLIGHT_KOYU) || defined(RGB_BACKLIGHT_HS60) || defined(RGB_BACKLIGHT_NK65)
  18. #else
  19. #error None of the following was defined: RGB_BACKLIGHT_ZEAL60, RGB_BACKLIGHT_ZEAL65, RGB_BACKLIGHT_M60_A, RGB_BACKLIGHT_M6_B, RGB_BACKLIGHT_KOYU, RGB_BACKLIGHT_HS60, RGB_BACKLIGHT_NK65
  20. #endif
  21. #ifndef MAX
  22. #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
  23. #endif
  24. #ifndef MIN
  25. #define MIN(a,b) ((a) < (b)? (a): (b))
  26. #endif
  27. #include "quantum.h"
  28. #include "rgb_backlight.h"
  29. #include "rgb_backlight_api.h"
  30. #include "rgb_backlight_keycodes.h"
  31. #if !defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
  32. #include <avr/io.h>
  33. #include <util/delay.h>
  34. #include <avr/interrupt.h>
  35. #include "drivers/avr/i2c_master.h"
  36. #else
  37. #include "ch.h"
  38. #include "hal.h"
  39. #include "drivers/arm/i2c_master.h"
  40. #include "tmk_core/common/eeprom.h"
  41. #endif
  42. #include "progmem.h"
  43. #include "quantum/color.h"
  44. #if defined(RGB_BACKLIGHT_M6_B)
  45. #include "drivers/issi/is31fl3218.h"
  46. #define BACKLIGHT_LED_COUNT 6
  47. #elif defined(RGB_BACKLIGHT_HS60)
  48. #include "drivers/issi/is31fl3733.h"
  49. #define BACKLIGHT_LED_COUNT 64
  50. #elif defined(RGB_BACKLIGHT_NK65)
  51. #include "drivers/issi/is31fl3733.h"
  52. #define BACKLIGHT_LED_COUNT 69
  53. #else
  54. #include "drivers/issi/is31fl3731.h"
  55. #define BACKLIGHT_LED_COUNT 72
  56. #endif
  57. #define BACKLIGHT_EFFECT_MAX 10
  58. backlight_config g_config = {
  59. .use_split_backspace = RGB_BACKLIGHT_USE_SPLIT_BACKSPACE,
  60. .use_split_left_shift = RGB_BACKLIGHT_USE_SPLIT_LEFT_SHIFT,
  61. .use_split_right_shift = RGB_BACKLIGHT_USE_SPLIT_RIGHT_SHIFT,
  62. .use_7u_spacebar = RGB_BACKLIGHT_USE_7U_SPACEBAR,
  63. .use_iso_enter = RGB_BACKLIGHT_USE_ISO_ENTER,
  64. .disable_hhkb_blocker_leds = RGB_BACKLIGHT_DISABLE_HHKB_BLOCKER_LEDS,
  65. .disable_when_usb_suspended = RGB_BACKLIGHT_DISABLE_WHEN_USB_SUSPENDED,
  66. .disable_after_timeout = RGB_BACKLIGHT_DISABLE_AFTER_TIMEOUT,
  67. .brightness = RGB_BACKLIGHT_BRIGHTNESS,
  68. .effect = RGB_BACKLIGHT_EFFECT,
  69. .effect_speed = RGB_BACKLIGHT_EFFECT_SPEED,
  70. .color_1 = RGB_BACKLIGHT_COLOR_1,
  71. .color_2 = RGB_BACKLIGHT_COLOR_2,
  72. .caps_lock_indicator = RGB_BACKLIGHT_CAPS_LOCK_INDICATOR,
  73. .layer_1_indicator = RGB_BACKLIGHT_LAYER_1_INDICATOR,
  74. .layer_2_indicator = RGB_BACKLIGHT_LAYER_2_INDICATOR,
  75. .layer_3_indicator = RGB_BACKLIGHT_LAYER_3_INDICATOR,
  76. .alphas_mods = {
  77. RGB_BACKLIGHT_ALPHAS_MODS_ROW_0,
  78. RGB_BACKLIGHT_ALPHAS_MODS_ROW_1,
  79. RGB_BACKLIGHT_ALPHAS_MODS_ROW_2,
  80. RGB_BACKLIGHT_ALPHAS_MODS_ROW_3,
  81. RGB_BACKLIGHT_ALPHAS_MODS_ROW_4 },
  82. #if defined(RGB_BACKLIGHT_M6_B)
  83. .custom_color = { { 0, 255 }, { 43, 255 }, { 85, 255 }, { 128, 255 }, { 171, 255 }, { 213, 255 } }
  84. #endif
  85. };
  86. bool g_suspend_state = false;
  87. uint8_t g_indicator_state = 0;
  88. // Global tick at 20 Hz
  89. uint32_t g_tick = 0;
  90. // Ticks since this key was last hit.
  91. uint8_t g_key_hit[BACKLIGHT_LED_COUNT];
  92. // Ticks since any key was last hit.
  93. uint32_t g_any_key_hit = 0;
  94. #if defined(RGB_BACKLIGHT_HS60)
  95. // This is a 7-bit address, that gets left-shifted and bit 0
  96. // set to 0 for write, 1 for read (as per I2C protocol)
  97. // ADDR_2 is not needed. it is here as a dummy
  98. #define ISSI_ADDR_1 0x50
  99. const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
  100. /* Refer to IS31 manual for these locations
  101. * driver
  102. * | R location
  103. * | | G location
  104. * | | | B location
  105. * | | | | */
  106. {0, B_1, A_1, C_1}, //LA1
  107. {0, E_1, D_1, F_1}, //LA2
  108. {0, H_1, G_1, I_1}, //LA3
  109. {0, K_1, J_1, L_1}, //LA4
  110. {0, B_2, A_2, C_2}, //LA5
  111. {0, E_2, D_2, F_2}, //LA6
  112. {0, H_2, G_2, I_2}, //LA7
  113. {0, K_2, J_2, L_2}, //LA8
  114. {0, B_3, A_3, C_3}, //LA9
  115. {0, E_3, D_3, F_3}, //LA10
  116. {0, H_3, G_3, I_3}, //LA11
  117. {0, K_3, J_3, L_3}, //LA12
  118. {0, B_4, A_4, C_4}, //LA13
  119. {0, E_4, D_4, F_4}, //LA14
  120. {0, H_4, G_4, I_4}, //LA15
  121. {0, K_4, J_4, L_4}, //LA16
  122. {0, B_5, A_5, C_5}, //LA17
  123. {0, E_5, D_5, F_5}, //LA18
  124. {0, H_5, G_5, I_5}, //LA19
  125. {0, K_5, J_5, L_5}, //LA20
  126. {0, B_6, A_6, C_6}, //LA21
  127. {0, E_6, D_6, F_6}, //LA22
  128. {0, H_6, G_6, I_6}, //LA23
  129. {0, K_6, J_6, L_6}, //LA24
  130. {0, B_7, A_7, C_7}, //LA25
  131. {0, E_7, D_7, F_7}, //LA26
  132. {0, H_7, G_7, I_7}, //LA27
  133. {0, K_7, J_7, L_7}, //LA28
  134. {0, B_8, A_8, C_8}, //LA29
  135. {0, E_8, D_8, F_8}, //LA30
  136. {0, H_8, G_8, I_8}, //LA31
  137. {0, K_8, J_8, L_8}, //LA32
  138. {0, B_9, A_9, C_9}, //LA33
  139. {0, E_9, D_9, F_9}, //LA34
  140. {0, H_9, G_9, I_9}, //LA35
  141. {0, K_9, J_9, L_9}, //LA36
  142. {0, B_10, A_10, C_10}, //LA37
  143. {0, E_10, D_10, F_10}, //LA38
  144. {0, H_10, G_10, I_10}, //LA39
  145. {0, K_10, J_10, L_10}, //LA40
  146. {0, B_11, A_11, C_11}, //LA41
  147. {0, E_11, D_11, F_11}, //LA42
  148. {0, H_11, G_11, I_11}, //LA43
  149. {0, K_11, J_11, L_11}, //LA44
  150. {0, B_12, A_12, C_12}, //LA45
  151. {0, E_12, D_12, F_12}, //LA46
  152. {0, H_12, G_12, I_12}, //LA47
  153. {0, K_12, J_12, L_12}, //LA48
  154. {0, B_13, A_13, C_13}, //LA49
  155. {0, E_13, D_13, F_13}, //LA50
  156. {0, H_13, G_13, I_13}, //LA51
  157. {0, K_13, J_13, L_13}, //LA52
  158. {0, B_14, A_14, C_14}, //LA53
  159. {0, E_14, D_14, F_14}, //LA54
  160. {0, H_14, G_14, I_14}, //LA55
  161. {0, K_14, J_14, L_14}, //LA56
  162. {0, B_15, A_15, C_15}, //LA57
  163. {0, E_15, D_15, F_15}, //LA58
  164. {0, H_15, G_15, I_15}, //LA59
  165. {0, K_15, J_15, L_15}, //LA60
  166. {0, B_16, A_16, C_16}, //LA61
  167. {0, E_16, D_16, F_16}, //LA62
  168. {0, H_16, G_16, I_16}, //LA63
  169. {0, K_16, J_16, L_16}, //LA64
  170. };
  171. #elif defined(RGB_BACKLIGHT_NK65)
  172. // This is a 7-bit address, that gets left-shifted and bit 0
  173. // set to 0 for write, 1 for read (as per I2C protocol)
  174. // ADDR_2 is not needed. it is here as a dummy
  175. #define ISSI_ADDR_1 0x50
  176. #define ISSI_ADDR_2 0x52
  177. const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
  178. /* Refer to IS31 manual for these locations
  179. * driver
  180. * | R location
  181. * | | G location
  182. * | | | B location
  183. * | | | | */
  184. {0, B_1, A_1, C_1}, //LA1
  185. {0, E_1, D_1, F_1}, //LA2
  186. {0, H_1, G_1, I_1}, //LA3
  187. {0, K_1, J_1, L_1}, //LA4
  188. {0, B_2, A_2, C_2}, //LA5
  189. {0, E_2, D_2, F_2}, //LA6
  190. {0, H_2, G_2, I_2}, //LA7
  191. {0, K_2, J_2, L_2}, //LA8
  192. {0, B_3, A_3, C_3}, //LA9
  193. {0, E_3, D_3, F_3}, //LA10
  194. {0, H_3, G_3, I_3}, //LA11
  195. {0, K_3, J_3, L_3}, //LA12
  196. {0, B_4, A_4, C_4}, //LA13
  197. {0, E_4, D_4, F_4}, //LA14
  198. {0, H_4, G_4, I_4}, //LA15
  199. {0, K_4, J_4, L_4}, //LA16
  200. {0, B_5, A_5, C_5}, //LA17
  201. {0, E_5, D_5, F_5}, //LA18
  202. {0, H_5, G_5, I_5}, //LA19
  203. {0, K_5, J_5, L_5}, //LA20
  204. {0, B_6, A_6, C_6}, //LA21
  205. {0, E_6, D_6, F_6}, //LA22
  206. {0, H_6, G_6, I_6}, //LA23
  207. {0, K_6, J_6, L_6}, //LA24
  208. {0, B_7, A_7, C_7}, //LA25
  209. {0, E_7, D_7, F_7}, //LA26
  210. {0, H_7, G_7, I_7}, //LA27
  211. {0, K_7, J_7, L_7}, //LA28
  212. {0, B_8, A_8, C_8}, //LA29
  213. {0, E_8, D_8, F_8}, //LA30
  214. {0, H_8, G_8, I_8}, //LA31
  215. {0, K_8, J_8, L_8}, //LA32
  216. {0, B_9, A_9, C_9}, //LA33
  217. {0, E_9, D_9, F_9}, //LA34
  218. {0, H_9, G_9, I_9}, //LA35
  219. {0, K_9, J_9, L_9}, //LA36
  220. {0, B_10, A_10, C_10}, //LA37
  221. {0, E_10, D_10, F_10}, //LA38
  222. {0, H_10, G_10, I_10}, //LA39
  223. {0, K_10, J_10, L_10}, //LA40
  224. {0, B_11, A_11, C_11}, //LA41
  225. {0, E_11, D_11, F_11}, //LA42
  226. {0, H_11, G_11, I_11}, //LA43
  227. {0, K_11, J_11, L_11}, //LA44
  228. {0, B_12, A_12, C_12}, //LA45
  229. {0, E_12, D_12, F_12}, //LA46
  230. {0, H_12, G_12, I_12}, //LA47
  231. {0, K_12, J_12, L_12}, //LA48
  232. {0, B_13, A_13, C_13}, //LA49
  233. {0, E_13, D_13, F_13}, //LA50
  234. {0, H_13, G_13, I_13}, //LA51
  235. {0, K_13, J_13, L_13}, //LA52
  236. {0, B_14, A_14, C_14}, //LA53
  237. {0, E_14, D_14, F_14}, //LA54
  238. {0, H_14, G_14, I_14}, //LA55
  239. {0, K_14, J_14, L_14}, //LA56
  240. {0, B_15, A_15, C_15}, //LA57
  241. {0, E_15, D_15, F_15}, //LA58
  242. {0, H_15, G_15, I_15}, //LA59
  243. {0, K_15, J_15, L_15}, //LA60
  244. {0, B_16, A_16, C_16}, //LA61
  245. {0, E_16, D_16, F_16}, //LA62
  246. {0, H_16, G_16, I_16}, //LA63
  247. {0, K_16, J_16, L_16}, //LA64
  248. {1, B_1, A_1, C_1}, //LB1
  249. {1, E_1, D_1, F_1}, //LB2
  250. {1, H_1, G_1, I_1}, //LB3
  251. {1, K_1, J_1, L_1}, //LB4
  252. {1, B_2, A_2, C_2}, //LB5
  253. {1, E_2, D_2, F_2}, //LB6
  254. {1, H_2, G_2, I_2}, //LB7
  255. {1, K_2, J_2, L_2}, //LB8
  256. {1, B_3, A_3, C_3}, //LB9
  257. {1, E_3, D_3, F_3}, //LB10
  258. {1, H_3, G_3, I_3}, //LB11
  259. {1, K_3, J_3, L_3}, //LB12
  260. {1, B_4, A_4, C_4}, //LB13
  261. {1, E_4, D_4, F_4}, //LB14
  262. {1, H_4, G_4, I_4}, //LB15
  263. {1, K_4, J_4, L_4}, //LB16
  264. {1, B_5, A_5, C_5}, //LB17
  265. {1, E_5, D_5, F_5}, //LB18
  266. {1, H_5, G_5, I_5}, //LB19
  267. {1, K_5, J_5, L_5}, //LB20
  268. {1, B_6, A_6, C_6}, //LB21
  269. {1, E_6, D_6, F_6}, //LB22
  270. {1, H_6, G_6, I_6}, //LB23
  271. {1, K_6, J_6, L_6}, //LB24
  272. {1, B_7, A_7, C_7}, //LB25
  273. {1, E_7, D_7, F_7}, //LB26
  274. {1, H_7, G_7, I_7}, //LB27
  275. {1, K_7, J_7, L_7}, //LB28
  276. {1, B_8, A_8, C_8}, //LB29
  277. {1, E_8, D_8, F_8}, //LB30
  278. {1, H_8, G_8, I_8}, //LB31
  279. {1, K_8, J_8, L_8}, //LB32
  280. {1, B_9, A_9, C_9}, //LB33
  281. {1, E_9, D_9, F_9}, //LB34
  282. {1, H_9, G_9, I_9}, //LB35
  283. {1, K_9, J_9, L_9}, //LB36
  284. {1, B_10, A_10, C_10}, //LB37
  285. {1, E_10, D_10, F_10}, //LB38
  286. {1, H_10, G_10, I_10}, //LB39
  287. {1, K_10, J_10, L_10}, //LB40
  288. {1, B_11, A_11, C_11}, //LB41
  289. {1, E_11, D_11, F_11}, //LB42
  290. {1, H_11, G_11, I_11}, //LB43
  291. {1, K_11, J_11, L_11}, //LB44
  292. {1, B_12, A_12, C_12}, //LB45
  293. {1, E_12, D_12, F_12}, //LB46
  294. {1, H_12, G_12, I_12}, //LB47
  295. {1, K_12, J_12, L_12}, //LB48
  296. {1, B_13, A_13, C_13}, //LB49
  297. {1, E_13, D_13, F_13}, //LB50
  298. {1, H_13, G_13, I_13}, //LB51
  299. {1, K_13, J_13, L_13}, //LB52
  300. {1, B_14, A_14, C_14}, //LB53
  301. {1, E_14, D_14, F_14}, //LB54
  302. {1, H_14, G_14, I_14}, //LB55
  303. {1, K_14, J_14, L_14}, //LB56
  304. {1, B_15, A_15, C_15}, //LB57
  305. {1, E_15, D_15, F_15}, //LB58
  306. {1, H_15, G_15, I_15}, //LB59
  307. {1, K_15, J_15, L_15}, //LB60
  308. {1, B_16, A_16, C_16}, //LB61
  309. {1, E_16, D_16, F_16}, //LB62
  310. {1, H_16, G_16, I_16}, //LB63
  311. {1, K_16, J_16, L_16}, //LB64
  312. };
  313. #elif !defined(RGB_BACKLIGHT_M6_B)
  314. // This is a 7-bit address, that gets left-shifted and bit 0
  315. // set to 0 for write, 1 for read (as per I2C protocol)
  316. #define ISSI_ADDR_1 0x74
  317. #define ISSI_ADDR_2 0x76
  318. const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
  319. /* Refer to IS31 manual for these locations
  320. * driver
  321. * | R location
  322. * | | G location
  323. * | | | B location
  324. * | | | | */
  325. {0, C2_1, C3_1, C4_1}, // LA0
  326. {0, C1_1, C3_2, C4_2}, // LA1
  327. {0, C1_2, C2_2, C4_3}, // LA2
  328. {0, C1_3, C2_3, C3_3}, // LA3
  329. {0, C1_4, C2_4, C3_4}, // LA4
  330. {0, C1_5, C2_5, C3_5}, // LA5
  331. {0, C1_6, C2_6, C3_6}, // LA6
  332. {0, C1_7, C2_7, C3_7}, // LA7
  333. {0, C1_8, C2_8, C3_8}, // LA8
  334. {0, C9_1, C8_1, C7_1}, // LA9
  335. {0, C9_2, C8_2, C7_2}, // LA10
  336. {0, C9_3, C8_3, C7_3}, // LA11
  337. {0, C9_4, C8_4, C7_4}, // LA12
  338. {0, C9_5, C8_5, C7_5}, // LA13
  339. {0, C9_6, C8_6, C7_6}, // LA14
  340. {0, C9_7, C8_7, C6_6}, // LA15
  341. {0, C9_8, C7_7, C6_7}, // LA16
  342. {0, C8_8, C7_8, C6_8}, // LA17
  343. {0, C2_9, C3_9, C4_9}, // LB0
  344. {0, C1_9, C3_10, C4_10}, // LB1
  345. {0, C1_10, C2_10, C4_11}, // LB2
  346. {0, C1_11, C2_11, C3_11}, // LB3
  347. {0, C1_12, C2_12, C3_12}, // LB4
  348. {0, C1_13, C2_13, C3_13}, // LB5
  349. {0, C1_14, C2_14, C3_14}, // LB6
  350. {0, C1_15, C2_15, C3_15}, // LB7
  351. {0, C1_16, C2_16, C3_16}, // LB8
  352. {0, C9_9, C8_9, C7_9}, // LB9
  353. {0, C9_10, C8_10, C7_10}, // LB10
  354. {0, C9_11, C8_11, C7_11}, // LB11
  355. {0, C9_12, C8_12, C7_12}, // LB12
  356. {0, C9_13, C8_13, C7_13}, // LB13
  357. {0, C9_14, C8_14, C7_14}, // LB14
  358. {0, C9_15, C8_15, C6_14}, // LB15
  359. {0, C9_16, C7_15, C6_15}, // LB16
  360. {0, C8_16, C7_16, C6_16}, // LB17
  361. {1, C2_1, C3_1, C4_1}, // LC0
  362. {1, C1_1, C3_2, C4_2}, // LC1
  363. {1, C1_2, C2_2, C4_3}, // LC2
  364. {1, C1_3, C2_3, C3_3}, // LC3
  365. {1, C1_4, C2_4, C3_4}, // LC4
  366. {1, C1_5, C2_5, C3_5}, // LC5
  367. {1, C1_6, C2_6, C3_6}, // LC6
  368. {1, C1_7, C2_7, C3_7}, // LC7
  369. {1, C1_8, C2_8, C3_8}, // LC8
  370. {1, C9_1, C8_1, C7_1}, // LC9
  371. {1, C9_2, C8_2, C7_2}, // LC10
  372. {1, C9_3, C8_3, C7_3}, // LC11
  373. {1, C9_4, C8_4, C7_4}, // LC12
  374. {1, C9_5, C8_5, C7_5}, // LC13
  375. {1, C9_6, C8_6, C7_6}, // LC14
  376. {1, C9_7, C8_7, C6_6}, // LC15
  377. {1, C9_8, C7_7, C6_7}, // LC16
  378. {1, C8_8, C7_8, C6_8}, // LC17
  379. {1, C2_9, C3_9, C4_9}, // LD0
  380. {1, C1_9, C3_10, C4_10}, // LD1
  381. {1, C1_10, C2_10, C4_11}, // LD2
  382. {1, C1_11, C2_11, C3_11}, // LD3
  383. {1, C1_12, C2_12, C3_12}, // LD4
  384. {1, C1_13, C2_13, C3_13}, // LD5
  385. {1, C1_14, C2_14, C3_14}, // LD6
  386. {1, C1_15, C2_15, C3_15}, // LD7
  387. {1, C1_16, C2_16, C3_16}, // LD8
  388. {1, C9_9, C8_9, C7_9}, // LD9
  389. {1, C9_10, C8_10, C7_10}, // LD10
  390. {1, C9_11, C8_11, C7_11}, // LD11
  391. {1, C9_12, C8_12, C7_12}, // LD12
  392. {1, C9_13, C8_13, C7_13}, // LD13
  393. {1, C9_14, C8_14, C7_14}, // LD14
  394. {1, C9_15, C8_15, C6_14}, // LD15
  395. {1, C9_16, C7_15, C6_15}, // LD16
  396. {1, C8_16, C7_16, C6_16}, // LD17
  397. };
  398. #endif // !defined(RGB_BACKLIGHT_M6_B)
  399. typedef struct Point {
  400. uint8_t x;
  401. uint8_t y;
  402. } Point;
  403. // index in range 0..71 (LA0..LA17, LB0..LB17, LC0..LC17, LD0..LD17)
  404. // point values in range x=0..224 y=0..64
  405. // origin is center of top-left key (i.e Esc)
  406. #if defined(RGB_BACKLIGHT_ZEAL65)
  407. const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
  408. // LA0..LA17
  409. {120,16}, {104,16}, {88,16}, {72,16}, {56,16}, {40,16}, {24,16}, {4,16}, {4,32},
  410. {128,0}, {112,0}, {96,0}, {80,0}, {64,0}, {48,0}, {32,0}, {16,0}, {0,0},
  411. // LB0..LB17
  412. {144,0}, {160,0}, {176,0}, {192,0}, {216,0}, {224,0}, {240,0}, {240,16}, {240,32},
  413. {136,16}, {152,16}, {168,16}, {184,16}, {200,16}, {220,16}, {240,48}, {240,64}, {224,64},
  414. // LC0..LC17
  415. {96,64}, {100,48}, {84,48}, {68,48}, {52,48}, {36,48}, {255,255}, {48,60}, {28,64},
  416. {108,32}, {92,32}, {76,32}, {60,32}, {44,32}, {28,32}, {20,44}, {10,48}, {4,64},
  417. // LD0..LD17
  418. {124,32}, {140,32}, {156,32}, {172,32}, {188,32}, {214,32}, {180,48}, {202,48}, {224,48},
  419. {116,48}, {132,48}, {148,48}, {164,48}, {255,255}, {144,60}, {164,64}, {188,64}, {208,64}
  420. };
  421. const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
  422. // LA0..LA17
  423. {64,128}, {75,132}, {84,145}, {91,164}, {97,187}, {102,213}, {105,242}, {109,255}, {128,243},
  424. {61,255}, {67,255}, {72,255}, {77,255}, {82,255}, {86,255}, {90,255}, {93,255}, {96,255},
  425. // LB0..LB17
  426. {56,255}, {51,255}, {46,255}, {42,255}, {37,255}, {35,255}, {32,255}, {19,255}, {0,255},
  427. {53,132}, {44,145}, {37,164}, {31,187}, {26,213}, {22,249}, {237,255}, {224,255}, {221,255},
  428. // LC0..LC17
  429. {184,255}, {179,135}, {170,149}, {163,169}, {157,193}, {153,220}, {255,255}, {167,255}, {165,255},
  430. {128,26}, {128,60}, {128,94}, {128,128}, {128,162}, {128,196}, {145,233}, {148,255}, {161,255},
  431. // LD0..LD17
  432. {0,9}, {0,43}, {0,77}, {0,111}, {0,145}, {0,201}, {224,181}, {230,217}, {235,255},
  433. {189,128}, {200,131}, {210,141}, {218,159}, {255,255}, {201,228}, {206,255}, {213,255}, {218,255}
  434. };
  435. #elif defined(RGB_BACKLIGHT_KOYU)
  436. const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
  437. // LA0..LA17
  438. {120,16}, {104,16}, {88,16}, {72,16}, {56,16}, {40,16}, {24,16}, {4,16}, {4,32},
  439. {128,0}, {112,0}, {96,0}, {80,0}, {64,0}, {48,0}, {32,0}, {16,0}, {0,0},
  440. // LB0..LB17
  441. {144,0}, {160,0}, {176,0}, {192,0}, {208,0}, {224,0}, {240,0}, {240,16}, {240,32},
  442. {136,16}, {152,16}, {168,16}, {184,16}, {200,16}, {220,16}, {240,48}, {240,64}, {224,64},
  443. // LC0..LC17
  444. {112,64}, {100,48}, {84,48}, {68,48}, {52,48}, {36,48}, {64,60}, {44,60}, {24,64},
  445. {108,32}, {92,32}, {76,32}, {60,32}, {44,32}, {28,32}, {255,255}, {10,48}, {4,64},
  446. // LD0..LD17
  447. {124,32}, {140,32}, {156,32}, {172,32}, {188,32}, {214,32}, {180,48}, {202,48}, {224,48},
  448. {116,48}, {132,48}, {148,48}, {164,48}, {255,255}, {160,60}, {180,64}, {208,64}, {255,255}
  449. };
  450. const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
  451. // LA0..LA17
  452. {64,128}, {75,132}, {84,145}, {91,164}, {97,187}, {102,213}, {105,242}, {109,255}, {128,243},
  453. {61,255}, {67,255}, {72,255}, {77,255}, {82,255}, {86,255}, {90,255}, {93,255}, {96,255},
  454. // LB0..LB17
  455. {56,255}, {51,255}, {46,255}, {42,255}, {38,255}, {35,255}, {32,255}, {19,255}, {0,255},
  456. {53,132}, {44,145}, {37,164}, {31,187}, {26,213}, {22,249}, {237,255}, {224,255}, {221,255},
  457. // LC0..LC17
  458. {189,255}, {179,135}, {170,149}, {163,169}, {157,193}, {153,220}, {172,252}, {169,255}, {165,255},
  459. {128,26}, {128,60}, {128,94}, {128,128}, {128,162}, {128,196}, {255,255}, {148,255}, {161,255},
  460. // LD0..LD17
  461. {0,9}, {0,43}, {0,77}, {0,111}, {0,145}, {0,201}, {224,181}, {230,217}, {235,255},
  462. {189,128}, {200,131}, {210,141}, {218,159}, {255,255}, {207,238}, {211,255}, {218,255}, {255,255}
  463. };
  464. #elif defined(RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_M60_A)
  465. const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
  466. // LA0..LA17
  467. {120,16}, {104,16}, {88,16}, {72,16}, {56,16}, {40,16}, {24,16}, {4,16}, {4,32},
  468. {128,0}, {112,0}, {96,0}, {80,0}, {64,0}, {48,0}, {32,0}, {16,0}, {0,0},
  469. // LB0..LB17
  470. {144,0}, {160,0}, {176,0}, {192,0}, {216,0}, {224,0}, {255,255}, {255,255}, {255,255},
  471. {136,16}, {152,16}, {168,16}, {184,16}, {200,16}, {220,16}, {255,255}, {255,255}, {255,255},
  472. // LC0..LC17
  473. {102,64}, {100,48}, {84,48}, {68,48}, {52,48}, {36,48}, {60,64}, {43,64}, {23,64},
  474. {108,32}, {92,32}, {76,32}, {60,32}, {44,32}, {28,32}, {20,48}, {2,48}, {3,64},
  475. // LD0..LD17
  476. {124,32}, {140,32}, {156,32}, {172,32}, {188,32}, {214,32}, {180,48}, {210,48}, {224,48},
  477. {116,48}, {132,48}, {148,48}, {164,48}, {144,64}, {161,64}, {181,64}, {201,64}, {221,64}
  478. };
  479. const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
  480. // LA0..LA17
  481. {58,129}, {70,129}, {80,139}, {89,157}, {96,181}, {101,208}, {105,238}, {109,255}, {128,247}, {58,255},
  482. {64,255}, {70,255}, {75,255}, {80,255}, {85,255}, {89,255}, {93,255}, {96,255},
  483. // LB0..LB17
  484. {53,255}, {48,255}, {43,255}, {39,255}, {34,255}, {32,255}, {255,255}, {255,255}, {255,255},
  485. {48,139}, {39,157}, {32,181}, {27,208}, {23,238}, {19,255}, {255,255}, {255,255}, {255,255},
  486. // LC0..LC17
  487. {188,255}, {183,131}, {173,143}, {165,163}, {159,188}, {154,216}, {172,252}, {170,255}, {165,255},
  488. {128,9}, {128,46}, {128,82}, {128,119}, {128,155}, {128,192}, {150,244}, {147,255}, {161,255},
  489. // LD0..LD17
  490. {0,27}, {0,64}, {0,101}, {0,137}, {0,174}, {255,233}, {228,201}, {235,255}, {237,255},
  491. {195,128}, {206,136}, {215,152}, {222,175}, {205,234}, {209,255}, {214,255}, {219,255}, {223,255}
  492. };
  493. #elif defined(RGB_BACKLIGHT_HS60) && defined(HS60_ANSI)
  494. const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
  495. // LA1..LA47
  496. {0,0}, {4,16}, {6,32}, {10,48}, {16,0}, {24,16}, {28,32}, {36,48}, {32,0}, {40,16}, {44,32}, {52,48},
  497. {48,0}, {56,16}, {60,32}, {68,48}, {64,0}, {72,16}, {76,32}, {84,48}, {80,0}, {88,16}, {92,32}, {100,48},
  498. {96,0}, {104,16}, {108,32}, {116,48}, {112,0}, {120,16}, {124,32}, {132,48}, {128,0}, {136,16}, {140,32},
  499. {148,48}, {144,0}, {152,16}, {156,32}, {164,48}, {160,0}, {168,16}, {172,32}, {180,48}, {176,0}, {184, 16}, {188,32},
  500. {255,255},// LA48 does not exist, dummy
  501. // LA49..LA50
  502. {192,0}, {200,16},
  503. {255,255},// LA51 does not exit, dummy
  504. // LA52..LA60
  505. {210,48}, {216,0}, {220,16}, {214,32}, {222,64}, {2,64}, {22,64}, {42,64}, {102,64},
  506. {255,255},// LA61 does not exit, dummy
  507. {162,64}, {182,64}, {202,64}
  508. };
  509. const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
  510. // LA1..LA47
  511. {96,255}, {109,255}, {128,242}, {148,255}, {93,255}, {105,238}, {128,192}, {154,216}, {89,255}, {101,208}, {128,155}, {159,188},
  512. {85,255}, {96,181}, {128,119}, {165,163}, {81,255}, {89,157}, {128,82}, {173,143}, {75,255}, {81,139}, {128,46}, {183,131},
  513. {70,255}, {70,129}, {129,9}, {195,128}, {64,255}, {58,129}, {255,27}, {206,136}, {58,255}, {47,139}, {255,64}, {215,152},
  514. {53,255}, {39,157}, {255,101}, {222,175}, {47,255}, {32,181}, {255,137}, {228,201}, {43,255}, {27,208}, {255, 174},
  515. {255,255},// LA48 does not exist, dummy
  516. // LA49..LA50
  517. {39,255}, {23,238},
  518. {255,255},// LA51 does not exit, dummy
  519. // LA52..LA60
  520. {235,255}, {33,255}, {19,255}, {255,233}, {224,255}, {160,255}, {164,255}, {169,255}, {188,255},
  521. {255,255},// LA61 does not exit, dummy
  522. {209,255}, {215,255}, {220,255}
  523. };
  524. #elif defined(RGB_BACKLIGHT_HS60) && defined(HS60_HHKB)
  525. const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
  526. // LA1..LA60
  527. {0,0}, {4,16}, {6,32}, {10,48}, {16,0}, {24,16}, {28,32}, {36,48}, {32,0}, {40,16}, {44,32}, {52,48},
  528. {48,0}, {56,16}, {60,32}, {68,48}, {64,0}, {72,16}, {76,32}, {84,48}, {80,0}, {88,16}, {92,32}, {100,48},
  529. {96,0}, {104,16}, {108,32}, {116,48}, {112,0}, {120,16}, {124,32}, {132,48}, {128,0}, {136,16}, {140,32},
  530. {148,48}, {144,0}, {152,16}, {156,32}, {164,48}, {160,0}, {168,16}, {172,32}, {180,48}, {176,0}, {184, 16}, {188,32},
  531. {224,0}, {192,0}, {200,16}, {202,48}, {224,48}, {208,0}, {220,16}, {214,32}, {220,64}, {4,64}, {24,64}, {44,64}, {112,64},
  532. {255,255}, {255,255}, // LA61..LA62 does not exit, dummy
  533. // LA63..LA64
  534. {180,64}, {200,64}
  535. };
  536. const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
  537. // LA1..LA60
  538. {96,255}, {109,255}, {128,242}, {148,255}, {93,255}, {105,238}, {128,192}, {154,216}, {89,255}, {101,208}, {128,155}, {159,188},
  539. {85,255}, {96,181}, {128,119}, {165,163}, {81,255}, {89,157}, {128,82}, {173,143}, {75,255}, {81,139}, {128,46}, {183,131},
  540. {70,255}, {70,129}, {129,9}, {195,128}, {64,255}, {58,129}, {255,27}, {206,136}, {58,255}, {47,139}, {255,64}, {215,152},
  541. {53,255}, {39,157}, {255,101}, {222,175}, {47,255}, {32,181}, {255,137}, {228,201}, {43,255}, {27,208}, {255, 174}, {32,255},
  542. {39,255}, {23,238}, {233,242}, {237,255}, {35,255}, {19,255}, {255,233}, {223,255}, {161,255}, {165,255}, {170,255}, {192,255},
  543. {255,255}, {255,255}, // LA61..LA62 does not exit, dummy
  544. // LA63..LA64
  545. {214,255}, {219,255}
  546. };
  547. #elif defined(RGB_BACKLIGHT_HS60) //HS60_ISO
  548. const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
  549. // LA1..LA50
  550. {0,0}, {4,16}, {6,32}, {2,48}, {16,0}, {24,16}, {28,32}, {36,48}, {32,0}, {40,16}, {44,32}, {52,48}, {48,0},
  551. {56,16}, {60,32}, {68,48}, {64,0}, {72,16}, {76,32}, {84,48}, {80,0}, {88,16}, {92,32}, {100,48}, {96,0}, {104,16},
  552. {108,32}, {116,48}, {112,0}, {120,16}, {124,32}, {132,48}, {128,0}, {136,16}, {140,32}, {148,48}, {144,0}, {152,16},
  553. {156,32}, {164,48}, {160,0}, {168,16}, {172,32}, {180,48}, {176,0}, {184, 16}, {188,32}, {20,48}, {192,0}, {200,16},
  554. {255,255},// LA51 does not exit, dummy
  555. // LA52..LA60
  556. {210,48}, {216,0}, {220,16}, {222,24}, {222,64}, {2,64}, {22,64}, {42,64}, {102,64},
  557. {255,255},// LA61 does not exit, dummy
  558. {162,64}, {182,64}, {202,64}
  559. };
  560. const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
  561. // LA1..LA50
  562. {96,255}, {109,255}, {128,242}, {147,255}, {93,255}, {105,238}, {128,192}, {154,216}, {89,255}, {101,208}, {128,155}, {159,188}, {85,255},
  563. {96,181}, {128,119}, {165,163}, {81,255}, {89,157}, {128,82}, {173,143}, {75,255}, {81,139}, {128,46}, {183,131}, {70,255}, {70,129},
  564. {129,9}, {195,128}, {64,255}, {58,129}, {255,27}, {206,136}, {58,255}, {47,139}, {255,64}, {215,152}, {53,255}, {39,157}, {255,101},
  565. {222,175}, {47,255}, {32,181}, {255,137}, {228,201}, {43,255}, {27,208}, {255, 174}, {150,246}, {39,255}, {23,238},
  566. {255,255},// LA51 does not exit, dummy
  567. // LA52..LA60
  568. {235,255}, {33,255}, {19,255}, {10,255}, {224,255}, {160,255}, {164,255}, {169,255}, {188,255},
  569. {255,255},// LA61 does not exit, dummy
  570. {209,255}, {215,255}, {220,255}
  571. };
  572. #elif defined(RGB_BACKLIGHT_NK65)
  573. const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
  574. // LA1..LA60
  575. {0,0}, {4,16}, {6,32}, {10,48}, {16,0}, {24,16}, {28,32}, {36,48}, {32,0}, {40,16}, {44,32}, {52,48},
  576. {48,0}, {56,16}, {60,32}, {68,48}, {64,0}, {72,16}, {76,32}, {84,48}, {80,0}, {88,16}, {92,32}, {100,48},
  577. {96,0}, {104,16}, {108,32}, {116,48}, {112,0}, {120,16}, {124,32}, {132,48}, {128,0}, {136,16}, {140,32},
  578. {148,48}, {144,0}, {152,16}, {156,32}, {164,48}, {160,0}, {168,16}, {172,32}, {180,48}, {176,0}, {184, 16}, {188,32},
  579. {160,64}, {192,0}, {200,16}, {210,48}, {224,48}, {216,0}, {220,16}, {214,32}, {224,64}, {2,64}, {22,64}, {42,64}, {102,64},
  580. {255,255},// LA61 does not exit, dummy
  581. //LA62..LB5
  582. {176,64}, {192,64}, {208,64}, {240,0}, {240,16}, {240,48}, {240,64}, {240,32}
  583. };
  584. const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
  585. // LA1..LA60
  586. {96,255}, {109,255}, {128,242}, {148,255}, {93,255}, {105,238}, {128,192}, {154,216}, {89,255}, {101,208}, {128,155}, {159,188},
  587. {85,255}, {96,181}, {128,119}, {165,163}, {81,255}, {89,157}, {128,82}, {173,143}, {75,255}, {81,139}, {128,46}, {183,131},
  588. {70,255}, {70,129}, {129,9}, {195,128}, {64,255}, {58,129}, {255,27}, {206,136}, {58,255}, {47,139}, {255,64}, {215,152},
  589. {53,255}, {39,157}, {255,101}, {222,175}, {47,255}, {32,181}, {255,137}, {228,201}, {43,255}, {27,208}, {255, 174},
  590. {208,255}, {39,255}, {23,238}, {235,255}, {235,255}, {33,255}, {19,255}, {255,233}, {224,255}, {160,255}, {164,255}, {169,255}, {188,255},
  591. {255,255},// LA61 does not exit, dummy
  592. //LA62..LB5
  593. {221,255}, {225,255}, {229,255}, {22,255}, {12,255}, {244,255}, {234,255}, {255,255},
  594. };
  595. #elif defined(RGB_BACKLIGHT_M6_B)
  596. // M6-B is really simple:
  597. // 0 3 5
  598. // 1 2 4
  599. const Point g_map_led_to_point[BACKLIGHT_LED_COUNT] PROGMEM = {
  600. {0,0}, {0,16}, {16,16}, {16,0}, {32,16}, {32,0}
  601. };
  602. const Point g_map_led_to_point_polar[BACKLIGHT_LED_COUNT] PROGMEM = {
  603. {160,255}, {96,255}, {77,255}, {179,255}, {51,255}, {205,255}
  604. };
  605. #endif
  606. // This may seem counter-intuitive, but it's quite flexible.
  607. // For each LED, get it's position to decide what color to make it.
  608. // This solves the issue of LEDs (and switches) not aligning to a grid,
  609. // or having a large "bitmap" and sampling them.
  610. void map_led_to_point( uint8_t index, Point *point )
  611. {
  612. // Slightly messy way to get Point structs out of progmem.
  613. uint8_t *addr = (uint8_t*)&g_map_led_to_point[index];
  614. point->x = pgm_read_byte(addr);
  615. point->y = pgm_read_byte(addr+1);
  616. #if defined(RGB_BACKLIGHT_M6_B) || defined(RGB_BACKLIGHT_HS60) || defined(RGB_BACKLIGHT_NK65)
  617. return;
  618. #endif
  619. switch (index)
  620. {
  621. case 18+4: // LB4A
  622. if ( g_config.use_split_backspace )
  623. point->x -= 8;
  624. break;
  625. #if defined(RGB_BACKLIGHT_ZEAL60)
  626. case 18+14: // LB14A
  627. if ( g_config.use_iso_enter )
  628. point->y += 8; // extremely pedantic
  629. break;
  630. case 54+5: // LD5A
  631. if ( !g_config.use_iso_enter )
  632. point->x -= 10;
  633. break;
  634. case 36+16: // LC16A
  635. if ( !g_config.use_split_left_shift )
  636. point->x += 8;
  637. break;
  638. #endif
  639. #if defined(RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_M60_A)
  640. case 36+0: // LC0A
  641. if ( g_config.use_7u_spacebar )
  642. point->x += 10;
  643. break;
  644. case 36+6: // LC6A
  645. if ( g_config.use_7u_spacebar )
  646. point->x += 4;
  647. break;
  648. case 54+7: // LD7A
  649. if ( !g_config.use_split_right_shift )
  650. point->x -= 8;
  651. break;
  652. #endif
  653. }
  654. }
  655. void map_led_to_point_polar( uint8_t index, Point *point )
  656. {
  657. // Slightly messy way to get Point structs out of progmem.
  658. uint8_t *addr = (uint8_t*)&g_map_led_to_point_polar[index];
  659. point->x = pgm_read_byte(addr);
  660. point->y = pgm_read_byte(addr+1);
  661. }
  662. //
  663. // Maps switch matrix coordinate (row,col) to LED index
  664. //
  665. #if defined(RGB_BACKLIGHT_ZEAL65)
  666. // Note: Left spacebar stab is at 4,2 (LC7)
  667. // Right spacebar stab is at 4,9 (D14)
  668. //
  669. // A17, A16, A15, A14, A13, A12, A11, A10, A9, B0, B1, B2, B3, B4, B6
  670. // A7, A6, A5, A4, A3, A2, A1, A0, B9, B10, B11, B12, B13, B14, B7
  671. // A8, C14, C13, C12, C11, C10, C9, D0, D1, D2, D3, D4, D5, B5, B8
  672. // C16, C15, C5, C4, C3, C2, C1, D9, D10, D11, D12, D6, D7, D8, B15
  673. // C17, C8, C7, ---, ---, ---, ---, C0, ---, D14, D15, D16, D17, B17, B16
  674. const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
  675. { 0+17, 0+16, 0+15, 0+14, 0+13, 0+12, 0+11, 0+10, 0+9, 18+0, 18+1, 18+2, 18+3, 18+4, 18+6 },
  676. { 0+7, 0+6, 0+5, 0+4, 0+3, 0+2, 0+1, 0+0, 18+9, 18+10, 18+11, 18+12, 18+13, 18+14, 18+7 },
  677. { 0+8, 36+14, 36+13, 36+12, 36+11, 36+10, 36+9, 54+0, 54+1, 54+2, 54+3, 54+4, 54+5, 18+5, 18+8 },
  678. { 36+16, 36+15, 36+5, 36+4, 36+3, 36+2, 36+1, 54+9, 54+10, 54+11, 54+12, 54+6, 54+7, 54+8, 18+15 },
  679. { 36+17, 36+8, 36+7, 255, 255, 255, 255, 36+0, 255, 54+14, 54+15, 54+16, 54+17, 18+17, 18+16 }
  680. };
  681. #elif defined(RGB_BACKLIGHT_KOYU)
  682. // Note: Left spacebar stab is at 4,4 (LC6)
  683. // Right spacebar stab is at 4,10 (D14)
  684. //
  685. // A17, A16, A15, A14, A13, A12, A11, A10, A9, B0, B1, B2, B3, B4, B6
  686. // A7, A6, A5, A4, A3, A2, A1, A0, B9, B10, B11, B12, B13, B14, B7
  687. // A8, C14, C13, C12, C11, C10, C9, D0, D1, D2, D3, D4, D5, B5, B8
  688. // C16, C15, C5, C4, C3, C2, C1, D9, D10, D11, D12, D6, D7, D8, B15
  689. // C17, C8, C7, C6, ---, ---, ---, C0, ---, ---, D14, D15, D16, B17, B16
  690. const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
  691. { 0+17, 0+16, 0+15, 0+14, 0+13, 0+12, 0+11, 0+10, 0+9, 18+0, 18+1, 18+2, 18+3, 18+4, 18+6 },
  692. { 0+7, 0+6, 0+5, 0+4, 0+3, 0+2, 0+1, 0+0, 18+9, 18+10, 18+11, 18+12, 18+13, 18+14, 18+7 },
  693. { 0+8, 36+14, 36+13, 36+12, 36+11, 36+10, 36+9, 54+0, 54+1, 54+2, 54+3, 54+4, 54+5, 18+5, 18+8 },
  694. { 36+16, 36+15, 36+5, 36+4, 36+3, 36+2, 36+1, 54+9, 54+10, 54+11, 54+12, 54+6, 54+7, 54+8, 18+15 },
  695. { 36+17, 36+8, 36+7, 36+6, 255, 255, 255, 36+0, 255, 255, 54+14, 54+15, 54+16, 18+17, 18+16 }
  696. };
  697. #elif defined(RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_M60_A)
  698. // Note: Left spacebar stab is at 4,3 (LC6)
  699. // Right spacebar stab is at 4,9 (LD13) or 4,10 (LD14)
  700. //
  701. // A17, A16, A15, A14, A13, A12, A11, A10, A9, B0, B1, B2, B3, B4,
  702. // A7, A6, A5, A4, A3, A2, A1, A0, B9, B10, B11, B12, B13, B14,
  703. // A8, C14, C13, C12, C11, C10, C9, D0, D1, D2, D3, D4, D5, B5,
  704. // C16, C15, C5, C4, C3, C2, C1, D9, D10, D11, D12, D6, D7, D8,
  705. // C17, C8, C7, C6, ---, ---, ---, C0, ---, D13, D14, D15, D16, D17,
  706. const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
  707. { 0+17, 0+16, 0+15, 0+14, 0+13, 0+12, 0+11, 0+10, 0+9, 18+0, 18+1, 18+2, 18+3, 18+4 },
  708. { 0+7, 0+6, 0+5, 0+4, 0+3, 0+2, 0+1, 0+0, 18+9, 18+10, 18+11, 18+12, 18+13, 18+14 },
  709. { 0+8, 36+14, 36+13, 36+12, 36+11, 36+10, 36+9, 54+0, 54+1, 54+2, 54+3, 54+4, 54+5, 18+5 },
  710. { 36+16, 36+15, 36+5, 36+4, 36+3, 36+2, 36+1, 54+9, 54+10, 54+11, 54+12, 54+6, 54+7, 54+8 },
  711. { 36+17, 36+8, 36+7, 36+6, 255, 255, 255, 36+0, 255, 54+13, 54+14, 54+15, 54+16, 54+17 }
  712. };
  713. #elif defined(RGB_BACKLIGHT_HS60) && defined(HS60_ANSI)
  714. //
  715. // LA1, LA5, LA9, LA13, LA17, LA21, LA25, LA29, LA33, LA37, LA41, LA45, LA49, LA53,
  716. // LA2, LA6, LA10, LA14, LA18, LA22, LA26, LA30, LA34, LA38, LA42, LA46, LA50, ---,
  717. // LA3, LA7, LA11, LA15, LA19, LA23, LA27, LA31, LA35, LA39, LA43, LA47, LA54, LA55,
  718. // LA4, ---, LA8, LA12, LA16, LA20, LA24, LA28, LA32, LA36, LA40, LA44, ---, LA52,
  719. // LA57, LA58, LA59, ---, ---, ---, LA60, ---, ---, ---, LA62, LA63, LA64, LA56
  720. const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
  721. { 1-1, 5-1, 9-1, 13-1, 17-1, 21-1, 25-1, 29-1, 33-1, 37-1, 41-1, 45-1, 49-1, 53-1 },
  722. { 2-1, 6-1, 10-1, 14-1, 18-1, 22-1, 26-1, 30-1, 34-1, 38-1, 42-1, 46-1, 50-1, 255 },
  723. { 3-1, 7-1, 11-1, 15-1, 19-1, 23-1, 27-1, 31-1, 35-1, 39-1, 43-1, 47-1, 54-1, 55-1 },
  724. { 4-1, 255, 8-1, 12-1, 16-1, 20-1, 24-1, 28-1, 32-1, 36-1, 40-1, 44-1, 255, 52-1 },
  725. { 57-1, 58-1, 59-1, 255, 255, 255, 60-1, 255, 255, 255, 62-1, 63-1, 64-1, 56-1 }
  726. };
  727. #elif defined(RGB_BACKLIGHT_HS60) && defined(HS60_HHKB)
  728. //
  729. // LA1, LA5, LA9, LA13, LA17, LA21, LA25, LA29, LA33, LA37, LA41, LA45, LA49, LA53,
  730. // LA2, LA6, LA10, LA14, LA18, LA22, LA26, LA30, LA34, LA38, LA42, LA46, LA50, LA48,
  731. // LA3, LA7, LA11, LA15, LA19, LA23, LA27, LA31, LA35, LA39, LA43, LA47, LA54, LA55,
  732. // LA4, ---, LA8, LA12, LA16, LA20, LA24, LA28, LA32, LA36, LA40, LA44, LA51, LA52,
  733. // LA57, LA58, LA59, ---, ---, ---, LA60, ---, ---, ---, ---, LA63, LA64, LA56
  734. const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
  735. { 1-1, 5-1, 9-1, 13-1, 17-1, 21-1, 25-1, 29-1, 33-1, 37-1, 41-1, 45-1, 49-1, 53-1 },
  736. { 2-1, 6-1, 10-1, 14-1, 18-1, 22-1, 26-1, 30-1, 34-1, 38-1, 42-1, 46-1, 50-1, 48-1 },
  737. { 3-1, 7-1, 11-1, 15-1, 19-1, 23-1, 27-1, 31-1, 35-1, 39-1, 43-1, 47-1, 54-1, 55-1 },
  738. { 4-1, 255, 8-1, 12-1, 16-1, 20-1, 24-1, 28-1, 32-1, 36-1, 40-1, 44-1, 51-1, 52-1 },
  739. { 57-1, 58-1, 59-1, 255, 255, 255, 60-1, 255, 255, 255, 255, 63-1, 64-1, 56-1 }
  740. };
  741. #elif defined(RGB_BACKLIGHT_HS60) //HS60_ISO
  742. //
  743. // LA1, LA5, LA9, LA13, LA17, LA21, LA25, LA29, LA33, LA37, LA41, LA45, LA49, LA53,
  744. // LA2, LA6, LA10, LA14, LA18, LA22, LA26, LA30, LA34, LA38, LA42, LA46, LA50, ---,
  745. // LA3, LA7, LA11, LA15, LA19, LA23, LA27, LA31, LA35, LA39, LA43, LA47, LA54, LA55,
  746. // LA4, LA48, LA8, LA12, LA16, LA20, LA24, LA28, LA32, LA36, LA40, LA44, ---, LA52,
  747. // LA57, LA58, LA59, ---, ---, ---, LA60, ---, ---, ---, LA62, LA63, LA64, LA56
  748. const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
  749. { 1-1, 5-1, 9-1, 13-1, 17-1, 21-1, 25-1, 29-1, 33-1, 37-1, 41-1, 45-1, 49-1, 53-1 },
  750. { 2-1, 6-1, 10-1, 14-1, 18-1, 22-1, 26-1, 30-1, 34-1, 38-1, 42-1, 46-1, 50-1, 255 },
  751. { 3-1, 7-1, 11-1, 15-1, 19-1, 23-1, 27-1, 31-1, 35-1, 39-1, 43-1, 47-1, 54-1, 55-1 },
  752. { 4-1, 48-1, 8-1, 12-1, 16-1, 20-1, 24-1, 28-1, 32-1, 36-1, 40-1, 44-1, 255, 52-1 },
  753. { 57-1, 58-1, 59-1, 255, 255, 255, 60-1, 255, 255, 255, 62-1, 63-1, 64-1, 56-1 }
  754. };
  755. #elif defined(RGB_BACKLIGHT_NK65)
  756. //
  757. // LA1, LA5, LA9, LA13, LA17, LA21, LA25, LA29, LA33, LA37, LA41, LA45, LA49, LA53, LB1,
  758. // LA2, LA6, LA10, LA14, LA18, LA22, LA26, LA30, LA34, LA38, LA42, LA46, LA50, ---, LB2,
  759. // LA3, LA7, LA11, LA15, LA19, LA23, LA27, LA31, LA35, LA39, LA43, LA47, LA54, LA55, LB5,
  760. // LA4, ---, LA8, LA12, LA16, LA20, LA24, LA28, LA32, LA36, LA40, LA44, LA51, LA52, LB3,
  761. // LA57, LA58, LA59, ---, ---, ---, LA60, ---, ---, LA48, LA62, LA63, LA64, LA56, LB4
  762. const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
  763. { 1-1, 5-1, 9-1, 13-1, 17-1, 21-1, 25-1, 29-1, 33-1, 37-1, 41-1, 45-1, 49-1, 53-1, 1+64-1 },
  764. { 2-1, 6-1, 10-1, 14-1, 18-1, 22-1, 26-1, 30-1, 34-1, 38-1, 42-1, 46-1, 50-1, 255, 2+64-1 },
  765. { 3-1, 7-1, 11-1, 15-1, 19-1, 23-1, 27-1, 31-1, 35-1, 39-1, 43-1, 47-1, 54-1, 55-1, 5+64-1 },
  766. { 4-1, 255, 8-1, 12-1, 16-1, 20-1, 24-1, 28-1, 32-1, 36-1, 40-1, 44-1, 51-1, 52-1, 3+64-1 },
  767. { 57-1, 58-1, 59-1, 255, 255, 255, 60-1, 255, 255, 48-1, 62-1, 63-1, 64-1, 56-1, 4+64-1 }
  768. };
  769. #elif defined(RGB_BACKLIGHT_M6_B)
  770. // M6-B is really simple:
  771. // 0 3 5
  772. // 1 2 4
  773. const uint8_t g_map_row_column_to_led[MATRIX_ROWS][MATRIX_COLS] PROGMEM = {
  774. { 0, 3, 5, 1, 2, 4 }
  775. };
  776. #endif
  777. void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led )
  778. {
  779. *led = 255;
  780. if ( row < MATRIX_ROWS && column < MATRIX_COLS )
  781. {
  782. *led = pgm_read_byte(&g_map_row_column_to_led[row][column]);
  783. }
  784. }
  785. void backlight_update_pwm_buffers(void)
  786. {
  787. #if defined(RGB_BACKLIGHT_M6_B)
  788. IS31FL3218_update_pwm_buffers();
  789. #elif defined(RGB_BACKLIGHT_HS60)
  790. IS31FL3733_update_pwm_buffers( ISSI_ADDR_1, 0 );
  791. IS31FL3733_update_led_control_registers( ISSI_ADDR_1, 0 );
  792. #elif defined(RGB_BACKLIGHT_NK65)
  793. IS31FL3733_update_pwm_buffers( ISSI_ADDR_1, 0 );
  794. IS31FL3733_update_pwm_buffers( ISSI_ADDR_2, 1 );
  795. IS31FL3733_update_led_control_registers( ISSI_ADDR_1, 0 );
  796. IS31FL3733_update_led_control_registers( ISSI_ADDR_2, 1 );
  797. #else
  798. IS31FL3731_update_pwm_buffers( ISSI_ADDR_1, ISSI_ADDR_2 );
  799. IS31FL3731_update_led_control_registers( ISSI_ADDR_1, ISSI_ADDR_2 );
  800. #endif
  801. }
  802. void backlight_set_color( int index, uint8_t red, uint8_t green, uint8_t blue )
  803. {
  804. #if defined(RGB_BACKLIGHT_M6_B)
  805. IS31FL3218_set_color( index, red, green, blue );
  806. #elif defined(RGB_BACKLIGHT_HS60) || defined(RGB_BACKLIGHT_NK65)
  807. IS31FL3733_set_color( index, red, green, blue );
  808. #else
  809. IS31FL3731_set_color( index, red, green, blue );
  810. #endif
  811. }
  812. void backlight_set_color_all( uint8_t red, uint8_t green, uint8_t blue )
  813. {
  814. #if defined(RGB_BACKLIGHT_M6_B)
  815. IS31FL3218_set_color_all( red, green, blue );
  816. #elif defined(RGB_BACKLIGHT_HS60) || defined(RGB_BACKLIGHT_NK65)
  817. IS31FL3733_set_color_all( red, green, blue );
  818. #else
  819. IS31FL3731_set_color_all( red, green, blue );
  820. #endif
  821. }
  822. void backlight_set_key_hit(uint8_t row, uint8_t column)
  823. {
  824. uint8_t led;
  825. map_row_column_to_led(row,column,&led);
  826. g_key_hit[led] = 0;
  827. g_any_key_hit = 0;
  828. }
  829. #if !defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
  830. // This is (F_CPU/1024) / 20 Hz
  831. // = 15625 Hz / 20 Hz
  832. // = 781
  833. #define TIMER3_TOP 781
  834. void backlight_timer_init(void)
  835. {
  836. static uint8_t backlight_timer_is_init = 0;
  837. if ( backlight_timer_is_init )
  838. {
  839. return;
  840. }
  841. backlight_timer_is_init = 1;
  842. // Timer 3 setup
  843. TCCR3B = _BV(WGM32) | // CTC mode OCR3A as TOP
  844. _BV(CS32) | _BV(CS30); // prescale by /1024
  845. // Set TOP value
  846. uint8_t sreg = SREG;
  847. cli();
  848. OCR3AH = (TIMER3_TOP >> 8) & 0xff;
  849. OCR3AL = TIMER3_TOP & 0xff;
  850. SREG = sreg;
  851. }
  852. void backlight_timer_enable(void)
  853. {
  854. TIMSK3 |= _BV(OCIE3A);
  855. }
  856. void backlight_timer_disable(void)
  857. {
  858. TIMSK3 &= ~_BV(OCIE3A);
  859. }
  860. #else //STM32, use GPT with TIM4. Enable in halconf.h
  861. static void gpt_backlight_timer_task(GPTDriver *gptp);
  862. // Timer setup at 200Khz, callback at 10k ticks = 20Hz
  863. static GPTConfig gpt4cfg1 = {
  864. .frequency = 200000U,
  865. .callback = gpt_backlight_timer_task
  866. };
  867. void backlight_timer_init(void)
  868. {
  869. gptStart(&GPTD4, &gpt4cfg1);
  870. }
  871. void backlight_timer_enable(void)
  872. {
  873. gptStartContinuous(&GPTD4, 10000);
  874. }
  875. void backlight_timer_disable(void)
  876. {
  877. gptStopTimer(&GPTD4);
  878. }
  879. #endif //!defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
  880. void backlight_set_suspend_state(bool state)
  881. {
  882. g_suspend_state = state;
  883. }
  884. void backlight_set_indicator_state(uint8_t state)
  885. {
  886. g_indicator_state = state;
  887. }
  888. void backlight_effect_rgb_test(void)
  889. {
  890. // Mask out bits 4 and 5
  891. // This 2-bit value will stay the same for 16 ticks.
  892. switch ( (g_tick & 0x30) >> 4 )
  893. {
  894. case 0:
  895. {
  896. backlight_set_color_all( 255, 0, 0 );
  897. break;
  898. }
  899. case 1:
  900. {
  901. backlight_set_color_all( 0, 255, 0 );
  902. break;
  903. }
  904. case 2:
  905. {
  906. backlight_set_color_all( 0, 0, 255 );
  907. break;
  908. }
  909. case 3:
  910. {
  911. backlight_set_color_all( 255, 255, 255 );
  912. break;
  913. }
  914. }
  915. }
  916. #if defined(RGB_DEBUGGING_ONLY)
  917. // This tests the LEDs
  918. // Note that it will change the LED control registers
  919. // in the LED drivers, and leave them in an invalid
  920. // state for other backlight effects.
  921. // ONLY USE THIS FOR TESTING LEDS!
  922. void backlight_effect_single_LED_test(void)
  923. {
  924. static uint8_t color = 0; // 0,1,2 for R,G,B
  925. static uint8_t row = 0;
  926. static uint8_t column = 0;
  927. static uint8_t tick = 0;
  928. tick++;
  929. if ( tick > 2 )
  930. {
  931. tick = 0;
  932. column++;
  933. }
  934. if ( column > 14 )
  935. {
  936. column = 0;
  937. row++;
  938. }
  939. if ( row > 4 )
  940. {
  941. row = 0;
  942. color++;
  943. }
  944. if ( color > 2 )
  945. {
  946. color = 0;
  947. }
  948. uint8_t led;
  949. map_row_column_to_led( row, column, &led );
  950. backlight_set_color_all( 255, 255, 255 );
  951. backlight_test_led( led, color==0, color==1, color==2 );
  952. }
  953. #endif // defined(RGB_DEBUGGING_ONLY)
  954. // All LEDs off
  955. void backlight_effect_all_off(void)
  956. {
  957. backlight_set_color_all( 0, 0, 0 );
  958. }
  959. // Solid color
  960. void backlight_effect_solid_color(void)
  961. {
  962. HSV hsv = { .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness };
  963. RGB rgb = hsv_to_rgb( hsv );
  964. backlight_set_color_all( rgb.r, rgb.g, rgb.b );
  965. }
  966. // alphas = color1, mods = color2
  967. void backlight_effect_alphas_mods(void)
  968. {
  969. RGB rgb1 = hsv_to_rgb( (HSV){ .h = g_config.color_1.h, .s = g_config.color_1.s, .v = g_config.brightness } );
  970. RGB rgb2 = hsv_to_rgb( (HSV){ .h = g_config.color_2.h, .s = g_config.color_2.s, .v = g_config.brightness } );
  971. for ( int row = 0; row < MATRIX_ROWS; row++ )
  972. {
  973. for ( int column = 0; column < MATRIX_COLS; column++ )
  974. {
  975. uint8_t index;
  976. map_row_column_to_led( row, column, &index );
  977. if ( index < BACKLIGHT_LED_COUNT )
  978. {
  979. if ( ( g_config.alphas_mods[row] & (1<<column) ) == 0 )
  980. {
  981. backlight_set_color( index, rgb1.r, rgb1.g, rgb1.b );
  982. }
  983. else
  984. {
  985. backlight_set_color( index, rgb2.r, rgb2.g, rgb2.b );
  986. }
  987. }
  988. }
  989. }
  990. }
  991. void backlight_effect_gradient_up_down(void)
  992. {
  993. int16_t h1 = g_config.color_1.h;
  994. int16_t h2 = g_config.color_2.h;
  995. int16_t deltaH = h2 - h1;
  996. // Take the shortest path between hues
  997. if ( deltaH > 127 )
  998. {
  999. deltaH -= 256;
  1000. }
  1001. else if ( deltaH < -127 )
  1002. {
  1003. deltaH += 256;
  1004. }
  1005. // Divide delta by 4, this gives the delta per row
  1006. deltaH /= 4;
  1007. int16_t s1 = g_config.color_1.s;
  1008. int16_t s2 = g_config.color_2.s;
  1009. int16_t deltaS = ( s2 - s1 ) / 4;
  1010. HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
  1011. RGB rgb;
  1012. Point point;
  1013. for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
  1014. {
  1015. map_led_to_point( i, &point );
  1016. // The y range will be 0..64, map this to 0..4
  1017. uint8_t y = (point.y>>4);
  1018. // Relies on hue being 8-bit and wrapping
  1019. hsv.h = g_config.color_1.h + ( deltaH * y );
  1020. hsv.s = g_config.color_1.s + ( deltaS * y );
  1021. rgb = hsv_to_rgb( hsv );
  1022. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  1023. }
  1024. }
  1025. void backlight_effect_raindrops(bool initialize)
  1026. {
  1027. int16_t h1 = g_config.color_1.h;
  1028. int16_t h2 = g_config.color_2.h;
  1029. int16_t deltaH = h2 - h1;
  1030. deltaH /= 4;
  1031. // Take the shortest path between hues
  1032. if ( deltaH > 127 )
  1033. {
  1034. deltaH -= 256;
  1035. }
  1036. else if ( deltaH < -127 )
  1037. {
  1038. deltaH += 256;
  1039. }
  1040. int16_t s1 = g_config.color_1.s;
  1041. int16_t s2 = g_config.color_2.s;
  1042. int16_t deltaS = ( s2 - s1 ) / 4;
  1043. HSV hsv;
  1044. RGB rgb;
  1045. // Change one LED every tick
  1046. uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % BACKLIGHT_LED_COUNT : 255;
  1047. for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
  1048. {
  1049. // If initialize, all get set to random colors
  1050. // If not, all but one will stay the same as before.
  1051. if ( initialize || i == led_to_change )
  1052. {
  1053. hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
  1054. hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
  1055. // Override brightness with global brightness control
  1056. hsv.v = g_config.brightness;;
  1057. rgb = hsv_to_rgb( hsv );
  1058. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  1059. }
  1060. }
  1061. }
  1062. void backlight_effect_cycle_all(void)
  1063. {
  1064. uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
  1065. // Relies on hue being 8-bit and wrapping
  1066. for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
  1067. {
  1068. uint16_t offset2 = g_key_hit[i]<<2;
  1069. #if !defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
  1070. // stabilizer LEDs use spacebar hits
  1071. if ( i == 36+6 || i == 54+13 || // LC6, LD13
  1072. ( g_config.use_7u_spacebar && i == 54+14 ) ) // LD14
  1073. {
  1074. offset2 = g_key_hit[36+0]<<2;
  1075. }
  1076. #endif
  1077. offset2 = (offset2<=63) ? (63-offset2) : 0;
  1078. HSV hsv = { .h = offset+offset2, .s = 255, .v = g_config.brightness };
  1079. RGB rgb = hsv_to_rgb( hsv );
  1080. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  1081. }
  1082. }
  1083. void backlight_effect_cycle_left_right(void)
  1084. {
  1085. uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
  1086. HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
  1087. RGB rgb;
  1088. Point point;
  1089. for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
  1090. {
  1091. uint16_t offset2 = g_key_hit[i]<<2;
  1092. #if !defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
  1093. // stabilizer LEDs use spacebar hits
  1094. if ( i == 36+6 || i == 54+13 || // LC6, LD13
  1095. ( g_config.use_7u_spacebar && i == 54+14 ) ) // LD14
  1096. {
  1097. offset2 = g_key_hit[36+0]<<2;
  1098. }
  1099. #endif
  1100. offset2 = (offset2<=63) ? (63-offset2) : 0;
  1101. map_led_to_point( i, &point );
  1102. // Relies on hue being 8-bit and wrapping
  1103. hsv.h = point.x + offset + offset2;
  1104. rgb = hsv_to_rgb( hsv );
  1105. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  1106. }
  1107. }
  1108. void backlight_effect_cycle_up_down(void)
  1109. {
  1110. uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
  1111. HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
  1112. RGB rgb;
  1113. Point point;
  1114. for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
  1115. {
  1116. uint16_t offset2 = g_key_hit[i]<<2;
  1117. #if !defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
  1118. // stabilizer LEDs use spacebar hits
  1119. if ( i == 36+6 || i == 54+13 || // LC6, LD13
  1120. ( g_config.use_7u_spacebar && i == 54+14 ) ) // LD14
  1121. {
  1122. offset2 = g_key_hit[36+0]<<2;
  1123. }
  1124. #endif
  1125. offset2 = (offset2<=63) ? (63-offset2) : 0;
  1126. map_led_to_point( i, &point );
  1127. // Relies on hue being 8-bit and wrapping
  1128. hsv.h = point.y + offset + offset2;
  1129. rgb = hsv_to_rgb( hsv );
  1130. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  1131. }
  1132. }
  1133. void backlight_effect_jellybean_raindrops( bool initialize )
  1134. {
  1135. HSV hsv;
  1136. RGB rgb;
  1137. // Change one LED every tick
  1138. uint8_t led_to_change = ( g_tick & 0x000 ) == 0 ? rand() % BACKLIGHT_LED_COUNT : 255;
  1139. for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
  1140. {
  1141. // If initialize, all get set to random colors
  1142. // If not, all but one will stay the same as before.
  1143. if ( initialize || i == led_to_change )
  1144. {
  1145. hsv.h = rand() & 0xFF;
  1146. hsv.s = rand() & 0xFF;
  1147. // Override brightness with global brightness control
  1148. hsv.v = g_config.brightness;;
  1149. rgb = hsv_to_rgb( hsv );
  1150. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  1151. }
  1152. }
  1153. }
  1154. void backlight_effect_cycle_radial1(void)
  1155. {
  1156. uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
  1157. HSV hsv = { .h = 0, .s = 255, .v = g_config.brightness };
  1158. RGB rgb;
  1159. Point point;
  1160. for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
  1161. {
  1162. map_led_to_point_polar( i, &point );
  1163. // Relies on hue being 8-bit and wrapping
  1164. hsv.h = point.x + offset;
  1165. hsv.s = point.y;
  1166. rgb = hsv_to_rgb( hsv );
  1167. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  1168. }
  1169. }
  1170. void backlight_effect_cycle_radial2(void)
  1171. {
  1172. uint8_t offset = ( g_tick << g_config.effect_speed ) & 0xFF;
  1173. HSV hsv = { .h = 0, .s = g_config.color_1.s, .v = g_config.brightness };
  1174. RGB rgb;
  1175. Point point;
  1176. for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
  1177. {
  1178. map_led_to_point_polar( i, &point );
  1179. uint8_t offset2 = offset + point.x;
  1180. if ( offset2 & 0x80 )
  1181. {
  1182. offset2 = ~offset2;
  1183. }
  1184. offset2 = offset2 >> 2;
  1185. hsv.h = g_config.color_1.h + offset2;
  1186. hsv.s = 127 + ( point.y >> 1 );
  1187. rgb = hsv_to_rgb( hsv );
  1188. backlight_set_color( i, rgb.r, rgb.g, rgb.b );
  1189. }
  1190. }
  1191. #if defined(RGB_BACKLIGHT_M6_B)
  1192. void backlight_effect_custom_colors(void)
  1193. {
  1194. RGB rgb;
  1195. for ( uint8_t i = 0; i < 6; i++ )
  1196. {
  1197. HSV hsv = { .h = g_config.custom_color[i].h, .s = g_config.custom_color[i].s, .v = g_config.brightness };
  1198. rgb = hsv_to_rgb( hsv );
  1199. uint8_t led;
  1200. map_row_column_to_led( 0, i, &led );
  1201. backlight_set_color( led, rgb.r, rgb.g, rgb.b );
  1202. }
  1203. }
  1204. #endif
  1205. void backlight_effect_indicators_set_colors( uint8_t index, HS color )
  1206. {
  1207. HSV hsv = { .h = color.h, .s = color.s, .v = g_config.brightness };
  1208. RGB rgb = hsv_to_rgb( hsv );
  1209. if ( index == 254 )
  1210. {
  1211. backlight_set_color_all( rgb.r, rgb.g, rgb.b );
  1212. }
  1213. else
  1214. {
  1215. backlight_set_color( index, rgb.r, rgb.g, rgb.b );
  1216. // If the spacebar LED is the indicator,
  1217. // do the same for the spacebar stabilizers
  1218. if ( index == 36+0 ) // LC0
  1219. {
  1220. #if defined(RGB_BACKLIGHT_ZEAL65)
  1221. backlight_set_color( 36+7, rgb.r, rgb.g, rgb.b ); // LC7
  1222. backlight_set_color( 54+14, rgb.r, rgb.g, rgb.b ); // LD14
  1223. #elif defined(RGB_BACKLIGHT_KOYU)
  1224. backlight_set_color( 36+6, rgb.r, rgb.g, rgb.b ); // LC6
  1225. backlight_set_color( 54+14, rgb.r, rgb.g, rgb.b ); // LD14
  1226. #elif defined(RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_M60_A)
  1227. backlight_set_color( 36+6, rgb.r, rgb.g, rgb.b ); // LC6
  1228. backlight_set_color( 54+13, rgb.r, rgb.g, rgb.b ); // LD13
  1229. if ( g_config.use_7u_spacebar )
  1230. {
  1231. backlight_set_color( 54+14, rgb.r, rgb.g, rgb.b ); // LD14
  1232. }
  1233. #endif
  1234. }
  1235. }
  1236. }
  1237. // This runs after another backlight effect and replaces
  1238. // colors already set
  1239. void backlight_effect_indicators(void)
  1240. {
  1241. if ( g_config.caps_lock_indicator.index != 255 &&
  1242. ( g_indicator_state & (1<<USB_LED_CAPS_LOCK) ) )
  1243. {
  1244. backlight_effect_indicators_set_colors( g_config.caps_lock_indicator.index, g_config.caps_lock_indicator.color );
  1245. }
  1246. #if defined(RGB_BACKLIGHT_NK65)
  1247. if ( g_indicator_state & (1<<USB_LED_CAPS_LOCK) )
  1248. {
  1249. IS31FL3733_set_color( 7+64-1, 0, 255, 0 );
  1250. } else {
  1251. IS31FL3733_set_color( 7+64-1, 0, 0, 0 );
  1252. }
  1253. if ( g_indicator_state & (1<<USB_LED_SCROLL_LOCK) )
  1254. {
  1255. IS31FL3733_set_color( 6+64-1, 255, 0, 255 );
  1256. } else {
  1257. IS31FL3733_set_color( 6+64-1, 0, 0, 0 );
  1258. }
  1259. if ( g_indicator_state & (1<<USB_LED_NUM_LOCK) )
  1260. {
  1261. IS31FL3733_set_color( 6+64-1, 0, 255, 0 );
  1262. } else {
  1263. IS31FL3733_set_color( 6+64-1, 0, 0, 0 );
  1264. }
  1265. #endif
  1266. // This if/else if structure allows higher layers to
  1267. // override lower ones. If we set layer 3's indicator
  1268. // to none, then it will NOT show layer 2 or layer 1
  1269. // indicators, even if those layers are on via the
  1270. // MO13/MO23 Fn combo magic.
  1271. //
  1272. // Basically we want to handle the case where layer 3 is
  1273. // still the backlight configuration layer and we don't
  1274. // want "all LEDs" indicators hiding the backlight effect,
  1275. // but still allow end users to do whatever they want.
  1276. if ( IS_LAYER_ON(3) )
  1277. {
  1278. if ( g_config.layer_3_indicator.index != 255 )
  1279. {
  1280. backlight_effect_indicators_set_colors( g_config.layer_3_indicator.index, g_config.layer_3_indicator.color );
  1281. }
  1282. }
  1283. else if ( IS_LAYER_ON(2) )
  1284. {
  1285. if ( g_config.layer_2_indicator.index != 255 )
  1286. {
  1287. backlight_effect_indicators_set_colors( g_config.layer_2_indicator.index, g_config.layer_2_indicator.color );
  1288. }
  1289. }
  1290. else if ( IS_LAYER_ON(1) )
  1291. {
  1292. if ( g_config.layer_1_indicator.index != 255 )
  1293. {
  1294. backlight_effect_indicators_set_colors( g_config.layer_1_indicator.index, g_config.layer_1_indicator.color );
  1295. }
  1296. }
  1297. }
  1298. #if !defined(RGB_BACKLIGHT_HS60) && !defined(RGB_BACKLIGHT_NK65)
  1299. ISR(TIMER3_COMPA_vect)
  1300. #else //STM32 interrupt
  1301. static void gpt_backlight_timer_task(GPTDriver *gptp)
  1302. #endif
  1303. {
  1304. // delay 1 second before driving LEDs or doing anything else
  1305. static uint8_t startup_tick = 0;
  1306. if ( startup_tick < 20 )
  1307. {
  1308. startup_tick++;
  1309. return;
  1310. }
  1311. g_tick++;
  1312. if ( g_any_key_hit < 0xFFFFFFFF )
  1313. {
  1314. g_any_key_hit++;
  1315. }
  1316. for ( int led = 0; led < BACKLIGHT_LED_COUNT; led++ )
  1317. {
  1318. if ( g_key_hit[led] < 255 )
  1319. {
  1320. g_key_hit[led]++;
  1321. }
  1322. }
  1323. // Factory default magic value
  1324. if ( g_config.effect == 255 )
  1325. {
  1326. backlight_effect_rgb_test();
  1327. return;
  1328. }
  1329. // Ideally we would also stop sending zeros to the LED driver PWM buffers
  1330. // while suspended and just do a software shutdown. This is a cheap hack for now.
  1331. bool suspend_backlight = ((g_suspend_state && g_config.disable_when_usb_suspended) ||
  1332. (g_config.disable_after_timeout > 0 && g_any_key_hit > g_config.disable_after_timeout * 60 * 20));
  1333. uint8_t effect = suspend_backlight ? 0 : g_config.effect;
  1334. // Keep track of the effect used last time,
  1335. // detect change in effect, so each effect can
  1336. // have an optional initialization.
  1337. static uint8_t effect_last = 255;
  1338. bool initialize = effect != effect_last;
  1339. effect_last = effect;
  1340. // this gets ticked at 20 Hz.
  1341. // each effect can opt to do calculations
  1342. // and/or request PWM buffer updates.
  1343. switch ( effect )
  1344. {
  1345. case 0:
  1346. backlight_effect_all_off();
  1347. break;
  1348. case 1:
  1349. backlight_effect_solid_color();
  1350. break;
  1351. case 2:
  1352. #if defined(RGB_BACKLIGHT_M6_B)
  1353. backlight_effect_custom_colors();
  1354. #else
  1355. backlight_effect_alphas_mods();
  1356. #endif
  1357. break;
  1358. case 3:
  1359. backlight_effect_gradient_up_down();
  1360. break;
  1361. case 4:
  1362. backlight_effect_raindrops( initialize );
  1363. break;
  1364. case 5:
  1365. backlight_effect_cycle_all();
  1366. break;
  1367. case 6:
  1368. backlight_effect_cycle_left_right();
  1369. break;
  1370. case 7:
  1371. backlight_effect_cycle_up_down();
  1372. break;
  1373. case 8:
  1374. backlight_effect_jellybean_raindrops( initialize );
  1375. break;
  1376. case 9:
  1377. backlight_effect_cycle_radial1();
  1378. break;
  1379. case 10:
  1380. backlight_effect_cycle_radial2();
  1381. break;
  1382. default:
  1383. backlight_effect_all_off();
  1384. break;
  1385. }
  1386. if ( ! suspend_backlight )
  1387. {
  1388. #if !defined(RGB_BACKLIGHT_M6_B)
  1389. backlight_effect_indicators();
  1390. #endif
  1391. }
  1392. }
  1393. void backlight_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
  1394. {
  1395. if ( row >= MATRIX_ROWS )
  1396. {
  1397. // Special value, 255=none, 254=all
  1398. *index = row;
  1399. }
  1400. else
  1401. {
  1402. map_row_column_to_led( row, column, index );
  1403. }
  1404. }
  1405. void backlight_get_indicator_row_col( uint8_t index, uint8_t *row, uint8_t *column )
  1406. {
  1407. if ( index == 255 || index == 254 )
  1408. {
  1409. // Special value, 255=none, 254=all
  1410. *row = index;
  1411. *column = 0;
  1412. return;
  1413. }
  1414. for ( uint8_t r = 0; r < MATRIX_ROWS; r++ )
  1415. {
  1416. for ( uint8_t c = 0; c < MATRIX_COLS; c++ )
  1417. {
  1418. uint8_t i = 255;
  1419. map_row_column_to_led( r, c, &i );
  1420. if ( i == index )
  1421. {
  1422. *row = r;
  1423. *column = c;
  1424. return;
  1425. }
  1426. }
  1427. }
  1428. }
  1429. // Some helpers for setting/getting HSV
  1430. void _set_color( HS *color, uint8_t *data )
  1431. {
  1432. color->h = data[0];
  1433. color->s = data[1];
  1434. }
  1435. void _get_color( HS *color, uint8_t *data )
  1436. {
  1437. data[0] = color->h;
  1438. data[1] = color->s;
  1439. }
  1440. void backlight_config_set_value( uint8_t *data )
  1441. {
  1442. bool reinitialize = false;
  1443. uint8_t *value_id = &(data[0]);
  1444. uint8_t *value_data = &(data[1]);
  1445. switch ( *value_id )
  1446. {
  1447. #if defined (RGB_BACKLIGHT_ZEAL60) || defined(RGB_BACKLIGHT_ZEAL65)
  1448. case id_use_split_backspace:
  1449. {
  1450. g_config.use_split_backspace = (bool)*value_data;
  1451. reinitialize = true;
  1452. break;
  1453. }
  1454. #endif
  1455. #if defined (RGB_BACKLIGHT_ZEAL60)
  1456. case id_use_split_left_shift:
  1457. {
  1458. g_config.use_split_left_shift = (bool)*value_data;
  1459. reinitialize = true;
  1460. break;
  1461. }
  1462. case id_use_split_right_shift:
  1463. {
  1464. g_config.use_split_right_shift = (bool)*value_data;
  1465. reinitialize = true;
  1466. break;
  1467. }
  1468. case id_use_7u_spacebar:
  1469. {
  1470. g_config.use_7u_spacebar = (bool)*value_data;
  1471. reinitialize = true;
  1472. break;
  1473. }
  1474. case id_use_iso_enter:
  1475. {
  1476. g_config.use_iso_enter = (bool)*value_data;
  1477. reinitialize = true;
  1478. break;
  1479. }
  1480. case id_disable_hhkb_blocker_leds:
  1481. {
  1482. g_config.disable_hhkb_blocker_leds = (bool)*value_data;
  1483. reinitialize = true;
  1484. break;
  1485. }
  1486. #endif
  1487. case id_disable_when_usb_suspended:
  1488. {
  1489. g_config.disable_when_usb_suspended = (bool)*value_data;
  1490. break;
  1491. }
  1492. case id_disable_after_timeout:
  1493. {
  1494. g_config.disable_after_timeout = *value_data;
  1495. break;
  1496. }
  1497. case id_brightness:
  1498. {
  1499. g_config.brightness = *value_data;
  1500. break;
  1501. }
  1502. case id_effect:
  1503. {
  1504. g_config.effect = *value_data;
  1505. break;
  1506. }
  1507. case id_effect_speed:
  1508. {
  1509. g_config.effect_speed = *value_data;
  1510. break;
  1511. }
  1512. case id_color_1:
  1513. {
  1514. _set_color( &(g_config.color_1), value_data );
  1515. break;
  1516. }
  1517. case id_color_2:
  1518. {
  1519. _set_color( &(g_config.color_2), value_data );
  1520. break;
  1521. }
  1522. case id_caps_lock_indicator_color:
  1523. {
  1524. _set_color( &(g_config.caps_lock_indicator.color), value_data );
  1525. break;
  1526. }
  1527. case id_caps_lock_indicator_row_col:
  1528. {
  1529. backlight_set_indicator_index( &(g_config.caps_lock_indicator.index), value_data[0], value_data[1] );
  1530. break;
  1531. }
  1532. case id_layer_1_indicator_color:
  1533. {
  1534. _set_color( &(g_config.layer_1_indicator.color), value_data );
  1535. break;
  1536. }
  1537. case id_layer_1_indicator_row_col:
  1538. {
  1539. backlight_set_indicator_index( &(g_config.layer_1_indicator.index), value_data[0], value_data[1] );
  1540. break;
  1541. }
  1542. case id_layer_2_indicator_color:
  1543. {
  1544. _set_color( &(g_config.layer_2_indicator.color), value_data );
  1545. break;
  1546. }
  1547. case id_layer_2_indicator_row_col:
  1548. {
  1549. backlight_set_indicator_index( &(g_config.layer_2_indicator.index), value_data[0], value_data[1] );
  1550. break;
  1551. }
  1552. case id_layer_3_indicator_color:
  1553. {
  1554. _set_color( &(g_config.layer_3_indicator.color), value_data );
  1555. break;
  1556. }
  1557. case id_layer_3_indicator_row_col:
  1558. {
  1559. backlight_set_indicator_index( &(g_config.layer_3_indicator.index), value_data[0], value_data[1] );
  1560. break;
  1561. }
  1562. case id_alphas_mods:
  1563. {
  1564. for ( int i=0; i<5; i++ )
  1565. {
  1566. g_config.alphas_mods[i] = ( *(value_data+i*2) << 8 ) | ( *(value_data+i*2+1) );
  1567. }
  1568. }
  1569. #if defined(RGB_BACKLIGHT_M6_B)
  1570. case id_custom_color:
  1571. {
  1572. uint8_t index = value_data[0];
  1573. if ( index >= 0 && index <= 6 )
  1574. {
  1575. _set_color( &(g_config.custom_color[index]), &(value_data[1]) );
  1576. }
  1577. }
  1578. #endif
  1579. }
  1580. if ( reinitialize )
  1581. {
  1582. backlight_init_drivers();
  1583. }
  1584. }
  1585. void backlight_config_get_value( uint8_t *data )
  1586. {
  1587. uint8_t *value_id = &(data[0]);
  1588. uint8_t *value_data = &(data[1]);
  1589. switch ( *value_id )
  1590. {
  1591. case id_use_split_backspace:
  1592. {
  1593. *value_data = ( g_config.use_split_backspace ? 1 : 0 );
  1594. break;
  1595. }
  1596. case id_use_split_left_shift:
  1597. {
  1598. *value_data = ( g_config.use_split_left_shift ? 1 : 0 );
  1599. break;
  1600. }
  1601. case id_use_split_right_shift:
  1602. {
  1603. *value_data = ( g_config.use_split_right_shift ? 1 : 0 );
  1604. break;
  1605. }
  1606. case id_use_7u_spacebar:
  1607. {
  1608. *value_data = ( g_config.use_7u_spacebar ? 1 : 0 );
  1609. break;
  1610. }
  1611. case id_use_iso_enter:
  1612. {
  1613. *value_data = ( g_config.use_iso_enter ? 1 : 0 );
  1614. break;
  1615. }
  1616. case id_disable_when_usb_suspended:
  1617. {
  1618. *value_data = ( g_config.disable_when_usb_suspended ? 1 : 0 );
  1619. break;
  1620. }
  1621. case id_disable_hhkb_blocker_leds:
  1622. {
  1623. *value_data = ( g_config.disable_hhkb_blocker_leds ? 1 : 0 );
  1624. break;
  1625. }
  1626. case id_disable_after_timeout:
  1627. {
  1628. *value_data = g_config.disable_after_timeout;
  1629. break;
  1630. }
  1631. case id_brightness:
  1632. {
  1633. *value_data = g_config.brightness;
  1634. break;
  1635. }
  1636. case id_effect:
  1637. {
  1638. *value_data = g_config.effect;
  1639. break;
  1640. }
  1641. case id_effect_speed:
  1642. {
  1643. *value_data = g_config.effect_speed;
  1644. break;
  1645. }
  1646. case id_color_1:
  1647. {
  1648. _get_color( &(g_config.color_1), value_data );
  1649. break;
  1650. }
  1651. case id_color_2:
  1652. {
  1653. _get_color( &(g_config.color_2), value_data );
  1654. break;
  1655. }
  1656. case id_caps_lock_indicator_color:
  1657. {
  1658. _get_color( &(g_config.caps_lock_indicator.color), value_data );
  1659. break;
  1660. }
  1661. case id_caps_lock_indicator_row_col:
  1662. {
  1663. backlight_get_indicator_row_col( g_config.caps_lock_indicator.index, &(value_data[0]), &(value_data[1]) );
  1664. break;
  1665. }
  1666. case id_layer_1_indicator_color:
  1667. {
  1668. _get_color( &(g_config.layer_1_indicator.color), value_data );
  1669. break;
  1670. }
  1671. case id_layer_1_indicator_row_col:
  1672. {
  1673. backlight_get_indicator_row_col( g_config.layer_1_indicator.index, &(value_data[0]), &(value_data[1]) );
  1674. break;
  1675. }
  1676. case id_layer_2_indicator_color:
  1677. {
  1678. _get_color( &(g_config.layer_2_indicator.color), value_data );
  1679. break;
  1680. }
  1681. case id_layer_2_indicator_row_col:
  1682. {
  1683. backlight_get_indicator_row_col( g_config.layer_2_indicator.index, &(value_data[0]), &(value_data[1]) );
  1684. break;
  1685. }
  1686. case id_layer_3_indicator_color:
  1687. {
  1688. _get_color( &(g_config.layer_3_indicator.color), value_data );
  1689. break;
  1690. }
  1691. case id_layer_3_indicator_row_col:
  1692. {
  1693. backlight_get_indicator_row_col( g_config.layer_3_indicator.index, &(value_data[0]), &(value_data[1]) );
  1694. break;
  1695. }
  1696. case id_alphas_mods:
  1697. {
  1698. for ( int i=0; i<5; i++ )
  1699. {
  1700. *(value_data+i*2) = g_config.alphas_mods[i] >> 8;
  1701. *(value_data+i*2+1) = g_config.alphas_mods[i] & 0xFF;
  1702. }
  1703. }
  1704. #if defined(RGB_BACKLIGHT_M6_B)
  1705. case id_custom_color:
  1706. {
  1707. uint8_t index = value_data[0];
  1708. if ( index >= 0 && index <= 6 )
  1709. {
  1710. _get_color( &(g_config.custom_color[index]), &(value_data[1]) );
  1711. }
  1712. }
  1713. #endif
  1714. }
  1715. }
  1716. void backlight_config_set_alphas_mods( uint16_t *alphas_mods )
  1717. {
  1718. for ( int i=0; i<5; i++ )
  1719. {
  1720. g_config.alphas_mods[i] = alphas_mods[i];
  1721. }
  1722. backlight_config_save();
  1723. }
  1724. void backlight_config_load(void)
  1725. {
  1726. eeprom_read_block( &g_config, ((void*)RGB_BACKLIGHT_CONFIG_EEPROM_ADDR), sizeof(backlight_config) );
  1727. }
  1728. void backlight_config_save(void)
  1729. {
  1730. eeprom_update_block( &g_config, ((void*)RGB_BACKLIGHT_CONFIG_EEPROM_ADDR), sizeof(backlight_config) );
  1731. }
  1732. void backlight_init_drivers(void)
  1733. {
  1734. // Initialize I2C
  1735. i2c_init();
  1736. #if defined(RGB_BACKLIGHT_M6_B)
  1737. IS31FL3218_init();
  1738. #elif defined(RGB_BACKLIGHT_HS60)
  1739. IS31FL3733_init( ISSI_ADDR_1, 0 );
  1740. for ( int index = 0; index < DRIVER_LED_TOTAL; index++ )
  1741. {
  1742. #if defined(HS60_ANSI)
  1743. bool enabled = !( ( index == 48-1 ) || //LA48
  1744. ( index == 51-1 ) || //LA51
  1745. ( index == 61-1 ) ); //LA61
  1746. #elif defined(HS60_HHKB)
  1747. bool enabled = !( ( index == 61-1 ) || //LA61
  1748. ( index == 62-1 ) ); //LA62
  1749. #else //HS60_ISO
  1750. bool enabled = !( ( index == 51-1 ) || //LA51
  1751. ( index == 61-1 ) ); //LA61
  1752. #endif
  1753. // This only caches it for later
  1754. IS31FL3733_set_led_control_register( index, enabled, enabled, enabled );
  1755. }
  1756. // This actually updates the LED drivers
  1757. IS31FL3733_update_led_control_registers( ISSI_ADDR_1, 0 );
  1758. #elif defined(RGB_BACKLIGHT_NK65)
  1759. IS31FL3733_init( ISSI_ADDR_1, 0 );
  1760. IS31FL3733_init( ISSI_ADDR_2, 0 );
  1761. for ( int index = 0; index < DRIVER_LED_TOTAL; index++ )
  1762. {
  1763. bool enabled = !( ( index == 61-1 ) || //LA61
  1764. ( index > 6+64-1 ) ); //LB7-LB64
  1765. // This only caches it for later
  1766. IS31FL3733_set_led_control_register( index, enabled, enabled, enabled );
  1767. }
  1768. IS31FL3733_set_led_control_register( 7+64-1, 0, 1, 0 ); //Enable LB7 green enable for indicators
  1769. // This actually updates the LED drivers
  1770. IS31FL3733_update_led_control_registers( ISSI_ADDR_1, 0 );
  1771. IS31FL3733_update_led_control_registers( ISSI_ADDR_2, 1 );
  1772. #else
  1773. IS31FL3731_init( ISSI_ADDR_1 );
  1774. IS31FL3731_init( ISSI_ADDR_2 );
  1775. for ( int index = 0; index < DRIVER_LED_TOTAL; index++ )
  1776. {
  1777. // OR the possible "disabled" cases together, then NOT the result to get the enabled state
  1778. // LC6 LD13 not present on Zeal65
  1779. #if defined(RGB_BACKLIGHT_ZEAL65)
  1780. bool enabled = !( ( index == 18+5 && !g_config.use_split_backspace ) || // LB5
  1781. ( index == 36+6 ) || // LC6
  1782. ( index == 54+13 ) ); // LD13
  1783. #elif defined(RGB_BACKLIGHT_KOYU)
  1784. bool enabled = !( ( index == 36+15 ) || // LC15
  1785. ( index == 54+13 ) || // LD13
  1786. ( index == 54+17 ) ); // LD17
  1787. #elif defined(RGB_BACKLIGHT_M60_A)
  1788. bool enabled = !(
  1789. // LB6 LB7 LB8 LB15 LB16 LB17 not present on M60-A
  1790. ( index == 18+6 ) || // LB6
  1791. ( index == 18+7 ) || // LB7
  1792. ( index == 18+8 ) || // LB8
  1793. ( index == 18+15 ) || // LB15
  1794. ( index == 18+16 ) || // LB16
  1795. ( index == 18+17 ) || // LB17
  1796. // HHKB blockers (LC17, LD17) and ISO extra keys (LC15,LD13) not present on M60-A
  1797. ( index == 36+17 ) || // LC17
  1798. ( index == 54+17 ) || // LD17
  1799. ( index == 36+15 ) || // LC15
  1800. ( index == 54+13 ) ); // LD13
  1801. #elif defined(RGB_BACKLIGHT_ZEAL60)
  1802. // LB6 LB7 LB8 LB15 LB16 LB17 not present on Zeal60
  1803. bool enabled = !( ( index == 18+5 && !g_config.use_split_backspace ) || // LB5
  1804. ( index == 36+15 && !g_config.use_split_left_shift ) || // LC15
  1805. ( index == 54+8 && !g_config.use_split_right_shift ) || // LD8
  1806. ( index == 54+13 && g_config.use_7u_spacebar ) || // LD13
  1807. ( index == 36+17 && g_config.disable_hhkb_blocker_leds ) || // LC17
  1808. ( index == 54+17 && g_config.disable_hhkb_blocker_leds ) || // LD17
  1809. ( index == 18+6 ) || // LB6
  1810. ( index == 18+7 ) || // LB7
  1811. ( index == 18+8 ) || // LB8
  1812. ( index == 18+15 ) || // LB15
  1813. ( index == 18+16 ) || // LB16
  1814. ( index == 18+17 ) ); // LB17
  1815. #endif
  1816. // This only caches it for later
  1817. IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
  1818. }
  1819. // This actually updates the LED drivers
  1820. IS31FL3731_update_led_control_registers( ISSI_ADDR_1, ISSI_ADDR_2 );
  1821. #endif // !defined(RGB_BACKLIGHT_M6_B)
  1822. // TODO: put the 1 second startup delay here?
  1823. // clear the key hits
  1824. for ( int led=0; led<BACKLIGHT_LED_COUNT; led++ )
  1825. {
  1826. g_key_hit[led] = 255;
  1827. }
  1828. }
  1829. bool process_record_backlight(uint16_t keycode, keyrecord_t *record)
  1830. {
  1831. // Record keypresses for backlight effects
  1832. if ( record->event.pressed )
  1833. {
  1834. backlight_set_key_hit( record->event.key.row, record->event.key.col );
  1835. }
  1836. switch(keycode)
  1837. {
  1838. case BR_INC:
  1839. if (record->event.pressed)
  1840. {
  1841. backlight_brightness_increase();
  1842. }
  1843. return false;
  1844. break;
  1845. case BR_DEC:
  1846. if (record->event.pressed)
  1847. {
  1848. backlight_brightness_decrease();
  1849. }
  1850. return false;
  1851. break;
  1852. case EF_INC:
  1853. if (record->event.pressed)
  1854. {
  1855. backlight_effect_increase();
  1856. }
  1857. return false;
  1858. break;
  1859. case EF_DEC:
  1860. if (record->event.pressed)
  1861. {
  1862. backlight_effect_decrease();
  1863. }
  1864. return false;
  1865. break;
  1866. case ES_INC:
  1867. if (record->event.pressed)
  1868. {
  1869. backlight_effect_speed_increase();
  1870. }
  1871. return false;
  1872. break;
  1873. case ES_DEC:
  1874. if (record->event.pressed)
  1875. {
  1876. backlight_effect_speed_decrease();
  1877. }
  1878. return false;
  1879. break;
  1880. case H1_INC:
  1881. if (record->event.pressed)
  1882. {
  1883. backlight_color_1_hue_increase();
  1884. }
  1885. return false;
  1886. break;
  1887. case H1_DEC:
  1888. if (record->event.pressed)
  1889. {
  1890. backlight_color_1_hue_decrease();
  1891. }
  1892. return false;
  1893. break;
  1894. case S1_INC:
  1895. if (record->event.pressed)
  1896. {
  1897. backlight_color_1_sat_increase();
  1898. }
  1899. return false;
  1900. break;
  1901. case S1_DEC:
  1902. if (record->event.pressed)
  1903. {
  1904. backlight_color_1_sat_decrease();
  1905. break;
  1906. }
  1907. return false;
  1908. break;
  1909. case H2_INC:
  1910. if (record->event.pressed)
  1911. {
  1912. backlight_color_2_hue_increase();
  1913. }
  1914. return false;
  1915. break;
  1916. case H2_DEC:
  1917. if (record->event.pressed)
  1918. {
  1919. backlight_color_2_hue_decrease();
  1920. }
  1921. return false;
  1922. break;
  1923. case S2_INC:
  1924. if (record->event.pressed)
  1925. {
  1926. backlight_color_2_sat_increase();
  1927. }
  1928. return false;
  1929. break;
  1930. case S2_DEC:
  1931. if (record->event.pressed)
  1932. {
  1933. backlight_color_2_sat_decrease();
  1934. break;
  1935. }
  1936. return false;
  1937. break;
  1938. }
  1939. return true;
  1940. }
  1941. // Deals with the messy details of incrementing an integer
  1942. uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max )
  1943. {
  1944. int16_t new_value = value;
  1945. new_value += step;
  1946. return MIN( MAX( new_value, min ), max );
  1947. }
  1948. uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max )
  1949. {
  1950. int16_t new_value = value;
  1951. new_value -= step;
  1952. return MIN( MAX( new_value, min ), max );
  1953. }
  1954. void backlight_effect_increase(void)
  1955. {
  1956. g_config.effect = increment( g_config.effect, 1, 0, BACKLIGHT_EFFECT_MAX );
  1957. backlight_config_save();
  1958. }
  1959. void backlight_effect_decrease(void)
  1960. {
  1961. g_config.effect = decrement( g_config.effect, 1, 0, BACKLIGHT_EFFECT_MAX );
  1962. backlight_config_save();
  1963. }
  1964. void backlight_effect_speed_increase(void)
  1965. {
  1966. g_config.effect_speed = increment( g_config.effect_speed, 1, 0, 3 );
  1967. backlight_config_save();
  1968. }
  1969. void backlight_effect_speed_decrease(void)
  1970. {
  1971. g_config.effect_speed = decrement( g_config.effect_speed, 1, 0, 3 );
  1972. backlight_config_save();
  1973. }
  1974. void backlight_brightness_increase(void)
  1975. {
  1976. g_config.brightness = increment( g_config.brightness, 8, 0, 255 );
  1977. backlight_config_save();
  1978. }
  1979. void backlight_brightness_decrease(void)
  1980. {
  1981. g_config.brightness = decrement( g_config.brightness, 8, 0, 255 );
  1982. backlight_config_save();
  1983. }
  1984. void backlight_color_1_hue_increase(void)
  1985. {
  1986. g_config.color_1.h = increment( g_config.color_1.h, 8, 0, 255 );
  1987. backlight_config_save();
  1988. }
  1989. void backlight_color_1_hue_decrease(void)
  1990. {
  1991. g_config.color_1.h = decrement( g_config.color_1.h, 8, 0, 255 );
  1992. backlight_config_save();
  1993. }
  1994. void backlight_color_1_sat_increase(void)
  1995. {
  1996. g_config.color_1.s = increment( g_config.color_1.s, 8, 0, 255 );
  1997. backlight_config_save();
  1998. }
  1999. void backlight_color_1_sat_decrease(void)
  2000. {
  2001. g_config.color_1.s = decrement( g_config.color_1.s, 8, 0, 255 );
  2002. backlight_config_save();
  2003. }
  2004. void backlight_color_2_hue_increase(void)
  2005. {
  2006. g_config.color_2.h = increment( g_config.color_2.h, 8, 0, 255 );
  2007. backlight_config_save();
  2008. }
  2009. void backlight_color_2_hue_decrease(void)
  2010. {
  2011. g_config.color_2.h = decrement( g_config.color_2.h, 8, 0, 255 );
  2012. backlight_config_save();
  2013. }
  2014. void backlight_color_2_sat_increase(void)
  2015. {
  2016. g_config.color_2.s = increment( g_config.color_2.s, 8, 0, 255 );
  2017. backlight_config_save();
  2018. }
  2019. void backlight_color_2_sat_decrease(void)
  2020. {
  2021. g_config.color_2.s = decrement( g_config.color_2.s, 8, 0, 255 );
  2022. backlight_config_save();
  2023. }
  2024. #if defined(RGB_DEBUGGING_ONLY)
  2025. void backlight_test_led( uint8_t index, bool red, bool green, bool blue )
  2026. {
  2027. for ( int i=0; i<BACKLIGHT_LED_COUNT; i++ )
  2028. {
  2029. if ( i == index )
  2030. {
  2031. IS31FL3731_set_led_control_register( i, red, green, blue );
  2032. }
  2033. else
  2034. {
  2035. IS31FL3731_set_led_control_register( i, false, false, false );
  2036. }
  2037. }
  2038. }
  2039. void backlight_debug_led( bool state )
  2040. {
  2041. if (state)
  2042. {
  2043. // Output high.
  2044. DDRE |= (1<<6);
  2045. PORTE |= (1<<6);
  2046. }
  2047. else
  2048. {
  2049. // Output low.
  2050. DDRE &= ~(1<<6);
  2051. PORTE &= ~(1<<6);
  2052. }
  2053. }
  2054. #endif // defined(RGB_DEBUGGING_ONLY)
  2055. #endif // BACKLIGHT_ENABLED