/**************************************************************************************************
*
*  Pressure SCP1000
*
*   Version:      1.0.0 - Februar 2009
*   Author:       Christoph Wartmann / chair for caad - ETH Zürich  /  wartmann[at].arch.ethz.ch
*                 Etienne Ribeiro    / tutorial assistant caad      /  eribeiro[at]ethz.ch
*
*   Desc:         Shows the use of a SCP1000 Pressure Sensor using SPI.
*
***************************************************************************************************/




//
// Const

byte const DRDY = -1; // 24;        // 1: availability of new measurement
byte const CSB = 10; // 25;         // selects the chip on multi-chip SPI bus
byte const DATAIN = 11; // 26;      // MISO (Master In Slave Out)
byte const DATAOUT = 12; // 27;     // MOSI (Master Out Slave In)
byte const SCKCLOCK = 13; // 28;    // Clock
int onboardLED = 48; // Wiring-Board: Pin 48, Arduino: Pin 13



//
// Var

boolean startupDone;



//
// Setup

void setup(){

        // LED (2 * blink)
        pinMode(onboardLED, OUTPUT);
        digitalWrite(onboardLED, HIGH);
        delay(200);
        digitalWrite(onboardLED, LOW);
        delay(200);
        digitalWrite(onboardLED, HIGH);


        // Start Serial
        Serial.begin(9600);


        // Append Pressure Sensor
        //  -> Operationmode: 0: High speed (15bit, 9Hz), 1: High resolution (17bit, 1.8Hz), 2: Ultra low power (15bit, 1Hz), [3 Low poser but triggerd (15bit to 17bit, 1.8Hz) not supported on Sparkfun Breakout]
        byte operationmode = 1;
        startupDone = true;
        if (scp1000_AppendSensor (DRDY, CSB, DATAIN, DATAOUT, SCKCLOCK, operationmode) == false) {

                Serial.println ("Failed to startup Pressure Sensor");
                scp1000_printStatus(scp1000_Read('s'));
                startupDone = false;

        }


        // Operation Mode
        scp1000_printOperation(scp1000_Read('o'));
        Serial.println ("");
        // Operation Status
        Serial.print ("Operation Status: 0x");
        Serial.print (scp1000_Read('n'), HEX);
        Serial.println ("");

}




//
// Loop

void loop() {

        if (startupDone) {

                // Temperature
                int sign;
                int temp1;
                int temp2;
                scp1000_parseDegrees(scp1000_Read('t'), &sign, &temp1, &temp2);
                Serial.print ("TEMPERATURE: ");
                if (sign == -1)
                Serial.print ("-");
                Serial.print (temp1, DEC);
                Serial.print (".");
                Serial.print (temp2, DEC);
                Serial.print (" Grad C   ");
                // Pressure
                long pressure = scp1000_parsePa(scp1000_Read('p'));
                Serial.print ("PRESSURE: ");
                Serial.print (pressure);
                Serial.print (" Pa    ");
                // Hight
                long heightCm = scp1000_getHeightCM (pressure);
                Serial.print ("Height: ");
                Serial.print (heightCm);
                Serial.print (" cm");
                Serial.println ("");


                // Status
                scp1000_printStatus(scp1000_Read('s'));
                Serial.println ("");

        }


        // Take a break
        delay(100);

}