  /*######################################################################################  
 ##########################################################################################
## ######################################################## ####################          ##
## ########  #######   ###      ## ########    ######   ### ####### ########  ###      ## ##
## ###    ## ###    #  ####    ### ###    ##  ###   ##  ### ###     ###    ## ####    ### ##
## ###   ### ###    ## #######  ## ###   ### ###     ## ### ###     ###   ### #######  ## ##
## #######   ######### ### ###  ## ########  ###     ## ### ####### #######   ### ###  ## ##
## ###   ### ###    ## ###  ##  ## ###   ### ###     ## ### ###     ###   ### ###  ##  ## ##
## ###    ## ###    ## ###      ## ###    ##  ###   ##  ### ###     ###    ## ###      ## ##
## ###    ## ###    ## ###      ## ########    ######   ### ####### ###    ## ###      ## ##
## #################################################### ### ############################# ##
############################################################################################
##### 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"

// -- Contact ID Init --
  void rTerm::cidInit(){

    Serial3.begin(115200);
    delay(100);

    Serial.println(F("CID Serial Initialized / 115200 bps -- "));
    delay(100);
    
  }

// -- Contact ID RX() --
  void rTerm::cidRx(){

    if(rTerm::cidEN){

      if(Serial3.available()){

        rTerm::loopIndex = 0;
        
        char logPath[15];
        char logName[5];
        itoa(rTerm::logIndexCID, logName, 10);
        
        strcpy(logPath, "/cid/");
        strcat(logPath, logName);
        strcat(logPath, ".log");
        logPath[strlen(logPath)] = char('\0');
        
        File cidLog = SD.open(logPath, FILE_WRITE);
    
        if(!cidLog){
          
          Serial.print(F("Could not open file path: ")); Serial.println(logPath); delay(10);
          return;
          
        }else{

          char _buff[30];
          strcpy(_buff, "cid(");
          strcat(_buff, logName);
          strcat(_buff, "): ");
          
          _buff[strlen(_buff)] = char('\0');
          int buffIndex = strlen(_buff);
          
          while(Serial3.available()){
            
            if(char(Serial3.peek()) == char('\r')){
              Serial3.read();
            }else if(char(Serial3.peek()) == char('\n')){
              Serial3.read();
              break;
            }else{
              _buff[buffIndex] = char(Serial3.read());
              buffIndex++;
            }
              
                  
          }

          _buff[buffIndex] = char('\0');
          
          cidLog.write(_buff); delay(10);
          
          cidLog.close(); delay(10);
    
          Serial.println(_buff); delay(10);

          for(int i=0; i<30; i++){
            _buff[i] = char('\0');
          }
          
          rTerm::logIndexCID++;
          EEPROM.put(186, rTerm::logIndexCID);
          
          delay(10);
          
        }
        
      }
      
    }
  
  }
