|  |  | @ -32,6 +32,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "pro_micro.h" |  |  |  | #include "pro_micro.h" | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "config.h" |  |  |  | #include "config.h" | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "timer.h" |  |  |  | #include "timer.h" | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | #include "Device.h" | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | #include "pincontrol.h" | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | #ifdef USE_I2C |  |  |  | #ifdef USE_I2C | 
			
		
	
	
		
		
			
				
					|  |  | @ -41,7 +43,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
			
		
	
		
		
			
				
					
					|  |  |  | #endif |  |  |  | #endif | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | // Seconds to keep checking if USB has been enumearted, before settling for being slave half.
 |  |  |  | // Seconds to keep checking if USB has been enumearted, before settling for being slave half.
 | 
			
		
	
		
		
			
				
					
					|  |  |  | #define USBTIMEOUT 3 |  |  |  | volatile bool is_slave = false; | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | #ifndef DEBOUNCING_DELAY |  |  |  | #ifndef DEBOUNCING_DELAY | 
			
		
	
		
		
			
				
					
					|  |  |  | #   define DEBOUNCING_DELAY 5 |  |  |  | #   define DEBOUNCING_DELAY 5 | 
			
		
	
	
		
		
			
				
					|  |  | @ -131,7 +133,7 @@ uint8_t matrix_cols(void) | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | bool has_usb(void) { |  |  |  | bool has_usb(void) { | 
			
		
	
		
		
			
				
					
					|  |  |  |   return UDADDR & _BV(ADDEN); // This will return true if a USB connection has been established
 |  |  |  |   return USB_DeviceState == DEVICE_STATE_Configured; | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | void timer_reset(void) { |  |  |  | void timer_reset(void) { | 
			
		
	
	
		
		
			
				
					|  |  | @ -150,20 +152,20 @@ void timer_reset(void) { | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | void matrix_init(void) |  |  |  | void matrix_init(void) | 
			
		
	
		
		
			
				
					
					|  |  |  | { |  |  |  | { | 
			
		
	
		
		
			
				
					
					|  |  |  | #ifdef DISABLE_JTAG |  |  |  |   #ifdef DISABLE_JTAG | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   // JTAG disable for PORT F. write JTD bit twice within four cycles.
 |  |  |  |     // JTAG disable for PORT F. write JTD bit twice within four cycles.
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   MCUCR |= (1<<JTD); |  |  |  |     MCUCR |= (1<<JTD); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   MCUCR |= (1<<JTD); |  |  |  |     MCUCR |= (1<<JTD); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | #endif |  |  |  |   #endif | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     // initialize row and col
 |  |  |  |   // initialize row and col
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | #if (DIODE_DIRECTION == COL2ROW) |  |  |  |   #if (DIODE_DIRECTION == COL2ROW) | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |     unselect_rows(); |  |  |  |     unselect_rows(); | 
			
		
	
		
		
			
				
					
					|  |  |  |     init_cols(); |  |  |  |     init_cols(); | 
			
		
	
		
		
			
				
					
					|  |  |  | #elif (DIODE_DIRECTION == ROW2COL) |  |  |  |   #elif (DIODE_DIRECTION == ROW2COL) | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |     unselect_cols(); |  |  |  |     unselect_cols(); | 
			
		
	
		
		
			
				
					
					|  |  |  |     init_rows(); |  |  |  |     init_rows(); | 
			
		
	
		
		
			
				
					
					|  |  |  | #endif |  |  |  |   #endif | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   TX_RX_LED_INIT; |  |  |  |   TX_RX_LED_INIT; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
	
		
		
			
				
					|  |  | @ -173,7 +175,6 @@ void matrix_init(void) | 
			
		
	
		
		
			
				
					
					|  |  |  |     matrix_debouncing[i] = 0; |  |  |  |     matrix_debouncing[i] = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   // initialize as slave
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   timer_init(); |  |  |  |   timer_init(); | 
			
		
	
		
		
			
				
					
					|  |  |  |   #ifdef USE_I2C |  |  |  |   #ifdef USE_I2C | 
			
		
	
		
		
			
				
					
					|  |  |  |     i2c_slave_init(SLAVE_I2C_ADDRESS); |  |  |  |     i2c_slave_init(SLAVE_I2C_ADDRESS); | 
			
		
	
	
		
		
			
				
					|  |  | @ -181,26 +182,26 @@ void matrix_init(void) | 
			
		
	
		
		
			
				
					
					|  |  |  |     serial_slave_init(); |  |  |  |     serial_slave_init(); | 
			
		
	
		
		
			
				
					
					|  |  |  |   #endif |  |  |  |   #endif | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   while (USB_DeviceState != DEVICE_STATE_Configured && !is_slave) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       USB_USBTask(); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   sei(); |  |  |  |   sei(); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   matrix_init_quantum(); |  |  |  |   matrix_init_quantum(); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   /* Wait USBTIMEOUT for USB
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   uint32_t timeout_start = timer_count; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   while(!has_usb() && USBTIMEOUT > timer_count - timeout_start) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     matrix_slave_scan(); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   }*/ |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   // If we have USB, switch to master
 |  |  |  |   // If we have USB, switch to master
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   if(has_usb()) { |  |  |  |   if(has_usb()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |     //timer_reset();
 |  |  |  |     timer_reset(); | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |     #ifdef USE_I2C |  |  |  |     #ifdef USE_I2C | 
			
		
	
		
		
			
				
					
					|  |  |  |       i2c_master_init(); |  |  |  |       i2c_master_init(); | 
			
		
	
		
		
			
				
					
					|  |  |  |     #else |  |  |  |     #else | 
			
		
	
		
		
			
				
					
					|  |  |  |       serial_master_init(); |  |  |  |       serial_master_init(); | 
			
		
	
		
		
			
				
					
					|  |  |  |     #endif |  |  |  |     #endif | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  |   else{ |  |  |  |   else { | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |     while (1) { |  |  |  |     while (1) { | 
			
		
	
		
		
			
				
					
					|  |  |  |       matrix_slave_scan(); |  |  |  |       matrix_slave_scan(); | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
	
		
		
			
				
					|  |  | 
 |