]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ai03/orbit/orbit.c
[Keyboard] Add Orbit keyboard (#5306)
[qmk_firmware.git] / keyboards / ai03 / orbit / orbit.c
1 /* Copyright 2018 Ryota Goto
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 "orbit.h"
17 #include "split_util.h"
18 #include "transport.h"
19
20
21 // Call led_toggle to set LEDs easily
22 // LED IDs:
23 // 
24 // (LEFT) 0 1 2   |   3 4 5 (RIGHT)
25
26 void led_toggle(int id, bool on) {
27         
28         if (isLeftHand) {
29                 switch(id) {
30                         case 0:
31                                 // Left hand C6
32                                 if (on)
33                                         //PORTC |= (1<<6);
34                                         writePinHigh(C6);
35                                 else
36                                         //PORTC &= ~(1<<6);
37                                         writePinLow(C6);
38                                 break;
39                         case 1:
40                                 // Left hand B6
41                                 if (on)
42                                         //PORTB |= (1<<6);
43                                         writePinHigh(B6);
44                                 else
45                                         //PORTB &= ~(1<<6);
46                                         writePinLow(B6);
47                                 break;
48                         case 2:
49                                 // Left hand B5
50                                 if (on)
51                                         //PORTB |= (1<<5);
52                                         writePinHigh(B5);
53                                 else
54                                         //PORTB &= ~(1<<5);
55                                         writePinLow(B5);
56                                 break;
57                         default:
58                                 break;
59                 }
60         } else {
61                 switch(id) {
62                         case 3:
63                                 // Right hand F6
64                                 if (on)
65                                         //PORTF |= (1<<6);
66                                         writePinHigh(F6);
67                                 else
68                                         //PORTF &= ~(1<<6);
69                                         writePinLow(F6);
70                                 break;
71                         case 4:
72                                 // Right hand F7
73                                 if (on)
74                                         //PORTF |= (1<<7);
75                                         writePinHigh(F7);
76                                 else
77                                         //PORTF &= ~(1<<7);
78                                         writePinLow(F7);
79                                 break;
80                         case 5:
81                                 // Right hand C7
82                                 if (on)
83                                         //PORTC |= (1<<7);
84                                         writePinHigh(C7);
85                                 else
86                                         //PORTC &= ~(1<<7);
87                                         writePinLow(C7);
88                                 break;
89                         default:
90                                 break;
91                 }
92         }
93 }
94
95 // Set all LEDs at once using an array of 6 booleans
96 // LED IDs:
97 // 
98 // (LEFT) 0 1 2   |   3 4 5 (RIGHT)
99 // 
100 // Ex. set_all_leds({ false, false, false, true, true, true }) would turn off left hand, turn on right hand
101
102 void set_all_leds(bool leds[6]) {
103         for (int i = 0; i < 6; i++) {
104                 led_toggle(i, leds[i]);
105         }
106 }
107
108 void set_layer_indicators(uint8_t layer) {
109         
110         switch (layer)
111         {
112                 case 0:
113                         led_toggle(0, true);
114                         led_toggle(1, false);
115                         led_toggle(2, false);
116                         break;
117                 case 1:
118                         led_toggle(0, true);
119                         led_toggle(1, true);
120                         led_toggle(2, false);
121                         break;
122                 case 2:
123                         led_toggle(0, true);
124                         led_toggle(1, true);
125                         led_toggle(2, true);
126                         break;
127                 case 3:
128                         led_toggle(0, false);
129                         led_toggle(1, true);
130                         led_toggle(2, true);
131                         break;
132                 case 4:
133                         led_toggle(0, false);
134                         led_toggle(1, false);
135                         led_toggle(2, true);
136                         break;
137                 default:
138                         led_toggle(0, true);
139                         led_toggle(1, false);
140                         led_toggle(2, true);
141                         break;
142         }
143         
144 }
145
146 void matrix_init_kb(void) {
147         // put your keyboard start-up code here
148         // runs once when the firmware starts up
149         
150         // Initialize indicator LEDs to output
151         if (isLeftHand)
152         {
153                 setPinOutput(C6);
154                 setPinOutput(B6);
155                 setPinOutput(B5);
156                 //DDRC |= (1<<6);
157                 //DDRB |= (1<<6);
158                 //DDRB |= (1<<5);
159         }
160         else
161         {
162                 setPinOutput(F6);
163                 setPinOutput(F7);
164                 setPinOutput(C7);
165                 //DDRF |= (1<<6);
166                 //DDRF |= (1<<7);
167                 //DDRC |= (1<<7);
168         }
169
170         set_layer_indicators(0);
171         
172         matrix_init_user();
173 }
174
175 void matrix_scan_kb(void) {
176         // put your looping keyboard code here
177         // runs every cycle (a lot)
178
179         matrix_scan_user();
180 }
181
182 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
183         // put your per-action keyboard code here
184         // runs for every action, just before processing by the firmware
185
186         return process_record_user(keycode, record);
187 }
188
189 void led_set_kb(uint8_t usb_led) {
190         // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
191         
192         if (is_keyboard_master()) {
193         
194                 serial_m2s_buffer.nlock_led = IS_LED_ON(usb_led, USB_LED_NUM_LOCK);
195                 serial_m2s_buffer.clock_led = IS_LED_ON(usb_led, USB_LED_CAPS_LOCK);
196                 serial_m2s_buffer.slock_led = IS_LED_ON(usb_led, USB_LED_SCROLL_LOCK);
197
198                 led_toggle(3, IS_LED_ON(usb_led, USB_LED_NUM_LOCK));
199                 led_toggle(4, IS_LED_ON(usb_led, USB_LED_CAPS_LOCK));
200                 led_toggle(5, IS_LED_ON(usb_led, USB_LED_SCROLL_LOCK));
201                 
202         }
203
204         led_set_user(usb_led);
205 }
206
207 uint32_t layer_state_set_kb(uint32_t state) {
208         
209         if (is_keyboard_master())
210         {
211                 
212                 current_layer = biton32(state);
213                 serial_m2s_buffer.current_layer = biton32(state);
214                 
215                 // If left half, do the LED toggle thing
216                 if (isLeftHand)
217                 {
218                         set_layer_indicators(biton32(state));
219                 }
220                 
221         }
222         // NOTE: Do not set slave LEDs here.
223         // This is not called on slave
224         
225         return layer_state_set_user(state);
226 }
227
228