diff 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
line wrap: on
line diff
--- a/main.c	Thu Sep 29 15:30:24 2011 -0700
+++ b/main.c	Thu Sep 29 22:22:19 2011 -0700
@@ -19,51 +19,57 @@
  * THE SOFTWARE.
  */
 
+// ----- Includes -----
+
+// AVR Includes
 #include <avr/io.h>
 #include <avr/pgmspace.h>
 #include <avr/interrupt.h>
-#include <util/delay.h>
+
+// Project Includes
 //#include "usb_keys.h"
 #include "scan_loop.h"
 //#include "layouts.h"
 //#include "usb_keyboard.h"
 
-// TEMP INCLUDES
 #include "usb_keyboard_debug.h"
 #include "print.h"
-
-#define CPU_PRESCALE(n)	(CLKPR = 0x80, CLKPR = (n))
+#include "led.h"
 
 
 
+// ----- Defines -----
 
 // Verified Keypress Defines
 #define USB_TRANSFER_DIVIDER 10 // 1024 == 1 Send of keypresses per second, 1 == 1 Send of keypresses per ~1 millisecond
 
-// Error LED Control
-void errorLED( uint8_t on )
-{
-	// Error LED On
-	if ( on ) {
-		PORTD |= (1<<6);
-	}
-	// Error LED Off
-	else {
-		PORTD &= ~(1<<6);
-	}
-}
+
+
+// ----- Macros -----
+#define CPU_PRESCALE(n)	(CLKPR = 0x80, CLKPR = (n))
 
 
 
-// Initial Pin Setup
-// If the matrix is properly set, this function does not need to be changed
+// ----- Variables -----
+
+// Timer Interrupt for flagging a send of the sampled key detection data to the USB host
+uint16_t sendKeypressCounter = 0;
+
+// Flag generated by the timer interrupt
+volatile uint8_t sendKeypresses = 0;
+
+
+
+// ----- Functions -----
+
+// Initial Pin Setup, make sure they are sane
 inline void pinSetup(void)
 {
 	// For each pin, 0=input, 1=output
 	DDRA = 0x00;
 	DDRB = 0x00;
 	DDRC = 0x00;
-	DDRD = 0x40; // LED Setup
+	DDRD = 0x00;
 	DDRE = 0x00;
 	DDRF = 0x00;
 
@@ -72,12 +78,12 @@
 	PORTA = 0x00;
 	PORTB = 0x00;
 	PORTC = 0x00;
-	PORTD = 0x40; // LED Enable
+	PORTD = 0x00;
 	PORTE = 0x00;
 	PORTF = 0x00;
 }
 
-int main( void )
+int main(void)
 {
 	// Setup with 16 MHz clock
 	CPU_PRESCALE( 0 );
@@ -85,15 +91,8 @@
 	// Configuring Pins
 	pinSetup();
 
-	// Initialize the USB, and then wait for the host to set configuration.
-	// If the Teensy is powered without a PC connected to the USB port,
-	// this will wait forever.
-	usb_init();
-	while ( !usb_configured() ) /* wait */ ;
-
-	// Wait an extra second for the PC's operating system to load drivers
-	// and do whatever it does to actually be ready for input
-	_delay_ms(1000);
+	// Setup USB Module
+	usb_setup();
 
 	// Setup ISR Timer for flagging a kepress send to USB
 	// Set to 256 * 1024 (8 bit timer with Clock/1024 prescalar) timer
@@ -101,7 +100,6 @@
 	TCCR0B = 0x03;
 	TIMSK0 = (1 << TOIE0);
 
-	uint16_t led = 0;
 	// Main Detection Loop
 	while ( 1 ) {
 		//scan_loop();
@@ -111,12 +109,19 @@
 
 		// HID Debug Error message
 		erro_print("Detection loop error, this is very bad...bug report!");
+
+		// Send keypresses over USB if the ISR has signalled that it's time
+		if ( !sendKeypresses )
+			continue;
+
+		// Send USB Data
+		usb_send();
+
+		// Clear sendKeypresses Flag
+		sendKeypresses = 0;
 	}
 }
 
-// Timer Interrupt for flagging a send of the sampled key detection data to the USB host
-uint16_t sendKeypressCounter = 0;
-
 ISR( TIMER0_OVF_vect )
 {
 	sendKeypressCounter++;