int main( void )
{
    halInit();
    printf("\r\n****************************************************\r\n");
    printf("Simple Application Example - COORDINATOR - using AFZDO\r\n");
    HAL_ENABLE_INTERRUPTS();
    startZnp(COORDINATOR);

    printf("On Network!\r\n");
    
    /* On network, display info about this network */   
    getNetworkConfigurationParameters();                
    getDeviceInformation();
    
    /* Now the network is running - wait for any received messages from the ZNP */
#ifdef VERBOSE_MESSAGE_DISPLAY    
        printAfIncomingMsgHeaderNames();
#endif
        while (1)
    {
        while (SRDY_IS_HIGH());      //wait until SRDY goes low indicating a message has been received.   
        displayMessages();
    }
    
}
int main( void )
{
    halInit();
    moduleInit();
    printf("\r\n****************************************************\r\n");    
    printf("Zigbee Network Explorer\r\n");
    debugConsoleIsr = &handleDebugConsoleInterrupt;
    HAL_ENABLE_INTERRUPTS();
    clearLeds();
    
    /* Now the network is running - wait for any received messages from the ZM */
#ifdef VERBOSE_MESSAGE_DISPLAY    
    printAfIncomingMsgHeaderNames();
#endif
    
    stateMachine();
}
/** 
The main state machine for the application.
Never exits.
*/
void stateMachine()
{
 //   while (1)
  //  {
        if (zigbeeNetworkStatus == NWK_ONLINE)
        {
            if(moduleHasMessageWaiting())      //wait until SRDY goes low indicating a message has been received. 
                stateFlags |= STATE_FLAG_MESSAGE_WAITING;
        }
        
        switch (state)
        {
        case STATE_IDLE:
            {
                if (stateFlags & STATE_FLAG_MESSAGE_WAITING & coordinator_on)    // If there is a message waiting...
                {
                    parseMessages();                            // ... then display it
                    trackingStateMachine(current_router_index);
                    stateFlags &= ~STATE_FLAG_MESSAGE_WAITING;
                }
                
                if (stateFlags & STATE_FLAG_BUTTON_PRESSED)     // If ISR set this flag...
                {
                    if (debounceButton(ANY_BUTTON))             // ...then debounce it
                    {
                        processButtonPress();                   // ...and process it
                    }
                    if (debounceButtonHold(ANY_BUTTON))
                    {
                        processButtonHold();
                    }
                    stateFlags &= ~STATE_FLAG_BUTTON_PRESSED;
                }
                
                /* Other flags (for different messages or events) can be added here */
                break;
            }
            
        case STATE_MODULE_STARTUP:              // Start the Zigbee Module on the network
            {
#define MODULE_START_DELAY_IF_FAIL_MS 5000
                moduleResult_t result;
                struct moduleConfiguration defaultConfiguration = DEFAULT_MODULE_CONFIGURATION_COORDINATOR;
                
                /* Uncomment below to restrict the device to a specific PANID
                defaultConfiguration.panId = 0x1234;
                */
                
                /* Below is an example of how to restrict the device to only one channel:
                defaultConfiguration.channelMask = CHANNEL_MASK_17;
                printf("DEMO - USING CUSTOM CHANNEL 17\r\n");
                */
                
                while ((result = startModule(&defaultConfiguration, GENERIC_APPLICATION_CONFIGURATION)) != MODULE_SUCCESS)
                {
                    printf("FAILED. Error Code 0x%02X. Retrying...\r\n", result);
                    delayMs(MODULE_START_DELAY_IF_FAIL_MS);
                }
                //printf("Success\r\n");
                zigbeeNetworkStatus = NWK_ONLINE;
                
                state = STATE_DISPLAY_NETWORK_INFORMATION;
                break;
            }
        case STATE_DISPLAY_NETWORK_INFORMATION:
            {
                printf("~ni~");
                /* On network, display info about this network */
                displayNetworkConfigurationParameters();
                displayDeviceInformation();
                if (sysGpio(GPIO_SET_DIRECTION, ALL_GPIO_PINS) != MODULE_SUCCESS)   //Set module GPIOs as output
                {
                    printf("ERROR\r\n");
                }
                /*
                printf("Press button to change which received value is displayed on RGB LED. D6 & D5 will indicate mode:\r\n");
                printf("    None = None\r\n");
                printf("    Yellow (D9) = IR Temp Sensor\r\n");
                printf("    Red (D8) = Color Sensor\r\n");
                */
                printf("Displaying Messages Received\r\n");
                setModuleLeds(RGB_LED_DISPLAY_MODE_NONE);
                
                /* Now the network is running - wait for any received messages from the ZM */
#ifdef VERBOSE_MESSAGE_DISPLAY    
                printAfIncomingMsgHeaderNames();
#endif                
                state = STATE_IDLE;
                break;
            }
            
        default:     //should never happen
            {
                printf("UNKNOWN STATE\r\n");
                state = STATE_MODULE_STARTUP;
            }
            break;
        }
 //   } 
}    
/** 
The main state machine for the application.
Never exits.
*/
void stateMachine()
{
    while (1)
    {
        if (zigbeeNetworkStatus == NWK_ONLINE)
        {
            if(moduleHasMessageWaiting())      //wait until SRDY goes low indicating a message has been received. 
                stateFlags |= STATE_FLAG_MESSAGE_WAITING;
        }
        
        switch (state)
        {
        case STATE_IDLE:
            {
                if (stateFlags & STATE_FLAG_MESSAGE_WAITING)    // If there is a message waiting...
                {
                    parseMessages();                            // ... then display it
                    stateFlags &= ~STATE_FLAG_MESSAGE_WAITING;
                }
                
                if (stateFlags & STATE_FLAG_BUTTON_PRESSED)     // If ISR set this flag...
                {
                    state = STATE_BUTTON_PRESSED;
                    stateFlags &= ~STATE_FLAG_BUTTON_PRESSED;
                }                
                /* Other flags (for different messages or events) can be added here */
            }
            break;            
            
        case STATE_BUTTON_PRESSED:
            {
                rgbLedColor++;
                if (rgbLedColor > RGB_LED_COLOR_MAX)
                {
                    rgbLedColor = 0; 
                }
                printf("Setting Color to %s (%u)\r\n", getRgbLedColorName(rgbLedColor), rgbLedColor);
                switch (rgbLedColor)
                {
                case RGB_LED_COLOR_WHITE: red=RGB_LED_MAX; blue=RGB_LED_MAX; green=RGB_LED_MAX; break;
                case RGB_LED_COLOR_RED: red=RGB_LED_MAX; blue=0; green=0; break;
                case RGB_LED_COLOR_VIOLET: red=RGB_LED_MAX; blue=RGB_LED_MAX; green=0; break;
                case RGB_LED_COLOR_BLUE: red=0; blue=RGB_LED_MAX; green=0; break;        
                case RGB_LED_COLOR_CYAN: red=0; blue=RGB_LED_MAX; green=RGB_LED_MAX; break;
                case RGB_LED_COLOR_GREEN: red=0; blue=0; green=RGB_LED_MAX; break;
                case RGB_LED_COLOR_YELLOW: red=RGB_LED_MAX; blue=0; green=RGB_LED_MAX; break;
                default: red=RGB_LED_MAX; blue=RGB_LED_MAX; green=RGB_LED_MAX; break;
                }
                halRgbSetLeds(red, blue, green);
                state = STATE_IDLE;                
            }
            break;
            
        case STATE_MODULE_STARTUP:              // Start the Zigbee Module on the network
            {
#define MODULE_START_DELAY_IF_FAIL_MS 5000
                moduleResult_t result;
                struct moduleConfiguration defaultConfiguration = DEFAULT_MODULE_CONFIGURATION_COORDINATOR;
                
                /* Change this if you wish to use a custom PAN */
                defaultConfiguration.panId = ANY_PAN;
                
                /*Example of how to use a custom channel:
                printf("DEMO - USING CUSTOM CHANNEL 25\r\n");
                defaultConfiguration.channelMask = CHANNEL_MASK_25; */
                
                /* Change this below to be your operating region - MODULE_REGION_NORTH_AMERICA or MODULE_REGION_EUROPE */
#define OPERATING_REGION    (MODULE_REGION_NORTH_AMERICA) // or MODULE_REGION_EUROPE
                
                while ((result = expressStartModule(&defaultConfiguration, GENERIC_APPLICATION_CONFIGURATION, OPERATING_REGION)) != MODULE_SUCCESS)
                {
                    SET_NETWORK_FAILURE_LED_ON();          // Turn on the LED to show failure
                    printf("FAILED. Error Code 0x%02X. Retrying...\r\n", result);
                    delayMs(MODULE_START_DELAY_IF_FAIL_MS/2);                    
                    SET_NETWORK_FAILURE_LED_OFF();
                    delayMs(MODULE_START_DELAY_IF_FAIL_MS/2);
                }                
                INIT_BOOSTER_PACK_LEDS();                
                SET_NETWORK_LED_ON();
                SET_NETWORK_FAILURE_LED_OFF();
                halRgbSetLeds(RGB_LED_MAX, RGB_LED_MAX, RGB_LED_MAX);
                printf("Module Start Complete\r\n"); 
                SET_NETWORK_STATUS_ONLINE();
                
                state = STATE_DISPLAY_NETWORK_INFORMATION;
            }
            break;            
            
        case STATE_DISPLAY_NETWORK_INFORMATION:
            {
                printf("~ni~");
                /* On network, display info about this network */
                displayNetworkConfigurationParameters();
                displayDeviceInformation();
                
                printf("Press button to change color configuration\r\n");                
                printf("Displaying Messages Received\r\n");
                
                /* Now the network is running - wait for any received messages from the ZM */
#ifdef VERBOSE_MESSAGE_DISPLAY    
                printAfIncomingMsgHeaderNames();
#endif                
                state = STATE_IDLE;
            }
            break;            
            
        default:     //should never happen
            {
                printf("UNKNOWN STATE\r\n");
                state = STATE_MODULE_STARTUP;
            }
            break;
        }
    } 
}