]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/octagon/v2/v2.c
Octagon V2 Refactor (#2170)
[qmk_firmware.git] / keyboards / octagon / v2 / v2.c
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 "v2.h"
17 #include "indicator_leds.h"
18
19 enum BACKLIGHT_AREAS {
20   BACKLIGHT_ALPHA    = 0b0000001,
21   BACKLIGHT_EXTRA    = 0b0000010,
22   BACKLIGHT_MODNUM   = 0b0000100,
23   BACKLIGHT_FROW     = 0b0001000,
24   BACKLIGHT_RGB      = 0b0010000,
25   BACKLIGHT_SWITCH   = 0b0001111
26 };
27
28 uint8_t backlight_rgb_r = 255;
29 uint8_t backlight_rgb_g = 0;
30 uint8_t backlight_rgb_b = 0;
31 uint8_t backlight_os_state = 0;
32 uint32_t backlight_layer_state = 0;
33
34 void backlight_toggle_rgb(bool enabled)
35 {
36   if(enabled) {
37     uint8_t rgb[17][3] = {
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       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
53       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b},
54       {backlight_rgb_r, backlight_rgb_g, backlight_rgb_b}
55     };
56     backlight_set_rgb(rgb);
57   } else {
58     uint8_t rgb[17][3] = {
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       {0, 0, 0},
74       {0, 0, 0},
75       {0, 0, 0}
76     };
77     backlight_set_rgb(rgb);
78   }
79 }
80
81 void backlight_set_rgb(uint8_t cfg[17][3])
82 {
83   cli();
84   for(uint8_t i = 0; i < 17; ++i) {
85     send_color(cfg[i][0], cfg[i][1], cfg[i][2], Device_PCBRGB);
86   }
87   sei();
88   show();
89 }
90
91 void backlight_set(uint8_t level) {
92   level & BACKLIGHT_ALPHA ? (PORTB |= 0b00000010) : (PORTB &= ~0b00000010);
93   level & BACKLIGHT_EXTRA ? (PORTB |= 0b00000100) : (PORTB &= ~0b00000100);
94   level & BACKLIGHT_MODNUM ? (PORTB |= 0b00001000) : (PORTB &= ~0b00001000);
95   level & BACKLIGHT_FROW ? (PORTE |= 0b01000000) : (PORTE &= ~0b01000000);
96   level & BACKLIGHT_RGB ? backlight_toggle_rgb(true) : backlight_toggle_rgb(false);
97 }
98
99 // Port from backlight_update_state
100 void led_set_kb(uint8_t usb_led) {
101     bool status[7] = {
102     backlight_os_state & (1<<USB_LED_CAPS_LOCK),
103     backlight_os_state & (1<<USB_LED_SCROLL_LOCK),
104     backlight_os_state & (1<<USB_LED_NUM_LOCK),
105     backlight_layer_state & (1<<1),
106     backlight_layer_state & (1<<2),
107     backlight_layer_state & (1<<3),
108     backlight_layer_state & (1<<4)
109   };
110   indicator_leds_set(status);
111   backlight_os_state & (1<<USB_LED_CAPS_LOCK) ? (PORTB &= ~0b00000001) : (PORTB |= 0b00000001);
112   backlight_os_state & (1<<USB_LED_SCROLL_LOCK) ? (PORTB &= ~0b00010000) : (PORTB |= 0b00010000);
113 }
114
115 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
116   return process_record_user(keycode, record);
117 }