annotate main.c @ 24:2d65da455ef3

Formalizing code module structure and inheritance (Large Commit) - Commenting cleaned up - usb_keyboard_debug is not finished yet in terms of cleanup (needs DEBUB_PRINT #ifdef's) - Added LoadFile script generation - Formalized KeyIndex and USBKeys data containers - Split parts of scan_loop into Macro, USB, and main.c - Added interface to USB modules - Added interface to Macro modules - Added proper Debug module enable/disable
author Jacob Alexander <triplehaata@gmail.com>
date Thu, 29 Sep 2011 22:22:19 -0700
parents 7a95e742a31f
children c2ef3daf39e1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
1 /* Copyright (C) 2011 by Jacob Alexander
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
2 *
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
4 * of this software and associated documentation files (the "Software"), to deal
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
5 * in the Software without restriction, including without limitation the rights
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
7 * copies of the Software, and to permit persons to whom the Software is
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
8 * furnished to do so, subject to the following conditions:
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
9 *
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
10 * The above copyright notice and this permission notice shall be included in
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
11 * all copies or substantial portions of the Software.
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
12 *
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
19 * THE SOFTWARE.
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
20 */
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
21
24
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
22 // ----- Includes -----
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
23
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
24 // AVR Includes
0
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
25 #include <avr/io.h>
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
26 #include <avr/pgmspace.h>
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
27 #include <avr/interrupt.h>
24
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
28
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
29 // Project Includes
17
85bc68be98df Code re-factor now compiles.
Jacob Alexander <triplehaata@gmail.com>
parents: 16
diff changeset
30 //#include "usb_keys.h"
16
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
31 #include "scan_loop.h"
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
32 //#include "layouts.h"
2
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
33 //#include "usb_keyboard.h"
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
34
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
35 #include "usb_keyboard_debug.h"
18
7a95e742a31f Small change while the build system is in flux.
Jacob Alexander <triplehaata@gmail.com>
parents: 17
diff changeset
36 #include "print.h"
24
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
37 #include "led.h"
0
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
38
6
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
39
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
40
24
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
41 // ----- Defines -----
6
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
42
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
43 // Verified Keypress Defines
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
44 #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
1
0f88e9aad77a Initial macro line filler design for multiple keyboards matrices.
Jacob Alexander <triplehaata@gmail.com>
parents: 0
diff changeset
45
24
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
46
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
47
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
48 // ----- Macros -----
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
49 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
2
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
50
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
51
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
52
24
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
53 // ----- Variables -----
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
54
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
55 // Timer Interrupt for flagging a send of the sampled key detection data to the USB host
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
56 uint16_t sendKeypressCounter = 0;
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
57
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
58 // Flag generated by the timer interrupt
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
59 volatile uint8_t sendKeypresses = 0;
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
60
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
61
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
62
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
63 // ----- Functions -----
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
64
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
65 // Initial Pin Setup, make sure they are sane
6
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
66 inline void pinSetup(void)
2
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
67 {
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
68 // For each pin, 0=input, 1=output
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
69 DDRA = 0x00;
5
a8be29294c26 Updating for the latest pinout
Jacob Alexander <triplehaata@gmail.com>
parents: 4
diff changeset
70 DDRB = 0x00;
a8be29294c26 Updating for the latest pinout
Jacob Alexander <triplehaata@gmail.com>
parents: 4
diff changeset
71 DDRC = 0x00;
24
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
72 DDRD = 0x00;
16
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
73 DDRE = 0x00;
5
a8be29294c26 Updating for the latest pinout
Jacob Alexander <triplehaata@gmail.com>
parents: 4
diff changeset
74 DDRF = 0x00;
2
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
75
6
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
76
2
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
77 // Setting pins to either high or pull-up resistor
16
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
78 PORTA = 0x00;
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
79 PORTB = 0x00;
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
80 PORTC = 0x00;
24
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
81 PORTD = 0x00;
16
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
82 PORTE = 0x00;
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
83 PORTF = 0x00;
2
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
84 }
7
18e424630cbd Adding basic per line keypad detection
Jacob Alexander <haata@users.sf.net>
parents: 6
diff changeset
85
24
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
86 int main(void)
0
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
87 {
6
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
88 // Setup with 16 MHz clock
0
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
89 CPU_PRESCALE( 0 );
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
90
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
91 // Configuring Pins
2
c3b2eaa4a89d Significant progress made.
Jacob Alexander <triplehaata@gmail.com>
parents: 1
diff changeset
92 pinSetup();
0
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
93
24
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
94 // Setup USB Module
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
95 usb_setup();
0
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
96
6
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
97 // Setup ISR Timer for flagging a kepress send to USB
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
98 // Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
99 TCCR0A = 0x00;
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
100 TCCR0B = 0x03;
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
101 TIMSK0 = (1 << TOIE0);
4
b97797936037 Redefining pinouts and adding key sending code.
Jacob Alexander <triplehaata@gmail.com>
parents: 3
diff changeset
102
0
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
103 // Main Detection Loop
16
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
104 while ( 1 ) {
17
85bc68be98df Code re-factor now compiles.
Jacob Alexander <triplehaata@gmail.com>
parents: 16
diff changeset
105 //scan_loop();
16
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
106
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
107 // Loop should never get here (indicate error)
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
108 errorLED( 1 );
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
109
17
85bc68be98df Code re-factor now compiles.
Jacob Alexander <triplehaata@gmail.com>
parents: 16
diff changeset
110 // HID Debug Error message
85bc68be98df Code re-factor now compiles.
Jacob Alexander <triplehaata@gmail.com>
parents: 16
diff changeset
111 erro_print("Detection loop error, this is very bad...bug report!");
24
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
112
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
113 // Send keypresses over USB if the ISR has signalled that it's time
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
114 if ( !sendKeypresses )
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
115 continue;
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
116
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
117 // Send USB Data
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
118 usb_send();
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
119
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
120 // Clear sendKeypresses Flag
2d65da455ef3 Formalizing code module structure and inheritance (Large Commit)
Jacob Alexander <triplehaata@gmail.com>
parents: 18
diff changeset
121 sendKeypresses = 0;
16
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
122 }
f74b797ac060 Parts of the upcoming rewrite
Jacob Alexander <triplehaata@gmail.com>
parents: 15
diff changeset
123 }
0
43dfb6e4e697 Initial Commit
Jacob Alexander <triplehaata@gmail.com>
parents:
diff changeset
124
6
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
125 ISR( TIMER0_OVF_vect )
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
126 {
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
127 sendKeypressCounter++;
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
128 if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
129 sendKeypressCounter = 0;
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
130 sendKeypresses = 1;
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
131 }
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
132 }
9df636be6feb Keyboard functionally working with very very good debouncing.
Jacob Alexander <triplehaata@gmail.com>
parents: 5
diff changeset
133