Peter's keyboard firmware (QMK) https://qmk.fm/
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.

lights.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "lights.h"
  2. static bool active_key_pos[50] = {};
  3. static uint8_t led_dim = 0;
  4. volatile led_key rbw_led_keys[RBW] = {
  5. [RBW_LCTL] = {DEFAULT, 21, true}, [RBW_LCAP] = {DEFAULT, 24, false},
  6. [RBW_LSPR] = {DEFAULT, 23, true}, [RBW_RCTL] = {DEFAULT, 48, true},
  7. [RBW_RCAP] = {DEFAULT, 45, false}, [RBW_RALT] = {DEFAULT, 46, true},
  8. [RBW_SCRL] = {DEFAULT, 42, true}};
  9. /* Pressed led color. */
  10. const uint32_t _PC[3] = {0xFF, 0x00, 0x00};
  11. /* Layer color. */
  12. const uint8_t _LC[5][3] = {[_BL] = {0x00, 0x00, 0x00},
  13. [_UL] = {0x00, 0x00, 0xFF},
  14. [_VL] = {0xFF, 0xFF, 0x00},
  15. [_DL] = {0x00, 0xFF, 0x00},
  16. [_AL] = {0xFF, 0x00, 0x00}};
  17. /* Color table by sine wave */
  18. const uint8_t _LIGHTS[360] = {
  19. 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8,
  20. 11, 12, 9, 13, 15, 17, 18, 20, 22, 24, 26, 28, 30, 32, 35,
  21. 37, 39, 42, 44, 47, 49, 52, 55, 58, 60, 63, 66, 69, 72, 75,
  22. 78, 81, 85, 88, 91, 94, 97, 101, 104, 107, 111, 114, 117, 121, 124,
  23. 127, 131, 134, 137, 141, 144, 147, 150, 154, 157, 160, 163, 167, 170, 173,
  24. 176, 179, 182, 185, 188, 191, 194, 197, 200, 202, 205, 208, 210, 213, 215,
  25. 217, 220, 222, 224, 226, 229, 231, 232, 234, 236, 238, 239, 241, 242, 244,
  26. 245, 246, 248, 249, 250, 251, 251, 252, 253, 253, 254, 254, 255, 255, 255,
  27. 255, 255, 255, 255, 254, 254, 253, 253, 252, 251, 251, 250, 249, 248, 246,
  28. 245, 244, 242, 241, 239, 238, 236, 234, 232, 231, 229, 226, 224, 222, 220,
  29. 217, 215, 213, 210, 208, 205, 202, 200, 197, 194, 191, 188, 185, 182, 179,
  30. 176, 173, 170, 167, 163, 160, 157, 154, 150, 147, 144, 141, 137, 134, 131,
  31. 127, 124, 121, 117, 114, 111, 107, 104, 101, 97, 94, 91, 88, 85, 81,
  32. 78, 75, 72, 69, 66, 63, 60, 58, 55, 52, 49, 47, 44, 42, 39,
  33. 37, 35, 32, 30, 28, 26, 24, 22, 20, 18, 17, 15, 13, 12, 11,
  34. 9, 8, 7, 6, 5, 4, 3, 2, 2, 1, 1, 0, 0, 0, 0,
  35. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  36. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  37. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  38. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  39. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  40. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  43. void set_key_led(keyrecord_t *record, uint8_t lyr) {
  44. static uint8_t base = 5;
  45. uint8_t r = record->event.key.row;
  46. uint8_t c = record->event.key.col;
  47. uint8_t pos;
  48. // This was the result of my soldering.
  49. // Lesson of the day: always check.
  50. if (r < 5) {
  51. pos = r % 2 == 0 ? r * base + c : r * base + (base - (c + 1));
  52. } else {
  53. pos = r % 2 == 0 ? r * base + (base - (c + 1)) : r * base + c;
  54. }
  55. if (record->event.pressed) {
  56. active_key_pos[pos] = true;
  57. SET_LED_RGB(_PC[0], _PC[1], _PC[2], led_dim, pos);
  58. } else {
  59. active_key_pos[pos] = false;
  60. SET_LED_RGB(_LC[lyr][0], _LC[lyr][1], _LC[lyr][2], led_dim, pos);
  61. }
  62. }
  63. /* Fix for unknown reason after testin flash the eeprom handedness,
  64. the leds will ligth up after boot.
  65. This should be call in the set layer led.*/
  66. bool is_first_run(void) {
  67. static uint8_t run = 0;
  68. if (run == 0) {
  69. run++;
  70. return true;
  71. } else {
  72. return false;
  73. }
  74. }
  75. bool set_layer_led(uint8_t lyr) {
  76. static uint8_t p_lyr = 0; // Previous layer.
  77. static uint8_t p_dim = 0; // Previous dim.
  78. if (p_lyr == lyr && p_dim == led_dim && !is_first_run()) {
  79. return false;
  80. }
  81. p_lyr = lyr;
  82. p_dim = led_dim;
  83. const uint8_t r = _LC[lyr][0];
  84. const uint8_t g = _LC[lyr][1];
  85. const uint8_t b = _LC[lyr][2];
  86. const uint8_t d = (p_lyr == _VL && p_dim < 1) ? 1 : p_dim;
  87. for (uint8_t i = 0; i < RGBLED_NUM; i++) {
  88. SET_LED_RGB(r, g, b, d, i);
  89. }
  90. return true;
  91. }
  92. bool rainbow_loop(uint8_t lyr) {
  93. static uint16_t last_timer = 0;
  94. static uint16_t i = 0;
  95. static uint8_t r, g, b, pos;
  96. if (timer_elapsed(last_timer) < 8) {
  97. return false;
  98. }
  99. if (i >= 360) {
  100. i = 0;
  101. }
  102. last_timer = timer_read();
  103. r = _LIGHTS[(i + 120) % 360];
  104. g = _LIGHTS[i];
  105. b = _LIGHTS[(i + 240) % 360];
  106. i++;
  107. bool set_rbw = false;
  108. for (uint8_t j = 0; j < RBW; j++) {
  109. pos = rbw_led_keys[j].pos;
  110. switch (rbw_led_keys[j].status) {
  111. case ENABLED:
  112. if (!active_key_pos[pos] || rbw_led_keys[j].forced) {
  113. SET_LED_RGB(r, g, b, led_dim, pos);
  114. set_rbw = true;
  115. }
  116. break;
  117. case DISABLED:
  118. if (!active_key_pos[pos] || rbw_led_keys[j].forced) {
  119. SET_LED_RGB(_LC[lyr][0], _LC[lyr][1], _LC[lyr][2], led_dim, pos);
  120. set_rbw = true;
  121. }
  122. rbw_led_keys[j].status = DEFAULT;
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. return set_rbw;
  129. }
  130. bool led_brightness(uint16_t keycode, keyrecord_t *record) {
  131. switch (keycode) {
  132. case RGUP:
  133. if (record->event.pressed && led_dim > 0) {
  134. led_dim--;
  135. eeprom_write_byte(EECONFIG_LED_DIM_LVL, led_dim);
  136. }
  137. return true;
  138. break;
  139. case RGDWN:
  140. if (record->event.pressed && led_dim < 8) {
  141. led_dim++;
  142. eeprom_write_byte(EECONFIG_LED_DIM_LVL, led_dim);
  143. }
  144. return true;
  145. break;
  146. default:
  147. return false;
  148. break;
  149. }
  150. }
  151. void eeprom_read_led_dim_lvl(void) {
  152. led_dim = eeprom_read_byte(EECONFIG_LED_DIM_LVL);
  153. if (led_dim > 8 || led_dim < 0) {
  154. led_dim = 0;
  155. eeprom_write_byte(EECONFIG_LED_DIM_LVL, led_dim);
  156. }
  157. }