-
Notifications
You must be signed in to change notification settings - Fork 13
/
main_alpha.c
174 lines (153 loc) · 3.84 KB
/
main_alpha.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
/* This file is part of ukbdc.
*
* ukbdc is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* ukbdc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ukbdc; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "platforms.h"
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <util/delay.h>
#include <stdbool.h>
#include "usb_keyboard.h"
#include "io.h"
#include "hid.h"
#include "rawhid.h"
#include "rawhid_protocol.h"
#include "dataflash.h"
#include "layout.h"
#include "hc595.h"
#include <stdio.h> /* sprintf */
volatile uint8_t matrix[19][8] = {
{41, 0, 0, 0, 0, 52, 0, 0},
{20, 49, 36, 21, 6, 7, 35, 48},
{0, 0, 58, 0, 0, 0, 0, 0},
{19, 50, 37, 22, 5, 8, 34, 47},
{0, 0, 59, 0, 0, 27, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{16, 0, 0, 25, 2, 11, 31, 44},
{17, 56, 39, 24, 3, 10, 32, 45},
{14, 0, 0, 40, 0, 13, 29, 42},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{15, 0, 0, 26, 1, 12, 30, 43},
{18, 51, 38, 23, 4, 9, 33, 46},
{0, 0, 0, 0, 54, 0, 0, 0},
{0, 0, 57, 0, 0, 0, 0, 55},
{0, 0, 0, 60, 28, 0, 53, 0},
};
bool states[19][8] = {{0}};
bool is_modifier(uint8_t key_n)
{
static const uint8_t modifiers[] = {41, 52, 53, 54, 55, 57, 58, 59, 60};
for (uint8_t i = 0; i < sizeof(modifiers); ++i)
if (key_n == modifiers[i])
return true;
return false;
}
uint16_t get_input()
{
uint16_t ret = 0;
for (int i = 0; i < 16; ++i) {
ret <<= 1;
ret |= IO_get(i) ? 0x0001 : 0x0000;
}
return ret;
}
void scan_matrix()
{
bool changed = false;
for (uint8_t i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j)
IO_set(j | 0x80, true);
IO_set((i) | 0x80, false);
_delay_us(10);
for (uint8_t j = 0; j < 19; ++j) {
bool state = !IO_get(j);
uint8_t layer = 0;
uint8_t code = LAYOUT_get_scancode(layer, matrix[j][i]);
if (state != states[j][i]) {
changed = true;
states[j][i] = state;
switch (state) {
case true:
if (code == KEY_ESC && (HID_scancode_is_pressed(KEY_LEFT_SHIFT) || HID_scancode_is_pressed(KEY_RIGHT_SHIFT)))
code = KEY_TILDE;
HID_set_scancode_state(code, true);
break;
case false:
if (code == KEY_ESC)
HID_set_scancode_state(KEY_TILDE, false);
HID_set_scancode_state(code, false);
break;
}
}
}
}
if (changed)
HID_commit_state();
}
int main(void)
{
clock_prescale_set(clock_div_1);
for (int i = 0; i < NUM_IO; ++i) {
IO_config(i, INPUT);
IO_set(i, true);
}
PORTB |= _BV(PB0);
DDRB |= _BV(PB2) | _BV(PB1) | _BV(PB0); // MOSI, SCK, MEMCS
DDRC |= _BV(PC7) | _BV(PC6); // LATCH, BUFEN
DDRB &= ~_BV(PB3); // MISO
PORTB &= ~_BV(PB3);
USB_init();
while (USB_get_configuration() == 0)
;
HID_commit_state();
int size;
DATAFLASH_read_page(1, sizeof(size), &size);
if (size != -1) {
uint8_t *layout = malloc(size);
DATAFLASH_read_page(2, (uint16_t)size, layout);
RAWHID_send(layout);
LAYOUT_set(layout);
}
DATAFLASH_read_page(0, sizeof(matrix), matrix);
TCCR0A = 0x00;
TCCR0B = 0x03; /* clk_io / 64 */
TIMSK0 = _BV(TOIE0);
while(1) {
RAWHID_PROTOCOL_task();
}
while (1)
;
}
void MAIN_handle_sof()
{
}
ISR(TIMER0_OVF_vect)
{
uint8_t prev_endp = USB_get_endpoint();
static uint8_t num = 0;
if (num > 16) {
num = 0;
scan_matrix();
} else {
++num;
}
USB_set_endpoint(prev_endp);
}