logo

qmk_firmware

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

700e.c (10766B)


  1. /**
  2. * @file 700e.c
  3. *
  4. Copyright 2022 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. typedef struct {
  63. uint8_t state;
  64. uint8_t testing;
  65. bool alert;
  66. uint8_t index;
  67. uint8_t delay;
  68. uint8_t count;
  69. bool dir;
  70. uint8_t duration;
  71. uint16_t ticks;
  72. } rgb_state_t;
  73. static rgb_state_t rgb_state = {
  74. .state = //NORMAL,
  75. SELF_TESTING,
  76. .testing = ST_STAGE_1,
  77. .ticks = 0,
  78. .alert = false,
  79. .index = ST_DEFAULT_INDEX,
  80. .delay = ST_STAGE_DELAY,
  81. .count = ST_STAGE_COUNT,
  82. .dir = true,
  83. .duration = ST_END_DURATION,
  84. };
  85. static void update_ticks(void)
  86. {
  87. rgb_state.ticks = timer_read();
  88. }
  89. static void self_testing(void)
  90. {
  91. if (timer_elapsed(rgb_state.ticks) < ST_INTERVAL) return;
  92. hsv_t hsv = rgblight_get_hsv();
  93. rgb_t led = hsv_to_rgb(hsv);
  94. switch(rgb_state.testing) {
  95. case ST_STAGE_1:
  96. if (rgb_state.index !=0 ) {
  97. is31fl3731_set_color_all(0, 0, 0);
  98. }
  99. if (rgb_state.index >= ST_LEFT_END) {
  100. for (int i = rgb_state.index - 1; i < IS31FL3731_LED_COUNT - rgb_state.index + 1; i++) {
  101. is31fl3731_set_color(i, led.r, led.g, led.b);
  102. }
  103. if (rgb_state.index == ST_LEFT_END) {
  104. rgb_state.index = ST_LEFT_BEGIN;
  105. } else {
  106. rgb_state.index -= ST_LEFT_SIZE;
  107. }
  108. } else{
  109. if (rgb_state.delay > 0) {
  110. rgb_state.delay--;
  111. } else {
  112. // move to stage 2
  113. rgb_state.index = ST_LEFT_BEGIN+ST_LEFT_SIZE;
  114. rgb_state.testing = ST_STAGE_2;
  115. }
  116. }
  117. break;
  118. case ST_STAGE_2: {
  119. // clear all
  120. is31fl3731_set_color_all(0, 0, 0);
  121. int i = 0;
  122. // light left and right
  123. for (i = 0; i < ST_LEFT_SIZE; i++) {
  124. is31fl3731_set_color(ST_LEFT_BEGIN+i, led.r, led.g, led.b);
  125. }
  126. for (i = 0; i < ST_RIGHT_SIZE; i++) {
  127. is31fl3731_set_color(ST_RIGHT_BEGIN+i, led.r, led.g, led.b);
  128. }
  129. if (rgb_state.dir) {
  130. // left to right
  131. for (int i = rgb_state.index; i < rgb_state.index+ST_LEFT_SIZE+ST_RIGHT_SIZE; i++) {
  132. is31fl3731_set_color(i, led.r, led.g, led.b);
  133. }
  134. rgb_state.index += ST_LEFT_SIZE+ST_RIGHT_SIZE;
  135. if (rgb_state.index == ST_RIGHT_BEGIN) {
  136. rgb_state.dir = !rgb_state.dir;
  137. rgb_state.count--;
  138. }
  139. } else {
  140. // right to left
  141. for (int i = rgb_state.index - ST_RIGHT_SIZE; i < rgb_state.index; i++) {
  142. is31fl3731_set_color(i, led.r, led.g, led.b);
  143. }
  144. rgb_state.index -= ST_LEFT_SIZE + ST_RIGHT_SIZE;
  145. if (rgb_state.index == ST_LEFT_BEGIN+ST_LEFT_SIZE) {
  146. rgb_state.dir = !rgb_state.dir;
  147. rgb_state.count--;
  148. }
  149. }
  150. if (rgb_state.count == 0) {
  151. // move to stage 3
  152. rgb_state.testing = ST_STAGE_3;
  153. rgb_state.index = 0;
  154. rgb_state.delay = ST_STAGE_DELAY;
  155. rgb_state.duration = ST_END_DURATION;
  156. }
  157. }
  158. break;
  159. case ST_STAGE_3:
  160. if (rgb_state.index != IS31FL3731_LED_COUNT/2) {
  161. is31fl3731_set_color_all(0, 0, 0);
  162. }
  163. // light left and right
  164. if (rgb_state.index == IS31FL3731_LED_COUNT/2) {
  165. if (rgb_state.duration) {
  166. rgb_state.duration--;
  167. } else {
  168. if (host_keyboard_led_state().caps_lock) {
  169. rgb_state.state = CAPS_ALERT;
  170. } else {
  171. rgb_state.state = NORMAL;
  172. rgblight_set();
  173. }
  174. }
  175. } else {
  176. // left
  177. for (int i = 0; i < rgb_state.index+1; i++) {
  178. is31fl3731_set_color(i, led.r, led.g, led.b);
  179. }
  180. // right
  181. for (int i = ST_RIGHT_END; i > ST_RIGHT_END - rgb_state.index - 1; i--) {
  182. is31fl3731_set_color(i, led.r, led.g, led.b);
  183. }
  184. rgb_state.index ++;
  185. }
  186. break;
  187. }
  188. update_ticks();
  189. }
  190. const is31fl3731_led_t PROGMEM g_is31fl3731_leds[IS31FL3731_LED_COUNT] = {
  191. /* Refer to IS31 manual for these locations
  192. * driver
  193. * | R location
  194. * | | G location
  195. * | | | B location
  196. * | | | | */
  197. // left CA
  198. {0, C1_1, C3_2, C4_2},
  199. {0, C1_2, C2_2, C4_3},
  200. {0, C1_3, C2_3, C3_3},
  201. {0, C1_4, C2_4, C3_4},
  202. {0, C1_5, C2_5, C3_5},
  203. {0, C1_6, C2_6, C3_6},
  204. {0, C1_7, C2_7, C3_7},
  205. {0, C1_8, C2_8, C3_8},
  206. {0, C9_1, C8_1, C7_1},
  207. {0, C9_2, C8_2, C7_2},
  208. {0, C9_3, C8_3, C7_3},
  209. {0, C9_4, C8_4, C7_4},
  210. {0, C9_5, C8_5, C7_5},
  211. {0, C9_6, C8_6, C7_6},
  212. {0, C9_7, C8_7, C6_6},
  213. {0, C9_8, C7_7, C6_7},
  214. // left CB
  215. {0, C1_9, C3_10, C4_10},
  216. {0, C1_10, C2_10, C4_11},
  217. {0, C1_11, C2_11, C3_11},
  218. {0, C1_12, C2_12, C3_12},
  219. {0, C1_13, C2_13, C3_13},
  220. {0, C1_14, C2_14, C3_14},
  221. {0, C1_15, C2_15, C3_15},
  222. {0, C1_16, C2_16, C3_16},
  223. {0, C9_9, C8_9, C7_9},
  224. {0, C9_10, C8_10, C7_10},
  225. {0, C9_11, C8_11, C7_11},
  226. {0, C9_12, C8_12, C7_12},
  227. {0, C9_13, C8_13, C7_13},
  228. {0, C9_14, C8_14, C7_14},
  229. {0, C9_15, C8_15, C6_14},
  230. {0, C9_16, C7_15, C6_15},
  231. // right CA
  232. {1, C1_1, C3_2, C4_2},
  233. {1, C1_2, C2_2, C4_3},
  234. {1, C1_3, C2_3, C3_3},
  235. {1, C1_4, C2_4, C3_4},
  236. {1, C1_5, C2_5, C3_5},
  237. {1, C1_6, C2_6, C3_6},
  238. {1, C1_7, C2_7, C3_7},
  239. {1, C1_8, C2_8, C3_8},
  240. {1, C9_1, C8_1, C7_1},
  241. {1, C9_2, C8_2, C7_2},
  242. {1, C9_3, C8_3, C7_3},
  243. {1, C9_4, C8_4, C7_4},
  244. {1, C9_5, C8_5, C7_5},
  245. {1, C9_6, C8_6, C7_6},
  246. {1, C9_7, C8_7, C6_6},
  247. {1, C9_8, C7_7, C6_7},
  248. // right CB
  249. {1, C1_9, C3_10, C4_10},
  250. {1, C1_10, C2_10, C4_11},
  251. {1, C1_11, C2_11, C3_11},
  252. {1, C1_12, C2_12, C3_12},
  253. {1, C1_13, C2_13, C3_13},
  254. {1, C1_14, C2_14, C3_14},
  255. {1, C1_15, C2_15, C3_15},
  256. {1, C1_16, C2_16, C3_16},
  257. {1, C9_9, C8_9, C7_9},
  258. {1, C9_10, C8_10, C7_10},
  259. {1, C9_11, C8_11, C7_11},
  260. {1, C9_12, C8_12, C7_12},
  261. {1, C9_13, C8_13, C7_13},
  262. {1, C9_14, C8_14, C7_14},
  263. {1, C9_15, C8_15, C6_14},
  264. {1, C9_16, C7_15, C6_15},
  265. };
  266. void matrix_init_kb(void)
  267. {
  268. gpio_set_pin_output(LED_CAPS_LOCK_PIN);
  269. gpio_write_pin_low(LED_CAPS_LOCK_PIN);
  270. update_ticks();
  271. matrix_init_user();
  272. }
  273. #define ALERM_LED_R 0xFF
  274. #define ALERM_LED_G 0xA5
  275. #define ALERM_LED_B 0x00
  276. //golden 0xFF, 0xD9, 0x00
  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(ALERM_LED_R, ALERM_LED_G, ALERM_LED_B);
  284. ws2812_set_color_all(ALERM_LED_G, ALERM_LED_R, ALERM_LED_B);
  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. ws2812_flush();
  296. }
  297. void init_custom(void) {
  298. is31fl3731_init_drivers();
  299. ws2812_init();
  300. }
  301. void set_color_custom(int index, uint8_t red, uint8_t green, uint8_t blue) {
  302. if (index < IS31FL3731_LED_COUNT) {
  303. is31fl3731_set_color(index, red, green, blue);
  304. } else if (index < IS31FL3731_LED_COUNT + WS2812_LED_COUNT) {
  305. ws2812_set_color(index - IS31FL3731_LED_COUNT, green, red, blue);
  306. }
  307. }
  308. void set_color_all_custom(uint8_t red, uint8_t green, uint8_t blue) {
  309. for (int i = 0; i < RGBLIGHT_LED_COUNT; i++) {
  310. set_color_custom(i, red, green, blue);
  311. }
  312. }
  313. void flush_custom(void) {
  314. if (rgb_state.state != NORMAL) return;
  315. is31fl3731_flush();
  316. ws2812_flush();
  317. }
  318. const rgblight_driver_t rgblight_driver = {
  319. .init = init_custom,
  320. .set_color = set_color_custom,
  321. .set_color_all = set_color_all_custom,
  322. .flush = flush_custom,
  323. };
  324. bool led_update_kb(led_t led_state)
  325. {
  326. bool res = led_update_user(led_state);
  327. if (res) {
  328. gpio_write_pin(LED_CAPS_LOCK_PIN, led_state.caps_lock);
  329. if (rgb_state.state != SELF_TESTING) {
  330. if (led_state.caps_lock) {
  331. rgb_state.state = CAPS_ALERT;
  332. update_ticks();
  333. } else {
  334. rgb_state.state = NORMAL;
  335. rgblight_set();
  336. }
  337. }
  338. }
  339. return res;
  340. }
  341. #endif