]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Macro/basic/macro.c
bf04851be67c20028a5df5d062ac28a1983165ae
[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 // AVR Includes
25 #include <util/delay.h>
26 #include <avr/interrupt.h>
27
28 // Project Includes
29 #include <led.h>
30 #include <print.h>
31 #include <scan_loop.h>
32 #include <usb_com.h>
33
34 // Keymaps
35 #include <keymap.h>
36 #include <usb_keys.h>
37
38 // Local Includes
39 #include "macro.h"
40
41
42
43 // ----- Variables -----
44
45 // Keeps track of the sequence used to reflash the teensy in software
46 static uint8_t Bootloader_ConditionSequence[] = {1,16,6,11};
47        uint8_t Bootloader_ConditionState      = 0;
48        uint8_t Bootloader_NextPositionReady   = 1;
49
50
51
52 // ----- Functions -----
53
54 void jumpToBootloader(void)
55 {
56         cli();
57         // disable watchdog, if enabled
58         // disable all peripherals
59         UDCON = 1;
60         USBCON = (1<<FRZCLK);  // disable USB
61         UCSR1B = 0;
62         _delay_ms(5);
63
64 #if defined(__AVR_AT90USB162__)                // Teensy 1.0
65         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
66         TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
67         DDRB = 0; DDRC = 0; DDRD = 0;
68         PORTB = 0; PORTC = 0; PORTD = 0;
69         asm volatile("jmp 0x3E00");
70 #elif defined(__AVR_ATmega32U4__)              // Teensy 2.0
71         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
72         TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
73         DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
74         PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
75         asm volatile("jmp 0x7E00");
76 #elif defined(__AVR_AT90USB646__)              // Teensy++ 1.0
77         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
78         TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
79         DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
80         PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
81         asm volatile("jmp 0xFC00");
82 #elif defined(__AVR_AT90USB1286__)             // Teensy++ 2.0
83         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
84         TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
85         DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
86         PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
87         asm volatile("jmp 0x1FC00");
88 #endif
89 }
90
91 // Given a sampling array, and the current number of detected keypress
92 // Add as many keypresses from the sampling array to the USB key send array as possible.
93 inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
94 {
95         uint8_t Bootloader_KeyDetected = 0;
96         uint8_t processed_keys         = 0;
97
98         // Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
99         for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
100         {
101                 if ( keys[key] & (1 << 7) )
102                 {
103                         processed_keys++;
104
105                         // Display the detected scancode
106                         char tmpStr[4];
107                         int8ToStr( key, tmpStr );
108                         dPrintStrs( tmpStr, " " );
109
110                         // Is this a bootloader sequence key?
111                         if ( !Bootloader_KeyDetected
112                            && Bootloader_NextPositionReady
113                            && key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
114                         {
115                                 Bootloader_KeyDetected = 1;
116                                 Bootloader_NextPositionReady = 0;
117                                 Bootloader_ConditionState++;
118                         }
119                         else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
120                         {
121                                 Bootloader_KeyDetected = 1;
122                         }
123
124                         // Determine if the key is a modifier
125                         uint8_t modFound = 0;
126                         for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
127                                 // Modifier found
128                                 if ( modifiers[mod] == key ) {
129                                         USBKeys_Modifiers |= map[key];
130                                         modFound = 1;
131                                         break;
132                                 }
133                         }
134
135                         // Modifier, already done this loop
136                         if ( modFound )
137                                 continue;
138
139                         // Too many keys
140                         if ( USBKeys_Sent >= USBKeys_MaxSize )
141                         {
142                                 info_print("USB Key limit reached");
143                                 errorLED( 1 );
144                                 break;
145                         }
146
147                         // Allow ignoring keys with 0's
148                         if ( map[key] != 0 )
149                                 USBKeys_Array[USBKeys_Sent++] = map[key];
150                 }
151         }
152
153         // Boot loader sequence state handler
154         switch ( processed_keys )
155         {
156         // The next bootloader key can now be pressed, if there were no keys processed
157         case 0:
158                 Bootloader_NextPositionReady = 1;
159                 break;
160         // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
161         // This case purposely falls through
162         case 1:
163                 if ( Bootloader_KeyDetected )
164                         break;
165         default:
166                 Bootloader_ConditionState = 0;
167                 break;
168         }
169
170         // Add debug separator if keys sent via USB
171         if ( USBKeys_Sent > 0 )
172                 print("\033[1;32m|\033[0m\n");
173 }
174
175 inline void process_macros(void)
176 {
177         // Online process macros once (if some were found), until the next USB send
178         if ( USBKeys_Sent != 0 )
179                 return;
180
181         // Debounce Sampling Array to USB Data Array
182         keyPressDetection( KeyIndex_Array, KeyIndex_Size, MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
183
184         // Check for bootloader condition
185         if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
186                 jumpToBootloader();
187 }
188