-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirmware.c
345 lines (307 loc) · 13 KB
/
firmware.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
//#include <avr/pgmspace.h>
//#include <avr/interrupt.h>
#include <util/delay.h>
//#include <string.h>
//#include <stdlib.h>
//#include <stdio.h>
//#include "usb_keyboard_debug.h"
#include "print.h"
//#include "sdcard.h"
//#include "macro.h"
#include "analog.h"
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
#define EMPTY_ROW {0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe}
#define ZERO_ROW { 0, 0, 0, 0, 0, 0, 0}
#define JUST_SWITCHED 0x80
#define STILL_ON 0x00
#define DEBUG 0
#define ON 1
#define OFF 0
#define ROWS 10
#define COLS 7
#define MAP_SIZE 139
typedef struct{
char name[30];
int id;
} pair;
pair str_map[MAP_SIZE] = {
//2 Misc (Fail early)
{"NA",0}, {"TOGGLE",KEY_TOGGLE},
//25 Alphabetic
{"A",KEY_A}, {"B",KEY_B}, {"C",KEY_C}, {"D",KEY_D}, {"E",KEY_E},
{"F",KEY_F}, {"G",KEY_G}, {"H",KEY_H}, {"I",KEY_I}, {"J",KEY_J},
{"K",KEY_K}, {"L",KEY_L}, {"M",KEY_M}, {"N",KEY_N}, {"O",KEY_O},
{"P",KEY_P}, {"Q",KEY_Q}, {"R",KEY_R}, {"S",KEY_S}, {"T",KEY_T},
{"U",KEY_U}, {"V",KEY_V}, {"W",KEY_W}, {"X",KEY_X}, {"Y",KEY_Y},
{"Z",KEY_Z},
//10 Numeric Keys
{"1",KEY_1}, {"2",KEY_2}, {"3",KEY_3}, {"4",KEY_4}, {"5",KEY_5},
{"6",KEY_6}, {"7",KEY_7}, {"8",KEY_8}, {"9",KEY_9}, {"0",KEY_0},
//8 Macros
{"MACRO_1",MACRO_1}, {"MACRO_2",MACRO_2}, {"MACRO_3",MACRO_3},
{"MACRO_4",MACRO_4}, {"MACRO_5",MACRO_5}, {"MACRO_6",MACRO_6},
{"MACRO_7",MACRO_7}, {"MACRO_8",MACRO_8},
//18 Command
{"ENTER",KEY_ENTER}, {"ESC",KEY_ESC}, {"BACKSPACE",KEY_BACKSPACE},
{"TAB",KEY_TAB}, {"SPACE",KEY_SPACE}, {"MINUS",KEY_MINUS},
{"EQUAL",KEY_EQUAL}, {"LEFT_BRACE",KEY_LEFT_BRACE},
{"RIGHT_BRACE",KEY_RIGHT_BRACE}, {"BACKSLASH",KEY_BACKSLASH},
{"NUMBER",KEY_NUMBER}, {"SEMICOLON",KEY_SEMICOLON},
{"QUOTE",KEY_QUOTE}, {"TILDE",KEY_TILDE}, {"COMMA",KEY_COMMA},
{"PERIOD",KEY_PERIOD}, {"SLASH",KEY_SLASH}, {"CAPS_LOCK",KEY_CAPS_LOCK},
//12 Function Keys
{"F1",KEY_F1}, {"F2",KEY_F2}, {"F3",KEY_F3}, {"F4",KEY_F4},
{"F5",KEY_F5}, {"F6",KEY_F6}, {"F7",KEY_F7}, {"F8",KEY_F8},
{"F9",KEY_F9}, {"F10",KEY_F10}, {"F11",KEY_F11}, {"F12",KEY_F12},
//14
{"PRINTSCREEN",KEY_PRINTSCREEN}, {"SCROLL_LOCK",KEY_SCROLL_LOCK},
{"PAUSE",KEY_PAUSE}, {"INSERT",KEY_INSERT}, {"HOME",KEY_HOME},
{"PAGE_UP",KEY_PAGE_UP}, {"DELETE",KEY_DELETE}, {"END",KEY_END},
{"PAGE_DOWN",KEY_PAGE_DOWN}, {"RIGHT",KEY_RIGHT}, {"LEFT",KEY_LEFT},
{"DOWN",KEY_DOWN}, {"UP",KEY_UP}, {"NUM_LOCK",KEY_NUM_LOCK},
//16 Keypad keys
{"KEYPAD_SLASH",KEYPAD_SLASH}, {"KEYPAD_ASTERIX",KEYPAD_ASTERIX},
{"KEYPAD_MINUS",KEYPAD_MINUS}, {"KEYPAD_PLUS",KEYPAD_PLUS},
{"KEYPAD_ENTER",KEYPAD_ENTER}, {"KEYPAD_PERIOD",KEYPAD_PERIOD},
{"KEYPAD_1",KEYPAD_1}, {"KEYPAD_2",KEYPAD_2}, {"KEYPAD_3",KEYPAD_3},
{"KEYPAD_4",KEYPAD_4}, {"KEYPAD_5",KEYPAD_5}, {"KEYPAD_6",KEYPAD_6},
{"KEYPAD_7",KEYPAD_7}, {"KEYPAD_8",KEYPAD_8}, {"KEYPAD_9",KEYPAD_9},
{"KEYPAD_0",KEYPAD_0},
//12 Meta Keys
{"CTRL",KEY_CTRL}, {"SHIFT",KEY_SHIFT}, {"ALT",KEY_ALT}, {"GUI",KEY_GUI},
{"LEFT_CTRL",KEY_LEFT_CTRL}, {"LEFT_SHIFT",KEY_LEFT_SHIFT},
{"LEFT_ALT",KEY_LEFT_ALT}, {"LEFT_GUI",KEY_LEFT_GUI},
{"RIGHT_CTRL",KEY_RIGHT_CTRL}, {"RIGHT_SHIFT",KEY_RIGHT_SHIFT},
{"RIGHT_ALT",KEY_RIGHT_ALT}, {"RIGHT_GUI",KEY_RIGHT_GUI},
//8 Mouse Directions
{"MOUSE_N",MOUSE_N}, {"MOUSE_E",MOUSE_E}, {"MOUSE_S",MOUSE_S}, {"MOUSE_W",MOUSE_W},
{"MOUSE_H",MOUSE_H}, {"MOUSE_V",MOUSE_V},
//3 Mouse Buttons
{"MOUSE_BTN_LEFT",MOUSE_BTN_LEFT}, {"MOUSE_BTN_MIDDLE",MOUSE_BTN_MIDDLE}, {"MOUSE_BTN_RIGHT",MOUSE_BTN_RIGHT},
//4 Scroll Functions
{"SCROLL_N",SCROLL_N}, {"SCROLL_E",SCROLL_E}, {"SCROLL_S",SCROLL_S}, {"SCROLL_W",SCROLL_W},
{"SCROLL_H",SCROLL_H}, {"SCROLL_V",SCROLL_V},
};
char maps[2][ROWS][COLS] = {
{
//LEFT SIDE
{ KEY_ESC , KEY_1 , KEY_2 , KEY_3 , KEY_4 , KEY_5 , /*RightH*/MOUSE_H },
{ KEY_TAB , KEY_Q , KEY_W , KEY_E , KEY_R , KEY_T , /*RightV*/MOUSE_V },
{ KEY_CAPS_LOCK , KEY_A , KEY_S , KEY_D , KEY_F , KEY_G , /*LeftH*/SCROLL_H },
{ KEY_LEFT_SHIFT , KEY_Z , KEY_X , KEY_C , KEY_V , KEY_B , /*LeftV*/SCROLL_V },
{ KEY_LEFT_CTRL , KEY_LEFT_ALT , KEY_LEFT_GUI , /*PED1*/KEY_TOGGLE, KEY_SPACE , MOUSE_BTN_LEFT , 0 },
//RIGHT SIDE
{ KEY_6 , KEY_7 , KEY_8 , KEY_9 , KEY_0 , KEY_MINUS , KEY_BACKSPACE },
{ KEY_Y , KEY_U , KEY_I , KEY_O , KEY_P , KEY_LEFT_BRACE , KEY_RIGHT_BRACE},
{ KEY_H , KEY_J , KEY_K , KEY_L , KEY_SEMICOLON , KEY_QUOTE , KEY_ENTER },
{ KEY_N , KEY_M , KEY_COMMA , KEY_PERIOD , KEY_BACKSLASH , KEY_SLASH , KEY_RIGHT_SHIFT},
{ MOUSE_BTN_RIGHT, KEY_SPACE , /*PED2*/MOUSE_BTN_LEFT, KEY_RIGHT_GUI , KEY_RIGHT_ALT , KEY_RIGHT_CTRL , KEY_TOGGLE },
},
{
//LEFT SIDE
{ KEY_TILDE , KEY_F1 , KEY_F2 , KEY_F3 , KEY_F4 , KEY_F5 , 0 },
{ KEY_TAB , 0 , MACRO_1 , MACRO_2 , MACRO_3 , 0 , 0 },
{ KEY_CAPS_LOCK , 0 , MOUSE_W , MOUSE_N , MOUSE_S , MOUSE_E , 0 },
{ KEY_LEFT_SHIFT, 0 , SCROLL_W , SCROLL_N , SCROLL_S , SCROLL_E , 0 },
{ KEY_LEFT_CTRL , KEY_LEFT_ALT , KEY_LEFT_GUI , 0 , KEY_BACKSPACE , MOUSE_BTN_LEFT , 0 },
//RIGHT SIDE
{ KEY_F6 , KEY_F7 , KEY_F8 , KEY_F9 , KEY_F10 , KEY_F11 , KEY_EQUAL },
{ 0 , MOUSE_BTN_LEFT , MOUSE_BTN_MIDDLE, MOUSE_BTN_RIGHT, KEY_PAGE_UP , KEY_INSERT , 0 },
{ KEY_LEFT , KEY_DOWN , KEY_UP , KEY_RIGHT , KEY_PAGE_DOWN , KEY_DELETE , KEY_ENTER },
{ 0 , KEY_HOME , 0 , KEY_END , 0 , 0 , KEY_RIGHT_SHIFT},
{ MOUSE_BTN_RIGHT, KEY_BACKSPACE , 0 , KEY_RIGHT_GUI , KEY_RIGHT_ALT , KEY_RIGHT_CTRL , KEY_TOGGLE },
}
};
char key_history[ROWS][COLS] = {EMPTY_ROW,EMPTY_ROW,EMPTY_ROW,EMPTY_ROW,EMPTY_ROW,EMPTY_ROW,EMPTY_ROW,EMPTY_ROW,EMPTY_ROW,EMPTY_ROW};
uint16_t key_ticks[ROWS][COLS] = {ZERO_ROW,ZERO_ROW,ZERO_ROW,ZERO_ROW,ZERO_ROW,ZERO_ROW,ZERO_ROW,ZERO_ROW,ZERO_ROW,ZERO_ROW};
int check_col(int c, int r) {
int out = 0;
if(r==4 && c==3) c=10; // Pedal 1
if(r==9 && c==2) c=11; // Pedal 2
switch (c) {
case 0: out = (PIND & (1<<5)); break;
case 1: out = (PINE & (1<<0)); break;
case 2: out = (PINC & (1<<7)); break;
case 3: out = (PINE & (1<<1)); break;
case 4: out = (PIND & (1<<7)); break;
case 5: out = (PINC & (1<<0)); break;
case 6: out = (PINC & (1<<1)); break;
case 10: out = (PINE & (1<<6)); break; // Pedal 1
case 11: out = (PINE & (1<<7)); break; // Pedal 2
}
out = (out ^ 0); // Inverting for pullup resistors
#if DEBUG
if (out) print(" ");
else print("*");
#endif
return out;
} // check_col
void turn_row(int i, int on) {
int out = 0;
switch (i) {
case 0: out = on ? (PORTD &= ~(1<<2)) : (PORTD |= (1<<2)); break;
case 1: out = on ? (PORTD &= ~(1<<0)) : (PORTD |= (1<<0)); break;
case 2: out = on ? (PORTD &= ~(1<<3)) : (PORTD |= (1<<3)); break;
case 3: out = on ? (PORTD &= ~(1<<1)) : (PORTD |= (1<<1)); break;
case 4: out = on ? (PORTD &= ~(1<<4)) : (PORTD |= (1<<4)); break;
case 5: out = on ? (PORTC &= ~(1<<4)) : (PORTC |= (1<<4)); break;
case 6: out = on ? (PORTC &= ~(1<<2)) : (PORTC |= (1<<2)); break;
case 7: out = on ? (PORTC &= ~(1<<5)) : (PORTC |= (1<<5)); break;
case 8: out = on ? (PORTC &= ~(1<<3)) : (PORTC |= (1<<3)); break;
case 9: out = on ? (PORTC &= ~(1<<6)) : (PORTC |= (1<<6)); break;
}
} // turn_row
int main(void) {
int8_t mouse_h, mouse_v, scroll_h, scroll_v, mouse_btn_left, mouse_btn_middle, mouse_btn_right;
uint8_t row, col, idx, key, delta, which_map;
uint8_t mouse_btn_left_prev = 0;
uint8_t mouse_btn_middle_prev = 0;
uint8_t mouse_btn_right_prev = 0;
uint16_t ticks;
// set for 16 MHz clock
CPU_PRESCALE(0);
// Turn off unneeded analog pins
DIDR0 = 0b00001111;
//0=In 1=Out
// 0b76543210;
DDRC = 0b01111100;
DDRD = 0b01011111;
DDRE = 0b11000000;
DDRF = 0b00000000;
//if DDR=0, 0=Normal, 1=Pullup Resistor
//if DDR=1, 0=Low Output, 1=High Output
// 0b76543210;
PORTC = 0b11111111;
PORTD = 0b10111111; //LED off till board is ready
PORTE = 0b11111111;
PORTF = 0b11111111;
// Initialize the USB, and then wait for the host to set configuration.
// If the Teensy is powered without a PC connected to the USB port,
// this will wait forever.
usb_init();
while (!usb_configured()) /* wait */ ;
// Wait an extra second for the PC's operating system to load drivers
// and do whatever it does to actually be ready for input
_delay_ms(1000);
print("Initializing\n");
analog_autocalibrate_center();
print("Autocalibration - DONE\n");
//TODO PARSE SD CARD
//PORTD &= ~(1<<6);
PORTD |= (1<<6); //Turn LED on?
print("Myhackit is ready!\n");
uint16_t test=0;
while (1) { // Main logic loop
// Re-initialize buffers
for(idx=0; idx<sizeof(keyboard_keys); idx++) keyboard_keys[idx] = 0; //TODO Seems like this could be reset with bit flipping?
keyboard_modifier_keys = idx = 0;
mouse_btn_left = mouse_btn_middle = mouse_btn_right = 0;
mouse_h = mouse_v = scroll_h = scroll_v = 0;
// TODO Map this out into the dead spots in the grid somehow?
test++;
analog_update(0);
if(test % 32 == 0) {
mouse_h = analogs[3].moving/20;
mouse_v = analogs[2].moving/20;
}
if(test % 128 == 0) {
scroll_v = analogs[0].moving/20; //~250-290 total points
scroll_h = analogs[1].moving/20;
}
#if DEBUG
print("\n");
_delay_ms(20);
continue;
#endif
// Read in the current key state
which_map = 0;
for(row=0; row<ROWS; row++){
turn_row(row, ON);
for(col=0; col<COLS; col++){
if (check_col(col, row)){
key_history[row][col] |= 1;
}
key_history[row][col] <<= 1;
#if DEBUG
phex(key_history[row][col]);print(" ");
#endif
switch (key_history[row][col]){
case JUST_SWITCHED:
key_ticks[row][col] = 1;
break;
case STILL_ON:
key_ticks[row][col] += 1;
if(maps[0][row][col] == KEY_TOGGLE){
which_map = 1; // Use the alternate map
}
break;
default:
key_ticks[row][col] = 0;
break;
} //switch
} //for col
turn_row(row, OFF);
#if DEBUG
print("\n");
#endif
} //for row
#if DEBUG
print("\n");
_delay_ms(20);
continue;
#endif
// Determine which keys are pressed
for(col=0; col<COLS; col++){
for(row=0; row<ROWS; row++){
if(key_history[row][col] != STILL_ON){ //We only care about non-bounced
continue;
}
key = maps[which_map][row][col];
if (key == KEY_TOGGLE){
continue; // Skip toggle buttons
} else if ( key >= KEY_CTRL && key <= KEY_RIGHT_GUI ){
keyboard_modifier_keys |= 1<<(key-224); // Could also change USB report to do this
} else if (key >= MOUSE_N && key < KEY_TOGGLE){ // Digital pointer concerns
ticks = key_ticks[row][col];
if (ticks > 120 ) delta = 4;
else if (ticks > 80 ) delta = 3;
else if (ticks > 40 ) delta = 2;
else delta = 1;
if (key == MOUSE_N) mouse_v -= delta;
if (key == MOUSE_S) mouse_v += delta;
if (key == MOUSE_E) mouse_h += delta;
if (key == MOUSE_W) mouse_h -= delta;
if (key == MOUSE_BTN_LEFT) mouse_btn_left = 1;
if (key == MOUSE_BTN_MIDDLE) mouse_btn_middle = 1;
if (key == MOUSE_BTN_RIGHT) mouse_btn_right = 1;
if (key == SCROLL_N && ticks % ANALOG_SPEED == 1) scroll_v = delta;
if (key == SCROLL_E && ticks % ANALOG_SPEED == 1) scroll_h = -delta;
if (key == SCROLL_S && ticks % ANALOG_SPEED == 1) scroll_v = -delta;
if (key == SCROLL_W && ticks % ANALOG_SPEED == 1) scroll_h = delta;
} else if (key >= MACRO_1 && key <= MACRO_8){ //Macro Concerns
//start_macro(MACRO_1 - key); //TODO
} else { // Not a special case, just boring old keypress
if (idx<7){ // Throwing away more than 6 at a time, damn usb :(
keyboard_keys[idx++] = key;
}
} // if decide
} // for row
} //for col
// run_macros(keyboard_keys, idx); //TODO
// Send digital keyboard signals
usb_keyboard_send(); // Send the buffered commands
// Send alaog (mouse+wheel) signals
if (mouse_h || mouse_v || scroll_v || scroll_h){
usb_mouse_move(mouse_h, mouse_v, scroll_v, scroll_h);
}
if ( mouse_btn_left != mouse_btn_left_prev ||
mouse_btn_middle != mouse_btn_middle_prev ||
mouse_btn_right != mouse_btn_right_prev
) {
usb_mouse_buttons(mouse_btn_left, mouse_btn_middle, mouse_btn_right);
}
mouse_btn_left_prev = mouse_btn_left;
mouse_btn_middle_prev = mouse_btn_middle;
mouse_btn_right_prev = mouse_btn_right;
} // while
} // main