-
Notifications
You must be signed in to change notification settings - Fork 0
/
naputin.c
339 lines (309 loc) · 9.75 KB
/
naputin.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
#include <stdbool.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include "usb_keyboard.h"
#include "helpers.h"
// zeros as empty array places
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
uint16_t idle_count = 0;
int main(void)
{
uint8_t operatingSystemFlag = 0; // 0 = windows, 1 = mac
uint8_t previousOperatingSystemFlag = 0; // 0 = windows, 1 = mac
uint8_t reset_idle;
uint8_t bootloaderCounter = 0;
uint8_t modeChangeCounter = 0;
uint8_t keys[15][6];
uint8_t keys_2[15][6];
uint8_t modifier_keys[8][2];
uint8_t layer_2_selector[2];
bool state[6][15] = {0};
bool state_prev[6][15] = {0};
bool layer_2_state = false;
char *volatile const inputColumns[15] = {
&PIND, // D
&PIND, // D
&PINC, // C
&PINC, // C
&PIND, // D
&PIND, // D
&PINB, // B
&PINB, // B
&PINB, // B
&PINF, // F
&PINF, // F
&PINF, // F
&PINF, // F
&PINF, // F
&PINF}; // F
const uint8_t inputColumnShifts[15] = {
2, // 2
3, // 3
6, // 6
7, // 7
6, // 6
7, // 7
4, // 4
5, // 5
6, // 6
7, // 7
6, // 6
5, // 5
4, // 4
1, // 1
0}; // 0
char *volatile const outputRows[6] = {
&PORTD,
&PORTD,
&PORTB,
&PORTB,
&PORTB,
&PORTB};
const uint8_t outputRowShifts[6] = {
1,
0,
7,
3,
2,
1};
CPU_PRESCALE(0);
// read operating system flag from eeprom
operatingSystemFlag = readOperatingSystemFlag();
// flip previous flag to trigger keymap change on first run
previousOperatingSystemFlag = !operatingSystemFlag;
usb_init();
while (!usb_configured())
;
// wait for system to become ready
_delay_ms(1000);
// Configure timer 0 to generate a timer overflow interrupt every
// 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
TCCR0A = 0x00;
TCCR0B = 0x05;
TIMSK0 = (1 << TOIE0);
// configure ports 0 = input, 1 = output, 1 = unused (as outputs)
// columns (inputs): F0 F1 F4 F5 F6 F7 B6 B5 B4 D7 D6 C7 C6 D3 D2
// rows (outputs): D1 D0 B7 B3 B2 B1 B0
DDRB = 0b10001111; // 3,2,1,0 outputs, 6,5,4 inputs, 7 unused
DDRC = 0b00111111; // outputs, 7,6 inputs, 5,4,3,2,1,0 unused
DDRD = 0b00110011; // 1,0 outputs, 7,6,3,2 inputs, 4,5, unused
DDRF = 0b00001100; // outputs, 0,1,4,5,6,7 inputs, 2,3 unused
// startup done blink indicator
for (int i = 0; i < 10; i++)
{
PORTB |= 0x01;
_delay_ms(100);
PORTB &= ~(0x01);
_delay_ms(100);
}
while (1)
{
// check if operating system flag has changed
if (operatingSystemFlag == 1 && previousOperatingSystemFlag == 0)
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 6; j++)
{
keys[i][j] = keys_osx[i][j];
keys_2[i][j] = keys_osx_2[i][j];
}
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 2; j++)
{
modifier_keys[i][j] = modifier_keys_osx[i][j];
}
}
layer_2_selector[0] = layer_2_selector_osx[0];
layer_2_selector[1] = layer_2_selector_osx[1];
previousOperatingSystemFlag = operatingSystemFlag;
}
else if (operatingSystemFlag == 0 && previousOperatingSystemFlag == 1)
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 6; j++)
{
keys[i][j] = keys_ms[i][j];
keys_2[i][j] = keys_ms_2[i][j];
}
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 2; j++)
{
modifier_keys[i][j] = modifier_keys_ms[i][j];
}
}
layer_2_selector[0] = layer_2_selector_ms[0];
layer_2_selector[1] = layer_2_selector_ms[1];
previousOperatingSystemFlag = operatingSystemFlag;
}
// set all pins high expect led pin
PORTB = 0xfe;
PORTC = 0xff;
PORTD = 0xff;
PORTF = 0xff;
// set led pin high if capslock on
PORTB |= (keyboard_leds >> 1 & 0x01);
// read all ports
for (int i = 0; i < 6; i++)
{
// set i:th row low
*(outputRows[i]) &= ~(0x01 << outputRowShifts[i]);
// check all columns
for (int b = 0; b < 15; b++)
{
state[i][b] = (*(inputColumns[b]) >> inputColumnShifts[b]) & 0x01;
}
// set i:th row back high
*(outputRows[i]) |= (0x01 << outputRowShifts[i]);
}
// logic for tracking 2 layer state
for (int r = 0; r < 6; r++)
{
for (int b = 0; b < 15; b++)
{
if (isLayer2Active(b, r, layer_2_selector))
{
if (state[r][b] && state_prev[r][b])
{
layer_2_state = false;
}
if (!state[r][b] && !state_prev[r][b])
{
layer_2_state = true;
}
}
}
}
// Check if bootloader access keys are pressed
if (!state[0][0] && !state_prev[0][0] && !state[0][14] && !state_prev[0][14])
{
PORTB |= 0x01;
_delay_ms(100);
PORTB &= ~(0x01);
_delay_ms(200);
bootloaderCounter++;
if (bootloaderCounter > 2)
{
jumpToBootloader();
}
}
// Check if mode change keys are pressed
else if (!state[0][0] && !state_prev[0][0] && !state[1][14] && !state_prev[1][14])
{
PORTB |= 0x01;
_delay_ms(50);
PORTB &= ~(0x01);
_delay_ms(100);
modeChangeCounter++;
if (modeChangeCounter > 2)
{
writeOperatingSystemFlag(!operatingSystemFlag);
operatingSystemFlag = !operatingSystemFlag;
PORTB |= 0x01;
_delay_ms(500);
PORTB &= ~(0x01);
_delay_ms(100);
modeChangeCounter = 0;
}
}
else
{
// reset counters
bootloaderCounter = 0;
modeChangeCounter = 0;
// sum logic for tracking changes
for (int r = 0; r < 6; r++)
{
for (int b = 0; b < 15; b++)
{
// released
if (state[r][b] && state_prev[r][b])
{
reset_idle = 1;
if (isModifierKey(b, r, modifier_keys))
{
// reversed because of pull up
keyboard_modifier_keys &= ~(keys[b][r]);
}
else
{
for (int d = 0; d < 6; d++)
{
if (keyboard_keys[d] != 0x00 && (keyboard_keys[d] == keys[b][r] || keyboard_keys[d] == keys_2[b][r]))
{
keyboard_keys[d] = 0x00;
d = 5;
}
}
}
}
// pressed
if (!state[r][b] && !state_prev[r][b])
{
if (isModifierKey(b, r, modifier_keys))
{
// reversed because of pull up
keyboard_modifier_keys |= keys[b][r];
}
else
{
for (int d = 0; d < 6; d++)
{
if (keyboard_keys[d] == 0x00)
{
if (layer_2_state)
{
PORTB |= 0x01;
keyboard_keys[d] = keys_2[b][r];
}
else
{
keyboard_keys[d] = keys[b][r];
}
d = 5;
}
}
}
}
}
}
}
// update previous state
for (int r = 0; r < 6; r++)
{
for (int b = 0; b < 15; b++)
{
state_prev[r][b] = state[r][b];
}
}
// reset idle interrupt
if (reset_idle)
{
cli();
idle_count = 0;
sei();
}
usb_keyboard_send();
_delay_us(100);
}
}
// This interrupt routine is run approx 61 times per second.
// A very simple inactivity timeout is implemented, where we
// will send a space character.
ISR(TIMER0_OVF_vect)
{
idle_count++;
if (idle_count > 61 * 8)
{
idle_count = 0;
usb_keyboard_send();
}
}