]> git.donarmstrong.com Git - kiibohd-controller.git/blob - main.c
Code re-factor now compiles.
[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 #include <avr/io.h>
23 #include <avr/pgmspace.h>
24 #include <avr/interrupt.h>
25 #include <util/delay.h>
26 //#include "usb_keys.h"
27 #include "scan_loop.h"
28 //#include "layouts.h"
29 //#include "usb_keyboard.h"
30
31 // TEMP INCLUDES
32 #include "usb_keyboard_debug.h"
33 #include <print.h>
34
35 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
36
37
38
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 // Error LED Control
44 void errorLED( uint8_t on )
45 {
46         // Error LED On
47         if ( on ) {
48                 PORTD |= (1<<6);
49         }
50         // Error LED Off
51         else {
52                 PORTD &= ~(1<<6);
53         }
54 }
55
56
57
58 // Initial Pin Setup
59 // If the matrix is properly set, this function does not need to be changed
60 inline void pinSetup(void)
61 {
62         // For each pin, 0=input, 1=output
63         DDRA = 0x00;
64         DDRB = 0x00;
65         DDRC = 0x00;
66         DDRD = 0x40; // LED Setup
67         DDRE = 0x00;
68         DDRF = 0x00;
69
70
71         // Setting pins to either high or pull-up resistor
72         PORTA = 0x00;
73         PORTB = 0x00;
74         PORTC = 0x00;
75         PORTD = 0x40; // LED Enable
76         PORTE = 0x00;
77         PORTF = 0x00;
78 }
79
80 int main( void )
81 {
82         // Setup with 16 MHz clock
83         CPU_PRESCALE( 0 );
84
85         // Configuring Pins
86         pinSetup();
87
88         // Initialize the USB, and then wait for the host to set configuration.
89         // If the Teensy is powered without a PC connected to the USB port,
90         // this will wait forever.
91         usb_init();
92         while ( !usb_configured() ) /* wait */ ;
93
94         // Wait an extra second for the PC's operating system to load drivers
95         // and do whatever it does to actually be ready for input
96         _delay_ms(1000);
97
98         // Setup ISR Timer for flagging a kepress send to USB
99         // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
100         TCCR0A = 0x00;
101         TCCR0B = 0x03;
102         TIMSK0 = (1 << TOIE0);
103
104         uint16_t led = 0;
105         // Main Detection Loop
106         while ( 1 ) {
107                 //scan_loop();
108
109                 // Loop should never get here (indicate error)
110                 errorLED( 1 );
111
112                 // HID Debug Error message
113                 erro_print("Detection loop error, this is very bad...bug report!");
114         }
115 }
116
117 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
118 uint16_t sendKeypressCounter = 0;
119
120 ISR( TIMER0_OVF_vect )
121 {
122         sendKeypressCounter++;
123         if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
124                 sendKeypressCounter = 0;
125                 sendKeypresses = 1;
126         }
127 }
128