void main(void) {
#else
int main(void) {
#endif
    /* Ensure that the Watchdog is not running. */
    wdt_disable();
    
	/* Initialize system. */
	if (true != avr_init()) {
		error_handler();
	} else if (true != eep_init()) {
        eep_deinit();
        error_handler();
    } else if (true != cmd_if_init()) {
        cmd_if_deinit();
        error_handler();
    }
	
    /* Disable modules that are not needed any more. */
    eep_deinit();
    
    LED_ORANGE_ON();
        
    /* Enable interrupts. */
    sei();
    
	/* Endless application loop. */
	for(;;) {
        /* Dispatch events from the event queue. */
		vrt_dispatch_event();
        
        /* Poll modules that require this. */
        vrt_timer_task();
        usb_task();
        air_capture_task();
        cmd_if_task();
	}
}
/* Main function */
int main(void)
{
    int value_mg_x, value_mg_y, value_mg_z;

    /* setup all GPIOs */
    gpio_setup();

    /* initialise LIS3DSH */
    lis3dsh_init();

    /* infinite loop */
    while (1) {
        /* get X, Y, Z values */
        value_mg_x = ((lis3dsh_read_reg(ADD_REG_OUT_X_H) << 8) |
                      lis3dsh_read_reg(ADD_REG_OUT_X_L));
        value_mg_y = ((lis3dsh_read_reg(ADD_REG_OUT_Y_H) << 8) |
                      lis3dsh_read_reg(ADD_REG_OUT_Y_L));
        value_mg_z = ((lis3dsh_read_reg(ADD_REG_OUT_Z_H) << 8) |
                      lis3dsh_read_reg(ADD_REG_OUT_Z_L));

        /* transform X value from two's complement to 16-bit int */
        value_mg_x = two_compl_to_int16(value_mg_x);
        /* convert X absolute value to mg value */
        value_mg_x = value_mg_x * SENS_2G_RANGE_MG_PER_DIGIT;

        /* transform Y value from two's complement to 16-bit int */
        value_mg_y = two_compl_to_int16(value_mg_y);
        /* convert Y absolute value to mg value */
        value_mg_y = value_mg_y * SENS_2G_RANGE_MG_PER_DIGIT;

        /* transform Z value from two's complement to 16-bit int */
        value_mg_z = two_compl_to_int16(value_mg_z);
        /* convert Z absolute value to mg value */
        value_mg_z = value_mg_z * SENS_2G_RANGE_MG_PER_DIGIT;

        /* set X related LEDs according to specified threshold */
        if (value_mg_x >= LED_TH_MG) {
            LED_BLUE_OFF();
            LED_ORANGE_OFF();
            LED_GREEN_OFF();
            LED_RED_ON();
        } else if (value_mg_x <= -LED_TH_MG) {
            LED_BLUE_OFF();
            LED_ORANGE_OFF();
            LED_RED_OFF();
            LED_GREEN_ON();
        }

        /* set Y related LEDs according to specified threshold */
        if (value_mg_y >= LED_TH_MG) {
            LED_BLUE_OFF();
            LED_RED_OFF();
            LED_GREEN_OFF();
            LED_ORANGE_ON();
        } else if (value_mg_y <= -LED_TH_MG) {
            LED_RED_OFF();
            LED_GREEN_OFF();
            LED_ORANGE_OFF();
            LED_BLUE_ON();
        }

        /* set Z related LEDs according to specified threshold */
        if (value_mg_z >= LED_TH_MG) {
            LED_BLUE_ON();
            LED_ORANGE_ON();
            LED_RED_ON();
            LED_GREEN_ON();
        } else if (value_mg_z <= -LED_TH_MG) {
            LED_BLUE_OFF();
            LED_ORANGE_OFF();
            LED_RED_OFF();
            LED_GREEN_OFF();
        }
    }

    return 0;
}
void main(void) {
#else
int main(void) {
#endif
    /* Ensure that the watchdog is not running. */
    wdt_disable();
        
    /* Initialize AVR peripheral modules. */
    (bool)avr_init();
    
    /* Check if the RX and TX pins are shorted. If they are shorted, the RZUSBSTICK
     * shall start the bootloader. If not, continue to verify if the application
     * requested to enter the bootloader.
     */
    
    /* Check if the application has requested to enter the bootloader. */
    if ((BOOT_PIN & (1 << BOOT_RX)) != (1 << BOOT_RX)) {
        /* Check that RX goes high when TX is pulled high. */
        BOOT_PORT |= (1 << BOOT_TX);
        
        nop();
        nop();
        nop();
        nop();
        
        if ((BOOT_PIN & (1 << BOOT_RX)) != (1 << BOOT_RX)) {
            start_application();
        }
    } else {
        /* Check if the application has requested to enter the bootloader. */
        uint8_t volatile magic_value = 0xAA;
        EEGET(magic_value, EE_BOOT_MAGIC_ADR);
   
        if (EE_BOOT_MAGIC_VALUE != magic_value) {
            start_application();
        } else {
            EEPUT(EE_BOOT_MAGIC_ADR, 0xFF);
        }
    }
    
    /* Set the interrupt vectors to the bootloader, initialize the LEDs and the
     * VRT kernel.
     */
    ENTER_CRITICAL_REGION();
    uint8_t temp_mcucr = MCUCR;
    MCUCR = (1 << IVCE);
    MCUCR = (1 << IVSEL);
    MCUCR = temp_mcucr;
    LEAVE_CRITICAL_REGION();

    LED_INIT();
    vrt_init();
    
    if (true != eep_init()) {
        error_handler();
    } else if (true != cmd_if_init()) {
        error_handler();
    }
    
    LED_ORANGE_ON();
    
    /* Enable Interrupts. */
    sei();
    
    /* Enter the endless application loop. */
    for (;;) {
        vrt_dispatch_event();
        usb_task();
    }
}