]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Macro/basic/macro.c
Code cleanup
[kiibohd-controller.git] / Macro / basic / macro.c
1 /* Copyright (C) 2011 by Jacob Alexander
2  * 
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 // ----- Includes -----
23
24 // Compiler Includes
25 #include <Lib/MacroLib.h>
26
27 // Project Includes
28 #include <led.h>
29 #include <print.h>
30 #include <scan_loop.h>
31 #include <usb_com.h>
32
33 // Keymaps
34 #include <keymap.h>
35 #include <usb_keys.h>
36
37 // Local Includes
38 #include "macro.h"
39
40
41
42 // ----- Variables -----
43
44 // Keeps track of the sequence used to reflash the teensy in software
45 static uint8_t Bootloader_ConditionSequence[] = {1,16,6,11};
46        uint8_t Bootloader_ConditionState      = 0;
47        uint8_t Bootloader_NextPositionReady   = 1;
48
49
50
51 // ----- Functions -----
52
53 void jumpToBootloader(void)
54 {
55         cli();
56         // disable watchdog, if enabled
57         // disable all peripherals
58         UDCON = 1;
59         USBCON = (1<<FRZCLK);  // disable USB
60         UCSR1B = 0;
61         _delay_ms(5);
62
63 #if defined(__AVR_AT90USB162__)                // Teensy 1.0
64         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
65         TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
66         DDRB = 0; DDRC = 0; DDRD = 0;
67         PORTB = 0; PORTC = 0; PORTD = 0;
68         asm volatile("jmp 0x3E00");
69 #elif defined(__AVR_ATmega32U4__)              // Teensy 2.0
70         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
71         TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
72         DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
73         PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
74         asm volatile("jmp 0x7E00");
75 #elif defined(__AVR_AT90USB646__)              // Teensy++ 1.0
76         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
77         TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
78         DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
79         PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
80         asm volatile("jmp 0xFC00");
81 #elif defined(__AVR_AT90USB1286__)             // Teensy++ 2.0
82         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
83         TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
84         DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
85         PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
86         asm volatile("jmp 0x1FC00");
87 #endif
88 }
89
90 // Given a sampling array, and the current number of detected keypress
91 // Add as many keypresses from the sampling array to the USB key send array as possible.
92 inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
93 {
94         uint8_t Bootloader_KeyDetected = 0;
95         uint8_t processed_keys         = 0;
96
97         // Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
98         for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
99         {
100                 if ( keys[key] & (1 << 7) )
101                 {
102                         processed_keys++;
103
104                         // Display the detected scancode
105                         char tmpStr[4];
106                         int8ToStr( key, tmpStr );
107                         dPrintStrs( tmpStr, " " );
108
109                         // Is this a bootloader sequence key?
110                         if ( !Bootloader_KeyDetected
111                            && Bootloader_NextPositionReady
112                            && key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
113                         {
114                                 Bootloader_KeyDetected = 1;
115                                 Bootloader_NextPositionReady = 0;
116                                 Bootloader_ConditionState++;
117                         }
118                         else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
119                         {
120                                 Bootloader_KeyDetected = 1;
121                         }
122
123                         // Determine if the key is a modifier
124                         uint8_t modFound = 0;
125                         for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
126                                 // Modifier found
127                                 if ( modifiers[mod] == key ) {
128                                         USBKeys_Modifiers |= map[key];
129                                         modFound = 1;
130                                         break;
131                                 }
132                         }
133
134                         // Modifier, already done this loop
135                         if ( modFound )
136                                 continue;
137
138                         // Too many keys
139                         if ( USBKeys_Sent >= USBKeys_MaxSize )
140                         {
141                                 info_print("USB Key limit reached");
142                                 errorLED( 1 );
143                                 break;
144                         }
145
146                         // Allow ignoring keys with 0's
147                         if ( map[key] != 0 )
148                                 USBKeys_Array[USBKeys_Sent++] = map[key];
149                 }
150         }
151
152         // Boot loader sequence state handler
153         switch ( processed_keys )
154         {
155         // The next bootloader key can now be pressed, if there were no keys processed
156         case 0:
157                 Bootloader_NextPositionReady = 1;
158                 break;
159         // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
160         // This case purposely falls through
161         case 1:
162                 if ( Bootloader_KeyDetected )
163                         break;
164         default:
165                 Bootloader_ConditionState = 0;
166                 break;
167         }
168
169         // Add debug separator if keys sent via USB
170         if ( USBKeys_Sent > 0 )
171                 print("\033[1;32m|\033[0m\n");
172 }
173
174 inline void process_macros(void)
175 {
176         // Online process macros once (if some were found), until the next USB send
177         if ( USBKeys_Sent != 0 )
178                 return;
179
180         // Debounce Sampling Array to USB Data Array
181         keyPressDetection( KeyIndex_Array, KeyIndex_Size, MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
182
183         // Check for bootloader condition
184         if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
185                 jumpToBootloader();
186 }
187