You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
8.6 KiB

  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 "n6.h"
  17. #include "i2c_master.h"
  18. #include "drivers/led/issi/is31fl3731.h"
  19. enum {
  20. SELF_TESTING,
  21. CAPS_ALERT,
  22. NORMAL,
  23. };
  24. enum {
  25. ST_STAGE_1,
  26. ST_STAGE_2,
  27. ST_STAGE_3,
  28. };
  29. // alert state update interval
  30. #define ALERT_INTERVAL 600
  31. // self testing state update interval
  32. #define ST_INTERVAL 100
  33. // self testing start index
  34. #define ST_DEFAULT_INDEX 15
  35. // self testing stage delay
  36. #define ST_STAGE_DELAY 10
  37. // self testing stage cycle count
  38. #define ST_STAGE_COUNT 4
  39. // self testing stage end duration
  40. #define ST_END_DURATION 10
  41. #ifdef RGBLIGHT_ENABLE
  42. extern rgblight_config_t rgblight_config;
  43. typedef struct {
  44. uint8_t state;
  45. uint8_t testing;
  46. bool alert;
  47. uint8_t index;
  48. uint8_t delay;
  49. uint8_t count;
  50. bool dir;
  51. uint8_t duration;
  52. uint16_t ticks;
  53. } rgb_state_t;
  54. static rgb_state_t rgb_state = {
  55. .state = //NORMAL,
  56. SELF_TESTING,
  57. .testing = ST_STAGE_1,
  58. .ticks = 0,
  59. .alert = false,
  60. .index = ST_DEFAULT_INDEX,
  61. .delay = ST_STAGE_DELAY,
  62. .count = ST_STAGE_COUNT,
  63. .dir = true,
  64. .duration = ST_END_DURATION,
  65. };
  66. static void update_ticks(void)
  67. {
  68. rgb_state.ticks = timer_read();
  69. }
  70. static void self_testing(void)
  71. {
  72. if (timer_elapsed(rgb_state.ticks) < ST_INTERVAL) return;
  73. HSV hsv;
  74. hsv.h = rgblight_config.hue;
  75. hsv.s = rgblight_config.sat;
  76. hsv.v = rgblight_config.val;
  77. RGB led = hsv_to_rgb(hsv);
  78. switch(rgb_state.testing) {
  79. case ST_STAGE_1:
  80. if (rgb_state.index !=0 ) {
  81. IS31FL3731_set_color_all(0, 0, 0);
  82. }
  83. if (rgb_state.index >= 1) {
  84. for (int i = rgb_state.index-1; i < 32-rgb_state.index+1; i++) {
  85. IS31FL3731_set_color(i, led.r, led.g, led.b);
  86. }
  87. if (rgb_state.index==1) {
  88. rgb_state.index=0;
  89. } else {
  90. rgb_state.index -= 2;
  91. }
  92. } else{
  93. if (rgb_state.delay > 0) {
  94. rgb_state.delay--;
  95. } else {
  96. // move to stage 2
  97. rgb_state.index = 2;
  98. rgb_state.testing = ST_STAGE_2;
  99. }
  100. }
  101. break;
  102. case ST_STAGE_2: {
  103. // clear all
  104. IS31FL3731_set_color_all(0, 0, 0);
  105. // light left and right
  106. IS31FL3731_set_color(0, led.r, led.g, led.b);
  107. IS31FL3731_set_color(1, led.r, led.g, led.b);
  108. IS31FL3731_set_color(30, led.r, led.g, led.b);
  109. IS31FL3731_set_color(31, led.r, led.g, led.b);
  110. if (rgb_state.dir) {
  111. // left to right
  112. for (int i = rgb_state.index; i < rgb_state.index+4; i++) {
  113. IS31FL3731_set_color(i, led.r, led.g, led.b);
  114. }
  115. rgb_state.index += 4;
  116. if (rgb_state.index == 30) {
  117. rgb_state.dir = !rgb_state.dir;
  118. rgb_state.count--;
  119. }
  120. } else {
  121. // right to left
  122. for (int i = rgb_state.index-4; i < rgb_state.index; i++) {
  123. IS31FL3731_set_color(i, led.r, led.g, led.b);
  124. }
  125. rgb_state.index -= 4;
  126. if (rgb_state.index == 2) {
  127. rgb_state.dir = !rgb_state.dir;
  128. rgb_state.count--;
  129. }
  130. }
  131. if (rgb_state.count == 0) {
  132. // move to stage 3
  133. rgb_state.testing = ST_STAGE_3;
  134. rgb_state.index = 0;
  135. rgb_state.delay = ST_STAGE_DELAY;
  136. rgb_state.duration = ST_END_DURATION;
  137. }
  138. }
  139. break;
  140. case ST_STAGE_3:
  141. if (rgb_state.index != 16) {
  142. IS31FL3731_set_color_all(0, 0, 0);
  143. }
  144. // light left and right
  145. if (rgb_state.index == 16) {
  146. if (rgb_state.duration) {
  147. rgb_state.duration--;
  148. } else {
  149. if (IS_HOST_LED_ON(USB_LED_CAPS_LOCK)) {
  150. rgb_state.state = CAPS_ALERT;
  151. } else {
  152. rgb_state.state = NORMAL;
  153. rgblight_set();
  154. }
  155. }
  156. } else {
  157. // left
  158. for (int i = 0; i < rgb_state.index+1; i++) {
  159. IS31FL3731_set_color(i, led.r, led.g, led.b);
  160. }
  161. // right
  162. for (int i = 31; i > 31-rgb_state.index-1; i--) {
  163. IS31FL3731_set_color(i, led.r, led.g, led.b);
  164. }
  165. rgb_state.index ++;
  166. }
  167. break;
  168. }
  169. update_ticks();
  170. }
  171. const is31_led __flash g_is31_leds[DRIVER_LED_TOTAL] = {
  172. /* Refer to IS31 manual for these locations
  173. * driver
  174. * | R location
  175. * | | G location
  176. * | | | B location
  177. * | | | | */
  178. // left CA
  179. {0, C1_1, C3_2, C4_2},
  180. {0, C1_2, C2_2, C4_3},
  181. {0, C1_3, C2_3, C3_3},
  182. {0, C1_4, C2_4, C3_4},
  183. {0, C1_5, C2_5, C3_5},
  184. {0, C1_6, C2_6, C3_6},
  185. {0, C1_7, C2_7, C3_7},
  186. {0, C1_8, C2_8, C3_8},
  187. {0, C9_1, C8_1, C7_1},
  188. {0, C9_2, C8_2, C7_2},
  189. {0, C9_3, C8_3, C7_3},
  190. {0, C9_4, C8_4, C7_4},
  191. {0, C9_5, C8_5, C7_5},
  192. {0, C9_6, C8_6, C7_6},
  193. {0, C9_7, C8_7, C6_6},
  194. {0, C9_8, C7_7, C6_7},
  195. // left CB
  196. {0, C1_9, C3_10, C4_10},
  197. {0, C1_10, C2_10, C4_11},
  198. {0, C1_11, C2_11, C3_11},
  199. {0, C1_12, C2_12, C3_12},
  200. {0, C1_13, C2_13, C3_13},
  201. {0, C1_14, C2_14, C3_14},
  202. {0, C1_15, C2_15, C3_15},
  203. {0, C1_16, C2_16, C3_16},
  204. {0, C9_9, C8_9, C7_9},
  205. {0, C9_10, C8_10, C7_10},
  206. {0, C9_11, C8_11, C7_11},
  207. {0, C9_12, C8_12, C7_12},
  208. {0, C9_13, C8_13, C7_13},
  209. {0, C9_14, C8_14, C7_14},
  210. {0, C9_15, C8_15, C6_14},
  211. {0, C9_16, C7_15, C6_15},
  212. };
  213. #endif
  214. void matrix_init_kb(void)
  215. {
  216. // clear caps led
  217. setPinOutput(CAPS_PIN);
  218. writePinLow(CAPS_PIN);
  219. #ifdef RGBLIGHT_ENABLE
  220. i2c_init();
  221. IS31FL3731_init(DRIVER_ADDR_1);
  222. for (int index = 0; index < DRIVER_LED_TOTAL; index++) {
  223. IS31FL3731_set_led_control_register(index, true, true, true);
  224. }
  225. IS31FL3731_update_led_control_registers(DRIVER_ADDR_1, 0);
  226. update_ticks();
  227. #endif
  228. matrix_init_user();
  229. }
  230. #ifdef RGBLIGHT_ENABLE
  231. void housekeeping_task_kb(void)
  232. {
  233. if (rgb_state.state == SELF_TESTING) {
  234. self_testing();
  235. } else if (rgb_state.state == CAPS_ALERT) {
  236. //gold 0xFF, 0xD9, 0x00
  237. LED_TYPE led = {
  238. .r = 0xFF,
  239. .g = 0xD9,
  240. .b = 0x00,
  241. };
  242. if (rgb_state.alert) {
  243. IS31FL3731_set_color_all(led.r, led.g, led.b);
  244. ws2812_setleds(&led, 1);
  245. } else {
  246. led.r = 0;
  247. led.g = 0;
  248. led.b = 0;
  249. IS31FL3731_set_color_all(0, 0, 0);
  250. ws2812_setleds(&led, 1);
  251. }
  252. if (timer_elapsed(rgb_state.ticks) > ALERT_INTERVAL) {
  253. rgb_state.alert = !rgb_state.alert;
  254. update_ticks();
  255. }
  256. }
  257. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_1,0);
  258. housekeeping_task_user();
  259. }
  260. void rgblight_call_driver(LED_TYPE *start_led, uint8_t num_leds)
  261. {
  262. if (rgb_state.state != NORMAL) return;
  263. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  264. IS31FL3731_set_color(i, start_led[i].r, start_led[i].g, start_led[i].b);
  265. }
  266. ws2812_setleds(start_led+DRIVER_LED_TOTAL, 1);
  267. }
  268. #endif
  269. bool led_update_kb(led_t led_state)
  270. {
  271. bool res = led_update_user(led_state);
  272. if (res) {
  273. writePin(CAPS_PIN, led_state.caps_lock);
  274. #ifdef RGBLIGHT_ENABLE
  275. if (rgb_state.state != SELF_TESTING) {
  276. if (led_state.caps_lock) {
  277. rgb_state.state = CAPS_ALERT;
  278. update_ticks();
  279. } else {
  280. rgb_state.state = NORMAL;
  281. rgblight_set();
  282. }
  283. }
  284. #endif
  285. }
  286. return res;
  287. }