logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

n6.c (10707B)


  1. /**
  2. * @file n6.c
  3. *
  4. Copyright 2021 astro
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "quantum.h"
  17. #ifdef RGBLIGHT_ENABLE
  18. # include "i2c_master.h"
  19. # include "drivers/led/issi/is31fl3731.h"
  20. # include "ws2812.h"
  21. enum {
  22. SELF_TESTING,
  23. CAPS_ALERT,
  24. NORMAL,
  25. };
  26. enum {
  27. ST_STAGE_1,
  28. ST_STAGE_2,
  29. ST_STAGE_3,
  30. };
  31. // alert state update interval
  32. #define ALERT_INTERVAL 500
  33. // self testing state update interval
  34. #define ST_INTERVAL 100
  35. // self testing start index
  36. #define ST_DEFAULT_INDEX 15
  37. // self testing stage delay
  38. #define ST_STAGE_DELAY 10
  39. // self testing stage cycle count
  40. #define ST_STAGE_COUNT 4
  41. // self testing stage end duration
  42. #define ST_END_DURATION 10
  43. // led index
  44. #define ST_LEFT_BEGIN 0
  45. #ifdef IS31FL3731_I2C_ADDRESS_2
  46. #define ST_LEFT_SIZE 4
  47. #else
  48. #define ST_LEFT_SIZE 2
  49. #endif
  50. #define ST_LEFT_END (ST_LEFT_BEGIN+ST_LEFT_SIZE-1)
  51. #ifdef IS31FL3731_I2C_ADDRESS_2
  52. #define ST_RIGHT_BEGIN 60
  53. #else
  54. #define ST_RIGHT_BEGIN 30
  55. #endif
  56. #ifdef IS31FL3731_I2C_ADDRESS_2
  57. #define ST_RIGHT_SIZE 4
  58. #else
  59. #define ST_RIGHT_SIZE 2
  60. #endif
  61. #define ST_RIGHT_END (ST_RIGHT_BEGIN+ST_RIGHT_SIZE-1)
  62. extern rgblight_config_t rgblight_config;
  63. typedef struct {
  64. uint8_t state;
  65. uint8_t testing;
  66. bool alert;
  67. uint8_t index;
  68. uint8_t delay;
  69. uint8_t count;
  70. bool dir;
  71. uint8_t duration;
  72. uint16_t ticks;
  73. } rgb_state_t;
  74. static rgb_state_t rgb_state = {
  75. .state = //NORMAL,
  76. SELF_TESTING,
  77. .testing = ST_STAGE_1,
  78. .ticks = 0,
  79. .alert = false,
  80. .index = ST_DEFAULT_INDEX,
  81. .delay = ST_STAGE_DELAY,
  82. .count = ST_STAGE_COUNT,
  83. .dir = true,
  84. .duration = ST_END_DURATION,
  85. };
  86. static void update_ticks(void)
  87. {
  88. rgb_state.ticks = timer_read();
  89. }
  90. static void self_testing(void)
  91. {
  92. if (timer_elapsed(rgb_state.ticks) < ST_INTERVAL) return;
  93. hsv_t hsv;
  94. hsv.h = rgblight_config.hue;
  95. hsv.s = rgblight_config.sat;
  96. hsv.v = rgblight_config.val;
  97. rgb_t led = hsv_to_rgb(hsv);
  98. switch(rgb_state.testing) {
  99. case ST_STAGE_1:
  100. if (rgb_state.index !=0 ) {
  101. is31fl3731_set_color_all(0, 0, 0);
  102. }
  103. if (rgb_state.index >= ST_LEFT_END) {
  104. for (int i = rgb_state.index - 1; i < IS31FL3731_LED_COUNT - rgb_state.index + 1; i++) {
  105. is31fl3731_set_color(i, led.r, led.g, led.b);
  106. }
  107. if (rgb_state.index == ST_LEFT_END) {
  108. rgb_state.index = ST_LEFT_BEGIN;
  109. } else {
  110. rgb_state.index -= ST_LEFT_SIZE;
  111. }
  112. } else{
  113. if (rgb_state.delay > 0) {
  114. rgb_state.delay--;
  115. } else {
  116. // move to stage 2
  117. rgb_state.index = ST_LEFT_BEGIN+ST_LEFT_SIZE;
  118. rgb_state.testing = ST_STAGE_2;
  119. }
  120. }
  121. break;
  122. case ST_STAGE_2: {
  123. // clear all
  124. is31fl3731_set_color_all(0, 0, 0);
  125. int i = 0;
  126. // light left and right
  127. for (i = 0; i < ST_LEFT_SIZE; i++) {
  128. is31fl3731_set_color(ST_LEFT_BEGIN+i, led.r, led.g, led.b);
  129. }
  130. for (i = 0; i < ST_RIGHT_SIZE; i++) {
  131. is31fl3731_set_color(ST_RIGHT_BEGIN+i, led.r, led.g, led.b);
  132. }
  133. if (rgb_state.dir) {
  134. // left to right
  135. for (int i = rgb_state.index; i < rgb_state.index+ST_LEFT_SIZE+ST_RIGHT_SIZE; i++) {
  136. is31fl3731_set_color(i, led.r, led.g, led.b);
  137. }
  138. rgb_state.index += ST_LEFT_SIZE+ST_RIGHT_SIZE;
  139. if (rgb_state.index == ST_RIGHT_BEGIN) {
  140. rgb_state.dir = !rgb_state.dir;
  141. rgb_state.count--;
  142. }
  143. } else {
  144. // right to left
  145. for (int i = rgb_state.index - ST_RIGHT_SIZE; i < rgb_state.index; i++) {
  146. is31fl3731_set_color(i, led.r, led.g, led.b);
  147. }
  148. rgb_state.index -= ST_LEFT_SIZE + ST_RIGHT_SIZE;
  149. if (rgb_state.index == ST_LEFT_BEGIN+ST_LEFT_SIZE) {
  150. rgb_state.dir = !rgb_state.dir;
  151. rgb_state.count--;
  152. }
  153. }
  154. if (rgb_state.count == 0) {
  155. // move to stage 3
  156. rgb_state.testing = ST_STAGE_3;
  157. rgb_state.index = 0;
  158. rgb_state.delay = ST_STAGE_DELAY;
  159. rgb_state.duration = ST_END_DURATION;
  160. }
  161. }
  162. break;
  163. case ST_STAGE_3:
  164. if (rgb_state.index != IS31FL3731_LED_COUNT/2) {
  165. is31fl3731_set_color_all(0, 0, 0);
  166. }
  167. // light left and right
  168. if (rgb_state.index == IS31FL3731_LED_COUNT/2) {
  169. if (rgb_state.duration) {
  170. rgb_state.duration--;
  171. } else {
  172. if (host_keyboard_led_state().caps_lock) {
  173. rgb_state.state = CAPS_ALERT;
  174. } else {
  175. rgb_state.state = NORMAL;
  176. rgblight_set();
  177. }
  178. }
  179. } else {
  180. // left
  181. for (int i = 0; i < rgb_state.index+1; i++) {
  182. is31fl3731_set_color(i, led.r, led.g, led.b);
  183. }
  184. // right
  185. for (int i = ST_RIGHT_END; i > ST_RIGHT_END - rgb_state.index - 1; i--) {
  186. is31fl3731_set_color(i, led.r, led.g, led.b);
  187. }
  188. rgb_state.index ++;
  189. }
  190. break;
  191. }
  192. update_ticks();
  193. }
  194. const is31fl3731_led_t PROGMEM g_is31fl3731_leds[IS31FL3731_LED_COUNT] = {
  195. /* Refer to IS31 manual for these locations
  196. * driver
  197. * | R location
  198. * | | G location
  199. * | | | B location
  200. * | | | | */
  201. // left CA
  202. {0, C1_1, C3_2, C4_2},
  203. {0, C1_2, C2_2, C4_3},
  204. {0, C1_3, C2_3, C3_3},
  205. {0, C1_4, C2_4, C3_4},
  206. {0, C1_5, C2_5, C3_5},
  207. {0, C1_6, C2_6, C3_6},
  208. {0, C1_7, C2_7, C3_7},
  209. {0, C1_8, C2_8, C3_8},
  210. {0, C9_1, C8_1, C7_1},
  211. {0, C9_2, C8_2, C7_2},
  212. {0, C9_3, C8_3, C7_3},
  213. {0, C9_4, C8_4, C7_4},
  214. {0, C9_5, C8_5, C7_5},
  215. {0, C9_6, C8_6, C7_6},
  216. {0, C9_7, C8_7, C6_6},
  217. {0, C9_8, C7_7, C6_7},
  218. // left CB
  219. {0, C1_9, C3_10, C4_10},
  220. {0, C1_10, C2_10, C4_11},
  221. {0, C1_11, C2_11, C3_11},
  222. {0, C1_12, C2_12, C3_12},
  223. {0, C1_13, C2_13, C3_13},
  224. {0, C1_14, C2_14, C3_14},
  225. {0, C1_15, C2_15, C3_15},
  226. {0, C1_16, C2_16, C3_16},
  227. {0, C9_9, C8_9, C7_9},
  228. {0, C9_10, C8_10, C7_10},
  229. {0, C9_11, C8_11, C7_11},
  230. {0, C9_12, C8_12, C7_12},
  231. {0, C9_13, C8_13, C7_13},
  232. {0, C9_14, C8_14, C7_14},
  233. {0, C9_15, C8_15, C6_14},
  234. {0, C9_16, C7_15, C6_15},
  235. // right CA
  236. {1, C1_1, C3_2, C4_2},
  237. {1, C1_2, C2_2, C4_3},
  238. {1, C1_3, C2_3, C3_3},
  239. {1, C1_4, C2_4, C3_4},
  240. {1, C1_5, C2_5, C3_5},
  241. {1, C1_6, C2_6, C3_6},
  242. {1, C1_7, C2_7, C3_7},
  243. {1, C1_8, C2_8, C3_8},
  244. {1, C9_1, C8_1, C7_1},
  245. {1, C9_2, C8_2, C7_2},
  246. {1, C9_3, C8_3, C7_3},
  247. {1, C9_4, C8_4, C7_4},
  248. {1, C9_5, C8_5, C7_5},
  249. {1, C9_6, C8_6, C7_6},
  250. {1, C9_7, C8_7, C6_6},
  251. {1, C9_8, C7_7, C6_7},
  252. // right CB
  253. {1, C1_9, C3_10, C4_10},
  254. {1, C1_10, C2_10, C4_11},
  255. {1, C1_11, C2_11, C3_11},
  256. {1, C1_12, C2_12, C3_12},
  257. {1, C1_13, C2_13, C3_13},
  258. {1, C1_14, C2_14, C3_14},
  259. {1, C1_15, C2_15, C3_15},
  260. {1, C1_16, C2_16, C3_16},
  261. {1, C9_9, C8_9, C7_9},
  262. {1, C9_10, C8_10, C7_10},
  263. {1, C9_11, C8_11, C7_11},
  264. {1, C9_12, C8_12, C7_12},
  265. {1, C9_13, C8_13, C7_13},
  266. {1, C9_14, C8_14, C7_14},
  267. {1, C9_15, C8_15, C6_14},
  268. {1, C9_16, C7_15, C6_15},
  269. };
  270. void matrix_init_kb(void)
  271. {
  272. gpio_set_pin_output(LED_CAPS_LOCK_PIN);
  273. gpio_write_pin_low(LED_CAPS_LOCK_PIN);
  274. update_ticks();
  275. matrix_init_user();
  276. }
  277. void housekeeping_task_kb(void)
  278. {
  279. if (rgb_state.state == SELF_TESTING) {
  280. self_testing();
  281. } else if (rgb_state.state == CAPS_ALERT) {
  282. if (rgb_state.alert) {
  283. is31fl3731_set_color_all(0xFF, 0xA5, 0x00);
  284. ws2812_set_color_all(0xFF, 0xA5, 0x00);
  285. } else {
  286. is31fl3731_set_color_all(0, 0, 0);
  287. ws2812_set_color_all(0, 0, 0);
  288. }
  289. if (timer_elapsed(rgb_state.ticks) > ALERT_INTERVAL) {
  290. rgb_state.alert = !rgb_state.alert;
  291. update_ticks();
  292. }
  293. }
  294. is31fl3731_flush();
  295. }
  296. void init_custom(void) {
  297. is31fl3731_init_drivers();
  298. ws2812_init();
  299. }
  300. void set_color_custom(int index, uint8_t red, uint8_t green, uint8_t blue) {
  301. if (index < IS31FL3731_LED_COUNT) {
  302. is31fl3731_set_color(index, red, green, blue);
  303. } else if (index < IS31FL3731_LED_COUNT + WS2812_LED_COUNT) {
  304. ws2812_set_color(index - IS31FL3731_LED_COUNT, red, green, blue);
  305. }
  306. }
  307. void set_color_all_custom(uint8_t red, uint8_t green, uint8_t blue) {
  308. for (int i = 0; i < RGBLIGHT_LED_COUNT; i++) {
  309. set_color_custom(i, red, green, blue);
  310. }
  311. }
  312. void flush_custom(void) {
  313. if (rgb_state.state != NORMAL) return;
  314. is31fl3731_flush();
  315. ws2812_flush();
  316. }
  317. const rgblight_driver_t rgblight_driver = {
  318. .init = init_custom,
  319. .set_color = set_color_custom,
  320. .set_color_all = set_color_all_custom,
  321. .flush = flush_custom,
  322. };
  323. bool led_update_kb(led_t led_state)
  324. {
  325. bool res = led_update_user(led_state);
  326. if (res) {
  327. gpio_write_pin(LED_CAPS_LOCK_PIN, led_state.caps_lock);
  328. if (rgb_state.state != SELF_TESTING) {
  329. if (led_state.caps_lock) {
  330. rgb_state.state = CAPS_ALERT;
  331. update_ticks();
  332. } else {
  333. rgb_state.state = NORMAL;
  334. rgblight_set();
  335. }
  336. }
  337. }
  338. return res;
  339. }
  340. #endif