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.

117 lines
3.8 KiB

  1. /* Copyright 2017 MechMerlin <mechmerlin@gmail.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  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. #include "indicator_leds.h"
  18. enum BACKLIGHT_AREAS {
  19. BACKLIGHT_ALPHA = 0b0000001,
  20. BACKLIGHT_EXTRA = 0b0000010,
  21. BACKLIGHT_MODNUM = 0b0000100,
  22. BACKLIGHT_FROW = 0b0001000,
  23. BACKLIGHT_RGB = 0b0010000,
  24. BACKLIGHT_SWITCH = 0b0001111
  25. };
  26. uint8_t backlight_rgb_r = 255;
  27. uint8_t backlight_rgb_g = 0;
  28. uint8_t backlight_rgb_b = 0;
  29. uint8_t backlight_os_state = 0;
  30. uint32_t backlight_layer_state = 0;
  31. void backlight_toggle_rgb(bool enabled)
  32. {
  33. if(enabled) {
  34. uint8_t rgb[17][3] = {
  35. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  36. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  37. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  38. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  39. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  40. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  41. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  42. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  43. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  44. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  45. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  46. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  47. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  48. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  49. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  50. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
  51. {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b}
  52. };
  53. backlight_set_rgb(rgb);
  54. } else {
  55. uint8_t rgb[17][3] = {
  56. {0, 0, 0},
  57. {0, 0, 0},
  58. {0, 0, 0},
  59. {0, 0, 0},
  60. {0, 0, 0},
  61. {0, 0, 0},
  62. {0, 0, 0},
  63. {0, 0, 0},
  64. {0, 0, 0},
  65. {0, 0, 0},
  66. {0, 0, 0},
  67. {0, 0, 0},
  68. {0, 0, 0},
  69. {0, 0, 0},
  70. {0, 0, 0},
  71. {0, 0, 0},
  72. {0, 0, 0}
  73. };
  74. backlight_set_rgb(rgb);
  75. }
  76. }
  77. void backlight_set_rgb(uint8_t cfg[17][3])
  78. {
  79. cli();
  80. for(uint8_t i = 0; i < 17; ++i) {
  81. send_color(cfg[i][0], cfg[i][1], cfg[i][2], Device_PCBRGB);
  82. }
  83. sei();
  84. show();
  85. }
  86. void backlight_set(uint8_t level) {
  87. level & BACKLIGHT_ALPHA ? (PORTB |= 0b00000010) : (PORTB &= ~0b00000010);
  88. level & BACKLIGHT_EXTRA ? (PORTB |= 0b00000100) : (PORTB &= ~0b00000100);
  89. level & BACKLIGHT_MODNUM ? (PORTB |= 0b00001000) : (PORTB &= ~0b00001000);
  90. level & BACKLIGHT_FROW ? (PORTE |= 0b01000000) : (PORTE &= ~0b01000000);
  91. level & BACKLIGHT_RGB ? backlight_toggle_rgb(true) : backlight_toggle_rgb(false);
  92. }
  93. // Port from backlight_update_state
  94. bool led_update_kb(led_t led_state) {
  95. bool res = led_update_user(led_state);
  96. if(res) {
  97. bool status[7] = {
  98. backlight_os_state & 2,
  99. backlight_os_state & 4,
  100. backlight_os_state & 1,
  101. backlight_layer_state & (1<<1),
  102. backlight_layer_state & (1<<2),
  103. backlight_layer_state & (1<<3),
  104. backlight_layer_state & (1<<4)
  105. };
  106. indicator_leds_set(status);
  107. backlight_os_state & 2 ? (PORTB &= ~0b00000001) : (PORTB |= 0b00000001);
  108. backlight_os_state & 4 ? (PORTB &= ~0b00010000) : (PORTB |= 0b00010000);
  109. }
  110. return res;
  111. }