]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Macro/buffer/macro.c
Intial commit of the SonyNEWS scan module
[kiibohd-controller.git] / Macro / buffer / 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 /*
94 inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
95 {
96         uint8_t Bootloader_KeyDetected = 0;
97         uint8_t processed_keys         = 0;
98
99         // Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
100         for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
101         {
102                 if ( keys[key] & (1 << 7) )
103                 {
104                         processed_keys++;
105
106                         // Display the detected scancode
107                         char tmpStr[4];
108                         int8ToStr( key, tmpStr );
109                         dPrintStrs( tmpStr, " " );
110
111                         // Is this a bootloader sequence key?
112                         if ( !Bootloader_KeyDetected
113                            && Bootloader_NextPositionReady
114                            && key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
115                         {
116                                 Bootloader_KeyDetected = 1;
117                                 Bootloader_NextPositionReady = 0;
118                                 Bootloader_ConditionState++;
119                         }
120                         else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
121                         {
122                                 Bootloader_KeyDetected = 1;
123                         }
124
125                         // Determine if the key is a modifier
126                         uint8_t modFound = 0;
127                         for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
128                                 // Modifier found
129                                 if ( modifiers[mod] == key ) {
130                                         USBKeys_Modifiers |= map[key];
131                                         modFound = 1;
132                                         break;
133                                 }
134                         }
135
136                         // Modifier, already done this loop
137                         if ( modFound )
138                                 continue;
139
140                         // Too many keys
141                         if ( USBKeys_Sent >= USBKeys_MaxSize )
142                         {
143                                 info_print("USB Key limit reached");
144                                 errorLED( 1 );
145                                 break;
146                         }
147
148                         // Allow ignoring keys with 0's
149                         if ( map[key] != 0 )
150                                 USBKeys_Array[USBKeys_Sent++] = map[key];
151                 }
152         }
153
154         // Boot loader sequence state handler
155         switch ( processed_keys )
156         {
157         // The next bootloader key can now be pressed, if there were no keys processed
158         case 0:
159                 Bootloader_NextPositionReady = 1;
160                 break;
161         // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
162         // This case purposely falls through
163         case 1:
164                 if ( Bootloader_KeyDetected )
165                         break;
166         default:
167                 Bootloader_ConditionState = 0;
168                 break;
169         }
170
171         // Add debug separator if keys sent via USB
172         if ( USBKeys_Sent > 0 )
173                 print("\033[1;32m|\033[0m\n");
174 }
175 */
176
177 // Given a list of keypresses, translate into the USB key codes
178 // The buffer is cleared after running
179 // If the buffer doesn't fit into the USB send array, the extra keys are dropped
180 void keyPressBufferRead( uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
181 {
182         // Loop through input buffer
183         for ( uint8_t index = 0; index < KeyIndex_BufferUsed; index++ )
184         {
185                 // Get the keycode from the buffer
186                 uint8_t key = KeyIndex_Buffer[index];
187
188                 // Determine if the key is a modifier
189                 uint8_t modFound = 0;
190                 for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
191                         // Modifier found
192                         if ( modifiers[mod] == key ) {
193                                 USBKeys_Modifiers |= map[key];
194                                 modFound = 1;
195                                 break;
196                         }
197                 }
198
199                 // Modifier, already done this loop
200                 if ( modFound )
201                         continue;
202
203                 // Too many keys
204                 if ( USBKeys_Sent >= USBKeys_MaxSize )
205                 {
206                         info_print("USB Key limit reached");
207                         errorLED( 1 );
208                         break;
209                 }
210
211                 // Allow ignoring keys with 0's
212                 if ( map[key] != 0 )
213                 {
214                         USBKeys_Array[USBKeys_Sent++] = map[key];
215                 }
216                 else
217                 {
218                         // Key was not mapped
219                         // TODO Add dead key map
220                         char tmpStr[6];
221                         hexToStr( key, tmpStr );
222                         erro_dPrint( "Key not mapped... - ", tmpStr );
223                         errorLED( 1 );
224                 }
225         }
226
227         // Signal buffer that we've used it
228         scan_finishedWithBuffer();
229 }
230
231 inline void process_macros(void)
232 {
233         // Online process macros once (if some were found), until the next USB send
234         if ( USBKeys_Sent != 0 )
235                 return;
236
237         // Query the input buffer for keypresses
238         keyPressBufferRead( MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
239
240         // Check for bootloader condition
241         //if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
242         //      jumpToBootloader();
243 }
244