]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Macro/buffer/macro.c
Fixing CMake dependency checking for kll_defs.h
[kiibohd-controller.git] / Macro / buffer / macro.c
1 /* Copyright (C) 2011-2014 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 <output_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        uint8_t Bootloader_KeyDetected         = 0;
49
50
51
52 // ----- Functions -----
53
54 inline void macro_finishedWithBuffer( uint8_t sentKeys )
55 {
56         /* BudKeypad
57         // Boot loader sequence state handler
58         switch ( KeyIndex_BufferUsed )
59         {
60         // The next bootloader key can now be pressed, if there were no keys processed
61         case 0:
62                 Bootloader_NextPositionReady = 1;
63                 break;
64         // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
65         // This case purposely falls through
66         case 1:
67                 if ( Bootloader_KeyDetected )
68                         break;
69         default:
70                 Bootloader_ConditionState = 0;
71                 break;
72         }
73
74         Bootloader_KeyDetected = 0;
75         */
76 }
77
78 void jumpToBootloader(void)
79 {
80 #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
81         cli();
82         // disable watchdog, if enabled
83         // disable all peripherals
84         UDCON = 1;
85         USBCON = (1<<FRZCLK);  // disable USB
86         UCSR1B = 0;
87         _delay_ms(5);
88
89 #if defined(__AVR_AT90USB162__)                // Teensy 1.0
90         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
91         TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
92         DDRB = 0; DDRC = 0; DDRD = 0;
93         PORTB = 0; PORTC = 0; PORTD = 0;
94         asm volatile("jmp 0x3E00");
95 #elif defined(__AVR_ATmega32U4__)              // Teensy 2.0
96         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
97         TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
98         DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
99         PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
100         asm volatile("jmp 0x7E00");
101 #elif defined(__AVR_AT90USB646__)              // Teensy++ 1.0
102         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
103         TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
104         DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
105         PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
106         asm volatile("jmp 0xFC00");
107 #elif defined(__AVR_AT90USB1286__)             // Teensy++ 2.0
108         EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
109         TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
110         DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
111         PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
112         asm volatile("jmp 0x1FC00");
113 #endif
114 #endif
115 }
116
117 // Given a sampling array, and the current number of detected keypress
118 // Add as many keypresses from the sampling array to the USB key send array as possible.
119 /*
120 inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
121 {
122         uint8_t Bootloader_KeyDetected = 0;
123         uint8_t processed_keys         = 0;
124
125         // Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
126         for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
127         {
128                 if ( keys[key] & (1 << 7) )
129                 {
130                         processed_keys++;
131
132                         // Display the detected scancode
133                         char tmpStr[4];
134                         int8ToStr( key, tmpStr );
135                         dPrintStrs( tmpStr, " " );
136
137                         // Is this a bootloader sequence key?
138                         if ( !Bootloader_KeyDetected
139                            && Bootloader_NextPositionReady
140                            && key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
141                         {
142                                 Bootloader_KeyDetected = 1;
143                                 Bootloader_NextPositionReady = 0;
144                                 Bootloader_ConditionState++;
145                         }
146                         else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
147                         {
148                                 Bootloader_KeyDetected = 1;
149                         }
150
151                         // Determine if the key is a modifier
152                         uint8_t modFound = 0;
153                         for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
154                                 // Modifier found
155                                 if ( modifiers[mod] == key ) {
156                                         USBKeys_Modifiers |= map[key];
157                                         modFound = 1;
158                                         break;
159                                 }
160                         }
161
162                         // Modifier, already done this loop
163                         if ( modFound )
164                                 continue;
165
166                         // Too many keys
167                         if ( USBKeys_Sent >= USBKeys_MaxSize )
168                         {
169                                 info_print("USB Key limit reached");
170                                 errorLED( 1 );
171                                 break;
172                         }
173
174                         // Allow ignoring keys with 0's
175                         if ( map[key] != 0 )
176                                 USBKeys_Array[USBKeys_Sent++] = map[key];
177                 }
178         }
179
180         // Boot loader sequence state handler
181         switch ( processed_keys )
182         {
183         // The next bootloader key can now be pressed, if there were no keys processed
184         case 0:
185                 Bootloader_NextPositionReady = 1;
186                 break;
187         // If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
188         // This case purposely falls through
189         case 1:
190                 if ( Bootloader_KeyDetected )
191                         break;
192         default:
193                 Bootloader_ConditionState = 0;
194                 break;
195         }
196
197         // Add debug separator if keys sent via USB
198         if ( USBKeys_Sent > 0 )
199                 print("\033[1;32m|\033[0m\n");
200 }
201 */
202
203 // Scancode Macro Detection
204 int scancodeMacro( uint8_t scanCode )
205 {
206         /*
207         if ( scanCode == 0x7A )
208         {
209                 scan_resetKeyboard();
210         }
211         else
212         {
213                 scan_sendData( scanCode );
214                 _delay_ms( 200 );
215                 scan_sendData( 0x80 | scanCode );
216         }
217         return 1;
218         */
219         /*
220         // BudKeypad
221         // Is this a bootloader sequence key?
222         if ( !Bootloader_KeyDetected
223            && Bootloader_NextPositionReady
224            && scanCode == Bootloader_ConditionSequence[Bootloader_ConditionState] )
225         {
226                 Bootloader_KeyDetected = 1;
227                 Bootloader_NextPositionReady = 0;
228                 Bootloader_ConditionState++;
229                 erro_dPrint("detect");
230         }
231         else if ( Bootloader_ConditionState > 0 && scanCode == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
232         {
233                 Bootloader_KeyDetected = 0;
234                 Bootloader_NextPositionReady = 1;
235                 erro_dPrint("detect-again!");
236         }
237         // Cancel sequence
238         else
239         {
240                 Bootloader_KeyDetected = 0;
241                 Bootloader_NextPositionReady = 1;
242                 Bootloader_ConditionState = 0;
243                 erro_dPrint("Arg");
244         }
245         */
246
247         return 0;
248 }
249
250 uint8_t sendCode = 0;
251
252 // USBCode Macro Detection
253 int usbcodeMacro( uint8_t usbCode )
254 {
255         // Keyboard Input Test Macro
256         /*
257         switch ( usbCode )
258         {
259         case KEY_F1:
260                 sendCode--;
261                 //scan_sendData( 0x90 );
262                 scan_sendData( sendCode );
263                 _delay_ms( 200 );
264                 break;
265
266         case KEY_F2:
267                 //scan_sendData( 0x90 );
268                 scan_sendData( sendCode );
269                 _delay_ms( 200 );
270                 break;
271
272         case KEY_F3:
273                 sendCode++;
274                 //scan_sendData( 0x90 );
275                 scan_sendData( sendCode );
276                 _delay_ms( 200 );
277                 break;
278
279         case KEY_F4:
280                 sendCode += 0x10;
281                 //scan_sendData( 0x90 );
282                 scan_sendData( sendCode );
283                 _delay_ms( 200 );
284                 break;
285
286         case KEY_F5:
287                 // Set 9th bit to 0
288                 UCSR1B &= ~(1 << 0);
289                 _delay_ms( 200 );
290                 break;
291
292         case KEY_F6:
293                 // Set 9th bit to 1
294                 UCSR1B |=  (1 << 0);
295                 _delay_ms( 200 );
296                 break;
297
298         case KEY_F11:
299                 // Set click code
300                 KeyIndex_Add_InputSignal = sendCode;
301                 _delay_ms( 200 );
302                 break;
303
304         default:
305                 return 0;
306         }
307         
308         return 1;
309         */
310         return 0;
311 }
312
313
314 // Given a list of keypresses, translate into the USB key codes
315 // The buffer is cleared after running
316 // If the buffer doesn't fit into the USB send array, the extra keys are dropped
317 void keyPressBufferRead( uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
318 {
319         // Loop through input buffer
320         for ( uint8_t index = 0; index < KeyIndex_BufferUsed; index++ )
321         {
322                 // Get the keycode from the buffer
323                 uint8_t key = KeyIndex_Buffer[index];
324
325                 // Check key for special usages using the scancode
326                 // If non-zero return, ignore normal processing of the scancode
327                 if ( scancodeMacro( key ) )
328                         continue;
329
330                 // Check key for special usages using the usbcode
331                 // If non-zero return, ignore normal processing of the usbcode
332                 if ( usbcodeMacro( map[key] ) )
333                         continue;
334
335                 // Determine if the key is a modifier
336                 uint8_t modFound = 0;
337                 for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
338                         // Modifier found
339                         if ( modifiers[mod] == key ) {
340                                 USBKeys_Modifiers |= map[key];
341                                 modFound = 1;
342                                 break;
343                         }
344                 }
345
346                 // Modifier, already done this loop
347                 if ( modFound )
348                         continue;
349
350                 // Too many keys
351                 if ( USBKeys_Sent >= USBKeys_MaxSize )
352                 {
353                         info_print("USB Key limit reached");
354                         errorLED( 1 );
355                         break;
356                 }
357
358                 // Allow ignoring keys with 0's
359                 if ( map[key] != 0 )
360                 {
361                         USBKeys_Array[USBKeys_Sent++] = map[key];
362                 }
363                 else
364                 {
365                         // Key was not mapped
366                         // TODO Add dead key map
367                         char tmpStr[6];
368                         hexToStr( key, tmpStr );
369                         erro_dPrint( "Key not mapped... - ", tmpStr );
370                         errorLED( 1 );
371                 }
372         }
373
374         // Signal Macro processor that all of the buffered keys have been processed
375         macro_finishedWithBuffer( KeyIndex_BufferUsed );
376
377         // Signal buffer that we've used it
378         scan_finishedWithBuffer( KeyIndex_BufferUsed );
379 }
380
381 inline void process_macros(void)
382 {
383         // Online process macros once (if some were found), until the next USB send
384         if ( USBKeys_Sent != 0 )
385                 return;
386
387         // Query the input buffer for keypresses
388         keyPressBufferRead( MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
389
390         // Check for bootloader condition
391         if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
392                 jumpToBootloader();
393 }
394