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.

194 lines
4.0 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 V60_POLESTAR
  20. #include "rgblight.h"
  21. #include <avr/pgmspace.h>
  22. #include "action_layer.h"
  23. #define SOFTPWM_LED_TIMER_TOP F_CPU/(256*64)
  24. extern rgblight_config_t rgblight_config;
  25. static uint8_t softpwm_buff[3] = {0};
  26. void matrix_init_kb(void) {
  27. rgb_init();
  28. matrix_init_user();
  29. }
  30. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  31. uint8_t r = led[0].r, g = led[0].g, b = led[0].b;
  32. switch(keycode) {
  33. case RGB_RI:
  34. if (record->event.pressed) {
  35. r += RGB_STEP;
  36. if (r < led[0].r) {
  37. r = 255;
  38. }
  39. rgblight_setrgb(r, g, b);
  40. }
  41. return false;
  42. case RGB_RD:
  43. if (record->event.pressed) {
  44. r -= RGB_STEP;
  45. if (r > led[0].r) {
  46. r = 0;
  47. }
  48. rgblight_setrgb(r, g, b);
  49. }
  50. return false;
  51. case RGB_BI:
  52. if (record->event.pressed) {
  53. b += RGB_STEP;
  54. if (b < led[0].b) {
  55. b = 255;
  56. }
  57. rgblight_setrgb(r, g, b);
  58. }
  59. return false;
  60. case RGB_BD:
  61. if (record->event.pressed) {
  62. b -= RGB_STEP;
  63. if (b > led[0].b) {
  64. b = 0;
  65. }
  66. rgblight_setrgb(r, g, b);
  67. }
  68. return false;
  69. case RGB_GI:
  70. if (record->event.pressed) {
  71. g += RGB_STEP;
  72. if (g < led[0].g) {
  73. g = 255;
  74. }
  75. rgblight_setrgb(r, g, b);
  76. }
  77. return false;
  78. case RGB_GD:
  79. if (record->event.pressed) {
  80. g -= RGB_STEP;
  81. if (g > led[0].g) {
  82. g = 0;
  83. }
  84. rgblight_setrgb(r, g, b);
  85. }
  86. return false;
  87. }
  88. return process_record_user(keycode, record);
  89. }
  90. void rgb_timer_init(void) {
  91. /* Timer1 setup */
  92. /* CTC mode */
  93. TCCR1B |= (1<<WGM12);
  94. /* Clock selelct: clk/8 */
  95. TCCR1B |= (1<<CS10);
  96. /* Set TOP value */
  97. uint8_t sreg = SREG;
  98. cli();
  99. OCR1AH = (SOFTPWM_LED_TIMER_TOP >> 8) & 0xff;
  100. OCR1AL = SOFTPWM_LED_TIMER_TOP & 0xff;
  101. SREG = sreg;
  102. // Enable the compare match interrupt on timer 1
  103. TIMSK1 |= (1<<OCIE1A);
  104. }
  105. void rgb_init(void) {
  106. DDRF |= (1<<PF6 | 1<<PF5 | 1<<PF4);
  107. PORTF |= (1<<PF6 | 1<<PF5 | 1<<PF4);
  108. rgb_timer_init();
  109. }
  110. void set_rgb_pin_on(uint8_t pin) {
  111. PORTF &= ~(1<<pin);
  112. }
  113. void set_rgb_pin_off(uint8_t pin) {
  114. PORTF |= (1<<pin);
  115. }
  116. void rgblight_set(void) {
  117. // xprintf("Setting RGB underglow\n");
  118. if (!rgblight_config.enable) {
  119. led[0].r = 0;
  120. led[0].g = 0;
  121. led[0].b = 0;
  122. set_rgb_pin_off(RGB_RED_PIN);
  123. set_rgb_pin_off(RGB_GREEN_PIN);
  124. set_rgb_pin_off(RGB_BLUE_PIN);
  125. }
  126. // //xprintf("Red: %u, Green: %u, Blue: %u\n", led[0].r, led[0].g, led[0].b);
  127. }
  128. ISR(TIMER1_COMPA_vect)
  129. {
  130. static uint8_t pwm = 0;
  131. pwm++;
  132. // turn the LEDS on
  133. if (pwm == 0) {
  134. if (softpwm_buff[0]) {
  135. set_rgb_pin_on(RGB_RED_PIN);
  136. softpwm_buff[0] = led[0].r;
  137. }
  138. if (softpwm_buff[1]) {
  139. set_rgb_pin_on(RGB_GREEN_PIN);
  140. softpwm_buff[1] = led[0].g;
  141. }
  142. if (softpwm_buff[2]) {
  143. set_rgb_pin_on(RGB_BLUE_PIN);
  144. softpwm_buff[2] = led[0].b;
  145. }
  146. }
  147. // turn em off
  148. if (pwm == softpwm_buff[0]) {
  149. set_rgb_pin_off(RGB_RED_PIN);
  150. softpwm_buff[0] = led[0].r;
  151. }
  152. if (pwm == softpwm_buff[1]) {
  153. set_rgb_pin_off(RGB_GREEN_PIN);
  154. softpwm_buff[1] = led[0].g;
  155. }
  156. if (pwm == softpwm_buff[2]) {
  157. set_rgb_pin_off(RGB_BLUE_PIN);
  158. softpwm_buff[2] = led[0].b;
  159. }
  160. }
  161. #endif // V60_POLESTAR