#include <JeeLib.h>

//sets up the port of the relay
Port relays (2);

// setup the UART board port
PortI2C i2cBus (3);
UartPlug uart (i2cBus, 0x4D);

void setup()
{
  Serial.begin(9600);
  uart.begin(19200);
  
  //initialize relays
  relays.digiWrite(0);      //  air pump off
  relays.digiWrite2(0);     //  co2 sensor off
  relays.mode(OUTPUT);
  relays.digiWrite2(1);      // co2  sensor on
  Serial.print("Functionality");
  delay(50000);
  //uncomment following line to clear the ABC logic off
  //Abc_Logic_Off();
 //Check_Abc_Logic();
  
 //uncomment the following lineS to calibrate the sensor to 600 ppm
 //delay(550000);
 //Calibrate_Telair(); 
}

void loop()
{
  
  //read CO2 every 10 seconds
  int co2=read_Co2();
  Serial.print("CO2 ppm=");
  Serial.println(co2);
  delay(10000);
}


/******************************************************************
 Function name: read_Co2(void)
 type: int
 input parameters: void
 output parameter: int
 Description: tells tehe sensor to start doing measurments and store 
              the two bytes in a varible called co2value
 Creators: MBARI
********************************************************************/
int read_Co2()
{
  byte buf[56];
  byte i=0;
  int co2Value;
  
  uart.write(0xFF);
  uart.write(0xFE);
  uart.write(0x02);
  uart.write(0x02);
  uart.write(0x03);
  delay(20);
  
  // put he two bit answer from the sensor into a buffer
  while(uart.available())
  {
    buf[i]=uart.read();
    i++;
  }
  
  // do a bitwise operation to acomodate the two bits
  // and make them an integer
  co2Value=0;
  co2Value|=buf[3] & 0xFF;
  co2Value=co2Value<<8;
  co2Value|=buf[4] & 0xFF;  
  return co2Value;
}

/******************************************************************
 Function name: Abc_Logic_Off()
 type: void
 input parameters: NULL
 output parameter: NULL
 Description:Tells the sensor to turn off the ABC logic (self calibration
             process)
 Creators: MBARI
********************************************************************/
void Abc_Logic_Off()
{
  byte buf[10]={}; 
  boolean out =true;
    while(out)
    {
      int i=0;
      uart.write(0xFF);
      uart.write(0xFE);
      uart.write(0x02);
      uart.write(0xB7);
      uart.write(0x02);
      delay(20);
      
      // put he two bit answer from the sensor into a buffer
      while(uart.available())
      {
      uart.read();
      }
      delay(20);
      //check if the abc logic was turned of
      uart.write(0xFF);
      uart.write(0xFE);
      uart.write(0x02);
      uart.write(0xB7);
      uart.write(0x00);
      delay(20);
  
      // put he two bit answer from the sensor into a buffer
      while(uart.available())
      {
      buf[i]=uart.read();
      i++;
      }
      Serial.println(buf[3]);
      if(buf[3]==0x01)
      {
        Serial.println("ABC logic on");
      }
      if(buf[3]==0x2)
      {
        Serial.println("ABC loigc turned off");
        out=false;
      }
      else if((buf[3]<1||buf[3]>2))
      {
        Serial.println("error reading ABC logic status");
      }
    }
}

/******************************************************************
 Function name: Check_Abc_Logic()
 type: void
 input parameters: NULL
 output parameter: NULL
 Description:Tells the sensor chek for the ABC logic status
 Creators: MBARI
********************************************************************/
void Check_Abc_Logic()
{
  boolean out=true;
  byte buf[10]={};
  
  while(out)
  {
    int i=0;
     uart.write(0xFF);
      uart.write(0xFE);
      uart.write(0x02);
      uart.write(0xB7);
      uart.write(0x00);
      delay(20);
  
      // put he two bit answer from the sensor into a buffer
      while(uart.available())
      {
      buf[i]=uart.read();
      i++;
      }
      Serial.println(buf[3]);
      if(buf[3]==0x01)
      {
        Serial.println("ABC logic on");
        out=false;
      }
      if(buf[3]==0x2)
      {
        Serial.println("ABC loigc turned off");
        out=false;
      }
      else if((buf[3]<1||buf[3]>2))
      {
        Serial.println("error reading ABC logic status");
      }
    }
}

/******************************************************************
 Function name:Calibrate_Telair()
 type: void
 input parameters: NULL
 output parameter: NULL
 Description: Calibrates the telair with a CO2 concentration of 600 ppm.
              If the value wants to be chnaged, go to the commands where
              it says "setting the calibration point to 600" and change
              the last two commands for the hexadecimal represenation of the
              CO2 concentration that will be used to do the calibration.
 Creators: MBARI
********************************************************************/
void Calibrate_Telair()
{
  byte reading[10];
  byte proof[10];
  byte  checking[10];
  int i=0;
  byte  checking1[6]={1,1,1,1,1,1};
 
  uart.write(0xFF);
  uart.write(0xFE);
  uart.write(0x01);
  uart.write(0xB6);
    while(uart.available())
    {
      checking[i]=uart.read();
    }
    i=0;
    if(checking[3]==0)
    {
      Serial.println("ready to set point");
    }
 
 
  //setting the calibration point to 600
  Serial.println("setting the calibration point to 600");
  uart.write(0xFF);
  uart.write(0xFE);
  uart.write(0x04);
  uart.write(0x03);
  uart.write(0x11);
  uart.write(0x02);
  uart.write(0x58);
    while(uart.available())
    {
      uart.read();
    }
 
 
 // verifying that tyhe calibration process is at 600
  uart.write(0xFF);
  uart.write(0xFE);
  uart.write(0x02);
  uart.write(0x02);
  uart.write(0x11);
    while(uart.available())
    {
      reading[i]=uart.read();
      i++;
    }
    i=0;
 
  Serial.println(reading[3]);
  Serial.println(reading[4]);

 
 //do the equilibration
  uart.write(0xFF);
  uart.write(0xFE);
  uart.write(0x01);
  uart.write(0x9B);
 delay(4000);
 
   while(uart.available())
   {
     uart.read();
   }
  while(checking1[3]!=0)
  {
    //check for the status of the sensor
    uart.write(0xFF);
    uart.write(0xFE);
    uart.write(0x01);
    uart.write(0xB6);
    while(uart.available())
    {
      checking1[i]=uart.read();
      i++;
    }
    i=0;
    Serial.println("waiting");
    delay(15000);
    uart.write(0xFF);
    uart.write(0xFE);
    uart.write(0x01);
    uart.write(0x9B);
 
    while(uart.available())
    {
      uart.read();
    }
  }
Serial.println("done");
}



  
  
