]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Formalizing code module structure and inheritance (Large Commit)
[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/pgmspace.h>
27 #include <avr/interrupt.h>
28
29 // Project Includes
30 //#include "usb_keys.h"
31 #include "scan_loop.h"
32 //#include "layouts.h"
33 //#include "usb_keyboard.h"
34
35 #include "usb_keyboard_debug.h"
36 #include "print.h"
37 #include "led.h"
38
39
40
41 // ----- Defines -----
42
43 // Verified Keypress Defines
44 #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
45
46
47
48 // ----- Macros -----
49 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
50
51
52
53 // ----- Variables -----
54
55 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
56 uint16_t sendKeypressCounter = 0;
57
58 // Flag generated by the timer interrupt
59 volatile uint8_t sendKeypresses = 0;
60
61
62
63 // ----- Functions -----
64
65 // Initial Pin Setup, make sure they are sane
66 inline void pinSetup(void)
67 {
68         // For each pin, 0=input, 1=output
69         DDRA = 0x00;
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         PORTA = 0x00;
79         PORTB = 0x00;
80         PORTC = 0x00;
81         PORTD = 0x00;
82         PORTE = 0x00;
83         PORTF = 0x00;
84 }
85
86 int main(void)
87 {
88         // Setup with 16 MHz clock
89         CPU_PRESCALE( 0 );
90
91         // Configuring Pins
92         pinSetup();
93
94         // Setup USB Module
95         usb_setup();
96
97         // Setup ISR Timer for flagging a kepress send to USB
98         // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
99         TCCR0A = 0x00;
100         TCCR0B = 0x03;
101         TIMSK0 = (1 << TOIE0);
102
103         // Main Detection Loop
104         while ( 1 ) {
105                 //scan_loop();
106
107                 // Loop should never get here (indicate error)
108                 errorLED( 1 );
109
110                 // HID Debug Error message
111                 erro_print("Detection loop error, this is very bad...bug report!");
112
113                 // Send keypresses over USB if the ISR has signalled that it's time
114                 if ( !sendKeypresses )
115                         continue;
116
117                 // Send USB Data
118                 usb_send();
119
120                 // Clear sendKeypresses Flag
121                 sendKeypresses = 0;
122         }
123 }
124
125 ISR( TIMER0_OVF_vect )
126 {
127         sendKeypressCounter++;
128         if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
129                 sendKeypressCounter = 0;
130                 sendKeypresses = 1;
131         }
132 }
133