/******************************************************************
 ******************************************************************
 *                                                                *
 *                POST FIRE SHUTDOWN SAFETY PERIOD                *
 *                                                                *
 ******************************************************************
 ******************************************************************/

void fireShutdown(void)
{
  // =====================================================================
  // Constants for LED clock
  
    // Constants for STAY
      const uint8_t SEG_STAY[] = 
      {
      SEG_A | SEG_C | SEG_D | SEG_F | SEG_G,                  // S
      SEG_D | SEG_E | SEG_F | SEG_G,                          // t
      SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,          // A
      SEG_B | SEG_C | SEG_D | SEG_F | SEG_G                   // Y
      };

    // Constants for BACK
      const uint8_t SEG_BACK[] = 
      {
      SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G,  // B 
      SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,          // A
      SEG_A | SEG_D | SEG_E | SEG_F,                          // C
      SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,                  // K
      };

    // Constants for SAFE
      const uint8_t SEG_SAFE[] = 
      {
      SEG_A | SEG_C | SEG_D | SEG_F | SEG_G,                  // S
      SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,          // A
      SEG_A | SEG_E | SEG_F | SEG_G,                          // F
      SEG_A | SEG_D | SEG_E | SEG_F | SEG_G                   // E
      };

  // =====================================================================
  // Backup to turn off power to pad in case it was not done earlier
    // reset relay to off
       digitalWrite(pinFireRelay, LOW);
     
    // stop buzzer
       digitalWrite(buzzer,LOW);
        
  // =====================================================================
  // Start 60-second wait period before approaching pad   

  // Yellow lamps on test stand
     rgbYellow();
     rgbSteadyLamp();
     
  // show power has stopped flowing on screen if not done earlier
     if (skip == false)
     {
      Serial.println();
      Serial.println(F("Power has stopped flowing to the Test Stand"));
      dateTimeSerialMonitor();
      timeMillis = micros()/1000000.f;    

      // System Log for Post Fire Shutdown
         sdTimeStamp = timeMillis;
         sdRTCStamp = bufferTime;       
         sdMessage = (F("Post Ignition Shutdown"));
         sdResult = (F("Power Off"));
         writeSysDataToCard(); 
     }  
     
  // One minute "Stay Away" post firing safety period
     Serial.println();
     Serial.println(F("Begin 1-minute post test safety period."));
     Serial.println(F("Stay Clear of the Test Stand till 'All Clear' is given"));
     dateTimeSerialMonitor();
     timeMillis = micros()/1000000.f;

  // System Log for Post Fire Shutdown
     sdTimeStamp = timeMillis;
     sdRTCStamp = bufferTime;       
     sdMessage = (F("Post Test Safety Period"));
     sdResult = (F("Start"));
     writeSysDataToCard(); 
     
  // Start loop for pad safety countdown 
     for (int safety = 60; safety > 0; safety -=10)
     {      
      clockDisplay.setSegments(SEG_STAY); 
      lcd.setCursor(0,0);
      lcd.print(F("Keep Stand Clear"));
      lcd.setCursor(0,1);
      lcd.print(F(" for ")); 
      lcd.print(safety); 
      lcd.print(F("-seconds ")); 
      
      // show data while looping for 5 seconds
      for (x = 1; x <= 5 ;x++)
      {
        delay (1000);
      }
      
      clockDisplay.setSegments(SEG_BACK);
      lcd.setCursor(0,1);
      lcd.print(F(" for ")); 
      lcd.print(safety-5); 
      lcd.print(F("-seconds ")); 
      
      // show data while looping for 5 seconds
      for (x = 1; x <= 5 ;x++)
      {
        delay (1000);
      }
      
     }
  
  // show green lamp on test stand
     rgbGreen();
     rgbSteadyLamp();

  // turn off strobes
     MsTimer2::stop();
     
  // return to normal operations  
     Serial.println();
     Serial.println(F("All Clear Announced"));
     dateTimeSerialMonitor();
     Serial.println();
     
     timeMillis = micros()/1000000.f;

  // System Log for Post Fire Shutdown
     sdTimeStamp = timeMillis;
     sdRTCStamp = bufferTime;       
     sdMessage = (F("Post Test Safety Period"));
     sdResult = (F("Finished"));
     writeSysDataToCard(); 
     
  // Display "SAFE" LED countdown clock
     clockDisplay.setSegments(SEG_SAFE);

  // Indicate safe on LCD  
     lcd.setCursor(0,0);
     lcd.print(F("It is safe to   "));
     lcd.setCursor(0,1);
     lcd.print(F("approach stand  "));
     
  // quick chirp
     buzzerShortTone();
}
