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.

382 lines
10 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 500
  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. // led index
  42. #define ST_LEFT_BEGIN 0
  43. #ifdef DRIVER_ADDR_2
  44. #define ST_LEFT_SIZE 4
  45. #else
  46. #define ST_LEFT_SIZE 2
  47. #endif
  48. #define ST_LEFT_END (ST_LEFT_BEGIN+ST_LEFT_SIZE-1)
  49. #ifdef DRIVER_ADDR_2
  50. #define ST_RIGHT_BEGIN 60
  51. #else
  52. #define ST_RIGHT_BEGIN 30
  53. #endif
  54. #ifdef DRIVER_ADDR_2
  55. #define ST_RIGHT_SIZE 4
  56. #else
  57. #define ST_RIGHT_SIZE 2
  58. #endif
  59. #define ST_RIGHT_END (ST_RIGHT_BEGIN+ST_RIGHT_SIZE-1)
  60. #ifdef RGBLIGHT_ENABLE
  61. extern rgblight_config_t rgblight_config;
  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 hsv;
  93. hsv.h = rgblight_config.hue;
  94. hsv.s = rgblight_config.sat;
  95. hsv.v = rgblight_config.val;
  96. RGB led = hsv_to_rgb(hsv);
  97. switch(rgb_state.testing) {
  98. case ST_STAGE_1:
  99. if (rgb_state.index !=0 ) {
  100. IS31FL3731_set_color_all(0, 0, 0);
  101. }
  102. if (rgb_state.index >= ST_LEFT_END) {
  103. for (int i = rgb_state.index - 1; i < DRIVER_LED_TOTAL - rgb_state.index + 1; i++) {
  104. IS31FL3731_set_color(i, led.r, led.g, led.b);
  105. }
  106. if (rgb_state.index == ST_LEFT_END) {
  107. rgb_state.index = ST_LEFT_BEGIN;
  108. } else {
  109. rgb_state.index -= ST_LEFT_SIZE;
  110. }
  111. } else{
  112. if (rgb_state.delay > 0) {
  113. rgb_state.delay--;
  114. } else {
  115. // move to stage 2
  116. rgb_state.index = ST_LEFT_BEGIN+ST_LEFT_SIZE;
  117. rgb_state.testing = ST_STAGE_2;
  118. }
  119. }
  120. break;
  121. case ST_STAGE_2: {
  122. // clear all
  123. IS31FL3731_set_color_all(0, 0, 0);
  124. int i = 0;
  125. // light left and right
  126. for (i = 0; i < ST_LEFT_SIZE; i++) {
  127. IS31FL3731_set_color(ST_LEFT_BEGIN+i, led.r, led.g, led.b);
  128. }
  129. for (i = 0; i < ST_RIGHT_SIZE; i++) {
  130. IS31FL3731_set_color(ST_RIGHT_BEGIN+i, led.r, led.g, led.b);
  131. }
  132. if (rgb_state.dir) {
  133. // left to right
  134. for (int i = rgb_state.index; i < rgb_state.index+ST_LEFT_SIZE+ST_RIGHT_SIZE; i++) {
  135. IS31FL3731_set_color(i, led.r, led.g, led.b);
  136. }
  137. rgb_state.index += ST_LEFT_SIZE+ST_RIGHT_SIZE;
  138. if (rgb_state.index == ST_RIGHT_BEGIN) {
  139. rgb_state.dir = !rgb_state.dir;
  140. rgb_state.count--;
  141. }
  142. } else {
  143. // right to left
  144. for (int i = rgb_state.index - ST_RIGHT_SIZE; i < rgb_state.index; i++) {
  145. IS31FL3731_set_color(i, led.r, led.g, led.b);
  146. }
  147. rgb_state.index -= ST_LEFT_SIZE + ST_RIGHT_SIZE;
  148. if (rgb_state.index == ST_LEFT_BEGIN+ST_LEFT_SIZE) {
  149. rgb_state.dir = !rgb_state.dir;
  150. rgb_state.count--;
  151. }
  152. }
  153. if (rgb_state.count == 0) {
  154. // move to stage 3
  155. rgb_state.testing = ST_STAGE_3;
  156. rgb_state.index = 0;
  157. rgb_state.delay = ST_STAGE_DELAY;
  158. rgb_state.duration = ST_END_DURATION;
  159. }
  160. }
  161. break;
  162. case ST_STAGE_3:
  163. if (rgb_state.index != DRIVER_LED_TOTAL/2) {
  164. IS31FL3731_set_color_all(0, 0, 0);
  165. }
  166. // light left and right
  167. if (rgb_state.index == DRIVER_LED_TOTAL/2) {
  168. if (rgb_state.duration) {
  169. rgb_state.duration--;
  170. } else {
  171. if (IS_HOST_LED_ON(USB_LED_CAPS_LOCK)) {
  172. rgb_state.state = CAPS_ALERT;
  173. } else {
  174. rgb_state.state = NORMAL;
  175. rgblight_set();
  176. }
  177. }
  178. } else {
  179. // left
  180. for (int i = 0; i < rgb_state.index+1; i++) {
  181. IS31FL3731_set_color(i, led.r, led.g, led.b);
  182. }
  183. // right
  184. for (int i = ST_RIGHT_END; i > ST_RIGHT_END - rgb_state.index - 1; i--) {
  185. IS31FL3731_set_color(i, led.r, led.g, led.b);
  186. }
  187. rgb_state.index ++;
  188. }
  189. break;
  190. }
  191. update_ticks();
  192. }
  193. const is31_led PROGMEM g_is31_leds[DRIVER_LED_TOTAL] = {
  194. /* Refer to IS31 manual for these locations
  195. * driver
  196. * | R location
  197. * | | G location
  198. * | | | B location
  199. * | | | | */
  200. // left CA
  201. {0, C1_1, C3_2, C4_2},
  202. {0, C1_2, C2_2, C4_3},
  203. {0, C1_3, C2_3, C3_3},
  204. {0, C1_4, C2_4, C3_4},
  205. {0, C1_5, C2_5, C3_5},
  206. {0, C1_6, C2_6, C3_6},
  207. {0, C1_7, C2_7, C3_7},
  208. {0, C1_8, C2_8, C3_8},
  209. {0, C9_1, C8_1, C7_1},
  210. {0, C9_2, C8_2, C7_2},
  211. {0, C9_3, C8_3, C7_3},
  212. {0, C9_4, C8_4, C7_4},
  213. {0, C9_5, C8_5, C7_5},
  214. {0, C9_6, C8_6, C7_6},
  215. {0, C9_7, C8_7, C6_6},
  216. {0, C9_8, C7_7, C6_7},
  217. // left CB
  218. {0, C1_9, C3_10, C4_10},
  219. {0, C1_10, C2_10, C4_11},
  220. {0, C1_11, C2_11, C3_11},
  221. {0, C1_12, C2_12, C3_12},
  222. {0, C1_13, C2_13, C3_13},
  223. {0, C1_14, C2_14, C3_14},
  224. {0, C1_15, C2_15, C3_15},
  225. {0, C1_16, C2_16, C3_16},
  226. {0, C9_9, C8_9, C7_9},
  227. {0, C9_10, C8_10, C7_10},
  228. {0, C9_11, C8_11, C7_11},
  229. {0, C9_12, C8_12, C7_12},
  230. {0, C9_13, C8_13, C7_13},
  231. {0, C9_14, C8_14, C7_14},
  232. {0, C9_15, C8_15, C6_14},
  233. {0, C9_16, C7_15, C6_15},
  234. // right CA
  235. {1, C1_1, C3_2, C4_2},
  236. {1, C1_2, C2_2, C4_3},
  237. {1, C1_3, C2_3, C3_3},
  238. {1, C1_4, C2_4, C3_4},
  239. {1, C1_5, C2_5, C3_5},
  240. {1, C1_6, C2_6, C3_6},
  241. {1, C1_7, C2_7, C3_7},
  242. {1, C1_8, C2_8, C3_8},
  243. {1, C9_1, C8_1, C7_1},
  244. {1, C9_2, C8_2, C7_2},
  245. {1, C9_3, C8_3, C7_3},
  246. {1, C9_4, C8_4, C7_4},
  247. {1, C9_5, C8_5, C7_5},
  248. {1, C9_6, C8_6, C7_6},
  249. {1, C9_7, C8_7, C6_6},
  250. {1, C9_8, C7_7, C6_7},
  251. // right CB
  252. {1, C1_9, C3_10, C4_10},
  253. {1, C1_10, C2_10, C4_11},
  254. {1, C1_11, C2_11, C3_11},
  255. {1, C1_12, C2_12, C3_12},
  256. {1, C1_13, C2_13, C3_13},
  257. {1, C1_14, C2_14, C3_14},
  258. {1, C1_15, C2_15, C3_15},
  259. {1, C1_16, C2_16, C3_16},
  260. {1, C9_9, C8_9, C7_9},
  261. {1, C9_10, C8_10, C7_10},
  262. {1, C9_11, C8_11, C7_11},
  263. {1, C9_12, C8_12, C7_12},
  264. {1, C9_13, C8_13, C7_13},
  265. {1, C9_14, C8_14, C7_14},
  266. {1, C9_15, C8_15, C6_14},
  267. {1, C9_16, C7_15, C6_15},
  268. };
  269. void matrix_init_kb(void)
  270. {
  271. setPinOutput(LED_CAPS_LOCK_PIN);
  272. writePinLow(LED_CAPS_LOCK_PIN);
  273. i2c_init();
  274. IS31FL3731_init(DRIVER_ADDR_1);
  275. #ifdef DRIVER_ADDR_2
  276. IS31FL3731_init(DRIVER_ADDR_2);
  277. #endif
  278. for (int index = 0; index < DRIVER_LED_TOTAL; index++) {
  279. IS31FL3731_set_led_control_register(index, true, true, true);
  280. }
  281. IS31FL3731_update_led_control_registers(DRIVER_ADDR_1, 0);
  282. #ifdef DRIVER_ADDR_2
  283. IS31FL3731_update_led_control_registers(DRIVER_ADDR_2, 1);
  284. #endif
  285. update_ticks();
  286. matrix_init_user();
  287. }
  288. void housekeeping_task_kb(void)
  289. {
  290. if (rgb_state.state == SELF_TESTING) {
  291. self_testing();
  292. } else if (rgb_state.state == CAPS_ALERT) {
  293. //gold 0xFF, 0xD9, 0x00
  294. LED_TYPE led = {
  295. .r = 0xFF,
  296. //.g = 0xD9,
  297. .g = 0xA5,
  298. .b = 0x00,
  299. };
  300. if (rgb_state.alert) {
  301. IS31FL3731_set_color_all(led.r, led.g, led.b);
  302. ws2812_setleds(&led, 1);
  303. } else {
  304. led.r = 0;
  305. led.g = 0;
  306. led.b = 0;
  307. IS31FL3731_set_color_all(0, 0, 0);
  308. ws2812_setleds(&led, 1);
  309. }
  310. if (timer_elapsed(rgb_state.ticks) > ALERT_INTERVAL) {
  311. rgb_state.alert = !rgb_state.alert;
  312. update_ticks();
  313. }
  314. }
  315. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_1, 0);
  316. #ifdef DRIVER_ADDR_2
  317. IS31FL3731_update_pwm_buffers(DRIVER_ADDR_2, 1);
  318. #endif
  319. housekeeping_task_user();
  320. }
  321. void rgblight_call_driver(LED_TYPE *start_led, uint8_t num_leds)
  322. {
  323. if (rgb_state.state != NORMAL) return;
  324. for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
  325. IS31FL3731_set_color(i, start_led[i].r, start_led[i].g, start_led[i].b);
  326. }
  327. ws2812_setleds(start_led+DRIVER_LED_TOTAL, 1);
  328. }
  329. bool led_update_kb(led_t led_state)
  330. {
  331. bool res = led_update_user(led_state);
  332. if (res) {
  333. writePin(LED_CAPS_LOCK_PIN, led_state.caps_lock);
  334. if (rgb_state.state != SELF_TESTING) {
  335. if (led_state.caps_lock) {
  336. rgb_state.state = CAPS_ALERT;
  337. update_ticks();
  338. } else {
  339. rgb_state.state = NORMAL;
  340. rgblight_set();
  341. }
  342. }
  343. }
  344. return res;
  345. }
  346. #endif