/**************************************************************************************************
*
*  Accelerator LIS302DL
*
*   Version:      1.0.0 - Januar 2009
*   Author:       Christoph Wartmann / chair for caad - ETH Zürich  /  wartmann[at].arch.ethz.ch
*                 Etienne Ribeiro    / tutorial assistant caad      /  eribeiro[at]ethz.ch
*                 (Based on code from BEN GATTI)
*
*   Desc:         Shows the use of an LIS302DL Accelerator Sensor using I2C-Protocoll.
*
***************************************************************************************************/




//
// Setup

void setup(){

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


        // Serial
        Serial.begin(57600);


        // Initialize Wire
        LIS302_iniWire ();


        // Append Sensor
        Serial.print("AppendSensor ... ");
        boolean success = LIS302_AppendSensor (); // +- 2g
        // boolean success = LIS302_AppendSensor (true); // +- 8g
        if (success == false)
        Serial.println("ERROR while Appending Sensor. Perhaps sensor is connected wrong.");
        else
        Serial.println("done.");


        // Uncomment this, if you want to define further configurations (usually not necessary)
        // Config
        //  default: false, false, false, false, false, true, true, true
        //  boolean dataRate400Hz, boolean powerUp, boolean use8g, boolean enableSelfTestP, boolean enableSelfTestM, boolean enableZ, boolean enableY, boolean enableX
        boolean powerup = true;
        boolean use8g = false;
        LIS302_ConfigR1 (false, powerup, use8g, false, false, true, true, true);
        //  default: false, false, false, false, false, 0
        //  boolean useSPI3WireMode, boolean rebootMemory, boolean enabledFilteredData, boolean enableFreeFallAndWakeUp02, boolean enableFreeFallAndWakeUp01, byte highPassFrequency
        LIS302_ConfigR2 (false, false, false, false, false, 0);
        //  default: false, false, 0, 0
        //  boolean interruptActiveLow, boolean interreptOpenDrain, byte int2ControlBits, byte int1ControlBits
        LIS302_ConfigR3 (false, false, 0, 0);



        delay (100);
        int val;


        // Who am I?
        val = LIS302_Read ('w');
        Serial.print(" Who Am I: ");
        Serial.print(val, HEX);
        Serial.print(" (should be '3B')"); // should be 0x3B
        Serial.println("");


        // Config1
        val = LIS302_Read ('a');
        Serial.print(" Config1: ");
        Serial.print(val, BIN);
        Serial.print(" (default: 00000111)");
        Serial.println("");
        // Config2
        val = LIS302_Read ('b');
        Serial.print(" Config2: ");
        Serial.print(val, BIN);
        Serial.print(" (default: 00000000)");
        Serial.println("");
        // Config3
        val = LIS302_Read ('c');
        Serial.print(" Config3: ");
        Serial.print(val, BIN);
        Serial.print(" (default: 00000000)");
        Serial.println("");

}



//
// Loop

void loop() {

        // Mesure Acceleration
        byte val_x = LIS302_Read ('x');
        byte val_y = LIS302_Read ('y');
        byte val_z = LIS302_Read ('z');
        byte val_xg = LIS302_ParseToG (val_x);
        byte val_yg = LIS302_ParseToG (val_y);
        byte val_zg = LIS302_ParseToG (val_z);
        Serial.print("  x: "); Serial.print(val_xg); Serial.print(" mG ("); Serial.print(val_x); Serial.print(")");
        Serial.print("  y: "); Serial.print(val_yg); Serial.print(" mG ("); Serial.print(val_y); Serial.print(")");
        Serial.print("  z: "); Serial.print(val_zg); Serial.print(" mG ("); Serial.print(val_z); Serial.print(")");
        Serial.println("");


        // Print state
        byte state = LIS302_Read('s');
        LIS302_PrintState(state);
        Serial.println("");


        // Delay
        delay (100);

}