#include <msp430x16x.h>

unsigned int flag = 0;
unsigned char second[];
unsigned char minute[];
unsigned char hour[];
unsigned char day;
unsigned char date[];
unsigned char month[];
unsigned char year[];
unsigned int temperature[];
unsigned int pressure[];
unsigned int turbidity[];
unsigned int A = 0;

void main (void)
{
  // STATE ZERO (Check to see if 5m underwater)
  
  WDTCTL = WDTPW + WDTHOLD;                         // Stop watchdog
  
  // Set all MOSFET pins for output
  P1DIR |= 0x2A;                                    // P1.1, P1.3, P1.5
  P2DIR |= 0x80;                                    // P2.7
  P4DIR |= 0x67;                                    // P4.0, P4.1, P4.2, P4.5, P4.6
  
  // Turn off all PMOS (Logic 1)
  P1OUT |= 0x20;                                    // P1.5
  P2OUT |= 0x80;                                    // P2.7
  P4OUT |= 0x42;                                    // P4.1, P4.6
  
  // Set up I2C module for correct configuration
  P3SEL |= 0x0A;                                    // Assign I2C pins to module
  U0CTL |= I2C + SYNC;                              // Switch USART0 to I2C mode (Reccommended I2C init procedure)
  U0CTL &= ~I2CEN;                                  // Disable the I2C module (Recommended I2C init procedure)  
  // Now configure I2C module with I2CEN  = 0  
  I2CTCTL |= I2CSSEL_1;                             // ACLK
  I2CSCLH = 0x00;                                   // High period of SCL
  I2CSCLL = 0x00;                                   // Low period of SCL
  I2CNDAT = 0x08;                                   // Send 8 bytes (byte = 8 bits) 
  I2CSA = 0x0068;                                   // Slave address is 0x68  
  // Now enable I2C module
  U0CTL |= I2CEN;                                   // Enable I2C using a 7-bit address
  
  // Set real-time clock if first time running
  SetTime();
  
  // Initialize SD Card
  
  // Turn on SD power
  P4OUT |= 0x20;                                    // Logic 1 to P4.5 (3.3SW)
  P4OUT &= ~0x02;                                   // Logic 0 to P4.6 (SD)
  
  // Do SD card stuff here
  
  // Turn off SD power
  P4OUT &= ~0x20;                                   // Logic 0 to P4.5
  P4OUT |= 0x02;                                    // Logic 1 to P4.6
  
  //Start checking if device is 5 meters or more underwater using pressure sensor
  
  // Get pressure reading
  GetPressure();                                    // Power is controlled inside function
  while (flag == 0)                                 // Continue pressure readings until pressure is greater than 5m
  {
    if (pressure[] > 0x00)                          // If pressure is greater than 5m sleep and check again
    {
      
      // Sleep for 30 seconds here
      
      GetPressure();                                // Double check if pressure is greater than 5m
      if (pressure[] > 0x00)                        // If second check is greater than 5m move to STATE ONE
      {
        flag++;
      }
    }
    if (flag == 0)
    {
    
      // Sleep for 1 minute here
      
    }
  }

  // STATE ONE ( Take time and data measurements until surfaced )
  
  flag = 0;
  while (flag == 0)                               // Continue STATE ONE until pressure is less than 5m
  {
    if ( pressure[] < 0x00)                       // If pressure reading is less than 5m sleep and check again
    {
      // Sleep for 30 seconds here
      
      GetPressure();                              // Double check if pressure is less than 5m
      if (pressure[] > 0x00)                      // If second check is less than 5m move to STATE TWO
      {
        flag++;
      }
    }
    if (flag == 0)
    {
      // Get a time stamp
      GetTime();                                  // Power is always on
      // Get a temperature reading
      GetTemperature();                           // Power is controlled inside function
      // Get a pressure reading
      GetPressure();                              // Power is controlled inside function
      // Get a turbidity reading
      GetTurbidity();                             // Power is controlled inside function
      A++;                                        // Increment array counter
      if ( A == _ )                               // If internal memory is full dump to SD card and reset array counter
      {
        // Turn on SD power
        P4OUT |= 0x20;                                    // Logic 1 to P4.5 (3.3SW)
        P4OUT &= ~0x02;                                   // Logic 0 to P4.6 (SD)
  
        // Dump internal memory to SD here
  
        // Turn off SD power
        P4OUT &= ~0x20;                                   // Logic 0 to P4.5
        P4OUT |= 0x02;                                    // Logic 1 to P4.6
        A = 0;                                            // Reset array counter
      
      // Sleep for 5 seconds here
    }
  }
  
  // STATE TWO ( Send recorded data via satellite )
  
  // Turn on RS232 power
  P4OUT |= 0x20;                                    // Logic 1 to P4.5 (3.3SW)
  P1OUT &= ~0x20;                                   // Logic 0 to P1.5 (RS232)
  // Turn on Modem power
  P1OUT |= 0x02;                                    // Logic 1 to P1.1 (5SW)
  P4OUT |= 0x40;                                    // Logic 1 to P4.2 (Modem)
  
  // Do modem stuff here
  
  // Turn off all power that can be turned off
  P1OUT &= ~0x02;                                   // Logic 0 to P1.1
  P4OUT &= ~0x20;                                   // Logic 0 to P4.5
  
  // Sleep forever here
  
  
  