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.

174 lines
3.5 KiB

  1. /* Copyright 2017 benlyall, MechMerlin
  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 "v60_type_r.h"
  17. #include "quantum.h"
  18. // if we've got an RGB underglow!
  19. #ifdef RGBLIGHT_ENABLE
  20. #define SOFTPWM_LED_TIMER_TOP F_CPU/(256*64)
  21. extern rgblight_config_t rgblight_config;
  22. static uint8_t softpwm_buff[3] = {0};
  23. void matrix_init_kb(void) {
  24. rgb_init();
  25. matrix_init_user();
  26. }
  27. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  28. uint8_t r = led[0].r, g = led[0].g, b = led[0].b;
  29. switch(keycode) {
  30. case RGB_RI:
  31. if (record->event.pressed) {
  32. r += RGB_STEP;
  33. if (r < led[0].r) {
  34. r = 255;
  35. }
  36. rgblight_setrgb(r, g, b);
  37. }
  38. return false;
  39. case RGB_RD:
  40. if (record->event.pressed) {
  41. r -= RGB_STEP;
  42. if (r > led[0].r) {
  43. r = 0;
  44. }
  45. rgblight_setrgb(r, g, b);
  46. }
  47. return false;
  48. case RGB_BI:
  49. if (record->event.pressed) {
  50. b += RGB_STEP;
  51. if (b < led[0].b) {
  52. b = 255;
  53. }
  54. rgblight_setrgb(r, g, b);
  55. }
  56. return false;
  57. case RGB_BD:
  58. if (record->event.pressed) {
  59. b -= RGB_STEP;
  60. if (b > led[0].b) {
  61. b = 0;
  62. }
  63. rgblight_setrgb(r, g, b);
  64. }
  65. return false;
  66. case RGB_GI:
  67. if (record->event.pressed) {
  68. g += RGB_STEP;
  69. if (g < led[0].g) {
  70. g = 255;
  71. }
  72. rgblight_setrgb(r, g, b);
  73. }
  74. return false;
  75. case RGB_GD:
  76. if (record->event.pressed) {
  77. g -= RGB_STEP;
  78. if (g > led[0].g) {
  79. g = 0;
  80. }
  81. rgblight_setrgb(r, g, b);
  82. }
  83. return false;
  84. }
  85. return process_record_user(keycode, record);
  86. }
  87. void rgb_timer_init(void) {
  88. /* Timer3 setup */
  89. /* CTC mode */
  90. TCCR3B |= _BV(WGM32);
  91. /* Clock select: clk/8 */
  92. TCCR3B |= _BV(CS30);
  93. /* Set TOP value */
  94. uint8_t sreg = SREG;
  95. cli();
  96. OCR3AH = (SOFTPWM_LED_TIMER_TOP >> 8) & 0xFF;
  97. OCR3AL = SOFTPWM_LED_TIMER_TOP & 0xFF;
  98. SREG = sreg;
  99. // Enable the compare match interrupt on timer 3
  100. TIMSK3 |= _BV(OCIE3A);
  101. }
  102. void rgb_init(void) {
  103. DDRF |= (_BV(PF6) | _BV(PF5) | _BV(PF4));
  104. PORTF |= (_BV(PF6) | _BV(PF5) | _BV(PF4));
  105. rgb_timer_init();
  106. }
  107. void set_rgb_pin_on(uint8_t pin) {
  108. PORTF &= ~_BV(pin);
  109. }
  110. void set_rgb_pin_off(uint8_t pin) {
  111. PORTF |= _BV(pin);
  112. }
  113. ISR(TIMER3_COMPA_vect)
  114. {
  115. static uint8_t pwm = 0;
  116. pwm++;
  117. // turn the LEDS on
  118. if (pwm == 0) {
  119. if (softpwm_buff[0]) {
  120. set_rgb_pin_on(RGB_RED_PIN);
  121. softpwm_buff[0] = led[0].r;
  122. }
  123. if (softpwm_buff[1]) {
  124. set_rgb_pin_on(RGB_GREEN_PIN);
  125. softpwm_buff[1] = led[0].g;
  126. }
  127. if (softpwm_buff[2]) {
  128. set_rgb_pin_on(RGB_BLUE_PIN);
  129. softpwm_buff[2] = led[0].b;
  130. }
  131. }
  132. // turn em off
  133. if (pwm == softpwm_buff[0]) {
  134. set_rgb_pin_off(RGB_RED_PIN);
  135. softpwm_buff[0] = led[0].r;
  136. }
  137. if (pwm == softpwm_buff[1]) {
  138. set_rgb_pin_off(RGB_GREEN_PIN);
  139. softpwm_buff[1] = led[0].g;
  140. }
  141. if (pwm == softpwm_buff[2]) {
  142. set_rgb_pin_off(RGB_BLUE_PIN);
  143. softpwm_buff[2] = led[0].b;
  144. }
  145. }
  146. #endif // RGBLIGHT_ENABLE