wt_rgb_backlight.c 85 KB

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