]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/v60_type_r/v60_type_r.c
016348db9f7f582a1f2c4014ea2c16a07a3e8eaa
[qmk_firmware.git] / keyboards / v60_type_r / v60_type_r.c
1 /* Copyright 2017 REPLACE_WITH_YOUR_NAME
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
18 #include "rgblight.h"
19
20 #include <avr/pgmspace.h>
21
22 #include "action_layer.h"
23 #include "quantum.h"
24
25 // if we've got an RGB underglow!
26 #ifdef V60_POLESTAR
27 #define SOFTPWM_LED_TIMER_TOP F_CPU/(256*64)
28
29 extern rgblight_config_t rgblight_config;
30 static uint8_t softpwm_buff[3] = {0};
31
32 void matrix_init_user(void) {
33         rgb_init();
34 }
35
36 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
37         uint8_t r = led[0].r, g = led[0].g, b = led[0].b;
38         switch(keycode) {
39                 case RGB_RI:
40                         if (record->event.pressed) {
41                                 r += RGB_STEP;
42                                 if (r < led[0].r) {
43                                         r = 255;
44                                 }
45                                 rgblight_setrgb(r, g, b);
46                         }
47
48                         return false;
49                 case RGB_RD:
50                   if (record->event.pressed) {
51                         r -= RGB_STEP;
52                                 if (r > led[0].r) {
53                                         r = 0;
54                                 }
55                                 rgblight_setrgb(r, g, b);
56                   }
57
58                         return false;
59                 case RGB_BI:
60                         if (record->event.pressed) {
61                                 b += RGB_STEP;
62                                 if (b < led[0].b) {
63                                         b = 255;
64                                 }
65                                 rgblight_setrgb(r, g, b);
66                         }
67
68                         return false;
69                 case RGB_BD:
70                   if (record->event.pressed) {
71                         b -= RGB_STEP;
72                                 if (b > led[0].b) {
73                                         b = 0;
74                                 }
75                                 rgblight_setrgb(r, g, b);
76                   }
77
78                         return false;
79                 case RGB_GI:
80                         if (record->event.pressed) {
81                                 g += RGB_STEP;
82                                 if (g < led[0].g) {
83                                         g = 255;
84                                 }
85                                 rgblight_setrgb(r, g, b);
86                         }
87
88                         return false;
89                 case RGB_GD:
90                   if (record->event.pressed) {
91                         g -= RGB_STEP;
92                                 if (g > led[0].g) {
93                                         g = 0;
94                                 }
95                                 rgblight_setrgb(r, g, b);
96                         }
97
98                         return false;
99         }
100
101         return true;
102 }
103
104
105 void rgb_timer_init(void) {
106     /* Timer1 setup */
107     /* CTC mode */
108     TCCR1B |= (1<<WGM12);
109     /* Clock selelct: clk/8 */
110     TCCR1B |= (1<<CS10);
111     /* Set TOP value */
112     uint8_t sreg = SREG;
113     cli();
114     OCR1AH = (SOFTPWM_LED_TIMER_TOP >> 8) & 0xff;
115     OCR1AL = SOFTPWM_LED_TIMER_TOP & 0xff;
116     SREG = sreg;
117
118     // Enable the compare match interrupt on timer 1
119     TIMSK1 |= (1<<OCIE1A);
120 }
121
122 void rgb_init(void) {
123     DDRF  |=  (1<<PF6 | 1<<PF5 | 1<<PF4);
124     PORTF |=  (1<<PF6 | 1<<PF5 | 1<<PF4);
125
126     rgb_timer_init();
127 }
128
129 void set_rgb_pin_on(uint8_t pin) {
130         PORTF &= ~(1<<pin);
131 }
132
133 void set_rgb_pin_off(uint8_t pin) {
134         PORTF |= (1<<pin);
135 }
136
137 void rgblight_set(void) {
138           // xprintf("Setting RGB underglow\n");
139     if (!rgblight_config.enable) {
140           led[0].r = 0;
141           led[0].g = 0;
142           led[0].b = 0;
143           set_rgb_pin_off(RGB_RED_PIN);
144           set_rgb_pin_off(RGB_GREEN_PIN);
145           set_rgb_pin_off(RGB_BLUE_PIN);
146     }
147
148    //  //xprintf("Red: %u, Green: %u, Blue: %u\n", led[0].r, led[0].g, led[0].b);
149 }
150
151 ISR(TIMER1_COMPA_vect)
152 {
153     static uint8_t pwm = 0;
154     pwm++;
155
156     // turn the LEDS on
157     if (pwm == 0) {
158         if (softpwm_buff[0]) {
159                 set_rgb_pin_on(RGB_RED_PIN);
160                 softpwm_buff[0] = led[0].r;
161         }
162
163         if (softpwm_buff[1]) {
164                 set_rgb_pin_on(RGB_GREEN_PIN);
165                 softpwm_buff[1] = led[0].g;
166         }
167
168         if (softpwm_buff[2]) {
169                 set_rgb_pin_on(RGB_BLUE_PIN);
170                 softpwm_buff[2] = led[0].b;
171         }
172     }
173
174     // turn em off
175         if (pwm == softpwm_buff[0]) {
176                 set_rgb_pin_off(RGB_RED_PIN);
177                 softpwm_buff[0] = led[0].r;
178
179         }
180
181         if (pwm == softpwm_buff[1]) {
182                 set_rgb_pin_off(RGB_GREEN_PIN);
183         softpwm_buff[1] = led[0].g;
184         }
185
186         if (pwm == softpwm_buff[2]) {
187                 set_rgb_pin_off(RGB_BLUE_PIN);
188         softpwm_buff[2] = led[0].b;
189         }
190 }
191 #else
192
193 void matrix_init_user(void) {
194 }
195
196 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
197         return true;
198 }
199
200 #endif // V60_POLESTAR
201
202 // we need these functions for both versions
203 void led_set_kb(uint8_t usb_led) {
204         // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
205
206         led_set_user(usb_led);
207 }
208
209 void matrix_scan_user(void) {
210 }
211
212 void matrix_init_kb(void) {
213         // put your keyboard start-up code here
214         // runs once when the firmware starts up
215
216         matrix_init_user();
217 }
218
219 void matrix_scan_kb(void) {
220         // put your looping keyboard code here
221         // runs every cycle (a lot)
222
223         matrix_scan_user();
224 }
225
226 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
227         // put your per-action keyboard code here
228         // runs for every action, just before processing by the firmware
229
230         return process_record_user(keycode, record);
231 }