-
Notifications
You must be signed in to change notification settings - Fork 271
/
macro.c
397 lines (344 loc) · 10.1 KB
/
macro.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* Copyright (C) 2011-2014 by Jacob Alexander
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// ----- Includes -----
// Compiler Includes
#include <Lib/MacroLib.h>
// Project Includes
#include <led.h>
#include <print.h>
#include <scan_loop.h>
#include <output_com.h>
// Keymaps
#include <keymap.h>
#include <usb_keys.h>
// Local Includes
#include "macro.h"
// ----- Variables -----
// Keeps track of the sequence used to reflash the teensy in software
static uint8_t Bootloader_ConditionSequence[] = {1,16,6,11};
uint8_t Bootloader_ConditionState = 0;
uint8_t Bootloader_NextPositionReady = 1;
uint8_t Bootloader_KeyDetected = 0;
// ----- Functions -----
inline void macro_finishedWithBuffer( uint8_t sentKeys )
{
/* BudKeypad
// Boot loader sequence state handler
switch ( KeyIndex_BufferUsed )
{
// The next bootloader key can now be pressed, if there were no keys processed
case 0:
Bootloader_NextPositionReady = 1;
break;
// If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
// This case purposely falls through
case 1:
if ( Bootloader_KeyDetected )
break;
default:
Bootloader_ConditionState = 0;
break;
}
Bootloader_KeyDetected = 0;
*/
}
void jumpToBootloader(void)
{
#if defined(_avr_at_) // AVR
cli();
// disable watchdog, if enabled
// disable all peripherals
UDCON = 1;
USBCON = (1<<FRZCLK); // disable USB
UCSR1B = 0;
_delay_ms(5);
#if defined(__AVR_AT90USB162__) // Teensy 1.0
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
DDRB = 0; DDRC = 0; DDRD = 0;
PORTB = 0; PORTC = 0; PORTD = 0;
asm volatile("jmp 0x3E00");
#elif defined(__AVR_ATmega32U4__) // Teensy 2.0
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
asm volatile("jmp 0x7E00");
#elif defined(__AVR_AT90USB646__) // Teensy++ 1.0
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
asm volatile("jmp 0xFC00");
#elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0
EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
asm volatile("jmp 0x1FC00");
#endif
#else
#warning "unsuported platform"
#endif
}
// Given a sampling array, and the current number of detected keypress
// Add as many keypresses from the sampling array to the USB key send array as possible.
/*
inline void keyPressDetection( uint8_t *keys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
{
uint8_t Bootloader_KeyDetected = 0;
uint8_t processed_keys = 0;
// Parse the detection array starting from 1 (all keys are purposefully mapped from 1 -> total as per typical PCB labels)
for ( uint8_t key = 0; key < numberOfKeys + 1; key++ )
{
if ( keys[key] & (1 << 7) )
{
processed_keys++;
// Display the detected scancode
char tmpStr[4];
int8ToStr( key, tmpStr );
dPrintStrs( tmpStr, " " );
// Is this a bootloader sequence key?
if ( !Bootloader_KeyDetected
&& Bootloader_NextPositionReady
&& key == Bootloader_ConditionSequence[Bootloader_ConditionState] )
{
Bootloader_KeyDetected = 1;
Bootloader_NextPositionReady = 0;
Bootloader_ConditionState++;
}
else if ( Bootloader_ConditionState > 0 && key == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
{
Bootloader_KeyDetected = 1;
}
// Determine if the key is a modifier
uint8_t modFound = 0;
for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
// Modifier found
if ( modifiers[mod] == key ) {
USBKeys_Modifiers |= map[key];
modFound = 1;
break;
}
}
// Modifier, already done this loop
if ( modFound )
continue;
// Too many keys
if ( USBKeys_Sent >= USBKeys_MaxSize )
{
info_printNL("USB Key limit reached");
errorLED( 1 );
break;
}
// Allow ignoring keys with 0's
if ( map[key] != 0 )
USBKeys_Array[USBKeys_Sent++] = map[key];
}
}
// Boot loader sequence state handler
switch ( processed_keys )
{
// The next bootloader key can now be pressed, if there were no keys processed
case 0:
Bootloader_NextPositionReady = 1;
break;
// If keys were detected, and it wasn't in the sequence (or there was multiple keys detected), start bootloader sequence over
// This case purposely falls through
case 1:
if ( Bootloader_KeyDetected )
break;
default:
Bootloader_ConditionState = 0;
break;
}
// Add debug separator if keys sent via USB
if ( USBKeys_Sent > 0 )
print("\033[1;32m|\033[0m\n");
}
*/
// Scancode Macro Detection
int scancodeMacro( uint8_t scanCode )
{
/*
if ( scanCode == 0x7A )
{
scan_resetKeyboard();
}
else
{
scan_sendData( scanCode );
_delay_ms( 200 );
scan_sendData( 0x80 | scanCode );
}
return 1;
*/
/*
// BudKeypad
// Is this a bootloader sequence key?
if ( !Bootloader_KeyDetected
&& Bootloader_NextPositionReady
&& scanCode == Bootloader_ConditionSequence[Bootloader_ConditionState] )
{
Bootloader_KeyDetected = 1;
Bootloader_NextPositionReady = 0;
Bootloader_ConditionState++;
erro_dPrint("detect");
}
else if ( Bootloader_ConditionState > 0 && scanCode == Bootloader_ConditionSequence[Bootloader_ConditionState - 1] )
{
Bootloader_KeyDetected = 0;
Bootloader_NextPositionReady = 1;
erro_dPrint("detect-again!");
}
// Cancel sequence
else
{
Bootloader_KeyDetected = 0;
Bootloader_NextPositionReady = 1;
Bootloader_ConditionState = 0;
erro_dPrint("Arg");
}
*/
return 0;
}
uint8_t sendCode = 0;
// USBCode Macro Detection
int usbcodeMacro( uint8_t usbCode )
{
// Keyboard Input Test Macro
/*
switch ( usbCode )
{
case KEY_F1:
sendCode--;
//scan_sendData( 0x90 );
scan_sendData( sendCode );
_delay_ms( 200 );
break;
case KEY_F2:
//scan_sendData( 0x90 );
scan_sendData( sendCode );
_delay_ms( 200 );
break;
case KEY_F3:
sendCode++;
//scan_sendData( 0x90 );
scan_sendData( sendCode );
_delay_ms( 200 );
break;
case KEY_F4:
sendCode += 0x10;
//scan_sendData( 0x90 );
scan_sendData( sendCode );
_delay_ms( 200 );
break;
case KEY_F5:
// Set 9th bit to 0
UCSR1B &= ~(1 << 0);
_delay_ms( 200 );
break;
case KEY_F6:
// Set 9th bit to 1
UCSR1B |= (1 << 0);
_delay_ms( 200 );
break;
case KEY_F11:
// Set click code
KeyIndex_Add_InputSignal = sendCode;
_delay_ms( 200 );
break;
default:
return 0;
}
return 1;
*/
return 0;
}
// Given a list of keypresses, translate into the USB key codes
// The buffer is cleared after running
// If the buffer doesn't fit into the USB send array, the extra keys are dropped
void keyPressBufferRead( uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
{
// Loop through input buffer
for ( uint8_t index = 0; index < KeyIndex_BufferUsed; index++ )
{
// Get the keycode from the buffer
uint8_t key = KeyIndex_Buffer[index];
// Check key for special usages using the scancode
// If non-zero return, ignore normal processing of the scancode
if ( scancodeMacro( key ) )
continue;
// Check key for special usages using the usbcode
// If non-zero return, ignore normal processing of the usbcode
if ( usbcodeMacro( map[key] ) )
continue;
// Determine if the key is a modifier
uint8_t modFound = 0;
for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
// Modifier found
if ( modifiers[mod] == key ) {
USBKeys_Modifiers |= map[key];
modFound = 1;
break;
}
}
// Modifier, already done this loop
if ( modFound )
continue;
// Too many keys
if ( USBKeys_Sent >= USBKeys_MaxSize )
{
info_printNL("USB Key limit reached");
errorLED( 1 );
break;
}
// Allow ignoring keys with 0's
if ( map[key] != 0 )
{
USBKeys_Array[USBKeys_Sent++] = map[key];
}
else
{
// Key was not mapped
// TODO Add dead key map
char tmpStr[6];
hexToStr( key, tmpStr );
erro_dPrint( "Key not mapped... - ", tmpStr );
errorLED( 1 );
}
}
// Signal Macro processor that all of the buffered keys have been processed
macro_finishedWithBuffer( KeyIndex_BufferUsed );
// Signal buffer that we've used it
scan_finishedWithBuffer( KeyIndex_BufferUsed );
}
inline void process_macros(void)
{
// Online process macros once (if some were found), until the next USB send
if ( USBKeys_Sent != 0 )
return;
// Query the input buffer for keypresses
keyPressBufferRead( MODIFIER_MASK, sizeof(MODIFIER_MASK), KEYINDEX_MASK );
// Check for bootloader condition
if ( Bootloader_ConditionState == sizeof( Bootloader_ConditionSequence ) )
jumpToBootloader();
}