]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Initial commit of the Epson QX-10 Keyboard module
[kiibohd-controller.git] / main.c
1 /* Copyright (C) 2011 by Jacob Alexander
2  * 
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 // ----- Includes -----
23
24 // AVR Includes
25 #include <avr/io.h>
26 #include <avr/interrupt.h>
27
28 // Project Includes
29 #include <macro.h>
30 #include <scan_loop.h>
31 #include <usb_com.h>
32
33 #include <led.h>
34 #include <print.h>
35
36
37
38 // ----- Defines -----
39
40 // Verified Keypress Defines
41 #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
42
43
44
45 // ----- Macros -----
46 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
47
48
49
50 // ----- Variables -----
51
52 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
53 uint16_t sendKeypressCounter = 0;
54
55 // Flag generated by the timer interrupt
56 volatile uint8_t sendKeypresses = 0;
57
58
59
60 // ----- Functions -----
61
62 // Initial Pin Setup, make sure they are sane
63 inline void pinSetup(void)
64 {
65
66         // For each pin, 0=input, 1=output
67 #if defined(__AVR_AT90USB1286__)
68         DDRA = 0x00;
69 #endif
70         DDRB = 0x00;
71         DDRC = 0x00;
72         DDRD = 0x00;
73         DDRE = 0x00;
74         DDRF = 0x00;
75
76
77         // Setting pins to either high or pull-up resistor
78 #if defined(__AVR_AT90USB1286__)
79         PORTA = 0x00;
80 #endif
81         PORTB = 0x00;
82         PORTC = 0x00;
83         PORTD = 0x00;
84         PORTE = 0x00;
85         PORTF = 0x00;
86 }
87
88 int main(void)
89 {
90         // Setup with 16 MHz clock
91         CPU_PRESCALE( 0 );
92
93         // Configuring Pins
94         pinSetup();
95         init_errorLED();
96
97         // Setup USB Module
98         usb_setup();
99
100         // Setup ISR Timer for flagging a kepress send to USB
101         // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
102         TCCR0A = 0x00;
103         TCCR0B = 0x03;
104         TIMSK0 = (1 << TOIE0);
105
106         // Main Detection Loop
107         uint8_t ledTimer = 15; // Enable LED for a short time
108         while ( 1 )
109         {
110                 // Setup the scanning module
111                 cli();
112                 scan_setup();
113                 sei();
114
115                 while ( 1 )
116                 {
117                         // Acquire Key Indices
118                         // Loop continuously until scan_loop returns 0
119                         cli();
120                         while ( scan_loop() );
121                         sei();
122
123                         // Run Macros over Key Indices and convert to USB Keys
124                         process_macros();
125
126                         // Send keypresses over USB if the ISR has signalled that it's time
127                         if ( !sendKeypresses )
128                                 continue;
129
130                         // Send USB Data
131                         usb_send();
132
133                         // Clear sendKeypresses Flag
134                         sendKeypresses = 0;
135
136                         // Indicate Error, if valid
137                         errorLED( ledTimer );
138
139                         if ( ledTimer > 0 )
140                                 ledTimer--;
141                 }
142
143                 // Loop should never get here (indicate error)
144                 ledTimer = 255;
145
146                 // HID Debug Error message
147                 erro_print("Detection loop error, this is very bad...bug report!");
148         }
149 }
150
151 // USB Keyboard Data Send Counter Interrupt
152 ISR( TIMER0_OVF_vect )
153 {
154         sendKeypressCounter++;
155         if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
156                 sendKeypressCounter = 0;
157                 sendKeypresses = 1;
158         }
159 }
160