void processButtonHold()
{
  if (coordinator_on == 0) {
    coordinator_on = 1;
    halRgbSetLeds(0, 0, 0);
  }
  else {
    coordinator_on = 0;
    halRgbSetLeds(0xFF, 0xFF, 0xFF);
  }
}
/** 
Called from state machine when a button was pressed. 
Selects which KVP value is displayed on the RGB LED. 
*/
void processButtonPress()
{
  if (alarm_sounding == 1) {
      halRgbSetLeds(0, 0xFF, 0);
      alarm_silenced = 1;
  }
}
Ejemplo n.º 3
0
/**
Initializes the PWM engine used for the RGB LED. This allows the RGB LED to display many colors.
@post RGB LED may be used, with halRgbSetLeds().
*/
void halRgbLedPwmInit()
{
    RGBInit(0);
	RGBIntensitySet(0.5f);
    RGBEnable();
    halRgbSetLeds(0x7F, 0x7F, 0x7F);   // A dim white
}
/** 
Called from state machine when a button was pressed. 
Selects which KVP value is displayed on the RGB LED. 
*/
void processButtonPress()
{
    rgbLedDisplayMode++;
    if (rgbLedDisplayMode > RGB_LED_DISPLAY_MODE_MAX)
    {
        rgbLedDisplayMode = 0;
        halRgbSetLeds(RGB_LED_PWM_OFF, RGB_LED_PWM_OFF, RGB_LED_PWM_OFF);    
    }
    printf("Setting State to %s (%u)\r\n", getRgbLedDisplayModeName(rgbLedDisplayMode), rgbLedDisplayMode);
    
    uint8_t result = setModuleLeds(rgbLedDisplayMode);
    if (result != 0)
    {
        printf("Error %u setting Module LEDs\r\n", result);
    }
    
    resetNominalTemperature();
    resetNominalColor();
    
}
void trackingStateMachine(int router_index) {
  if (routers[router_index].LQI != 0) {
    
    if (routers[router_index].LQI_iter == LQI_NUM_SAMPLES) {
      routers[router_index].LQI_iter = 0;
      routers[router_index].LQI_initialized = 1;
    }
    
    uint8_t oldest_LQI = routers[router_index].LQI_running_average[(routers[router_index].LQI_iter)];
    routers[router_index].LQI_running_average[routers[router_index].LQI_iter] = routers[router_index].LQI;
    
    if (routers[router_index].LQI_initialized == 1)
      routers[router_index].LQI_total -= oldest_LQI;
    routers[router_index].LQI_total += routers[router_index].LQI;

    routers[router_index].LQI_average = routers[router_index].LQI_total / LQI_NUM_SAMPLES;
    routers[router_index].LQI_iter++;
    
    int i, j, k;
    for (i = 0; i < NUM_DEVICES; i++) {
      printf("Most recent LQI value: %02X\r\n", routers[i].LQI);      
      
      printf("LQI ARRAY for device at MAC address: ");
      for (k = 7; k >= 0; k--) {
        printf("%02X", routers[i].MAC_address[k]);
      }
      printf("\r\n");
      for (j = 0; j < LQI_NUM_SAMPLES; j++) {
        printf("%d:", j);
        printf("%02X ", routers[i].LQI_running_average[j]);
      }
      printf("\r\n");
      printf("AVERAGE: %02X\r\n", routers[i].LQI_average);
    }
   
    switch(routers[router_index].track_state) {
      
    case ALL_ITEMS_CONNECTED:
      alarm_silenced = 0;
      if (alarm_sounding == 0) {
        int items_connected = 0;
        int j;
        for (j = 0; j < NUM_DEVICES; j++) {
          if (routers[router_index].track_state == ALL_ITEMS_CONNECTED)
            items_connected++;
        }
        if (items_connected == NUM_DEVICES)
          printf("ALL DEVICES CONNECTED\r\n");

        halRgbSetLeds(0, 0, 0xFF);
      }
      if (routers[router_index].LQI_average < LQI_THRESHOLD && routers[router_index].LQI_initialized == 1) {
          routers[router_index].track_state = ITEM_LOST_ALARM;
      }
      break;
    /*
    case SUSPECTED_ITEM_LOSS:
      halRgbSetLeds(0, 0xFF, 0);
      if (LQI_average < LQI_THRESHOLD && LQI_initialized == 1) {
          track_state = ITEM_LOST_ALARM;
      }
      else {
        track_state = ALL_ITEMS_CONNECTED;
      }
      break;
    */
    case ITEM_LOST_ALARM:
      printf("LOST ITEM AT ROUTER INDEX: %d\r\n", router_index);
      if (alarm_sounding == 0) {
        halRgbSetLeds(0xFF, 0, 0);
        alarm_sounding = 1;
      }
      if (routers[router_index].LQI_average > LQI_THRESHOLD) {
        routers[router_index].track_state = ALL_ITEMS_CONNECTED;
      }
      int i;
      int devices_connected = 0;
      for (i = 0; i < NUM_DEVICES; i++) {
        if (routers[router_index].track_state == ALL_ITEMS_CONNECTED)
          devices_connected++;
      }
      if (devices_connected == NUM_DEVICES)
        alarm_sounding = 0;
      break;
    
      /*
    case ITEM_LOST_SILENCED:
      halRgbSetLeds(0, 0xFF, 0);
      if (routers[router_index].LQI_average > LQI_THRESHOLD) {
        routers[router_index].track_state = ALL_ITEMS_CONNECTED;
      }
      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;
        }
    } 
}