  /*######################################################################################  
 ##########################################################################################
## ######################################################## ####################          ##
## ########  #######   ###      ## ########    ######   ### ####### ########  ###      ## ##
## ###    ## ###    #  ####    ### ###    ##  ###   ##  ### ###     ###    ## ####    ### ##
## ###   ### ###    ## #######  ## ###   ### ###     ## ### ###     ###   ### #######  ## ##
## #######   ######### ### ###  ## ########  ###     ## ### ####### #######   ### ###  ## ##
## ###   ### ###    ## ###  ##  ## ###   ### ###     ## ### ###     ###   ### ###  ##  ## ##
## ###    ## ###    ## ###      ## ###    ##  ###   ##  ### ###     ###    ## ###      ## ##
## ###    ## ###    ## ###      ## ########    ######   ### ####### ###    ## ###      ## ##
## #################################################### ### ############################# ##
############################################################################################
##### CURRENT / ramboTerm 2.2_ino / 04-04-2020                                            ##
##### ramboTerm: CLI Tool-set for fire alarm interfacing and communications.              ##
##### Matthew Stroble / matthewstroble@gmail.com / firealarmhackery.com                   ##
 ##                                                                                      ##
  ######################################################################################*/


#include <SPI.h>
#include <SD.h>
#include "Wire.h"
#include "EEPROM.h"

#include "rTerm.h"

  void rTerm::fcpRx(){
    
    if(Serial2.available()){

      rTerm::loopIndex = 0;
      char logPath[15];
      char logName[5];
      itoa(rTerm::logIndexFCP, logName, 10);
      strcpy(logPath, "/fcp/"); delay(5);
      strcat(logPath, logName); delay(5);
      strcat(logPath, ".log"); delay(5);
      
      logPath[strlen(logPath)] = char('\0');
      int buffIndex = 0;
      
      File fcpLog = SD.open(logPath, FILE_WRITE);
  
      if(!fcpLog){
        
        Serial.print(F("Could not open file path: ")); Serial.println(logPath); delay(50);
        return;
        
      }else{
          
        int c = 0;
        
        delay(50);
        
        while(Serial2.available()){
  
          if(c < rTerm::lineIndex){
            
            if(char(Serial2.peek()) == char(10)){
              c++;
            }

            fcpLog.write(Serial2.read());

            buffIndex++;
            
          }else if(c == rTerm::lineIndex-1){
            break;
          }
              
        }
                
      }

      fcpLog.close();
  
      delay(20);
      
      if(buffIndex > 1){

        Serial.print(F("fcp(")); Serial.print(rTerm::logIndexFCP); Serial.println(F(") --"));
        
        rTerm::logIndexFCP++;
        
        EEPROM.put(180, rTerm::logIndexFCP); delay(10);
        
      }
  
    }
    
  }
