-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
323 lines (282 loc) · 9.27 KB
/
main.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
/* Name: main.c
* Project: hid-mouse, a very simple HID example
* Author: Christian Starkjohann
* Creation Date: 2008-04-07
* Tabsize: 4
* Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
* License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
* This Revision: $Id: main.c 692 2008-11-07 15:07:40Z cs $
*/
/*
This example should run on most AVRs with only little changes. No special
hardware resources except INT0 are used. You may have to change usbconfig.h for
different I/O pins for USB. Please note that USB D+ must be the INT0 pin, or
at least be connected to INT0 as well.
We use VID/PID 0x046D/0xC00E which is taken from a Logitech mouse. Don't
publish any hardware using these IDs! This is for demonstration only!
*/
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h> /* for sei() */
#include <util/delay.h> /* for _delay_ms() */
#include <avr/pgmspace.h> /* required by usbdrv.h */
#include "usbdrv.h"
#include "oddebug.h" /* This is also an example for using debug macros */
#include "keys.h"
#define XTAL 16e6 // 16 MHz
#define PHASE_A (PINC & 1<<PC5) // an Pinbelegung anpassen
#define PHASE_B (PINC & 1<<PC6) // an Pinbelegung anpassen
volatile int8_t enc_delta; // Drehgeberbewegung zwischen
// zwei Auslesungen im Hauptprogramm
volatile uint8_t counterLoop = 0;
volatile uint8_t counterSaved = 0;
volatile uint8_t sendStack[32] = {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};
volatile uint8_t sendStackSend = 0;
volatile uint8_t sendStackAdd = 0;
/* ------------------------------------------------------------------------- */
/* ----------------------------- USB interface ----------------------------- */
/* ------------------------------------------------------------------------- */
const PROGMEM char usbHidReportDescriptor[35] = { /* USB report descriptor */
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x25, 0x65, // LOGICAL_MAXIMUM (101)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0 // END_COLLECTION
};
/* This is the same report descriptor as seen in a Logitech mouse. The data
* described by this descriptor consists of 4 bytes:
* . . . . . B2 B1 B0 .... one byte with mouse button states
* X7 X6 X5 X4 X3 X2 X1 X0 .... 8 bit signed relative coordinate x
* Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 .... 8 bit signed relative coordinate y
* W7 W6 W5 W4 W3 W2 W1 W0 .... 8 bit signed relative coordinate wheel
*/
typedef struct{
uchar mod;
uchar key;
}report_t;
static report_t reportBuffer;
static uchar idleRate; /* repeat rate for keyboards, never used for mice */
volatile uint8_t test = 0;
volatile uint16_t counter = 0;
volatile uint16_t counterRotate = 0;
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
usbRequest_t *rq = (void *)data;
/* The following requests are never used. But since they are required by
* the specification, we implement them in this example.
*/
if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
/* we only have one report type, so don't look at wValue */
usbMsgPtr = (void *)&reportBuffer;
return sizeof(reportBuffer);
} else if(rq->bRequest == USBRQ_HID_GET_IDLE) {
usbMsgPtr = &idleRate;
return 1;
} else if(rq->bRequest == USBRQ_HID_SET_IDLE) {
idleRate = rq->wValue.bytes[1];
}
} else {
/* no vendor specific requests implemented */
}
return 0; /* default for not implemented requests: return no data back to host */
}
ISR(TIMER0_OVF_vect) {
counter++;
counterRotate++;
if ((PINC & 0x06) == 0) {
asm volatile ("jmp 0");
}
}
ISR(INT1_vect) {
cli();
// PA4 PA5
if ((PIND & 0x10) == 0) { // rotate right
//if ((PIND & (1<<PD4)) == 0) { // rotate right
sendStack[sendStackAdd] = 0x41;
sendStackAdd = (sendStackAdd+1)&0x03;
//PORTA ^= 1 << PA4;
} else { // rotate left
sendStack[sendStackAdd] = 0x40;
sendStackAdd = (sendStackAdd+1)&0x03;
//PORTA ^= 1 << PA5;
}
sei();
}
int8_t encode_read( void ) // Encoder auslesen
{
int8_t val;
// atomarer Variablenzugriff
cli();
val = enc_delta;
enc_delta = 0;
sei();
return val;
}
int main(void) {
uchar i;
DDRC = 0x00;
DDRA = 0xff;
PORTA = 0x00;
DDRD &= ~(1<<PD3 | 1<<PD4);
while ((PINC & 0x01) == 1) {
}
PORTA = 0xF0;
wdt_enable(WDTO_1S);
/* Even if you don't use the watchdog, turn it off here. On newer devices,
* the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
*/
/* RESET status: all port bits are inputs without pull-up.
* That's the way we need D+ and D-. Therefore we don't need any
* additional hardware initialization.
*/
odDebugInit();
usbInit();
usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
i = 0;
while (--i) { /* fake USB disconnect for > 250 ms */
wdt_reset();
_delay_ms(1);
}
usbDeviceConnect();
// Enable timer0
TCCR0 = (1<<CS10) | (1<<CS01) | (1<<CS00); // CTC, XTAL / 64
TIMSK |= (1<<TOIE0);
// Enable INT1 for falling edges
MCUCR |= (1<<ISC11);
MCUCR &= ~(1<<ISC10);
GICR |= (1<<INT1);
sei();
uint8_t pinC = 0x1F;
uint8_t last = 0x1F;
uint8_t history[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
uint8_t first = 1;
uint8_t use, j;
uint8_t historyPosition = 0;
uint8_t beep = 0;
for (;;) { /* main event loop */
wdt_reset();
usbPoll();
//reportBuffer.mod = MOD_SHIFT_LEFT;
//reportBuffer.key = KEY_A + (PINC & 0x0F);
if (counter > 0x1FF && counter < 0x2FF && beep == 1) {
PORTA |= (1<<PA6);
} else {
PORTA &= ~(1<<PA6);
}
history[(historyPosition++) & 0xF] = (PINC & 0x1F);
first = history[(historyPosition+1) & 0xF];
use = 1;
for (j=1; j < 16; j++) {
if (history[(historyPosition+1+j) & 0xF] != first) {
use = 0;
break;
}
}
if (use == 1) {
last = first;
if (counter > 0x1FF && beep == 0) {
if (last == 0x13) {
PORTA |= (1<<PA6);
beep = 1;
sendStack[sendStackAdd] = 0x50;
sendStackAdd = (sendStackAdd+1)&0x03;
}
}
}
if (last != pinC) {
sendStack[sendStackAdd] = KEY_A;
if (pinC != 0x1F) {
// PORTA |= (1<<PA6);
if (counter < 0x1FF) {
sendStack[sendStackAdd] = (pinC & 0x1F);
} else {
sendStack[sendStackAdd] = 0x20 + (pinC & 0x1F);
}
sendStackAdd = (sendStackAdd+1)&0x03;
beep = 0;
} else {
beep = 1;
}
pinC = last;
counter = 0;
}
if(usbInterruptIsReady()) {
reportBuffer.mod = 0;
reportBuffer.key = 0;
if (sendStackSend != sendStackAdd) {
switch (sendStack[sendStackSend]) {
case 0x1E: // button 1 short
reportBuffer.mod = MOD_SHIFT_LEFT | MOD_CONTROL_LEFT;
reportBuffer.key = KEY_TAB;
break;
case 0x1D: // button 2 short
reportBuffer.mod = MOD_CONTROL_LEFT;
reportBuffer.key = KEY_TAB;
break;
case 0x1B: // button 3 short
reportBuffer.mod = 0;
reportBuffer.key = KEY_UP_ARROW;
break;
case 0x17: // button 4 short
reportBuffer.mod = 0;
reportBuffer.key = KEY_DOWN_ARROW;
break;
case 0x0F: // button 5 short
reportBuffer.mod = 0;
reportBuffer.key = KEY_RETURN;
break;
case 0x3E: // button 1 long
reportBuffer.mod = MOD_CONTROL_LEFT;
reportBuffer.key = KEY_W;
break;
case 0x3D: // button 2 long
reportBuffer.mod = MOD_SHIFT_LEFT | MOD_CONTROL_LEFT;
reportBuffer.key = KEY_RETURN;
break;
case 0x3B: // button 3 long
reportBuffer.mod = 0;
reportBuffer.key = KEY_F5;
break;
case 0x37: // button 4 long
reportBuffer.mod = MOD_ALT_LEFT;
reportBuffer.key = KEY_POS1;
break;
case 0x2F: // button 5 long
reportBuffer.mod = MOD_ALT_LEFT;
reportBuffer.key = KEY_LEFT_ARROW;
break;
case 0x41: // rotate right
reportBuffer.mod = 0;
reportBuffer.key = KEY_TAB;
break;
case 0x40: // rotate left
reportBuffer.mod = MOD_SHIFT_LEFT;
reportBuffer.key = KEY_TAB;
break;
case 0x50: // shutdown
reportBuffer.mod = MOD_CONTROL_LEFT | MOD_ALT_LEFT;
reportBuffer.key = KEY_BACKSPACE;
break;
}
sendStackSend = (sendStackSend+1)&0x03;
}
usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
}
counterLoop++;
}
return 0;
}
/* ------------------------------------------------------------------------- */