changeset 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 92de7e0a0a10
children c2ef3daf39e1
files CMakeLists.txt Debug/basic/print.c Debug/basic/print.h Debug/basic/setup.cmake Debug/full/setup.cmake Debug/led/led.c Debug/led/led.h Debug/led/setup.cmake Debug/off/led.h Debug/off/print.h Debug/off/setup.cmake Debug/print/print.c Debug/print/print.h Debug/print/setup.cmake Keymap/keymap.h Keymap/usb_keys.h LoadFile/bash LoadFile/load Macro/basic/macro.c Macro/basic/macro.h Macro/basic/setup.cmake Scan/matrix/matrix.c Scan/matrix/matrix.h Scan/matrix/scan_loop.c Scan/matrix/scan_loop.h USB/pjrc/setup.cmake USB/pjrc/usb_com.c USB/pjrc/usb_com.h USB/pjrc/usb_keyboard_debug.c USB/pjrc/usb_keyboard_debug.h main.c setup.cmake
diffstat 32 files changed, 1064 insertions(+), 488 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Thu Sep 29 15:30:24 2011 -0700
+++ b/CMakeLists.txt	Thu Sep 29 22:22:19 2011 -0700
@@ -9,11 +9,8 @@
 
 #| Set the Compilers (must be set first)
 include( CMakeForceCompiler )
-set( CMAKE_SYSTEM_NAME Generic )
 cmake_force_c_compiler  ( avr-gcc AVRCCompiler )
 cmake_force_cxx_compiler( avr-g++ AVRCxxCompiler )
-set( CMAKE_CROSSCOMPILING on )
-message( STATUS ${CMAKE_CROSSCOMPILING})
 
 
 ###
@@ -185,3 +182,20 @@
 	COMMENT "Size after generation:"
 )
 
+
+
+###
+# Setup Loader Script
+#
+
+#| Provides the user with the correct teensy-loader-cli command for the built .HEX file
+#| teensy-loader-cli must be in the user's path
+if( ${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" )
+	configure_file( LoadFile/bash load )
+endif( ${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" )
+
+#| TODO Windows
+if( ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )
+	message( STATUS "Load Script is on my TODO List for Windows..." )
+endif( ${CMAKE_SYSTEM_NAME} MATCHES "Windows" )
+
--- a/Debug/basic/print.c	Thu Sep 29 15:30:24 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,190 +0,0 @@
-/* Copyright (C) 2011 by Jacob Alexander
- * 
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- * 
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-// Compiler Includes
-#include <stdarg.h>
-
-// AVR Includes
-#include <avr/io.h>
-#include <avr/pgmspace.h>
-
-// Project Includes
-#include "print.h"
-
-// Defines
-
-
-// USB HID String Output
-void usb_debug_putstr( char* s )
-{
-	while ( *s != '\0' )
-		usb_debug_putchar( *s++ );
-}
-
-// Multiple string Output
-void usb_debug_putstrs( char* first, ... )
-{
-	// Initialize the variadic function parameter list
-	va_list ap;
-
-	// Get the first parameter
-	va_start( ap, first );
-	char *cur = first;
-
-	// Loop through the variadic list until "\0\0\0" is found
-	while ( !( cur[0] == '\0' && cur[1] == '\0' && cur[2] == '\0' ) )
-	{
-		// Print out the given string
-		usb_debug_putstr( cur );
-
-		// Get the next argument ready
-		cur = va_arg( ap, char* );
-	}
-
-	va_end( ap ); // Not required, but good practice
-}
-
-// Print a constant string
-void _print(const char *s)
-{
-	char c;
-
-	// Acquire the character from flash, and print it, as long as it's not NULL
-	// Also, if a newline is found, print a carrige return as well
-	while ( ( c = pgm_read_byte(s++) ) != '\0' )
-	{
-		if ( c == '\n' )
-			usb_debug_putchar('\r');
-		usb_debug_putchar(c);
-	}
-}
-
-
-
-
-// String Functions
-void int8ToStr( uint8_t in, char* out )
-{
-	// Position and sign containers
-	uint8_t pos;
-	pos = 0;
-
-	// Evaluate through digits as decimal
-	do
-	{
-		out[pos++] = in % 10 + '0';
-	}
-	while ( (in /= 10) > 0 );
-
-	// Append null
-	out[pos] = '\0';
-
-	// Reverse the string to the correct order
-	revsStr(out);
-}
-
-
-void int16ToStr( uint16_t in, char* out )
-{
-	// Position and sign containers
-	uint16_t pos;
-	pos = 0;
-
-	// Evaluate through digits as decimal
-	do
-	{
-		out[pos++] = in % 10 + '0';
-	}
-	while ( (in /= 10) > 0 );
-
-	// Append null
-	out[pos] = '\0';
-
-	// Reverse the string to the correct order
-	revsStr(out);
-}
-
-
-void hexToStr_op( uint16_t in, char* out, uint8_t op )
-{
-	// Position container
-	uint16_t pos = 0;
-
-	// Evaluate through digits as hex
-	do
-	{
-		uint16_t cur = in % 16;
-		out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
-	}
-	while ( (in /= 16) > 0 );
-
-	// Output formatting options
-	switch ( op )
-	{
-	case 1: // Add 0x
-		out[pos++] = 'x';
-		out[pos++] = '0';
-		break;
-	case 2: //  8-bit padding
-	case 4: // 16-bit padding
-		while ( pos < op )
-			out[pos++] = '0';
-		break;
-	}
-
-	// Append null
-	out[pos] = '\0';
-
-	// Reverse the string to the correct order
-	revsStr(out);
-}
-
-
-void revsStr( char* in )
-{
-	// Iterators
-	int i, j;
-
-	// Temp storage
-	char c;
-
-	// Loop through the string, and reverse the order of the characters
-	for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
-	{
-		c = in[i];
-		in[i] = in[j];
-		in[j] = c;
-	}
-}
-
-
-uint16_t lenStr( char* in )
-{
-	// Iterator
-	char *pos;
-
-	// Loop until null is found
-	for ( pos = in; *pos; pos++ );
-
-	// Return the difference between the pointers of in and pos (which is the string length)
-	return (pos - in);
-}
-
--- a/Debug/basic/print.h	Thu Sep 29 15:30:24 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,87 +0,0 @@
-/* Copyright (C) 2011 by Jacob Alexander
- * 
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- * 
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#ifndef print_h__
-#define print_h__
-
-// AVR Includes
-#include <avr/pgmspace.h>
-
-// Project Includes
-#include "usb_keyboard_debug.h"
-
-// Defines
-#define NL "\r\n"
-
-
-/* XXX
- * Note that all the variadic functions below, take comma separated string lists, they are purposely not printf style (simplicity)
- */
-
-// Function Aliases
-#define dPrint(c)         usb_debug_putchar(c)
-#define dPrintStr(c)      usb_debug_putstr (c)
-#define dPrintStrs(...)   usb_debug_putstrs(__VA_ARGS__, "\0\0\0")      // Convenience Variadic Macro
-#define dPrintStrNL(c)    dPrintStrs       (c, NL)                      // Appends New Line Macro
-#define dPrintStrsNL(...) usb_debug_putstrs(__VA_ARGS__, NL, "\0\0\0")  // Appends New Line Macro
-
-// Special Msg Constructs (Uses VT100 tags)
-#define dPrintMsg(colour_code_str,msg,...) \
-                          usb_debug_putstrs("\033[", colour_code_str, "m", msg, "\033[0m - ", __VA_ARGS__, NL, "\0\0\0")
-#define printMsg(colour_code_str,msg,str) \
-                          print("\033[" colour_code_str "m" msg "\033[0m - " str NL)
-
-// Info Messages
-#define info_dPrint(...)  dPrintMsg        ("1;32",   "INFO",    __VA_ARGS__) // Info Msg
-#define info_print(str)   printMsg         ("1;32",   "INFO",    str)         // Info Msg
-
-// Warning Messages
-#define warn_dPrint(...)  dPrintMsg        ("1;33",   "WARNING", __VA_ARGS__) // Warning Msg
-#define warn_print(str)   printMsg         ("1;33",   "WARNING", str)         // Warning Msg
-
-// Error Messages
-#define erro_dPrint(...)  dPrintMsg        ("1;5;31", "ERROR",   __VA_ARGS__) // Error Msg
-#define erro_print(str)   printMsg         ("1;5;31", "ERROR",   str)         // Error Msg
-
-// Debug Messages
-#define dbug_dPrint(...)  dPrintMsg        ("1;35",   "DEBUG",   __VA_ARGS__) // Debug Msg
-#define dbug_print(str)   printMsg         ("1;35",   "DEBUG",   str)         // Debug Msg
-
-// Static String Printing
-#define print(s) _print(PSTR(s))
-
-void _print(const char *s);
-void usb_debug_putstr( char* s );
-void usb_debug_putstrs( char* first, ... );
-
-
-
-// String Functions
-#define hexToStr(hex, out) hexToStr_op(hex, out, 1)
-
-void int8ToStr  ( uint8_t  in,   char*  out );
-void int16ToStr ( uint16_t in,   char*  out );
-void hexToStr_op( uint16_t in,   char*  out, uint8_t op );
-void revsStr    ( char*  in );
-uint16_t lenStr ( char*  in );
-
-#endif
-
--- a/Debug/basic/setup.cmake	Thu Sep 29 15:30:24 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,22 +0,0 @@
-###| CMake Kiibohd Controller Debug Module |###
-#
-# Written by Jacob Alexander in 2011 for the Kiibohd Controller
-#
-# Released into the Public Domain
-#
-###
-
-
-###
-# Module C files
-#
-
-set( DEBUG_SRCS
-	print.c
-)
-
-
-###
-# Module Specific Options
-#
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debug/full/setup.cmake	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,28 @@
+###| CMake Kiibohd Controller Debug Module |###
+#
+# Written by Jacob Alexander in 2011 for the Kiibohd Controller
+#
+# Released into the Public Domain
+#
+###
+
+
+###
+# Module C files
+#
+
+#| XXX Requires the ../ due to how the paths are constructed
+set( DEBUG_SRCS
+	../led/led.c
+	../print/print.c
+)
+
+
+###
+# Module Specific Options
+#
+add_definitions(
+	-I${HEAD_DIR}/Debug/led
+	-I${HEAD_DIR}/Debug/print
+)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debug/led/led.c	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,52 @@
+/* Copyright (C) 2011 by Jacob Alexander
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+// ----- Includes -----
+
+// AVR Includes
+
+// Project Includes
+#include "led.h"
+
+
+
+// ----- Functions -----
+
+// Error LED Setup
+void init_errorLED()
+{
+	// Use pin D6 as an output (LED)
+	DDRD |= (1<<6);
+}
+
+// Error LED Control
+void errorLED( uint8_t on )
+{
+	// Error LED On (D6)
+	if ( on ) {
+		PORTD |= (1<<6);
+	}
+	// Error LED Off
+	else {
+		PORTD &= ~(1<<6);
+	}
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debug/led/led.h	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,38 @@
+/* Copyright (C) 2011 by Jacob Alexander
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef __led_h
+#define __led_h
+
+// ----- Includes -----
+
+// AVR Includes
+#include <avr/io.h>
+
+
+
+// ----- Functions -----
+
+void init_errorLED( void );
+void errorLED( uint8_t on );
+
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debug/led/setup.cmake	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,28 @@
+###| CMake Kiibohd Controller Debug Module |###
+#
+# Written by Jacob Alexander in 2011 for the Kiibohd Controller
+#
+# Released into the Public Domain
+#
+###
+
+
+###
+# Module C files
+#
+
+set( DEBUG_SRCS
+	led.c
+)
+
+
+###
+# Module Specific Options
+#
+
+
+###
+# Just in case, you only want this module and are using others as well
+#
+add_definitions( -I${HEAD_DIR}/Debug/off )
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debug/off/led.h	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,30 @@
+/* Copyright (C) 2011 by Jacob Alexander
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef __led_h
+#define __led_h
+
+// ----- Disabler Defines -----
+#define init_errorLED()
+#define errorLED(on)
+
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debug/off/print.h	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,70 @@
+/* Copyright (C) 2011 by Jacob Alexander
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef print_h__
+#define print_h__
+
+// ----- Disabler Defines -----
+
+#define dPrint(c)
+#define dPrintStr(c)
+#define dPrintStrs(...)
+#define dPrintStrNL(c)
+#define dPrintStrsNL(...)
+
+// Special Msg Constructs (Uses VT100 tags)
+#define dPrintMsg(colour_code_str,msg,...)
+#define printMsg(colour_code_str,msg,str)
+
+// Info Messages
+#define info_dPrint(...)
+#define info_print(str)
+
+// Warning Messages
+#define warn_dPrint(...)
+#define warn_print(str)
+
+// Error Messages
+#define erro_dPrint(...)
+#define erro_print(str)
+
+// Debug Messages
+#define dbug_dPrint(...)
+#define dbug_print(str)
+
+// Static String Printing
+#define print(s) _print(PSTR(s))
+
+// Output Functions
+#define _print(s)
+#define usb_debug_putstr(s)
+#define usb_debug_putstrs(s, ...)
+
+// String Functions
+#define hexToStr(hex, out)
+#define int8ToStr(in, out)
+#define int16ToStr(in, out)
+#define hexToStr_op(in, out, op)
+#define revsStr(in)
+#define lenStr(in)
+
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debug/off/setup.cmake	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,19 @@
+###| CMake Kiibohd Controller Debug Module |###
+#
+# Written by Jacob Alexander in 2011 for the Kiibohd Controller
+#
+# Released into the Public Domain
+#
+###
+
+
+###
+# Module C files
+#
+#| None!
+
+
+###
+# Module Specific Options
+#
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debug/print/print.c	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,189 @@
+/* Copyright (C) 2011 by Jacob Alexander
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+// ----- Includes -----
+
+// Compiler Includes
+#include <stdarg.h>
+
+// Project Includes
+#include "print.h"
+
+
+
+// ----- Functions -----
+
+// USB HID String Output
+void usb_debug_putstr( char* s )
+{
+	while ( *s != '\0' )
+		usb_debug_putchar( *s++ );
+}
+
+// Multiple string Output
+void usb_debug_putstrs( char* first, ... )
+{
+	// Initialize the variadic function parameter list
+	va_list ap;
+
+	// Get the first parameter
+	va_start( ap, first );
+	char *cur = first;
+
+	// Loop through the variadic list until "\0\0\0" is found
+	while ( !( cur[0] == '\0' && cur[1] == '\0' && cur[2] == '\0' ) )
+	{
+		// Print out the given string
+		usb_debug_putstr( cur );
+
+		// Get the next argument ready
+		cur = va_arg( ap, char* );
+	}
+
+	va_end( ap ); // Not required, but good practice
+}
+
+// Print a constant string
+void _print(const char *s)
+{
+	char c;
+
+	// Acquire the character from flash, and print it, as long as it's not NULL
+	// Also, if a newline is found, print a carrige return as well
+	while ( ( c = pgm_read_byte(s++) ) != '\0' )
+	{
+		if ( c == '\n' )
+			usb_debug_putchar('\r');
+		usb_debug_putchar(c);
+	}
+}
+
+
+
+
+// String Functions
+void int8ToStr( uint8_t in, char* out )
+{
+	// Position and sign containers
+	uint8_t pos;
+	pos = 0;
+
+	// Evaluate through digits as decimal
+	do
+	{
+		out[pos++] = in % 10 + '0';
+	}
+	while ( (in /= 10) > 0 );
+
+	// Append null
+	out[pos] = '\0';
+
+	// Reverse the string to the correct order
+	revsStr(out);
+}
+
+
+void int16ToStr( uint16_t in, char* out )
+{
+	// Position and sign containers
+	uint16_t pos;
+	pos = 0;
+
+	// Evaluate through digits as decimal
+	do
+	{
+		out[pos++] = in % 10 + '0';
+	}
+	while ( (in /= 10) > 0 );
+
+	// Append null
+	out[pos] = '\0';
+
+	// Reverse the string to the correct order
+	revsStr(out);
+}
+
+
+void hexToStr_op( uint16_t in, char* out, uint8_t op )
+{
+	// Position container
+	uint16_t pos = 0;
+
+	// Evaluate through digits as hex
+	do
+	{
+		uint16_t cur = in % 16;
+		out[pos++] = cur + (( cur < 10 ) ? '0' : 'A' - 10);
+	}
+	while ( (in /= 16) > 0 );
+
+	// Output formatting options
+	switch ( op )
+	{
+	case 1: // Add 0x
+		out[pos++] = 'x';
+		out[pos++] = '0';
+		break;
+	case 2: //  8-bit padding
+	case 4: // 16-bit padding
+		while ( pos < op )
+			out[pos++] = '0';
+		break;
+	}
+
+	// Append null
+	out[pos] = '\0';
+
+	// Reverse the string to the correct order
+	revsStr(out);
+}
+
+
+void revsStr( char* in )
+{
+	// Iterators
+	int i, j;
+
+	// Temp storage
+	char c;
+
+	// Loop through the string, and reverse the order of the characters
+	for ( i = 0, j = lenStr( in ) - 1; i < j; i++, j-- )
+	{
+		c = in[i];
+		in[i] = in[j];
+		in[j] = c;
+	}
+}
+
+
+uint16_t lenStr( char* in )
+{
+	// Iterator
+	char *pos;
+
+	// Loop until null is found
+	for ( pos = in; *pos; pos++ );
+
+	// Return the difference between the pointers of in and pos (which is the string length)
+	return (pos - in);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debug/print/print.h	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,94 @@
+/* Copyright (C) 2011 by Jacob Alexander
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef print_h__
+#define print_h__
+
+// ----- Includes -----
+
+// AVR Includes
+#include <avr/pgmspace.h>
+
+// Project Includes
+#include "usb_keyboard_debug.h"
+
+
+
+// ----- Defines -----
+#define NL "\r\n"
+
+
+
+// ----- Functions and Corresponding Function Aliases -----
+
+/* XXX
+ * Note that all the variadic functions below, take comma separated string lists, they are purposely not printf style (simplicity)
+ */
+
+// Function Aliases
+#define dPrint(c)         usb_debug_putchar(c)
+#define dPrintStr(c)      usb_debug_putstr (c)
+#define dPrintStrs(...)   usb_debug_putstrs(__VA_ARGS__, "\0\0\0")      // Convenience Variadic Macro
+#define dPrintStrNL(c)    dPrintStrs       (c, NL)                      // Appends New Line Macro
+#define dPrintStrsNL(...) usb_debug_putstrs(__VA_ARGS__, NL, "\0\0\0")  // Appends New Line Macro
+
+// Special Msg Constructs (Uses VT100 tags)
+#define dPrintMsg(colour_code_str,msg,...) \
+                          usb_debug_putstrs("\033[", colour_code_str, "m", msg, "\033[0m - ", __VA_ARGS__, NL, "\0\0\0")
+#define printMsg(colour_code_str,msg,str) \
+                          print("\033[" colour_code_str "m" msg "\033[0m - " str NL)
+
+// Info Messages
+#define info_dPrint(...)  dPrintMsg        ("1;32",   "INFO",    __VA_ARGS__) // Info Msg
+#define info_print(str)   printMsg         ("1;32",   "INFO",    str)         // Info Msg
+
+// Warning Messages
+#define warn_dPrint(...)  dPrintMsg        ("1;33",   "WARNING", __VA_ARGS__) // Warning Msg
+#define warn_print(str)   printMsg         ("1;33",   "WARNING", str)         // Warning Msg
+
+// Error Messages
+#define erro_dPrint(...)  dPrintMsg        ("1;5;31", "ERROR",   __VA_ARGS__) // Error Msg
+#define erro_print(str)   printMsg         ("1;5;31", "ERROR",   str)         // Error Msg
+
+// Debug Messages
+#define dbug_dPrint(...)  dPrintMsg        ("1;35",   "DEBUG",   __VA_ARGS__) // Debug Msg
+#define dbug_print(str)   printMsg         ("1;35",   "DEBUG",   str)         // Debug Msg
+
+// Static String Printing
+#define print(s) _print(PSTR(s))
+
+void _print(const char *s);
+void usb_debug_putstr( char* s );
+void usb_debug_putstrs( char* first, ... );
+
+
+
+// String Functions
+#define hexToStr(hex, out) hexToStr_op(hex, out, 1)
+
+void int8ToStr  ( uint8_t  in,   char*  out );
+void int16ToStr ( uint16_t in,   char*  out );
+void hexToStr_op( uint16_t in,   char*  out, uint8_t op );
+void revsStr    ( char*  in );
+uint16_t lenStr ( char*  in );
+
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debug/print/setup.cmake	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,28 @@
+###| CMake Kiibohd Controller Debug Module |###
+#
+# Written by Jacob Alexander in 2011 for the Kiibohd Controller
+#
+# Released into the Public Domain
+#
+###
+
+
+###
+# Module C files
+#
+
+set( DEBUG_SRCS
+	print.c
+)
+
+
+###
+# Module Specific Options
+#
+
+
+###
+# Just in case, you only want this module and are using others as well
+#
+add_definitions( -I${HEAD_DIR}/Debug/off )
+
--- a/Keymap/keymap.h	Thu Sep 29 15:30:24 2011 -0700
+++ b/Keymap/keymap.h	Thu Sep 29 22:22:19 2011 -0700
@@ -1,16 +1,49 @@
+/* Copyright (C) 2011 by Jacob Alexander
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
 #ifndef __KEYMAP_h
 #define __KEYMAP_h
 
+// ----- Includes -----
+
 #include "usb_keys.h"
 
+
+
+// ----- Defines -----
+
 // Modifier Mask
 #define MODIFIERS_KEYPAD   0
 #define MODIFIERS_KEYBOARD 4
-static uint8_t    keypad_modifierMask[] = {};
+
+
+
+// ----- Variables -----
+
+//static uint8_t    keypad_modifierMask[] = {};
 static uint8_t  keyboard_modifierMask[] = { 1, 17, 33, 49 };
-static uint8_t alternate_modifierMask[] = { 1, 17, 33, 49, 62 };
+//static uint8_t alternate_modifierMask[] = { 1, 17, 33, 49, 62 };
 
 // Default 1-indexed key mappings
+/*
 static uint8_t keypadDefaultMap[] = { 0,
 				KEYPAD_7,
 				KEYPAD_8,
@@ -28,7 +61,7 @@
 				KEYPAD_0,
 				KEYPAD_PERIOD,
 				KEYPAD_PLUS };
-
+*/
 static uint8_t defaultMap[] = { 0,
 				KEY_GUI,
 				KEY_1,
@@ -93,7 +126,7 @@
 				KEY_LEFT,
 				KEY_RIGHT,
 				KEY_SPACE };
-
+/*
 static uint8_t navigationMap[] = { 0,
 				KEY_GUI,
 				KEY_F1,
@@ -223,6 +256,6 @@
 				165,
 				KEY_RIGHT_ALT,
 				KEY_SPACE };
-
+*/
 #endif
 
--- a/Keymap/usb_keys.h	Thu Sep 29 15:30:24 2011 -0700
+++ b/Keymap/usb_keys.h	Thu Sep 29 22:22:19 2011 -0700
@@ -1,6 +1,28 @@
+/* Copyright (C) 2011 by Jacob Alexander
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
 #ifndef __usb_keys_h
 #define __usb_keys_h
 
+// ----- Defines -----
 
 // List of Modifiers
 #define KEY_CTRL	0x01
@@ -236,9 +258,5 @@
 #define KEYS_RGUI       231
 // 232 - 65535 Reserved
 
-
-
-
-
 #endif
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LoadFile/bash	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,8 @@
+#!/bin/bash
+
+#| Loads the hex file onto the teensy
+
+sudo teensy-loader-cli -mmcu=@MCU@ -w @TARGET_HEX@
+
+exit 0
+
--- a/LoadFile/load	Thu Sep 29 15:30:24 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,8 +0,0 @@
-#!/bin/bash
-
-#| Loads the hex file onto the teensy++ 2.0
-
-sudo teensy-loader-cli -mmcu=at90usb1286 -w Build/main.hex
-
-exit 0
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Macro/basic/macro.c	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,86 @@
+/* Copyright (C) 2011 by Jacob Alexander
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+// ----- Includes -----
+
+// AVR Includes
+
+// Project Includes
+#include <usb_com.h>
+#include <scan_loop.h>
+#include <print.h>
+
+// Keymaps
+#include <keymap.h>
+#include <usb_keys.h>
+
+// Local Includes
+#include "macro.h"
+
+
+
+// ----- Functions -----
+
+// Given a sampling array, and the current number of detected keypress
+// Add as many keypresses from the sampling array to the USB key send array as possible.
+void keyPressDetection( uint8_t *keys, uint8_t *validKeys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map )
+{
+	for ( uint8_t key = 0; key < numberOfKeys + 1; key++ ) {
+		if ( keys[key] & (1 << 7) ) {
+			// TODO Debug Out
+			uint8_t modFound = 0;
+
+			// Determine if the key is a modifier
+			for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
+				// Modifier found
+				if ( modifiers[mod] == key ) {
+					USBKeys_Modifiers |= map[key];
+					modFound = 1;
+					break;
+				}
+			}
+			if ( modFound )
+				continue;
+
+			// Too many keys
+			if ( *validKeys >= USBKeys_MaxSize )
+				break;
+
+			// Allow ignoring keys with 0's
+			if ( map[key] != 0 )
+				USBKeys_Array[(*validKeys)++] = map[key];
+		}
+	}
+}
+
+void process_macros(void)
+{
+	// Layout Setup
+	uint8_t validKeys = 0;
+
+	uint8_t *keyboard_MODMASK = keyboard_modifierMask;
+	uint8_t  keyboard_NUMMODS = MODIFIERS_KEYBOARD;
+	uint8_t *keyboard_MAP     = defaultMap;
+
+	// Debounce Sampling Array to USB Data Array
+	keyPressDetection( KeyIndex_Array, &validKeys, KeyIndex_Size, keyboard_MODMASK, keyboard_NUMMODS, keyboard_MAP );
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Macro/basic/macro.h	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,38 @@
+/* Copyright (C) 2011 by Jacob Alexander
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef __macro_h
+#define __macro_h
+
+// ----- Includes -----
+
+// Compiler Includes
+#include <stdint.h>
+
+
+
+// ----- Functions -----
+
+void keyPressDetection( uint8_t *keys, uint8_t *validKeys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map );
+void process_macros(void);
+
+#endif
+
--- a/Macro/basic/setup.cmake	Thu Sep 29 15:30:24 2011 -0700
+++ b/Macro/basic/setup.cmake	Thu Sep 29 22:22:19 2011 -0700
@@ -12,6 +12,7 @@
 #
 
 set( MACRO_SRCS
+	macro.c
 )
 
 
--- a/Scan/matrix/matrix.c	Thu Sep 29 15:30:24 2011 -0700
+++ b/Scan/matrix/matrix.c	Thu Sep 29 22:22:19 2011 -0700
@@ -19,8 +19,15 @@
  * THE SOFTWARE.
  */
 
+// ----- Includes -----
+
+// Local Includes
 #include "matrix.h"
 
+
+
+// ----- Macros -----
+
 #define REG_SET(reg)	reg |= (1 << ( matrix[row][col] % 10 ) )
 			
 #define PIN_SET_COL(pin) \
@@ -59,6 +66,15 @@
 			break
 
 
+
+// ----- Variables -----
+
+uint8_t KeyIndex_Array[KEYBOARD_SIZE + 1];
+
+
+
+// ----- Functions -----
+
 void matrix_pinSetup( uint8_t *matrix )
 {
 	// Setup the variables
--- a/Scan/matrix/matrix.h	Thu Sep 29 15:30:24 2011 -0700
+++ b/Scan/matrix/matrix.h	Thu Sep 29 22:22:19 2011 -0700
@@ -22,6 +22,12 @@
 #ifndef __MATRIX_H
 #define __MATRIX_H
 
+// ----- Includes -----
+
+// Compiler Includes
+#include <stdint.h>
+
+
 // ----- Quick Map (don't change) -----
 #define pinA0  0
 #define pinA1  1
@@ -97,12 +103,13 @@
 
 
 // ----- Key Settings -----
-#define keyboardSize 16  // # of keys
+#define KEYBOARD_SIZE 16 // # of keys
+#define MAX_ROW_SIZE  16 // # of keys in the largest row
 
 
 
 // ----- Matrix Configuration -----
-static uint8_t matrix_pinout[][] = {
+static const uint8_t matrix_pinout[][MAX_ROW_SIZE + 1] = {
 
 
 
@@ -111,6 +118,7 @@
 // The mode allows for optimization in the kind of scanning algorithms that are done
 // 
 // The key numbers are used to translate into the keymap table (array) (and always start from 1, not 0).
+// Thus if a row doesn't use all the key positions, you can denote it as 0, which will be ignored/skipped on each scan
 // See the keymap.h file for the various preconfigured arrays.
 
 // Scan Mode | Col 1 | Col 2 | Col 3 | Col 4 | Col 4 | ...
@@ -135,11 +143,13 @@
 };
 
 
+
 // ----- Variables -----
 
 // NOTE: Highest Bit: Valid keypress (0x80 is valid keypress)
 //        Other Bits: Pressed state sample counter
-uint8_t keyboardDetectArray[keyboardSize + 1];
+extern              uint8_t KeyIndex_Array [KEYBOARD_SIZE + 1];
+       static const uint8_t KeyIndex_Size = KEYBOARD_SIZE;
 
 
 
--- a/Scan/matrix/scan_loop.c	Thu Sep 29 15:30:24 2011 -0700
+++ b/Scan/matrix/scan_loop.c	Thu Sep 29 22:22:19 2011 -0700
@@ -19,13 +19,23 @@
  * THE SOFTWARE.
  */
 
+// ----- Includes -----
 
-#include <stdint.h>
-#include <usb_keyboard_debug.h>
-#include <keymap.h>
+// Local Includes
+#include "scan_loop.h"
+
+
+
+// ----- Defines -----
+
 // Debouncing Defines
 #define SAMPLE_THRESHOLD 110
-#define MAX_SAMPLES 127 // Max is 127, reaching 128 is very bad
+#define MAX_SAMPLES      127 // Max is 127, reaching 128 is very bad
+
+
+
+// ----- Macros -----
+
 // Loop over all of the sampled keys of the given array
 // If the number of samples is higher than the sample threshold, flag the high bit, clear otherwise
 // This should be resetting VERY quickly, cutting off a potentially valid keypress is not an issue
@@ -34,104 +44,32 @@
 				table[key] = ( table[key] & ~(1 << 7) ) > SAMPLE_THRESHOLD ? (1 << 7) : 0x00; \
 			} \
 
-// NOTE: Highest Bit: Valid keypress (0x80 is valid keypress)
-//        Other Bits: Pressed state sample counter
-#define KEYBOARD_SIZE 23
-uint8_t keyboardDetectArray[KEYBOARD_SIZE + 1];
+
 
-// Interrupt Variable
-volatile uint8_t sendKeypresses = 0;
+// ----- Variables -----
 
-// USB Data Send
-void usb_send( uint8_t validKeys )
-{
-		// TODO undo potentially old keys
-		for ( uint8_t c = validKeys; c < 6; c++ )
-			keyboard_keys[c] = 0;
+// Keeps track of the number of scans, so we only do a debounce assess when it would be valid (as it throws away data)
+uint8_t scan_count = 0;
 
-		// Send keypresses
-		usb_keyboard_send();
-
-		// Clear sendKeypresses Flag
-		sendKeypresses = 0;
-
-		// Clear modifiers
-		keyboard_modifier_keys = 0;
-}
 
 
-// Given a sampling array, and the current number of detected keypress
-// Add as many keypresses from the sampling array to the USB key send array as possible.
-void keyPressDetection( uint8_t *keys, uint8_t *validKeys, uint8_t numberOfKeys, uint8_t *modifiers, uint8_t numberOfModifiers, uint8_t *map ) {
-	for ( uint8_t key = 0; key < numberOfKeys + 1; key++ ) {
-		if ( keys[key] & (1 << 7) ) {
-			pint8( key );
-			//print(" ");
-			uint8_t modFound = 0;
-
-			// Determine if the key is a modifier
-			for ( uint8_t mod = 0; mod < numberOfModifiers; mod++ ) {
-				// Modifier found
-				if ( modifiers[mod] == key ) {
-					keyboard_modifier_keys |= map[key];
-					modFound = 1;
-					break;
-				}
-			}
-			if ( modFound )
-				continue;
-
-			// Too many keys
-			if ( *validKeys == 6 )
-				break;
-
-			// Allow ignoring keys with 0's
-			if ( map[key] != 0 )
-				keyboard_keys[(*validKeys)++] = map[key];
-		}
-	}
-}
-
+// ----- Functions -----
 
 // Main Detection Loop
-void scan_loop( void )
+void scan_loop()
 {
 	//matrix_pinSetup( matrix_pinout );
-	uint8_t count = 0;
 
-	for ( ;; ) {
-		//matrix_scan( matrix_pinout, keyboardDetectArray );
-
-		// Check count to see if the sample threshold may have been reached, otherwise collect more data
-		if ( count++ < MAX_SAMPLES )
-			continue;
-
-		// Reset Sample Counter
-		count = 0;
-
-		// Assess debouncing sample table
-		//DEBOUNCE_ASSESS(keyDetectArray,KEYBOARD_SIZE)
+	//matrix_scan( matrix_pinout, keyboardDetectArray );
 
-		// Send keypresses over USB if the ISR has signalled that it's time
-		if ( !sendKeypresses )
-			continue;
-
-		// Layout Setup
-		uint8_t validKeys = 0;
-
-		uint8_t *keyboard_MODMASK = keyboard_modifierMask;
-		uint8_t  keyboard_NUMMODS = MODIFIERS_KEYBOARD;
-		uint8_t *keyboard_MAP     = defaultMap;
+	// Check count to see if the sample threshold may have been reached, otherwise collect more data
+	if ( scan_count++ < MAX_SAMPLES )
+		return;
 
-		// TODO Layout Switching
-
-		// TODO Macro Processing
+	// Reset Sample Counter
+	scan_count = 0;
 
-		// Debounce Sampling Array to USB Data Array
-		keyPressDetection( keyboardDetectArray, &validKeys, KEYBOARD_SIZE, keyboard_MODMASK, keyboard_NUMMODS, keyboard_MAP );
-
-		// Send USB Data
-		usb_send( validKeys );
-	}
+	// Assess debouncing sample table
+	//DEBOUNCE_ASSESS(keyDetectArray,KEYBOARD_SIZE)
 }
 
--- a/Scan/matrix/scan_loop.h	Thu Sep 29 15:30:24 2011 -0700
+++ b/Scan/matrix/scan_loop.h	Thu Sep 29 22:22:19 2011 -0700
@@ -22,8 +22,14 @@
 #ifndef __SCAN_LOOP_H
 #define __SCAN_LOOP_H
 
-//extern uint8_t keyboardDetectArray[KEYBOARDZ
-extern volatile uint8_t sendKeypresses;
+// ----- Includes -----
+
+// Local Includes
+#include "matrix.h"
+
+
+
+// ----- Functions -----
 
 void scan_loop( void );
 
--- a/USB/pjrc/setup.cmake	Thu Sep 29 15:30:24 2011 -0700
+++ b/USB/pjrc/setup.cmake	Thu Sep 29 22:22:19 2011 -0700
@@ -12,6 +12,7 @@
 #
 
 set( USB_SRCS
+	usb_com.c
 	usb_keyboard_debug.c
 )
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USB/pjrc/usb_com.c	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,85 @@
+/* Copyright (C) 2011 by Jacob Alexander
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+// ----- Includes -----
+
+// Compiler Includes
+#include <util/delay.h>
+
+// AVR Includes
+
+// Project Includes
+#include "usb_keyboard_debug.h"
+
+// Local Includes
+#include "usb_com.h"
+
+
+
+// ----- Variables -----
+
+// which modifier keys are currently pressed
+// 1=left ctrl,    2=left shift,   4=left alt,    8=left gui
+// 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
+         uint8_t USBKeys_Modifiers = 0;
+
+// which keys are currently pressed, up to 6 keys may be down at once
+         uint8_t USBKeys_Array[USB_MAX_KEY_SEND] = {0,0,0,0,0,0};
+
+// The number of keys sent to the usb in the array
+         uint8_t USBKeys_Sent;
+
+// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
+volatile uint8_t USBKeys_LEDs = 0;
+
+
+
+// ----- Functions -----
+
+// USB Module Setup
+void usb_setup(void)
+{
+	// 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);
+}
+
+
+// USB Data Send
+void usb_send(void)
+{
+		// TODO undo potentially old keys
+		for ( uint8_t c = USBKeys_Sent; c < USBKeys_MaxSize; c++ )
+			USBKeys_Array[c] = 0;
+
+		// Send keypresses
+		usb_keyboard_send();
+
+		// Clear modifiers
+		USBKeys_Modifiers = 0;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USB/pjrc/usb_com.h	Thu Sep 29 22:22:19 2011 -0700
@@ -0,0 +1,73 @@
+/* Copyright (C) 2011 by Jacob Alexander
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef __usb_com_h
+#define __usb_com_h
+
+// ----- Includes -----
+
+// AVR Includes
+
+// Compiler Includes
+#include <stdint.h>
+
+// Local Includes
+
+
+
+// ----- Defines -----
+
+// Indicator for other modules through USBKeys_MaxSize for how capable the USB module is when sending large number of keypresses
+#define USB_MAX_KEY_SEND 6
+
+
+// You can change these to give your code its own name.
+#define STR_MANUFACTURER	L"MfgName"
+#define STR_PRODUCT		L"Keyboard"
+
+
+// Mac OS-X and Linux automatically load the correct drivers.  On
+// Windows, even though the driver is supplied by Microsoft, an
+// INF file is needed to load the driver.  These numbers need to
+// match the INF file.
+#define VENDOR_ID		0x16C0
+#define PRODUCT_ID		0x047D
+
+
+
+// ----- Variables -----
+
+// Variables used to communciate to the usb module
+extern                       uint8_t USBKeys_Modifiers;
+extern                       uint8_t USBKeys_Array[USB_MAX_KEY_SEND];
+extern                       uint8_t USBKeys_Sent;
+extern volatile              uint8_t USBKeys_LEDs;
+                static const uint8_t USBKeys_MaxSize = USB_MAX_KEY_SEND;
+
+
+
+// ----- Functions -----
+
+void usb_setup(void);
+void usb_send(void);
+
+#endif
+
--- a/USB/pjrc/usb_keyboard_debug.c	Thu Sep 29 15:30:24 2011 -0700
+++ b/USB/pjrc/usb_keyboard_debug.c	Thu Sep 29 22:22:19 2011 -0700
@@ -33,19 +33,6 @@
  *
  **************************************************************************/
 
-// You can change these to give your code its own name.
-#define STR_MANUFACTURER	L"MfgName"
-#define STR_PRODUCT		L"Keyboard"
-
-
-// Mac OS-X and Linux automatically load the correct drivers.  On
-// Windows, even though the driver is supplied by Microsoft, an
-// INF file is needed to load the driver.  These numbers need to
-// match the INF file.
-#define VENDOR_ID		0x16C0
-#define PRODUCT_ID		0x047D
-
-
 // USB devices are supposed to implment a halt feature, which is
 // rarely (if ever) used.  If you comment this line out, the halt
 // code will be removed, saving 102 bytes of space (gcc 4.3.0).
@@ -286,14 +273,6 @@
 // packet, or send a zero length packet.
 static volatile uint8_t debug_flush_timer=0;
 
-// which modifier keys are currently pressed
-// 1=left ctrl,    2=left shift,   4=left alt,    8=left gui
-// 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
-uint8_t keyboard_modifier_keys=0;
-
-// which keys are currently pressed, up to 6 keys may be down at once
-uint8_t keyboard_keys[6]={0,0,0,0,0,0};
-
 // protocol setting from the host.  We use exactly the same report
 // either way, so this variable only stores the setting since we
 // are required to be able to report which setting is in use.
@@ -306,9 +285,6 @@
 // count until idle timeout
 static uint8_t keyboard_idle_count=0;
 
-// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
-volatile uint8_t keyboard_leds=0;
-
 
 /**************************************************************************
  *
@@ -344,16 +320,16 @@
 {
 	int8_t r;
 
-	keyboard_modifier_keys = modifier;
-	keyboard_keys[0] = key;
+	USBKeys_Modifiers = modifier;
+	USBKeys_Array[0] = key;
 	r = usb_keyboard_send();
 	if (r) return r;
-	keyboard_modifier_keys = 0;
-	keyboard_keys[0] = 0;
+	USBKeys_Modifiers = 0;
+	USBKeys_Array[0] = 0;
 	return usb_keyboard_send();
 }
 
-// send the contents of keyboard_keys and keyboard_modifier_keys
+// send the contents of USBKeys_Array and USBKeys_Modifiers
 int8_t usb_keyboard_send(void)
 {
 	uint8_t i, intr_state, timeout;
@@ -376,10 +352,10 @@
 		cli();
 		UENUM = KEYBOARD_ENDPOINT;
 	}
-	UEDATX = keyboard_modifier_keys;
+	UEDATX = USBKeys_Modifiers;
 	UEDATX = 0;
 	for (i=0; i<6; i++) {
-		UEDATX = keyboard_keys[i];
+		UEDATX = USBKeys_Array[i];
 	}
 	UEINTX = 0x3A;
 	keyboard_idle_count = 0;
@@ -505,10 +481,10 @@
 				keyboard_idle_count++;
 				if (keyboard_idle_count == keyboard_idle_config) {
 					keyboard_idle_count = 0;
-					UEDATX = keyboard_modifier_keys;
+					UEDATX = USBKeys_Modifiers;
 					UEDATX = 0;
 					for (i=0; i<6; i++) {
-						UEDATX = keyboard_keys[i];
+						UEDATX = USBKeys_Array[i];
 					}
 					UEINTX = 0x3A;
 				}
@@ -679,10 +655,10 @@
 			if (bmRequestType == 0xA1) {
 				if (bRequest == HID_GET_REPORT) {
 					usb_wait_in_ready();
-					UEDATX = keyboard_modifier_keys;
+					UEDATX = USBKeys_Modifiers;
 					UEDATX = 0;
 					for (i=0; i<6; i++) {
-						UEDATX = keyboard_keys[i];
+						UEDATX = USBKeys_Array[i];
 					}
 					usb_send_in();
 					return;
@@ -703,7 +679,7 @@
 			if (bmRequestType == 0x21) {
 				if (bRequest == HID_SET_REPORT) {
 					usb_wait_receive_out();
-					keyboard_leds = UEDATX;
+					USBKeys_LEDs = UEDATX;
 					usb_ack_out();
 					usb_send_in();
 					return;
--- a/USB/pjrc/usb_keyboard_debug.h	Thu Sep 29 15:30:24 2011 -0700
+++ b/USB/pjrc/usb_keyboard_debug.h	Thu Sep 29 22:22:19 2011 -0700
@@ -2,15 +2,14 @@
 #define usb_serial_h__
 
 #include <stdint.h>
+#include "usb_com.h"
 
 void usb_init(void);			// initialize everything
 uint8_t usb_configured(void);		// is the USB port configured
 
 int8_t usb_keyboard_press(uint8_t key, uint8_t modifier);
 int8_t usb_keyboard_send(void);
-extern uint8_t keyboard_modifier_keys;
-extern uint8_t keyboard_keys[6];
-extern volatile uint8_t keyboard_leds;
+
 
 int8_t usb_debug_putchar(uint8_t c);	// transmit a character
 void usb_debug_flush_output(void);	// immediately transmit any buffered output
--- 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++;
--- a/setup.cmake	Thu Sep 29 15:30:24 2011 -0700
+++ b/setup.cmake	Thu Sep 29 22:22:19 2011 -0700
@@ -29,7 +29,7 @@
 set(   USBModule  "pjrc"   )
 
 ##| Debugging source to use, each module has it's own set of defines that it sets
-set( DebugModule  "basic"  )
+set( DebugModule  "full"   )