- // Setup the scanning module
- scan_setup();
-
- while ( 1 )
- {
- // Acquire Key Indices
- // Loop continuously until scan_loop returns 0
- cli();
- while ( scan_loop() );
- sei();
-
- // Run Macros over Key Indices and convert to USB Keys
- process_macros();
-
- // 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;
-
- // Indicate Error, if valid
- errorLED( ledTimer );
-
- if ( ledTimer > 0 )
- ledTimer--;
- }
-
- // Loop should never get here (indicate error)
- ledTimer = 255;
-
- // HID Debug Error message
- erro_print("Detection loop error, this is very bad...bug report!");
- }
-}
-
-
-// ----- Interrupts -----
-
-// USB Keyboard Data Send Counter Interrupt
-#if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
-ISR( TIMER0_OVF_vect )
-#elif defined(_mk20dx128_) // ARM
-void pit0_isr(void)
-#endif
-{
- sendKeypressCounter++;
- if ( sendKeypressCounter > USB_TRANSFER_DIVIDER ) {
- sendKeypressCounter = 0;
- sendKeypresses = 1;
- }
-
-#if defined(_mk20dx128_) // ARM
- // Clear the interrupt flag
- PIT_TFLG0 = 1;
-#endif
-}
-
-
-// ----- CLI Command Functions -----
-
-void cliFunc_distRead( char* args )
-{
- // Prepare to print output
- print( NL );
- info_msg("Distance: ");
-
- // Data
- uint32_t distInput = 0;
-
- // Setup distance read parameters for iGaging Distance Scale
- // freq = 9kHz
- // duty_cycle = 20%
- // high_delay = (1/freq) * (duty_cycle/100)
- // low_delay = (1/freq) * ((100-duty_cycle)/100)
- uint8_t bits = 21; // 21 clock pulses, for 21 bits
- //uint32_t high_delay = 22; // Clock high time per pulse
- //uint32_t low_delay = 89; // Clock low time per pulse
- uint32_t high_delay = 40; // Clock high time per pulse
- uint32_t low_delay = 60; // Clock low time per pulse
-
- // Make sure clock is low initially
- GPIOC_PCOR |= (1<<2); // Set Clock low
-/*
-while(1)
-{
-*/
- // Scan each of the bits
- for ( uint8_t bit = bits; bit > 0; bit-- )
- {
- // Begin clock pulse
- GPIOC_PSOR |= (1<<2); // Set Clock high
-
- // Delay for duty cycle
- delayMicroseconds( high_delay );