void start_scheduler(void) {
    setDutyCycle (50, 50);      // dutyCycle = 50%, frequency = 50Hz
    while (1) {
//  OpenEPWM1(0xff);
//  SetDCEPWM1(0);
//  SetOutputEPWM1(FULL_OUT_FWD, PWM_MODE_1);
//  CloseEPWM1();
        
        //timer interrupt
        if (sampling_flag == 1) {
            timer_rst();
            if (curr_channel < high) {
                PORTBbits.RB0 = 1;
                PORTBbits.RB1 = 1;
                PORTBbits.RB2 = 0;
                PORTBbits.RB3 = 0;
            } else if (curr_channel >= high){
                PORTBbits.RB0 = 0;
                PORTBbits.RB1 = 0;
                PORTBbits.RB2 = 1;
                PORTBbits.RB3 = 1;
            } else if (curr_channel == totalTicks){
                update_status(SYSTEM_UART);
                done = uart_task();
            }
            //update status if a task terminate
        } else {
            if ((check_status() == SYSTEM_UART) && done && TXSTAbits.TRMT) {
               update_status(SYSTEM_IDLE);
               done = 0;
            }
        }
    }
}
Beispiel #2
0
int
main(void)
{
  wdt_disable();

  // un-reset ethernet
  ENC28J60_RESET_DDR  |= _BV( ENC28J60_RESET_BIT );
  ENC28J60_RESET_PORT |= _BV( ENC28J60_RESET_BIT );
  
  led_init();
  LED_ON();

  spi_init();

  eeprom_init();

  // if we had been restarted by watchdog check the REQ BootLoader byte in the
  // EEPROM ...
  if(bit_is_set(MCUSR,WDRF) && eeprom_read_byte(EE_REQBL)) {
    eeprom_write_byte( EE_REQBL, 0 ); // clear flag
// TBD: This is useless as button needs to be pressed - needs moving into bootloader directly
//    start_bootloader();
  }


// Setup OneWire and make a full search at the beginning (takes some time)
#ifdef HAS_ONEWIRE
  i2c_init();
  onewire_Init();
  onewire_FullSearch();
#endif

  // Setup the timers. Are needed for watchdog-reset

#if defined (HAS_IRRX) || defined (HAS_IRTX)
  ir_init();
  // IR uses highspeed TIMER0 for sampling 
  OCR0A  = 1;                              // Timer0: 0.008s = 8MHz/256/2   == 15625Hz
#else
  OCR0A  = 249;                            // Timer0: 0.008s = 8MHz/256/250 == 125Hz
#endif

  TCCR0B = _BV(CS02);
  TCCR0A = _BV(WGM01);
  TIMSK0 = _BV(OCIE0A);

  TCCR1A = 0;
  TCCR1B = _BV(CS11) | _BV(WGM12);         // Timer1: 1us = 8MHz/8

  clock_prescale_set(clock_div_1);

  MCUSR &= ~(1 << WDRF);                   // Enable the watchdog
  wdt_enable(WDTO_2S);

  uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );

  fht_init();
  tx_init();
  input_handle_func = analyze_ttydata;
#ifdef HAS_RF_ROUTER
  rf_router_init();
  display_channel = (DISPLAY_USB|DISPLAY_RFROUTER);
#else
  display_channel = DISPLAY_USB;
#endif

  ethernet_init();
    
  LED_OFF();

  sei();

  for(;;) {
    uart_task();
    RfAnalyze_Task();
    Minute_Task();
#ifdef HAS_FASTRF
    FastRF_Task();
#endif
#ifdef HAS_RF_ROUTER
    rf_router_task();
#endif
#ifdef HAS_ASKSIN
    rf_asksin_task();
#endif
#ifdef HAS_IRRX
    ir_task();
#endif
#ifdef HAS_ETHERNET
    Ethernet_Task();
#endif
  }

}
Beispiel #3
0
int
main(void)
{
  wdt_disable();
#ifdef HAS_16MHZ_CLOCK
  /* set clock to 16MHz/2 = 8Mhz */
  clock_prescale_set(clock_div_2);
#endif

//  LED_ON_DDR  |= _BV( LED_ON_PIN );
//  LED_ON_PORT |= _BV( LED_ON_PIN );

  led_init();
  LED_ON();

  spi_init();
 // init_adcw();

  //eeprom_factory_reset("xx");
  eeprom_init();

  // Setup the timers. Are needed for watchdog-reset
  OCR0A  = 249;                            // Timer0: 0.008s = 8MHz/256/250 == 125Hz
  TCCR0B = _BV(CS02);
  TCCR0A = _BV(WGM01);
  TIMSK0 = _BV(OCIE0A);

  TCCR1A = 0;
  TCCR1B = _BV(CS11) | _BV(WGM12);         // Timer1: 1us = 8MHz/8


  MCUSR &= ~(1 << WDRF);                   // Enable the watchdog
  wdt_enable(WDTO_2S);

#ifdef HAS_16MHZ_CLOCK
  uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
#else
  uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );
#endif
  fht_init();
  tx_init();
  input_handle_func = analyze_ttydata;

  display_channel = DISPLAY_USB;

#ifdef HAS_RF_ROUTER
  rf_router_init();
  display_channel |= DISPLAY_RFROUTER;
#endif

  LED_OFF();

  sei();

  for(;;) {
    uart_task();
    RfAnalyze_Task();
    Minute_Task();
#ifdef HAS_FASTRF
    FastRF_Task();
#endif
#ifdef HAS_RF_ROUTER
    rf_router_task();
#endif
#ifdef HAS_ASKSIN
    rf_asksin_task();
#endif
#ifdef HAS_MORITZ
    rf_moritz_task();
#endif
#ifdef HAS_RWE
    rf_rwe_task();
#endif
#ifdef HAS_MBUS
    rf_mbus_task();
#endif
  }

}
Beispiel #4
0
int main(void)
{

  /* USER CODE BEGIN 1 */


  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_CRC_Init();
  MX_I2C1_Init();
  MX_SPI2_Init();
  MX_TIM2_Init();
  MX_TIM3_Init();
  MX_TIM4_Init();
  MX_USART2_UART_Init();

  /* USER CODE BEGIN 2 */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);

  // reinitialize uart with speed from config
  huart2.Init.BaudRate = USART_DEBUG_SPEED;
  HAL_UART_Init(&huart2);

  DEBUG_PRINTF("Hello, Lepton!\n\r");
  fflush(stdout);

  lepton_init();

  HAL_Delay(1000);

  init_lepton_command_interface();
#ifdef ENABLE_LEPTON_AGC
  enable_lepton_agc();
#endif

#ifdef Y16
  enable_telemetry();
#else
  enable_rgb888(PSUEDOCOLOR_LUT);
#endif

  DEBUG_PRINTF("reading_tmp007_regs...\n\r");

  read_tmp007_regs();

  DEBUG_PRINTF("Initialized...\n\r");

  HAL_Delay(250);

  MX_USB_DEVICE_Init();

  PT_INIT(&lepton_task_pt);
  PT_INIT(&usb_task_pt);
  PT_INIT(&uart_task_pt);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

	  PT_SCHEDULE(lepton_task(&lepton_task_pt));
	  PT_SCHEDULE(usb_task(&usb_task_pt));
	  PT_SCHEDULE(uart_task(&uart_task_pt));
	  PT_SCHEDULE(button_task(&button_task_pt));

  }
  /* USER CODE END 3 */

}
Beispiel #5
0
int
main(void)
{
  wdt_disable();
  clock_prescale_set(clock_div_1);

  LED_ON_DDR  |= _BV( LED_ON_PIN );
  LED_ON_PORT |= _BV( LED_ON_PIN );

  led_init();
  LED_ON();

  spi_init();

//  eeprom_factory_reset("xx");
  eeprom_init();

// Setup OneWire and make a full search at the beginning (takes some time)
#ifdef HAS_ONEWIRE
  i2c_init();
	onewire_Init();
	onewire_FullSearch();
#endif

  // Setup the timers. Are needed for watchdog-reset
#if defined (HAS_IRRX) || defined (HAS_IRTX)
  ir_init();
  // IR uses highspeed TIMER0 for sampling 
  OCR0A  = 1;                              // Timer0: 0.008s = 8MHz/256/2   == 15625Hz
#else
  OCR0A  = 249;                            // Timer0: 0.008s = 8MHz/256/250 == 125Hz
#endif
  TCCR0B = _BV(CS02);
  TCCR0A = _BV(WGM01);
  TIMSK0 = _BV(OCIE0A);

  TCCR1A = 0;
  TCCR1B = _BV(CS11) | _BV(WGM12);         // Timer1: 1us = 8MHz/8


  MCUSR &= ~(1 << WDRF);                   // Enable the watchdog
  wdt_enable(WDTO_2S);

  uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );

  fht_init();
  tx_init();
  input_handle_func = analyze_ttydata;

  display_channel = DISPLAY_USB;

#ifdef HAS_RF_ROUTER
  rf_router_init();
  display_channel |= DISPLAY_RFROUTER;
#endif

  checkFrequency(); 
  LED_OFF();

  sei();

  for(;;) {
    uart_task();
    RfAnalyze_Task();
    Minute_Task();
#ifdef HAS_FASTRF
    FastRF_Task();
#endif
#ifdef HAS_RF_ROUTER
    rf_router_task();
#endif
#ifdef HAS_ASKSIN
    rf_asksin_task();
#endif
#ifdef HAS_MORITZ
    rf_moritz_task();
#endif
#ifdef HAS_RWE
    rf_rwe_task();
#endif
#if defined(HAS_IRRX) || defined(HAS_IRTX)
    ir_task();
#endif
#ifdef HAS_MBUS
    rf_mbus_task();
#endif
  }

}
Beispiel #6
0
int main(void){
	adc_init();
	init_3lb();
	timer1_init();
    buttons_init();
	pi_shutdown_init();
	set_sleep_mode(SLEEP_MODE_IDLE);
	ZV_count = 10;
	USART_Init(MYUBRR);
	sei();
	while(1){
		//*
		if(buttons_active){
			if((AMP_PIN & (1<<AMP_ON))){
				start_pi();
				switch(aux_check()){
					case AUX:{
						buttons_task();
						uart_task();
						break;
					}
					case NOT_AUX:{
						USART_Transmit(0x01);
						_delay_ms(250);
						uint8_t i;
						for(i=1; i<17; i++){
							USART_Transmit(data_3lb[i]);
							_delay_ms(25);
						}
						USART_Transmit(0x00);
						_delay_ms(250);
						break;
					}
					case AUX_INFO:{
						USART_Transmit(0x01);
						_delay_ms(250);
						break;
					}
				}
				mfd_active = true;
			}else{
//				pi_shutdown_task();
//*
				if(ZV_PIN & (1<<ZV_ZU)){
					pi_shutdown_task();
				}else{
					start_pi();
					USART_Transmit(0x02);
					_delay_ms(250);
				}
//*/
			}
			buttons_active = false;
		}
		if(!(PISTARTPORT & (1<<PISTART))){
			sleep_mode();
			buttons_active = true;
		}	
		//*/		
    }
	return 0;
}
Beispiel #7
0
int
main(void)
{
  wdt_disable();

#ifdef CSMV4

  LED_ON_DDR  |= _BV( LED_ON_PIN );
  LED_ON_PORT |= _BV( LED_ON_PIN );

#endif

  led_init();
  LED_ON();

  spi_init();

//  eeprom_factory_reset("xx");
  eeprom_init();

//  led_mode = 2;

  // if we had been restarted by watchdog check the REQ BootLoader byte in the
  // EEPROM ...
//  if(bit_is_set(MCUSR,WDRF) && eeprom_read_byte(EE_REQBL)) {
//    eeprom_write_byte( EE_REQBL, 0 ); // clear flag
//    start_bootloader();
//  }

  // Setup the timers. Are needed for watchdog-reset
#ifdef HAS_IRRX
  ir_init();
  // IR uses highspeed TIMER0 for sampling
  OCR0A  = 1;                              // Timer0: 0.008s = 8MHz/256/2   == 15625Hz
#else
  OCR0A  = 249;                            // Timer0: 0.008s = 8MHz/256/250 == 125Hz
#endif
  TCCR0B = _BV(CS02);
  TCCR0A = _BV(WGM01);
  TIMSK0 = _BV(OCIE0A);

  TCCR1A = 0;
  TCCR1B = _BV(CS11) | _BV(WGM12);         // Timer1: 1us = 8MHz/8

  clock_prescale_set(clock_div_1);

  MCUSR &= ~(1 << WDRF);                   // Enable the watchdog
  wdt_enable(WDTO_2S);

  uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );

#ifdef HAS_DOGM
  dogm_init();
#endif

  fht_init();
  tx_init();
  input_handle_func = analyze_ttydata;

  display_channel = DISPLAY_USB;

#ifdef HAS_RF_ROUTER
  rf_router_init();
  display_channel |= DISPLAY_RFROUTER;
#endif

#ifdef HAS_DOGM
  display_channel |= DISPLAY_DOGM;
#endif

  LED_OFF();

  sei();

  for(;;) {
    uart_task();
    RfAnalyze_Task();
    Minute_Task();
#ifdef HAS_FASTRF
    FastRF_Task();
#endif
#ifdef HAS_RF_ROUTER
    rf_router_task();
#endif
#ifdef HAS_ASKSIN
    rf_asksin_task();
#endif
#ifdef HAS_MORITZ
    rf_moritz_task();
#endif
#ifdef HAS_IRRX
    ir_task();
#endif
  }

}
Beispiel #8
0
int
main(void)
{
//  wdt_disable();

    clock_prescale_set(clock_div_1);

    MARK433_PORT |= _BV( MARK433_BIT ); // Pull 433MHz marker
    MARK915_PORT |= _BV( MARK915_BIT ); // Pull 915MHz marker

    led_init();

    spi_init();

    // Setup the timers. Are needed for watchdog-reset
    OCR0A  = 249;                            // Timer0: 0.008s = 8MHz/256/250 == 125Hz
    TCCR0B = _BV(CS02);
    TCCR0A = _BV(WGM01);
    TIMSK0 = _BV(OCIE0A);

    TCCR1A = 0;
    TCCR1B = _BV(CS11) | _BV(WGM12);         // Timer1: 1us = 8MHz/8

    //eeprom_factory_reset("xx");
    eeprom_init();

    MCUSR &= ~(1 << WDRF);                   // Enable the watchdog
    wdt_enable(WDTO_2S);

    //uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );
    uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );

    fht_init();
    tx_init();
    input_handle_func = analyze_ttydata;

    display_channel = DISPLAY_USB;

#ifdef HAS_RF_ROUTER
    rf_router_init();
    display_channel |= DISPLAY_RFROUTER;
#endif

    checkFrequency();
    sei();

    for(;;) {
        uart_task();
        RfAnalyze_Task();
        Minute_Task();
#ifdef HAS_FASTRF
        FastRF_Task();
#endif
#ifdef HAS_RF_ROUTER
        rf_router_task();
#endif
#ifdef HAS_ASKSIN
        rf_asksin_task();
#endif
#ifdef HAS_MORITZ
        rf_moritz_task();
#endif
#ifdef HAS_RWE
        rf_rwe_task();
#endif
#ifdef HAS_RFNATIVE
        native_task();
#endif
#ifdef HAS_KOPP_FC
        kopp_fc_task();
#endif
#ifdef HAS_MBUS
        rf_mbus_task();
#endif
#ifdef HAS_ZWAVE
        rf_zwave_task();
#endif
    }

}
Beispiel #9
0
int
main(void)
{
  wdt_disable();

  led_init();
  LED_ON();

  MARK433_PORT |= _BV( MARK433_BIT );
  MARK915_PORT |= _BV( MARK915_BIT );

  spi_init();

//  eeprom_factory_reset("xx");
  eeprom_init();

//  led_mode = 2;

  // if we had been restarted by watchdog check the REQ BootLoader byte in the
  // EEPROM ...
//  if(bit_is_set(MCUSR,WDRF) && eeprom_read_byte(EE_REQBL)) {
//    eeprom_write_byte( EE_REQBL, 0 ); // clear flag
//    start_bootloader();
//  }

  // Setup the timers. Are needed for watchdog-reset
  OCR0A  = 249;                            // Timer0: 0.008s = 8MHz/256/250 == 125Hz
  TCCR0B = _BV(CS02);
  TCCR0A = _BV(WGM01);
  TIMSK0 = _BV(OCIE0A);

  TCCR1A = 0;
  TCCR1B = _BV(CS11) | _BV(WGM12);         // Timer1: 1us = 8MHz/8

  clock_prescale_set(clock_div_1);

  MCUSR &= ~(1 << WDRF);                   // Enable the watchdog
  wdt_enable(WDTO_2S);

  uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );

#ifdef HAS_STACKING
  stacking_initialize();

  // make sure i2c is inactive
  DDRC  &= 0xfc;
  PORTC &= 0xfc;

#endif

  fht_init();
  tx_init();
  input_handle_func = analyze_ttydata;

  display_channel = DISPLAY_USB;

#ifdef HAS_RF_ROUTER
  rf_router_init();
  display_channel |= DISPLAY_RFROUTER;
#endif

  LED_OFF();
  checkFrequency(); 

  sei();

  for(;;) {
    uart_task();
    RfAnalyze_Task();
    Minute_Task();
#ifdef HAS_FASTRF
    FastRF_Task();
#endif
#ifdef HAS_RF_ROUTER
    rf_router_task();
#endif
#ifdef HAS_ASKSIN
    rf_asksin_task();
#endif
#ifdef HAS_MORITZ
    rf_moritz_task();
#endif
#ifdef HAS_STACKING
  stacking_task();
#endif
#ifdef HAS_MBUS
    rf_mbus_task();
#endif

  }

}
Beispiel #10
0
int
main(void)
{
  wdt_disable();

  // un-reset ethernet
  ENC28J60_RESET_DDR  |= _BV( ENC28J60_RESET_BIT );
  ENC28J60_RESET_PORT |= _BV( ENC28J60_RESET_BIT );

  MARK433_PORT |= _BV( MARK433_BIT ); // Pull 433MHz marker
  MARK915_PORT |= _BV( MARK915_BIT ); // Pull 915MHz marker
  
  led_init();
  LED_ON();

  spi_init();

  eeprom_init();
  
  // Reset the "Request Bootloader" flag in EEPROM, to avoid a permanent loop
  eeprom_update_byte( EE_REQBL, 0);

// Setup OneWire and make a full search at the beginning (takes some time)
#ifdef HAS_ONEWIRE
  i2c_init();
  onewire_Init();
  onewire_FullSearch();
#endif

  // Setup the timers. Are needed for watchdog-reset

#if defined (HAS_IRRX) || defined (HAS_IRTX)
  ir_init();
  // IR uses highspeed TIMER0 for sampling 
  OCR0A  = 1;                              // Timer0: 0.008s = 8MHz/256/2   == 15625Hz Fac: 125
#else
  OCR0A  = 249;                            // Timer0: 0.008s = 8MHz/256/250 == 125Hz
#endif

  TCCR0B = _BV(CS02);
  TCCR0A = _BV(WGM01);
  TIMSK0 = _BV(OCIE0A);

  TCCR1A = 0;
  TCCR1B = _BV(CS11) | _BV(WGM12);         // Timer1: 1us = 8MHz/8

  clock_prescale_set(clock_div_1);

  MCUSR &= ~(1 << WDRF);                   // Enable the watchdog
  wdt_enable(WDTO_2S);

  uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );

  fht_init();
  tx_init();
  input_handle_func = analyze_ttydata;
#ifdef HAS_RF_ROUTER
  rf_router_init();
  display_channel = (DISPLAY_USB|DISPLAY_RFROUTER);
#else
  display_channel = DISPLAY_USB;
#endif

#ifdef HAS_VZ
  vz_init();
#endif

  ethernet_init();
    
  LED_OFF();

#ifdef HAS_DMX
#ifdef DMX_CHANNELS
  dmx_initialize(DMX_CHANNELS);
#else
  dmx_initialize(16);
#endif
#endif

#ifdef HAS_HM485
  hm485_initialize();
#endif

#ifdef HAS_HELIOS
  helios_initialize();
#endif

#ifdef HAS_CUNOTTY
rf_cunotty_init();
#endif

  sei();

#ifdef HAS_DMX
  dmx_start();
#endif

  for(;;) {
    uart_task();
    RfAnalyze_Task();
    Minute_Task();
#ifdef HAS_FASTRF
    FastRF_Task();
#endif
#ifdef HAS_RF_ROUTER
    rf_router_task();
#endif
#ifdef HAS_CUNOTTY
	rf_cunotty_task();
#endif
#ifdef HAS_ASKSIN
    rf_asksin_task();
#endif
#ifdef HAS_IRRX
    ir_task();
#endif
#ifdef HAS_ETHERNET
    Ethernet_Task();
#endif
#ifdef HAS_VZ
    vz_task();
#endif
#ifdef HAS_MORITZ
    rf_moritz_task();
#endif
#ifdef HAS_HM485
    hm485_task();
#endif    
#ifdef HAS_HELIOS
    helios_task();
#endif    
  }

}
Beispiel #11
0
int
main(void)
{

  led_init();

#ifdef LED_RGB
  led_off(LED_CHANNEL_GREEN);
  led_off(LED_CHANNEL_RED);
  led_off(LED_CHANNEL_BLUE);
#else
  LED_ON();
#endif


  spi_init();


  OCR0A  = 249;                            // Timer0: 0.008s = 8MHz/256/250 == 125Hz
  TCCR0B = _BV(CS02);
  TCCR0A = _BV(WGM01);
  TIMSK0 = _BV(OCIE0A);

  TCCR1A = 0;
  TCCR1B = _BV(CS11) | _BV(WGM12);         // Timer1: 1us = 8MHz/8

  clock_prescale_set(clock_div_1);

  MCUSR &= ~(1 << WDRF);                   // Enable the watchdog

  uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );

  input_handle_func = analyze_ttydata;

  display_channel = DISPLAY_USB;

#ifdef LED_RGB
  my_delay_ms(200);
  led_on(LED_CHANNEL_RED);
  my_delay_ms(200);
  led_off(LED_CHANNEL_RED);
  led_on(LED_CHANNEL_GREEN);
  my_delay_ms(200);
  led_off(LED_CHANNEL_GREEN);
  led_on(LED_CHANNEL_BLUE);
  my_delay_ms(200);
  led_off(LED_CHANNEL_BLUE);
#else
  LED_OFF();
#endif

  sei();

  /* start moritz function */
  moritz_func("Zr\n");
  for(;;) {
	led_process(ticks);

    uart_task();
    Minute_Task();
    rf_asksin_task();
    rf_moritz_task();
    if (rf_moritz_data_available()) {
        DC('Z');
        uint8_t *rf_data = (uint8_t*) &max_data;
        for (uint8_t i=0; i<=*rf_data; i++) {
        	DH2( *rf_data++ );
        }
        DNL();
        DS("length: ");
        DU(max_data.length, 2);
        DNL();
        DS("msg count: ");
        DU(max_data.message_count, 2);
        DNL();
        DS("msg type: ");
        DU(max_data.message_type, 2);
        DNL();
    }
  }

}
int main( void )
{
	init_timer();
	init_uart();

	/*
	 * Configure the port for incoming pulses with falling edge
	 * triggered IRQ's
	 */
	sbi( PORTD, 2 );
	cbi( DDRD, 2 );
	sbi( MCUCR, ISC01 );
	cbi( MCUCR, ISC00 );
	sbi( GIMSK, INT0 );

	sei();
	puts( "$Id: joystick.c,v 2.0 2002/09/22 02:10:18 tramm Exp $\r\n" );

	while( 1 )
	{
		uint16_t start;
		uint16_t width;


		/*
		 *  The UART is running with interrupts disabled, so we
		 * have to manually flush it until there are no bytes
		 * left in the queue.  This means that we're also missing
		 * frames from the transmitter, but it will resend the
		 * values anyway.
		 *
		 * In practice, we miss every other frame.  The wait
		 * for sync pulse loop will see 8 pulses go by before
		 * the next sync.  This corresponds to one PPM frame.
		 */
		while( !uart_send_empty() )
			uart_task();

		/*
		 * Wait for a sync pulse.
		 */
		start = last_int;

		while(1)
		{
			if( start == last_int )
				continue;

	 		width = last_int - start;
			start = last_int;
			if( width > SYNC_WIDTH )
				break;
		}


		/*
		 * Put out a time stamp for the user to know how long
		 * has passed since the last update.
		 */
		put_uint16_t( start );
		putc( ' ' );


		/*
		 *  Print out pulse widths until we receive another
		 * sync pulse.
		 */
		while( 1 )
		{
			if( start == last_int )
				continue;

	 		width = last_int - start;
			start = last_int;

			if( width > SYNC_WIDTH )
				break;

			put_uint16_t( width );
			putc( ' ' );
		}
		
		puts( "\r\n" );
	}

	return 0;
}